columns.anubis
11.2 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
/*
* Created by PyramIDE.
* User: フランスのトトロ aka (David RENÉ)
* Date: 13/02/2016
* Time: 11:05
* © Calexium
*/
read system/string.anubis
read hayamiki_lib/types/model/columns.anubis
read fields.anubis
define List(String)
/* return the list of all columns name
*/
_to_List_String
(
List(HK_Model_Column) columns,
Bool with_pk,
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(t, with_pk, so_far)
else
_to_List_String(t, with_pk, [ 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, []).
define List(String)
/**
*/
components
(
List(HK_Model_Column) columns,
String cursor_name,
List(String) so_far,
Int idx
)=
if columns is
{
[] then reverse(so_far),
[h . t ] then
since h is hk_column(name, col_type, _, _),
with line = fill(from_DB_cursor(col_type, cursor_name, idx), 35)+ "/* "+name+" */",
components(t, cursor_name, [line . so_far], idx + 1 )
}.
public define String
generate_constructor_from_cursor
(
String constructor_name,
String cursor_name,
List(HK_Model_Column) columns,
String indent
)=
indent+constructor_name+"(\n"+indent+" "+join(",\n"+indent+" ", components(columns, cursor_name, [], 0))+"\n"+indent+")".
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, _, _),
with result = to_Bind(col_type, 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)
get_column_name
(
HK_Model_Column col,
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 failure,
//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),
char_field(Int size) then success(name),
text_field then success(name)
}.
public define List(String)
columns_name
(
List(HK_Model_Column) columns,
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, insert) is
{
failure then columns_name(t, insert, so_far)
success(result) then
if insert then
columns_name(t, insert, [result . so_far])
else
columns_name(t, 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,
String indent
)=
with columns_list = columns_name(columns, 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, []),
indent+" "+join(", ", columns_list).
public define Maybe(String)
get_VT_edit_entry
(
HK_Model_Column col,
String type_name
) =
since col is hk_column(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+"."+name+"))"),
//binary then "constant_byte_array(0,0)",
boolean_field then success("boolean(\""+to_upper(name)+"\", \""+name+"\", "+type_name+"."+name+", "+to_Anubis_source(help)+")"),
date_field(attrs) then if attrs is
{
none then success("date(\""+to_upper(name)+"\", \""+name+"\", "+type_name+"."+name+".date, "+to_Anubis_source(help)+")"),
auto_now then success("information(\""+to_upper(name)+"\", \""+name+"\", "+type_name+"."+name+".date, "+to_Anubis_source(help)+")"),
auto_now_add then success("information(\""+to_upper(name)+"\", \""+name+"\", "+type_name+"."+name+".date, "+to_Anubis_source(help)+")")
},
time_field(attrs) then if attrs is
{
none then success("time(\""+to_upper(name)+"\", \""+name+"\", "+type_name+"."+name+".time, "+to_Anubis_source(help)+")"),
auto_now then success("information(\""+to_upper(name)+"\", \""+name+"\", "+type_name+"."+name+".time, "+to_Anubis_source(help)+")"),
auto_now_add then success("information(\""+to_upper(name)+"\", \""+name+"\", "+type_name+"."+name+".time, "+to_Anubis_source(help)+")")
},
datetime_field(attrs) then if attrs is
{
none then success("datetime(\""+to_upper(name)+"\", \""+name+"\", "+type_name+"."+name+".datetime, "+to_Anubis_source(help)+")"),
auto_now then success("information(\""+to_upper(name)+"\", \""+name+"\", "+type_name+"."+name+".datetime, "+to_Anubis_source(help)+")"),
auto_now_add then success("information(\""+to_upper(name)+"\", \""+name+"\", "+type_name+"."+name+".datetime, "+to_Anubis_source(help)+")")
},
foreign_key(app,foreign_table) then success("foreign_text(\""+to_upper(name)+"\", \""+name+"\", "+type_name+"."+name+", vt_edit_selector(get_selector_"+foreign_table+"), "+to_Anubis_source(help)+")"),
integer_field then success("integer(\""+to_upper(name)+"\", \""+name+"\", "+type_name+"."+name+", "+to_Anubis_source(help)+")"),
char_field(Int size) then success("text(\""+to_upper(name)+"\", \""+name+"\", "+type_name+"."+name+", "+to_Anubis_source(help)+")"),
text_field then success("text_area(\""+to_upper(name)+"\", \""+name+"\", "+type_name+"."+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_VT_edit_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
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(HK_Model_Column)
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 String
/** to_String return the String of the given model of the column 'col'
*/
to_String
(
HK_Model_Column col,
String current_type_name,
String general_type_name
) =
since col is hk_column(name, col_type, _, _),
if col_type is
{
//anubis(_T) then "constant_byte_array(0,0)",
p_key then "get_"+general_type_name+"_show_string(db, "+current_type_name+".id)",
//binary then "constant_byte_array(0,0)",
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+")",
char_field(Int size) then current_type_name+"."+name,
text_field then current_type_name+"."+name
}.