diff --git a/controller/hk_sql_utils.anubis b/controller/hk_sql_utils.anubis index 80725e7..3ce21f5 100644 --- a/controller/hk_sql_utils.anubis +++ b/controller/hk_sql_utils.anubis @@ -115,6 +115,171 @@ public define List(Int) ok(_, cursor, _) then db_get_integer_list(cursor) } . + +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_integer(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 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) + } +. + +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 (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 @@ -152,6 +317,103 @@ public define List($T) 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 ( diff --git a/view/types/hk_view.anubis b/view/types/hk_view.anubis index 05eff65..1e010d7 100644 --- a/view/types/hk_view.anubis +++ b/view/types/hk_view.anubis @@ -106,7 +106,7 @@ public type VT_view: public type HK_Form_action: new_entry, - edit_entry(String idx). + edit_entry(DB_id idx). public define String default_select = "--------------------". //20 dash public define (List(CoreAttrs),WebArgValue,String) selector_default_entry = ([], wav("none"),default_select). -- libgit2 0.21.4