Commit 1a0e85ff8cbaa0f349d7e42874178b2bb62aca96

Authored by totoro
1 parent 6429553e

improve code generation for p_key

replace the now by db_now which was a change in Calexium lib. This avoid confusion in compilation time with primitive now
remove the generation of delete function in profit of hk_sql_utils delete
add trigger list in attached in table
in header generation, replace read by transmit
replace the function get_name by get_mb_name, where name is the type of line row of database entry. mb means Maybe type of name, because we are not sure to retrieve the data from row given index.
add function "from_web_arg_to_type_with_default" which decode the type component from web arg and fill with default value the missing component fields
calexium_lib @ 07f1bbce4bb
1   -Subproject commit 8b4b368f40fb62c5583b14081d817309aa56337a
  1 +Subproject commit 07f1bbce4bbd814d97625f59577e98fc4f8eceea
... ...
hayamiki_generator.aproj
... ... @@ -92,6 +92,7 @@
92 92 <Compile Include="calexium_lib\web\CXM_web_arg_encode.anubis" />
93 93 <Compile Include="calexium_lib\web\CXM_web_arg_utils.anubis" />
94 94 <Compile Include="calexium_lib\web\CXM_web_dump.anubis" />
  95 + <Compile Include="calexium_lib\web\CXM_web_session.anubis" />
95 96 <Compile Include="calexium_lib\web\CXM_widgets.anubis" />
96 97 <Compile Include="calexium_lib\web\CXM_xml_rpc.anubis" />
97 98 <Compile Include="calexium_lib\web\CXM_xml_rpc_parser.anubis" />
... ...
hayamiki_lib @ 57345f2acee
1   -Subproject commit 601f9714449737d52f05351db81e3c22fd0b27a7
  1 +Subproject commit 57345f2acee9ffeb19673b957d0aeb7296dfb742
... ...
src/generation/columns.anubis
... ... @@ -49,6 +49,7 @@ define List(String)
49 49 */
50 50 _to_List_String
51 51 (
  52 + String table_name,
52 53 List(HK_Model_Column) columns,
53 54 Bool with_pk, //if set, add the primary key named "id"
54 55 List(String) so_far
... ... @@ -59,9 +60,9 @@ define List(String)
59 60 [h . t ] then
60 61 since h is hk_column(name, _, _, _),
61 62 if name = "id" & with_pk = false then
62   - _to_List_String(t, with_pk, so_far)
  63 + _to_List_String(table_name, t, with_pk, so_far)
63 64 else
64   - _to_List_String(t, with_pk, [ name . so_far])
  65 + _to_List_String(table_name, t, with_pk, [ table_name+name . so_far])
65 66 }.
66 67  
67 68 public define List(String)
... ... @@ -72,9 +73,18 @@ public define List(String)
72 73 List(HK_Model_Column) columns,
73 74 Bool with_pk
74 75 )=
75   - _to_List_String(columns, with_pk, []).
76   -
  76 + _to_List_String("", columns, with_pk, []).
77 77  
  78 +public define List(String)
  79 +/* return the list of all columns name
  80 + */
  81 + to_List_String
  82 + (
  83 + String table_name,
  84 + List(HK_Model_Column) columns,
  85 + Bool with_pk
  86 + )=
  87 + _to_List_String(table_name+".", columns, with_pk, []).
