Commit 55c024e5aed2865bbb2a6fa8f8ebc70ec8fe7cf1

Authored by totoro
1 parent b1ffa9d4

add get_relation to get final type in relation A <-> B, just choose which side …

…you need and provide the type get_all function
controller/hk_controller.anubis
... ... @@ -8,7 +8,7 @@
8 8  
9 9 read tools/basis.anubis
10 10 transmit hayamiki_lib/types/hayamiki.anubis
11   -transmit hayamiki_lib/controller/hk_delete.anubis
  11 +//transmit hayamiki_lib/controller/hk_delete.anubis
12 12 transmit hayamiki_lib/controller/hk_trigger.anubis
13 13  
14 14 transmit kayamiki_lib/controller/hk_rows_import.anubis
... ... @@ -31,6 +31,7 @@ public type HK_Table_controller:
31 31 (SQLite3DataBase db, List(Web_arg) lwa) -> Maybe(Int) write_from_fields,
32 32 //Read
33 33 (SQLite3DataBase db, String clause) -> Int get_count,
  34 + //(SQLite3DataBase db, String clause) -> List($T) get_all_by_clause_to_list_type,
34 35 (SQLite3DataBase db, List(String) columns, Bool resolve_fk, DB_id) -> JsonValue get_one_json,
35 36 (SQLite3DataBase db, List(String) columns, Bool resolve_fk, HK_Referer referer, String clause, String order_by) -> JsonValue get_rows_json,
36 37 //Update
... ...
controller/hk_sql_utils.anubis
... ... @@ -9,6 +9,7 @@
9 9 read data_base/sqlite.anubis
10 10 read calexium_lib/database/db_utils.anubis
11 11 read hayamiki_lib/types/hayamiki.anubis
  12 +transmit hayamiki_lib/controller/hk_controller.anubis
12 13  
13 14 public define String
14 15 referer_to_sql_clause
... ... @@ -95,6 +96,62 @@ public define List(Int)
95 96 }
96 97 .
97 98  
  99 +public define List(Int)
  100 + get_ids_by_clause
  101 + (
  102 + SQLite3DataBase db,
  103 + String table_name,
  104 + String id_col,
  105 + String clause
  106 + )=
  107 + with final_clause = if clause = "" then
  108 + ""
  109 + else
  110 + "WHERE "+clause,
  111 +
  112 + if sql_query_timeout(db, "SELECT \""+id_col+"\" FROM "+table_name+" "+final_clause+";", [], "get_ids_by_clause") is
  113 + {
  114 + error(_) then [],
  115 + ok(_, cursor, _) then db_get_integer_list(cursor)
  116 + }
  117 +.
  118 + relation_table
  119 + id = id of that relation
  120 + source_id = id of the first record in relation
  121 + target_id = id of the second record in relation
  122 +
  123 + for example we search the all record targeted by source id
  124 +
  125 + relation_id | source_id | target_id
  126 + -----------------------------------
  127 + 0 | 1 | 1
  128 + 1 | 1 | 2
  129 + 2 | 1 | 4
  130 + 3 | 0 | 2
  131 +
  132 + if we want to get all relation from source matching with id 1 we will get a list of ids [1, 2, 4]
  133 +
  134 +
  135 +public define List($T)
  136 + get_relation
  137 + (
  138 + SQLite3DataBase db,
  139 + String relation_table,
  140 + String relation_source_id_col,
  141 + String relation_target_id_col,
  142 + 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
  143 + String foreign_order,
  144 + (SQLite3DataBase db, String clause) -> List($T) get_all_by_clause_to_list_type
  145 + )=
  146 + //get
  147 + with ids = get_ids_by_clause(db, relation_table, relation_target_id_col, relation_source_id_col+" = "+relation_condition),
  148 + if length(ids) = 0 then
  149 + []
  150 + else
  151 + with the_clause = " id IN ("+join(", ", to_List_String(ids))+") "+foreign_order,
  152 + get_all_by_clause_to_list_type(db, the_clause)
  153 +.
  154 +
98 155 public define Maybe(Int)
99 156 insert_or_update
100 157 (
... ...
view/view_table_renderer.anubis
... ... @@ -655,7 +655,7 @@ public define List(CXM_Form_Field)
655 655 foreign_text(s_name, c_name, value, foreign_table_name, select, help) then
656 656 with uid = "_"+generate_random_string(20)+"_",
657 657 with refresh_func = uid+foreign_table_name+"_"+c_name+"_selector_refresh",
658   - with load_script = script("text/javascript","function "+refresh_func+"() { CalexiumToolBox.ajax_load_content('"+uid+foreign_table_name+"_"+c_name+"_selector', '?aws_controller=hk_c&aws_action=selector&table_name="+foreign_table_name+"&fk_filter="+fk_clause+"&selected="+value+"');} "+refresh_func+"(); "),
  658 + with load_script = script("text/javascript","function "+refresh_func+"() { CalexiumToolBox.ajax_load_content('"+uid+foreign_table_name+"_"+c_name+"_selector', '?aws_controller=hk_c&aws_action=selector&table_name="+foreign_table_name+"&fk_filter="+fk_clause+"&selected="+value+"&"+to_Web_arg(table_referer, "")+"');} "+refresh_func+"(); "),
659 659 [
660 660 partial(jq_dialog_create(dialog_id(uid+foreign_table_name+"_dlg_ajax"), ajax(controller_action("hk_c", "ajax_edit_table&dialog_id="+uid+foreign_table_name+"_dlg_ajax&sub_dialog=true&table_name="+foreign_table_name+"&after_close_cb="+refresh_func)))),
661 661 one_line_fields([
... ...