Commit 18ac3754f0238b3de897dd32d8f6a689bcab1ae5

Authored by totoro
1 parent 8abaa6f5

add reference of the foreign table in foreign_text

generate extractor list for json
add get_all to_json with selection of the columns to be extracted
remove vt_view_db_call according to the hayamiki lib
src/generation/SQL_get_all.anubis
@@ -53,6 +53,19 @@ public define Maybe(One) @@ -53,6 +53,19 @@ public define Maybe(One)
53 success(_) then success(unique) 53 success(_) then success(unique)
54 }. 54 }.
55 55
  56 +//public define JsonMember
  57 +// get_component
  58 +// (
  59 +// String component_name,
  60 +// Crm_price_level type
  61 +// )=
  62 +// if component_name = "id" then json_member(component_name, json_int(type.id))
  63 +// else if component_name = "code" then json_member(component_name, json_string(type.code))
  64 +// else if component_name = "label" then json_member(component_name, json_string(type.label))
  65 +// else if component_name = "active" then json_member(component_name, json_bool(type.active))
  66 +// else if component_name = "module" then json_member(component_name, json_string(type.module))
  67 +// else json_member(component_name, json_null)
  68 +//.
56 69
57 public define Maybe(One) 70 public define Maybe(One)
58 generate_get_all_with_clause 71 generate_get_all_with_clause
@@ -110,10 +123,27 @@ public define Maybe(One) @@ -110,10 +123,27 @@ public define Maybe(One)
110 " success(data) then _get_all_"+name+"_to_json(db, table_cursor, [data . so_far])\n"+ 123 " success(data) then _get_all_"+name+"_to_json(db, table_cursor, [data . so_far])\n"+
111 " }\n"+ 124 " }\n"+
112 ".\n"+ 125 ".\n"+
113 -"public define JsonMember\n"+ 126 +
  127 +"public define List(JsonValue)\n"+
  128 +" _get_all_"+name+"_to_json\n"+
  129 +" (\n"+
  130 +" SQLite3DataBase db,\n"+
  131 +" List(((Int -> SQLite3Datum) -> JsonMember)) extractors,\n"+
  132 +" One -> SQLite3Row table_cursor,\n"+
  133 +" List(JsonValue) so_far\n"+
  134 +" ) =\n"+
  135 +" if extract_"+name+"_cursor_to_json(db, extractors, table_cursor) is\n"+
  136 +" {\n"+
  137 +" failure then reverse(so_far),\n"+
  138 +" success(data) then _get_all_"+name+"_to_json(db, extractors, table_cursor, [data . so_far])\n"+
  139 +" }\n"+
  140 +".\n"+
  141 +
  142 +"public define JsonValue\n"+
114 " get_all_"+name+"_by_clause_to_json\n"+ 143 " get_all_"+name+"_by_clause_to_json\n"+
115 " (\n"+ 144 " (\n"+
116 " SQLite3DataBase db,\n"+ 145 " SQLite3DataBase db,\n"+
  146 +" List(String) columns,\n"+
117 " HK_Referer referer,\n"+ 147 " HK_Referer referer,\n"+
118 " String filter,\n"+ 148 " String filter,\n"+
119 " String order_by\n"+ 149 " String order_by\n"+
@@ -122,8 +152,14 @@ public define Maybe(One) @@ -122,8 +152,14 @@ public define Maybe(One)
122 " with _and_ = if length(ref_clause) > 0 & length(filter) > 0 then \" AND \" else \"\",\n"+ 152 " with _and_ = if length(ref_clause) > 0 & length(filter) > 0 then \" AND \" else \"\",\n"+
123 " if get_all_"+name+"_by_clause(db, ref_clause+_and_+filter+\" \"+order_by) is\n"+ 153 " if get_all_"+name+"_by_clause(db, ref_clause+_and_+filter+\" \"+order_by) is\n"+
124 " {\n"+ 154 " {\n"+
125 -" failure then json_member(\"rows\", json_array([])),\n"+  
126 -" success(cursor) then json_member(\"rows\", json_array(_get_all_"+name+"_to_json(db, cursor, [])))\n"+ 155 +" failure then json_array([]),\n"+
  156 +" success(cursor) then\n"+
  157 +" json_array(\n"+
  158 +" if length(columns) = 0 then\n"+
  159 +" _get_all_"+name+"_to_json(db, cursor, [])\n"+
  160 +" else\n"+
  161 +" _get_all_"+name+"_to_json(db, make_json_extractor(db, columns), cursor, [])\n"+
  162 +" )\n"+
