SQL_get_all.anubis
5.58 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
/*
* Created by PyramIDE.
* User: フランスのトトロ aka (David RENÉ)
* Date: 17/05/2025
* Time: 23:52
* © David RENÉ
*/
read hayamiki_lib/types/hayamiki.anubis
read tools/streams.anubis
read ../columns.anubis
public define Maybe(One)
generate_postgresql_get_all
(
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\n"+
"public define List("+type_name+")\n"+
" _get_all_"+name+"\n"+
" (\n"+
" One -> DbRow table_cursor,\n"+
" List("+type_name+") so_far\n"+
" ) =\n"+
" if extract_"+name+"(table_cursor) is\n"+
" {\n"+
" failure then reverse(so_far),\n"+
" success(data) then _get_all_"+name+"(table_cursor, [data . so_far])\n"+
" }.\n"+
"\n\n"
//SQL query
// "public define List("+type_name+")\n"+
// " get_all_"+name+"\n"+
// " (\n"+
// " SQLite3DataBase db,\n"+
// " )=\n"+
// " if sql_query_timeout(db, \"SELECT "+join(", ", enclose("\\\"",to_List_String(columns, true)))+" FROM "+name+";\", [], \"get_all_"+name+"\") is\n"+
// " {\n"+
// " error(_) then [],\n"+
// " ok(_, cursor, _) then _get_all_"+name+"(cursor, [])\n"+
// " }.\n"+
// "\n\n"
) is //end of the function
{
failure then println("can't write generate_get_all "+type_name); failure,
success(_) then success(unique)
}.
//public define JsonMember
// get_component
// (
// String component_name,
// Crm_price_level type
// )=
// if component_name = "id" then json_member(component_name, json_int(type.id))
// else if component_name = "code" then json_member(component_name, json_string(type.code))
// else if component_name = "label" then json_member(component_name, json_string(type.label))
// else if component_name = "active" then json_member(component_name, json_bool(type.active))
// else if component_name = "module" then json_member(component_name, json_string(type.module))
// else json_member(component_name, json_null)
//.
public define Maybe(One)
generate_postgresql_get_all_with_clause
(
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,
//SQL query
"public define Maybe(One -> BdRow)\n"+
" get_all_"+name+"_by_clause\n"+
" (\n"+
" DataBase db,\n"+
" String clause\n"+
" )=\n"+
" with final_clause = if clause = \"\" then\n"+
" \"\"\n"+
" else\n"+
" \"WHERE \"+clause,\n"+
"\n"+
" if sql_query_timeout(db, \"SELECT "+join(", ", enclose("\\\"",to_List_String(columns, true)))+" FROM "+name+" \"+final_clause+\";\", [], \"get_all_"+name+"_by_clause\") is\n"+
" {\n"+
" error(_) then failure,\n"+
" ok(_, cursor, _) then success(cursor)\n"+
" }\n"+
".\n"+
"\n"+
"public define List("+type_name+")\n"+
" get_all_"+name+"_by_clause_to_list_type\n"+
" (\n"+
" DataBase db,\n"+
" String clause\n"+
" )=\n"+
" if get_all_"+name+"_by_clause(db, clause) is\n"+
" {\n"+
" failure then [],\n"+
" success(cursor) then _get_all_"+name+"(cursor, [])\n"+
" }\n"+
".\n"+
//"public define List(JsonValue)\n"+
//" _get_all_"+name+"_to_json\n"+
//" (\n"+
//" SQLite3DataBase db,\n"+
//" List(((Int -> SQLite3Datum) -> JsonMember)) extractors,\n"+
//" One -> SQLite3Row table_cursor,\n"+
//" List(JsonValue) so_far\n"+
//" ) =\n"+
//" if extract_"+name+"_cursor_to_json(db, extractors, table_cursor) is\n"+
//" {\n"+
//" failure then reverse(so_far),\n"+
//" success(data) then _get_all_"+name+"_to_json(db, extractors, table_cursor, [data . so_far])\n"+
//" }\n"+
//".\n"+
"public define JsonValue\n"+
" get_all_"+name+"_by_clause_to_json\n"+
" (\n"+
" DataBase db,\n"+
" List(String) columns,\n"+
" Bool resolve_fk,\n"+
" HK_Referer referer,\n"+
" String filter,\n"+
" String order_by\n"+
" )=\n"+
" with ref_clause = referer_to_sql_clause(referer, \""+name+"\", "+name+"_columns),\n"+
" with final_columns = if length(columns) = 0 then [\"id\". "+name+"_columns] else columns,\n\n"+
" with _and_ = if length(ref_clause) > 0 & length(filter) > 0 then \" AND \" else \"\",\n"+
" if get_all_"+name+"_by_clause(db, ref_clause+_and_+filter+\" \"+order_by) is\n"+
" {\n"+
" failure then json_array([]),\n"+
" success(cursor) then\n"+
" json_array(\n"+
//" _get_all_"+name+"_to_json(db, make_"+name+"_json_extractor(db, final_columns, resolve_fk), cursor, [])\n"+
" get_all_to_json(db, make_"+name+"_json_extractor(db, final_columns, resolve_fk), cursor, [])\n"+
" )\n"+
" }\n"+
".\n"
) is //end of the function
{
failure then println("can't write generate_get_all "+type_name); failure,
success(_) then success(unique)
}
.