web_args.anubis
13.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
/*
* Created by PyramIDE.
* User: フランスのトトロ aka (David RENÉ)
* Date: 16/03/2017
* Time: 23:38
* © David RENÉ
*/
read hayamiki_lib/types/hayamiki.anubis
read tools/streams.anubis
read fields.anubis
read columns.anubis
public define String
from_web_arg_to_type
(
HK_Model_Field t,
String name
) =
if t is
{
//anubis(_T) then "constant_byte_array(0,0)",
p_key then "with "+name+" = get_DB_id(lwa, __prefix__+\""+name+"\"),",
//binary then "constant_byte_array(0,0)",
boolean_field then "with "+name+" = get_Bool(lwa, __prefix__+\""+name+"\"),",
date_field(attrs) then if attrs = none then "get_DB_date(lwa, __prefix__+\""+name+"\")" else "with "+name+" = get_dummy_DB_date,",
time_field(attrs) then if attrs = none then "get_DB_time(lwa, __prefix__+\""+name+"\")" else "with "+name+" = get_dummy_DB_time,",
datetime_field(attrs) then if attrs = none then "get_DB_datetime(lwa, __prefix__+\""+name+"\")" else "with "+name+" = get_dummy_DB_datetime,",
foreign_key(_,_,_,_,_) then "with "+name+" = get_DB_id(lwa, __prefix__+\""+name+"\"),",
integer_field(_) then "get_Int(lwa, __prefix__+\""+name+"\")",
float_field then "get_Float(lwa, __prefix__+\""+name+"\")",
char_field(_,_) then "get_String(lwa, __prefix__+\""+name+"\")",
password_field(min_size) then "get_Password(lwa, __prefix__+\""+name+"_1\", __prefix__+\""+name+"_2\")",
text_field(_) then "get_String(lwa, __prefix__+\""+name+"\")",
color_field then "get_String(lwa, __prefix__+\""+name+"\")",
phone_field(_) then "get_String(lwa, __prefix__+\""+name+"\")",
// mobile_field then "get_String(lwa, __prefix__+\""+name+"\")",
url_field then "get_String(lwa, __prefix__+\""+name+"\")",
email_field then "get_String(lwa, __prefix__+\""+name+"\")"
}.
define (List(String), Int)
/*
*/
_generate_extract_web_arg_to_type
(
List(HK_Model_Column) columns,
List(String) so_far,
Int closure
)=
if columns is
{
[] then (reverse(so_far), closure),
[h . t ] then
since h is hk_column(name, col_type, _, _),
with result = from_web_arg_to_type(col_type, name),
//If the field is not editable no need to get it from the web
if result = "" then
_generate_extract_web_arg_to_type(t, so_far, closure)
else
if start_with(result, "with") then
_generate_extract_web_arg_to_type(t, [ " "+result . so_far], closure)
else
with line = " if "+fill(result, 70)+" is {failure then failure, "+fill("success("+name+")",35)+" then",
_generate_extract_web_arg_to_type(t, [ line . so_far], closure+1)
}
.
public define String
from_web_arg_to_type_with_default
(
HK_Model_Field col_type,
String name
) =
if col_type is
{
//anubis(_T) then "constant_byte_array(0,0)",
p_key then "get_DB_id(lwa, __prefix__+\""+name+"\"),",
//binary then "constant_byte_array(0,0)",
boolean_field then "get_Bool(lwa, __prefix__+\""+name+"\"),",
date_field(attrs) then if attrs = none then "get_DB_date(lwa, __prefix__+\""+name+"\", "+to_Anubis_default(col_type)+")," else "get_dummy_DB_date,",
time_field(attrs) then if attrs = none then "get_DB_time(lwa, __prefix__+\""+name+"\", "+to_Anubis_default(col_type)+")," else "get_dummy_DB_time,",
datetime_field(attrs) then if attrs = none then "get_DB_datetime(lwa, __prefix__+\""+name+"\", "+to_Anubis_default(col_type)+")," else "get_dummy_DB_datetime,",
foreign_key(_,_,_,_,_) then "get_DB_id(lwa, __prefix__+\""+name+"\"),",
integer_field(_) then "get_Int(lwa, __prefix__+\""+name+"\", "+to_Anubis_default(col_type)+"),",
float_field then "get_Float(lwa, __prefix__+\""+name+"\", "+to_Anubis_default(col_type)+"),",
char_field(_,_) then "get_String(lwa, __prefix__+\""+name+"\", "+to_Anubis_default(col_type)+"),",
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},",
text_field(_) then "get_String(lwa, __prefix__+\""+name+"\", "+to_Anubis_default(col_type)+"),",
color_field then "get_String(lwa, __prefix__+\""+name+"\", "+to_Anubis_default(col_type)+"),",
phone_field(_) then "get_String(lwa, __prefix__+\""+name+"\", "+to_Anubis_default(col_type)+"),",
// mobile_field then "get_String(lwa, __prefix__+\""+name+"\", "+to_Anubis_default(col_type)+"),",
url_field then "get_String(lwa, __prefix__+\""+name+"\", "+to_Anubis_default(col_type)+"),",
email_field then "get_String(lwa, __prefix__+\""+name+"\", "+to_Anubis_default(col_type)+"),"
}.
define List(String)
/*
*/
_generate_extract_web_arg_to_type_with_default
(
List(HK_Model_Column) columns,
List(String) so_far
)=
if columns is
{
[] then reverse(so_far),
[h . t ] then
since h is hk_column(name, col_type, _, _),
with result = from_web_arg_to_type_with_default(col_type, name),
//If the field is not editable no need to get it from the web
_generate_extract_web_arg_to_type_with_default(t, [ " "+result . so_far])
}
.
public define Maybe(One)
/* generate the function which extract the entire database table row type from web
* arguments and return maybe that type.
*/
generate_extract_web_arg_to_type
(
Stream stream,
HK_Table table
)=
since table is hk_table(name, short_name, model, _),
since model is hk_model(columns, constraint, _),
with type_name = to_upper(name, 1), //make the first character to upper case to able to be a Anubis Type.
since _generate_extract_web_arg_to_type(columns, [], 0) is ((List(String))list_of_web_arg, (Int) nb_closure),
//generate the type of the table that contain all components
if write_string(stream,
"\n\npublic define Maybe("+type_name+")\n"+
" extract_"+type_name+"_from_web_arg\n"+
" (\n"+
" List(Web_arg) lwa,\n"+
" String __prefix__\n"+
" )=\n"+ //head of the definition
to_String(list_of_web_arg)+ //all editable components of the type
" success(\n"+generate_constructor(name, columns, " ")+")\n"+ //generate type construction with values got from web arg
" "+fill(nb_closure, '}')+ //add close } according to number of web arg
".\n\n"+
"\n\npublic define "+type_name+"\n"+
" extract_"+type_name+"_from_web_arg_with_default\n"+
" (\n"+
" List(Web_arg) lwa,\n"+
" String __prefix__\n"+
" )=\n"+ //head of the definition
" "+name+"(\n"+
to_String(_generate_extract_web_arg_to_type_with_default(columns, []))+
")\n"+ //generate type construction with values got from web arg
".\n\n"
) is //end of the function
{
failure then println("can't write extract_"+type_name+"_from_web_arg "+type_name); failure,
success(_) then success(unique)
}
.
/***** UPDATE FIELD ********/
public define String
from_web_arg_to_update_field
(
HK_Model_Field t,
String name
) =
if t is
{
//anubis(_T) then "constant_byte_array(0,0)",
p_key then
if name ="id" then
"[]"
else
"if get_mb_DB_id(lwa, __prefix__+\""+name+"\") is {failure then [], success("+name+") then [field(\""+name+"\", bind_DB_id_or_NULL(\":v_"+name+"\", "+name+"))] }",
boolean_field then
"if get_mb_Bool(lwa, __prefix__+\""+name+"\") is {failure then [], success("+name+") then [field(\""+name+"\", bind_Bool(\":v_"+name+"\", "+name+"))] }",
date_field(attrs) then
if attrs is
{
none then "if get_DB_date(lwa, __prefix__+\""+name+"\") is {failure then [], success("+name+") then [field(\""+name+"\", bind_Date(\":v_"+name+"\", "+name+"))] }",
auto_now then "[field(\""+name+"\", bind_Date(\":v_"+name+"\", db_now))]",
auto_now_add then "[]"
},
time_field(attrs) then
if attrs is
{
none then "if get_DB_time(lwa, __prefix__+\""+name+"\") is {failure then [], success("+name+") then [field(\""+name+"\", bind_Time(\":v_"+name+"\", "+name+"))] }",
auto_now then "[field(\""+name+"\", bind_Time(\":v_"+name+"\", db_now))]",
auto_now_add then "[]"
},
datetime_field(attrs) then
if attrs is
{
none then "if get_DB_datetime(lwa, __prefix__+\""+name+"\") is {failure then [], success("+name+") then [field(\""+name+"\", bind_Datetime(\":v_"+name+"\", "+name+"))] }",
auto_now then "[field(\""+name+"\", bind_Datetime(\":v_"+name+"\", db_now))]",
auto_now_add then "[]"
},
foreign_key(_,_,_,_,_) then //"with "+name+" = get_DB_id(lwa, __prefix__+\""+name+"\"),",
"if get_mb_DB_id(lwa, __prefix__+\""+name+"\") is {failure then [], success("+name+") then [field(\""+name+"\", bind_DB_id_or_NULL(\":v_"+name+"\", "+name+"))] }"
integer_field(_) then //"get_Int(lwa, __prefix__+\""+name+"\")",
"if get_Int(lwa, __prefix__+\""+name+"\") is {failure then [], success("+name+") then [field(\""+name+"\", bind_Int(\":v_"+name+"\", "+name+"))] }"
float_field then //"get_Float(lwa, __prefix__+\""+name+"\")",
"if get_Float(lwa, __prefix__+\""+name+"\") is {failure then [], success("+name+") then [field(\""+name+"\", bind_Float(\":v_"+name+"\", "+name+"))] }"
char_field(_,_) then
"if get_String(lwa, __prefix__+\""+name+"\") is {failure then [], success("+name+") then [field(\""+name+"\", bind_String(\":v_"+name+"\", "+name+"))] }",
password_field(min_size) then
"(if get_String(lwa, \"password_pwd_change_option\", \"false\") = \"false\" then \n"+
" if get_Password(lwa, __prefix__+\""+name+"_1\", __prefix__+\""+name+"_2\") is {failure then [], success("+name+") then [field(\""+name+"\", bind_String(\":v_"+name+"\", "+name+"))] }\n"+
" else\n"+
" if get_Bool(lwa, __prefix__+\""+name+"_pwd_change\") then \n"+
" if get_Password(lwa, __prefix__+\""+name+"_1\", __prefix__+\""+name+"_2\") is {failure then [], success("+name+") then [field(\""+name+"\", bind_String(\":v_"+name+"\", "+name+"))] }\n"+
" else\n"+
" []\n"+
" )"
// if get_Password(lwa, __prefix__+"password_1", __prefix__+"password_2") is {failure then [], success(password) then [field("passwxord", bind_String(":v_password", password))] }
text_field(_) then
"if get_String(lwa, __prefix__+\""+name+"\") is {failure then [], success("+name+") then [field(\""+name+"\", bind_String(\":v_"+name+"\", "+name+"))] }",
color_field then
"if get_String(lwa, __prefix__+\""+name+"\") is {failure then [], success("+name+") then [field(\""+name+"\", bind_String(\":v_"+name+"\", "+name+"))] }"
phone_field(_) then
"if get_String(lwa, __prefix__+\""+name+"\") is {failure then [], success("+name+") then [field(\""+name+"\", bind_String(\":v_"+name+"\", "+name+"))] }"
// mobile_field then
// "if get_String(lwa, __prefix__+\""+name+"\") is {failure then [], success("+name+") then [field(\""+name+"\", bind_String(\":v_"+name+"\", "+name+"))] }"
url_field then
"if get_String(lwa, __prefix__+\""+name+"\") is {failure then [], success("+name+") then [field(\""+name+"\", bind_String(\":v_"+name+"\", "+name+"))] }"
email_field then
"if get_String(lwa, __prefix__+\""+name+"\") is {failure then [], success("+name+") then [field(\""+name+"\", bind_String(\":v_"+name+"\", "+name+"))] }"
}.
define String
/*
*/
_generate_extract_web_arg_to_update_field
(
List(HK_Model_Column) columns,
)=
join("+\n",
map((HK_Model_Column col) |->
since col is hk_column(name, col_type, _, _),
" "+from_web_arg_to_update_field(col_type, name),
columns
)
)
.
public define Maybe(One)
/* generate the function which extract the entire or part database table row type
* from web arguments and return a list of table fields presents in that web args.
*/
generate_extract_web_arg_to_update_field
(
Stream stream,
HK_Table table
)=
since table is hk_table(name, short_name, model, _),
since model is hk_model(columns, constraints, _),
with type_name = to_upper(name, 1), //make the first character to upper case to able to be a Anubis Type.
//generate the type of the table that contain all components
if write_string(stream,
"\n\npublic define List(SQLite3_update_field)\n"+
" extract_"+type_name+"_update_fields_from_web_arg\n"+
" (\n"+
" List(Web_arg) lwa,\n"+
" String __prefix__\n"+
" )=\n"+ //head of the definition
_generate_extract_web_arg_to_update_field(columns)+ //all editable components of the type
"\n.\n") is //end of the function
{
failure then println("can't write extract_"+type_name+"_update_fields_from_web_arg "+type_name); failure,
success(_) then success(unique)
}
.