127 " }\n"+ 163 " }\n"+
128 ".\n" 164 ".\n"
129 ) is //end of the function 165 ) is //end of the function
src/generation/columns.anubis
@@ -242,10 +242,10 @@ public define Maybe(String) @@ -242,10 +242,10 @@ public define Maybe(String)
242 }, 242 },
243 foreign_key(app,foreign_table,fields) then 243 foreign_key(app,foreign_table,fields) then
244 if length(fields) = 0 then 244 if length(fields) = 0 then
245 - success("foreign_text(\""+to_upper(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_name+", hk_form_selector(get_selector_"+foreign_table+"), "+to_Anubis_source(help)+")") 245 + success("foreign_text(\""+to_upper(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_name+", \""+foreign_table+"\", hk_form_selector(get_selector_"+foreign_table+"), "+to_Anubis_source(help)+")")
246 else 246 else
247 with search_fields = join(", ", map((String field) |-> "\""+field+"\"",fields)), 247 with search_fields = join(", ", map((String field) |-> "\""+field+"\"",fields)),
248 - success("foreign_text_search(\""+to_upper(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_name+", \""+foreign_table+"\", ["+search_fields+"]"+to_Anubis_source(help)+")"), 248 + success("foreign_text_search(\""+to_upper(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_name+", \""+foreign_table+"\", ["+search_fields+"], "+to_Anubis_source(help)+")"),
249 249
250 integer_field(choices) then 250 integer_field(choices) then
251 //success("integer(\""+to_upper(name)+"\", \""+name+"\", "+type_name+"."+name+", "+to_Anubis_source(help)+")"), 251 //success("integer(\""+to_upper(name)+"\", \""+name+"\", "+type_name+"."+name+", "+to_Anubis_source(help)+")"),
src/generation/extractor.anubis
@@ -116,7 +116,7 @@ define List(String) @@ -116,7 +116,7 @@ define List(String)
116 to_json_member(t, cursor_name, [line . so_far], idx + 1 ) 116 to_json_member(t, cursor_name, [line . so_far], idx + 1 )
117 } 117 }
118 . 118 .
119 - 119 +
120 public define String 120 public define String
121 generate_json_object_from_cursor 121 generate_json_object_from_cursor
122 ( 122 (
@@ -125,7 +125,40 @@ public define String @@ -125,7 +125,40 @@ public define String
125 List(HK_Model_Column) columns, 125 List(HK_Model_Column) columns,
126 String indent 126 String indent
127 )= 127 )=
128 - indent+"json_object([\n"+indent+" "+join(",\n"+indent+" ", to_json_member(columns, cursor_name, [], 0))+"\n"+indent+"])". 128 + indent+"json_object([\n"+indent+" "+join(",\n"+indent+" ", to_json_member(columns, cursor_name, [], 0))+"\n"+indent+"])"
  129 +.
  130 +
  131 +
  132 +define List(String)
  133 +/**
  134 + */
  135 + to_get_extractor_line_json_member
  136 + (
  137 + List(HK_Model_Column) columns,
  138 + String cursor_name,
  139 + List(String) so_far,
  140 + Int idx
  141 + )=
  142 + if columns is
  143 + {
  144 + [] then reverse(so_far),
  145 + [h . t ] then
  146 + since h is hk_column(name, col_type, _, _),
  147 + with line = "if component_name =\""+name+"\" then ((Int -> SQLite3Datum) cursor) |-> "+from_DB_cursor_to_JSON(h, cursor_name, idx),
  148 + to_get_extractor_line_json_member(t, cursor_name, [line . so_far], idx + 1 )
  149 + }
  150 +.
  151 +
  152 +public define String
  153 + generate_json_extractor
  154 + (
  155 + String constructor_name,
  156 + String cursor_name,
  157 + List(HK_Model_Column) columns,
  158 + String indent
  159 + )=
  160 + indent+join("\n"+indent+"else ", to_get_extractor_line_json_member(columns, cursor_name, [], 0))+
  161 + "\n"+indent+"else ((Int -> SQLite3Datum) cursor) |-> json_member(component_name, json_null)".
129 162
130 163
131 public define Maybe(One) 164 public define Maybe(One)
@@ -153,7 +186,42 @@ public define Maybe(One) @@ -153,7 +186,42 @@ public define Maybe(One)
153 " no_more_row then failure,\n"+ 186 " no_more_row then failure,\n"+
154 " row(cursor) then\n"+ 187 " row(cursor) then\n"+
155 " success("+generate_json_object_from_cursor(name, "cursor", columns, " ")+")\n"+ //generate type construction with values got from web arg 188 " success("+generate_json_object_from_cursor(name, "cursor", columns, " ")+")\n"+ //generate type construction with values got from web arg
156 - " }\n.\n" 189 + " }\n.\n"+
  190 + "\n\n"+
  191 + "public define Maybe(JsonValue)\n"+
  192 + " extract_"+name+"_cursor_to_json\n"+
  193 + " (\n"+
  194 + " SQLite3DataBase db,\n"+
  195 + " List(((Int -> SQLite3Datum) -> JsonMember)) extractors,\n"+
  196 + " One -> SQLite3Row table_cursor\n"+
  197 + " ) =\n"+
  198 + " if table_cursor(unique) is\n"+
  199 + " {\n"+
  200 + " error(sql_error) then println(db_error(sql_error, \"extract_"+name+"\")); failure,\n"+
  201 + " no_more_row then failure,\n"+
  202 + " row(cursor) then\n"+
  203 + " success(\n"+
  204 + " json_object(\n"+
  205 + " map(((Int -> SQLite3Datum) -> JsonMember extractor) |-> extractor(cursor) , extractors)\n"+
  206 + " ))\n"+
  207 + " }\n.\n"+
  208 +
  209 +"define (Int -> SQLite3Datum) -> JsonMember\n"+
  210 +" get_extractor\n"+
  211 +" (\n"+
  212 +" SQLite3DataBase db,\n"+
  213 +" String component_name,\n"+
  214 +" )=\n"+generate_json_extractor(name, "cursor", columns, " ")+
  215 +"\n.\n\n"+
  216 +
  217 +"define List(((Int -> SQLite3Datum) -> JsonMember))\n"+
  218 +" make_json_extractor\n"+
  219 +" (\n"+
  220 +" SQLite3DataBase db,\n"+
  221 +" List(String) columns\n"+
  222 +" )=\n"+
  223 +" map((String column) |-> get_extractor(db, column), columns)\n"+
  224 +".\n"
157 ) is //end of the function 225 ) is //end of the function
158 { 226 {
159 failure then println("can't write generate_extract_db_cursor "+type_name); failure, 227 failure then println("can't write generate_extract_db_cursor "+type_name); failure,
src/generation/files.anubis
@@ -70,6 +70,7 @@ define Maybe(One) @@ -70,6 +70,7 @@ define Maybe(One)
70 if generate_get_all_with_clause(strm, table) is { failure then failure, success(_) then 70 if generate_get_all_with_clause(strm, table) is { failure then failure, success(_) then
71 if generate_get_one(strm, table) is { failure then failure, success(_) then 71 if generate_get_one(strm, table) is { failure then failure, success(_) then
72 if generate_get_one_with_clause(strm, table) is { failure then failure, success(_) then 72 if generate_get_one_with_clause(strm, table) is { failure then failure, success(_) then
  73 + if generate_get_one_json(strm, table) is { failure then failure, success(_) then
73 if generate_get_count_by_clause(strm, table) is { failure then failure, success(_) then 74 if generate_get_count_by_clause(strm, table) is { failure then failure, success(_) then
74 if generate_update(strm, table) is { failure then failure, success(_) then 75 if generate_update(strm, table) is { failure then failure, success(_) then
75 if generate_delete(strm, table) is { failure then failure, success(_) then 76 if generate_delete(strm, table) is { failure then failure, success(_) then
@@ -78,7 +79,7 @@ define Maybe(One) @@ -78,7 +79,7 @@ define Maybe(One)
78 if generate_choices_selector(strm, table) is { failure then failure, success(_) then 79 if generate_choices_selector(strm, table) is { failure then failure, success(_) then
79 if generate_VT_all_view(strm, table) is { failure then failure, success(_) then 80 if generate_VT_all_view(strm, table) is { failure then failure, success(_) then
80 if generate_VT_all_edit(strm, table) is { failure then failure, success(_) then 81 if generate_VT_all_edit(strm, table) is { failure then failure, success(_) then
81 - success(unique)}}}}}}}}}}}}}}}}}}}}} 82 + success(unique)}}}}}}}}}}}}}}}}}}}}}}
82 }. 83 }.
83 84
84 define One 85 define One
@@ -210,6 +211,7 @@ define One @@ -210,6 +211,7 @@ define One
210 update_"+name+"_from_fields, 211 update_"+name+"_from_fields,
211 //Read 212 //Read
212 get_count_"+name+"_by_clause, 213 get_count_"+name+"_by_clause,
  214 + get_one_"+name+"_to_json,
