hk_sql_utils.anubis 23.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
/*
 * Created by PyramIDE.
 * User: フランスのトトロ aka (David RENÉ) 
 * Date: 02/04/2017
 * Time: 19:23
 * © David RENÉ
 */

read data_base/sqlite.anubis
read xlib/database/db_utils.anubis
read hayamiki_lib/types/hayamiki.anubis
transmit hayamiki_lib/controller/hk_controller.anubis

public define String
  referer_to_sql_clause
  (
    HK_Referer    referer,
    String        table_name,
    List(String)  table_columns
  )=
  if referer is
  {
    no_referer                                                     then " (1 = 1) ",
    hk_referer(referer_table, referer_row_id, fk_table, fk_column) then
      if referer_table = table_name then
        "id = " +referer_row_id
      else if fk_column:table_columns then
        fk_column + " = " + referer_row_id
      else
        " (1 = 1) ",
  }
.

public define Maybe(One)
  delete_from_ids
  (
    SQLite3DataBase db,
    String          table_name,    
    List(Int)       ids
  )=
  if length(ids) = 0 then 
    success(unique)
  else
    if sql_query_timeout(db,
          "DELETE FROM "+table_name+
          " WHERE id IN ("+join(", ", to_List_String(ids))+");",
           [],
          "delete_from_ids") is
    {
      error(_)    then failure,
      ok(_, _, _) then success(unique)
    }
  .

public define Maybe(One)
  delete_by_clause
  (
    SQLite3DataBase db,
    String          table_name,    
    String          clause
  )=
  if sql_query_timeout(db,
        "DELETE FROM "+table_name+
        " "+ clause +";",
         [],
        "delete_by_clause") is
  {
    error(_)    then failure,
    ok(_, _, _) then success(unique)
  }.
  
public define Int
  get_count_by_full_clause
  (
    SQLite3DataBase db,
    String          clause
  )=
  if sql_query_timeout(db, clause+";", [], "get_count_by_clause") is
  {
    error(sql_error) then println("get_count_by_full_clause SQL ERROR '"+ sql_error.text + "'\n clause ["+clause+";]");0,
    ok(_, cursor, _) then /*println("get_count_by_full_clause OK ["+clause+";]");*/db_get_Int(cursor)
  }
.

public define Int
  db_get_Int
  (
    SQLite3DataBase   db,
    String            clause,
    List(SQLite3Bind) binds
  )=
  if sql_query_timeout(db, clause+";", binds, "db_get_Int") is
  {
    error(sql_error) then println("db_get_Int SQL ERROR '"+ sql_error.text + "'\n clause ["+clause+";]");0,
    ok(_, cursor, _) then db_get_Int(cursor)
  }
.

public define Int
  db_get_Int
  (
    SQLite3DataBase   db,
    String            clause
  )=
  db_get_Int(db, clause, [])
.

public define List(String)
  db_get_List_String
  (
    SQLite3DataBase   db,
    String            clause,
    List(SQLite3Bind) binds
  )=
  if sql_query_timeout(db, clause+";", binds, "db_get_List_String") is
  {
    error(sql_error) then println("db_get_List_String SQL ERROR '"+ sql_error.text + "'\n clause ["+clause+";]");[],
    ok(_, cursor, _) then db_get_List_String(cursor)
  }
.

public define List(String)
  db_get_List_String
  (
    SQLite3DataBase   db,
    String            clause
  )=
  db_get_List_String(db, clause, [])
.

public define Float
  get_count_float_by_full_clause
  (
    SQLite3DataBase db,
    String          clause
  )=
  if sql_query_timeout(db, clause+";", [], "get_count_by_clause") is
  {
    error(sql_error) then println("get_count_by_full_clause SQL ERROR '"+ sql_error.text + "'\n clause ["+clause+";]");0.0,
    ok(_, cursor, _) then /*println("get_count_by_full_clause OK ["+clause+";]");*/db_get_Float(cursor)
  }
.

public define Float
  db_get_Float
  (
    SQLite3DataBase   db,
    String            clause,
    List(SQLite3Bind) binds
  )=
  if sql_query_timeout(db, clause+";", binds, "db_get_Float") is
  {
    error(sql_error) then println("db_get_Float SQL ERROR '"+ sql_error.text + "'\n clause ["+clause+";]");0.0,
    ok(_, cursor, _) then db_get_Float(cursor)
  }
