DCL 4.1
Loading...
Searching...
No Matches
SQLField.cpp
Go to the documentation of this file.
1#include <dcl/Config.h>
2
3#if __DCL_WINDOWS
4 #include <windows.h>
5#else
6 #include <time.h>
7#endif
8
9#include <string.h> // memset
10#include <wctype.h> // iswdigit
11#include <float.h> // FLT_MIN, FLT_MAX
12#include <math.h> // modf
13#include <wchar.h> // wcsftime
14
15#include <dcl/Object.h>
16
17#if __DCL_HAVE_ALLOC_DEBUG
18#undef __DCL_ALLOC_LEVEL
19#define __DCL_ALLOC_LEVEL __DCL_ALLOC_INTERNAL
20#endif
21
22#include <dcl/Charset.h>
23#include <dcl/SQL.h>
24
25#if __DCL_DEBUG
26#undef __THIS_FILE__
27static const char_t __THIS_FILE__[] = __T("dcl/SQLField.cpp");
28#endif
29
30__DCL_BEGIN_NAMESPACE
31
33
35
36/*
37SQLField::SQLField(SQL::Field* hField)
38{
39 __handle = hField;
40}
41*/
42
44{
45 __handle = NULL;
46 __query = NULL;
47}
48
52
53const String& SQLField::name() const
54{
55 __DCL_ASSERT(this != NULL);
57
58 return __handle->name();
59}
60
61SQL::DataType SQLField::dataType() const
62{
63 __DCL_ASSERT(this != NULL);
65
66 return __handle->dataType();
67}
68
69const wchar_t* SQLField::serverDataTypeName() const
70{
71 __DCL_ASSERT(this != NULL);
73
74 return __handle->serverDataTypeName();
75}
76
77short SQLField::precision() const
78{
79 __DCL_ASSERT(this != NULL);
81
82 return __handle->precision();
83}
84
85short SQLField::scale() const
86{
87 __DCL_ASSERT(this != NULL);
89
90 return __handle->scale();
91}
92
93bool SQLField::isNull() const
94{
95 __DCL_ASSERT(this != NULL);
97
98 return __handle->isNull();
99}
100
101size_t SQLField::getDataSize(bool _maxSize)__DCL_THROWS1(SQLException*)
102{
103 __DCL_ASSERT(this != NULL);
104 __DCL_ASSERT(__handle != NULL);
105
106 size_t nSize = 0;
107 if (!__handle->getDataSize(&nSize, _maxSize)) {
108 throw new SQLException(this);
109 }
110 return nSize;
111}
112
113void SQLField::getData(
114 void* _pv, // buffer
115 size_t* _pn, // sizeof *_pv
116 SQL::DataType _dataType // buffer data type
118{
119 __DCL_ASSERT(this != NULL);
120 __DCL_ASSERT(__handle != NULL);
121
122 __DCL_ASSERT(!isNull());
123
124 if (!__handle->getData(_pv, _pn, _dataType)) {
125 throw new SQLException(this);
126 }
127}
128
130
131void SQLField::getValue(int32_t& _r) __DCL_THROWS1(SQLException*)
132{
133 __DCL_ASSERT(dataType() == SQL::typeInteger
134 || dataType() == SQL::typeNumeric);
135
136 size_t n = sizeof(int32_t);
137 getData(&_r, &n, SQL::typeInteger);
138
139 __DCL_ASSERT(n == sizeof(int32_t));
140}
141
142void SQLField::getValue(uint32_t& _r) __DCL_THROWS1(SQLException*)
143{
144 __DCL_ASSERT(dataType() == SQL::typeUInteger
145 || dataType() == SQL::typeNumeric);
146
147 size_t n = sizeof(uint32_t);
148 getData(&_r, &n, SQL::typeUInteger);
149
150 __DCL_ASSERT(n == sizeof(uint32_t));
151}
152
153void SQLField::getValue(int64_t& _r) __DCL_THROWS1(SQLException*)
154{
155 __DCL_ASSERT(dataType() == SQL::typeInteger
156 || dataType() == SQL::typeNumeric);
157
158 size_t n = sizeof(int64_t);
159 getData(&_r, &n, SQL::typeInteger);
160
161 __DCL_ASSERT(n == sizeof(int64_t));
162}
163
164void SQLField::getValue(uint64_t& _r) __DCL_THROWS1(SQLException*)
165{
166 __DCL_ASSERT(dataType() == SQL::typeUInteger
167 || dataType() == SQL::typeNumeric);
168
169 size_t n = sizeof(uint64_t);
170 getData(&_r, &n, SQL::typeUInteger);
171
172 __DCL_ASSERT(n == sizeof(uint64_t));
173}
174
175void SQLField::getValue(float& _r) __DCL_THROWS1(SQLException*)
176{
177 __DCL_ASSERT(dataType() == SQL::typeFloat
178 || dataType() == SQL::typeNumeric);
179
180 size_t n = sizeof(float);
181 getData(&_r, &n, SQL::typeFloat);
182
183 __DCL_ASSERT(n == sizeof(float));
184}
185
186void SQLField::getValue(double& _r) __DCL_THROWS1(SQLException*)
187{
188 __DCL_ASSERT(dataType() == SQL::typeFloat
189 || dataType() == SQL::typeNumeric);
190
191 size_t n = sizeof(double);
192 getData(&_r, &n, SQL::typeFloat);
193
194 __DCL_ASSERT(n == sizeof(double));
195}
196
197void SQLField::getValue(SQL::Date& _r) __DCL_THROWS1(SQLException*)
198{
199 __DCL_ASSERT(dataType() == SQL::typeDate);
200 __DCL_ASSERT(dataSizeMax() == sizeof(SQL::Date));
201
202 size_t n = sizeof(SQL::Date);
203 getData(&_r, &n, SQL::typeDate);
204
205 __DCL_ASSERT(n == sizeof(SQL::Date));
206}
207
208void SQLField::getValue(SQL::Time& _r) __DCL_THROWS1(SQLException*)
209{
210 __DCL_ASSERT(dataType() == SQL::typeTime
211 || dataType() == SQL::typeTimeTz);
212 __DCL_ASSERT(dataSizeMax() == sizeof(SQL::Time));
213
214 size_t n = sizeof(SQL::Time);
215 getData(&_r, &n, SQL::typeTime);
216
217 __DCL_ASSERT(n == sizeof(SQL::Time));
218}
219
220void SQLField::getValue(SQL::TimeStamp& _r) __DCL_THROWS1(SQLException*)
221{
222 __DCL_ASSERT(dataType() == SQL::typeTimeStamp
223 || dataType() == SQL::typeTimeStampTz);
224 __DCL_ASSERT(dataSizeMax() == sizeof(SQL::TimeStamp));
225
226 size_t n = sizeof(SQL::TimeStamp);
227 getData(&_r, &n, SQL::typeTimeStamp);
228
229 __DCL_ASSERT(n == sizeof(SQL::TimeStamp));
230}
231
232void SQLField::getValue(SQL::Interval& _r) __DCL_THROWS1(SQLException*)
233{
234 __DCL_ASSERT(dataType() == SQL::typeInterval
235 || dataType() == SQL::typeIntervalYm
236 || dataType() == SQL::typeIntervalDs);
237 __DCL_ASSERT(dataSizeMax() == sizeof(SQL::Interval));
238
239 size_t n = sizeof(SQL::Interval);
240 getData(&_r, &n, SQL::typeInterval);
241
242 __DCL_ASSERT(n == sizeof(SQL::Interval));
243}
244
245void SQLField::getValue(String& _r)
247{
248 __DCL_ASSERT((dataType() == SQL::typeNumeric)
249 || (dataType() == SQL::typeText)
250 || (dataType() == SQL::typeLongText)
251 || (dataType() == SQL::typeClob)
252 );
253
254 size_t n = dataSize(); // current size
256
257 ByteBuffer* buf = ByteBuffer::create(n);
258 try {
259 getData(buf->data(), &n, SQL::typeText);
260 buf->__dataLength = n;
261 _r = UTF8Decoder::decode(buf->data(), buf->__dataLength);
262 buf->release();
263 }
264 catch(Exception* e) {
265 buf->release();
266 throw e;
267 }
268}
269
270void SQLField::getValue(
271 ByteString& _r,
272 SQL::DataType _dataType // = SQL::typeBinary
274{
275 __DCL_ASSERT((dataType() == SQL::typeNumeric)
276 || (dataType() == SQL::typeText)
277 || (dataType() == SQL::typeLongText)
278 || (dataType() == SQL::typeClob)
279 || (dataType() == SQL::typeBinary)
280 || (dataType() == SQL::typeLongBinary)
281 || (dataType() == SQL::typeBlob));
282 __DCL_ASSERT((_dataType == SQL::typeText)
283 || (_dataType == SQL::typeBinary));
284
285 size_t n = dataSize(); // current size
287
288 ByteBuffer* buf = ByteBuffer::create(n);
289 try {
290 getData(buf->data(), &n, _dataType);
291 buf->__dataLength = n;
292 _r.assign(buf);
293 buf->release();
294 }
295 catch (SQLException* e) {
296 buf->release();
297 throw e;
298 }
299}
300
301void SQLField::getValue(
303 size_t _n
305{
306 __DCL_ASSERT((dataType() == SQL::typeText)
307 || (dataType() == SQL::typeBinary)
308 || (dataType() == SQL::typeLongText)
309 || (dataType() == SQL::typeLongBinary)
310 || (dataType() == SQL::typeClob)
311 || (dataType() == SQL::typeBlob)
312 );
313 getData(&_r, &_n, SQL::typeOutputStream);
314}
315
316bool SQLField::asBoolean() __DCL_THROWS1(SQLException*)
317{
318 static const char_t* _cast = __T("asBoolean");
319 bool result = false;
320 switch(dataType()) {
321 case SQL::typeInteger :
322 case SQL::typeUInteger: {
323 int64_t _r = 0;
324 getValue(_r);
325 result = _r != (int64_t)0;
326 break;
327 }
328 case SQL::typeFloat: {
329 double d;
330 getValue(d);
331 result = d != 0.;
332 break;
333 }
334 case SQL::typeNumeric: {
335 String _r;
336 getValue(_r);
337 const wchar_t* p = (const wchar_t*)_r;
338 while (*p) {
339 if (iswdigit(*p)) {
340 if (*p != __T('0')) {
341 result = true;
342 break;
343 }
344 }
345 else {
346 if (*p != __T('.')) {
347 result = true;
348 break;
349 }
350 }
351 p++;
352 }
353 break;
354 }
355 case SQL::typeText: {
356 String _r;
357 getValue(_r);
358 const wchar_t* p = (const wchar_t*)_r;
359 if (_r.length() == 1) {
360 if (!(*p == __T(' ') || *p == __T('0')
361 || *p == __T('f') || *p == __T('F')))
362 result = true;
363 }
364 else {
365 while (*p) {
366 if (!(*p == __T(' ') || *p == __T('0'))) {
367 result = true;
368 break;
369 }
370 p++;
371 }
372 }
373 break;
374 }
375 default : {
376 // invalid data-type
377 throw new SQLException(
378 this,
379 _cast,
380 SQLException::eInvalidCast
381 );
382 }
383 }
384
385 return result;
386}
387
388int32_t SQLField::asInt32() __DCL_THROWS1(SQLException*)
389{
390 static const char_t* _cast = __T("asInt32");
391 int32_t result = 0;
392 switch(dataType()) {
393 case SQL::typeInteger: {
394 if (dataSizeMax() <= sizeof(int32_t))
395 getValue(result);
396 else {
397 int64_t _r;
398 getValue(_r);
399 if (_r < (int64_t)INT32_MIN || (int64_t)INT32_MAX < _r) {
400 throw new SQLException(
401 this,
402 _cast,
403 SQLException::eOutOfRange
404 );
405 }
406 result = (int32_t)_r;
407 }
408 break;
409 }
410 case SQL::typeUInteger: {
411 if (dataSizeMax() <= sizeof(uint32_t))
412 getValue((uint32_t&)result);
413 else {
414 uint64_t _r;
415 getValue(_r);
416 if ((uint64_t)UINT32_MAX < _r) {
417 throw new SQLException(
418 this,
419 _cast,
420 SQLException::eOutOfRange
421 );
422 }
423 result = (int32_t)_r;
424 }
425 break;
426 }
427 case SQL::typeFloat: {
428 double _r;
429 getValue(_r);
430 double n;
431 modf(_r, &n);
432 if (n < (double)INT32_MIN || (double)INT32_MAX < n) {
433 throw new SQLException(
434 this,
435 _cast,
436 SQLException::eOutOfRange
437 );
438 }
439 result = (int32_t)n;
440 break;
441 }
442 case SQL::typeNumeric: {
443 getValue(result);
444 break;
445 }
446 case SQL::typeText: {
447 String _r;
448 getValue(_r);
449
450 size_t index = _r.indexOf(__T('e'));
451 if (index == (size_t)-1)
452 index = _r.indexOf(__T('E'));
453 if (index != (size_t)-1) {
454 // float format string
455 double d = 0.;
456 try {
457 d = Double::parse(_r);
458 }
459 catch (NumericConvertException* e) {
460 throw new SQLException(this, _cast, e);
461 }
462 double n;
463 modf(d, &n);
464 if (n < (double)INT32_MIN || (double)INT32_MAX < n) {
465 throw new SQLException(
466 this,
467 _cast,
468 SQLException::eOutOfRange
469 );
470 }
471 result = (int32_t)n;
472 }
473 else {
474 // decimal point
475 index = _r.indexOf(__T('.'));
476 if (index != (size_t)-1)
477 _r = _r.left(index);
478 //_r.setLength(index);
479
480 try {
481 result = Int32::parse(_r, 10);
482 }
483 catch (NumericConvertException* e) {
484 // ERANGE
485 throw new SQLException(this, _cast, e);
486 }
487 }
488 break;
489 }
490 default : {
491 // invalid data-type
492 throw new SQLException(
493 this,
494 _cast,
495 SQLException::eInvalidCast
496 );
497 }
498 }
499
500 return result;
501}
502
503int64_t SQLField::asInt64() __DCL_THROWS1(SQLException*)
504{
505 static const char_t* _cast = __T("asInt64");
506 int64_t result = 0;
507 switch(dataType()) {
508 case SQL::typeInteger: {
509 getValue(result);
510 break;
511 }
512 case SQL::typeUInteger: {
513 uint64_t _r;
514 getValue(_r);
515 result = (int64_t)_r;
516 break;
517 }
518 case SQL::typeFloat: {
519 double _r;
520 getValue(_r);
521 double n;
522 modf(_r, &n);
523 if (n < (double)INT64_MIN || (double)INT64_MAX < n) {
524 throw new SQLException(
525 this,
526 _cast,
527 SQLException::eOutOfRange
528 );
529 }
530 result = (int64_t)_r;
531 break;
532 }
533 case SQL::typeNumeric: {
534 getValue(result);
535 break;
536 }
537 case SQL::typeText: {
538 String _r;
539 getValue(_r);
540 size_t index = _r.indexOf(__T('e'));
541 if (index == (size_t)-1)
542 index = _r.indexOf(__T('E'));
543 if (index != (size_t)-1) {
544 // float format string
545 double d = 0.;
546 try {
547 d = Double::parse(_r);
548 }
549 catch (NumericConvertException* e) {
550 throw new SQLException(this, _cast, e);
551 }
552 double n;
553 modf(d, &n);
554 if (n < (double)INT64_MIN || (double)INT64_MAX < n) {
555 throw new SQLException(
556 this,
557 _cast,
558 SQLException::eOutOfRange
559 );
560 }
561 result = (int64_t)n;
562 }
563 else {
564 // decimal point
565 index = _r.indexOf('.');
566 if (index != (size_t)-1)
567 _r = _r.left(index);
568 //_r.setLength(index);
569
570 try {
571 result = Int64::parse(_r, 10);
572 }
573 catch (NumericConvertException* e) {
574 // ERANGE
575 throw new SQLException(this, _cast, e);
576 }
577 }
578 break;
579 }
580 default: {
581 // invalid data-type
582 throw new SQLException(
583 this,
584 _cast,
585 SQLException::eInvalidCast
586 );
587 }
588 }
589
590 return result;
591}
592
593float SQLField::asSingle() __DCL_THROWS1(SQLException*)
594{
595 static const char_t* _cast = __T("asSingle");
596 float result = 0.;
597 switch(dataType()) {
598 case SQL::typeFloat: {
599 if (dataSizeMax() <= sizeof(float))
600 getValue(result);
601 else {
602 double _r;
603 getValue(_r);
604 if (_r < FLT_MIN || FLT_MAX < _r) {
605 throw new SQLException(
606 this,
607 _cast,
608 SQLException::eOutOfRange
609 );
610 }
611 result = (float)_r;
612 }
613 break;
614 }
615 case SQL::typeInteger: {
616 if (dataSizeMax() <= sizeof(int32_t)) {
617 int32_t _r;
618 getValue(_r);
619 result = (float)_r;
620 }
621 else {
622 int64_t _r;
623 getValue(_r);
624 result = (float)_r;
625 }
626 }
627 case SQL::typeUInteger: {
628 if (dataSizeMax() <= sizeof(uint32_t)) {
629 uint32_t _r;
630 getValue(_r);
631 result = (float)_r;
632 }
633 else {
634 uint64_t _r;
635 getValue(_r);
636 result = (float)_r;
637 }
638 break;
639 }
640 case SQL::typeNumeric: {
641 getValue(result);
642 break;
643 }
644 case SQL::typeText: {
645 String _r;
646 getValue(_r);
647
648 try {
650 }
651 catch (NumericConvertException* e) {
652 // ERANGE
653 throw new SQLException(this, _cast, e);
654 }
655 break;
656 }
657 default : {
658 // invalid data-type
659 throw new SQLException(
660 this,
661 _cast,
662 SQLException::eInvalidCast
663 );
664 }
665 }
666
667 return result;
668}
669
670double SQLField::asDouble() __DCL_THROWS1(SQLException*)
671{
672 static const char_t* _cast = __T("asDouble");
673 double result = 0.;
674 switch(dataType()) {
675 case SQL::typeFloat: {
676 getValue(result);
677 break;
678 }
679 case SQL::typeInteger: {
680 if (dataSizeMax() <= sizeof(int32_t)) {
681 int32_t _r;
682 getValue(_r);
683 result = (double)_r;
684 }
685 else {
686 int64_t _r;
687 getValue(_r);
688 result = (double)_r;
689 }
690 break;
691 }
692 case SQL::typeUInteger: {
693 if (dataSizeMax() <= sizeof(uint32_t)) {
694 uint32_t _r;
695 getValue(_r);
696 result = (double)_r;
697 }
698 else {
699 uint64_t _r;
700 getValue(_r);
701 result = (double)_r;
702 }
703 break;
704 }
705 case SQL::typeNumeric: {
706 getValue(result);
707 break;
708 }
709 case SQL::typeText: {
710 String _r;
711 getValue(_r);
712
713 try {
715 }
716 catch (NumericConvertException* e) {
717 // ERANGE
718 throw new SQLException(this, _cast, e);
719 }
720 break;
721 }
722 default : {
723 // invalid data-type
724 throw new SQLException(
725 this,
726 _cast,
727 SQLException::eInvalidCast
728 );
729 }
730 }
731
732 return result;
733}
734
735Date SQLField::asDate() __DCL_THROWS1(SQLException*)
736{
737 static const char_t* _cast = __T("asDate");
738 Date result;
739
740 switch(dataType()) {
741 case SQL::typeDate: {
742 SQL::Date _r;
743 getValue(_r);
744 result.assign(_r.year, _r.month, _r.day);
745 break;
746 }
747 case SQL::typeTimeStamp :
749 SQL::TimeStamp _r;
750 getValue(_r);
751 result.assign(_r.year, _r.month, _r.day);
752 break;
753 }
754 default : {
755 // invalid data-type
756 throw new SQLException(
757 this,
758 _cast,
759 SQLException::eInvalidCast
760 );
761 }
762 }
763
764 return result;
765}
766
767Time SQLField::asTime() __DCL_THROWS1(SQLException*)
768{
769 static const char_t* _cast = __T("asTime");
770 Time result;
771
772 switch(dataType()) {
773 case SQL::typeTime:
774 case SQL::typeTimeTz: {
775 SQL::Time _r;
776 getValue(_r);
777 result.assign(
778 _r.hour,
779 _r.min,
780 _r.sec,
781 _r.frac / 1000000
782 );
783 break;
784 }
785 case SQL::typeTimeStamp :
787 SQL::TimeStamp _r;
788 getValue(_r);
789 result.assign(
790 _r.hour,
791 _r.min,
792 _r.sec,
793 _r.frac / 1000000
794 );
795 break;
796 }
797 default : {
798 // invalid data-type
799 throw new SQLException(
800 this,
801 _cast,
802 SQLException::eInvalidCast
803 );
804 }
805 }
806
807 return result;
808}
809
810DateTime SQLField::asDateTime() __DCL_THROWS1(SQLException*)
811{
812 static const char_t* _cast = __T("asDateTime");
813 DateTime result;
814
815 switch(dataType()) {
816 case SQL::typeTimeStamp :
818 SQL::TimeStamp _r;
819 getValue(_r);
820 result.assign(
821 _r.year,
822 _r.month,
823 _r.day,
824 _r.hour,
825 _r.min,
826 _r.sec,
827 _r.frac / 1000000
828 );
829 break;
830 }
831 case SQL::typeDate: {
832 SQL::Date _r;
833 getValue(_r);
834 result.date().assign(
835 _r.year,
836 _r.month,
837 _r.day
838 );
839 break;
840 }
841 case SQL::typeTime:
842 case SQL::typeTimeTz: {
843 SQL::Time _r;
844 getValue(_r);
845 result.time().assign(
846 _r.hour,
847 _r.min,
848 _r.sec,
849 _r.frac / 1000000
850 );
851 break;
852 }
853 default : {
854 // invalid data-type
855 throw new SQLException(
856 this,
857 _cast,
858 SQLException::eInvalidCast
859 );
860 }
861 }
862
863 return result;
864}
865
866inline int __ABS(int n)
867{
868 return n < 0 ? -n : n;
869}
870
871Interval SQLField::asInterval() __DCL_THROWS1(SQLException*)
872{
873 static const char_t* _cast = __T("asInterval");
874 Interval result;
875
876 switch(dataType()) {
877 case SQL::typeIntervalYm: {
878 SQL::Interval _r;
879 getValue(_r);
880 if (__ABS(_r.years) > 5965231) {
881 // YEAR TO MONTH의 경우 5965231년 4개월 까지만 유효
882 throw new SQLException(
883 this,
884 _cast,
885 SQLException::eOutOfRange
886 );
887 }
888 result.assign(
889 _r.years * 360 + _r.months * 30,
890 0
891 );
892 break;
893 }
894 case SQL::typeIntervalDs: {
895 SQL::Interval _r;
896 getValue(_r);
897 result.assign(
898 _r.days,
899 _r.hours,
900 _r.mins,
901 _r.secs,
902 _r.fracs / 1000000
903 );
904 break;
905 }
906 case SQL::typeInterval: {
907 SQL::Interval _r;
908 getValue(_r);
909 if (__ABS(_r.years) > 5965231) {
910 // YEAR TO MONTH의 경우 5965231년 4개월 까지만 유효
911 throw new SQLException(
912 this,
913 _cast,
914 SQLException::eOutOfRange
915 );
916 }
917 result.assign(
918 _r.days + _r.years * 360 + _r.months * 30,
919 _r.hours,
920 _r.mins,
921 _r.secs,
922 _r.fracs / 1000000
923 );
924 break;
925 }
926 default : {
927 // invalid data-type
928 throw new SQLException(
929 this,
930 _cast,
931 SQLException::eInvalidCast
932 );
933 }
934 }
935 return result;
936}
937
938String SQLField::asString()
940{
941 String result;
942 size_t nDataSize = dataSize(); // current size
943 switch(dataType()) {
944 case SQL::typeInteger: {
945 if (nDataSize <= sizeof(int32_t)) {
946 int32_t _r;
947 getValue(_r);
949 }
950 else {
951 int64_t _r;
952 getValue(_r);
954 }
955 break;
956 }
957 case SQL::typeUInteger: {
958 if (nDataSize <= sizeof(int32_t)) {
959 uint32_t _r;
960 getValue(_r);
962 }
963 else {
964 uint64_t _r;
965 getValue(_r);
967 }
968 break;
969 }
970 case SQL::typeFloat: {
971 if (nDataSize == sizeof(float)) {
972 float _r;
973 getValue(_r);
975 }
976 else {
977 double _r;
978 getValue(_r);
980 }
981 break;
982 }
983 case SQL::typeNumeric: {
984 getValue(result);
985 break;
986 }
987 case SQL::typeDate: {
989 getValue(_r);
990 result = String::format(
991 __T("%04d-%02d-%02d"),
992 _r.year,
993 _r.month,
994 _r.day
995 );
996 break;
997 }
998 case SQL::typeTime: {
1000 getValue(_r);
1001 result = String::format(
1002 __T("%02d:%02d:%02d.%03d"),
1003 _r.hour,
1004 _r.min,
1005 _r.sec,
1006 _r.frac / 1000000
1007 );
1008 break;
1009 }
1010 case SQL::typeTimeTz: {
1011 SQL::Time _r;
1012 getValue(_r);
1013 result = String::format(
1014 __T("%02d:%02d:%02d.%03d %+03d%02d"),
1015 _r.hour,
1016 _r.min,
1017 _r.sec,
1018 _r.frac / 1000000,
1019 _r.tzoff / 60,
1020 __ABS(_r.tzoff % 60)
1021 );
1022 break;
1023 }
1024 case SQL::typeTimeStamp: {
1026 getValue(_r);
1027 result = String::format(
1028 __T("%04d-%02d-%02d %02d:%02d:%02d.%03d"),
1029 _r.year,
1030 _r.month,
1031 _r.day,
1032 _r.hour,
1033 _r.min,
1034 _r.sec,
1035 _r.frac / 1000000
1036 );
1037 break;
1038 }
1039 case SQL::typeTimeStampTz: {
1041 getValue(_r);
1042 result = String::format(
1043 __T("%04d-%02d-%02d %02d:%02d:%02d.%03d %+03d%02d"),
1044 _r.year,
1045 _r.month,
1046 _r.day,
1047 _r.hour,
1048 _r.min,
1049 _r.sec,
1050 _r.frac / 1000000,
1051 _r.tzoff / 60,
1052 __ABS(_r.tzoff % 60)
1053 );
1054 break;
1055 }
1056 case SQL::typeInterval: {
1058 getValue(_r);
1059 if (_r.years < 0 || _r.months < 0) {
1060 result = String::format(
1061 // 20,
1062 __T("-%d-%02d"),
1063 -_r.years,
1064 -_r.months
1065 );
1066 }
1067 else if (_r.years > 0 || _r.months > 0) {
1068 result = String::format(
1069 // 20,
1070 __T("+%d-%02d"),
1071 _r.years,
1072 _r.months
1073 );
1074 }
1075 else if (_r.days < 0
1076 || _r.hours < 0 || _r.mins < 0
1077 || _r.secs < 0 || _r.fracs < 0
1078 ) {
1079 result = String::format(
1080 // 30, // 24,
1081 __T("-%d %02d:%02d:%02d.%03d"),
1082 -(_r.days),
1083 -(_r.hours),
1084 -(_r.mins),
1085 -(_r.secs),
1086 -(_r.fracs / 1000000)
1087 );
1088 }
1089 else {
1090 result = String::format(
1091 // 30, // 24 = 1 + 10 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 3
1092 __T("+%d %02d:%02d:%02d.%03d"),
1093 (_r.days),
1094 (_r.hours),
1095 (_r.mins),
1096 (_r.secs),
1097 (_r.fracs / 1000000)
1098 );
1099 }
1100 break;
1101 }
1102 case SQL::typeIntervalYm: {
1104 getValue(_r);
1105 if (_r.years < 0 || _r.months < 0) {
1106 result = String::format(
1107 // 20,
1108 __T("-%d-%02d"),
1109 -_r.years,
1110 -_r.months
1111 );
1112 }
1113 else {
1114 result = String::format(
1115 // 20,
1116 __T("+%d-%02d"),
1117 _r.years,
1118 _r.months
1119 );
1120 }
1121 break;
1122 }
1123 case SQL::typeIntervalDs: {
1125 getValue(_r);
1126 if (_r.days < 0
1127 || _r.hours < 0 || _r.mins < 0
1128 || _r.secs < 0 || _r.fracs < 0
1129 ) {
1130 result = String::format(
1131 // 30, // 24,
1132 __T("-%d %02d:%02d:%02d.%03d"),
1133 -(_r.days),
1134 -(_r.hours),
1135 -(_r.mins),
1136 -(_r.secs),
1137 -(_r.fracs / 1000000)
1138 );
1139 }
1140 else {
1141 result = String::format(
1142 // 30, // 24 = 1 + 10 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 3
1143 __T("+%d %02d:%02d:%02d.%03d"),
1144 (_r.days),
1145 (_r.hours),
1146 (_r.mins),
1147 (_r.secs),
1148 (_r.fracs / 1000000)
1149 );
1150 }
1151 break;
1152 }
1153 case SQL::typeText :
1154 case SQL::typeLongText :
1155 case SQL::typeClob: {
1156 getValue(result);
1157 break;
1158 }
1159 case SQL::typeBinary :
1160 case SQL::typeLongBinary :
1161 case SQL::typeBlob: {
1162 ByteString _r;
1163 getValue(_r);
1164 result = String::tryString(_r, 20);
1165 break;
1166 }
1167 default: {
1168 __DCL_ASSERT(false);
1169 }
1170 }
1171 return result;
1172}
1173
1174// Formatted String
1175String SQLField::asStringF(const char_t* _format /* = NULL */)
1177{
1178 String result;
1179 size_t nDataSize = dataSize(); // current size
1180 switch(dataType()) {
1181 case SQL::typeInteger: {
1182 if (nDataSize <= sizeof(int32_t)) {
1183 int32_t _r;
1184 getValue(_r);
1185 result = Int32::toString(_r, _format);
1186 }
1187 else {
1188 int64_t _r;
1189 getValue(_r);
1190 result = Int64::toString(_r, _format);
1191 }
1192 break;
1193 }
1194 case SQL::typeUInteger: {
1195 if (nDataSize <= sizeof(uint32_t)) {
1196 uint32_t _r;
1197 getValue(_r);
1198 result = UInt32::toString(_r, _format);
1199 }
1200 else {
1201 uint64_t _r;
1202 getValue(_r);
1203 result = UInt64::toString(_r, _format);
1204 }
1205 break;
1206 }
1207 case SQL::typeFloat: {
1208 if (nDataSize == sizeof(float)) {
1209 float _r;
1210 getValue(_r);
1211 result = Single::toString(_r, _format);
1212 }
1213 else {
1214 double _r;
1215 getValue(_r);
1216 result = Double::toString(_r, _format);
1217 }
1218 break;
1219 }
1220 case SQL::typeNumeric: {
1221 getValue(result);
1222 break;
1223 }
1224 case SQL::typeDate: {
1225 if (!_format)
1226 _format = Date::FORMAT_STRING;
1227
1228 SQL::Date _r;
1229 getValue(_r);
1230
1231 struct tm t;
1232 memset(&t, 0, sizeof(struct tm));
1233 t.tm_year = _r.year - 1900;
1234 t.tm_mon = _r.month - 1;
1235 t.tm_mday = _r.day;
1236 t.tm_isdst = -1;
1237
1238 CharBuffer* buf = CharBuffer::create(DATETIME_FORMAT_BUFFER_SIZE);
1239 size_t n = wcsftime(buf->data(), DATETIME_FORMAT_BUFFER_SIZE, _format, &t);
1240 __DCL_ASSERT(buf->__allocLength >= n);
1241 buf->__dataLength = n;
1242
1243 result.assign(buf);
1244 buf->release();
1245 break;
1246 }
1247 case SQL::typeTime:
1248 case SQL::typeTimeTz: {
1249 if (!_format)
1250 _format = Time::FORMAT_STRING;
1251
1252 SQL::Time _r;
1253 getValue(_r);
1254
1255 struct tm t;
1256 memset(&t, 0, sizeof(struct tm));
1257 t.tm_hour = _r.hour;
1258 t.tm_min = _r.min;
1259 t.tm_sec = _r.sec;
1260 t.tm_isdst = -1;
1261
1262 CharBuffer* buf = CharBuffer::create(DATETIME_FORMAT_BUFFER_SIZE);
1263 size_t n = wcsftime(buf->data(), DATETIME_FORMAT_BUFFER_SIZE, _format, &t);
1264 __DCL_ASSERT(buf->__allocLength >= n);
1265 buf->__dataLength = n;
1266
1267 result.assign(buf);
1268 buf->release();
1269 break;
1270 }
1271 case SQL::typeTimeStamp:
1272 case SQL::typeTimeStampTz: {
1273 if (!_format)
1274 _format = DateTime::FORMAT_STRING;
1275
1277 getValue(_r);
1278
1279 struct tm t;
1280 t.tm_year = _r.year - 1900;
1281 t.tm_mon = _r.month - 1;
1282 t.tm_mday = _r.day;
1283 t.tm_hour = _r.hour;
1284 t.tm_min = _r.min;
1285 t.tm_sec = _r.sec;
1286
1287 t.tm_wday = 0;
1288 t.tm_yday = 0;
1289 t.tm_isdst = -1;
1290
1291 CharBuffer* buf = CharBuffer::create(DATETIME_FORMAT_BUFFER_SIZE);
1292 size_t n = wcsftime(buf->data(), DATETIME_FORMAT_BUFFER_SIZE, _format, &t);
1293 __DCL_ASSERT(buf->__allocLength >= n);
1294 buf->__dataLength = n;
1295
1296 result.assign(buf);
1297 buf->release();
1298 break;
1299 }
1300 case SQL::typeInterval:
1302 case SQL::typeIntervalDs: {
1303 result = asString();
1304 break;
1305 }
1306 case SQL::typeText:
1307 case SQL::typeLongText:
1308 case SQL::typeClob: {
1309 getValue(result);
1310 break;
1311 }
1312 case SQL::typeBinary :
1313 case SQL::typeLongBinary :
1314 case SQL::typeBlob: {
1315 ByteString _r;
1316 getValue(_r);
1317 result = String::tryString(_r, 20);
1318 break;
1319 }
1320 default: {
1321 __DCL_ASSERT(false);
1322 }
1323 }
1324 return result;
1325}
1326
1327__DCL_END_NAMESPACE
#define __THIS_FILE__
Definition _trace.h:14
#define NULL
Definition Config.h:340
#define INT32_MAX
Definition Config.h:318
wchar_t char_t
Definition Config.h:275
#define INT32_MIN
Definition Config.h:313
#define __DCL_THROWS2(e1, e2)
Definition Config.h:168
#define INT64_MIN
Definition Config.h:314
#define UINT32_MAX
Definition Config.h:323
#define INT64_MAX
Definition Config.h:319
#define __DCL_THROWS1(e)
Definition Config.h:167
#define DATETIME_FORMAT_BUFFER_SIZE
Definition DateTime.h:21
#define __ABS(n)
Definition MyParam.cpp:143
#define __DCL_ASSERT(expr)
Definition Object.h:371
#define IMPLEMENT_CLASSINFO(class_name, base_class_name)
Definition Object.h:228
#define __T(str)
Definition Object.h:44
ByteBuffer * buf
_r
Definition SQLField.cpp:261
void CharsetConvertException *size_t n
Definition SQLField.cpp:254
String CharsetConvertException *size_t nDataSize
Definition SQLField.cpp:942
return result
static const wchar_t * FORMAT_STRING
Definition DateTime.h:66
static const wchar_t * FORMAT_STRING
Definition DateTime.h:236
static double parse(const wchar_t *_number) __DCL_THROWS1(NumericConvertException *)
Definition Numeric.cpp:826
String toString() const
Definition Numeric.inl:147
String toString(unsigned _base=10) const
Definition Numeric.inl:87
static int32_t parse(const wchar_t *_number, unsigned _base=10) __DCL_THROWS1(NumericConvertException *)
Definition Numeric.cpp:284
String toString(unsigned _base=10) const
Definition Numeric.inl:111
static int64_t parse(const wchar_t *_number, unsigned _base=10) __DCL_THROWS1(NumericConvertException *)
Definition Numeric.cpp:513
Definition SQL.h:48
SQL::Field * __handle
Definition SQL.h:117
virtual ~SQLField()
Definition SQLField.cpp:49
SQLQuery * __query
Definition SQL.h:118
DataType
Definition SQLCore.h:62
@ typeBinary
Definition SQLCore.h:77
@ typeClob
Definition SQLCore.h:80
@ typeNumeric
Definition SQLCore.h:67
@ typeTime
Definition SQLCore.h:69
@ typeLongBinary
Definition SQLCore.h:79
@ typeUInteger
Definition SQLCore.h:65
@ typeTimeStamp
Definition SQLCore.h:71
@ typeBlob
Definition SQLCore.h:81
@ typeTimeStampTz
Definition SQLCore.h:72
@ typeInterval
Definition SQLCore.h:73
@ typeIntervalDs
Definition SQLCore.h:75
@ typeDate
Definition SQLCore.h:68
@ typeOutputStream
Definition SQLCore.h:85
@ typeText
Definition SQLCore.h:76
@ typeTimeTz
Definition SQLCore.h:70
@ typeFloat
Definition SQLCore.h:66
@ typeInteger
Definition SQLCore.h:64
@ typeIntervalYm
Definition SQLCore.h:74
@ typeLongText
Definition SQLCore.h:78
String toString() const
Definition Numeric.inl:135
static float parse(const wchar_t *_number) __DCL_THROWS1(NumericConvertException *)
Definition Numeric.cpp:728
static const wchar_t * FORMAT_STRING
Definition DateTime.h:129
String toString(unsigned _base=10) const
Definition Numeric.inl:99
String toString(unsigned _base=10) const
Definition Numeric.inl:123