213 get_all_"+name+"_by_clause_to_json, 215 get_all_"+name+"_by_clause_to_json,
214 //Delete 216 //Delete
215 delete_"+name+ 217 delete_"+name+
src/generation/types.anubis
@@ -88,7 +88,7 @@ public define Maybe(One) @@ -88,7 +88,7 @@ public define Maybe(One)
88 88
89 //generate the type of the table that contain all components 89 //generate the type of the table that contain all components
90 if write_string(stream, 90 if write_string(stream,
91 - "\n\ndefine "+type_name+"\n get_default_"+name+" =\n "+name+"("+ //head of the definition 91 + "\n\npublic define "+type_name+"\n get_default_"+name+" =\n "+name+"("+ //head of the definition
92 join(", ",_generate_get_default(columns, []))+ //all components of the type 92 join(", ",_generate_get_default(columns, []))+ //all components of the type
93 ").\n\n") is //end of the function 93 ").\n\n") is //end of the function
94 { 94 {
@@ -174,6 +174,59 @@ public define Maybe(One) @@ -174,6 +174,59 @@ public define Maybe(One)
174 failure then println("can't write generate_get_one "+type_name); failure, 174 failure then println("can't write generate_get_one "+type_name); failure,
175 success(_) then success(unique) 175 success(_) then success(unique)
176 }. 176 }.
  177 +
  178 +public define Maybe(One)
  179 + generate_get_one_json
  180 + (
  181 + Stream stream,
  182 + HK_Table table
  183 + )=
  184 + since table is hk_table(name, short_name, model, _),
  185 + since model is hk_model(columns, show_string),
  186 + with type_name = to_upper(name, 1), //make the first character to upper case to able to be a Anubis Type.
  187 +
  188 + //generate the type of the table that contain all components
  189 + if write_string(stream,
  190 + "\n\n"+
  191 + //SQL query
  192 +"public define JsonValue\n"+
  193 +" get_one_"+name+"_to_json\n"+
  194 +" (\n"+
  195 +" SQLite3DataBase db,\n"+
  196 +" List(String) columns,\n"+
  197 +" DB_id idx\n"+
  198 +" )=\n"+
  199 +" with final_columns = if length(columns) = 0 then "+name+"_columns else columns,\n"+
  200 +" with error = json_object(map((String col_name) |-> json_member(col_name, json_null), final_columns)),\n"+
  201 +"\n"+
  202 +" if idx is\n"+
  203 +" {\n"+
  204 +" none then error,\n"+
  205 +" db_id(_idx) then\n"+
  206 +" if get_all_"+name+"_by_clause(db, \"id=\"+to_decimal(_idx)) is\n"+
  207 +" {\n"+
  208 +" failure then error,\n"+
  209 +" success(cursor) then\n"+
  210 +" if length(columns) = 0 then\n"+
  211 +" if extract_"+name+"_cursor_to_json(db, cursor) is\n"+
  212 +" {\n"+
  213 +" failure then error,\n"+
  214 +" success(result) then result\n"+
  215 +" }\n"+
  216 +" else\n"+
  217 +" if extract_"+name+"_cursor_to_json(db, make_json_extractor(db, columns), cursor) is\n"+
  218 +" {\n"+
  219 +" failure then error,\n"+
  220 +" success(result) then result\n"+
  221 +" }\n"+
  222 +" }\n"+
  223 +" }\n"+
  224 +".\n\n"
  225 + ) is //end of the function
  226 + {
  227 + failure then println("can't write generate_get_one "+type_name); failure,
  228 + success(_) then success(unique)
  229 + }.
177 230
178 public define Maybe(One) 231 public define Maybe(One)
179 generate_get_one_with_clause 232 generate_get_one_with_clause
src/generation/vt_view.anubis
@@ -156,12 +156,12 @@ public define Maybe(One) @@ -156,12 +156,12 @@ public define Maybe(One)
156 " \""+table_name+"\", //Name of the database table\n"+ 156 " \""+table_name+"\", //Name of the database table\n"+
157 " \""+view_name+"\", //Name of the view\n"+ 157 " \""+view_name+"\", //Name of the view\n"+
158 " model,\n"+ 158 " model,\n"+
159 - " header,\n"+  
160 -" vt_view_db_calls(\n"+  
161 -" get_count_"+table_name+"_by_clause,\n"+  
162 -" get_all_"+table_name+"_by_clause_to_json\n"+  
163 -//" get_"+xxx+"_rows\n"+  
164 -" )\n"+ 159 + " header\n"+
  160 +//" vt_view_db_calls(\n"+
  161 +//" get_count_"+table_name+"_by_clause,\n"+
  162 +//" get_all_"+table_name+"_by_clause_to_json\n"+
  163 +////" get_"+xxx+"_rows\n"+
  164 +//" )\n"+
165 " )\n"+ 165 " )\n"+
166 ".\n\n" 166 ".\n\n"
167 167