.

public define Float
  db_get_Float
  (
    SQLite3DataBase   db,
    String            clause
  )=
  db_get_Float(db, clause, [])
.

public define Int
  get_count_by_clause
  (
    SQLite3DataBase db,
    String          table_name,
    String          clause,
    List(SQLite3Bind) binds
  )=
//  with final_clause = if clause = "" then
//                         ""
//                      else
//                        "WHERE "+clause,

  if sql_query_timeout(db, "SELECT COUNT(\""+table_name+".id\") FROM "+table_name+" "+clause+";", binds, "get_count_by_clause") is
  {
    error(sql_error) then println("get_count_by_clause SQL ERROR '"+ sql_error.text + "'\n table_name ["+table_name+"]\n clause ["+clause+";]");0,
    ok(_, cursor, _) then db_get_Int(cursor)
  }
.

public define Int
  get_count_by_clause
  (
    SQLite3DataBase db,
    String          table_name,
    String          clause
  )=
  get_count_by_clause(db, table_name, clause, [])
.

public define Int
  get_count
  (
    SQLite3DataBase db,
    String          table_name
  )=
  get_count_by_clause(db, table_name, "")
.


public define List(Int)
  get_ids_by_clause
  (
    SQLite3DataBase db,
    String          table_name,
    String          clause
  )=
  with final_clause = if clause = "" then
                         ""
                      else
                        "WHERE "+clause,

  if sql_query_timeout(db, "SELECT \"id\" FROM "+table_name+" "+final_clause+";", [], "get_ids_by_clause") is
  {
    error(sql_error) then println("get_ids_by_clause SQL ERROR '"+ sql_error.text + "'\n table_name ["+table_name+"]\n clause ["+clause+";]");[],
    ok(_, cursor, _) then db_get_List_Int(cursor)
  }
.

public define List(DB_id)
  get_db_ids_by_clause
  (
    SQLite3DataBase db,
    String          table_name,
    String          clause
  )=
  with final_clause = if clause = "" then
                         ""
                      else
                        "WHERE "+clause,

  if sql_query_timeout(db, "SELECT \"id\" FROM "+table_name+" "+final_clause+";", [], "get_db_ids_by_clause") is
  {
    error(sql_error) then println("get_db_ids_by_clause SQL ERROR '"+ sql_error.text + "'\n table_name ["+table_name+"]\n clause ["+clause+";]");[],
    ok(_, cursor, _) then db_get_List_DB_id(cursor)
  }
.

public define List(Int)
  get_ids_by_clause
  (
    SQLite3DataBase db,
    String          table_name,
    String          id_col,
    String          clause
  )=
  with final_clause = if clause = "" then
                         ""
                      else
                        "WHERE "+clause,

  if sql_query_timeout(db, "SELECT \""+id_col+"\" FROM "+table_name+" "+final_clause+";", [], "get_ids_by_clause") is
  {
    error(sql_error) then println("get_ids_by_clause SQL ERROR '"+ sql_error.text + "'\n table_name ["+table_name+"]\n id_col["+id_col+"]\nclause ["+clause+";]");[],
    ok(_, cursor, _) then db_get_List_Int(cursor)
  }
.

public define List(DB_id)
  get_db_ids_by_clause
  (
    SQLite3DataBase db,
    String          table_name,
    String          id_col,
    String          clause
  )=
  with final_clause = if clause = "" then
                         ""
                      else
                        "WHERE "+clause,

  if sql_query_timeout(db, "SELECT \""+id_col+"\" FROM "+table_name+" "+final_clause+";", [], "get_ids_by_clause") is
  {
    error(sql_error) then println("get_db_ids_by_clause SQL ERROR '"+ sql_error.text + "'\n table_name ["+table_name+"]\n id_col["+id_col+"]\nclause ["+clause+";]");[],
    ok(_, cursor, _) then db_get_List_DB_id(cursor)
  }
.

