extractor.anubis
5.33 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
/*
* 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
}.
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, _),
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+"])".
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, _),
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"
) is //end of the function
{
failure then println("can't write generate_extract_db_cursor "+type_name); failure,
success(_) then success(unique)
}.