web_args.anubis
8.61 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
/*
* Created by PyramIDE.
* User: フランスのトトロ aka (David RENÉ)
* Date: 16/03/2017
* Time: 23:38
* © Calexium
*/
read hayamiki_lib/types/hayamiki.anubis
read tools/streams.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+"\")"
}.
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 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, _),
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") 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 "[]",
boolean_field then
"with "+name+" = get_Bool(lwa, __prefix__+\""+name+"\"), [field(\""+name+"\", bind_Bool(\":v_"+name+"\", "+name+"))]",
date_field(attrs) then
if attrs = none then
"if get_DB_date(lwa, __prefix__+\""+name+"\") is {failure then [], success("+name+") then [field(\""+name+"\", bind_Date(\":v_"+name+"\", "+name+"))] }"
else
"[]",
time_field(attrs) then
if attrs = none then
"if get_DB_time(lwa, __prefix__+\""+name+"\") is {failure then [], success("+name+") then [field(\""+name+"\", bind_Time(\":v_"+name+"\", "+name+"))] }"
else
"[]",
datetime_field(attrs) then
if attrs = none then
"if get_DB_datetime(lwa, __prefix__+\""+name+"\") is {failure then [], success("+name+") then [field(\""+name+"\", bind_Datetime(\":v_"+name+"\", "+name+"))] }"
else
"[]",
foreign_key(_,_) then //"with "+name+" = get_DB_id(lwa, __prefix__+\""+name+"\"),",
"with "+name+" = get_DB_id(lwa, __prefix__+\""+name+"\"), [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"+
" []"
// 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+"))] }"
//"get_String(lwa, __prefix__+\""+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, _),
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)
}
.