-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconvert.c
1619 lines (1368 loc) · 60.6 KB
/
convert.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
#include "wcs.h"
#include "convert.h"
#include <stdio.h>
#include <stdint.h>
/*
Converts a real number to a @c_data_type value.
FIXME Currently, this function only supports real-to-float/double conversion.
*/
conv_resp convert_cdb2real(const void *value, int size, SQLSMALLINT c_data_type, SQLPOINTER target_ptr, SQLLEN target_len, SQLLEN *str_len)
{
double num = *((double*) value);
switch(c_data_type) {
case SQL_C_CHAR:
*str_len = snprintf((char *)target_ptr, target_len, "%f", num);
if(*str_len >= target_len)
return CONV_TRUNCATED;
break;
case SQL_C_WCHAR:
*str_len = swprintf((wchar_t *)target_ptr, target_len / sizeof(wchar_t), L"%f", num) * sizeof(SQLWCHAR);
if(*str_len >= target_len / (SQLLEN)sizeof(wchar_t))
return CONV_TRUNCATED;
break;
case SQL_C_FLOAT:
SET_SQLREAL(target_ptr, num, *str_len);
break;
case SQL_C_DOUBLE:
case SQL_C_DEFAULT:
SET_SQLDOUBLE(target_ptr, num, *str_len);
break;
default:
return CONV_IMPOSSIBLE;
}
return CONV_YEAH;
}
/*
Converts a datetime value to a @c_data_type value.
FIXME Currently, this function only supports datetime-to-date/time/timestamp conversion.
*/
conv_resp convert_cdb2datetime(const void *value, int size, SQLSMALLINT c_data_type, SQLPOINTER target_ptr, SQLLEN target_len, SQLLEN *str_len)
{
cdb2_client_datetime_t *datetime = (cdb2_client_datetime_t *)value;
DATE_STRUCT *date;
TIME_STRUCT *time;
TIMESTAMP_STRUCT *timestamp;
switch(c_data_type) {
case SQL_C_CHAR:
*str_len = snprintf((char *)target_ptr, target_len, "%.4d-%.2d-%.2d %.2d:%.2d:%.2d.%.3d %.*s",
datetime->tm.tm_year + 1900, datetime->tm.tm_mon + 1, datetime->tm.tm_mday, datetime->tm.tm_hour,
datetime->tm.tm_min, datetime->tm.tm_sec, datetime->msec, CDB2_MAX_TZNAME, datetime->tzname);
if(*str_len >= target_len)
return CONV_TRUNCATED;
break;
case SQL_C_WCHAR:
*str_len = swprintf((wchar_t *)target_ptr, target_len / sizeof(wchar_t),
L"%.4d-%.2d-%.2d %.2d:%.2d:%.2d.%.3d %.*hs", datetime->tm.tm_year + 1900,
datetime->tm.tm_mon + 1, datetime->tm.tm_mday, datetime->tm.tm_hour,
datetime->tm.tm_min, datetime->tm.tm_sec, datetime->msec, CDB2_MAX_TZNAME, datetime->tzname) * sizeof(SQLWCHAR);
if(*str_len >= target_len / (SQLLEN)sizeof(wchar_t))
return CONV_TRUNCATED;
break;
case SQL_C_TYPE_DATE:
case SQL_C_DATE: /* odbc 2.x */
date = (DATE_STRUCT *)target_ptr;
date->year = (SQLSMALLINT)datetime->tm.tm_year + 1900;
date->month = (SQLSMALLINT)datetime->tm.tm_mon + 1;
date->day = (SQLSMALLINT)datetime->tm.tm_mday;
*str_len = sizeof(DATE_STRUCT);
break;
case SQL_C_TYPE_TIME:
case SQL_C_TIME: /* odbc 2.x */
time = (TIME_STRUCT *)target_ptr;
time->hour = (SQLUSMALLINT)datetime->tm.tm_hour;
time->minute = (SQLUSMALLINT)datetime->tm.tm_min;
time->second = (SQLUSMALLINT)datetime->tm.tm_sec;
*str_len = sizeof(TIME_STRUCT);
break;
case SQL_C_TYPE_TIMESTAMP:
case SQL_C_TIMESTAMP: /* odbc 2.x */
case SQL_C_DEFAULT:
timestamp = (TIMESTAMP_STRUCT *)target_ptr;
timestamp->year = (SQLSMALLINT)datetime->tm.tm_year + 1900;
timestamp->month = (SQLUSMALLINT)datetime->tm.tm_mon + 1;
timestamp->day = (SQLUSMALLINT)datetime->tm.tm_mday;
timestamp->hour = (SQLUSMALLINT)datetime->tm.tm_hour;
timestamp->minute = (SQLUSMALLINT)datetime->tm.tm_min;
timestamp->second = (SQLUSMALLINT)datetime->tm.tm_sec;
timestamp->fraction = datetime->msec * 1000000;
*str_len = sizeof(TIMESTAMP_STRUCT);
break;
default:
return CONV_IMPOSSIBLE;
}
return CONV_YEAH;
}
/*
Converts a datetimeus value to a @c_data_type value.
*/
conv_resp convert_cdb2datetimeus(const void *value, int size,
SQLSMALLINT c_data_type, SQLPOINTER target_ptr, SQLLEN target_len, SQLLEN *str_len)
{
cdb2_client_datetimeus_t *datetime = (cdb2_client_datetimeus_t *)value;
DATE_STRUCT *date;
TIME_STRUCT *time;
TIMESTAMP_STRUCT *timestamp;
switch(c_data_type) {
case SQL_C_CHAR:
*str_len = snprintf((char *)target_ptr, target_len, "%.4d-%.2d-%.2d %.2d:%.2d:%.2d.%.6d %.*s",
datetime->tm.tm_year + 1900, datetime->tm.tm_mon + 1, datetime->tm.tm_mday, datetime->tm.tm_hour,
datetime->tm.tm_min, datetime->tm.tm_sec, datetime->usec, CDB2_MAX_TZNAME, datetime->tzname);
if(*str_len >= target_len)
return CONV_TRUNCATED;
break;
case SQL_C_WCHAR:
*str_len = swprintf((wchar_t *)target_ptr, target_len / sizeof(wchar_t),
L"%.4d-%.2d-%.2d %.2d:%.2d:%.2d.%.6d %.*hs", datetime->tm.tm_year + 1900,
datetime->tm.tm_mon + 1, datetime->tm.tm_mday, datetime->tm.tm_hour,
datetime->tm.tm_min, datetime->tm.tm_sec, datetime->usec, CDB2_MAX_TZNAME, datetime->tzname) * sizeof(SQLWCHAR);
if(*str_len >= target_len / (SQLLEN)sizeof(wchar_t))
return CONV_TRUNCATED;
break;
case SQL_C_TYPE_DATE:
date = (DATE_STRUCT *)target_ptr;
date->year = (SQLSMALLINT)datetime->tm.tm_year + 1900;
date->month = (SQLSMALLINT)datetime->tm.tm_mon + 1;
date->day = (SQLSMALLINT)datetime->tm.tm_mday;
*str_len = sizeof(DATE_STRUCT);
break;
case SQL_C_TYPE_TIME:
time = (TIME_STRUCT *)target_ptr;
time->hour = (SQLUSMALLINT)datetime->tm.tm_hour;
time->minute = (SQLUSMALLINT)datetime->tm.tm_min;
time->second = (SQLUSMALLINT)datetime->tm.tm_sec;
*str_len = sizeof(TIME_STRUCT);
break;
case SQL_C_TYPE_TIMESTAMP:
case SQL_C_DEFAULT:
timestamp = (TIMESTAMP_STRUCT *)target_ptr;
timestamp->year = (SQLSMALLINT)datetime->tm.tm_year + 1900;
timestamp->month = (SQLUSMALLINT)datetime->tm.tm_mon + 1;
timestamp->day = (SQLUSMALLINT)datetime->tm.tm_mday;
timestamp->hour = (SQLUSMALLINT)datetime->tm.tm_hour;
timestamp->minute = (SQLUSMALLINT)datetime->tm.tm_min;
timestamp->second = (SQLUSMALLINT)datetime->tm.tm_sec;
timestamp->fraction = datetime->usec * 1000;
*str_len = sizeof(TIMESTAMP_STRUCT);
break;
default:
return CONV_IMPOSSIBLE;
}
return CONV_YEAH;
}
/*
Converts a intervalds value to a @c_data_type value.
FIXME Currently, this function only supports ds-to-interval day/hour/min/sec... conversion.
*/
conv_resp convert_cdb2inds(const void *value, int size, SQLSMALLINT c_data_type, SQLPOINTER target_ptr, SQLLEN target_len, SQLLEN *str_len)
{
cdb2_client_intv_ds_t *intv_cdb2 = (cdb2_client_intv_ds_t *)value;
SQL_INTERVAL_STRUCT *intv_odbc = (SQL_INTERVAL_STRUCT *)target_ptr;
switch(c_data_type) {
case SQL_C_CHAR:
*str_len = snprintf((char *)target_ptr, target_len, "%d %d:%d:%d.%d",
intv_cdb2->sign * intv_cdb2->days, intv_cdb2->hours, intv_cdb2->mins, intv_cdb2->sec, intv_cdb2->msec);
if(*str_len >= target_len)
return CONV_TRUNCATED;
break;
case SQL_C_WCHAR:
*str_len = swprintf((wchar_t *)target_ptr, target_len / sizeof(wchar_t), L"%d %d:%d:%d.%d",
intv_cdb2->sign * intv_cdb2->days, intv_cdb2->hours, intv_cdb2->mins, intv_cdb2->sec, intv_cdb2->msec) * sizeof(SQLWCHAR);
if(*str_len >= target_len / (SQLLEN)sizeof(wchar_t))
return CONV_TRUNCATED;
break;
case SQL_C_INTERVAL_DAY:
case SQL_C_INTERVAL_HOUR:
case SQL_C_INTERVAL_MINUTE:
case SQL_C_INTERVAL_SECOND:
case SQL_C_INTERVAL_DAY_TO_HOUR:
case SQL_C_INTERVAL_DAY_TO_MINUTE:
case SQL_C_INTERVAL_DAY_TO_SECOND:
case SQL_C_INTERVAL_HOUR_TO_MINUTE:
case SQL_C_INTERVAL_HOUR_TO_SECOND:
case SQL_C_INTERVAL_MINUTE_TO_SECOND:
case SQL_C_DEFAULT:
/* I will not do any calculation here (like 2 days and 6 hrs ==> 54 hrs).
The flag @interval_type will always set to SQL_IS_DAY_TO_SECOND. */
intv_odbc->interval_type = SQL_IS_DAY_TO_SECOND;
intv_odbc->interval_sign = (SQLSMALLINT)intv_cdb2->sign;
intv_odbc->intval.day_second.day = intv_cdb2->days;
intv_odbc->intval.day_second.hour = intv_cdb2->hours;
intv_odbc->intval.day_second.minute = intv_cdb2->mins;
intv_odbc->intval.day_second.second = intv_cdb2->sec;
intv_odbc->intval.day_second.fraction = intv_cdb2->msec * 1000000;
*str_len = sizeof(SQL_INTERVAL_STRUCT);
break;
default:
return CONV_IMPOSSIBLE;
}
return CONV_YEAH;
}
/*
Converts a intervaldsus value to a @c_data_type value.
*/
conv_resp convert_cdb2indsus(const void *value, int size, SQLSMALLINT c_data_type, SQLPOINTER target_ptr, SQLLEN target_len, SQLLEN *str_len)
{
cdb2_client_intv_dsus_t *intv_cdb2 = (cdb2_client_intv_dsus_t *)value;
SQL_INTERVAL_STRUCT *intv_odbc = (SQL_INTERVAL_STRUCT *)target_ptr;
switch(c_data_type) {
case SQL_C_CHAR:
*str_len = snprintf((char *)target_ptr, target_len, "%d %d:%d:%d.%d",
intv_cdb2->sign * intv_cdb2->days, intv_cdb2->hours, intv_cdb2->mins, intv_cdb2->sec, intv_cdb2->usec);
if(*str_len >= target_len)
return CONV_TRUNCATED;
break;
case SQL_C_WCHAR:
*str_len = swprintf((wchar_t *)target_ptr, target_len / sizeof(wchar_t), L"%d %d:%d:%d.%d",
intv_cdb2->sign * intv_cdb2->days, intv_cdb2->hours, intv_cdb2->mins, intv_cdb2->sec, intv_cdb2->usec) * sizeof(SQLWCHAR);
if(*str_len >= target_len / (SQLLEN)sizeof(wchar_t))
return CONV_TRUNCATED;
break;
case SQL_C_INTERVAL_DAY:
case SQL_C_INTERVAL_HOUR:
case SQL_C_INTERVAL_MINUTE:
case SQL_C_INTERVAL_SECOND:
case SQL_C_INTERVAL_DAY_TO_HOUR:
case SQL_C_INTERVAL_DAY_TO_MINUTE:
case SQL_C_INTERVAL_DAY_TO_SECOND:
case SQL_C_INTERVAL_HOUR_TO_MINUTE:
case SQL_C_INTERVAL_HOUR_TO_SECOND:
case SQL_C_INTERVAL_MINUTE_TO_SECOND:
case SQL_C_DEFAULT:
/* I will not do any calculation here (like 2 days and 6 hrs ==> 54 hrs).
The flag @interval_type will always set to SQL_IS_DAY_TO_SECOND. */
intv_odbc->interval_type = SQL_IS_DAY_TO_SECOND;
intv_odbc->interval_sign = (SQLSMALLINT)intv_cdb2->sign;
intv_odbc->intval.day_second.day = intv_cdb2->days;
intv_odbc->intval.day_second.hour = intv_cdb2->hours;
intv_odbc->intval.day_second.minute = intv_cdb2->mins;
intv_odbc->intval.day_second.second = intv_cdb2->sec;
intv_odbc->intval.day_second.fraction = intv_cdb2->usec * 1000;
*str_len = sizeof(SQL_INTERVAL_STRUCT);
break;
default:
return CONV_IMPOSSIBLE;
}
return CONV_YEAH;
}
/*
Converts a intervalym value to a @c_data_type value.
FIXME Currently, this function only supports ym-to-interval month/year/year&month conversion.
*/
conv_resp convert_cdb2inym(const void *value, int size, SQLSMALLINT c_data_type, SQLPOINTER target_ptr, SQLLEN target_len, SQLLEN *str_len)
{
cdb2_client_intv_ym_t *intv_cdb2 = (cdb2_client_intv_ym_t *)value;
SQL_INTERVAL_STRUCT *intv_odbc = (SQL_INTERVAL_STRUCT *)target_ptr;
switch(c_data_type) {
case SQL_C_CHAR:
*str_len = snprintf((char *)target_ptr, target_len, "%d-%d",
intv_cdb2->sign * intv_cdb2->years, intv_cdb2->months);
if(*str_len >= target_len)
return CONV_TRUNCATED;
break;
case SQL_C_WCHAR:
*str_len = swprintf((wchar_t *)target_ptr, target_len / sizeof(wchar_t), L"%d-%d",
intv_cdb2->sign * intv_cdb2->years, intv_cdb2->months) * sizeof(SQLWCHAR);
if(*str_len >= target_len / (SQLLEN)sizeof(wchar_t))
return CONV_TRUNCATED;
break;
case SQL_C_INTERVAL_MONTH:
case SQL_C_INTERVAL_YEAR:
case SQL_C_INTERVAL_YEAR_TO_MONTH:
case SQL_C_DEFAULT:
intv_odbc->interval_type = SQL_IS_YEAR_TO_MONTH;
intv_odbc->interval_sign = (SQLSMALLINT)intv_cdb2->sign;
intv_odbc->intval.year_month.year = intv_cdb2->years;
intv_odbc->intval.year_month.month = intv_cdb2->months;
*str_len = sizeof(SQL_INTERVAL_STRUCT);
break;
default:
return CONV_IMPOSSIBLE;
}
return CONV_YEAH;
}
/*
Converts a large object to a @c_data_type value.
FIXME Currently, this function only supports blob-to-binary conversion.
*/
conv_resp convert_cdb2blob(const void *value, int size, SQLSMALLINT c_data_type, SQLPOINTER target_ptr, SQLLEN target_len, SQLLEN *str_len)
{
conv_resp resp = CONV_YEAH;
int charlen;
switch(c_data_type) {
case SQL_C_BINARY:
case SQL_C_DEFAULT:
*str_len = size;
if(size > target_len) {
size = (int)target_len;
resp = CONV_TRUNCATED;
}
memcpy(target_ptr, value, size);
break;
case SQL_C_CHAR:
// https://docs.microsoft.com/en-us/sql/odbc/reference/appendixes/sql-to-c-binary?view=sql-server-ver15
*str_len = size * 2;
if((*str_len) + 1 > target_len) {
size = (target_len - 1) / 2;
resp = CONV_TRUNCATED;
}
for (int i = 0, ofs = 0; i < size; ++i)
ofs += sprintf(((char *)target_ptr) + ofs, "%02X", ((unsigned char *)value)[i]);
((char *)target_ptr)[size] = '\0';
break;
case SQL_C_WCHAR:
*str_len = size * 2 * sizeof(SQLWCHAR);
charlen = target_len / sizeof(SQLWCHAR);
if((*str_len) + 1 > charlen) {
size = (charlen - 1) / 2;
resp = CONV_TRUNCATED;
}
for (int i = 0, ofs = 0; i < size; ++i)
ofs += swprintf(((SQLWCHAR *)target_ptr) + ofs, charlen - ofs, L"%02X", ((unsigned char *)value)[i]);
((SQLWCHAR *)target_ptr)[size * 2] = '\0';
break;
default:
return CONV_IMPOSSIBLE;
}
return resp;
}
/*
Converts a cdb2 integer to a @c_data_type value.
This function exactly follows ODBC standard and is fully functional.
*/
conv_resp convert_cdb2int(const void *value, int size, SQLSMALLINT c_data_type, SQLPOINTER target_ptr, SQLLEN target_len, SQLLEN *str_len)
{
LL num = *((LL*) value);
switch(c_data_type) {
case SQL_C_CHAR:
*str_len = snprintf((char *)target_ptr, target_len, "%lld", num);
if(*str_len >= target_len)
return CONV_TRUNCATED;
break;
case SQL_C_WCHAR:
*str_len = swprintf((wchar_t *)target_ptr, target_len / sizeof(wchar_t), L"%lld", num) * sizeof(SQLWCHAR);
if(*str_len >= target_len / (SQLLEN)sizeof(wchar_t))
return CONV_TRUNCATED;
break;
case SQL_C_STINYINT:
case SQL_C_TINYINT:
SET_SQLSCHAR(target_ptr, num, *str_len);
break;
case SQL_C_UTINYINT:
SET_SQLCHAR(target_ptr, num, *str_len);
break;
case SQL_C_SBIGINT:
case SQL_C_DEFAULT:
SET_SQLBIGINT(target_ptr, num, *str_len);
break;
case SQL_C_UBIGINT:
SET_SQLUBIGINT(target_ptr, num, *str_len);
break;
case SQL_C_SSHORT:
case SQL_C_SHORT:
SET_SQLSMALLINT(target_ptr, num, *str_len);
break;
case SQL_C_USHORT:
SET_SQLUSMALLINT(target_ptr, num, *str_len);
break;
case SQL_C_SLONG:
case SQL_C_LONG:
SET_SQLINT(target_ptr, num, *str_len);
break;
case SQL_C_ULONG:
SET_SQLUINT(target_ptr, num, *str_len);
break;
case SQL_C_FLOAT:
SET_SQLFLOAT(target_ptr, num, *str_len);
break;
case SQL_C_DOUBLE:
SET_SQLDOUBLE(target_ptr, num, *str_len);
break;
case SQL_C_BIT:
if(num != 1 && num != 0)
return CONV_IMPOSSIBLE;
SET_SQLCHAR(target_ptr, num, *str_len);
break;
case SQL_C_BINARY:
memcpy(target_ptr, value, target_len > size ? size : target_len);
*str_len = size;
break;
default:
return CONV_IMPOSSIBLE;
}
return CONV_YEAH;
}
/*
Converts a cdb2 cstring to a @c_data_type value.
FIXME Only cstring-to-char/numeric conversion is supported currently.
*/
conv_resp convert_cdb2cstring(const void *value, int size, SQLSMALLINT c_data_type, SQLPOINTER target_ptr, SQLLEN target_len, SQLLEN *str_len)
{
conv_resp resp = CONV_YEAH;
int len;
switch(c_data_type) {
case SQL_C_CHAR:
case SQL_C_DEFAULT:
my_strncpy_out((char *)target_ptr, value, target_len);
*str_len = size - 1;
if(size > target_len)
resp = CONV_TRUNCATED;
break;
case SQL_C_WCHAR:
len = utf8_to_ucs2((SQLCHAR *)value, (SQLWCHAR *)target_ptr, target_len / sizeof(SQLWCHAR));
if (len > 0)
*str_len = (len - 1) * sizeof(SQLWCHAR);
if(size > (target_len / sizeof(SQLWCHAR)))
resp = CONV_TRUNCATED;
break;
default:
return CONV_IMPOSSIBLE;
}
return resp;
}
/*
Binds long long int value.
*/
conv_resp cdb2_bind_int(const char *name, LL val, void **buf, cdb2_hndl_tp *sqlh)
{
if((*buf = malloc(sizeof(LL))) == NULL)
return CONV_MEM_FAIL;
*(LL *)(*buf) = val;
/* For interger-to-integer, or integer-to-characters, we use CDB2_INTEGER type. */
if(cdb2_bind_param(sqlh, name, CDB2_INTEGER, *buf, sizeof(LL)))
return CONV_INTERNAL_ERR;
return CONV_YEAH;
}
/*
Binds real value.
*/
conv_resp cdb2_bind_real(const char *name, double val, void **buf, cdb2_hndl_tp *sqlh)
{
if((*buf = malloc(sizeof(double))) == NULL)
return CONV_MEM_FAIL;
*(double *)(*buf) = val;
/* For interger-to-integer, or integer-to-characters, we use CDB2_INTEGER type. */
if(cdb2_bind_param(sqlh, name, CDB2_REAL, *buf, sizeof(double)))
return CONV_INTERNAL_ERR;
return CONV_YEAH;
}
/* Converts an integral value to an interval. */
#define INT_TO_INTERVALYM(field) \
conv_resp int_to_interval_ ## field ( \
const char *name, LL val, void **buf, \
bool is_u, cdb2_hndl_tp *sqlh) \
{ \
cdb2_client_intv_ym_t *intv_ym; \
if((intv_ym = my_calloc(cdb2_client_intv_ym_t, 1)) == NULL) \
return CONV_MEM_FAIL; \
\
if(is_u) { intv_ym->sign = 1; intv_ym->field = (SQLUINTEGER)val; } \
else { \
intv_ym->sign = val < 0 ? -1 : 1; \
intv_ym->field = (SQLUINTEGER)(val < 0 ? -val : val); \
} \
*buf = (void *)intv_ym; \
if(cdb2_bind_param(sqlh, name, CDB2_INTERVALYM, *buf, sizeof(cdb2_client_intv_ym_t))) \
return CONV_INTERNAL_ERR; \
return CONV_YEAH; \
} \
extern int no_such_variable
INT_TO_INTERVALYM(years);
INT_TO_INTERVALYM(months);
#define INT_TO_INTERVALDS(field) \
conv_resp int_to_interval_ ## field ( \
const char *name, LL val, void **buf, \
bool is_u, cdb2_hndl_tp *sqlh) \
{ \
cdb2_client_intv_ds_t *intv_ds; \
if((intv_ds = my_calloc(cdb2_client_intv_ds_t, 1)) == NULL) \
return CONV_MEM_FAIL; \
\
if(is_u) { intv_ds->sign = 1; intv_ds->field = (SQLUINTEGER)val; } \
else { \
intv_ds->sign = val < 0 ? -1 : 1; \
intv_ds->field = (SQLUINTEGER)(val < 0 ? -val : val); \
} \
*buf = (void *)intv_ds; \
if(cdb2_bind_param(sqlh, name, CDB2_INTERVALDS, *buf, sizeof(cdb2_client_intv_ds_t))) \
return CONV_INTERNAL_ERR; \
return CONV_YEAH; \
} \
extern int no_such_variable
INT_TO_INTERVALDS(days);
INT_TO_INTERVALDS(hours);
INT_TO_INTERVALDS(mins);
INT_TO_INTERVALDS(sec);
/* Converts a real number to an interval. */
#define REAL_TO_INTERVALYM(field) \
static conv_resp real_to_interval_ ## field ( \
const char *name, double val, void **buf, cdb2_hndl_tp *sqlh) \
{ \
cdb2_client_intv_ym_t *intv_ym; \
if((intv_ym = my_calloc(cdb2_client_intv_ym_t, 1)) == NULL) \
return CONV_MEM_FAIL; \
\
intv_ym->sign = val < 0 ? -1 : 1; \
intv_ym->field = (SQLUINTEGER)(val < 0 ? -val : val); \
*buf = (void *)intv_ym; \
if(cdb2_bind_param(sqlh, name, CDB2_INTERVALYM, *buf, sizeof(cdb2_client_intv_ym_t))) \
return CONV_INTERNAL_ERR; \
return CONV_YEAH; \
} \
extern int no_such_variable
REAL_TO_INTERVALYM(years);
REAL_TO_INTERVALYM(months);
#define REAL_TO_INTERVALDS(field) \
static conv_resp real_to_interval_ ## field ( \
const char *name, double val, void **buf, cdb2_hndl_tp *sqlh) \
{ \
cdb2_client_intv_ds_t *intv_ds; \
if((intv_ds = my_calloc(cdb2_client_intv_ds_t, 1)) == NULL) \
return CONV_MEM_FAIL; \
\
intv_ds->sign = val < 0 ? -1 : 1; \
intv_ds->field = (SQLUINTEGER)(val < 0 ? -val : val); \
*buf = (void *)intv_ds; \
if(cdb2_bind_param(sqlh, name, CDB2_INTERVALDS, *buf, sizeof(cdb2_client_intv_ds_t))) \
return CONV_INTERNAL_ERR; \
return CONV_YEAH; \
} \
extern int no_such_variable
REAL_TO_INTERVALDS(days);
REAL_TO_INTERVALDS(hours);
REAL_TO_INTERVALDS(mins);
REAL_TO_INTERVALDS(sec);
/**
* Convert an integer from C type to CDB2 type and bind the parameter using CDB2 API.
* See http://msdn.microsoft.com/en-us/library/ms714147(v=vs.85).aspx for details.
*
* @return
* CONV_MEM_FAIL - memory allocation failure.
* CONV_OOPS - what the hell?
* CONV_UNSUPPORTED_C_TYPE - type unsupported.
* CONV_IMPOSSIBLE - conversion impossible.
* CONV_INTERNAL_ERR - cdb2 api reports an error.
* CONV_YEAH - yeah!
*/
conv_resp convert_and_bind_int(cdb2_hndl_tp *sqlh, struct param *param)
{
bool is_u;
LL val;
const void *buf = param->buf;
char *name = param->name + 1;
__debug("Try to bind an integral number.");
if(!param->buf) { /* A NULL value */
if(cdb2_bind_param(sqlh, name, CDB2_INTEGER, NULL, 0))
return CONV_INTERNAL_ERR;
return CONV_YEAH;
}
switch(param->c_type) {
case SQL_C_STINYINT:
case SQL_C_TINYINT:
val = *(SQLSCHAR *) buf;
is_u = 0;
break;
case SQL_C_SLONG:
case SQL_C_LONG:
val = *(SQLINTEGER *)buf;
is_u = 0;
break;
case SQL_C_SSHORT:
case SQL_C_SHORT:
val = *(SQLSMALLINT *)buf;
is_u = 0;
break;
case SQL_C_SBIGINT:
case SQL_C_DEFAULT:
val = *(SQLBIGINT *)buf;
is_u = 0;
break;
case SQL_C_BIT:
case SQL_C_UTINYINT:
val = *(SQLCHAR *) buf;
is_u = 1;
break;
case SQL_C_ULONG:
val = *(SQLUINTEGER *)buf;
is_u = 1;
break;
case SQL_C_USHORT:
val = *(SQLUSMALLINT *)buf;
is_u = 1;
break;
case SQL_C_UBIGINT:
val = *(SQLUBIGINT *)buf;
is_u = 1;
break;
default: /* Returns a flag and let other convertors take it over. */
__debug("Not a valid integral type.");
return CONV_UNSUPPORTED_C_TYPE;
}
free(param->internal_buffer);
param->internal_buffer = NULL;
switch(param->sql_type) {
case SQL_CHAR:
case SQL_VARCHAR:
case SQL_LONGVARCHAR:
case SQL_WCHAR:
case SQL_WVARCHAR:
case SQL_WLONGVARCHAR:
/* I am not sure if comdb2 has wchar type. */
case SQL_REAL:
case SQL_FLOAT:
case SQL_DOUBLE:
/* Handle precision. */
case SQL_DECIMAL:
case SQL_NUMERIC:
/* DECIMAL and NUMERIC also need scale. However for an integral type, there's no scale. */
if((param->internal_buffer = malloc(param->precision + 1)) == NULL)
return CONV_MEM_FAIL;
is_u ? snprintf(param->internal_buffer, param->precision + 1, "%llu", val)
: snprintf(param->internal_buffer, param->precision + 1, "%lld", val);
if(cdb2_bind_param(sqlh, name, CDB2_CSTRING, param->internal_buffer, (int)strlen(param->internal_buffer)))
return CONV_INTERNAL_ERR;
break;
case SQL_BIT:
/* SQL_BIT is a special case: the source must be 0 or 1. */
if(val != 0 && val != 1)
return CONV_IMPOSSIBLE;
case SQL_TINYINT:
case SQL_SMALLINT:
case SQL_INTEGER:
case SQL_BIGINT:
return cdb2_bind_int(name, val, ¶m->internal_buffer, sqlh);
/* Next, deal with interval types. */
case SQL_INTERVAL_YEAR:
return int_to_interval_years(name, val, ¶m->internal_buffer, is_u, sqlh);
case SQL_INTERVAL_MONTH:
return int_to_interval_months(name, val, ¶m->internal_buffer, is_u, sqlh);
case SQL_INTERVAL_DAY:
return int_to_interval_days(name, val, ¶m->internal_buffer, is_u, sqlh);
case SQL_INTERVAL_HOUR:
return int_to_interval_hours(name, val, ¶m->internal_buffer, is_u, sqlh);
case SQL_INTERVAL_MINUTE:
return int_to_interval_mins(name, val, ¶m->internal_buffer, is_u, sqlh);
case SQL_INTERVAL_SECOND:
return int_to_interval_sec(name, val, ¶m->internal_buffer, is_u, sqlh);
default:
return CONV_IMPOSSIBLE;
}
return CONV_YEAH;
}
/**
* Convert a real parameter from C type to CDB2 type and bind the parameter using CDB2 API.
* See http://msdn.microsoft.com/en-us/library/ms714147(v=vs.85).aspx for details.
*
* @return
* CONV_MEM_FAIL - memory allocation failure.
* CONV_OOPS - what the hell?
* CONV_UNSUPPORTED_C_TYPE - type unsupported.
* CONV_IMPOSSIBLE - conversion impossible.
* CONV_INTERNAL_ERR - cdb2 api reports an error.
* CONV_YEAH - yeah!
*/
conv_resp convert_and_bind_real(cdb2_hndl_tp *sqlh, struct param *param)
{
double val;
char *name = param->name + 1;
__debug("Try to bind a real number.");
if(!param->buf) { /* A NULL value */
if(cdb2_bind_param(sqlh, name, CDB2_REAL, NULL, 0))
return CONV_INTERNAL_ERR;
return CONV_YEAH;
}
/* float and double. */
switch(param->c_type) {
case SQL_C_FLOAT:
val = *((float *)param->buf);
break;
case SQL_C_DOUBLE:
case SQL_C_DEFAULT:
val = *((double *)param->buf);
break;
default: /* Returns a flag and let other convertors take it over. */
__debug("Not a valid real type.");
return CONV_UNSUPPORTED_C_TYPE;
}
free(param->internal_buffer);
param->internal_buffer = NULL;
switch(param->sql_type) {
case SQL_CHAR:
case SQL_VARCHAR:
case SQL_LONGVARCHAR:
case SQL_WCHAR:
case SQL_WVARCHAR:
case SQL_WLONGVARCHAR:
case SQL_REAL:
case SQL_FLOAT:
case SQL_DOUBLE:
case SQL_DECIMAL:
case SQL_NUMERIC:
if((param->internal_buffer = malloc(param->precision + 1)) == NULL)
return CONV_MEM_FAIL;
snprintf(param->internal_buffer, param->precision + 1, "%.*f", param->scale, val);
if(cdb2_bind_param(sqlh, name, CDB2_CSTRING, param->internal_buffer, (int)strlen(param->internal_buffer)))
return CONV_INTERNAL_ERR;
return CONV_YEAH;
}
/* @val now is guaranteed to be a valid value. */
switch(param->sql_type) {
/* Convert a real number to an integral number. But... Who would do this? */
case SQL_BIT:
/* SQL_BIT is a special case: the source must be 0 or 1. */
if(val < 0 || val >= 2)
return CONV_IMPOSSIBLE;
case SQL_TINYINT:
case SQL_SMALLINT:
case SQL_INTEGER:
case SQL_BIGINT:
return cdb2_bind_int(name, (LL)val, ¶m->internal_buffer, sqlh);
case SQL_INTERVAL_YEAR:
return real_to_interval_years(name, val, ¶m->internal_buffer, sqlh);
case SQL_INTERVAL_MONTH:
return real_to_interval_months(name, val, ¶m->internal_buffer, sqlh);
case SQL_INTERVAL_DAY:
return real_to_interval_days(name, val, ¶m->internal_buffer, sqlh);
case SQL_INTERVAL_HOUR:
return real_to_interval_hours(name, val, ¶m->internal_buffer, sqlh);
case SQL_INTERVAL_MINUTE:
return real_to_interval_mins(name, val, ¶m->internal_buffer, sqlh);
case SQL_INTERVAL_SECOND:
return real_to_interval_sec(name, val, ¶m->internal_buffer, sqlh);
default:
return CONV_IMPOSSIBLE;
}
}
/**
* Convert a cstring from C type to CDB2 type and bind the parameter using CDB2 API.
* See http://msdn.microsoft.com/en-us/library/ms714147(v=vs.85).aspx for details.
*
* @return
* CONV_MEM_FAIL - memory allocation failure.
* CONV_OOPS - what the hell?
* CONV_UNSUPPORTED_C_TYPE - type unsupported.
* CONV_IMPOSSIBLE - conversion impossible.
* CONV_INTERNAL_ERR - cdb2 api reports an error.
* CONV_YEAH - yeah!
*/
conv_resp convert_and_bind_cstring(cdb2_hndl_tp *sqlh, struct param *param)
{
const wchar_t *wcs;
const void *bound_buffer;
SQLULEN len, width;
double dval;
LL lval;
conv_resp resp = CONV_YEAH;
char *name = param->name + 1;
__debug("Try to bind a string.");
if(!param->buf) { /* A NULL value */
if(cdb2_bind_param(sqlh, name, CDB2_CSTRING, NULL, 0))
return CONV_INTERNAL_ERR;
return CONV_YEAH;
}
switch(param->c_type) {
case SQL_C_CHAR:
case SQL_C_DEFAULT:
len = strlen((char *)param->buf);
break;
case SQL_C_WCHAR:
/* Convert wchar_t to char. */
wcs = (wchar_t *)param->buf;
len = wcslen((wchar_t *)param->buf) * sizeof(wchar_t);
free(param->internal_buffer);
if((param->internal_buffer = malloc(len + 1)) == NULL)
return CONV_MEM_FAIL;
wcstombs((char *)param->internal_buffer, wcs, len);
break;
default: /* Returns a flag and let other convertors take it over. */
__debug("Not a valid cstring type.");
return CONV_UNSUPPORTED_C_TYPE;
}
bound_buffer = param->internal_buffer ? param->internal_buffer : param->buf;
switch(param->sql_type) {
case SQL_CHAR:
case SQL_VARCHAR:
case SQL_LONGVARCHAR:
case SQL_WCHAR:
case SQL_WVARCHAR:
case SQL_WLONGVARCHAR:
/* 1. Check deferred input. If the length is specified in the deferred input, use it.
2. No deferred input, we use @BufferLength.
3. Make sure the length is less than precision. */
/* The order cannot be altered. */
if(param->str_len) { /* deferred input */
width = *param->str_len;
if(width < 0) {
if(width == SQL_NTS)
width = len;
else
return CONV_INVALID_BUFLEN;
}
} else
width = param->buflen;
width = MIN(width, len);
if(width >= param->precision)
/* #1 Unlike binary data(see #1 below), a '\0' is needed to terminate the string. */
resp = CONV_BUF_OVERFLOW;
if(cdb2_bind_param(sqlh, name, CDB2_CSTRING, bound_buffer, (int)width))
resp = CONV_INTERNAL_ERR;
break;
case SQL_BINARY:
case SQL_VARBINARY:
case SQL_LONGVARBINARY:
if(param->str_len) { /* deferred input */
width = *param->str_len;
if(width < 0) {
if(width == SQL_NTS)
width = len;
else
return CONV_INVALID_BUFLEN;
}
} else
width = param->buflen;
width = MIN(width, len);
if(width > param->precision) /* #1 */
resp = CONV_BUF_OVERFLOW;
if(cdb2_bind_param(sqlh, name, CDB2_BLOB, bound_buffer, (int)width))
resp = CONV_INTERNAL_ERR;
break;
/* For the next 3 types, let COMDB2 handle them (They must be valid literals).
Just send the orignial string representation. */
case SQL_TYPE_DATE:
case SQL_TYPE_TIME:
case SQL_TYPE_TIMESTAMP:
if(cdb2_bind_param(sqlh, name, CDB2_CSTRING, bound_buffer, (int)len))
resp = CONV_INTERNAL_ERR;
break;
case SQL_BIT:
if(strcmp(bound_buffer, "0") || strcmp(bound_buffer, "1"))
resp = CONV_IMPOSSIBLE;
case SQL_REAL:
case SQL_FLOAT:
case SQL_DOUBLE:
case SQL_DECIMAL:
case SQL_NUMERIC:
dval = atof(bound_buffer);
/* If the param is not wchar_t *, no harm to free a NULL pointer.