Merge pull request #5039 from CEikermann/patch-1
[vuplus_xbmc] / xbmc / dbwrappers / qry_dat.cpp
1 /**********************************************************************
2  * Copyright (c) 2004, Leo Seib, Hannover
3  *
4  * Project: C++ Dynamic Library
5  * Module: FieldValue class realisation file
6  * Author: Leo Seib      E-Mail: leoseib@web.de
7  * Begin: 5/04/2002
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a copy
10  * of this software and associated documentation files (the "Software"), to deal
11  * in the Software without restriction, including without limitation the rights
12  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13  * copies of the Software, and to permit persons to whom the Software is
14  * furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be included in
17  * all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
22  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25  * THE SOFTWARE.
26  *
27  **********************************************************************/
28 /**********************************************************************
29  * 2005-03-29 - Minor modifications to allow get_asBool to function on
30  *              on string values that are 1 or 0
31  **********************************************************************/
32
33 #include "qry_dat.h"
34 #include "system.h" // for PRId64
35
36 #include <stdio.h>
37 #include <stdlib.h>
38
39 #ifndef __GNUC__
40 #pragma warning (disable:4800)
41 #pragma warning (disable:4715)
42 #endif
43
44 using namespace std;
45
46 namespace dbiplus {
47
48 //Constructors 
49 field_value::field_value(){
50   str_value = "";
51   field_type = ft_String;
52   is_null = false;
53   }
54
55 field_value::field_value(const char *s) {
56   str_value = s;
57   field_type = ft_String;
58   is_null = false;
59 }
60   
61 field_value::field_value(const bool b) {
62   bool_value = b; 
63   field_type = ft_Boolean;
64   is_null = false;
65 }
66
67 field_value::field_value(const char c) {
68   char_value = c; 
69   field_type = ft_Char;
70   is_null = false;
71 }
72   
73 field_value::field_value(const short s) {
74   short_value = s; 
75   field_type = ft_Short;
76   is_null = false;
77 }
78   
79 field_value::field_value(const unsigned short us) {
80   ushort_value = us; 
81   field_type = ft_UShort;
82   is_null = false;
83 }
84   
85 field_value::field_value(const int i) {
86   int_value = i; 
87   field_type = ft_Int;
88   is_null = false;
89 }
90   
91 field_value::field_value(const unsigned int ui) {
92   uint_value = ui; 
93   field_type = ft_UInt;
94   is_null = false;
95 }
96   
97 field_value::field_value(const float f) {
98   float_value = f; 
99   field_type = ft_Float;
100   is_null = false;
101 }
102   
103 field_value::field_value(const double d) {
104   double_value = d; 
105   field_type = ft_Double;
106   is_null = false;
107 }
108   
109 field_value::field_value(const int64_t i) {
110   int64_value = i; 
111   field_type = ft_Int64;
112   is_null = false;
113 }
114
115 field_value::field_value (const field_value & fv) {
116   switch (fv.get_fType()) {
117     case ft_String: {
118       set_asString(fv.get_asString());
119       break;
120     }
121     case ft_Boolean:{
122       set_asBool(fv.get_asBool());
123       break;     
124     }
125     case ft_Char: {
126       set_asChar(fv.get_asChar());
127       break;
128     }
129     case ft_Short: {
130       set_asShort(fv.get_asShort());
131       break;
132     }
133     case ft_UShort: {
134       set_asUShort(fv.get_asUShort());
135       break;
136     }
137     case ft_Int: {
138       set_asInt(fv.get_asInt());
139       break;
140     }
141     case ft_UInt: {
142       set_asUInt(fv.get_asUInt());
143       break;
144     }
145     case ft_Float: {
146       set_asFloat(fv.get_asFloat());
147       break;
148     }
149     case ft_Double: {
150       set_asDouble(fv.get_asDouble());
151       break;
152     }
153     case ft_Int64: {
154       set_asInt64(fv.get_asInt64());
155       break;
156     }
157     default:
158       break;
159   }
160   is_null = fv.get_isNull();
161 }
162
163
164 //empty destructor
165 field_value::~field_value(){
166
167   }
168
169   
170 //Conversations functions
171 string field_value::get_asString() const {
172     string tmp;
173     switch (field_type) {
174     case ft_String: {
175       tmp = str_value;
176       return tmp;
177     }
178     case ft_Boolean:{
179       if (bool_value) 
180         return tmp = "True";
181       else
182         return tmp = "False";
183     }
184     case ft_Char: {
185       return tmp = char_value;
186     }
187     case ft_Short: {
188       char t[10];
189       sprintf(t,"%i",short_value);
190       return tmp = t;
191     }
192     case ft_UShort: {
193       char t[10];
194       sprintf(t,"%i",ushort_value);
195       return tmp = t;
196     }
197     case ft_Int: {
198       char t[12];
199       sprintf(t,"%d",int_value);
200       return tmp = t;
201     }
202     case ft_UInt: {
203       char t[12];
204       sprintf(t,"%u",uint_value);
205       return tmp = t;
206     }
207     case ft_Float: {
208       char t[16];
209       sprintf(t,"%f",float_value);
210       return tmp = t;
211     }
212     case ft_Double: {
213       char t[32];
214       sprintf(t,"%f",double_value);
215       return tmp = t;
216     }
217     case ft_Int64: {
218       char t[23];
219       sprintf(t,"%"PRId64,int64_value);
220       return tmp = t;
221     }
222     default:
223       return tmp = "";
224     }
225   }
226
227
228
229 bool field_value::get_asBool() const {
230     switch (field_type) {
231     case ft_String: {
232       if (str_value == "True" || str_value == "true" || str_value == "1")
233           return true;
234       else
235         return false;
236     }
237     case ft_Boolean:{
238       return bool_value;
239       }
240     case ft_Char: {
241       if (char_value == 'T' || char_value == 't')
242         return true;
243       else
244         return false;
245     }
246     case ft_Short: {
247       return (bool)short_value;
248     }
249     case ft_UShort: {
250       return (bool)ushort_value;
251     }
252     case ft_Int: {
253       return (bool)int_value;
254     }
255     case ft_UInt: {
256       return (bool)uint_value;
257     }
258     case ft_Float: {
259       return (bool)float_value;
260     }
261     case ft_Double: {
262       return (bool)double_value;
263     }
264     case ft_Int64: {
265       return (bool)int64_value;
266     }
267     default:
268       return false;
269     }
270   }
271   
272
273 char field_value::get_asChar() const {
274   switch (field_type) {
275     case ft_String: {
276       return str_value[0];
277     }
278     case ft_Boolean:{
279       char c;
280       if (bool_value) 
281         return c='T';
282       else
283         return c='F';
284     }
285     case ft_Char: {
286       return  char_value;
287     }
288     case ft_Short: {
289       char t[10];
290       sprintf(t,"%i",short_value);
291       return t[0];
292     }
293     case ft_UShort: {
294       char t[10];
295       sprintf(t,"%i",ushort_value);
296       return t[0];
297     }
298     case ft_Int: {
299       char t[12];
300       sprintf(t,"%d",int_value);
301       return t[0];
302     }
303     case ft_UInt: {
304       char t[12];
305       sprintf(t,"%u",uint_value);
306       return t[0];
307     }
308     case ft_Float: {
309       char t[16];
310       sprintf(t,"%f",float_value);
311       return t[0];
312     }
313     case ft_Double: {
314       char t[32];
315       sprintf(t,"%f",double_value);
316       return t[0];
317     }
318     case ft_Int64: {
319       char t[24];
320       sprintf(t,"%"PRId64,int64_value);
321       return t[0];
322     }
323     default:
324       return '\0';
325     }
326   }
327
328
329 short field_value::get_asShort() const {
330     switch (field_type) {
331     case ft_String: {
332       return (short)atoi(str_value.c_str());
333     }
334     case ft_Boolean:{
335       return (short)bool_value;
336     }
337     case ft_Char: {
338       return (short)char_value;
339     }
340     case ft_Short: {
341        return short_value;
342     }
343     case ft_UShort: {
344        return (short)ushort_value;
345     }
346     case ft_Int: {
347       return (short)int_value;
348     }
349     case ft_UInt: {
350       return (short)uint_value;
351     }
352     case ft_Float: {
353       return (short)float_value;
354     }
355     case ft_Double: {
356       return (short)double_value;
357     }
358     case ft_Int64: {
359       return (short)int64_value;
360     }
361     default:
362       return 0;
363     }
364   }
365
366
367 unsigned short field_value::get_asUShort() const {
368     switch (field_type) {
369     case ft_String: {
370       return (unsigned short)atoi(str_value.c_str());
371     }
372     case ft_Boolean:{
373       return (unsigned short)bool_value;
374     }
375     case ft_Char: {
376       return (unsigned short)char_value;
377     }
378     case ft_Short: {
379        return (unsigned short)short_value;
380     }
381     case ft_UShort: {
382        return ushort_value;
383     }
384     case ft_Int: {
385       return (unsigned short)int_value;
386     }
387     case ft_UInt: {
388       return (unsigned short)uint_value;
389     }
390     case ft_Float: {
391       return (unsigned short)float_value;
392     }
393     case ft_Double: {
394       return (unsigned short)double_value;
395     }
396     case ft_Int64: {
397       return (unsigned short)int64_value;
398     }
399     default:
400       return 0;
401     }
402   }
403
404 int field_value::get_asInt() const {
405     switch (field_type) {
406     case ft_String: {
407       return (int)atoi(str_value.c_str());
408     }
409     case ft_Boolean:{
410       return (int)bool_value;
411     }
412     case ft_Char: {
413       return (int)char_value;
414     }
415     case ft_Short: {
416        return (int)short_value;
417     }
418     case ft_UShort: {
419        return (int)ushort_value;
420     }
421     case ft_Int: {
422       return int_value;
423     }
424     case ft_UInt: {
425       return (int)uint_value;
426     }
427     case ft_Float: {
428       return (int)float_value;
429     }
430     case ft_Double: {
431       return (int)double_value;
432     }
433     case ft_Int64: {
434       return (int)int64_value;
435     }
436     default:
437       return 0;
438     }
439   }
440
441 unsigned int field_value::get_asUInt() const {
442     switch (field_type) {
443     case ft_String: {
444       return (unsigned int)atoi(str_value.c_str());
445     }
446     case ft_Boolean:{
447       return (unsigned int)bool_value;
448     }
449     case ft_Char: {
450       return (unsigned int)char_value;
451     }
452     case ft_Short: {
453        return (unsigned int)short_value;
454     }
455     case ft_UShort: {
456        return (unsigned int)ushort_value;
457     }
458     case ft_Int: {
459       return (unsigned int)int_value;
460     }
461     case ft_UInt: {
462       return uint_value;
463     }
464     case ft_Float: {
465       return (unsigned int)float_value;
466     }
467     case ft_Double: {
468       return (unsigned int)double_value;
469     }
470     case ft_Int64: {
471       return (unsigned int)int64_value;
472     }
473     default:
474       return 0;
475     }
476   }
477
478 float field_value::get_asFloat() const {
479     switch (field_type) {
480     case ft_String: {
481       return (float)atof(str_value.c_str());
482     }
483     case ft_Boolean:{
484       return (float)bool_value;
485     }
486     case ft_Char: {
487       return (float)char_value;
488     }
489     case ft_Short: {
490        return (float)short_value;
491     }
492     case ft_UShort: {
493        return (float)ushort_value;
494     }
495     case ft_Int: {
496       return (float)int_value;
497     }
498     case ft_UInt: {
499       return (float)uint_value;
500     }
501     case ft_Float: {
502       return float_value;
503     }
504     case ft_Double: {
505       return (float)double_value;
506     }
507     case ft_Int64: {
508       return (float)int64_value;
509     }
510     default:
511       return 0.0;
512     }
513   }
514
515 double field_value::get_asDouble() const {
516     switch (field_type) {
517     case ft_String: {
518       return atof(str_value.c_str());
519     }
520     case ft_Boolean:{
521       return (double)bool_value;
522     }
523     case ft_Char: {
524       return (double)char_value;
525     }
526     case ft_Short: {
527        return (double)short_value;
528     }
529     case ft_UShort: {
530        return (double)ushort_value;
531     }
532     case ft_Int: {
533       return (double)int_value;
534     }
535     case ft_UInt: {
536       return (double)uint_value;
537     }
538     case ft_Float: {
539       return (double)float_value;
540     }
541     case ft_Double: {
542       return (double)double_value;
543     }
544     case ft_Int64: {
545       return (double)int64_value;
546     }
547     default:
548       return 0.0;
549     }
550   }
551
552 int64_t field_value::get_asInt64() const {
553     switch (field_type) {
554     case ft_String: {
555       return _atoi64(str_value.c_str());
556     }
557     case ft_Boolean:{
558       return (int64_t)bool_value;
559     }
560     case ft_Char: {
561       return (int64_t)char_value;
562     }
563     case ft_Short: {
564        return (int64_t)short_value;
565     }
566     case ft_UShort: {
567        return (int64_t)ushort_value;
568     }
569     case ft_Int: {
570       return (int64_t)int_value;
571     }
572     case ft_UInt: {
573       return (int64_t)uint_value;
574     }
575     case ft_Float: {
576       return (int64_t)float_value;
577     }
578     case ft_Double: {
579       return (int64_t)double_value;
580     }
581     case ft_Int64: {
582       return int64_value;
583     }
584     default:
585       return 0;
586     }
587   }
588
589
590 field_value& field_value::operator= (const field_value & fv) {
591   if ( this == &fv ) return *this;
592   
593   is_null = fv.get_isNull();
594
595   switch (fv.get_fType()) {
596     case ft_String: {
597       set_asString(fv.get_asString());
598       return *this;
599       break;
600     }
601     case ft_Boolean:{
602       set_asBool(fv.get_asBool());
603       return *this;
604       break;     
605     }
606     case ft_Char: {
607       set_asChar(fv.get_asChar());
608       return *this;
609       break;
610     }
611     case ft_Short: {
612       set_asShort(fv.get_asShort());
613       return *this;
614       break;
615     }
616     case ft_UShort: {
617       set_asUShort(fv.get_asUShort());
618       return *this;
619       break;
620     }
621     case ft_Int: {
622       set_asInt(fv.get_asInt());
623       return *this;
624       break;
625     }
626     case ft_UInt: {
627       set_asUInt(fv.get_asUInt());
628       return *this;
629       break;
630     }
631     case ft_Float: {
632       set_asFloat(fv.get_asFloat());
633       return *this;
634       break;
635     }
636     case ft_Double: {
637       set_asDouble(fv.get_asDouble());
638       return *this;
639       break;
640     }
641     case ft_Int64: {
642       set_asInt64(fv.get_asInt64());
643       return *this;
644       break;
645     }
646     default:
647       return *this;
648     }
649 }
650
651
652
653 //Set functions
654 void field_value::set_asString(const char *s) {
655   str_value = s;
656   field_type = ft_String;}
657
658 void field_value::set_asString(const string & s) {
659   str_value = s;
660   field_type = ft_String;}
661   
662 void field_value::set_asBool(const bool b) {
663   bool_value = b; 
664   field_type = ft_Boolean;}
665   
666 void field_value::set_asChar(const char c) {
667   char_value = c; 
668   field_type = ft_Char;}
669   
670 void field_value::set_asShort(const short s) {
671   short_value = s; 
672   field_type = ft_Short;}
673   
674 void field_value::set_asUShort(const unsigned short us) {
675   ushort_value = us; 
676   field_type = ft_UShort;
677 }
678
679 void field_value::set_asInt(const int i) {
680   int_value = i; 
681   field_type = ft_Int;
682 }
683   
684 void field_value::set_asUInt(const unsigned int ui) {
685   int_value = ui; 
686   field_type = ft_UInt;
687 }
688   
689 void field_value::set_asFloat(const float f) {
690   float_value = f; 
691   field_type = ft_Float;}
692   
693 void field_value::set_asDouble(const double d) {
694   double_value = d; 
695   field_type = ft_Double;}
696
697 void field_value::set_asInt64(const int64_t i) {
698   int64_value = i; 
699   field_type = ft_Int64;}
700   
701 fType field_value::get_field_type() {
702   return field_type;}
703
704   
705 string field_value::gft() {
706     string tmp;
707     switch (field_type) {
708     case ft_String: {
709       tmp.assign("string");
710       return tmp;
711     }
712     case ft_Boolean:{
713       tmp.assign("bool");
714       return tmp;
715     }
716     case ft_Char: {
717       tmp.assign("char");
718       return tmp;
719     }
720     case ft_Short: {
721       tmp.assign("short");
722       return tmp;
723     }
724     case ft_Int: {
725       tmp.assign("int");
726       return tmp;
727     }
728     case ft_Float: {
729       tmp.assign("float");
730       return tmp;
731     }
732     case ft_Double: {
733       tmp.assign("double");
734       return tmp;
735     }
736     case ft_Int64: {
737       tmp.assign("int64");
738       return tmp;
739     }
740     default:
741       break;
742     }
743
744   return tmp;
745   }
746
747 } //namespace