forked from bloomberg/comdb2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathltypes.c
2320 lines (2087 loc) · 62.5 KB
/
ltypes.c
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2015 Bloomberg Finance L.P.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <stddef.h>
#include <limits.h>
#include <math.h>
#include <time.h>
#include <assert.h>
#include <alloca.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <luautil.h>
#include <comdb2.h>
#include <sql.h>
#include <flibc.h>
#include <luaglue.h>
#include <sp.h>
dbtypes_t dbtypes;
#define XMACRO_DBTYPES(type, name, structname, tostring) name,
const char *dbtypes_str[] = { DBTYPES };
#undef XMACRO_DBTYPES
static int l_int_tostring(Lua);
static int l_cstring_tostring(Lua);
static int l_real_tostring(Lua);
static int l_datetime_tostring(Lua);
static int l_decimal_tostring(Lua);
static int l_blob_tostring(Lua);
static int l_intervalym_tostring(Lua);
static int l_intervalds_tostring(Lua);
#define XMACRO_DBTYPES(type, name, structname, tostring) tostring,
const tostringfunc dbtypes_tostring[] = { DBTYPES };
#undef XMACRO_DBTYPES
#define nullchk(lua, idx) \
do { \
if (luabb_dbtype(lua, idx) > DBTYPES_MINTYPE) { \
const lua_dbtypes_t *t = lua_topointer(lua, idx); \
if (t->is_null) { \
return luaL_error( \
lua, "attempt to perform arithmetic on a NULL value"); \
} \
} \
} while (0)
static int typecmperr(Lua lua, int i1, int i2)
{
return luabb_error(lua, NULL, "attempt to compare %s with %s",
luabb_dbtypename(lua, i1), luabb_dbtypename(lua, i2));
}
static int l_datetime_tostring_int(Lua, int);
static int l_blob_tostring_int(Lua, int);
void datetime_t_to_client_datetime(const datetime_t *in, cdb2_client_datetime_t *out)
{
out->tm.tm_sec = htonl(in->tm.tm_sec);
out->tm.tm_min = htonl(in->tm.tm_min);
out->tm.tm_hour = htonl(in->tm.tm_hour);
out->tm.tm_mday = htonl(in->tm.tm_mday);
out->tm.tm_mon = htonl(in->tm.tm_mon);
out->tm.tm_year = htonl(in->tm.tm_year);
out->tm.tm_wday = htonl(in->tm.tm_wday);
out->tm.tm_yday = htonl(in->tm.tm_yday);
out->tm.tm_isdst = htonl(in->tm.tm_isdst);
out->msec = (in->prec == DTTZ_PREC_MSEC) ?
htonl(in->frac) :
htonl(in->frac * 1000);
strcpy(out->tzname, in->tzname);
}
void datetime_t_to_client_datetimeus(const datetime_t *in, cdb2_client_datetimeus_t *out)
{
out->tm.tm_sec = htonl(in->tm.tm_sec);
out->tm.tm_min = htonl(in->tm.tm_min);
out->tm.tm_hour = htonl(in->tm.tm_hour);
out->tm.tm_mday = htonl(in->tm.tm_mday);
out->tm.tm_mon = htonl(in->tm.tm_mon);
out->tm.tm_year = htonl(in->tm.tm_year);
out->tm.tm_wday = htonl(in->tm.tm_wday);
out->tm.tm_yday = htonl(in->tm.tm_yday);
out->tm.tm_isdst = htonl(in->tm.tm_isdst);
out->usec = (in->prec == DTTZ_PREC_MSEC) ?
htonl(in->frac) * 1000 :
htonl(in->frac);
strcpy(out->tzname, in->tzname);
}
void client_datetime_to_datetime_t(const cdb2_client_datetime_t *cdt,
datetime_t *datetime, int flip)
{
datetime->prec = DTTZ_PREC_MSEC;
strcpy(datetime->tzname, cdt->tzname);
if (flip) {
datetime->tm.tm_sec = flibc_intflip(cdt->tm.tm_sec);
datetime->tm.tm_min = flibc_intflip(cdt->tm.tm_min);
datetime->tm.tm_hour = flibc_intflip(cdt->tm.tm_hour);
datetime->tm.tm_mday = flibc_intflip(cdt->tm.tm_mday);
datetime->tm.tm_mon = flibc_intflip(cdt->tm.tm_mon);
datetime->tm.tm_year = flibc_intflip(cdt->tm.tm_year);
datetime->tm.tm_wday = flibc_intflip(cdt->tm.tm_wday);
datetime->tm.tm_yday = flibc_intflip(cdt->tm.tm_yday);
datetime->tm.tm_isdst = flibc_intflip(cdt->tm.tm_isdst);
datetime->frac = flibc_intflip(cdt->msec);
} else {
datetime->tm.tm_sec = cdt->tm.tm_sec;
datetime->tm.tm_min = cdt->tm.tm_min;
datetime->tm.tm_hour = cdt->tm.tm_hour;
datetime->tm.tm_mday = cdt->tm.tm_mday;
datetime->tm.tm_mon = cdt->tm.tm_mon;
datetime->tm.tm_year = cdt->tm.tm_year;
datetime->tm.tm_wday = cdt->tm.tm_wday;
datetime->tm.tm_yday = cdt->tm.tm_yday;
datetime->tm.tm_isdst = cdt->tm.tm_isdst;
datetime->frac = cdt->msec;
}
}
void client_datetimeus_to_datetime_t(const cdb2_client_datetimeus_t *cdt,
datetime_t *datetime, int flip)
{
datetime->prec = DTTZ_PREC_USEC;
strcpy(datetime->tzname, cdt->tzname);
if (flip) {
datetime->tm.tm_sec = flibc_intflip(cdt->tm.tm_sec);
datetime->tm.tm_min = flibc_intflip(cdt->tm.tm_min);
datetime->tm.tm_hour = flibc_intflip(cdt->tm.tm_hour);
datetime->tm.tm_mday = flibc_intflip(cdt->tm.tm_mday);
datetime->tm.tm_mon = flibc_intflip(cdt->tm.tm_mon);
datetime->tm.tm_year = flibc_intflip(cdt->tm.tm_year);
datetime->tm.tm_wday = flibc_intflip(cdt->tm.tm_wday);
datetime->tm.tm_yday = flibc_intflip(cdt->tm.tm_yday);
datetime->tm.tm_isdst = flibc_intflip(cdt->tm.tm_isdst);
datetime->frac = flibc_intflip(cdt->usec);
} else {
datetime->tm.tm_sec = cdt->tm.tm_sec;
datetime->tm.tm_min = cdt->tm.tm_min;
datetime->tm.tm_hour = cdt->tm.tm_hour;
datetime->tm.tm_mday = cdt->tm.tm_mday;
datetime->tm.tm_mon = cdt->tm.tm_mon;
datetime->tm.tm_year = cdt->tm.tm_year;
datetime->tm.tm_wday = cdt->tm.tm_wday;
datetime->tm.tm_yday = cdt->tm.tm_yday;
datetime->tm.tm_isdst = cdt->tm.tm_isdst;
datetime->frac = cdt->usec;
}
}
void datetime_t_to_dttz(const datetime_t *datetime, dttz_t *dt)
{
cdb2_client_datetime_t cdt;
cdb2_client_datetimeus_t cdtus;
if (datetime->prec == DTTZ_PREC_MSEC) {
datetime_t_to_client_datetime(datetime, &cdt);
client_datetime_to_dttz(&cdt, datetime->tzname, dt, 0);
} else {
datetime_t_to_client_datetimeus(datetime, &cdtus);
client_datetimeus_to_dttz(&cdtus, datetime->tzname, dt, 0);
}
}
void dttz_to_datetime_t(const dttz_t *dt, const char *tz, datetime_t *datetime)
{
cdb2_client_datetime_t cdt;
cdb2_client_datetimeus_t cdtus;
if (datetime->prec == DTTZ_PREC_MSEC) {
dttz_to_client_datetime(dt, tz, &cdt);
client_datetime_to_datetime_t(&cdt, datetime, 0);
} else {
dttz_to_client_datetimeus(dt, tz, &cdtus);
client_datetimeus_to_datetime_t(&cdtus, datetime, 0);
}
}
void init_lua_dbtypes(void)
{
int i;
const char **p = (const char **) &dbtypes;
for (i = 0; i < DBTYPES_MAXTYPE; ++i, ++p) {
*p = dbtypes_str[i];
}
}
static int luabb_type_err(Lua lua, const char *to, int from)
{
int type = lua_type(lua, from);
const char *ourtype = luabb_dbtypename(lua, from);
return luaL_error(lua, "conversion to: %s failed from: %s",
to, ourtype ? ourtype : lua_typename(lua, type));
}
/* Trying to make this work ->
* i := '10.0' -- declared as int */
static int parseint(const char *str, long long *val)
{
char *endp;
*val = strtoll(str, &endp, 10);
if (*endp == 0) return 0;
double d = strtod(str, &endp);
if (*endp != 0) return 1;
*val = d;
#if 0
if (*val == d) return 0; // lossless?
return 1;
#else
return 0;
#endif
}
void luabb_tointeger(Lua lua, int idx, long long *val)
{
const lua_intervalym_t *ym;
const lua_intervalds_t *ds;
const lua_datetime_t *d;
cdb2_client_datetime_t c;
dttz_t dt;
double darg;
const char *s;
switch (luabb_dbtype(lua, idx)) {
case DBTYPES_CSTRING:
s = ((lua_cstring_t *)lua_topointer(lua, idx))->val;
goto str;
case LUA_TSTRING:
s = lua_tostring(lua, idx);
str:if (parseint(s, val) != 0)
luaL_error(lua, "conversion to: int failed from: %s", s);
return;
case LUA_TNUMBER:
darg = lua_tonumber(lua, idx);
goto dbl;
case DBTYPES_REAL:
darg = ((lua_real_t *)lua_topointer(lua, idx))->val;
dbl:if (darg < LLONG_MIN || darg > LLONG_MAX)
luaL_error(lua, "conversion to: int failed from: %f", darg);
*val= (long long) darg;
return;
case DBTYPES_INTEGER:
*val = ((lua_int_t *)lua_topointer(lua, idx))->val;
return;
case DBTYPES_DATETIME:
d = lua_topointer(lua, idx);
/* Precision does not matter as we only need the whole seconds. */
datetime_t_to_client_datetime(&d->val, &c);
if (client_datetime_to_dttz(&c, c.tzname, &dt, 0) == 0) {
*val = dt.dttz_sec;
return;
}
break;
case DBTYPES_INTERVALYM:
ym = lua_topointer(lua, idx);
*val = interval_to_double(&ym->val);
return;
case DBTYPES_INTERVALDS:
ds = lua_topointer(lua, idx);
*val = interval_to_double(&ds->val);
return;
}
luabb_type_err(lua, dbtypes.integer, idx);
*val = 0; //just to silence warnings -- will never reach here
}
void luabb_toreal(Lua lua, int idx, double *ret)
{
const lua_intervalym_t *ym;
const lua_intervalds_t *ds;
const lua_datetime_t *d;
cdb2_client_datetimeus_t c; /* convert to usec to avoid loss of precision */
dttz_t dt;
const char *s;
char *e;
switch (luabb_dbtype(lua, idx)) {
case DBTYPES_INTEGER:
*ret = ((lua_int_t *)lua_topointer(lua, idx))->val;
return;
case DBTYPES_LNUMBER:
*ret = lua_tonumber(lua, idx);
return;
case DBTYPES_REAL:
*ret = ((lua_real_t *)lua_topointer(lua, idx))->val;
return;
case DBTYPES_LSTRING:
s = lua_tostring(lua, idx);
goto str;
case DBTYPES_CSTRING:
s = ((lua_cstring_t *)lua_topointer(lua, idx))->val;
str:errno = 0;
*ret = strtod(s, &e);
if (errno == 0)
return;
break;
case DBTYPES_DATETIME:
d = lua_topointer(lua, idx);
/* Use highest precision so we don't lose precision. */
datetime_t_to_client_datetimeus(&d->val, &c);
if (client_datetimeus_to_dttz(&c, c.tzname, &dt, 0) == 0) {
*ret = (double)dt.dttz_sec + (dt.dttz_frac / 1E6);
return;
}
break;
case DBTYPES_INTERVALYM:
ym = lua_topointer(lua, idx);
*ret = interval_to_double(&ym->val);
return;
case DBTYPES_INTERVALDS:
ds = lua_topointer(lua, idx);
*ret = interval_to_double(&ds->val);
return;
}
err:luabb_type_err(lua, dbtypes.real, idx);
*ret = 0; //just to silence warnings -- will never reach here
}
void luabb_tointervalym(Lua lua, int idx, intv_t *ret)
{
int rc = 0;
const char *c;
double d;
long long i;
int64_t tmp = 0, tmp2 = 0;
int type = 0;
ret->type = INTV_YM_TYPE;
switch(luabb_dbtype(lua, idx)) {
case DBTYPES_INTEGER:
type = 2; // see str_to_interval()
luabb_tointeger(lua, idx, &i);
rc = int_to_interval(i, &tmp, &tmp2, &ret->sign);
break;
case LUA_TNUMBER:
case DBTYPES_REAL:
type = 2; // see str_to_interval()
luabb_toreal(lua, idx, &d);
rc = double_to_interval(d, &tmp, &tmp2, &ret->sign);
break;
case DBTYPES_INTERVALYM:
*ret = ((lua_intervalym_t *)lua_topointer(lua, idx))->val;
return;
case LUA_TSTRING:
case DBTYPES_CSTRING:
type = INTV_YM_TYPE;
c = luabb_tostring(lua, idx);
rc = str_to_interval(c, strlen(c), &type, &tmp, &tmp2, &ret->u.ds,
&ret->sign);
if (type == 0 || type == 2) // 'years-months' or 'number'
break;
// else fall through - couldn't parse intervalym
err:
default:
luabb_type_err(lua, dbtypes.intervalym, idx);
break;
}
if (rc != 0)
goto err;
if (type == 0) {
ret->u.ym.years = tmp;
ret->u.ym.months = tmp2;
} else if (type == 2) {
ret->u.ym.years = 0;
ret->u.ym.months = tmp;
}
_normalizeIntervalYM(&ret->u.ym);
}
void luabb_tointervalds(Lua lua, int idx, intv_t *ret)
{
int rc = 0;
const char *c;
double d;
long long i;
int64_t tmp = 0, tmp2 = 0;
int type;
switch(luabb_dbtype(lua, idx)) {
case DBTYPES_INTEGER:
luabb_tointeger(lua, idx, &i);
rc = int_to_interval(i, &tmp, &tmp2, &ret->sign);
break;
case LUA_TNUMBER:
case DBTYPES_REAL:
luabb_toreal(lua, idx, &d);
rc = double_to_interval(d, &tmp, &tmp2, &ret->sign);
break;
case DBTYPES_INTERVALDS:
*ret = ((lua_intervalds_t *)lua_topointer(lua, idx))->val;
return;
case LUA_TSTRING:
case DBTYPES_CSTRING:
type = INTV_DS_TYPE;
c = luabb_tostring(lua, idx);
rc = str_to_interval(c, strlen(c), &type, &tmp, &tmp2, &ret->u.ds,
&ret->sign);
if (type == 1) // days hr:mn:sec.msec
return;
else if (type == 2) // number
break;
// else fall through - couldn't parse intervalds
err:
default:
luabb_type_err(lua, dbtypes.intervalym, idx);
break;
}
if (rc != 0)
goto err;
if ((int)(tmp2 / 1E3) * 1000 == tmp2) {
ret->type = INTV_DS_TYPE;
_setIntervalDS(&ret->u.ds, tmp, tmp2 / 1000);
} else {
ret->type = INTV_DSUS_TYPE;
_setIntervalDSUS(&ret->u.ds, tmp, tmp2);
}
}
/*
** WARNING: luabb_tostring RETURNS AN EPHEMERAL STRING.
** DON'T CALL INTO ANY lua_* FUNCTIONS WITH IT.
** A GC-RUN CAN TRASH RETURNED MEMORY.
*/
const char *luabb_tostring(Lua L, int idx)
{
idx = to_positive_index(L, idx);
const char *ret = NULL;
dbtypes_enum dbtype = luabb_dbtype(L, idx);
if (dbtype > DBTYPES_MINTYPE && dbtype < DBTYPES_MAXTYPE) {
if (dbtypes_tostring[dbtype]) {
lua_pushvalue(L, idx);
dbtypes_tostring[dbtype](L);
ret = lua_tostring(L, -1);
lua_pop(L, 2);
return ret;
}
} else if (lua_isnumber(L, idx)) {
lua_pushvalue(L, idx);
ret = lua_tostring(L, -1);
lua_pop(L, 1);
return ret;
} else if (lua_isstring(L, idx)) {
lua_pushvalue(L, idx);
ret = lua_tostring(L, -1);
lua_pop(L, 1);
return ret;
}
luabb_type_err(L, "string", idx);
return NULL;
}
void luabb_toblob(Lua lua, int idx, blob_t *ret)
{
const char *c;
size_t s;
int type = luabb_type(lua, idx);
if (type == DBTYPES_LSTRING) {
c = lua_tostring(lua, idx);
str: s = strlen(c);
ret->data = strdup(c);
ret->length = s;
return;
} else if (type == DBTYPES_CSTRING) {
c = ((lua_cstring_t*)lua_touserdata(lua, idx))->val;
goto str;
} else if (type == DBTYPES_BLOB) {
const lua_blob_t *blob = lua_topointer(lua, idx);
*ret = blob->val;
} else {
luabb_type_err(lua, dbtypes.blob, idx);
}
}
static int get_int_value(lua_State *lua, int index, char *type)
{
if (index < 0) index = lua_gettop(lua) + index + 1;
lua_pushstring(lua, type);
lua_gettable(lua, index);
int i;
if (lua_isnumber(lua, -1)) {
i = lua_tointeger( lua, -1 );
lua_pop(lua, 1);
return i;
} else if (lua_isboolean(lua, -1)) {
i = lua_toboolean( lua, -1 );
lua_pop(lua, 1);
return i;
}
luaL_error(lua, "data is not a valid number.");
return -1;
}
static const char * get_string_value(lua_State *lua, int index, char *type)
{
lua_pushvalue(lua, index);
lua_pushstring(lua,type);
lua_gettable(lua, -2);
if (lua_isstring(lua, -1)) {
const char* str = lua_tostring( lua, -1 );
return str;
} else {
return NULL;
}
}
static int l_datetime_change_timezone(lua_State *lua)
{
lua_datetime_t *d;
struct tm newtm;
const char *newtimezone;
server_datetime_t sdt;
luaL_checkudata(lua, 1, dbtypes.datetime);
d = (lua_datetime_t *) lua_topointer(lua, 1);
newtimezone = lua_tostring(lua,2);
sdt.sec = db_struct2time(d->val.tzname, &(d->val.tm));
if( db_time2struct(newtimezone, &sdt.sec, &(newtm)) == 0) {
strncpy(d->val.tzname, newtimezone, sizeof(d->val.tzname));
d->val.tm = newtm;
lua_pushinteger(lua, 0);
return 1;
}
lua_pushinteger(lua, -1);
return 1;
}
static int l_datetime_change_datetime_precision(lua_State *lua)
{
lua_datetime_t *d;
int newprecision;
const char *z;
luaL_checkudata(lua, 1, dbtypes.datetime);
d = (lua_datetime_t *) lua_topointer(lua, 1);
z = lua_tostring(lua, 2);
DTTZ_TEXT_TO_PREC(z, newprecision, 0, goto err);
d->val.frac = (d->val.frac * newprecision) / d->val.prec;
d->val.prec = newprecision;
if (0) {
err:
lua_pushinteger(lua, -1);
}
lua_pushinteger(lua, 0);
return 1;
}
void luabb_todatetime(lua_State *lua, int idx, datetime_t *ret)
{
double d;
int64_t i;
dttz_t dt;
time_t temp_t;
const char *str;
cdb2_client_datetime_t cdtms;
cdb2_client_datetimeus_t cdtus;
dbtypes_enum dbtype;
SP sp = getsp(lua);
const char *tzname = sp->clnt->tzname;
int precision = sp->clnt->dtprec;
switch (luabb_dbtype(lua, idx)) {
case LUA_TSTRING:
str = lua_tostring(lua, idx);
if (str_to_dttz(str, strlen(str), tzname, &dt, precision) != 0)
goto err;
break;
case LUA_TNUMBER:
d = lua_tonumber(lua, idx);
if(real_to_dttz(d, &dt, precision) != 0)
goto err;
break;
case LUA_TTABLE:
/* This is for lua date, which is of type LUA_TTABLE. */
ret->tm.tm_year = get_int_value(lua,idx, "year") - 1900;
ret->tm.tm_mon = get_int_value(lua,idx, "month") - 1;
ret->tm.tm_mday = get_int_value(lua,idx, "day");
ret->tm.tm_yday = get_int_value(lua,idx, "yday") - 1;
ret->tm.tm_wday = get_int_value(lua,idx, "wday");
ret->tm.tm_hour = get_int_value(lua,idx, "hour");
ret->tm.tm_min = get_int_value(lua,idx, "min");
ret->tm.tm_sec = get_int_value(lua,idx, "sec");
ret->tm.tm_isdst = get_int_value(lua,idx, "isdst");
temp_t = mktime(&ret->tm);
ret->frac = 0;
ret->prec = precision;
tzname = get_string_value(lua,idx, "tz");
if (tzname) {
strcpy(ret->tzname, tzname);
} else {
strcpy(ret->tzname, "US/Eastern");
}
struct tm *t = localtime(&temp_t);
ret->tm = *t; /* Correct it, if table doesn't have correct values. */
return;
case DBTYPES_DATETIME:
*ret = ((lua_datetime_t *)lua_topointer(lua, idx))->val;
return;
case DBTYPES_CSTRING:
str = ((lua_cstring_t*)lua_topointer(lua, idx))->val;
if (str_to_dttz(str, strlen(str), tzname, &dt, precision) != 0)
goto err;
break;
case DBTYPES_REAL:
d = ((lua_real_t*)lua_topointer(lua, idx))->val;
if (real_to_dttz(d, &dt, precision) != 0)
goto err;
break;
case DBTYPES_INTEGER:
i = ((lua_int_t*)lua_topointer(lua, idx))->val;
if (int_to_dttz(i, &dt, precision) != 0)
goto err;
break;
default:
goto err;
}
if (dt.dttz_prec == DTTZ_PREC_USEC) {
if (dttz_to_client_datetimeus(&dt, tzname, &cdtus) != 0)
goto err;
client_datetimeus_to_datetime_t(&cdtus, ret, 0);
} else {
if (dttz_to_client_datetime(&dt, tzname, &cdtms) != 0)
goto err;
client_datetime_to_datetime_t(&cdtms, ret, 0);
}
return;
err:luabb_type_err(lua, dbtypes.datetime, idx);
}
void luabb_todecimal(lua_State *lua, int idx, decQuad *val)
{
const char *sval = NULL;
char sbuf[LUAI_MAXNUMBER2STR];
double rval;
int ival;
switch (luabb_dbtype(lua, idx)) {
case LUA_TNUMBER:
case LUA_TSTRING:
sval = lua_tostring(lua, idx);
break;
case DBTYPES_CSTRING:
sval = ((lua_cstring_t *)lua_topointer(lua, idx))->val;
break;
case DBTYPES_INTEGER:
ival = ((lua_int_t *) lua_topointer(lua, idx))->val;
sprintf(sbuf, "%lld", ival);
sval = sbuf;
break;
case DBTYPES_REAL:
rval = ((lua_real_t *) lua_topointer(lua, idx))->val;
lua_number2str(sbuf, rval);
sval = sbuf;
break;
case DBTYPES_DECIMAL:
*val = ((lua_dec_t *) lua_topointer(lua, idx))->val;
return;
default:
luabb_type_err(lua, dbtypes.decimal, idx);
}
decContext ctx;
dec_ctx_init(&ctx, DEC_INIT_DECQUAD, gbl_decimal_rounding);
decQuadFromString(val, sval, &ctx);
if (dfp_conv_check_status(&ctx,"string", "quad")) {
luabb_type_err(lua, dbtypes.decimal, idx);
}
}
static int l_add(Lua);
static int l_sub(Lua);
static int l_mul(Lua);
static int l_div(Lua);
static int l_mod(Lua);
void init_arithmetic(Lua lua, int mod)
{
lua_pushcfunction(lua, l_add);
lua_setfield(lua, -2, "__add");
lua_pushcfunction(lua, l_sub);
lua_setfield(lua, -2, "__sub");
lua_pushcfunction(lua, l_mul);
lua_setfield(lua, -2, "__mul");
lua_pushcfunction(lua, l_div);
lua_setfield(lua, -2, "__div");
if (mod != LUA_OP_MOD) return;
lua_pushcfunction(lua, l_mod);
lua_setfield(lua, -2, "__mod");
}
static int l_eq(Lua);
static int l_lt(Lua);
static int l_le(Lua);
void init_cmp(Lua lua)
{
lua_pushcfunction(lua, l_eq);
lua_setfield(lua, -2, "__eq");
lua_pushcfunction(lua, l_lt);
lua_setfield(lua, -2, "__lt");
lua_pushcfunction(lua, l_le);
lua_setfield(lua, -2, "__le");
}
static int l_typed_assignment(Lua);
static int l_concat(Lua);
void init_common(Lua lua)
{
lua_pushcfunction(lua, l_typed_assignment);
lua_setfield(lua, -2, "__type");
lua_pushcfunction(lua, l_column_cast);
lua_setfield(lua, -2, "__cast");
lua_pushcfunction(lua, l_concat);
lua_setfield(lua, -2, "__concat");
}
static void l_decimal_cmp(lua_State *lua, int op) {
decQuad dec1, dec2;
luabb_todecimal(lua, 1, &dec1);
luabb_todecimal(lua, 2, &dec2);
decQuad result;
decContext ctx;
dec_ctx_init(&ctx, DEC_INIT_DECQUAD, gbl_decimal_rounding);
decQuadCompare( &result, (decQuad*)&dec1, (decQuad*)&dec2, &ctx);
if (((op ==LUA_OP_EQ) || (op == LUA_OP_LE)) && decQuadIsZero( &result) ) {
lua_pushboolean(lua, 1);
} else if (((op ==LUA_OP_LT) || (op == LUA_OP_LE)) && decQuadIsSigned( &result) ) {
lua_pushboolean(lua, 1);
} else {
lua_pushboolean(lua, 0);
}
}
static void l_real_cmp(Lua lua, int op)
{
double val1, val2;
luabb_toreal(lua, 1, &val1);
luabb_toreal(lua, 2, &val2);
switch (op) {
case LUA_OP_EQ: lua_pushboolean(lua, val1 == val2); break;
case LUA_OP_LT: lua_pushboolean(lua, val1 < val2); break;
case LUA_OP_LE: lua_pushboolean(lua, val1 <= val2); break;
}
}
static void l_int_cmp(Lua lua, int op)
{
long long val1, val2;
luabb_tointeger(lua, 1, &val1);
luabb_tointeger(lua, 2, &val2);
switch (op) {
case LUA_OP_EQ: lua_pushboolean(lua, val1 == val2); break;
case LUA_OP_LT: lua_pushboolean(lua, val1 < val2); break;
case LUA_OP_LE: lua_pushboolean(lua, val1 <= val2); break;
}
}
static int datetime_cmp(Lua);
static void l_datetime_cmp(Lua lua, int op)
{
switch (op) {
case LUA_OP_EQ: lua_pushboolean(lua, datetime_cmp(lua) == 0); break;
case LUA_OP_LT: lua_pushboolean(lua, datetime_cmp(lua) < 0); break;
case LUA_OP_LE: lua_pushboolean(lua, datetime_cmp(lua) <= 0); break;
}
}
static void intv_cmp(Lua lua, intv_t *i1, intv_t *i2, int op)
{
switch (op) {
case LUA_OP_EQ: lua_pushboolean(lua, interval_cmp(i1, i2) == 0);
case LUA_OP_LT: lua_pushboolean(lua, interval_cmp(i1, i2) < 0);
case LUA_OP_LE: lua_pushboolean(lua, interval_cmp(i1, i2) <= 0);
}
}
static void l_intervalds_cmp(Lua lua, int op)
{
intv_t i1, i2;
luabb_tointervalds(lua, 1, &i1);
luabb_tointervalds(lua, 2, &i2);
intv_cmp(lua, &i1, &i2, op);
}
static void l_intervalym_cmp(Lua lua, int op)
{
intv_t i1, i2;
luabb_tointervalym(lua, 1, &i1);
luabb_tointervalym(lua, 2, &i2);
intv_cmp(lua, &i1, &i2, op);
}
static rank_t getrank(Lua lua, int idx)
{
switch (luabb_dbtype(lua, idx)) {
case DBTYPES_LNIL: return RANK_NIL;
case DBTYPES_INTEGER: return RANK_INT;
case DBTYPES_REAL:
case DBTYPES_LNUMBER: return RANK_REAL;
case DBTYPES_DECIMAL: return RANK_DECIMAL;
case DBTYPES_INTERVALDS: return RANK_INTERVALDS;
case DBTYPES_INTERVALYM: return RANK_INTERVALYM;
case DBTYPES_DATETIME: return RANK_DATETIME;
default: return RANK_MAX;
}
}
static int l_cmp(Lua lua, int op)
{
nullchk(lua, 1);
nullchk(lua, 2);
rank_t rank1 = getrank(lua, 1);
rank_t rank2 = getrank(lua, 2);
rank_t min = rank1 < rank2 ? rank1 : rank2;
rank_t max = rank1 > rank2 ? rank1 : rank2;
if (min == 0) { // got one nil
if (op == LUA_OP_EQ)
lua_pushboolean(lua, 0); // nil compares false in equality
else
typecmperr(lua, 1, 2);
return 0;
}
switch (max) {
case RANK_INT: l_int_cmp(lua, op); break;
case RANK_REAL: l_real_cmp(lua, op); break;
case RANK_DECIMAL: l_decimal_cmp(lua, op); break;
case RANK_INTERVALDS: l_intervalds_cmp(lua, op); break;
case RANK_INTERVALYM: l_intervalym_cmp(lua, op); break;
case RANK_DATETIME: l_datetime_cmp(lua, op); break;
default:
if (op == LUA_OP_EQ)
lua_pushboolean(lua, 0);
else
typecmperr(lua, 1, 2);
}
return 0;
}
static int l_eq(Lua lua)
{
l_cmp(lua, LUA_OP_EQ);
return 1;
}
static int l_lt(Lua lua)
{
l_cmp(lua, LUA_OP_LT);
return 1;
}
static int l_le(Lua lua)
{
l_cmp(lua, LUA_OP_LE);
return 1;
}
static int l_decimal_add(lua_State *lua)
{
lua_dec_t *in;
decQuad arg;
lua_dec_t *out;
decContext dfp_ctx;
dec_ctx_init(&dfp_ctx, DEC_INIT_DECQUAD, gbl_decimal_rounding);
luaL_checkudata(lua, 1, dbtypes.decimal);
in = (lua_dec_t*) lua_topointer(lua, 1);
luabb_todecimal(lua, 2, &arg);
new_lua_t(out, lua_dec_t, DBTYPES_DECIMAL);
decQuadAdd(&(out->val), &(in->val), &arg, &dfp_ctx);
if (dfp_conv_check_status( &dfp_ctx,"quad", "add(quad)")) {
luaL_error(lua, "decimal conversion error");
}
return 1;
}
static int l_decimal_sub(lua_State *lua)
{
lua_dec_t *in;
decQuad arg;
lua_dec_t *out;
decContext dfp_ctx;
dec_ctx_init(&dfp_ctx, DEC_INIT_DECQUAD, gbl_decimal_rounding);
luaL_checkudata(lua, 1, dbtypes.decimal);
in = (lua_dec_t*) lua_topointer(lua, 1);
luabb_todecimal(lua, 2, &arg);
new_lua_t(out, lua_dec_t, DBTYPES_DECIMAL);
decQuadSubtract (&(out->val), &(in->val), &arg, &dfp_ctx);
if (dfp_conv_check_status( &dfp_ctx,"quad", "subtract(quad)")) {
luaL_error(lua, "decimal conversion error");
}
return 1;
}
static int l_decimal_mul(lua_State *lua)
{
lua_dec_t *in;
decQuad arg;
lua_dec_t *out;
decContext dfp_ctx;
dec_ctx_init(&dfp_ctx, DEC_INIT_DECQUAD, gbl_decimal_rounding);
luaL_checkudata(lua, 1, dbtypes.decimal);
in = (lua_dec_t*) lua_topointer(lua, 1);
luabb_todecimal(lua, 2, &arg);
new_lua_t(out, lua_dec_t, DBTYPES_DECIMAL);
decQuadMultiply(&(out->val), &(in->val), &arg, &dfp_ctx);
if (dfp_conv_check_status( &dfp_ctx,"quad", "multiply(quad)")) {
luaL_error(lua, "decimal conversion error");
}
return 1;
}
static int l_decimal_div(lua_State *lua)
{
lua_dec_t *in;
decQuad arg;
lua_dec_t *out;
decContext dfp_ctx;
dec_ctx_init(&dfp_ctx, DEC_INIT_DECQUAD, gbl_decimal_rounding);
luaL_checkudata(lua, 1, dbtypes.decimal);
in = (lua_dec_t*) lua_topointer(lua, 1);
luabb_todecimal(lua, 2, &arg);
new_lua_t(out, lua_dec_t, DBTYPES_DECIMAL);
decQuadDivide(&(out->val), &(in->val), &arg, &dfp_ctx);
if (dfp_conv_check_status( &dfp_ctx,"quad", "divide(quad)")) {
luaL_error(lua, "decimal conversion error");
}
return 1;
}
static int l_int_new(Lua);
static int l_int_arithmetic(Lua lua, operation_t op)
{
long long v1, v2;
luabb_tointeger(lua, 1, &v1);
luabb_tointeger(lua, 2, &v2);
l_int_new(lua);
lua_int_t *v = (lua_int_t *)lua_topointer(lua, -1);
switch (op) {
case LUA_OP_ADD: v->val = v1 + v2; break;
case LUA_OP_SUB: v->val = v1 - v2; break;
case LUA_OP_MUL: v->val = v1 * v2; break;
case LUA_OP_DIV: v->val = v1 / v2; break;
case LUA_OP_MOD: v->val = v1 % v2; break;
}
return 1;
}
static int l_real_new(Lua);
static int l_real_arithmetic(Lua lua, operation_t op)
{
double v1, v2;
luabb_toreal(lua, 1, &v1);
luabb_toreal(lua, 2, &v2);
l_real_new(lua);
lua_real_t *v = (lua_real_t *)lua_topointer(lua, -1);
switch (op) {
case LUA_OP_ADD: v->val = v1 + v2; break;
case LUA_OP_SUB: v->val = v1 - v2; break;
case LUA_OP_MUL: v->val = v1 * v2; break;