changes for 2.5
[csql.git] / include / DataType.h
blob86fe423cef910790546b235a2530060aad768ae6
1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 ***************************************************************************/
16 #ifndef DATATYPE_H
17 #define DATATYPE_H
18 #include<sqlext.h>
19 #include<sqltypes.h>
20 #include<ErrorType.h>
21 //#include<os.h>
22 typedef int JulianRep;
25 /**
26 * @class DataType
27 * Data Types supported by the database system.
28 * <br/>
31 enum DataType {
32 typeInt = 0, /**<integer type*/
33 typeLong = 1,
34 typeLongLong = 2,
35 typeShort = 3,
36 typeByteInt = 4,
38 typeDouble = 10,
39 typeFloat = 11,
40 typeDecimal = 12,
42 typeDate = 20,
43 typeTime = 21,
44 typeTimeStamp = 22,
46 typeString = 30,
47 typeBinary = 31,
49 typeComposite = 98,
50 typeULong = 99,
51 typeUnknown = 100
54 /**
55 * @class ComparisionOp
56 * Comparision operators supported by the database system.
57 * <br/>
60 enum ComparisionOp {
61 OpEquals = 0,
62 OpNotEquals,
63 OpLessThan,
64 OpLessThanEquals,
65 OpGreaterThan,
66 OpGreaterThanEquals,
67 OpLike, // for Like operator
68 OpInvalidComparisionOp
70 static char CompOpNames[][20] =
72 "Equals", "NotEquals", "LessThan", "LessThanEquals", "GreaterThan",
73 "GreaterThanEquals", "Like", "Invalid"
77 /**
78 * @class LogicalOp
79 * Logical operators supported by the database system.
80 * <br/>
83 enum LogicalOp {
84 OpAnd = 0,
85 OpOr,
86 OpNot,
87 OpInvalidLogicalOp
90 static char LogOpNames[][10] =
92 "AND", "OR", "NOT", "Invalid"
95 class AllDataType
97 public:
98 inline static long size(DataType type, int length =0);
99 static char* getSQLString(DataType type);
100 static SQLSMALLINT convertToSQLType(DataType type);
101 static SQLSMALLINT convertToSQL_C_Type(DataType type);
102 static DataType convertFromSQLType(SQLSMALLINT type);
104 inline static void copyVal(void* dest, void *src, DataType type, int length = 0);
105 inline static void addVal(void* dest, void *src, DataType type);
106 static void divVal(void* dest, int src, DataType type);
107 static void subVal(void* dest, void *src, DataType type);
108 static void mulVal(void* dest, void *src, DataType type);
109 static void mudVal(void* dest, void *src, DataType type);
110 static void divVal(void* dest, void *src, DataType type);
111 static void increment(void* dest, void *src, DataType type);
112 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);
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);
146 static void memoryset(void *value,DataType type);
147 inline static ComparisionOp getComparisionOperator(char *str);
149 static void* alloc(DataType type, int length =0);
150 static DbRetVal strToValue(void *dest, char *src, DataType type, int length=0);
151 static int printVal(void *src, DataType type, int length);
158 * @class ByteInt
159 * Represents 8 bit integer.
160 * <br/>
163 class 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 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 /** returns the day of the month. Values are 1 to 31
283 int dayOfMonth() const;
285 int dayOfYear() const;
287 /** returns the month Values are 1 to 12.
289 int month() const;
291 /** returns the month name
292 * values are "January", "February", "March", "April", "May", "June",
293 * "July", "August", "September", "October", "November", "December"
295 const char *monthName() const;
297 /** returns the month name abbreviation
298 * values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
299 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
301 const char *monthAbbr() const;
303 /** returns the month name
304 * values are "January", "February", "March", "April", "May", "June",
305 * "July", "August", "September", "October", "November", "December"
307 static const char *monthName(int month);
310 /** returns the month name abbreviation
311 * values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
312 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
314 static const char *monthAbbr(int month);
316 /** parses the date string passed and stores it
317 *It should of the format "mm/dd/yyyy"
319 int parseFrom(const char *s);
321 Date &operator++() { julianDate++; return *this; }
322 Date &operator--() { julianDate--; return *this; }
324 Date &operator+=(int days) { julianDate += days; return *this;}
325 Date &operator-=(int days) { julianDate -= days; return *this;}
327 /** returns the year
329 int year() const;
331 /** checks for the validity of the date
333 static bool isValidDate(int year, int month, int day);
335 friend Date operator+(const Date &d1, int days);
336 friend Date operator+(int days, const Date &d1);
337 friend Date operator-(const Date &d1, int days);
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);
342 friend int operator>=(const Date &d1 ,const Date &d2);
343 friend int operator==(const Date &d1 ,const Date &d2);
344 friend int operator!=(const Date &d1 ,const Date &d2);
346 /** checks for leap year
348 static bool isLeapYear(int year);
350 /** returns the number of days in the specified month of the year.
352 static int daysInMonth(int month, int year);
354 static int YMDToJulian(int year,int mon,int day, JulianRep &julian);
355 static int julianToYMD(JulianRep julian,int &year,int &month,int &day);
357 private:
358 JulianRep julianDate;
365 * @class Time
366 * Represents Time Data type.
367 * <br/>
370 class Time { // The class a user would declare to hold time
371 public:
372 Time() {timeVal = 0;}
374 /** Overloaded constructor
375 * @param hours hours
376 * @param mins mins
377 * @param secs secs
378 * @param usec usec
380 Time(int hours, int mins, int secs, int usec = 0);
381 Time(int totalSecs) : timeVal(totalSecs) {;}
382 Time(const Time &d2) { timeVal = d2.timeVal; }
383 Time& operator=(const Time& d2) { timeVal=d2.timeVal; return *this; }
385 /** sets the time with specified hours, mins, secs
386 * @param hours hours
387 * @param mins mins
388 * @param secs secs
389 * @param usec usec
391 int set(int hours, int mins, int secs, int usec = 0);
393 /** retrieves the time using IN parameters
394 * @param hours hours
395 * @param mins mins
396 * @param secs secs
398 int get(int &hours, int &mins, int &secs) const;
400 /** checks for the validity of the time
402 bool isValid() const;
404 /** resets the time
406 void setNull() { timeVal = -1;}
408 int secondsSinceMidnight() const { return timeVal/10000;}
410 /** returns the microsecs
412 int usec() const; // to nearest 100 of usec.
414 /** returns the millisecs
416 int msec() const;
418 /** returns the secs
420 int seconds() const;
422 /** returns the minutes
424 int minutes() const;
426 /** returns the hours
428 int hours() const;
431 /** sets the millisecs
433 int setMsec(int ms);
435 /** sets the microsecs
437 int setUsec(int us);
439 /** parses the time string passed and stores it
440 *It should of the format "hh:mm::ss"
442 int parseFrom(const char *s);
444 Time &operator++() { timeVal += 10000; return *this; }
445 Time &operator--() { timeVal -= 10000; return *this; }
447 Time &operator+=(int seconds) { timeVal += seconds*10000; return *this; }
448 Time &operator-=(int seconds) { timeVal -= seconds*10000; return *this; }
451 /** checks for the validity of the time specified
453 static bool isValidTime(int hours, int mins, int secs);
455 friend Time operator+(const Time &t1, int seconds);
456 friend Time operator+(int seconds, const Time &t1);
457 friend Time operator-(const Time &t1, int seconds);
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 );
462 friend int operator>=(const Time &t1 ,const Time &t2 );
463 friend int operator==(const Time &t1 ,const Time &t2 );
464 friend int operator!=(const Time &t1 ,const Time &t2 );
467 private:
468 int timeVal;
472 * @class TimeStamp
473 * Represents TimeStamp Data type.
474 * <br/>
477 class TimeStamp {
479 public:
480 TimeStamp() {}
482 /** Overloaded constructor
483 * @param year year
484 * @param month month
485 * @param day day
486 * @param hours hours
487 * @param mins mins
488 * @param secs secs
489 * @param usec usec
491 TimeStamp(int year, int month, int day, int hour, int minute, int sec, int usec = 0) :
492 date(year, month, day), time(hour, minute, sec, usec) { }
494 TimeStamp(const TimeStamp &ts)
495 { date = ts.date; time = ts.time; }
496 TimeStamp(const Date &d, Time &t) : date(d), time(t) {}
499 TimeStamp& operator=(const TimeStamp& d2)
500 { date=d2.date; time = d2.time; return *this; }
502 /** get year, month, day from the date part of the timestamp
503 * @param year year IN
504 * @param month month IN
505 * @param day day IN
507 int getDate(int &year, int &month, int &day)
508 { return date.get(year, month, day); }
510 /** get the date part of the timestamp
511 * @param Date date
513 void getDate(Date &newDate) const
514 { newDate = date; }
516 /** sets the date with specified year, month, day
517 * @param year year
518 * @param month month
519 * @param day day
521 int setDate(int year, int month, int day)
522 { return date.set(year, month, day); }
524 /** set the date part of the timestamp
525 * @param Date date
527 void setDate(const Date &newDate)
528 { date = newDate; }
531 operator Date() { return date; }
532 operator Time() { return time; }
536 /** retrieves the time using IN parameters
537 * @param hours hours
538 * @param mins mins
539 * @param secs secs
541 int getTime(int &hours, int &mins, int &secs) const
542 { return time.get(hours, mins, secs); }
543 /** retrieves the time part of the timestamp
544 * @param newTime Time
546 void getTime(Time &newTime) const
547 { newTime = time; }
549 /** sets the time with specified hours, mins, secs
550 * @param hours hours
551 * @param mins mins
552 * @param secs secs
553 * @param usec usec
555 int setTime(int hours, int mins, int secs, int usec = 0)
556 { return time.set(hours, mins, secs, usec); }
558 /** set the time part of the timestamp
559 * @param newTime Time
561 void setTime(const Time &newTime)
562 { time = newTime; }
564 /** checks for the validity of the timestamp
566 bool isValid() const { return date.isValid() && time.isValid(); }
568 /** resets the date and time */
569 void setNull() { date.setNull(); time.setNull(); }
571 /** returns day of the week. Values are 1-7
573 int dayOfWeek() const { return date.dayOfWeek(); }
575 /** returns day of the week.
576 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Satur
577 day".
579 const char *dayOfWeekName() const { return date.dayOfWeekName(); }
581 /** returns day of the week abbreviation
582 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
584 const char *dayOfWeekAbbr() const { return date.dayOfWeekAbbr(); }
586 /** returns the day of the month. Values are 1 to 31
588 int dayOfMonth() const { return date.dayOfMonth(); }
589 int dayOfYear() const { return date.dayOfYear(); }
591 /** returns the month. Values are 1 to 12.
594 int month() const { return date.month(); }
596 /** returns the month name
597 * values are "January", "February", "March", "April", "May", "June",
598 * "July", "August", "September", "October", "November", "December"
600 const char *monthName() const { return date.monthName(); }
602 /** returns the month name abbreviation
603 * Values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
604 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
606 const char *monthAbbr() const { return date.monthAbbr(); }
608 /** returns the year
610 int year() const { return date.year(); }
612 int secondsSinceMidnight() const { return time.secondsSinceMidnight(); }
613 /** returns the seconds */
614 int seconds() const { return time.seconds(); }
615 /** returns the minutes */
616 int minutes() const { return time.minutes(); }
617 /** returns the hours */
618 int hours() const { return time.hours(); }
619 /** returns the millisecs */
620 int msec() const { return time.msec(); }
621 /** returns the microsecs */
622 int usec() const { return time.usec(); }
624 /** sets the millisecs */
625 int setMsec(int ms) { return time.setMsec(ms) ; }
626 /** sets the microsecs */
627 int setUsec(int us) { return time.setUsec(us) ; }
629 /** parses the date string passed and stores it
630 *It should of the format "mm/dd/yyyy"
632 int parseDateFrom(const char *s) { return date.parseFrom(s); }
634 /** parses the time string passed and stores it
635 *It should of the format "hh:mm::ss"
638 int parseTimeFrom(const char *s) { return time.parseFrom(s); }
640 int parseFrom(const char *s);
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);
646 friend int operator!=(const TimeStamp &d1, const TimeStamp &d2);
649 private:
650 Date date;
651 Time time;
654 #include<DataTypeInline.h>
655 #endif