public define List(DB_id)
  get_db_ids_by_full_clause
  (
    SQLite3DataBase db,
    String          table_name,
    String          id_col,
    String          full_clause,
    List(SQLite3Bind) binds
  )=

  if sql_query_timeout(db, "SELECT \""+id_col+"\" FROM "+table_name+" "+full_clause+";", binds, "get_db_ids_by_full_clause") is
  {
    error(sql_error) then println("get_db_ids_by_full_clause SQL ERROR '"+ sql_error.text + "'\n table_name ["+table_name+"]\n id_col["+id_col+"]\nfull_clause ["+full_clause+";]");[],
    ok(_, cursor, _) then db_get_List_DB_id(cursor)
  }
.

public define List(DB_id)
  get_db_ids_by_full_clause
  (
    SQLite3DataBase db,
    String          table_name,
    String          id_col,
    String          full_clause
  )=
  get_db_ids_by_full_clause(db, table_name, id_col, full_clause, [])
.

public define Maybe(DB_id)
  get_db_id_by_full_clause
  (
    SQLite3DataBase db,
    String          table_name,
    String          id_col,
    String          full_clause,
    List(SQLite3Bind) binds
  )=
  with the_list = get_db_ids_by_full_clause(db, table_name, id_col, full_clause, binds),
  println("list size "+length(the_list));
  if the_list is
  {
    []      then failure,
    [h . _] then success(h)
  }
.
  
public define Maybe(Int)
  get_mb_id_by_clause
  (
    SQLite3DataBase db,
    String          table_name,
    String          id_col,    
    String          clause
  )=
  with final_clause = if clause = "" then
                         ""
                      else
                        "WHERE "+clause,

  if sql_query_timeout(db, "SELECT \""+id_col+"\" FROM "+table_name+" "+final_clause+";", [], "get_mb_id_by_clause") is
  {
    error(_)         then failure,
    ok(_, cursor, _) then db_get_mb_Int(cursor)
  }
.

public define Maybe(Int)
  get_mb_id_by_clause
  (
    SQLite3DataBase db,
    String          table_name,  
    String          clause
  )=
  get_mb_id_by_clause(db, table_name, "id", clause)
.

public define DB_id
  get_DB_id_by_clause
  (
    SQLite3DataBase db,
    String          table_name,  
    String          clause
  )=
  if get_mb_id_by_clause(db, table_name, "id", clause) is
  {
    failure     then none,
    success(id) then db_id(id)
  }
.

public define Maybe(One -> SQLite3Row)
  get_cursor_by_clause
  (
    SQLite3DataBase   db,
    String            table_name,
    String            fields,
    String            _join,
    String            _where,
    String            _order,
    String            _limit
  )=
  with where = if _where = "" then "" else " WHERE "+_where,
       join  = if _join  = "" then "" else " JOIN "+_join,

  if sql_query_timeout(db, "SELECT "+fields+" FROM "+table_name+join+where+" "+_order+" "+_limit+";", [], "get_cursor_by_clause") is
  {
    error(_)         then failure,
    ok(_, cursor, _) then success(cursor)
  }
.

public define (String, String, String, String) -> Maybe(One -> SQLite3Row)
  call_back_get_cursor_by_clause
  (
    SQLite3DataBase   db,
    String            table_name,
    String            fields
  )=
  with head = "SELECT "+fields+" FROM "+table_name,
  (
    String            _join,
    String            _where,
    String            _order,
    String            _limit
  )|->
  with where = if _where = "" then "" else " WHERE "+_where,
       join  = if _join  = "" then "" else " JOIN "+_join,

  if sql_query_timeout(db, head + join + where + " "+ _order + " " + _limit + ";", [], "get_cursor_by_clause") is
  {
    error(_)         then failure,
    ok(_, cursor, _) then success(cursor)
  }
.
 
 /***** JSON *****/

public define Maybe(JsonValue)
  extract_cursor_to_json
  (
    SQLite3DataBase                             db,
    List(((Int -> SQLite3Datum) -> JsonMember)) extractors,
    One -> SQLite3Row                           table_cursor
  ) =
  //get next cursor
  if table_cursor(unique) is
  {
    error(sql_error)  then println(db_error(sql_error, "extract_cursor_to_json")); failure,
    no_more_row       then failure,
    row(cursor)       then
      success(
        json_object(
          map(((Int -> SQLite3Datum) -> JsonMember extractor) |-> extractor(cursor) , extractors)
        ))
  }
