allocator fixes
[csql.git] / include / DataType.h
blob9bb9177dd082f8875da43721c76c32de88fb96ab
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<Function.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,
40 typeVarchar = 32,
42 typeComposite = 98,
43 typeULong = 99,
44 typeUnknown = 100
47 /**
48 * @class ComparisionOp
49 * Comparision operators supported by the database system.
50 * <br/>
53 enum ComparisionOp {
54 OpEquals = 0,
55 OpNotEquals,
56 OpLessThan,
57 OpLessThanEquals,
58 OpGreaterThan,
59 OpGreaterThanEquals,
60 OpLike, // for Like operator
61 OpIsNull,
62 OpInvalidComparisionOp
64 static char CompOpNames[][20] =
66 "Equals", "NotEquals", "LessThan", "LessThanEquals", "GreaterThan",
67 "GreaterThanEquals", "Like", "isNULL", "Invalid"
71 /**
72 * @class LogicalOp
73 * Logical operators supported by the database system.
74 * <br/>
77 enum LogicalOp {
78 OpAnd = 0,
79 OpOr,
80 OpNot,
81 OpInvalidLogicalOp
84 static char LogOpNames[][10] =
86 "AND", "OR", "NOT", "Invalid"
89 class AllDataType
91 public:
92 inline static long size(DataType type, int length =0);
93 static char* getSQLString(DataType type);
94 static SQLSMALLINT convertToSQLType(DataType type);
95 static SQLSMALLINT convertToCSQLSQLType(DataType type);
96 static SQLSMALLINT convertToSQL_C_Type(DataType type,TDBInfo tdbname=mysql);
97 static DataType convertFromSQLType(SQLSMALLINT type,int length=0, int scale=0, TDBInfo tdbname=mysql);
99 inline static void copyVal(void* dest, void *src, DataType type, int length = 0,int dbFlag=0);
100 inline static void copyZeroVal(void* dest, DataType type, int length = 0);
101 inline static void cachecopyVal(void* dest, void *src, DataType type, int length = 0,int dbFlag=0);
102 inline static void addVal(void* dest, void *src, DataType type);
103 static void divVal(double* dest, int src, DataType type);
104 static void subVal(void* dest, void *src, DataType type);
105 static void mulVal(void* dest, void *src, DataType type);
106 static void mudVal(void* dest, void *src, DataType type);
107 static void divVal(void* dest, void *src, DataType type);
108 static void increment(void* dest, void *src, DataType type);
109 static bool isValueZero(void* dest, DataType type);
111 inline static bool compareVal(void *src1, void *src2, ComparisionOp op,
112 DataType type, long length = 0);
113 static bool compareIntVal(void* src1, void* src2, ComparisionOp op);
114 static bool compareLongVal(void* src1, void* src2, ComparisionOp op);
115 static bool compareLongLongVal(void* src1, void* src2, ComparisionOp op);
116 static bool compareShortVal(void* src1, void* src2, ComparisionOp op);
117 static bool compareByteIntVal(void* src1, void* src2, ComparisionOp op);
118 static bool compareDoubleVal(void* src1, void* src2, ComparisionOp op);
119 static bool compareFloatVal(void* src1, void* src2, ComparisionOp op);
120 static bool compareDateVal(void* src1, void* src2, ComparisionOp op);
121 static bool compareTimeVal(void* src1, void* src2, ComparisionOp op);
122 static bool compareTimeStampVal(void* src1, void* src2, ComparisionOp op);
123 static bool compareStringVal(void* src1, void* src2, ComparisionOp op);
124 static bool compareBinaryVal(void* src1, void* src2,
125 ComparisionOp op, int length);
128 static void convert(DataType srcType, void *src, DataType destType, void *dest, int length=0);
129 static void convertToInt(void* dest, void* src, DataType srcType);
130 static void convertToLong(void* dest, void* src, DataType srcType);
131 static void convertToLongLong(void* dest, void* src, DataType srcType);
132 static void convertToShort(void* dest, void* src, DataType srcType);
133 static void convertToByteInt(void* dest, void* src, DataType srcType);
134 static void convertToFloat(void* dest, void* src, DataType srcType);
135 static void convertToDouble(void* dest, void* src, DataType srcType);
136 static void convertToString(void* dest, void* src, DataType srcType, int length=0,TDBInfo tdbname=mysql);
137 static void convertToDate(void* dest, void* src, DataType srcType);
138 static void convertToTime(void* dest, void* src, DataType srcType);
139 static void convertToTimeStamp(void* dest, void* src, DataType srcType);
140 static void convertToBinary(void* dest, void* src, DataType srcType, int length);
141 static DataType getCsqlTypeFromFunctionType(FunctionType type);
142 static DataType getCsqlTypeFromFunctionTypeForComparision(FunctionType type);
143 static void memoryset(void *value,DataType type);
144 inline static ComparisionOp getComparisionOperator(char *str);
146 static void* alloc(DataType type, int length =0);
147 static DbRetVal strToValue(void *dest, char *src, DataType type, int length=0);
148 static int printVal(void *src, DataType type, int length,int dbFlag=0);
149 inline static bool isValidFieldForAvg(DataType type);
154 * @class ByteInt
155 * Represents 8 bit integer.
156 * <br/>
159 class ByteInt {
161 public:
162 ByteInt() { }
164 /** copy constructor
166 ByteInt(const ByteInt &v) { val = v.val; }
167 /** constructor with char
168 * @param v char value
170 ByteInt(char v) { val = v; }
171 operator int() const { return (int) val; }
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^=(ByteInt v) { return val ^= v.val; }
193 char operator^=(char v) { return val ^= v; }
194 char operator++() { return val++; }
195 char operator++(int) { char tmp = val; val++; return val; }
196 char operator--() { return val--; }
197 char operator--(int) { char tmp = val; val--; return val; }
199 private:
200 signed char val;
205 * @class Date
206 * Represents Date Data type.
207 * <br/>
210 class Date { // The class a user would declare to hold date
212 public:
213 Date() {julianDate = 0;}
214 Date(JulianRep julian) : julianDate(julian) {}
216 /** constructor with year, month, day
217 * @param year year
218 * @param month month
219 * @param day day
221 Date(int year, int month, int day);
223 Date(const Date &d2) { julianDate = d2.julianDate; }
224 Date& operator=(const Date& d2)
225 { julianDate=d2.julianDate; return *this; }
227 /** sets the date with specified year, month, day
228 * @param year year
229 * @param month month
230 * @param day day
232 int set(int year, int month, int day);
233 int set(const struct tm *tmptr);
235 /** get year, month, day of the date
236 * @param year year IN
237 * @param month month IN
238 * @param day day IN
240 int get(int &year, int &month, int &day) const;
242 /** checks for the validity of the date
244 bool isValid() const;
246 /** resets the date to zero
248 void setNull() { julianDate = 0;}
250 /** returns day of the week
252 int dayOfWeek() const;
254 /** returns day of the week.
255 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday".
257 const char *dayOfWeekName() const;
259 /** returns day of the week abbreviation
260 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
262 const char *dayOfWeekAbbr() const;
264 /** returns day of the week.
265 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday".
267 static const char *dayOfWeekName(int day); // 0--> Sunday
269 /** returns day of the week abbreviation
270 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
272 static const char *dayOfWeekAbbr(int day);
274 static int dayOfWeek(JulianRep juldate);
277 void addDay(int noOfDays);
278 void subDay(int noOfDays);
279 void addMonth(int noOfMons);
280 void subMonth(int noOfMons);
281 void addYear(int noOfYrs);
282 void subYear(int noOfYrs);
284 /** returns the day of the month. Values are 1 to 31
286 int dayOfMonth() const;
288 int dayOfYear() const;
290 /** returns the month Values are 1 to 12.
292 int month() const;
294 /** returns the month name
295 * values are "January", "February", "March", "April", "May", "June",
296 * "July", "August", "September", "October", "November", "December"
298 const char *monthName() const;
300 /** returns the month name abbreviation
301 * values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
302 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
304 const char *monthAbbr() const;
306 /** returns the month name
307 * values are "January", "February", "March", "April", "May", "June",
308 * "July", "August", "September", "October", "November", "December"
310 static const char *monthName(int month);
313 /** returns the month name abbreviation
314 * values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
315 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
317 static const char *monthAbbr(int month);
319 /** parses the date string passed and stores it
320 *It should of the format "mm/dd/yyyy"
322 int parseFrom(const char *s);
324 int getCalDay() { return julianDate;}
325 static void changeToCsqlFormat(char *src);
328 Date &operator++() { julianDate++; return *this; }
329 Date &operator--() { julianDate--; return *this; }
331 Date &operator+=(int days) { julianDate += days; return *this;}
332 Date &operator-=(int days) { julianDate -= days; return *this;}
334 /** returns the year
336 int year() const;
338 /** checks for the validity of the date
340 static bool isValidDate(int year, int month, int day);
342 friend Date operator+(const Date &d1, int days);
343 friend Date operator+(int days, const Date &d1);
344 friend Date operator-(const Date &d1, int days);
345 friend int operator-(const Date &d1, const Date & d2);
346 friend int operator<(const Date &d1 ,const Date &d2);
347 friend int operator>(const Date &d1 ,const Date &d2);
348 friend int operator<=(const Date &d1 ,const Date &d2);
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);
353 /** checks for leap year
355 static bool isLeapYear(int year);
357 /** returns the number of days in the specified month of the year.
359 static int daysInMonth(int month, int year);
361 static int YMDToJulian(int year,int mon,int day, JulianRep &julian);
362 static int julianToYMD(JulianRep julian,int &year,int &month,int &day);
364 private:
365 JulianRep julianDate;
372 * @class Time
373 * Represents Time Data type.
374 * <br/>
377 class Time { // The class a user would declare to hold time
378 public:
379 Time() {timeVal = 0;}
381 /** Overloaded constructor
382 * @param hours hours
383 * @param mins mins
384 * @param secs secs
385 * @param usec usec
387 Time(int hours, int mins, int secs, int usec = 0);
388 Time(int totalSecs) : timeVal(totalSecs) {;}
389 Time(const Time &d2) { timeVal = d2.timeVal; }
390 Time& operator=(const Time& d2) { timeVal=d2.timeVal; return *this; }
392 /** sets the time with specified hours, mins, secs
393 * @param hours hours
394 * @param mins mins
395 * @param secs secs
396 * @param usec usec
398 int set(int hours, int mins, int secs, int usec = 0);
400 /** retrieves the time using IN parameters
401 * @param hours hours
402 * @param mins mins
403 * @param secs secs
405 int get(int &hours, int &mins, int &secs) const;
410 int getCalSec(){ return timeVal/1000; }
411 /** checks for the validity of the time
413 bool isValid() const;
415 /** resets the time
417 void setNull() { timeVal = -1;}
419 int secondsSinceMidnight() const { return timeVal/10000;}
421 /** returns the microsecs
423 int usec() const; // to nearest 100 of usec.
425 /** returns the millisecs
427 int msec() const;
429 /** returns the secs
431 int seconds() const;
433 /** returns the minutes
435 int minutes() const;
437 /** returns the hours
439 int hours() const;
442 int addSec(int secs);
443 int subSec(int secs);
444 int addMin(int mins);
445 int subMin(int mins);
446 int addHour(int hours);
447 int subHour(int hours);
449 /** sets the millisecs
451 int setMsec(int ms);
453 /** sets the microsecs
455 int setUsec(int us);
457 /** parses the time string passed and stores it
458 *It should of the format "hh:mm::ss"
460 int parseFrom(const char *s);
462 Time &operator++() { timeVal += 10000; return *this; }
463 Time &operator--() { timeVal -= 10000; return *this; }
465 Time &operator+=(int seconds) { timeVal += seconds*10000; return *this; }
466 Time &operator-=(int seconds) { timeVal -= seconds*10000; return *this; }
469 /** checks for the validity of the time specified
471 static bool isValidTime(int hours, int mins, int secs);
473 friend Time operator+(const Time &t1, int seconds);
474 friend Time operator+(int seconds, const Time &t1);
475 friend Time operator-(const Time &t1, int seconds);
476 friend int operator-(const Time &t1, const Time& t2);
477 friend int operator<(const Time &t1 ,const Time &t2 );
478 friend int operator>(const Time &t1 ,const Time &t2 );
479 friend int operator<=(const Time &t1 ,const Time &t2 );
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 );
485 private:
486 int timeVal;
490 * @class TimeStamp
491 * Represents TimeStamp Data type.
492 * <br/>
495 class TimeStamp {
497 public:
498 TimeStamp() {}
500 /** Overloaded constructor
501 * @param year year
502 * @param month month
503 * @param day day
504 * @param hours hours
505 * @param mins mins
506 * @param secs secs
507 * @param usec usec
509 TimeStamp(int year, int month, int day, int hour, int minute, int sec, int usec = 0) :
510 date(year, month, day), time(hour, minute, sec, usec) { }
512 TimeStamp(const TimeStamp &ts)
513 { date = ts.date; time = ts.time; }
514 TimeStamp(const Date &d, Time &t) : date(d), time(t) {}
517 TimeStamp& operator=(const TimeStamp& d2)
518 { date=d2.date; time = d2.time; return *this; }
520 /** get year, month, day from the date part of the timestamp
521 * @param year year IN
522 * @param month month IN
523 * @param day day IN
525 int getDate(int &year, int &month, int &day)
526 { return date.get(year, month, day); }
528 /** get the date part of the timestamp
529 * @param Date date
531 void getDate(Date &newDate) const
532 { newDate = date; }
534 /** sets the date with specified year, month, day
535 * @param year year
536 * @param month month
537 * @param day day
539 int setDate(int year, int month, int day)
540 { return date.set(year, month, day); }
542 /** set the date part of the timestamp
543 * @param Date date
545 void setDate(const Date &newDate)
546 { date = newDate; }
549 operator Date() { return date; }
550 operator Time() { return time; }
554 /** retrieves the time using IN parameters
555 * @param hours hours
556 * @param mins mins
557 * @param secs secs
559 int getTime(int &hours, int &mins, int &secs) const
560 { return time.get(hours, mins, secs); }
561 /** retrieves the time part of the timestamp
562 * @param newTime Time
564 void getTime(Time &newTime) const
565 { newTime = time; }
567 /** sets the time with specified hours, mins, secs
568 * @param hours hours
569 * @param mins mins
570 * @param secs secs
571 * @param usec usec
573 int setTime(int hours, int mins, int secs, int usec = 0)
574 { return time.set(hours, mins, secs, usec); }
576 /** set the time part of the timestamp
577 * @param newTime Time
579 void setTime(const Time &newTime)
580 { time = newTime; }
582 /** checks for the validity of the timestamp
584 bool isValid() const { return date.isValid() && time.isValid(); }
586 /** resets the date and time */
587 void setNull() { date.setNull(); time.setNull(); }
589 /** returns day of the week. Values are 1-7
591 int dayOfWeek() const { return date.dayOfWeek(); }
593 /** returns day of the week.
594 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Satur
595 day".
597 const char *dayOfWeekName() const { return date.dayOfWeekName(); }
599 /** returns day of the week abbreviation
600 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
602 const char *dayOfWeekAbbr() const { return date.dayOfWeekAbbr(); }
604 /** returns the day of the month. Values are 1 to 31
606 int dayOfMonth() const { return date.dayOfMonth(); }
607 int dayOfYear() const { return date.dayOfYear(); }
609 /** returns the month. Values are 1 to 12.
612 int month() const { return date.month(); }
614 /** returns the month name
615 * values are "January", "February", "March", "April", "May", "June",
616 * "July", "August", "September", "October", "November", "December"
618 const char *monthName() const { return date.monthName(); }
620 /** returns the month name abbreviation
621 * Values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
622 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
624 const char *monthAbbr() const { return date.monthAbbr(); }
626 /** returns the year
628 int year() const { return date.year(); }
630 int secondsSinceMidnight() const { return time.secondsSinceMidnight(); }
631 /** returns the seconds */
632 int seconds() const { return time.seconds(); }
633 /** returns the minutes */
634 int minutes() const { return time.minutes(); }
635 /** returns the hours */
636 int hours() const { return time.hours(); }
637 /** returns the millisecs */
638 int msec() const { return time.msec(); }
639 /** returns the microsecs */
640 int usec() const { return time.usec(); }
642 /** sets the millisecs */
643 int setMsec(int ms) { return time.setMsec(ms) ; }
644 /** sets the microsecs */
645 int setUsec(int us) { return time.setUsec(us) ; }
647 /** parses the date string passed and stores it
648 *It should of the format "mm/dd/yyyy"
650 int parseDateFrom(const char *s) { return date.parseFrom(s); }
652 /** parses the time string passed and stores it
653 *It should of the format "hh:mm::ss"
656 int parseTimeFrom(const char *s) { return time.parseFrom(s); }
658 static void changeToCsqlFormat(char *src);
660 int parseFrom(const char *s);
661 friend int operator<(const TimeStamp &d1, const TimeStamp &d2);
662 friend int operator>(const TimeStamp &d1, const TimeStamp &d2);
663 friend int operator<=(const TimeStamp &d1, const TimeStamp &d2);
664 friend int operator>=(const TimeStamp &d1, const TimeStamp &d2);
665 friend int operator==(const TimeStamp &d1, const TimeStamp &d2);
666 friend int operator!=(const TimeStamp &d1, const TimeStamp &d2);
667 void addYear(int noOfYrs ){ date.addYear(noOfYrs);}
669 void subYear(int noOfYrs ) { date.subYear(noOfYrs); }
671 void addMonth(int noOfMons ) { date.addMonth(noOfMons);}
673 void subMonth(int noOfMons) { date.subMonth(noOfMons);}
675 void addDay(int noOfDays) { date.addDay(noOfDays);}
677 void subDay(int noOfDays) { date.subDay(noOfDays); }
679 void addHour(int hours);
681 void subHour(int hours);
683 void addMin(int noOfMins);
685 void subMin(int noOfMins);
687 void addSec(int noOfSecs);
689 void subSec(int noOfSecs);
691 int secDiff(TimeStamp &ts);
692 int minDiff(TimeStamp &ts);
693 int hourDiff(TimeStamp &ts);
694 int dayDiff(TimeStamp &ts);
695 int monthDiff(TimeStamp &ts);
696 int yearDiff(TimeStamp &ts);
698 private:
699 Date date;
700 Time time;
703 #include<DataTypeInline.h>
704 #endif