vt_view.anubis
13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
* Created by PyramIDE.
* User: フランスのトトロ aka (David RENÉ)
* Date: 21/03/2017
* Time: 22:00
* © Calexium
*/
read hayamiki_lib/types/hayamiki.anubis
read tools/basis.anubis
read tools/streams.anubis
read system/string.anubis
read fields.anubis
read columns.anubis
read html.anubis
read json.anubis
public define String
make_list_view_json_row
(
HK_Table table, //table to format
String data_name, //name of data
String indent, //indentation string to use
List(HK_Model_Column) columns //list view to generate
)=
since table is hk_table(table_name, short, model, _), //get name and model of the table
// since model is hk_model( columns, _), //get all columns of table
// since display is db_display(views, _), //
//and the other set with list_view
// "\"{ "+ join("+\", ", map((HK_Model_Column column) |-> column.name+": \"+to_JSON_String(" + to_String(column, data_name, table_name)+")", columns))+"+\" }\""
"\"{ id : \"+to_JSON_String(to_String("+data_name+".id))+\", "+
join("+\", ", map((HK_Model_Column column) |-> column.name+": \"+" + to_JSON_String(column, data_name, table_name), columns)) +"+\" }\""
.
define String
to_VT_view_model
(
HK_Model_Column col,
String type_name
)=
since col is hk_column(name, col_type, _, help),
if col_type is
{
p_key then "primary_key",
boolean_field then "boolean(\""+to_upper(name)+"\", \""+name+"\", "+to_Anubis_source(help)+")",
date_field(attrs) then "date(\""+to_upper(name)+"\", \""+name+"\", "+to_Anubis_source(help)+")",
time_field(attrs) then "time(\""+to_upper(name)+"\", \""+name+"\", "+to_Anubis_source(help)+")",
datetime_field(attrs) then "datetime(\""+to_upper(name)+"\", \""+name+"\", "+to_Anubis_source(help)+")",
foreign_key(app,foreign_table) then "foreign_text(\""+to_upper(name)+"\", \""+name+"\", hk_form_selector(get_selector_"+foreign_table+"), "+to_Anubis_source(help)+")",
integer_field(choices) then
if choices is
{
no_choice then "integer(\""+to_upper(name)+"\", \""+name+"\", "+to_Anubis_source(help)+")",
int_choices(choices_list) then
//if choice list is NOT empty we construct the possible choices selector
if length(choices_list) > 0 then
"choices_int(\""+to_upper(name)+"\", \""+name+"\", hk_form_selector(get_choices_selector_"+to_lower(type_name)+"_"+name+"), "+to_Anubis_source(help)+")"
else
"integer(\""+to_upper(name)+"\", \""+name+"\", "+to_Anubis_source(help)+")",
}
float_field then "float(\""+to_upper(name)+"\", \""+name+"\", "+to_Anubis_source(help)+")",
//TODO make choices selection if need
char_field(choices,_) then
if choices is
{
no_choice then "text(\""+to_upper(name)+"\", \""+name+"\", "+to_Anubis_source(help)+")",
text_choices(choices_list) then
//if choice list is NOT empty we construct the possible choices selector
if length(choices_list) > 0 then
"choices_text(\""+to_upper(name)+"\", \""+name+"\", hk_form_selector(get_choices_selector_"+to_lower(type_name)+"_"+name+"), "+to_Anubis_source(help)+")"
else
"text(\""+to_upper(name)+"\", \""+name+"\", "+to_Anubis_source(help)+")"
}
password_field(min_size) then "password(\""+to_upper(name)+"\", \""+name+"\", "+to_Anubis_source(help)+")",
text_field(txt_attr) then "text_area(\""+to_upper(name)+"\", \""+name+"\", "+to_Anubis_source(help)+")",
color_field then "color(\""+to_upper(name)+"\", \""+name+"\", "+to_Anubis_source(help)+")"
}.
public define String
make_list_view_model
(
List(HK_Model_Column) columns,
String type_name,
String indent
)=
indent+join(",\n"+indent, map(((HK_Model_Column col) |-> to_VT_view_model(col, type_name)), columns))
.
public define String
make_list_view_header
(
List(String) list_view,
String indent
)=
indent+join(",\n"+indent, map((String text) |-> "text(\""+to_upper(text)+"\")", list_view)).
public define Maybe(One)
/* Generate
*/
generate_VT_view
(
Stream stream, //stream file where to write
HK_Table table, //
HK_List_View list_view //list view to generate
)=
since table is hk_table(table_name, short_name, model, display),
since model is hk_model( columns, _),
//since display is db_display(list_view, list_edit),
with type_name = to_upper(table_name, 1), //make the first character to upper case and be an Anubis Type.
with view_name = if list_view is
{
list_view_all then "default",
list_view(view_name, _, _) then view_name
},
with current_list_view = if list_view is
{
list_view_all then to_List_String(columns, false),
list_view(_, cols, _) then cols
},
with current_view_models = if list_view is
{
list_view_all then columns,
list_view(v_name, cols, _) then
if get_HK_Model_Column_list_from_name_list(columns, cols) is
{
failure then println("generate_VT_view: can't get all specified columns for table \""+table_name+"\" view name \""+v_name+"\" cols["+join(", ",cols)+"]");[],
success(got_columns) then got_columns,
}
},
//generate the type of the table that contain all components
if write_string(stream,
"\n\n"+
//SQL query
"define VT_view\n"+
" _get_viewable_"+table_name+"_"+view_name+"\n"+
" =\n"+
" with header = vt_view_row_header([\n"+
// view row header
make_list_view_header(current_list_view, " ")+"\n"+
" ]),\n"+
" with model = vt_view_model([\n"+
//view model
make_list_view_model(current_view_models, type_name, " ")+"\n"+
" ]),\n"+
" vt_view(\n"+
" \""+table_name+"\", //Name of the database table\n"+
" \""+view_name+"\", //Name of the view\n"+
" model,\n"+
" header,\n"+
" vt_view_db_calls(\n"+
" get_count_"+table_name+"_by_clause,\n"+
" get_all_"+table_name+"_by_clause_to_json\n"+
//" get_"+xxx+"_rows\n"+
" )\n"+
" )\n"+
".\n\n"
// "public define VT_view\n"+
// " get_viewable_"+table_name+"_"+view_name+"\n"+
// " (\n"+
// " SQLite3DataBase db,\n"+
// " String clause\n"+
// " )=\n"+
// " _get_viewable_"+table_name+"_"+view_name+"(db, get_all_"+table_name+"_by_clause(db, clause), [], [] ).\n\n"
) is //end of the function
{
failure then println("can't write generate_VT_view "+type_name); failure,
success(_) then success(unique)
}.
public define Maybe(One)
/* Generate
*/
_generate_VT_view
(
Stream stream, //stream file where to write
HK_Table table, //
List(HK_List_View) list_view, //list view to generate
Bool has_default //set to true if a default view was encountered.
)=
if list_view is
{
[] then
//if default view was not generated, we force to generate it
if has_default = false then
generate_VT_view(stream, table, list_view_all)
else
success(unique),
[view . t] then
if generate_VT_view(stream, table, view) is
{
failure then failure,
success(_) then
with new_has_default = if has_default = true then //already true, no need to go further
true
else
if view is
{
list_view_all then true, //list_view_all is default view
list_view(view_name, _, _) then
//if not we search for view named 'default'
if view_name = "default" then true else false
},
_generate_VT_view(stream, table, t, new_has_default)
}
}.
public define String
/* Generate
*/
_generate_get_viewable_if_else
(
String table_name, //
List(HK_List_View) list_view, //list view to generate
String so_far
)=
if list_view is
{
[] then
so_far +
"\n"+
" _get_viewable_"+table_name+"_default\n",
[view . t] then
with view_name = if view is
{
list_view_all then "default",
list_view(view_name, _, _) then view_name
},
_generate_get_viewable_if_else( table_name, t,
so_far +
" if view = \""+view_name+"\" then\n"+
" _get_viewable_"+table_name+"_"+view_name+"\n"+
" else"
)
}.
public define Maybe(One)
/* Generate
*/
generate_get_viewable
(
Stream stream, //stream file where to write
HK_Table table, //
List(HK_List_View) list_view //list view to generate
)=
since table is hk_table(table_name, short_name, model, display),
if write_string(stream,
"public define VT_view\n"+
" get_viewable_"+table_name+"\n"+
" (\n"+
" String view,\n"+
" )=\n"+
_generate_get_viewable_if_else(table_name, list_view, "")+
".\n"
) is //end of the function
{
failure then println("can't write generate_get_viewable "+table_name); failure,
success(_) then success(unique)
}.
public define List(String)
/* Generate
*/
_generate_list_view_name
(
List(HK_List_View) list_view, //list view to generate
List(String) so_far,
Bool has_default //set to true if a default view was encountered.
)=
if list_view is
{
[] then
//if default view was not generated, we force to generate it
if has_default = false then
reverse(["\"default\"" . so_far])
else
reverse(so_far),
[view . t] then
with new_has_default = if has_default = true then //already true, no need to go further
true
else
if view is
{
list_view_all then true, //list_view_all is default view
list_view(view_name, _, _) then
//if not we search for view named 'default'
if view_name = "default" then true else false
},
with view_name = if view is
{
list_view_all then "default",
list_view(view_name, _, _) then view_name
},
_generate_list_view_name(t, ["\""+view_name+"\"" . so_far], new_has_default)
}
.
public define Maybe(One)
_generate_get_all_view_name
(
Stream stream,
HK_Table table, //
List(HK_List_View) list_view
)=
since table is hk_table(table_name, short_name, model, display),
if write_string(stream,
"\n\npublic define List(String)\n"+
" get_all_"+table_name+"_view_name\n"+
" (\n"+
" One dummy\n"+
" )=\n"+
" [\n"+
" "+join(",\n ",_generate_list_view_name(list_view, [], false))+
"\n ]\n"+
".\n\n"
) is //end of the function
{
failure then println("can't write _generate_get_all_view_name "+table_name); failure,
success(_) then success(unique)
}
.
public define Maybe(One)
generate_VT_all_view
(
Stream stream,
HK_Table table
)=
since table is hk_table(name, short_name, model, display),
since model is hk_model( columns, _),
since display is hk_display(list_view, list_edit),
with type_name = to_upper(name, 1), //make the first character to upper case to able to be a Anubis Type.
if _generate_get_all_view_name(stream, table, list_view) is
{
failure then failure,
success(_) then
if _generate_VT_view(stream, table, list_view, false) is //list view to generate
{
failure then failure,
success(_) then generate_get_viewable(stream, table, list_view)
}
}
.