.

public define List(JsonValue)
  get_all_to_json
  (
    SQLite3DataBase   db,
    List(((Int -> SQLite3Datum) -> JsonMember)) extractors,
    One -> SQLite3Row table_cursor,
    List(JsonValue)   so_far
  ) =
  if extract_cursor_to_json(db, extractors, table_cursor) is
  {
    failure       then reverse(so_far),
    success(data) then get_all_to_json(db, extractors, table_cursor, [data . so_far])
  }
.

  
 /***** Type $T ******/

define (One -> SQLite3Row, List($T)) ->List($T)
  _get_all
  (
    (One -> SQLite3Row table_cursor) -> Maybe($T) extractor
  )=
  (
    One -> SQLite3Row cursor,
    List($T)          so_far
  )|-loop->
  if extractor(cursor) is
  {
    failure       then reverse(so_far),
    success(data) then loop(cursor, [data . so_far])
  }
.

public define (One -> SQLite3Row table_cursor) -> List($T)
  get_all
  (
    (One -> SQLite3Row table_cursor) -> Maybe($T)       extractor
  )=
  with real_get = _get_all(extractor),
  (
    One -> SQLite3Row cursor
  ) |->
  real_get(cursor, [])
.

public define List($T)
  get_all_by_full_clause
  (
    SQLite3DataBase db,
    String          table_name,
    String          full_clause,
    (One -> SQLite3Row table_cursor) -> Maybe($T)       extractor
  )=
  
  if sql_query_timeout(db, "SELECT * FROM "+table_name+" "+full_clause+";", [], "get_all_by_full_clause") is
  {
    error(sql_error) then println("get_all_by_full_clause SQL ERROR '"+ sql_error.text + "'\n table_name ["+table_name+"]\n full_clause ["+full_clause+";]");[],
    ok(_, cursor, _) then _get_all(extractor)(cursor, [])
  }
.

public define List($T)
  get_all_by_full_clause
  (
    SQLite3DataBase db,
    String          full_clause,
    (One -> SQLite3Row table_cursor) -> Maybe($T)       extractor
  )=
  
  if sql_query_timeout(db, full_clause+";", [], "get_all_by_full_clause") is
  {
    error(sql_error) then println("get_all_by_full_clause SQL ERROR '"+ sql_error.text + "'\n full_clause ["+full_clause+";]");[],
    ok(_, cursor, _) then _get_all(extractor)(cursor, [])
  }
.

public define (String, String, String, String) -> List($T)
  call_back_get_all_by_clause
  (
    SQLite3DataBase   db,
    String            table_name,
    String            fields,
    (One -> SQLite3Row table_cursor) -> Maybe($T)       extractor
    //(One -> SQLite3Row table_cursor) -> List($T) list_extractor    
  )=
  with head = "SELECT "+fields+" FROM "+table_name,   //Construct the head of the SQL query
  with list_extractor = get_all(extractor),           //construct the call_back for the list of the type $T with the extractor
  (
    String            _join,
    String            _where,
    String            _order,
    String            _limit
  )|->
  with where = if _where = "" then "" else " WHERE "+_where,
       join  = if _join  = "" then "" else " JOIN "+_join,
       order = if _order = "" then "" else " ORDER BY "+_order,
 
  with final_clause = head + join + where + " "+ order + " " + _limit + ";",
  //println("call_back_get_all_by_clause Final clause ["+final_clause+"]");
  
  if sql_query_timeout(db, final_clause, [], "get_cursor_by_clause") is
  {
    error(_)         then [],
    ok(_, cursor, _) then list_extractor(cursor)
  }
.

