windows changes
[csql.git] / include / DataType.h
bloba217b76388faa8692bc050da41e5b168cb62b354
1 /***************************************************************************
2 * *
3 * Copyright (C) Lakshya Solutions Ltd. All rights reserved. *
4 * *
5 ***************************************************************************/
7 #ifndef DATATYPE_H
8 #define DATATYPE_H
10 #ifndef WINNT
11 //#include<sqlext.h>
12 //#include<sqltypes.h>
13 #endif
15 #include<ErrorType.h>
16 #include<Config.h>
17 #include<Function.h>
18 typedef int JulianRep;
21 /**
22 * @class DataType
23 * Data Types supported by the database system.
24 * <br/>
27 enum DataType {
28 typeInt = 0, /**<integer type*/
29 typeLong = 1,
30 typeLongLong = 2,
31 typeShort = 3,
32 typeByteInt = 4,
34 typeDouble = 10,
35 typeFloat = 11,
36 typeDecimal = 12,
38 typeDate = 20,
39 typeTime = 21,
40 typeTimeStamp = 22,
42 typeString = 30,
43 typeBinary = 31,
44 typeVarchar = 32,
46 typeComposite = 98,
47 typeULong = 99,
48 typeUnknown = 100
51 /**
52 * @class ComparisionOp
53 * Comparision operators supported by the database system.
54 * <br/>
57 enum ComparisionOp {
58 OpEquals = 0,
59 OpNotEquals,
60 OpLessThan,
61 OpLessThanEquals,
62 OpGreaterThan,
63 OpGreaterThanEquals,
64 OpLike, // for Like operator
65 OpIsNull,
66 OpInvalidComparisionOp
68 static char CompOpNames[][20] =
70 "Equals", "NotEquals", "LessThan", "LessThanEquals", "GreaterThan",
71 "GreaterThanEquals", "Like", "isNULL", "Invalid"
75 /**
76 * @class LogicalOp
77 * Logical operators supported by the database system.
78 * <br/>
81 enum LogicalOp {
82 OpAnd = 0,
83 OpOr,
84 OpNot,
85 OpInvalidLogicalOp
88 static char LogOpNames[][10] =
90 "AND", "OR", "NOT", "Invalid"
93 class DllExport AllDataType
95 public:
96 inline static long size(DataType type, int length =0);
97 static char* getSQLString(DataType type);
98 static short convertToSQLType(DataType type);
99 static short convertToCSQLSQLType(DataType type);
100 static short convertToSQL_C_Type(DataType type,TDBInfo tdbname=mysql);
101 static DataType convertFromSQLType(short type,int length=0, int scale=0, TDBInfo tdbname=mysql);
103 inline static void copyVal(void* dest, void *src, DataType type, int length = 0,int dbFlag=0);
104 inline static void copyZeroVal(void* dest, DataType type, int length = 0);
105 inline static void cachecopyVal(void* dest, void *src, DataType type, int length = 0,int dbFlag=0);
106 inline static void addVal(void* dest, void *src, DataType type);
107 static void divVal(double* dest, int src, DataType type);
108 static void subVal(void* dest, void *src, DataType type);
109 static void mulVal(void* dest, void *src, DataType type);
110 static void mudVal(void* dest, void *src, DataType type);
111 static void divVal(void* dest, void *src, DataType type);
112 static void increment(void* dest, void *src, DataType type);
113 static bool isValueZero(void* dest, DataType type);
115 inline static bool compareVal(void *src1, void *src2, ComparisionOp op,
116 DataType type, long length = 0);
117 static bool compareIntVal(void* src1, void* src2, ComparisionOp op);
118 static bool compareLongVal(void* src1, void* src2, ComparisionOp op);
119 static bool compareLongLongVal(void* src1, void* src2, ComparisionOp op);
120 static bool compareShortVal(void* src1, void* src2, ComparisionOp op);
121 static bool compareByteIntVal(void* src1, void* src2, ComparisionOp op);
122 static bool compareDoubleVal(void* src1, void* src2, ComparisionOp op);
123 static bool compareFloatVal(void* src1, void* src2, ComparisionOp op);
124 static bool compareDateVal(void* src1, void* src2, ComparisionOp op);
125 static bool compareTimeVal(void* src1, void* src2, ComparisionOp op);
126 static bool compareTimeStampVal(void* src1, void* src2, ComparisionOp op);
127 static bool compareStringVal(void* src1, void* src2, ComparisionOp op);
128 static bool compareBinaryVal(void* src1, void* src2,
129 ComparisionOp op, int length);
132 static void convert(DataType srcType, void *src, DataType destType, void *dest, int length=0);
133 static void convertToInt(void* dest, void* src, DataType srcType);
134 static void convertToLong(void* dest, void* src, DataType srcType);
135 static void convertToLongLong(void* dest, void* src, DataType srcType);
136 static void convertToShort(void* dest, void* src, DataType srcType);
137 static void convertToByteInt(void* dest, void* src, DataType srcType);
138 static void convertToFloat(void* dest, void* src, DataType srcType);
139 static void convertToDouble(void* dest, void* src, DataType srcType);
140 static void convertToString(void* dest, void* src, DataType srcType, int length=0,TDBInfo tdbname=mysql);
141 static void convertToDate(void* dest, void* src, DataType srcType);
142 static void convertToTime(void* dest, void* src, DataType srcType);
143 static void convertToTimeStamp(void* dest, void* src, DataType srcType);
144 static void convertToBinary(void* dest, void* src, DataType srcType, int length);
145 static DataType getCsqlTypeFromFunctionType(FunctionType type);
146 static DataType getCsqlTypeFromFunctionTypeForComparision(FunctionType type);
147 static void memoryset(void *value,DataType type);
148 inline static ComparisionOp getComparisionOperator(char *str);
150 static void* alloc(DataType type, int length =0);
151 static DbRetVal strToValue(void *dest, char *src, DataType type, int length=0);
152 static int printVal(void *src, DataType type, int length,int dbFlag=0);
153 inline static bool isValidFieldForAvg(DataType type);
158 * @class ByteInt
159 * Represents 8 bit integer.
160 * <br/>
163 class DllExport ByteInt {
165 public:
166 ByteInt() { }
168 /** copy constructor
170 ByteInt(const ByteInt &v) { val = v.val; }
171 /** constructor with char
172 * @param v char value
174 ByteInt(char v) { val = v; }
175 operator int() const { return (int) val; }
176 char operator=(ByteInt v) { return val = v.val; }
177 char operator=(char v) { return val = v; }
178 char operator+=(ByteInt v) { return val += v.val; }
179 char operator+=(char v) { return val += v; }
180 char operator-=(ByteInt v) { return val -= v.val; }
181 char operator-=(char v) { return val -= v; }
182 char operator*=(ByteInt v) { return val *= v.val; }
183 char operator*=(char v) { return val *= v; }
184 char operator/=(ByteInt v) { return val /= v.val; }
185 char operator/=(char v) { return val /= v; }
186 char operator%=(ByteInt v) { return val %= v.val; }
187 char operator%=(char v) { return val %= v; }
188 char operator<<=(ByteInt v) { return val <<= v.val; }
189 char operator<<=(char v) { return val <<= v; }
190 char operator>>=(ByteInt v) { return val >>= v.val; }
191 char operator>>=(char v) { return val >>= v; }
192 char operator&=(ByteInt v) { return val &= v.val; }
193 char operator&=(char v) { return val &= v; }
194 char operator|=(ByteInt v) { return val |= v.val; }
195 char operator|=(char v) { return val |= v; }
196 char operator^=(ByteInt v) { return val ^= v.val; }
197 char operator^=(char v) { return val ^= v; }
198 char operator++() { return val++; }
199 char operator++(int) { char tmp = val; val++; return val; }
200 char operator--() { return val--; }
201 char operator--(int) { char tmp = val; val--; return val; }
203 private:
204 signed char val;
209 * @class Date
210 * Represents Date Data type.
211 * <br/>
214 class DllExport Date { // The class a user would declare to hold date
216 public:
217 Date() {julianDate = 0;}
218 Date(JulianRep julian) : julianDate(julian) {}
220 /** constructor with year, month, day
221 * @param year year
222 * @param month month
223 * @param day day
225 Date(int year, int month, int day);
227 Date(const Date &d2) { julianDate = d2.julianDate; }
228 Date& operator=(const Date& d2)
229 { julianDate=d2.julianDate; return *this; }
231 /** sets the date with specified year, month, day
232 * @param year year
233 * @param month month
234 * @param day day
236 int set(int year, int month, int day);
237 int set(const struct tm *tmptr);
239 /** get year, month, day of the date
240 * @param year year IN
241 * @param month month IN
242 * @param day day IN
244 int get(int &year, int &month, int &day) const;
246 /** checks for the validity of the date
248 bool isValid() const;
250 /** resets the date to zero
252 void setNull() { julianDate = 0;}
254 /** returns day of the week
256 int dayOfWeek() const;
258 /** returns day of the week.
259 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday".
261 const char *dayOfWeekName() const;
263 /** returns day of the week abbreviation
264 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
266 const char *dayOfWeekAbbr() const;
268 /** returns day of the week.
269 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday".
271 static const char *dayOfWeekName(int day); // 0--> Sunday
273 /** returns day of the week abbreviation
274 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
276 static const char *dayOfWeekAbbr(int day);
278 static int dayOfWeek(JulianRep juldate);
281 void addDay(int noOfDays);
282 void subDay(int noOfDays);
283 void addMonth(int noOfMons);
284 void subMonth(int noOfMons);
285 void addYear(int noOfYrs);
286 void subYear(int noOfYrs);
288 /** returns the day of the month. Values are 1 to 31
290 int dayOfMonth() const;
292 int dayOfYear() const;
294 /** returns the month Values are 1 to 12.
296 int month() const;
298 /** returns the month name
299 * values are "January", "February", "March", "April", "May", "June",
300 * "July", "August", "September", "October", "November", "December"
302 const char *monthName() const;
304 /** returns the month name abbreviation
305 * values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
306 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
308 const char *monthAbbr() const;
310 /** returns the month name
311 * values are "January", "February", "March", "April", "May", "June",
312 * "July", "August", "September", "October", "November", "December"
314 static const char *monthName(int month);
317 /** returns the month name abbreviation
318 * values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
319 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
321 static const char *monthAbbr(int month);
323 /** parses the date string passed and stores it
324 *It should of the format "mm/dd/yyyy"
326 int parseFrom(const char *s);
328 int getCalDay() { return julianDate;}
329 static void changeToCsqlFormat(char *src);
332 Date &operator++() { julianDate++; return *this; }
333 Date &operator--() { julianDate--; return *this; }
335 Date &operator+=(int days) { julianDate += days; return *this;}
336 Date &operator-=(int days) { julianDate -= days; return *this;}
338 /** returns the year
340 int year() const;
342 /** checks for the validity of the date
344 static bool isValidDate(int year, int month, int day);
346 friend Date operator+(const Date &d1, int days);
347 friend Date operator+(int days, const Date &d1);
348 friend Date operator-(const Date &d1, int days);
349 friend int operator-(const Date &d1, const Date & d2);
350 friend int operator<(const Date &d1 ,const Date &d2);
351 friend int operator>(const Date &d1 ,const Date &d2);
352 friend int operator<=(const Date &d1 ,const Date &d2);
353 friend int operator>=(const Date &d1 ,const Date &d2);
354 friend int operator==(const Date &d1 ,const Date &d2);
355 friend int operator!=(const Date &d1 ,const Date &d2);
357 /** checks for leap year
359 static bool isLeapYear(int year);
361 /** returns the number of days in the specified month of the year.
363 static int daysInMonth(int month, int year);
365 static int YMDToJulian(int year,int mon,int day, JulianRep &julian);
366 static int julianToYMD(JulianRep julian,int &year,int &month,int &day);
368 private:
369 JulianRep julianDate;
376 * @class Time
377 * Represents Time Data type.
378 * <br/>
381 class DllExport Time { // The class a user would declare to hold time
382 public:
383 Time() {timeVal = 0;}
385 /** Overloaded constructor
386 * @param hours hours
387 * @param mins mins
388 * @param secs secs
389 * @param usec usec
391 Time(int hours, int mins, int secs, int usec = 0);
392 Time(int totalSecs) : timeVal(totalSecs) {;}
393 Time(const Time &d2) { timeVal = d2.timeVal; }
394 Time& operator=(const Time& d2) { timeVal=d2.timeVal; return *this; }
396 /** sets the time with specified hours, mins, secs
397 * @param hours hours
398 * @param mins mins
399 * @param secs secs
400 * @param usec usec
402 int set(int hours, int mins, int secs, int usec = 0);
404 /** retrieves the time using IN parameters
405 * @param hours hours
406 * @param mins mins
407 * @param secs secs
409 int get(int &hours, int &mins, int &secs) const;
414 int getCalSec(){ return timeVal/10000; }
415 /** checks for the validity of the time
417 bool isValid() const;
419 /** resets the time
421 void setNull() { timeVal = -1;}
423 int secondsSinceMidnight() const { return timeVal/10000;}
425 /** returns the microsecs
427 int usec() const; // to nearest 100 of usec.
429 /** returns the millisecs
431 int msec() const;
433 /** returns the secs
435 int seconds() const;
437 /** returns the minutes
439 int minutes() const;
441 /** returns the hours
443 int hours() const;
446 int addSec(int secs);
447 int subSec(int secs);
448 int addMin(int mins);
449 int subMin(int mins);
450 int addHour(int hours);
451 int subHour(int hours);
453 /** sets the millisecs
455 int setMsec(int ms);
457 /** sets the microsecs
459 int setUsec(int us);
461 /** parses the time string passed and stores it
462 *It should of the format "hh:mm::ss"
464 int parseFrom(const char *s);
466 Time &operator++() { timeVal += 10000; return *this; }
467 Time &operator--() { timeVal -= 10000; return *this; }
469 Time &operator+=(int seconds) { timeVal += seconds*10000; return *this; }
470 Time &operator-=(int seconds) { timeVal -= seconds*10000; return *this; }
473 /** checks for the validity of the time specified
475 static bool isValidTime(int hours, int mins, int secs);
477 friend Time operator+(const Time &t1, int seconds);
478 friend Time operator+(int seconds, const Time &t1);
479 friend Time operator-(const Time &t1, int seconds);
480 friend int operator-(const Time &t1, const Time& t2);
481 friend int operator<(const Time &t1 ,const Time &t2 );
482 friend int operator>(const Time &t1 ,const Time &t2 );
483 friend int operator<=(const Time &t1 ,const Time &t2 );
484 friend int operator>=(const Time &t1 ,const Time &t2 );
485 friend int operator==(const Time &t1 ,const Time &t2 );
486 friend int operator!=(const Time &t1 ,const Time &t2 );
489 private:
490 int timeVal;
494 * @class TimeStamp
495 * Represents TimeStamp Data type.
496 * <br/>
499 class DllExport TimeStamp {
501 public:
502 TimeStamp() {}
504 /** Overloaded constructor
505 * @param year year
506 * @param month month
507 * @param day day
508 * @param hours hours
509 * @param mins mins
510 * @param secs secs
511 * @param usec usec
513 TimeStamp(int year, int month, int day, int hour, int minute, int sec, int usec = 0) :
514 date(year, month, day), time(hour, minute, sec, usec) { }
516 TimeStamp(const TimeStamp &ts)
517 { date = ts.date; time = ts.time; }
518 TimeStamp(const Date &d, Time &t) : date(d), time(t) {}
521 TimeStamp& operator=(const TimeStamp& d2)
522 { date=d2.date; time = d2.time; return *this; }
524 /** get year, month, day from the date part of the timestamp
525 * @param year year IN
526 * @param month month IN
527 * @param day day IN
529 int getDate(int &year, int &month, int &day)
530 { return date.get(year, month, day); }
532 /** get the date part of the timestamp
533 * @param Date date
535 void getDate(Date &newDate) const
536 { newDate = date; }
538 /** sets the date with specified year, month, day
539 * @param year year
540 * @param month month
541 * @param day day
543 int setDate(int year, int month, int day)
544 { return date.set(year, month, day); }
546 /** set the date part of the timestamp
547 * @param Date date
549 void setDate(const Date &newDate)
550 { date = newDate; }
553 operator Date() { return date; }
554 operator Time() { return time; }
558 /** retrieves the time using IN parameters
559 * @param hours hours
560 * @param mins mins
561 * @param secs secs
563 int getTime(int &hours, int &mins, int &secs) const
564 { return time.get(hours, mins, secs); }
565 /** retrieves the time part of the timestamp
566 * @param newTime Time
568 void getTime(Time &newTime) const
569 { newTime = time; }
571 /** sets the time with specified hours, mins, secs
572 * @param hours hours
573 * @param mins mins
574 * @param secs secs
575 * @param usec usec
577 int setTime(int hours, int mins, int secs, int usec = 0)
578 { return time.set(hours, mins, secs, usec); }
580 /** set the time part of the timestamp
581 * @param newTime Time
583 void setTime(const Time &newTime)
584 { time = newTime; }
586 /** checks for the validity of the timestamp
588 bool isValid() const { return date.isValid() && time.isValid(); }
590 /** resets the date and time */
591 void setNull() { date.setNull(); time.setNull(); }
593 /** returns day of the week. Values are 1-7
595 int dayOfWeek() const { return date.dayOfWeek(); }
597 /** returns day of the week.
598 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Satur
599 day".
601 const char *dayOfWeekName() const { return date.dayOfWeekName(); }
603 /** returns day of the week abbreviation
604 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
606 const char *dayOfWeekAbbr() const { return date.dayOfWeekAbbr(); }
608 /** returns the day of the month. Values are 1 to 31
610 int dayOfMonth() const { return date.dayOfMonth(); }
611 int dayOfYear() const { return date.dayOfYear(); }
613 /** returns the month. Values are 1 to 12.
616 int month() const { return date.month(); }
618 /** returns the month name
619 * values are "January", "February", "March", "April", "May", "June",
620 * "July", "August", "September", "October", "November", "December"
622 const char *monthName() const { return date.monthName(); }
624 /** returns the month name abbreviation
625 * Values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
626 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
628 const char *monthAbbr() const { return date.monthAbbr(); }
630 /** returns the year
632 int year() const { return date.year(); }
634 int secondsSinceMidnight() const { return time.secondsSinceMidnight(); }
635 /** returns the seconds */
636 int seconds() const { return time.seconds(); }
637 /** returns the minutes */
638 int minutes() const { return time.minutes(); }
639 /** returns the hours */
640 int hours() const { return time.hours(); }
641 /** returns the millisecs */
642 int msec() const { return time.msec(); }
643 /** returns the microsecs */
644 int usec() const { return time.usec(); }
646 /** sets the millisecs */
647 int setMsec(int ms) { return time.setMsec(ms) ; }
648 /** sets the microsecs */
649 int setUsec(int us) { return time.setUsec(us) ; }
651 /** parses the date string passed and stores it
652 *It should of the format "mm/dd/yyyy"
654 int parseDateFrom(const char *s) { return date.parseFrom(s); }
656 /** parses the time string passed and stores it
657 *It should of the format "hh:mm::ss"
660 int parseTimeFrom(const char *s) { return time.parseFrom(s); }
662 static void changeToCsqlFormat(char *src);
664 int parseFrom(const char *s);
665 friend int operator<(const TimeStamp &d1, const TimeStamp &d2);
666 friend int operator>(const TimeStamp &d1, const TimeStamp &d2);
667 friend int operator<=(const TimeStamp &d1, const TimeStamp &d2);
668 friend int operator>=(const TimeStamp &d1, const TimeStamp &d2);
669 friend int operator==(const TimeStamp &d1, const TimeStamp &d2);
670 friend int operator!=(const TimeStamp &d1, const TimeStamp &d2);
671 void addYear(int noOfYrs ){ date.addYear(noOfYrs);}
673 void subYear(int noOfYrs ) { date.subYear(noOfYrs); }
675 void addMonth(int noOfMons ) { date.addMonth(noOfMons);}
677 void subMonth(int noOfMons) { date.subMonth(noOfMons);}
679 void addDay(int noOfDays) { date.addDay(noOfDays);}
681 void subDay(int noOfDays) { date.subDay(noOfDays); }
683 void addHour(int hours);
685 void subHour(int hours);
687 void addMin(int noOfMins);
689 void subMin(int noOfMins);
691 void addSec(int noOfSecs);
693 void subSec(int noOfSecs);
695 int secDiff(TimeStamp &ts);
696 int minDiff(TimeStamp &ts);
697 int hourDiff(TimeStamp &ts);
698 int dayDiff(TimeStamp &ts);
699 int monthDiff(TimeStamp &ts);
700 int yearDiff(TimeStamp &ts);
702 private:
703 Date date;
704 Time time;
707 #include<DataTypeInline.h>
708 #endif