changes for open source mmdb build
[csql.git] / include / DataType.h
blobdbc4de5667fdb1579882a75e3c8523fd31495d84
1 /***************************************************************************
2 * *
3 * Copyright (C) Lakshya Solutions Ltd. All rights reserved. *
4 * *
5 ***************************************************************************/
7 #ifndef DATATYPE_H
8 #define DATATYPE_H
9 #include<sqlext.h>
10 #include<sqltypes.h>
11 #include<ErrorType.h>
12 #include<Config.h>
13 //#include<os.h>
14 typedef int JulianRep;
17 /**
18 * @class DataType
19 * Data Types supported by the database system.
20 * <br/>
23 enum DataType {
24 typeInt = 0, /**<integer type*/
25 typeLong = 1,
26 typeLongLong = 2,
27 typeShort = 3,
28 typeByteInt = 4,
30 typeDouble = 10,
31 typeFloat = 11,
32 typeDecimal = 12,
34 typeDate = 20,
35 typeTime = 21,
36 typeTimeStamp = 22,
38 typeString = 30,
39 typeBinary = 31,
41 typeComposite = 98,
42 typeULong = 99,
43 typeUnknown = 100
46 /**
47 * @class ComparisionOp
48 * Comparision operators supported by the database system.
49 * <br/>
52 enum ComparisionOp {
53 OpEquals = 0,
54 OpNotEquals,
55 OpLessThan,
56 OpLessThanEquals,
57 OpGreaterThan,
58 OpGreaterThanEquals,
59 OpLike, // for Like operator
60 OpIsNull,
61 OpInvalidComparisionOp
63 static char CompOpNames[][20] =
65 "Equals", "NotEquals", "LessThan", "LessThanEquals", "GreaterThan",
66 "GreaterThanEquals", "Like", "isNULL", "Invalid"
70 /**
71 * @class LogicalOp
72 * Logical operators supported by the database system.
73 * <br/>
76 enum LogicalOp {
77 OpAnd = 0,
78 OpOr,
79 OpNot,
80 OpInvalidLogicalOp
83 static char LogOpNames[][10] =
85 "AND", "OR", "NOT", "Invalid"
88 class AllDataType
90 public:
91 inline static long size(DataType type, int length =0);
92 static char* getSQLString(DataType type);
93 static SQLSMALLINT convertToSQLType(DataType type);
94 static SQLSMALLINT convertToCSQLSQLType(DataType type);
95 static SQLSMALLINT convertToSQL_C_Type(DataType type,TDBInfo tdbname=mysql);
96 static DataType convertFromSQLType(SQLSMALLINT type,int length=0, int scale=0, TDBInfo tdbname=mysql);
98 inline static void copyVal(void* dest, void *src, DataType type, int length = 0,int dbFlag=0);
99 inline static void copyZeroVal(void* dest, DataType type, int length = 0);
100 inline static void cachecopyVal(void* dest, void *src, DataType type, int length = 0,int dbFlag=0);
101 inline static void addVal(void* dest, void *src, DataType type);
102 static void divVal(double* dest, int src, DataType type);
103 static void subVal(void* dest, void *src, DataType type);
104 static void mulVal(void* dest, void *src, DataType type);
105 static void mudVal(void* dest, void *src, DataType type);
106 static void divVal(void* dest, void *src, DataType type);
107 static void increment(void* dest, void *src, DataType type);
108 static bool isValueZero(void* dest, DataType type);
110 inline static bool compareVal(void *src1, void *src2, ComparisionOp op,
111 DataType type, long length = 0);
112 static bool compareIntVal(void* src1, void* src2, ComparisionOp op);
113 static bool compareLongVal(void* src1, void* src2, ComparisionOp op);
114 static bool compareLongLongVal(void* src1, void* src2, ComparisionOp op);
115 static bool compareShortVal(void* src1, void* src2, ComparisionOp op);
116 static bool compareByteIntVal(void* src1, void* src2, ComparisionOp op);
117 static bool compareDoubleVal(void* src1, void* src2, ComparisionOp op);
118 static bool compareFloatVal(void* src1, void* src2, ComparisionOp op);
119 static bool compareDateVal(void* src1, void* src2, ComparisionOp op);
120 static bool compareTimeVal(void* src1, void* src2, ComparisionOp op);
121 static bool compareTimeStampVal(void* src1, void* src2, ComparisionOp op);
122 static bool compareStringVal(void* src1, void* src2, ComparisionOp op);
123 static bool compareBinaryVal(void* src1, void* src2,
124 ComparisionOp op, int length);
127 static void convert(DataType srcType, void *src, DataType destType, void *dest, int length=0);
128 static void convertToInt(void* dest, void* src, DataType srcType);
129 static void convertToLong(void* dest, void* src, DataType srcType);
130 static void convertToLongLong(void* dest, void* src, DataType srcType);
131 static void convertToShort(void* dest, void* src, DataType srcType);
132 static void convertToByteInt(void* dest, void* src, DataType srcType);
133 static void convertToFloat(void* dest, void* src, DataType srcType);
134 static void convertToDouble(void* dest, void* src, DataType srcType);
135 static void convertToString(void* dest, void* src, DataType srcType, int length=0,TDBInfo tdbname=mysql);
136 static void convertToDate(void* dest, void* src, DataType srcType);
137 static void convertToTime(void* dest, void* src, DataType srcType);
138 static void convertToTimeStamp(void* dest, void* src, DataType srcType);
139 static void convertToBinary(void* dest, void* src, DataType srcType, int length);
141 static void memoryset(void *value,DataType type);
142 inline static ComparisionOp getComparisionOperator(char *str);
144 static void* alloc(DataType type, int length =0);
145 static DbRetVal strToValue(void *dest, char *src, DataType type, int length=0);
146 static int printVal(void *src, DataType type, int length,int dbFlag=0);
147 inline static bool isValidFieldForAvg(DataType type);
152 * @class ByteInt
153 * Represents 8 bit integer.
154 * <br/>
157 class ByteInt {
159 public:
160 ByteInt() { }
162 /** copy constructor
164 ByteInt(const ByteInt &v) { val = v.val; }
165 /** constructor with char
166 * @param v char value
168 ByteInt(char v) { val = v; }
169 operator int() const { return (int) val; }
170 char operator=(ByteInt v) { return val = v.val; }
171 char operator=(char v) { return val = v; }
172 char operator+=(ByteInt v) { return val += v.val; }
173 char operator+=(char v) { return val += v; }
174 char operator-=(ByteInt v) { return val -= v.val; }
175 char operator-=(char v) { return val -= v; }
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++() { return val++; }
193 char operator++(int) { char tmp = val; val++; return val; }
194 char operator--() { return val--; }
195 char operator--(int) { char tmp = val; val--; return val; }
197 private:
198 signed char val;
203 * @class Date
204 * Represents Date Data type.
205 * <br/>
208 class Date { // The class a user would declare to hold date
210 public:
211 Date() {julianDate = 0;}
212 Date(JulianRep julian) : julianDate(julian) {}
214 /** constructor with year, month, day
215 * @param year year
216 * @param month month
217 * @param day day
219 Date(int year, int month, int day);
221 Date(const Date &d2) { julianDate = d2.julianDate; }
222 Date& operator=(const Date& d2)
223 { julianDate=d2.julianDate; return *this; }
225 /** sets the date with specified year, month, day
226 * @param year year
227 * @param month month
228 * @param day day
230 int set(int year, int month, int day);
231 int set(const struct tm *tmptr);
233 /** get year, month, day of the date
234 * @param year year IN
235 * @param month month IN
236 * @param day day IN
238 int get(int &year, int &month, int &day) const;
240 /** checks for the validity of the date
242 bool isValid() const;
244 /** resets the date to zero
246 void setNull() { julianDate = 0;}
248 /** returns day of the week
250 int dayOfWeek() const;
252 /** returns day of the week.
253 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday".
255 const char *dayOfWeekName() const;
257 /** returns day of the week abbreviation
258 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
260 const char *dayOfWeekAbbr() const;
262 /** returns day of the week.
263 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday".
265 static const char *dayOfWeekName(int day); // 0--> Sunday
267 /** returns day of the week abbreviation
268 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
270 static const char *dayOfWeekAbbr(int day);
272 static int dayOfWeek(JulianRep juldate);
275 /** returns the day of the month. Values are 1 to 31
277 int dayOfMonth() const;
279 int dayOfYear() const;
281 /** returns the month Values are 1 to 12.
283 int month() const;
285 /** returns the month name
286 * values are "January", "February", "March", "April", "May", "June",
287 * "July", "August", "September", "October", "November", "December"
289 const char *monthName() const;
291 /** returns the month name abbreviation
292 * values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
293 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
295 const char *monthAbbr() const;
297 /** returns the month name
298 * values are "January", "February", "March", "April", "May", "June",
299 * "July", "August", "September", "October", "November", "December"
301 static const char *monthName(int month);
304 /** returns the month name abbreviation
305 * values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
306 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
308 static const char *monthAbbr(int month);
310 /** parses the date string passed and stores it
311 *It should of the format "mm/dd/yyyy"
313 int parseFrom(const char *s);
315 static void changeToCsqlFormat(char *src);
318 Date &operator++() { julianDate++; return *this; }
319 Date &operator--() { julianDate--; return *this; }
321 Date &operator+=(int days) { julianDate += days; return *this;}
322 Date &operator-=(int days) { julianDate -= days; return *this;}
324 /** returns the year
326 int year() const;
328 /** checks for the validity of the date
330 static bool isValidDate(int year, int month, int day);
332 friend Date operator+(const Date &d1, int days);
333 friend Date operator+(int days, const Date &d1);
334 friend Date operator-(const Date &d1, int days);
335 friend int operator-(const Date &d1, const Date & d2);
336 friend int operator<(const Date &d1 ,const Date &d2);
337 friend int operator>(const Date &d1 ,const Date &d2);
338 friend int operator<=(const Date &d1 ,const Date &d2);
339 friend int operator>=(const Date &d1 ,const Date &d2);
340 friend int operator==(const Date &d1 ,const Date &d2);
341 friend int operator!=(const Date &d1 ,const Date &d2);
343 /** checks for leap year
345 static bool isLeapYear(int year);
347 /** returns the number of days in the specified month of the year.
349 static int daysInMonth(int month, int year);
351 static int YMDToJulian(int year,int mon,int day, JulianRep &julian);
352 static int julianToYMD(JulianRep julian,int &year,int &month,int &day);
354 private:
355 JulianRep julianDate;
362 * @class Time
363 * Represents Time Data type.
364 * <br/>
367 class Time { // The class a user would declare to hold time
368 public:
369 Time() {timeVal = 0;}
371 /** Overloaded constructor
372 * @param hours hours
373 * @param mins mins
374 * @param secs secs
375 * @param usec usec
377 Time(int hours, int mins, int secs, int usec = 0);
378 Time(int totalSecs) : timeVal(totalSecs) {;}
379 Time(const Time &d2) { timeVal = d2.timeVal; }
380 Time& operator=(const Time& d2) { timeVal=d2.timeVal; return *this; }
382 /** sets the time with specified hours, mins, secs
383 * @param hours hours
384 * @param mins mins
385 * @param secs secs
386 * @param usec usec
388 int set(int hours, int mins, int secs, int usec = 0);
390 /** retrieves the time using IN parameters
391 * @param hours hours
392 * @param mins mins
393 * @param secs secs
395 int get(int &hours, int &mins, int &secs) const;
397 /** checks for the validity of the time
399 bool isValid() const;
401 /** resets the time
403 void setNull() { timeVal = -1;}
405 int secondsSinceMidnight() const { return timeVal/10000;}
407 /** returns the microsecs
409 int usec() const; // to nearest 100 of usec.
411 /** returns the millisecs
413 int msec() const;
415 /** returns the secs
417 int seconds() const;
419 /** returns the minutes
421 int minutes() const;
423 /** returns the hours
425 int hours() const;
428 /** sets the millisecs
430 int setMsec(int ms);
432 /** sets the microsecs
434 int setUsec(int us);
436 /** parses the time string passed and stores it
437 *It should of the format "hh:mm::ss"
439 int parseFrom(const char *s);
441 Time &operator++() { timeVal += 10000; return *this; }
442 Time &operator--() { timeVal -= 10000; return *this; }
444 Time &operator+=(int seconds) { timeVal += seconds*10000; return *this; }
445 Time &operator-=(int seconds) { timeVal -= seconds*10000; return *this; }
448 /** checks for the validity of the time specified
450 static bool isValidTime(int hours, int mins, int secs);
452 friend Time operator+(const Time &t1, int seconds);
453 friend Time operator+(int seconds, const Time &t1);
454 friend Time operator-(const Time &t1, int seconds);
455 friend int operator-(const Time &t1, const Time& t2);
456 friend int operator<(const Time &t1 ,const Time &t2 );
457 friend int operator>(const Time &t1 ,const Time &t2 );
458 friend int operator<=(const Time &t1 ,const Time &t2 );
459 friend int operator>=(const Time &t1 ,const Time &t2 );
460 friend int operator==(const Time &t1 ,const Time &t2 );
461 friend int operator!=(const Time &t1 ,const Time &t2 );
464 private:
465 int timeVal;
469 * @class TimeStamp
470 * Represents TimeStamp Data type.
471 * <br/>
474 class TimeStamp {
476 public:
477 TimeStamp() {}
479 /** Overloaded constructor
480 * @param year year
481 * @param month month
482 * @param day day
483 * @param hours hours
484 * @param mins mins
485 * @param secs secs
486 * @param usec usec
488 TimeStamp(int year, int month, int day, int hour, int minute, int sec, int usec = 0) :
489 date(year, month, day), time(hour, minute, sec, usec) { }
491 TimeStamp(const TimeStamp &ts)
492 { date = ts.date; time = ts.time; }
493 TimeStamp(const Date &d, Time &t) : date(d), time(t) {}
496 TimeStamp& operator=(const TimeStamp& d2)
497 { date=d2.date; time = d2.time; return *this; }
499 /** get year, month, day from the date part of the timestamp
500 * @param year year IN
501 * @param month month IN
502 * @param day day IN
504 int getDate(int &year, int &month, int &day)
505 { return date.get(year, month, day); }
507 /** get the date part of the timestamp
508 * @param Date date
510 void getDate(Date &newDate) const
511 { newDate = date; }
513 /** sets the date with specified year, month, day
514 * @param year year
515 * @param month month
516 * @param day day
518 int setDate(int year, int month, int day)
519 { return date.set(year, month, day); }
521 /** set the date part of the timestamp
522 * @param Date date
524 void setDate(const Date &newDate)
525 { date = newDate; }
528 operator Date() { return date; }
529 operator Time() { return time; }
533 /** retrieves the time using IN parameters
534 * @param hours hours
535 * @param mins mins
536 * @param secs secs
538 int getTime(int &hours, int &mins, int &secs) const
539 { return time.get(hours, mins, secs); }
540 /** retrieves the time part of the timestamp
541 * @param newTime Time
543 void getTime(Time &newTime) const
544 { newTime = time; }
546 /** sets the time with specified hours, mins, secs
547 * @param hours hours
548 * @param mins mins
549 * @param secs secs
550 * @param usec usec
552 int setTime(int hours, int mins, int secs, int usec = 0)
553 { return time.set(hours, mins, secs, usec); }
555 /** set the time part of the timestamp
556 * @param newTime Time
558 void setTime(const Time &newTime)
559 { time = newTime; }
561 /** checks for the validity of the timestamp
563 bool isValid() const { return date.isValid() && time.isValid(); }
565 /** resets the date and time */
566 void setNull() { date.setNull(); time.setNull(); }
568 /** returns day of the week. Values are 1-7
570 int dayOfWeek() const { return date.dayOfWeek(); }
572 /** returns day of the week.
573 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Satur
574 day".
576 const char *dayOfWeekName() const { return date.dayOfWeekName(); }
578 /** returns day of the week abbreviation
579 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
581 const char *dayOfWeekAbbr() const { return date.dayOfWeekAbbr(); }
583 /** returns the day of the month. Values are 1 to 31
585 int dayOfMonth() const { return date.dayOfMonth(); }
586 int dayOfYear() const { return date.dayOfYear(); }
588 /** returns the month. Values are 1 to 12.
591 int month() const { return date.month(); }
593 /** returns the month name
594 * values are "January", "February", "March", "April", "May", "June",
595 * "July", "August", "September", "October", "November", "December"
597 const char *monthName() const { return date.monthName(); }
599 /** returns the month name abbreviation
600 * Values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
601 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
603 const char *monthAbbr() const { return date.monthAbbr(); }
605 /** returns the year
607 int year() const { return date.year(); }
609 int secondsSinceMidnight() const { return time.secondsSinceMidnight(); }
610 /** returns the seconds */
611 int seconds() const { return time.seconds(); }
612 /** returns the minutes */
613 int minutes() const { return time.minutes(); }
614 /** returns the hours */
615 int hours() const { return time.hours(); }
616 /** returns the millisecs */
617 int msec() const { return time.msec(); }
618 /** returns the microsecs */
619 int usec() const { return time.usec(); }
621 /** sets the millisecs */
622 int setMsec(int ms) { return time.setMsec(ms) ; }
623 /** sets the microsecs */
624 int setUsec(int us) { return time.setUsec(us) ; }
626 /** parses the date string passed and stores it
627 *It should of the format "mm/dd/yyyy"
629 int parseDateFrom(const char *s) { return date.parseFrom(s); }
631 /** parses the time string passed and stores it
632 *It should of the format "hh:mm::ss"
635 int parseTimeFrom(const char *s) { return time.parseFrom(s); }
637 static void changeToCsqlFormat(char *src);
639 int parseFrom(const char *s);
640 friend int operator<(const TimeStamp &d1, const TimeStamp &d2);
641 friend int operator>(const TimeStamp &d1, const TimeStamp &d2);
642 friend int operator<=(const TimeStamp &d1, const TimeStamp &d2);
643 friend int operator>=(const TimeStamp &d1, const TimeStamp &d2);
644 friend int operator==(const TimeStamp &d1, const TimeStamp &d2);
645 friend int operator!=(const TimeStamp &d1, const TimeStamp &d2);
648 private:
649 Date date;
650 Time time;
653 #include<DataTypeInline.h>
654 #endif