extractor.anubis
7.97 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
/*
* Created by PyramIDE.
* User: フランスのトトロ aka (David RENÉ)
* Date: 25/03/2017
* Time: 03:18
* © Calexium
*/
read hayamiki_lib/types/hayamiki.anubis
read json.anubis
read tools/streams.anubis
public define String
from_DB_cursor
(
HK_Model_Field t,
String cursor, //db cursor name
Int index //current index of the column in the cursor
) =
with cursor_index = "("+cursor+")("+index+")",
if t is
{
p_key then "db_id((Int)db_integer"+cursor_index+")",
boolean_field then "db_bool"+cursor_index,
date_field(_) then "db_date(text"+cursor_index+")",
time_field(_) then "db_time(text"+cursor_index+")",
datetime_field(_) then "db_datetime(text"+cursor_index+")",
foreign_key(_,_,_) then "db_id((Int)db_integer"+cursor_index+")",
integer_field(_) then "(Int)db_integer"+cursor_index,
float_field then "db_float"+cursor_index,
char_field(_,_) then "text"+cursor_index,
password_field(_) then "text"+cursor_index,
text_field(_) then "text"+cursor_index,
color_field then "text"+cursor_index,
phone_field then "text"+cursor_index,
mobile_field then "text"+cursor_index,
url_field then "text"+cursor_index,
email_field then "text"+cursor_index
}.
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 Maybe(One)
generate_extract_db_cursor_to_mb_type
(
Stream stream,
HK_Table table
)=
since table is hk_table(name, short_name, model, display),
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.
//generate the type of the table that contain all components
if write_string(stream,
"\n\n"+
"public define Maybe("+type_name+")\n"+
" extract_"+name+"\n"+
" (\n"+
" One -> SQLite3Row table_cursor\n"+
" ) =\n"+
" if table_cursor(unique) is\n"+
" {\n"+
" error(sql_error) then println(db_error(sql_error, \"extract_"+name+"\")); failure,\n"+
" no_more_row then failure,\n"+
" row(cursor) then\n"+
" success(\n"+generate_constructor_from_cursor(name, "cursor", columns, " ")+")\n"+ //generate type construction with values got from web arg
" }.\n") is //end of the function
{
failure then println("can't write generate_extract_db_cursor "+type_name); failure,
success(_) then success(unique)
}.
/************* JSON *************/
define List(String)
/**
*/
to_json_member
(
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_to_JSON(h, cursor_name, idx), 135)+ "/* "+name+" */",
to_json_member(t, cursor_name, [line . so_far], idx + 1 )
}
.
public define String
generate_json_object_from_cursor
(
String constructor_name,
String cursor_name,
List(HK_Model_Column) columns,
String indent
)=
indent+"json_object([\n"+indent+" "+join(",\n"+indent+" ", to_json_member(columns, cursor_name, [], 0))+"\n"+indent+"])"
.
define List(String)
/**
*/
to_get_extractor_line_json_member
(
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 = "if component_name =\""+name+"\" then ((Int -> SQLite3Datum) cursor) |-> "+from_DB_cursor_to_JSON(h, cursor_name, idx),
to_get_extractor_line_json_member(t, cursor_name, [line . so_far], idx + 1 )
}
.
public define String
generate_json_extractor
(
String constructor_name,
String cursor_name,
List(HK_Model_Column) columns,
String indent
)=
indent+join("\n"+indent+"else ", to_get_extractor_line_json_member(columns, cursor_name, [], 0))+
"\n"+indent+"else ((Int -> SQLite3Datum) cursor) |-> json_member(component_name, json_null)".
public define Maybe(One)
generate_extract_db_cursor_to_json
(
Stream stream,
HK_Table table
)=
since table is hk_table(name, short_name, model, display),
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\n"+
// "public define Maybe(JsonValue)\n"+
// " extract_"+name+"_cursor_to_json\n"+
// " (\n"+
// " SQLite3DataBase db,\n"+
// " One -> SQLite3Row table_cursor\n"+
// " ) =\n"+
// " if table_cursor(unique) is\n"+
// " {\n"+
// " error(sql_error) then println(db_error(sql_error, \"extract_"+name+"\")); failure,\n"+
// " no_more_row then failure,\n"+
// " row(cursor) then\n"+
// " success("+generate_json_object_from_cursor(name, "cursor", columns, " ")+")\n"+ //generate type construction with values got from web arg
// " }\n.\n"+
// "\n\n"+
"public define Maybe(JsonValue)\n"+
" extract_"+name+"_cursor_to_json\n"+
" (\n"+
" SQLite3DataBase db,\n"+
" List(((Int -> SQLite3Datum) -> JsonMember)) extractors,\n"+
" One -> SQLite3Row table_cursor\n"+
" ) =\n"+
" if table_cursor(unique) is\n"+
" {\n"+
" error(sql_error) then println(db_error(sql_error, \"extract_"+name+"\")); failure,\n"+
" no_more_row then failure,\n"+
" row(cursor) then\n"+
" success(\n"+
" json_object(\n"+
" map(((Int -> SQLite3Datum) -> JsonMember extractor) |-> extractor(cursor) , extractors)\n"+
" ))\n"+
" }\n.\n\n"+
"public define String get_"+name+"_show_string(SQLite3DataBase db, DB_id _idx).\n\n"+
"define (Int -> SQLite3Datum) -> JsonMember\n"+
" get_extractor\n"+
" (\n"+
" SQLite3DataBase db,\n"+
" String component_name,\n"+
" Bool resolve_fk\n"+
" )=\n"+generate_json_extractor(name, "cursor", columns, " ")+
"\n.\n\n"+
"define List(((Int -> SQLite3Datum) -> JsonMember))\n"+
" make_json_extractor\n"+
" (\n"+
" SQLite3DataBase db,\n"+
" List(String) columns,\n"+
" Bool resolve_fk\n"+
" )=\n"+
" map((String column) |-> get_extractor(db, column, resolve_fk), columns)\n"+
".\n"
) is //end of the function
{
failure then println("can't write generate_extract_db_cursor "+type_name); failure,
success(_) then success(unique)
}.