78 88  
79 89 public define String
80 90 generate_constructor
... ... @@ -134,7 +144,7 @@ public define Maybe(String)
134 144 if col_type is
135 145 {
136 146 //anubis(_T) then "constant_byte_array(0,0)",
137   - p_key then failure,
  147 + p_key then if name = "id" then failure else success(name),
138 148 //binary then "constant_byte_array(0,0)",
139 149 boolean_field then success(name),
140 150 date_field(attrs) then success(name),
... ... @@ -316,7 +326,7 @@ public define String
316 326 since col is hk_column(name, col_type, _, _),
317 327 if col_type is
318 328 {
319   - p_key then "get_"+general_type_name+"_show_string(db, "+current_type_name+".id)",
  329 + p_key then if name ="id" then "get_"+general_type_name+"_show_string(db, "+current_type_name+".id)" else "to_String("+current_type_name+"."+name+")",
320 330 boolean_field then "to_String("+current_type_name+"."+name+")",
321 331 date_field(attrs) then "to_String("+current_type_name+"."+name+")",
322 332 time_field(attrs) then "to_String("+current_type_name+"."+name+")",
... ...
src/generation/create_table.anubis
... ... @@ -50,7 +50,7 @@ define String
50 50 Int fill_size
51 51 )=
52 52 since column is hk_column(name, col_type, attributes, _),
53   - fill(name, fill_size)+ to_SQL_CREATE(col_type, attributes)
  53 + fill(name, fill_size)+ to_SQL_CREATE(col_type, attributes, name)
54 54 .
55 55  
56 56 define String
... ...
src/generation/fields.anubis
... ... @@ -71,11 +71,12 @@ public define String
71 71 (
72 72 HK_Model_Field t,
73 73 List(HK_Model_Attr) attributes,
  74 + String name //name of component into the type
74 75 ) =
75 76 if t is
76 77 {
77 78 //anubis(_T) then "ByteArray",
78   - p_key then fill("INTEGER", 15) + " PRIMARY KEY",
  79 + p_key then fill("INTEGER", 15) + (if name = "id" then " PRIMARY KEY" else ""),
79 80 boolean_field then fill("BOOL", 15) + format_attributes(attributes),
80 81 date_field(dt_attrs) then
81 82 with new_attributes = if dt_attrs is auto_now_add then [default("(date('now', 'LOCALTIME'))")] else attributes,
... ... @@ -137,7 +138,7 @@ public define String
137 138 if t is
138 139 {
139 140 //anubis(_T) then "constant_byte_array(0,0)",
140   - p_key then "",
  141 + p_key then if name = "id" then "" else "bind_DB_id_or_NULL(\":v_"+name+"\", "+type_name+"."+name+")",
141 142 //binary then "constant_byte_array(0,0)",
142 143 boolean_field then "bind_Bool(\":v_"+name+"\", "+type_name+"."+name+")",
143 144 date_field(attrs) then "bind_Date(\":v_"+name+"\", "+type_name+"."+name+")",
... ... @@ -145,10 +146,10 @@ public define String
145 146 datetime_field(attrs) then if attrs is
146 147 {
147 148 none then "bind_Datetime(\":v_"+name+"\", "+type_name+"."+name+")",
148   - auto_now then "bind_Datetime(\":v_"+name+"\", now)",
  149 + auto_now then "bind_Datetime(\":v_"+name+"\", db_now)",
149 150 auto_now_add then
150 151 if insert then
151   - "bind_Datetime(\":v_"+name+"\", now)"
  152 + "bind_Datetime(\":v_"+name+"\", db_now)"
152 153 else
153 154 ""
154 155 },
... ...
src/generation/files.anubis
... ... @@ -68,18 +68,18 @@ define Maybe(One)
68 68 if generate_extract_db_cursor_to_json(strm, table) is { failure then failure, success(_) then
69 69 if generate_get_all(strm, table) is { failure then failure, success(_) then
70 70 if generate_get_all_with_clause(strm, table) is { failure then failure, success(_) then
  71 + if generate_get_one_with_clause(strm, table) is { failure then failure, success(_) then
71 72 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
73 73 if generate_get_one_json(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_get_count_by_clause(strm, table) is { failure then failure, success(_) then
75 75 if generate_update(strm, table) is { failure then failure, success(_) then
76   - if generate_delete(strm, table) is { failure then failure, success(_) then
  76 + //if generate_delete(strm, table) is { failure then failure, success(_) then
77 77 if generate_show_string(strm, table) is { failure then failure, success(_) then
78 78 if generate_selector(strm, table) is { failure then failure, success(_) then
79 79 if generate_choices_selector(strm, table) is { failure then failure, success(_) then
80 80 if generate_VT_all_view(strm, table) is { failure then failure, success(_) then
81 81 if generate_VT_all_edit(strm, table) is { failure then failure, success(_) then
82   - success(unique)}}}}}}}}}}}}}}}}}}}}}}
  82 + success(unique)}}}}}}}}}}}}}}}}}}}}
83 83 }.
84 84  
85 85 define One
... ... @@ -125,7 +125,7 @@ define Maybe(One)
125 125 List(String) view,
126 126 List(String) edit,
127 127 // List(String) writer,
128   - List(String) delete,
  128 +// List(String) delete,
129 129 List(String) controllers,
130 130 String dest_dir
131 131 )=
... ... @@ -144,6 +144,7 @@ define Maybe(One)
144 144 */
145 145 read tools/basis.anubis
146 146 read hayamiki_lib/view/view_table_manager_types.anubis\n
  147 +read hayamiki_lib/controller/hk_controller.anubis
147 148 read hayamiki_lib/controller/hk_sql_utils.anubis\n"+
148 149 join("\n", transmit)+"\n\n"+
149 150 "public define List(VTM_view)\n"+
... ... @@ -158,14 +159,14 @@ join(&quot;\n&quot;, transmit)+&quot;\n\n&quot;+
158 159 //" generated_tm_writer_list =\n"+
159 160 //" [\n "+join(",\n ",writer)+"\n"+
160 161 //" ].\n\n"+
161   -"public define List(TM_delete)\n"+
162   -" generated_tm_delete_list =\n"+
163   -" [\n "+join(",\n ",delete)+"\n"+
164   -" ].\n\n"+
  162 +//"public define List(TM_delete)\n"+
  163 +//" generated_tm_delete_list =\n"+
  164 +//" [\n "+join(",\n ",delete)+"\n"+
  165 +//" ].\n\n"+
165 166 "public define List(HK_Table_controller)\n"+
166 167 " generated_hk_table_controller_list =\n"+
167   -" [\n "+join(",\n ",controllers)+"\n"+
168   -" ].\n\n"
  168 +" [\n "+join(",\n ",controllers)+"\n"+
  169 +" ]\n.\n\n"
