columns.anubis
16.9 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
/*
* Created by PyramIDE.
* User: フランスのトトロ aka (David RENÉ)
* Date: 13/02/2016
* Time: 11:05
* © David RENÉ
*/
read system/string.anubis
read tools/list.anubis
read hayamiki_lib/model/columns.anubis
read hayamiki_lib/model/fields.anubis
read fields.anubis
public define Maybe(HK_Model_Column)
/* return the HK_Model_Column corresponding of the given column name
*/
get_column_type_from_name
(
List(HK_Model_Column) columns, //columns in wich we search
String name_to_find //Column name to find in previous list
)=
if columns is
{
[] then failure,
[h . t] then
since h is hk_column(name, _, _, _),
if name_to_find = name then
success(h)
else
get_column_type_from_name(t, name_to_find)
}.
public define Maybe(List(HK_Model_Column))
/* return all columns matching with the list of given list of columns to get
*/
get_HK_Model_Column_list_from_name_list
(
List(HK_Model_Column) columns, //columns in wich we search
List(String) names_to_find //Column name to find in previous list
)=
map_escape((String column_name) |-> get_column_type_from_name(columns, column_name), names_to_find)
.
define List(String)
/* return the list of all columns name
*
*/
_to_List_String
(
String table_name,
List(HK_Model_Column) columns,
Bool with_pk, //if set, add the primary key named "id"
List(String) so_far
)=
if columns is
{
[] then reverse(so_far),
[h . t ] then
since h is hk_column(name, _, _, _),
if name = "id" & with_pk = false then
_to_List_String(table_name, t, with_pk, so_far)
else
_to_List_String(table_name, t, with_pk, [ table_name+name . so_far])
}.
public define List(String)
/* return the list of all columns name
*/
to_List_String
(
List(HK_Model_Column) columns,
Bool with_pk
)=
_to_List_String("", columns, with_pk, []).
public define List(String)
/* return the list of all columns name
*/
to_List_String
(
String table_name,
List(HK_Model_Column) columns,
Bool with_pk
)=
_to_List_String(table_name+".", columns, with_pk, []).
public define String
generate_constructor
(
String constructor_name,
List(HK_Model_Column) columns,
String indent
)=
indent+constructor_name+"(\n"+indent+" "+join(",\n"+indent+" ", to_List_String(columns, true))+")".
public define List(String)
/**
*/
to_Bind_list
(
List(HK_Model_Column) columns,
String type_name, //name of the type
List(String) so_far,
Bool insert
)=
if columns is
{
[] then reverse(so_far),
[h . t ] then
since h is hk_column(name, col_type, attributes, _),
with result = to_Bind(col_type, attributes, type_name, name, insert),
//we eliminate the empty string because during the join it will have an empty line
if result = "" then
to_Bind_list(t, type_name, so_far, insert)
else
to_Bind_list(t, type_name, [result . so_far], insert)
}.
public define String
generate_Bind_list
(
List(HK_Model_Column) columns,
String type_name,
String indent,
Bool insert
)=
indent+"[\n"+indent+" "+join(",\n"+indent+" ", to_Bind_list(columns, type_name, [], insert))+"]".
public define Maybe(String)
/** Return the column name according to the model.
* insert boolean indicate that we only want the column name during insert processing.
* In fact some column no need to be updated hence the column name will be ignored if
* the insert boolean = false.
*/
get_column_name
(
HK_Model_Column col,
Bool p_key_id, //true if the "id" column must be returned
Bool insert //true if insert time else false for update
) =
since col is hk_column(name, col_type, _, _),
if col_type is
{
//anubis(_T) then "constant_byte_array(0,0)",
p_key then if name = "id" & p_key_id = false then failure else success(name),
//binary then "constant_byte_array(0,0)",
boolean_field then success(name),
date_field(attrs) then success(name),
time_field(attrs) then success(name),
datetime_field(attrs) then if attrs is
{
none then success(name),
auto_now then success(name),
auto_now_add then
if insert then success(name) else failure
},
foreign_key(_,_,_,_,_) then success(name),
integer_field(_) then success(name),
float_field then success(name),
char_field(_,_) then success(name),
password_field(_) then success(name),
text_field(_) then success(name),
color_field then success(name),
phone_field(attr) then success(name),
url_field then success(name),
email_field then success(name)
}.
public define List(String)
columns_name
(
List(HK_Model_Column) columns,
Bool p_key_id, //true if we need to have the first primary_key column named "id"
Bool insert, //true if insert time else false for update
List(String) so_far
) =
if columns is
{
[] then reverse(so_far),
[h . t] then
if get_column_name(h, p_key_id, insert) is
{
failure then columns_name(t, p_key_id, insert, so_far)
success(result) then
if insert then
columns_name(t, p_key_id, insert, [result . so_far])
else
columns_name(t, p_key_id, insert, [enclose("\\\"",result)+" = :v_"+result . so_far])
}
}.
public define String
/*
Generate the SQL "(xxx, yyy, ...) VALUES ( :v_xxx, :v_yyy, ...)" for the insert
SQL query.
*/
insert_values
(
List(HK_Model_Column) columns,
Bool p_key_id,
String indent
)=
with columns_list = columns_name(columns, p_key_id, true, []),
indent+" ("+join(", ", enclose("\\\"",columns_list))+")\n"+indent+"VALUES\n"+indent+" ("+prefixed_join(":v_", columns_list, ", ")+")".
public define String
/*
Generate the SQL "xxx = :v_xxx, yyy = :v_yyy, ..." for the update
SQL query.
*/
update_values
(
List(HK_Model_Column) columns,
String indent
)=
with columns_list = columns_name(columns, false, false, []),
indent+" "+join(", ", columns_list).
public define Maybe(String)
/** Here we generate the VT_Edit entry according to the model
*/
get_HK_Form_Entry
(
HK_Model_Column col,
String type_name //name of the instanciated type
) =
since col is hk_column(col_name, col_type, _, help),
if col_type is
{
//anubis(_T) then "constant_byte_array(0,0)",
p_key then success("primary_key(to_String("+type_name+"."+col_name+"))"),
//binary then "constant_byte_array(0,0)",
boolean_field then success("boolean(\""+to_upper(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_name+", "+to_Anubis_source(help)+")"),
date_field(attrs) then if attrs is
{
none then success("date(\""+to_upper(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_name+".date, "+to_Anubis_source(help)+")"),
auto_now then success("information(\""+to_upper(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_name+".date, 10, "+to_Anubis_source(help)+")"),
auto_now_add then success("information(\""+to_upper(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_name+".date, 10, "+to_Anubis_source(help)+")")
},
time_field(attrs) then if attrs is
{
none then success("time(\""+to_upper(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_name+".time, "+to_Anubis_source(help)+")"),
auto_now then success("information(\""+to_upper(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_name+".time, 10, "+to_Anubis_source(help)+")"),
auto_now_add then success("information(\""+to_upper(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_name+".time, 10, "+to_Anubis_source(help)+")")
},
datetime_field(attrs) then if attrs is
{
none then success("datetime(\""+to_upper(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_name+".datetime, "+to_Anubis_source(help)+")"),
auto_now then success("information(\""+to_upper(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_name+".datetime, 18, "+to_Anubis_source(help)+")"),
auto_now_add then success("information(\""+to_upper(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_name+".datetime, 18, "+to_Anubis_source(help)+")")
},
foreign_key(app,foreign_table,fields,active_filters,_) then
if length(fields) = 0 then
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(active_filters)+", "+to_Anubis_source(help)+")")
else
with search_fields = join(", ", map((String field) |-> "\""+field+"\"",fields)),
success("foreign_text_search(\""+to_upper(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_name+", \""+foreign_table+"\", ["+search_fields+"], "+to_Anubis_source(active_filters)+", "+to_Anubis_source(help)+")"),
integer_field(choices) then
//success("integer(\""+to_upper(name)+"\", \""+name+"\", "+type_name+"."+name+", "+to_Anubis_source(help)+")"),
if choices is
{
no_choice then success("integer(\""+to_upper(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_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
success("choices_int(\""+to_upper(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_name+", hk_form_selector(get_choices_selector_"+to_lower(type_name)+"_"+col_name+"), "+to_Anubis_source(help)+")")
else
success("integer(\""+to_upper(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_name+", "+to_Anubis_source(help)+")"),
}
float_field then success("float(\""+to_upper(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_name+", "+to_Anubis_source(help)+")"),
//TODO make choices selection if need
char_field(choices,_) then
if choices is
{
no_choice then success("text(\""+to_upper(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_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
success("choices_text(\""+to_upper(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_name+", hk_form_selector(get_choices_selector_"+to_lower(type_name)+"_"+col_name+"), "+to_Anubis_source(help)+")")
else
success("text(\""+to_upper(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_name+", "+to_Anubis_source(help)+")")
}
password_field(min_size) then success("password(\""+to_upper(col_name)+"\", \""+col_name+"\", "+to_Anubis_source(help)+")")
text_field(txt_attr) then success("text_area(\""+to_upper(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_name+", "+to_Anubis_source(txt_attr)+", "+to_Anubis_source(help)+")"),
color_field then success("color(\""+to_upper(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_name+", "+to_Anubis_source(help)+")"),
phone_field(ph_attr) then success("phone(\""+to_upper(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_name+", "+to_Anubis_source(ph_attr)+", "+to_Anubis_source(help)+")"),
// mobile_field then success("mobile_phone(\""+to_upper(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_name+", "+to_Anubis_source(help)+")"),
url_field then success("url(\""+to_upper(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_name+", "+to_Anubis_source(help)+")"),
email_field then success("email(\""+to_upper(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_name+", "+to_Anubis_source(help)+")"),
}.
public define String
edit_entries
(
List(HK_Model_Column) columns,
String type_name,
String indent,
List(String) so_far
) =
if columns is
{
[] then indent+" "+join(",\n"+indent+" ", reverse(so_far)),
[h . t] then
if get_HK_Form_Entry(h, type_name) is
{
failure then edit_entries(t, type_name, indent, so_far)
success(result) then edit_entries(t, type_name, indent, [result . so_far])
}
}.
public define String
/** to_String return the content of the given model column 'col' in String format.
*
*/
to_String
(
HK_Model_Column col, //Model of the column
String current_type_name, //Instance name of the type i.e. toto (this is instance of Toto_Type below)
String general_type_name //Type declaration name i.e. Toto_Type
) =
since col is hk_column(name, col_type, _, _),
if col_type is
{
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+")",
boolean_field then "to_String("+current_type_name+"."+name+")",
date_field(attrs) then "to_String("+current_type_name+"."+name+")",
time_field(attrs) then "to_String("+current_type_name+"."+name+")",
datetime_field(attrs) then "to_String("+current_type_name+"."+name+")"
foreign_key(app,foreign_table,_,_,_) then "get_"+foreign_table+"_show_string(db, "+current_type_name+"."+name+")",
integer_field(_) then "to_String("+current_type_name+"."+name+")",
float_field then "float_to_string("+current_type_name+"."+name+",5)",
char_field(_,_) then current_type_name+"."+name,
password_field(_) then current_type_name+"."+name,
text_field(_) then current_type_name+"."+name,
color_field then current_type_name+"."+name,
phone_field(_) then current_type_name+"."+name,
url_field then current_type_name+"."+name,
email_field then current_type_name+"."+name
}.