public define (String, String, String, String) -> Maybe($T)
  call_back_get_one_by_clause
  (
    SQLite3DataBase   db,
    String            table_name,
    String            fields,
    (One -> SQLite3Row table_cursor) -> Maybe($T)       extractor
    //(One -> SQLite3Row table_cursor) -> List($T) list_extractor    
  )=
  with head = "SELECT "+fields+" FROM "+table_name,   //Construct the head of the SQL query
  (
    String            _join,
    String            _where,
    String            _order,
    String            _limit
  )|->
  with where = if _where = "" then "" else " WHERE "+_where,
       join  = if _join  = "" then "" else " JOIN "+_join,
       order = if _order = "" then "" else " ORDER BY "+_order,

  with final_clause = head + join + where + " "+ order + " " + _limit + ";",
  //println("call_back_get_one_by_clause Final clause ["+final_clause+"]");
  
  if sql_query_timeout(db, final_clause, [], "get_cursor_by_clause") is
  {
    error(_)         then failure,
    ok(_, cursor, _) then extractor(cursor)
  }
.

     relation_table
     id = id of that relation
     source_id = id of the first record in relation
     target_id = id of the second record in relation
     
     for example we search the all record targeted by source id
     
     relation_id | source_id | target_id
     -----------------------------------
     0           | 1         | 1
     1           | 1         | 2
     2           | 1         | 4
     3           | 0         | 2
     
     if we want to get all relation from source matching with id 1 we will get a list of ids [1, 2, 4]
     

public define List($T)
  get_relation
  (
    SQLite3DataBase db,
    String  relation_table,
    String  relation_source_id_col,
    String  relation_target_id_col,
    String  relation_condition,       //only the right part atfer = in SQL format. This means the SQL query will be constructed with "relation_source_id_col = " relation_condition
    String  foreign_order,
    (SQLite3DataBase db, String clause) -> List($T)       get_all_by_clause_to_list_type
  )=
  //get 
  with ids = get_ids_by_clause(db, relation_table, relation_target_id_col, relation_source_id_col+" = "+relation_condition),
  if length(ids) = 0 then 
    []
  else
    with the_clause = " id IN ("+join(", ", to_List_String(ids))+") "+foreign_order,
    get_all_by_clause_to_list_type(db, the_clause)
.

  /** Get relation by foreign table
     relation_table_to_f_table 
     get the relation from source_id 
     id = id of that relation
     source_id = id of the first record in relation
     f_table   = foreign table
     f_id      = id in foreign table for second leg of the relation
   */
public define List($T)
  get_relation_to_f_table
  (
    SQLite3DataBase db,
    String  relation_table,
    String  foreign_table_name,       //col f_table
    String  relation_condition,       //only the right part atfer = in SQL format. This means the SQL query will be constructed with "relation_source_id_col = " relation_condition
    String  foreign_order,
    (SQLite3DataBase db, String clause) -> List($T)       get_all_by_clause_to_list_type
  )=
  //get 
  with ids = get_ids_by_clause(db, relation_table, "f_id", "f_table = '"+foreign_table_name+"' AND fk_id = "+relation_condition),
  if length(ids) = 0 then 
    []
  else
    with the_clause = " id IN ("+join(", ", to_List_String(ids))+") "+foreign_order,
    get_all_by_clause_to_list_type(db, the_clause)
.

public define List($T)
  get_relation_from_f_table
  (
    SQLite3DataBase db,
    String  relation_table,
    String  foreign_table_name,       //col f_table
    String  relation_condition,       //only the right part atfer = in SQL format. This means the SQL query will be constructed with "relation_source_id_col = " relation_condition
    String  foreign_order,
    (SQLite3DataBase db, String clause) -> List($T)       get_all_by_clause_to_list_type
  )=
  //get 
  with ids = get_ids_by_clause(db, relation_table, "fk_id", "f_table = '"+foreign_table_name+"' AND f_id = "+relation_condition),
  if length(ids) = 0 then 
    []
  else
    with the_clause = " id IN ("+join(", ", to_List_String(ids))+") "+foreign_order,
    get_all_by_clause_to_list_type(db, the_clause)
.

public define Maybe($T)
  get_relation
  (
    SQLite3DataBase db,
    String  relation_table,
    String  relation_source_id_col,
    String  relation_target_id_col,
    String  relation_condition,       //only the right part atfer = in SQL format. This means the SQL query will be constructed with "relation_source_id_col = " relation_condition
    (SQLite3DataBase db, String clause) -> Maybe($T)       get_by_clause_to_mb_type
  )=
  //get 
  if get_mb_id_by_clause(db, relation_table, relation_target_id_col, relation_source_id_col+" = "+relation_condition) is
  {
    failure     then failure
    success(id) then get_by_clause_to_mb_type(db, "id = "+id)      
  }
