Commit 5b2625729fd48b277f5779aedd58cce918ade824
1 parent
57345f2a
add all management of relation
HK_Form_Action has DB_id in edit_entry instead of String
Showing
2 changed files
with
263 additions
and
1 deletions
Show diff stats
controller/hk_sql_utils.anubis
| @@ -115,6 +115,171 @@ public define List(Int) | @@ -115,6 +115,171 @@ public define List(Int) | ||
| 115 | ok(_, cursor, _) then db_get_integer_list(cursor) | 115 | ok(_, cursor, _) then db_get_integer_list(cursor) |
| 116 | } | 116 | } |
| 117 | . | 117 | . |
| 118 | + | ||
| 119 | +public define Maybe(Int) | ||
| 120 | + get_mb_id_by_clause | ||
| 121 | + ( | ||
| 122 | + SQLite3DataBase db, | ||
| 123 | + String table_name, | ||
| 124 | + String id_col, | ||
| 125 | + String clause | ||
| 126 | + )= | ||
| 127 | + with final_clause = if clause = "" then | ||
| 128 | + "" | ||
| 129 | + else | ||
| 130 | + "WHERE "+clause, | ||
| 131 | + | ||
| 132 | + if sql_query_timeout(db, "SELECT \""+id_col+"\" FROM "+table_name+" "+final_clause+";", [], "get_mb_id_by_clause") is | ||
| 133 | + { | ||
| 134 | + error(_) then failure, | ||
| 135 | + ok(_, cursor, _) then db_get_mb_integer(cursor) | ||
| 136 | + } | ||
| 137 | +. | ||
| 138 | + | ||
| 139 | +public define Maybe(Int) | ||
| 140 | + get_mb_id_by_clause | ||
| 141 | + ( | ||
| 142 | + SQLite3DataBase db, | ||
| 143 | + String table_name, | ||
| 144 | + String clause | ||
| 145 | + )= | ||
| 146 | + get_mb_id_by_clause(db, table_name, "id", clause) | ||
| 147 | +. | ||
| 148 | + | ||
| 149 | +public define Maybe(One -> SQLite3Row) | ||
| 150 | + get_cursor_by_clause | ||
| 151 | + ( | ||
| 152 | + SQLite3DataBase db, | ||
| 153 | + String table_name, | ||
| 154 | + String fields, | ||
| 155 | + String _join, | ||
| 156 | + String _where, | ||
| 157 | + String _order, | ||
| 158 | + String _limit | ||
| 159 | + )= | ||
| 160 | + with where = if _where = "" then "" else " WHERE "+_where, | ||
| 161 | + join = if _join = "" then "" else " JOIN "+_join, | ||
| 162 | + | ||
| 163 | + if sql_query_timeout(db, "SELECT "+fields+" FROM "+table_name+join+where+" "+_order+" "+_limit+";", [], "get_cursor_by_clause") is | ||
| 164 | + { | ||
| 165 | + error(_) then failure, | ||
| 166 | + ok(_, cursor, _) then success(cursor) | ||
| 167 | + } | ||
| 168 | +. | ||
| 169 | + | ||
| 170 | +public define (String, String, String, String) -> Maybe(One -> SQLite3Row) | ||
| 171 | + call_back_get_cursor_by_clause | ||
| 172 | + ( | ||
| 173 | + SQLite3DataBase db, | ||
| 174 | + String table_name, | ||
| 175 | + String fields | ||
| 176 | + )= | ||
| 177 | + with head = "SELECT "+fields+" FROM "+table_name, | ||
| 178 | + ( | ||
| 179 | + String _join, | ||
| 180 | + String _where, | ||
| 181 | + String _order, | ||
| 182 | + String _limit | ||
| 183 | + )|-> | ||
| 184 | + with where = if _where = "" then "" else " WHERE "+_where, | ||
| 185 | + join = if _join = "" then "" else " JOIN "+_join, | ||
| 186 | + | ||
| 187 | + if sql_query_timeout(db, head + join + where + " "+ _order + " " + _limit + ";", [], "get_cursor_by_clause") is | ||
| 188 | + { | ||
| 189 | + error(_) then failure, | ||
| 190 | + ok(_, cursor, _) then success(cursor) | ||
| 191 | + } | ||
| 192 | +. | ||
| 193 | + | ||
| 194 | +define (One -> SQLite3Row, List($T)) ->List($T) | ||
| 195 | + _get_all | ||
| 196 | + ( | ||
| 197 | + (One -> SQLite3Row table_cursor) -> Maybe($T) extractor | ||
| 198 | + )= | ||
| 199 | + ( | ||
| 200 | + One -> SQLite3Row cursor, | ||
| 201 | + List($T) so_far | ||
| 202 | + )|-loop-> | ||
| 203 | + if extractor(cursor) is | ||
| 204 | + { | ||
| 205 | + failure then reverse(so_far), | ||
| 206 | + success(data) then loop(cursor, [data . so_far]) | ||
| 207 | + } | ||
| 208 | +. | ||
| 209 | + | ||
| 210 | +public define (One -> SQLite3Row table_cursor) -> List($T) | ||
| 211 | + get_all | ||
| 212 | + ( | ||
| 213 | + (One -> SQLite3Row table_cursor) -> Maybe($T) extractor | ||
| 214 | + )= | ||
| 215 | + with real_get = _get_all(extractor), | ||
| 216 | + ( | ||
| 217 | + One -> SQLite3Row cursor | ||
| 218 | + ) |-> | ||
| 219 | + real_get(cursor, []) | ||
| 220 | +. | ||
| 221 | + | ||
| 222 | +public define (String, String, String, String) -> List($T) | ||
| 223 | + call_back_get_all_by_clause | ||
| 224 | + ( | ||
| 225 | + SQLite3DataBase db, | ||
| 226 | + String table_name, | ||
| 227 | + String fields, | ||
| 228 | + (One -> SQLite3Row table_cursor) -> Maybe($T) extractor | ||
| 229 | + //(One -> SQLite3Row table_cursor) -> List($T) list_extractor | ||
| 230 | + )= | ||
| 231 | + with head = "SELECT "+fields+" FROM "+table_name, //Construct the head of the SQL query | ||
| 232 | + with list_extractor = get_all(extractor), //construct the call_back for the list of the type $T with the extractor | ||
| 233 | + ( | ||
| 234 | + String _join, | ||
| 235 | + String _where, | ||
| 236 | + String _order, | ||
| 237 | + String _limit | ||
| 238 | + )|-> | ||
| 239 | + with where = if _where = "" then "" else " WHERE "+_where, | ||
| 240 | + join = if _join = "" then "" else " JOIN "+_join, | ||
| 241 | + order = if _order = "" then "" else " ORDER BY "+_order, | ||
| 242 | + | ||
| 243 | + with final_clause = head + join + where + " "+ order + " " + _limit + ";", | ||
| 244 | + //println("call_back_get_all_by_clause Final clause ["+final_clause+"]"); | ||
| 245 | + | ||
| 246 | + if sql_query_timeout(db, final_clause, [], "get_cursor_by_clause") is | ||
| 247 | + { | ||
| 248 | + error(_) then [], | ||
| 249 | + ok(_, cursor, _) then list_extractor(cursor) | ||
| 250 | + } | ||
| 251 | +. | ||
| 252 | + | ||
| 253 | +public define (String, String, String, String) -> Maybe($T) | ||
| 254 | + call_back_get_one_by_clause | ||
| 255 | + ( | ||
| 256 | + SQLite3DataBase db, | ||
| 257 | + String table_name, | ||
| 258 | + String fields, | ||
| 259 | + (One -> SQLite3Row table_cursor) -> Maybe($T) extractor | ||
| 260 | + //(One -> SQLite3Row table_cursor) -> List($T) list_extractor | ||
| 261 | + )= | ||
| 262 | + with head = "SELECT "+fields+" FROM "+table_name, //Construct the head of the SQL query | ||
| 263 | + ( | ||
| 264 | + String _join, | ||
| 265 | + String _where, | ||
| 266 | + String _order, | ||
| 267 | + String _limit | ||
| 268 | + )|-> | ||
| 269 | + with where = if _where = "" then "" else " WHERE "+_where, | ||
| 270 | + join = if _join = "" then "" else " JOIN "+_join, | ||
| 271 | + order = if _order = "" then "" else " ORDER BY "+_order, | ||
| 272 | + | ||
| 273 | + with final_clause = head + join + where + " "+ order + " " + _limit + ";", | ||
| 274 | + //println("call_back_get_one_by_clause Final clause ["+final_clause+"]"); | ||
| 275 | + | ||
| 276 | + if sql_query_timeout(db, final_clause, [], "get_cursor_by_clause") is | ||
| 277 | + { | ||
| 278 | + error(_) then failure, | ||
| 279 | + ok(_, cursor, _) then extractor(cursor) | ||
| 280 | + } | ||
| 281 | +. | ||
| 282 | + | ||
| 118 | relation_table | 283 | relation_table |
| 119 | id = id of that relation | 284 | id = id of that relation |
| 120 | source_id = id of the first record in relation | 285 | source_id = id of the first record in relation |
| @@ -152,6 +317,103 @@ public define List($T) | @@ -152,6 +317,103 @@ public define List($T) | ||
| 152 | get_all_by_clause_to_list_type(db, the_clause) | 317 | get_all_by_clause_to_list_type(db, the_clause) |
| 153 | . | 318 | . |
| 154 | 319 | ||
| 320 | + /** Get relation by foreign table | ||
| 321 | + relation_table_to_f_table | ||
| 322 | + get the relation from source_id | ||
| 323 | + id = id of that relation | ||
| 324 | + source_id = id of the first record in relation | ||
| 325 | + f_table = foreign table | ||
| 326 | + f_id = id in foreign table for second leg of the relation | ||
| 327 | + */ | ||
| 328 | +public define List($T) | ||
| 329 | + get_relation_to_f_table | ||
| 330 | + ( | ||
| 331 | + SQLite3DataBase db, | ||
| 332 | + String relation_table, | ||
| 333 | + String foreign_table_name, //col f_table | ||
| 334 | + 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 | ||
| 335 | + String foreign_order, | ||
| 336 | + (SQLite3DataBase db, String clause) -> List($T) get_all_by_clause_to_list_type | ||
| 337 | + )= | ||
| 338 | + //get | ||
| 339 | + with ids = get_ids_by_clause(db, relation_table, "f_id", "f_table = '"+foreign_table_name+"' AND fk_id = "+relation_condition), | ||
| 340 | + if length(ids) = 0 then | ||
| 341 | + [] | ||
| 342 | + else | ||
| 343 | + with the_clause = " id IN ("+join(", ", to_List_String(ids))+") "+foreign_order, | ||
| 344 | + get_all_by_clause_to_list_type(db, the_clause) | ||
| 345 | +. | ||
| 346 | + | ||
| 347 | +public define List($T) | ||
| 348 | + get_relation_from_f_table | ||
| 349 | + ( | ||
| 350 | + SQLite3DataBase db, | ||
| 351 | + String relation_table, | ||
| 352 | + String foreign_table_name, //col f_table | ||
| 353 | + 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 | ||
| 354 | + String foreign_order, | ||
| 355 | + (SQLite3DataBase db, String clause) -> List($T) get_all_by_clause_to_list_type | ||
| 356 | + )= | ||
| 357 | + //get | ||
| 358 | + with ids = get_ids_by_clause(db, relation_table, "fk_id", "f_table = '"+foreign_table_name+"' AND f_id = "+relation_condition), | ||
| 359 | + if length(ids) = 0 then | ||
| 360 | + [] | ||
| 361 | + else | ||
| 362 | + with the_clause = " id IN ("+join(", ", to_List_String(ids))+") "+foreign_order, | ||
| 363 | + get_all_by_clause_to_list_type(db, the_clause) | ||
| 364 | +. | ||
| 365 | + | ||
| 366 | +public define Maybe($T) | ||
| 367 | + get_relation | ||
| 368 | + ( | ||
| 369 | + SQLite3DataBase db, | ||
| 370 | + String relation_table, | ||
| 371 | + String relation_source_id_col, | ||
| 372 | + String relation_target_id_col, | ||
| 373 | + 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 | ||
| 374 | + (SQLite3DataBase db, String clause) -> Maybe($T) get_by_clause_to_mb_type | ||
| 375 | + )= | ||
| 376 | + //get | ||
| 377 | + if get_mb_id_by_clause(db, relation_table, relation_target_id_col, relation_source_id_col+" = "+relation_condition) is | ||
| 378 | + { | ||
| 379 | + failure then failure | ||
| 380 | + success(id) then get_by_clause_to_mb_type(db, "id = "+id) | ||
| 381 | + } | ||
| 382 | +. | ||
| 383 | + | ||
| 384 | +public define Maybe($T) | ||
| 385 | + get_relation_to_f_table | ||
| 386 | + ( | ||
| 387 | + SQLite3DataBase db, | ||
| 388 | + String relation_table, //table where is located the relation | ||
| 389 | + String foreign_table_name, //table name as filter for concerned relation | ||
| 390 | + 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 | ||
| 391 | + (SQLite3DataBase db, String clause) -> Maybe($T) get_by_clause_to_mb_type | ||
| 392 | + )= | ||
| 393 | + | ||
| 394 | + if get_mb_id_by_clause(db, relation_table, "f_id", "f_table = '"+foreign_table_name+"' AND fk_id = "+relation_condition) is | ||
| 395 | + { | ||
| 396 | + failure then failure | ||
| 397 | + success(id) then get_by_clause_to_mb_type(db, "id = "+id) | ||
| 398 | + } | ||
| 399 | +. | ||
| 400 | + | ||
| 401 | +public define Maybe($T) | ||
| 402 | + get_relation_from_f_table | ||
| 403 | + ( | ||
| 404 | + SQLite3DataBase db, | ||
| 405 | + String relation_table, //table where is located the relation | ||
| 406 | + String foreign_table_name, //table name as filter for concerned relation | ||
| 407 | + 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 | ||
| 408 | + (SQLite3DataBase db, String clause) -> Maybe($T) get_by_clause_to_mb_type | ||
| 409 | + )= | ||
| 410 | + //get | ||
| 411 | + if get_mb_id_by_clause(db, relation_table, "fk_id", "f_table = '"+foreign_table_name+"' AND f_id = "+relation_condition) is | ||
| 412 | + { | ||
| 413 | + failure then failure | ||
| 414 | + success(id) then get_by_clause_to_mb_type(db, "id = "+id) | ||
| 415 | + } | ||
| 416 | +. | ||
| 155 | public define Maybe(Int) | 417 | public define Maybe(Int) |
| 156 | insert_or_update | 418 | insert_or_update |
| 157 | ( | 419 | ( |
view/types/hk_view.anubis
| @@ -106,7 +106,7 @@ public type VT_view: | @@ -106,7 +106,7 @@ public type VT_view: | ||
| 106 | 106 | ||
| 107 | public type HK_Form_action: | 107 | public type HK_Form_action: |
| 108 | new_entry, | 108 | new_entry, |
| 109 | - edit_entry(String idx). | 109 | + edit_entry(DB_id idx). |
| 110 | 110 | ||
| 111 | public define String default_select = "--------------------". //20 dash | 111 | public define String default_select = "--------------------". //20 dash |
| 112 | public define (List(CoreAttrs),WebArgValue,String) selector_default_entry = ([], wav("none"),default_select). | 112 | public define (List(CoreAttrs),WebArgValue,String) selector_default_entry = ([], wav("none"),default_select). |