169 170 ) is //end of the function
170 171 {
171 172 failure then println("can't write vtm "); failure,
... ... @@ -183,7 +184,7 @@ define One
183 184 List(String) so_far_view,
184 185 List(String) so_far_edit,
185 186 // List(String) so_far_writer,
186   - List(String) so_far_delete,
  187 +// List(String) so_far_delete,
187 188 List(String) so_far_controllers,
188 189 String dest_dir
189 190 )=
... ... @@ -192,9 +193,9 @@ define One
192 193 [] then
193 194 if apps is
194 195 {
195   - [] then forget(generate_vtm_file(so_far_transmit, so_far_view, so_far_edit, so_far_delete, so_far_controllers, dest_dir));println("Generation finished"),
  196 + [] then forget(generate_vtm_file(so_far_transmit, so_far_view, so_far_edit, so_far_controllers, dest_dir));println("Generation finished"),
196 197 [app . apps_t] then
197   - _make_vtm(apps_t, app.hk_tables, so_far_transmit, so_far_view, so_far_edit, so_far_delete, so_far_controllers, dest_dir)
  198 + _make_vtm(apps_t, app.hk_tables, so_far_transmit, so_far_view, so_far_edit, so_far_controllers, dest_dir)
198 199  
199 200 },
200 201  
... ... @@ -204,25 +205,26 @@ define One
204 205 with view = "vtm_view(\""+name+"\", get_viewable_"+name+", get_all_"+name+"_view_name)",
205 206 with edit = "vtm_edit(\""+name+"\", get_editable_"+name+")",
206 207 // with writer = "tm_writer(\""+name+"\", update_"+name+"_from_type, update_"+name+"_from_fields)",
207   - with delete = "tm_delete(\""+name+"\", delete_"+name+")",
  208 +// with delete = "tm_delete(\""+name+"\", delete_"+name+")",
208 209 with controller =
209 210 "hk_table_controller(\""+name+"\",
210   - //Create
211   - update_"+name+"_from_type,
212   - update_"+name+"_from_fields,
213   - //Read
214   - (SQLite3DataBase db, String clause) |-> get_count_by_clause(db, \""+name+"\", clause),
215   - get_one_"+name+"_to_json,
216   - get_all_"+name+"_by_clause_to_json,
217   - //Delete
218   - delete_"+name+",
  211 + //Create
  212 + update_"+name+"_from_type,
  213 + update_"+name+"_from_fields,
  214 + //Read
  215 + (SQLite3DataBase db, String clause) |-> get_count_by_clause(db, \""+name+"\", clause),
  216 + get_one_"+name+"_to_json,
  217 + get_all_"+name+"_by_clause_to_json,
  218 + //Delete
  219 + (SQLite3DataBase db, List(Int) ids) |-> delete_from_ids(db, \""+name+"\", ids),
219 220  
220   - //view interface
221   - //selector
222   - get_selector_"+name+"\n"+
223   -")",
  221 + //view interface
  222 + //selector
  223 + get_selector_"+name+",
  224 + hk_trigger_set(var([]), var([])) //initialize the triggers list as empty
  225 + )",
224 226  
225   - _make_vtm(apps, t, [transmit . so_far_transmit], [view . so_far_view], [edit . so_far_edit], [delete . so_far_delete], [controller . so_far_controllers], dest_dir)
  227 + _make_vtm(apps, t, [transmit . so_far_transmit], [view . so_far_view], [edit . so_far_edit], [controller . so_far_controllers], dest_dir)
226 228 }.
227 229  
228 230 define One
... ... @@ -232,7 +234,7 @@ define One
232 234 String dest_dir
233 235 )
234 236 =
235   - _make_vtm(apps, [], [], [], [], [], [], dest_dir).
  237 + _make_vtm(apps, [], [], [], [], [], dest_dir).
