changing file creation mode from 777 to 644
[csql.git] / include / DataType.h
blob74bed503a0db5bf5fc18d6d137c3ad6331f12414
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,
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);
142 static void memoryset(void *value,DataType type);
143 inline static ComparisionOp getComparisionOperator(char *str);
145 static void* alloc(DataType type, int length =0);
146 static DbRetVal strToValue(void *dest, char *src, DataType type, int length=0);
147 static int printVal(void *src, DataType type, int length,int dbFlag=0);
148 inline static bool isValidFieldForAvg(DataType type);
153 * @class ByteInt
154 * Represents 8 bit integer.
155 * <br/>
158 class ByteInt {
160 public:
161 ByteInt() { }
163 /** copy constructor
165 ByteInt(const ByteInt &v) { val = v.val; }
166 /** constructor with char
167 * @param v char value
169 ByteInt(char v) { val = v; }
170 operator int() const { return (int) val; }
171 char operator=(ByteInt v) { return val = v.val; }
172 char operator=(char v) { return val = v; }
173 char operator+=(ByteInt v) { return val += v.val; }
174 char operator+=(char v) { return val += v; }
175 char operator-=(ByteInt v) { return val -= v.val; }
176 char operator-=(char v) { return val -= v; }
177 char operator*=(ByteInt v) { return val *= v.val; }
178 char operator*=(char v) { return val *= v; }
179 char operator/=(ByteInt v) { return val /= v.val; }
180 char operator/=(char v) { return val /= v; }
181 char operator%=(ByteInt v) { return val %= v.val; }
182 char operator%=(char v) { return val %= v; }
183 char operator<<=(ByteInt v) { return val <<= v.val; }
184 char operator<<=(char v) { return val <<= v; }
185 char operator>>=(ByteInt v) { return val >>= v.val; }
186 char operator>>=(char v) { return val >>= v; }
187 char operator&=(ByteInt v) { return val &= v.val; }
188 char operator&=(char v) { return val &= v; }
189 char operator|=(ByteInt v) { return val |= v.val; }
190 char operator|=(char v) { return val |= v; }
191 char operator^=(ByteInt v) { return val ^= v.val; }
192 char operator^=(char v) { return val ^= v; }
193 char operator++() { return val++; }
194 char operator++(int) { char tmp = val; val++; return val; }
195 char operator--() { return val--; }
196 char operator--(int) { char tmp = val; val--; return val; }
198 private:
199 signed char val;
204 * @class Date
205 * Represents Date Data type.
206 * <br/>
209 class Date { // The class a user would declare to hold date
211 public:
212 Date() {julianDate = 0;}
213 Date(JulianRep julian) : julianDate(julian) {}
215 /** constructor with year, month, day
216 * @param year year
217 * @param month month
218 * @param day day
220 Date(int year, int month, int day);
222 Date(const Date &d2) { julianDate = d2.julianDate; }
223 Date& operator=(const Date& d2)
224 { julianDate=d2.julianDate; return *this; }
226 /** sets the date with specified year, month, day
227 * @param year year
228 * @param month month
229 * @param day day
231 int set(int year, int month, int day);
232 int set(const struct tm *tmptr);
234 /** get year, month, day of the date
235 * @param year year IN
236 * @param month month IN
237 * @param day day IN
239 int get(int &year, int &month, int &day) const;
241 /** checks for the validity of the date
243 bool isValid() const;
245 /** resets the date to zero
247 void setNull() { julianDate = 0;}
249 /** returns day of the week
251 int dayOfWeek() const;
253 /** returns day of the week.
254 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday".
256 const char *dayOfWeekName() const;
258 /** returns day of the week abbreviation
259 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
261 const char *dayOfWeekAbbr() const;
263 /** returns day of the week.
264 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday".
266 static const char *dayOfWeekName(int day); // 0--> Sunday
268 /** returns day of the week abbreviation
269 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
271 static const char *dayOfWeekAbbr(int day);
273 static int dayOfWeek(JulianRep juldate);
276 /** returns the day of the month. Values are 1 to 31
278 int dayOfMonth() const;
280 int dayOfYear() const;
282 /** returns the month Values are 1 to 12.
284 int month() const;
286 /** returns the month name
287 * values are "January", "February", "March", "April", "May", "June",
288 * "July", "August", "September", "October", "November", "December"
290 const char *monthName() const;
292 /** returns the month name abbreviation
293 * values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
294 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
296 const char *monthAbbr() const;
298 /** returns the month name
299 * values are "January", "February", "March", "April", "May", "June",
300 * "July", "August", "September", "October", "November", "December"
302 static const char *monthName(int month);
305 /** returns the month name abbreviation
306 * values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
307 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
309 static const char *monthAbbr(int month);
311 /** parses the date string passed and stores it
312 *It should of the format "mm/dd/yyyy"
314 int parseFrom(const char *s);
316 static void changeToCsqlFormat(char *src);
319 Date &operator++() { julianDate++; return *this; }
320 Date &operator--() { julianDate--; return *this; }
322 Date &operator+=(int days) { julianDate += days; return *this;}
323 Date &operator-=(int days) { julianDate -= days; return *this;}
325 /** returns the year
327 int year() const;
329 /** checks for the validity of the date
331 static bool isValidDate(int year, int month, int day);
333 friend Date operator+(const Date &d1, int days);
334 friend Date operator+(int days, const Date &d1);
335 friend Date operator-(const Date &d1, int days);
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);
342 friend int operator!=(const Date &d1 ,const Date &d2);
344 /** checks for leap year
346 static bool isLeapYear(int year);
348 /** returns the number of days in the specified month of the year.
350 static int daysInMonth(int month, int year);
352 static int YMDToJulian(int year,int mon,int day, JulianRep &julian);
353 static int julianToYMD(JulianRep julian,int &year,int &month,int &day);
355 private:
356 JulianRep julianDate;
363 * @class Time
364 * Represents Time Data type.
365 * <br/>
368 class Time { // The class a user would declare to hold time
369 public:
370 Time() {timeVal = 0;}
372 /** Overloaded constructor
373 * @param hours hours
374 * @param mins mins
375 * @param secs secs
376 * @param usec usec
378 Time(int hours, int mins, int secs, int usec = 0);
379 Time(int totalSecs) : timeVal(totalSecs) {;}
380 Time(const Time &d2) { timeVal = d2.timeVal; }
381 Time& operator=(const Time& d2) { timeVal=d2.timeVal; return *this; }
383 /** sets the time with specified hours, mins, secs
384 * @param hours hours
385 * @param mins mins
386 * @param secs secs
387 * @param usec usec
389 int set(int hours, int mins, int secs, int usec = 0);
391 /** retrieves the time using IN parameters
392 * @param hours hours
393 * @param mins mins
394 * @param secs secs
396 int get(int &hours, int &mins, int &secs) const;
398 /** checks for the validity of the time
400 bool isValid() const;
402 /** resets the time
404 void setNull() { timeVal = -1;}
406 int secondsSinceMidnight() const { return timeVal/10000;}
408 /** returns the microsecs
410 int usec() const; // to nearest 100 of usec.
412 /** returns the millisecs
414 int msec() const;
416 /** returns the secs
418 int seconds() const;
420 /** returns the minutes
422 int minutes() const;
424 /** returns the hours
426 int hours() const;
429 /** sets the millisecs
431 int setMsec(int ms);
433 /** sets the microsecs
435 int setUsec(int us);
437 /** parses the time string passed and stores it
438 *It should of the format "hh:mm::ss"
440 int parseFrom(const char *s);
442 Time &operator++() { timeVal += 10000; return *this; }
443 Time &operator--() { timeVal -= 10000; return *this; }
445 Time &operator+=(int seconds) { timeVal += seconds*10000; return *this; }
446 Time &operator-=(int seconds) { timeVal -= seconds*10000; return *this; }
449 /** checks for the validity of the time specified
451 static bool isValidTime(int hours, int mins, int secs);
453 friend Time operator+(const Time &t1, int seconds);
454 friend Time operator+(int seconds, const Time &t1);
455 friend Time operator-(const Time &t1, int seconds);
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 );
462 friend int operator!=(const Time &t1 ,const Time &t2 );
465 private:
466 int timeVal;
470 * @class TimeStamp
471 * Represents TimeStamp Data type.
472 * <br/>
475 class TimeStamp {
477 public:
478 TimeStamp() {}
480 /** Overloaded constructor
481 * @param year year
482 * @param month month
483 * @param day day
484 * @param hours hours
485 * @param mins mins
486 * @param secs secs
487 * @param usec usec
489 TimeStamp(int year, int month, int day, int hour, int minute, int sec, int usec = 0) :
490 date(year, month, day), time(hour, minute, sec, usec) { }
492 TimeStamp(const TimeStamp &ts)
493 { date = ts.date; time = ts.time; }
494 TimeStamp(const Date &d, Time &t) : date(d), time(t) {}
497 TimeStamp& operator=(const TimeStamp& d2)
498 { date=d2.date; time = d2.time; return *this; }
500 /** get year, month, day from the date part of the timestamp
501 * @param year year IN
502 * @param month month IN
503 * @param day day IN
505 int getDate(int &year, int &month, int &day)
506 { return date.get(year, month, day); }
508 /** get the date part of the timestamp
509 * @param Date date
511 void getDate(Date &newDate) const
512 { newDate = date; }
514 /** sets the date with specified year, month, day
515 * @param year year
516 * @param month month
517 * @param day day
519 int setDate(int year, int month, int day)
520 { return date.set(year, month, day); }
522 /** set the date part of the timestamp
523 * @param Date date
525 void setDate(const Date &newDate)
526 { date = newDate; }
529 operator Date() { return date; }
530 operator Time() { return time; }
534 /** retrieves the time using IN parameters
535 * @param hours hours
536 * @param mins mins
537 * @param secs secs
539 int getTime(int &hours, int &mins, int &secs) const
540 { return time.get(hours, mins, secs); }
541 /** retrieves the time part of the timestamp
542 * @param newTime Time
544 void getTime(Time &newTime) const
545 { newTime = time; }
547 /** sets the time with specified hours, mins, secs
548 * @param hours hours
549 * @param mins mins
550 * @param secs secs
551 * @param usec usec
553 int setTime(int hours, int mins, int secs, int usec = 0)
554 { return time.set(hours, mins, secs, usec); }
556 /** set the time part of the timestamp
557 * @param newTime Time
559 void setTime(const Time &newTime)
560 { time = newTime; }
562 /** checks for the validity of the timestamp
564 bool isValid() const { return date.isValid() && time.isValid(); }
566 /** resets the date and time */
567 void setNull() { date.setNull(); time.setNull(); }
569 /** returns day of the week. Values are 1-7
571 int dayOfWeek() const { return date.dayOfWeek(); }
573 /** returns day of the week.
574 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Satur
575 day".
577 const char *dayOfWeekName() const { return date.dayOfWeekName(); }
579 /** returns day of the week abbreviation
580 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
582 const char *dayOfWeekAbbr() const { return date.dayOfWeekAbbr(); }
584 /** returns the day of the month. Values are 1 to 31
586 int dayOfMonth() const { return date.dayOfMonth(); }
587 int dayOfYear() const { return date.dayOfYear(); }
589 /** returns the month. Values are 1 to 12.
592 int month() const { return date.month(); }
594 /** returns the month name
595 * values are "January", "February", "March", "April", "May", "June",
596 * "July", "August", "September", "October", "November", "December"
598 const char *monthName() const { return date.monthName(); }
600 /** returns the month name abbreviation
601 * Values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
602 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
604 const char *monthAbbr() const { return date.monthAbbr(); }
606 /** returns the year
608 int year() const { return date.year(); }
610 int secondsSinceMidnight() const { return time.secondsSinceMidnight(); }
611 /** returns the seconds */
612 int seconds() const { return time.seconds(); }
613 /** returns the minutes */
614 int minutes() const { return time.minutes(); }
615 /** returns the hours */
616 int hours() const { return time.hours(); }
617 /** returns the millisecs */
618 int msec() const { return time.msec(); }
619 /** returns the microsecs */
620 int usec() const { return time.usec(); }
622 /** sets the millisecs */
623 int setMsec(int ms) { return time.setMsec(ms) ; }
624 /** sets the microsecs */
625 int setUsec(int us) { return time.setUsec(us) ; }
627 /** parses the date string passed and stores it
628 *It should of the format "mm/dd/yyyy"
630 int parseDateFrom(const char *s) { return date.parseFrom(s); }
632 /** parses the time string passed and stores it
633 *It should of the format "hh:mm::ss"
636 int parseTimeFrom(const char *s) { return time.parseFrom(s); }
638 static void changeToCsqlFormat(char *src);
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