.

public define Maybe($T)
  get_relation_to_f_table
  (
    SQLite3DataBase db,
    String  relation_table,           //table where is located the relation
    String  foreign_table_name,       //table name as filter for concerned relation
    String  relation_condition,       //only the right part atfer = in SQL format. This means the SQL query will be constructed with "relation_source_id_col = " relation_condition
    (SQLite3DataBase db, String clause) -> Maybe($T)       get_by_clause_to_mb_type
  )=

  if get_mb_id_by_clause(db, relation_table, "f_id", "f_table = '"+foreign_table_name+"' AND fk_id = "+relation_condition) is
  {
    failure     then failure
    success(id) then get_by_clause_to_mb_type(db, "id = "+id)      
  }
.

public define Maybe($T)
  get_relation_from_f_table
  (
    SQLite3DataBase db,
    String  relation_table,           //table where is located the relation
    String  foreign_table_name,       //table name as filter for concerned relation
    String  relation_condition,       //only the right part atfer = in SQL format. This means the SQL query will be constructed with "relation_source_id_col = " relation_condition
    (SQLite3DataBase db, String clause) -> Maybe($T)       get_by_clause_to_mb_type
  )=
  //get 
  if get_mb_id_by_clause(db, relation_table, "fk_id", "f_table = '"+foreign_table_name+"' AND f_id = "+relation_condition) is
  {
    failure     then failure
    success(id) then get_by_clause_to_mb_type(db, "id = "+id)      
  }
.

public define Maybe(Int)
  insert_or_update
  (
    SQLite3DataBase             db,
    String                      table_name,
    DB_id                       db_id,
    List(SQLite3_update_field)  fields
  )=
  if db_id is
  {
    none then
      since make_insert_clause_and_binds(fields) is (binds, insert_columns, insert_values),
      with insert_query = "INSERT INTO "+table_name+" "+insert_columns+" VALUES "+insert_values+";",
      if sql_query_timeout(db, insert_query, binds, table_name+" (INSERT)") is
      {
        error(sql_error)  then 
          println("insert on table '"+table_name+"' SQL ERROR '"+ sql_error.text + "'\n sql_insert ["+insert_query+"]\n binds "+dump_Binds(binds));
          failure,
        ok(_, cursor, _)  then 
          if sql_query_scalar(db, "SELECT last_insert_rowid()") is
          {
            error(_)  then  failure,
            ok(datum) then  
              with result1 = if datum is integer(n) then n else 0,
              println(table_name+" inserted id ["+result1+"]");
              success(result1)
          }
//    SQLite3DataBase        db, 
//    String                 sql_command
//  )        
//        success(unique)
      },
    db_id(idx) then
      since make_update_clause_and_binds(fields) is (binds, set_string),
      with update_query = "UPDATE "+table_name+" SET "+set_string +" WHERE id = "+idx+";",
      if sql_query_timeout(db, update_query, binds, table_name+" (UPDATE)") is
      {
        error(sql_error) then 
          println("update on table '"+table_name+"' id ["+idx+"] SQL ERROR '"+ sql_error.text + "'\n sql_insert ["+update_query+"]\n binds "+dump_Binds(binds));
          //logError(debug_log, db_error(sql_error, table_name));
          failure,
        ok(_, _, _)      then success(idx)
      }
  }
.

public define Maybe(One)
  update_with_clause
  (
    SQLite3DataBase             db,
    String                      table_name,
    String                      clause,
    List(SQLite3_update_field)  fields
  )=

  since make_update_clause_and_binds(fields) is (binds, set_string),
  if sql_query_timeout(db, "UPDATE "+table_name+" SET "+set_string +" "+clause+";", binds, table_name+" (UPDATE)") is
  {
    error(sql_error) then 
      //logError(debug_log, db_error(sql_error, table_name));
      failure,
    ok(_, _, _)      then success(unique)
  }
.