236 238  
237 239  
238 240 define Maybe(One)
... ...
src/generation/fn_delete.anubis
... ... @@ -25,26 +25,26 @@ public define Maybe(One)
25 25 with type_name = to_upper(name, 1), //make the first character to upper case to able to be a Anubis Type.
26 26  
27 27 //generate the type of the table that contain all components
28   - if write_string(stream,
29   - "\n\n"+
30   - //SQL query
31   - "read tools/int.anubis\n\n"+
32   - "public define Maybe(One)\n"+
33   - " delete_"+name+"\n"+
34   - " (\n"+
35   - " SQLite3DataBase db,\n"+
36   - " List(Int) ids\n"+
37   - " )=\n"+
38   - //DELETE SQL query
39   - " if sql_query_timeout(db,\n"+
40   - " \"DELETE FROM "+name+"\n"+
41   - " WHERE id IN (\"+join(\", \", to_List_String(ids))+\");\",\n"+
42   - " [],\n"+
43   - " \"delete_"+name+"\") is\n"+
44   - " {\n"+
45   - " error(_) then failure,\n"+
46   - " ok(_, _, _) then success(unique)\n"+
47   - " }.\n\n"
  28 + if write_string(stream, ""
  29 +// "\n\n"+
  30 +// //SQL query
  31 +// "read tools/int.anubis\n\n"+
  32 +// "public define Maybe(One)\n"+
  33 +// " delete_"+name+"\n"+
  34 +// " (\n"+
  35 +// " SQLite3DataBase db,\n"+
  36 +// " List(Int) ids\n"+
  37 +// " )=\n"+
  38 +// //DELETE SQL query
  39 +// " if sql_query_timeout(db,\n"+
  40 +// " \"DELETE FROM "+name+"\n"+
  41 +// " WHERE id IN (\"+join(\", \", to_List_String(ids))+\");\",\n"+
  42 +// " [],\n"+
  43 +// " \"delete_"+name+"\") is\n"+
  44 +// " {\n"+
  45 +// " error(_) then failure,\n"+
  46 +// " ok(_, _, _) then success(unique)\n"+
  47 +// " }.\n\n"
48 48 // +
49 49 // "public define Maybe(One)\n"+
50 50 // " delete_"+name+"\n"+
... ...
src/generation/header.anubis
... ... @@ -50,30 +50,30 @@ public define Maybe(One)
50 50 * Time: "+_Int_to_ISO_8601_time(now)+"
51 51 *
52 52 */
53   -read tools/basis.anubis
  53 +transmit tools/basis.anubis
54 54  
55   -read calexium_lib/web/CXM_making_a_web_site.anubis
56   -read calexium_lib/web/CXM_common.anubis
57   -read calexium_lib/web/CXM_web_dump.anubis
58   -read calexium_lib/web/CXM_web_arg_utils.anubis
59   -read calexium_lib/web/CXM_json.anubis
60   -read calexium_lib/web/widgets/icons_set.anubis
61   -read data_base/sqlite.anubis
62   -read data_base/db_tools.anubis
63   -read calexium_lib/database/db_utils.anubis
64   -read calexium_lib/database/db_types.anubis
65   -read hayamiki_lib/view/types/hk_form.anubis
66   -read hayamiki_lib/view/types/hk_view.anubis
67   -read hayamiki_lib/view/types/hk_filter.anubis
68   -read hayamiki_lib/types/hayamiki.anubis
69   -read hayamiki_lib/controller/hk_sql_utils.anubis
70   -read system/logger.anubis
71   -read system/datetime.anubis
72   -read system/convert.anubis
73   -read system/string.anubis
  55 +transmit calexium_lib/web/CXM_making_a_web_site.anubis
  56 +transmit calexium_lib/web/CXM_common.anubis
  57 +transmit calexium_lib/web/CXM_web_dump.anubis
  58 +transmit calexium_lib/web/CXM_web_arg_utils.anubis
  59 +transmit calexium_lib/web/CXM_json.anubis
  60 +transmit calexium_lib/web/widgets/icons_set.anubis
  61 +transmit data_base/sqlite.anubis
  62 +transmit data_base/db_tools.anubis
  63 +transmit calexium_lib/database/db_utils.anubis
  64 +transmit calexium_lib/database/db_types.anubis
  65 +transmit hayamiki_lib/view/types/hk_form.anubis
  66 +transmit hayamiki_lib/view/types/hk_view.anubis
  67 +transmit hayamiki_lib/view/types/hk_filter.anubis
  68 +transmit hayamiki_lib/types/hayamiki.anubis
  69 +transmit hayamiki_lib/controller/hk_sql_utils.anubis
  70 +transmit system/logger.anubis
  71 +transmit system/datetime.anubis
  72 +transmit system/convert.anubis
  73 +transmit system/string.anubis
74 74  
75   -read calexium_lib/net_services_protocols/logger_service.anubis
76   -read app/app_loggers.anubis
  75 +transmit calexium_lib/net_services_protocols/logger_service.anubis
  76 +transmit app/app_loggers.anubis
77 77 ") is failure then failure else
78 78 foreign_table_header(stream, columns)
79 79 .
... ...
src/generation/html.anubis
... ... @@ -89,7 +89,11 @@ public define String
89 89 since column is hk_column(name, col_type, _, _),
90 90 if col_type is
91 91 {
92   - p_key then to_HTML_cell_link(column, data_name, general_type_name),
  92 + p_key then
  93 + if name = "id" then
  94 + to_HTML_cell_link(column, data_name, general_type_name)
  95 + else
  96 + to_HTML_cell_text(column, data_name, general_type_name),
93 97 boolean_field then to_HTML_cell_icon(column, data_name, general_type_name),
94 98 date_field(attrs) then to_HTML_cell_text(column, data_name, general_type_name),
95 99 time_field(attrs) then to_HTML_cell_text(column, data_name, general_type_name),
... ...
src/generation/json.anubis
... ... @@ -23,7 +23,7 @@ public define String
23 23 since col is hk_column(name, col_type, _, _),
24 24 if col_type is
25 25 {
26   - p_key then current_type_name+".id",
  26 + p_key then current_type_name+"."+name,
27 27 boolean_field then "to_JSON_String(to_String("+current_type_name+"."+name+"))",
28 28 date_field(attrs) then "to_JSON_String(to_String("+current_type_name+"."+name+"))",
29 29 time_field(attrs) then "to_JSON_String(to_String("+current_type_name+"."+name+"))",
... ... @@ -55,7 +55,7 @@ public define String
55 55 since col is hk_column(name, col_type, _, _),
56 56 if col_type is
57 57 {
58   - p_key then "json_member(\""+name+"\", json_int("+current_type_name+".id))",
  58 + p_key then "json_member(\""+name+"\", json_int("+current_type_name+"."+name+"))",
59 59 boolean_field then "json_member(\""+name+"\", json_bool("+current_type_name+"."+name+"))",
60 60 date_field(attrs) then "json_member(\""+name+"\", json_string("+current_type_name+"."+name+"))",
61 61 time_field(attrs) then "json_member(\""+name+"\", json_string("+current_type_name+"."+name+"))",
... ...
src/generation/types.anubis
... ... @@ -109,9 +109,11 @@ public define Maybe(One)
109 109  
110 110 //generate the type of the table that contain all components
111 111 if write_string(stream,
112   - "\n\ndefine List(String)\n "+name+"_columns =\n ["+ //head of the definition
  112 + "\n\npublic define List(String)\n "+name+"_columns =\n ["+ //head of the definition
113 113 join(",\n ",enclose("\"", to_List_String(columns, false)))+ //all columns name wihtout id
114   - " ].\n\n") is //end of the list
  114 + " ].\n\n"+
  115 + "public define String\n"+
  116 + " "+name+"_select_fields = \""+join(", ",to_List_String(name, columns, true))+"\".\n\n") is //end of the list
115 117 {
116 118 failure then println("can't write generate_columns_list "+type_name); failure,
117 119 success(_) then success(unique)
... ... @@ -132,27 +134,36 @@ public define Maybe(One)
132 134 if write_string(stream,
133 135 "\n\n"+
134 136 //SQL query
  137 +// "public define Maybe("+type_name+")\n"+
  138 +// " get_"+name+"\n"+
  139 +// " (\n"+
  140 +// " SQLite3DataBase db,\n"+
  141 +// " String idx\n"+
  142 +// " )=\n"+
  143 +// " if sql_query_timeout(db, \"SELECT "+join(", ", enclose("\\\"",to_List_String(columns, true)))+" FROM "+name+" WHERE id=\"+idx+\";\", [], \"get_"+name+"\") is\n"+
  144 +// " {\n"+
  145 +// " error(_) then failure,\n"+
  146 +// " ok(_, cursor, _) then extract_"+name+"(cursor)\n"+
  147 +// " }.\n\n"+
  148 + //SQL query
135 149 "public define Maybe("+type_name+")\n"+
136   - " get_"+name+"\n"+
  150 + " get_mb_"+name+"\n"+
137 151 " (\n"+
138 152 " SQLite3DataBase db,\n"+
139 153 " String idx\n"+
140 154 " )=\n"+
141   - " if sql_query_timeout(db, \"SELECT "+join(", ", enclose("\\\"",to_List_String(columns, true)))+" FROM "+name+" WHERE id=\"+idx+\";\", [], \"get_"+name+"\") is\n"+
142   - " {\n"+
143   - " error(_) then failure,\n"+
144   - " ok(_, cursor, _) then extract_"+name+"(cursor)\n"+
145   - " }.\n\n"+
146   - //SQL query
  155 + " get_"+name+"_by_clause(db, \" id = \"+idx).\n\n"+
  156 +
147 157 "public define Maybe("+type_name+")\n"+
148   - " get_"+name+"\n"+
  158 + " get_mb_"+name+"\n"+
149 159 " (\n"+
150 160 " SQLite3DataBase db,\n"+
151 161 " Int idx\n"+
152 162 " )=\n"+
153   - " get_"+name+"(db, to_decimal(idx)).\n\n"+
  163 + " get_"+name+"_by_clause(db, \" id = \"+to_decimal(idx)).\n\n"+
  164 +
154 165 "public define Maybe("+type_name+")\n"+
155   - " get_"+name+"\n"+
  166 + " get_mb_"+name+"\n"+
156 167 " (\n"+
157 168 " SQLite3DataBase db,\n"+
158 169 " DB_id idx\n"+
... ... @@ -160,9 +171,27 @@ public define Maybe(One)
160 171 " if idx is\n"+
161 172 " {\n"+
162 173 " none then failure,\n"+
163   - " db_id(_idx) then get_"+name+"(db, to_decimal(_idx))\n"+
  174 + " db_id(_idx) then get_mb_"+name+"(db, _idx)\n"+
164 175 " }"+
165   - ".\n\n"
  176 +
  177 + ".\n\n"+
  178 + "public define "+type_name+"\n"+
  179 + " get_"+name+"\n"+
  180 + " (\n"+
  181 + " SQLite3DataBase db,\n"+
  182 + " DB_id idx\n"+
  183 + " )=\n"+
  184 + " if idx is\n"+
  185 + " {\n"+
  186 + " none then get_default_"+name+",\n"+
  187 + " db_id(_idx) then\n"+
  188 + " if get_"+name+"_by_clause(db, \" id = \"+to_decimal(_idx)) is\n"+
  189 + " {\n"+
  190 + " failure then get_default_"+name+",\n"+
  191 + " success(r) then r\n"+
  192 + " }\n"+
  193 + " }"+
  194 + ".\n\n"
166 195 ) is //end of the function
167 196 {
168 197 failure then println("can't write generate_get_one "+type_name); failure,
... ... @@ -268,7 +297,7 @@ public define Maybe(One)
268 297 if write_string(stream,
269 298 "\n\n"+
270 299 //SQL query
271   - "public define One\n"+
  300 + "public define Maybe(Int)\n"+
272 301 " update_"+name+"\n"+
273 302 " (\n"+
274 303 " SQLite3DataBase db,\n"+
... ... @@ -278,11 +307,24 @@ public define Maybe(One)
278 307 " {\n"+
279 308 //INSERT SQL query
280 309 " none then\n"+
281   - " forget(sql_query_timeout(db,\n"+
  310 + " if sql_query_timeout(db,\n"+
282 311 " \"INSERT INTO "+name+"\n"+
283 312 insert_values(columns, " ")+";\",\n"+
284 313 generate_Bind_list(columns, name, " ", true)+",\n"+
285   - " \"update_"+name+" (INSERT)\")),\n"+
  314 + " \"update_"+name+" (INSERT)\") is\n"+
  315 + " {\n"+
  316 + " error(sql_error) then\n"+
  317 + " //logError(debug_log, db_error(sql_error, table_name));\n"+
  318 + " failure,\n"+
  319 + " ok(_, cursor, _) then\n"+
  320 + " if sql_query_scalar(db, \"SELECT last_insert_rowid()\") is\n"+
  321 + " {\n"+
  322 + " error(_) then failure,\n"+
  323 + " ok(datum) then\n"+
  324 + " with r = if datum is integer(n) then n else 0,\n"+
  325 + " success(r)\n"+
  326 + " }\n"+
  327 + " },\n"+
286 328 //UPDATE SQL query
287 329 " db_id(idx) then\n"+
288 330 " if sql_query_timeout(db,\n"+
... ... @@ -291,38 +333,39 @@ public define Maybe(One)
291 333 " WHERE id = \"+idx+\";\",\n"+
292 334 generate_Bind_list(columns, name, " ", false)+",\n"+
293 335 " \"update_"+name+" (UPDATE)\") is { \n"+
294   - " error(sql_error)\n"+
295   - " then logError(debug_log, db_error(sql_error, \"update_"+name+"\")), ok(_, _, _) then unique }\n"+
  336 + " error(sql_error) then\n"+
  337 + " logError(debug_log, db_error(sql_error, \"update_"+name+"\")); failure,\n"+
  338 + " ok(_, _, _) then success(idx) }\n"+
296 339 " }.\n\n"+
297 340  
298   - "public define Maybe(One)\n"+
299   - " update_"+name+"\n"+
300   - " (\n"+
301   - " SQLite3DataBase db,\n"+
302   - " DB_id db_id,\n"+
303   - " List(SQLite3_update_field) fields\n"+
304   - " )=\n"+
305   - " if db_id is\n"+
306   - " {\n"+
307   - //INSERT SQL query
308   - " none then\n"+
309   - " since make_insert_clause_and_binds(fields) is (binds, insert_columns, insert_values),\n"+
310   - " if sql_query_timeout(db, \"INSERT INTO "+name+" \"+insert_columns+\" VALUES \"+insert_values+\";\", binds, \"update_"+name+" (INSERT)\") is\n"+
311   - " {\n"+
312   - " error(sql_error) then logError(debug_log, db_error(sql_error, \"update_"+name+"\"));failure,\n"+
313   - " ok(_, _, _) then success(unique)\n"+
314   - " },\n"+
315   - //UPDATE SQL query
316   - " db_id(idx) then\n"+
317   - " since make_update_clause_and_binds(fields) is (binds, set_string),\n"+
318   - " if sql_query_timeout(db, \"UPDATE "+name+" SET \"+set_string +\" WHERE id = \"+idx+\";\", binds, \"update_"+name+" (UPDATE)\") is\n"+
319   - " {\n"+
320   - " error(sql_error) then logError(debug_log, db_error(sql_error, \"update_"+name+"\"));failure,\n"+
321   - " ok(_, _, _) then success(unique)\n"+
322   - " }\n"+
323   - " }.\n\n"+
  341 +// "public define Maybe(One)\n"+
  342 +// " update_"+name+"\n"+
  343 +// " (\n"+
  344 +// " SQLite3DataBase db,\n"+
  345 +// " DB_id db_id,\n"+
  346 +// " List(SQLite3_update_field) fields\n"+
  347 +// " )=\n"+
  348 +// " if db_id is\n"+
  349 +// " {\n"+
  350 +// //INSERT SQL query
  351 +// " none then\n"+
  352 +// " since make_insert_clause_and_binds(fields) is (binds, insert_columns, insert_values),\n"+
  353 +// " if sql_query_timeout(db, \"INSERT INTO "+name+" \"+insert_columns+\" VALUES \"+insert_values+\";\", binds, \"update_"+name+" (INSERT)\") is\n"+
  354 +// " {\n"+
  355 +// " error(sql_error) then logError(debug_log, db_error(sql_error, \"update_"+name+"\"));failure,\n"+
  356 +// " ok(_, _, _) then success(unique)\n"+
  357 +// " },\n"+
  358 +// //UPDATE SQL query
  359 +// " db_id(idx) then\n"+
  360 +// " since make_update_clause_and_binds(fields) is (binds, set_string),\n"+
  361 +// " if sql_query_timeout(db, \"UPDATE "+name+" SET \"+set_string +\" WHERE id = \"+idx+\";\", binds, \"update_"+name+" (UPDATE)\") is\n"+
  362 +// " {\n"+
  363 +// " error(sql_error) then logError(debug_log, db_error(sql_error, \"update_"+name+"\"));failure,\n"+
  364 +// " ok(_, _, _) then success(unique)\n"+
  365 +// " }\n"+
  366 +// " }.\n\n"+
324 367  
325   - "public define Maybe(One)\n"+
  368 + "public define Maybe(Int)\n"+
326 369 " update_"+name+"_from_type\n"+
327 370 " (\n"+
328 371 " SQLite3DataBase db,\n"+
... ... @@ -331,16 +374,16 @@ public define Maybe(One)
331 374 " if extract_"+type_name+"_from_web_arg(lwa, \"\") is\n"+
332 375 " {\n"+
333 376 " failure then logError(debug_log, \"update_"+name+" extract_"+type_name+"_from_web_arg is failure\"); failure,\n"+
334   - " success("+name+") then success(update_"+name+"(db, "+name+"))\n"+
  377 + " success("+name+") then update_"+name+"(db, "+name+")\n"+
335 378 " }.\n\n"+
336 379  
337   - "public define Maybe(One)\n"+
  380 + "public define Maybe(Int)\n"+
338 381 " update_"+name+"_from_fields\n"+
339 382 " (\n"+
340 383 " SQLite3DataBase db,\n"+
341 384 " List(Web_arg) lwa\n"+
342 385 " )=\n"+
343   - " update_"+name+"(db, get_DB_id(lwa, \"id\"),extract_"+type_name+"_update_fields_from_web_arg(lwa, \"\"))\n"+
  386 + " insert_or_update(db, \""+name+"\", get_DB_id(lwa, \"id\"), extract_"+type_name+"_update_fields_from_web_arg(lwa, \"\"))\n"+
344 387 ".\n\n"
345 388  
346 389 ) is //end of the function
... ... @@ -373,7 +416,7 @@ public define Maybe(One)
373 416 " {\n"+
374 417 " none then default_select,\n"+
375 418 " db_id(idx) then \n"+
376   - " if get_"+name+"(db, idx) is\n"+
  419 + " if get_mb_"+name+"(db, idx) is\n"+
377 420 " {\n"+
378 421 " failure then default_select,\n"+
379 422 " success(obj) then "+show_string+"\n"+
... ...
src/generation/vt_edit.anubis
... ... @@ -153,12 +153,7 @@ public define Maybe(One)
153 153 " with "+table_name+" = if action is\n"+
154 154 " {\n"+
155 155 " new_entry then get_default_"+table_name+"\n"+
156   - " edit_entry(idx) then\n"+
157   - " if get_"+table_name+"(db, idx) is\n"+
158   - " {\n"+
159   - " failure then get_default_"+table_name+",\n"+
160   - " success(data) then data\n"+
161   - " }\n"+
  156 + " edit_entry(idx) then get_"+table_name+"(db, idx)\n"+
162 157 " },\n"+
163 158 _generate_get_editable_if_else(table_name, edit_view, "")+
164 159 ".\n"
... ...
src/generation/web_args.anubis
... ... @@ -8,6 +8,7 @@
8 8  
9 9 read hayamiki_lib/types/hayamiki.anubis
10 10 read tools/streams.anubis
  11 +read fields.anubis
11 12 read columns.anubis
12 13  
13 14 public define String
... ... @@ -35,7 +36,7 @@ public define String
35 36 phone_field then "get_String(lwa, __prefix__+\""+name+"\")",
36 37 email_field then "get_String(lwa, __prefix__+\""+name+"\")"
37 38 }.
38   -
  39 +
39 40 define (List(String), Int)
40 41 /*
41 42 */
... ... @@ -62,6 +63,52 @@ define (List(String), Int)
62 63 _generate_extract_web_arg_to_type(t, [ line . so_far], closure+1)
63 64 }
64 65 .
  66 +
  67 +public define String
  68 + from_web_arg_to_type_with_default
  69 + (
  70 + HK_Model_Field col_type,
  71 + String name
  72 + ) =
  73 + if col_type is
  74 + {
  75 + //anubis(_T) then "constant_byte_array(0,0)",
  76 + p_key then "get_DB_id(lwa, __prefix__+\""+name+"\"),",
  77 + //binary then "constant_byte_array(0,0)",
  78 + boolean_field then "get_Bool(lwa, __prefix__+\""+name+"\"),",
  79 + date_field(attrs) then if attrs = none then "get_DB_date(lwa, __prefix__+\""+name+"\", "+to_Anubis_default(col_type)+")," else "get_dummy_DB_date,",
  80 + time_field(attrs) then if attrs = none then "get_DB_time(lwa, __prefix__+\""+name+"\", "+to_Anubis_default(col_type)+")," else "get_dummy_DB_time,",
  81 + datetime_field(attrs) then if attrs = none then "get_DB_datetime(lwa, __prefix__+\""+name+"\", "+to_Anubis_default(col_type)+")," else "get_dummy_DB_datetime,",
  82 + foreign_key(_,_,_) then "get_DB_id(lwa, __prefix__+\""+name+"\"),",
  83 + integer_field(_) then "get_Int(lwa, __prefix__+\""+name+"\", "+to_Anubis_default(col_type)+"),",
  84 + float_field then "get_Float(lwa, __prefix__+\""+name+"\", "+to_Anubis_default(col_type)+"),",
  85 + char_field(_,_) then "get_String(lwa, __prefix__+\""+name+"\", "+to_Anubis_default(col_type)+"),",
  86 + password_field(min_size) then "if get_Password(lwa, __prefix__+\""+name+"_1\", __prefix__+\""+name+"_2\") is { failure then "+to_Anubis_default(col_type)+", success(pass) then pass},",
  87 + text_field(_) then "get_String(lwa, __prefix__+\""+name+"\", "+to_Anubis_default(col_type)+"),",
  88 + color_field then "get_String(lwa, __prefix__+\""+name+"\", "+to_Anubis_default(col_type)+"),",
  89 + phone_field then "get_String(lwa, __prefix__+\""+name+"\", "+to_Anubis_default(col_type)+"),",
  90 + email_field then "get_String(lwa, __prefix__+\""+name+"\", "+to_Anubis_default(col_type)+"),"
  91 + }.
  92 +
  93 +define List(String)
  94 +/*
  95 + */
  96 + _generate_extract_web_arg_to_type_with_default
  97 + (
  98 + List(HK_Model_Column) columns,
  99 + List(String) so_far
  100 + )=
  101 + if columns is
  102 + {
  103 + [] then reverse(so_far),
  104 + [h . t ] then
  105 + since h is hk_column(name, col_type, _, _),
  106 + with result = from_web_arg_to_type_with_default(col_type, name),
  107 + //If the field is not editable no need to get it from the web
  108 + _generate_extract_web_arg_to_type_with_default(t, [ " "+result . so_far])
  109 +
  110 + }
  111 +.
65 112  
66 113 public define Maybe(One)
67 114 /* generate the function which extract the entire database table row type from web
... ... @@ -87,7 +134,18 @@ public define Maybe(One)
87 134 to_String(list_of_web_arg)+ //all editable components of the type
88 135 " success(\n"+generate_constructor(name, columns, " ")+")\n"+ //generate type construction with values got from web arg
89 136 " "+fill(nb_closure, '}')+ //add close } according to number of web arg
90   - ".\n") is //end of the function
  137 + ".\n\n"+
  138 + "\n\npublic define "+type_name+"\n"+
  139 + " extract_"+type_name+"_from_web_arg_with_default\n"+
  140 + " (\n"+
  141 + " List(Web_arg) lwa,\n"+
  142 + " String __prefix__\n"+
  143 + " )=\n"+ //head of the definition
  144 + " "+name+"(\n"+
  145 + to_String(_generate_extract_web_arg_to_type_with_default(columns, []))+
  146 + ")\n"+ //generate type construction with values got from web arg
  147 + ".\n\n"
  148 + ) is //end of the function
91 149 {
92 150 failure then println("can't write extract_"+type_name+"_from_web_arg "+type_name); failure,
93 151 success(_) then success(unique)
... ... @@ -105,28 +163,32 @@ public define String
105 163 if t is
106 164 {
107 165 //anubis(_T) then "constant_byte_array(0,0)",
108   - p_key then "[]",
  166 + p_key then
  167 + if name ="id" then
  168 + "[]"
  169 + else
  170 + "if get_mb_DB_id(lwa, __prefix__+\""+name+"\") is {failure then [], success("+name+") then [field(\""+name+"\", bind_DB_id_or_NULL(\":v_"+name+"\", "+name+"))] }",
109 171 boolean_field then
110 172 "if get_mb_Bool(lwa, __prefix__+\""+name+"\") is {failure then [], success("+name+") then [field(\""+name+"\", bind_Bool(\":v_"+name+"\", "+name+"))] }",
111 173 date_field(attrs) then
112 174 if attrs is
113 175 {
114 176 none then "if get_DB_date(lwa, __prefix__+\""+name+"\") is {failure then [], success("+name+") then [field(\""+name+"\", bind_Date(\":v_"+name+"\", "+name+"))] }",
115   - auto_now then "[field(\""+name+"\", bind_Date(\":v_"+name+"\", now))]",
  177 + auto_now then "[field(\""+name+"\", bind_Date(\":v_"+name+"\", db_now))]",
116 178 auto_now_add then "[]"
117 179 },
118 180 time_field(attrs) then
119 181 if attrs is
120 182 {
121 183 none then "if get_DB_time(lwa, __prefix__+\""+name+"\") is {failure then [], success("+name+") then [field(\""+name+"\", bind_Time(\":v_"+name+"\", "+name+"))] }",
122   - auto_now then "[field(\""+name+"\", bind_Time(\":v_"+name+"\", now))]",
  184 + auto_now then "[field(\""+name+"\", bind_Time(\":v_"+name+"\", db_now))]",
123 185 auto_now_add then "[]"
124 186 },
125 187 datetime_field(attrs) then
126 188 if attrs is
127 189 {
128 190 none then "if get_DB_datetime(lwa, __prefix__+\""+name+"\") is {failure then [], success("+name+") then [field(\""+name+"\", bind_Datetime(\":v_"+name+"\", "+name+"))] }",
129   - auto_now then "[field(\""+name+"\", bind_Datetime(\":v_"+name+"\", now))]",
  191 + auto_now then "[field(\""+name+"\", bind_Datetime(\":v_"+name+"\", db_now))]",
130 192 auto_now_add then "[]"
131 193 },
132 194 foreign_key(_,_,_) then //"with "+name+" = get_DB_id(lwa, __prefix__+\""+name+"\"),",
... ...