db_types.anubis
7.27 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/*
* Created by PyramIDE.
* User: フランスのトトロ aka (David RENÉ)
* Date: 14/11/2011
* Time: 22:41
*
*/
transmit system/convert.anubis
transmit tools/basis.anubis
transmit tools/ISO-8601.anubis
transmit xlib/database/types/db_id.anubis
transmit xlib/database/types/db_date.anubis
transmit xlib/database/types/db_time.anubis
transmit xlib/database/types/db_datetime.anubis
transmit xlib/database/types/db_integer.anubis
transmit xlib/web/json.anubis
/* DB_id will is deprecated. Please use next version below all upper case */
//public type DB_id:
// none,
// db_id(Int value).
//public type DB_ID:
// none,
// pk(Int value).
public define Int -> DB_id
db_id_or_null
(
Int -> SQLite3Datum row
) =
(Int i) |->
if row(i) is integer(n) then db_id(n)
else if row(i) is null then none
else if row(i) is text(txt) then
//println("db_id_or_null text "+txt);
if decimal_scan(txt) is success(val) then
db_id(val)
else
none
else
none
.
public define Maybe(One)
add_DB_id
(
Message msg,
String name,
DB_id db_id
)=
add_message(msg, name, to_Message(db_id))
.
public define Maybe(DB_id)
find_DB_id
(
Message msg,
String name,
)=
if find_message(msg, name) is
{
failure then failure,
success(_db_id__msg) then (Maybe(DB_id))from_Message(_db_id__msg)
}
.
public define DB_id
find_DB_id
(
Message msg, //Message in where we search the DB_id
String name, //name of DB_id in the Message
DB_id default //default value
)=
if find_DB_id(msg, name) is
{
failure then default,
success(db_id) then db_id
}
.
public define String
to_String
(
DB_id idx
)=
if idx is
{
none then "NULL",
db_id(_idx) then abs_to_decimal(_idx)
}.
public define String
to_SQL
(
DB_id idx
)=
if idx is
{
none then "NULL",
db_id(_idx) then abs_to_decimal(_idx)
}.
public define Maybe(Int)
to_mb_Int
(
DB_id idx
)=
if idx is
{
none then failure,
db_id(_idx) then success(_idx)
}.
public define DB_id
db_id
(
Maybe(Int) mb_int
)=
if mb_int is
{
failure then none,
success(id) then db_id(id)
}
.
public define JsonValue
to_JsonValue
(
DB_id idx
)=
if idx is
{
none then json_string("none"),
db_id(_idx) then json_int(_idx)
}.
public define String
String s + DB_id idx =
s + to_String(idx)
.
public define String
DB_id idx + String s =
to_String(idx) + s
.
public define Int
to_Int
(
DB_id idx
)=
if idx is
{
none then (Int)0,
db_id(_idx) then _idx
}
.
public define Word32
to_Word32
(
DB_id idx
)=
if idx is
{
none then 0xFFFFFFFF,
db_id(_idx) then truncate_to_Word32(_idx)
}
.
public define DB_id
to_DB_id
(
Int value
)=
if value < 0 then
none
else
db_id(value)
.
public define Maybe(DB_id)
to_mb_DB_id
(
String db_id_str
)=
if decimal_scan(db_id_str) is
{
failure then failure,
success(db_id_int) then success(to_DB_id(db_id_int))
}
.
public define Maybe(DB_id)
to_mb_DB_id
(
Maybe(String) mb_db_id_str
)=
if mb_db_id_str is
{
failure then failure,
success(db_id_str) then to_mb_DB_id(db_id_str)
}
.
/**
* produce path from db_id to optimize fs access and never have more than 100 folders
*/
public define String
to_fs_folder
(
DB_id idx
)=
if idx is
{
none then "none/",
db_id(_idx) then
with idx_str = abs_to_decimal(_idx),
//if the idx value is less than 10, we add 0 as prefix of that idx. i.e. if the idx is 3 the final_idx_str will be 03 which will be cut as 0/3/.
//the idx < 10 will be located in the 0 folder.
with final_idx_str= if _idx < 10 then "0"+idx_str else idx_str,
with result_path = join("/", reverse(map_select((Bool first, Word8 char) |-> if first then (false, failure) else (false, success(implode([char]))), true, reverse(explode(final_idx_str)))) + [idx_str])+"/",
//println("to_fs_folder result_path "+result_path);
result_path
}
.
//public define String
// to_String
// (
// DB_ID idx
// )=
// if idx is
// {
// none then "none",
// pk(_idx) then abs_to_decimal(_idx)
// }.
//
//public define Int
// to_Int
// (
// DB_ID idx
// )=
// if idx is
// {
// none then (Int)-1,
// pk(_idx) then _idx
// }.
public define String
to_String
(
DB_date d
)=
date(d).
public define String
String s + DB_date d =
s + to_String(d)
.
public define String
String s + Maybe(DB_date) mb_date =
if mb_date is
{
failure then s + "unknow date",
success(date) then s + date
}
.
public define String
DB_date d + String s =
to_String(d) + s
.
public define String
date_not_null
(
DB_date d,
String default
)=
if d.date = "{NULL}" then
default
else
d.date
.
public define String
datetime_not_null
(
DB_datetime d,
String default
)=
if d.datetime = "{NULL}" then
default
else
d.datetime
.
public define String
to_String
(
DB_datetime dt
)=
datetime_not_null(dt, "").
public define String
String s + DB_datetime dt =
s + to_String(dt)
.
public define String
DB_datetime dt + String s =
to_String(dt) + s
.
public define Bool
DB_datetime a < DB_datetime b
=
//convert datetime in String to allows String comparison
a.datetime < b.datetime
.
public define String
get_time
(
DB_datetime dt
)=
with ba_dt = to_byte_array(dt.datetime),
to_string(extract(ba_dt, 11, 19))
.
public define String
get_date
(
DB_datetime dt
)=
with ba_dt = to_byte_array(dt.datetime),
to_string(extract(ba_dt, 0, 10))
.
public define String
to_String
(
DB_time t
)=
time(t).
public define String
String s + DB_time t =
s + to_String(t)
.
public define String
DB_time t + String s =
to_String(t) + s
.
public define String
to_DBString
(
Bool value
)=
if value then "1" else "0".
public define Int
to_DBInt
(
Bool value
)=
if value then 1 else 0.
* Some Bind helper *
public define DB_date
datetime_to_date
(
DB_datetime dt
)=
db_date(get_date(dt))
.
public define Maybe(DB_date)
datetime_to_mb_date
(
DB_datetime dt
)=
success(db_date(get_date(dt)))
.
public define Maybe(DB_date)
mb_datetime_to_mb_date
(
Maybe(DB_datetime) mb_dt
)=
if mb_dt is
{
failure then failure,
success(dt) then success(db_date(get_date(dt)))
}
.
public define Maybe(DB_date)
to_mb_date
(
String _date
)=
if _ISO_8601_date_to_Int(_date) is
{
failure then failure,
success(int_date) then success(db_date(_Int_to_ISO_8601_date(int_date)))
}
.
/********** Some default type values ********************/
public define DB_datetime
get_dummy_DB_datetime
=
db_datetime("").
public define DB_datetime
db_now
=
db_datetime(_Int_to_ISO_8601_datetime(now)).
public define DB_date
db_now
=
db_date(_Int_to_ISO_8601_date(now)).
public define DB_time
db_now
=
db_time(_Int_to_ISO_8601_time(now)).
public define DB_time
get_dummy_DB_time
=
db_time("").
public define DB_date
get_dummy_DB_date
=
db_date("").