Remove nwmode in csqlnw.conf file and making UDPClient and UDPServer as
[csql.git] / include / DataType.h
blobf4f6c678d6b1908a0c849e69914ba3aeaf4c539a
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<os.h>
21 typedef int JulianRep;
24 /**
25 * @class DataType
26 * Data Types supported by the database system.
27 * <br/>
28 * @author Prabakaran Thirumalai
30 enum DataType {
31 typeInt = 0, /**<integer type*/
32 typeLong = 1,
33 typeLongLong = 2,
34 typeShort = 3,
35 typeByteInt = 4,
37 typeDouble = 10,
38 typeFloat = 11,
39 typeDecimal = 12,
41 typeDate = 20,
42 typeTime = 21,
43 typeTimeStamp = 22,
45 typeString = 30,
46 typeBinary = 31,
48 typeULong = 99,
49 typeUnknown = 100
52 /**
53 * @class ComparisionOp
54 * Comparision operators supported by the database system.
55 * <br/>
56 * @author Prabakaran Thirumalai
58 enum ComparisionOp {
59 OpEquals = 0,
60 OpNotEquals,
61 OpLessThan,
62 OpLessThanEquals,
63 OpGreaterThan,
64 OpGreaterThanEquals,
65 OpInvalidComparisionOp
68 /**
69 * @class LogicalOp
70 * Logical operators supported by the database system.
71 * <br/>
72 * @author Prabakaran Thirumalai
74 enum LogicalOp {
75 OpAnd = 0,
76 OpOr,
77 OpNot,
78 OpInvalidLogicalOp
82 class AllDataType
84 public:
85 static long size(DataType type, int length =0);
86 static SQLSMALLINT convertToSQLType(DataType type);
87 static SQLSMALLINT convertToSQL_C_Type(DataType type);
88 static DataType convertFromSQLType(SQLSMALLINT type);
90 static void copyVal(void* dest, void *src, DataType type, int length = 0);
92 static bool compareVal(void *src1, void *src2, ComparisionOp op,
93 DataType type, long length = 0);
94 static bool compareIntVal(void* src1, void* src2, ComparisionOp op);
95 static bool compareLongVal(void* src1, void* src2, ComparisionOp op);
96 static bool compareLongLongVal(void* src1, void* src2, ComparisionOp op);
97 static bool compareShortVal(void* src1, void* src2, ComparisionOp op);
98 static bool compareByteIntVal(void* src1, void* src2, ComparisionOp op);
99 static bool compareDoubleVal(void* src1, void* src2, ComparisionOp op);
100 static bool compareFloatVal(void* src1, void* src2, ComparisionOp op);
101 static bool compareDateVal(void* src1, void* src2, ComparisionOp op);
102 static bool compareTimeVal(void* src1, void* src2, ComparisionOp op);
103 static bool compareTimeStampVal(void* src1, void* src2, ComparisionOp op);
104 static bool compareStringVal(void* src1, void* src2, ComparisionOp op);
105 static bool compareBinaryVal(void* src1, void* src2,
106 ComparisionOp op, int length);
109 static void convert(DataType srcType, void *src, DataType destType, void *dest);
110 static void convertToInt(void* dest, void* src, DataType srcType);
111 static void convertToLong(void* dest, void* src, DataType srcType);
112 static void convertToLongLong(void* dest, void* src, DataType srcType);
113 static void convertToShort(void* dest, void* src, DataType srcType);
114 static void convertToByteInt(void* dest, void* src, DataType srcType);
115 static void convertToFloat(void* dest, void* src, DataType srcType);
116 static void convertToDouble(void* dest, void* src, DataType srcType);
117 static void convertToString(void* dest, void* src, DataType srcType);
120 static ComparisionOp getComparisionOperator(char *str);
122 static void* alloc(DataType type, int length =0);
123 static void strToValue(void *dest, char *src, DataType type, int length=0);
124 static void printVal(void *src, DataType type, int length);
131 * @class ByteInt
132 * Represents 8 bit integer.
133 * <br/>
134 * @author Prabakaran Thirumalai
136 class ByteInt {
138 public:
139 ByteInt() { }
141 /** copy constructor
143 ByteInt(const ByteInt &v) { val = v.val; }
144 /** constructor with char
145 * @param v char value
147 ByteInt(char v) { val = v; }
148 operator int() const { return (int) val; }
149 char operator=(ByteInt v) { return val = v.val; }
150 char operator=(char v) { return val = v; }
151 char operator+=(ByteInt v) { return val += v.val; }
152 char operator+=(char v) { return val += v; }
153 char operator-=(ByteInt v) { return val -= v.val; }
154 char operator-=(char v) { return val -= v; }
155 char operator*=(ByteInt v) { return val *= v.val; }
156 char operator*=(char v) { return val *= v; }
157 char operator/=(ByteInt v) { return val /= v.val; }
158 char operator/=(char v) { return val /= v; }
159 char operator%=(ByteInt v) { return val %= v.val; }
160 char operator%=(char v) { return val %= v; }
161 char operator<<=(ByteInt v) { return val <<= v.val; }
162 char operator<<=(char v) { return val <<= v; }
163 char operator>>=(ByteInt v) { return val >>= v.val; }
164 char operator>>=(char v) { return val >>= v; }
165 char operator&=(ByteInt v) { return val &= v.val; }
166 char operator&=(char v) { return val &= v; }
167 char operator|=(ByteInt v) { return val |= v.val; }
168 char operator|=(char v) { return val |= v; }
169 char operator^=(ByteInt v) { return val ^= v.val; }
170 char operator^=(char v) { return val ^= v; }
171 char operator++() { return val++; }
172 char operator++(int) { char tmp = val; val++; return val; }
173 char operator--() { return val--; }
174 char operator--(int) { char tmp = val; val--; return val; }
176 private:
177 signed char val;
182 * @class Date
183 * Represents Date Data type.
184 * <br/>
185 * @author Prabakaran Thirumalai
187 class Date { // The class a user would declare to hold date
189 public:
190 Date() {julianDate = 0;}
191 Date(JulianRep julian) : julianDate(julian) {}
193 /** constructor with year, month, day
194 * @param year year
195 * @param month month
196 * @param day day
198 Date(int year, int month, int day);
200 Date(const Date &d2) { julianDate = d2.julianDate; }
201 Date& operator=(const Date& d2)
202 { julianDate=d2.julianDate; return *this; }
204 /** sets the date with specified year, month, day
205 * @param year year
206 * @param month month
207 * @param day day
209 int set(int year, int month, int day);
210 int set(const struct tm *tmptr);
212 /** get year, month, day of the date
213 * @param year year IN
214 * @param month month IN
215 * @param day day IN
217 int get(int &year, int &month, int &day) const;
219 /** checks for the validity of the date
221 bool isValid() const;
223 /** resets the date to zero
225 void setNull() { julianDate = 0;}
227 /** returns day of the week
229 int dayOfWeek() const;
231 /** returns day of the week.
232 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday".
234 const char *dayOfWeekName() const;
236 /** returns day of the week abbreviation
237 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
239 const char *dayOfWeekAbbr() const;
241 /** returns day of the week.
242 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday".
244 static const char *dayOfWeekName(int day); // 0--> Sunday
246 /** returns day of the week abbreviation
247 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
249 static const char *dayOfWeekAbbr(int day);
251 static int dayOfWeek(JulianRep juldate);
254 /** returns the day of the month. Values are 1 to 31
256 int dayOfMonth() const;
258 int dayOfYear() const;
260 /** returns the month Values are 1 to 12.
262 int month() const;
264 /** returns the month name
265 * values are "January", "February", "March", "April", "May", "June",
266 * "July", "August", "September", "October", "November", "December"
268 const char *monthName() const;
270 /** returns the month name abbreviation
271 * values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
272 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
274 const char *monthAbbr() const;
276 /** returns the month name
277 * values are "January", "February", "March", "April", "May", "June",
278 * "July", "August", "September", "October", "November", "December"
280 static const char *monthName(int month);
283 /** returns the month name abbreviation
284 * values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
285 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
287 static const char *monthAbbr(int month);
289 /** parses the date string passed and stores it
290 *It should of the format "mm/dd/yyyy"
292 int parseFrom(const char *s);
294 Date &operator++() { julianDate++; return *this; }
295 Date &operator--() { julianDate--; return *this; }
297 Date &operator+=(int days) { julianDate += days; return *this;}
298 Date &operator-=(int days) { julianDate -= days; return *this;}
300 /** returns the year
302 int year() const;
304 /** checks for the validity of the date
306 static bool isValidDate(int year, int month, int day);
308 friend Date operator+(const Date &d1, int days);
309 friend Date operator+(int days, const Date &d1);
310 friend Date operator-(const Date &d1, int days);
311 friend int operator-(const Date &d1, const Date & d2);
312 friend int operator<(const Date &d1 ,const Date &d2);
313 friend int operator>(const Date &d1 ,const Date &d2);
314 friend int operator<=(const Date &d1 ,const Date &d2);
315 friend int operator>=(const Date &d1 ,const Date &d2);
316 friend int operator==(const Date &d1 ,const Date &d2);
317 friend int operator!=(const Date &d1 ,const Date &d2);
319 /** checks for leap year
321 static bool isLeapYear(int year);
323 /** returns the number of days in the specified month of the year.
325 static int daysInMonth(int month, int year);
327 static int YMDToJulian(int year,int mon,int day, JulianRep &julian);
328 static int julianToYMD(JulianRep julian,int &year,int &month,int &day);
330 private:
331 JulianRep julianDate;
338 * @class Time
339 * Represents Time Data type.
340 * <br/>
341 * @author Prabakaran Thirumalai
343 class Time { // The class a user would declare to hold time
344 public:
345 Time() {timeVal = 0;}
347 /** Overloaded constructor
348 * @param hours hours
349 * @param mins mins
350 * @param secs secs
351 * @param usec usec
353 Time(int hours, int mins, int secs, int usec = 0);
354 Time(int totalSecs) : timeVal(totalSecs) {;}
355 Time(const Time &d2) { timeVal = d2.timeVal; }
356 Time& operator=(const Time& d2) { timeVal=d2.timeVal; return *this; }
358 /** sets the time with specified hours, mins, secs
359 * @param hours hours
360 * @param mins mins
361 * @param secs secs
362 * @param usec usec
364 int set(int hours, int mins, int secs, int usec = 0);
366 /** retrieves the time using IN parameters
367 * @param hours hours
368 * @param mins mins
369 * @param secs secs
371 int get(int &hours, int &mins, int &secs) const;
373 /** checks for the validity of the time
375 bool isValid() const;
377 /** resets the time
379 void setNull() { timeVal = -1;}
381 int secondsSinceMidnight() const { return timeVal/10000;}
383 /** returns the microsecs
385 int usec() const; // to nearest 100 of usec.
387 /** returns the millisecs
389 int msec() const;
391 /** returns the secs
393 int seconds() const;
395 /** returns the minutes
397 int minutes() const;
399 /** returns the hours
401 int hours() const;
404 /** sets the millisecs
406 int setMsec(int ms);
408 /** sets the microsecs
410 int setUsec(int us);
412 /** parses the time string passed and stores it
413 *It should of the format "hh:mm::ss"
415 int parseFrom(const char *s);
417 Time &operator++() { timeVal += 10000; return *this; }
418 Time &operator--() { timeVal -= 10000; return *this; }
420 Time &operator+=(int seconds) { timeVal += seconds*10000; return *this; }
421 Time &operator-=(int seconds) { timeVal -= seconds*10000; return *this; }
424 /** checks for the validity of the time specified
426 static bool isValidTime(int hours, int mins, int secs);
428 friend Time operator+(const Time &t1, int seconds);
429 friend Time operator+(int seconds, const Time &t1);
430 friend Time operator-(const Time &t1, int seconds);
431 friend int operator-(const Time &t1, const Time& t2);
432 friend int operator<(const Time &t1 ,const Time &t2 );
433 friend int operator>(const Time &t1 ,const Time &t2 );
434 friend int operator<=(const Time &t1 ,const Time &t2 );
435 friend int operator>=(const Time &t1 ,const Time &t2 );
436 friend int operator==(const Time &t1 ,const Time &t2 );
437 friend int operator!=(const Time &t1 ,const Time &t2 );
440 private:
441 int timeVal;
445 * @class TimeStamp
446 * Represents TimeStamp Data type.
447 * <br/>
448 * @author Prabakaran Thirumalai
450 class TimeStamp {
452 public:
453 TimeStamp() {}
455 /** Overloaded constructor
456 * @param year year
457 * @param month month
458 * @param day day
459 * @param hours hours
460 * @param mins mins
461 * @param secs secs
462 * @param usec usec
464 TimeStamp(int year, int month, int day, int hour, int minute, int sec, int usec = 0) :
465 date(year, month, day), time(hour, minute, sec, usec) { }
467 TimeStamp(const TimeStamp &ts)
468 { date = ts.date; time = ts.time; }
469 TimeStamp(const Date &d, Time &t) : date(d), time(t) {}
472 TimeStamp& operator=(const TimeStamp& d2)
473 { date=d2.date; time = d2.time; return *this; }
475 /** get year, month, day from the date part of the timestamp
476 * @param year year IN
477 * @param month month IN
478 * @param day day IN
480 int getDate(int &year, int &month, int &day)
481 { return date.get(year, month, day); }
483 /** get the date part of the timestamp
484 * @param Date date
486 void getDate(Date &newDate) const
487 { newDate = date; }
489 /** sets the date with specified year, month, day
490 * @param year year
491 * @param month month
492 * @param day day
494 int setDate(int year, int month, int day)
495 { return date.set(year, month, day); }
497 /** set the date part of the timestamp
498 * @param Date date
500 void setDate(const Date &newDate)
501 { date = newDate; }
504 operator Date() { return date; }
505 operator Time() { return time; }
509 /** retrieves the time using IN parameters
510 * @param hours hours
511 * @param mins mins
512 * @param secs secs
514 int getTime(int &hours, int &mins, int &secs) const
515 { return time.get(hours, mins, secs); }
516 /** retrieves the time part of the timestamp
517 * @param newTime Time
519 void getTime(Time &newTime) const
520 { newTime = time; }
522 /** sets the time with specified hours, mins, secs
523 * @param hours hours
524 * @param mins mins
525 * @param secs secs
526 * @param usec usec
528 int setTime(int hours, int mins, int secs, int usec = 0)
529 { return time.set(hours, mins, secs, usec); }
531 /** set the time part of the timestamp
532 * @param newTime Time
534 void setTime(const Time &newTime)
535 { time = newTime; }
537 /** checks for the validity of the timestamp
539 bool isValid() const { return date.isValid() && time.isValid(); }
541 /** resets the date and time */
542 void setNull() { date.setNull(); time.setNull(); }
544 /** returns day of the week. Values are 1-7
546 int dayOfWeek() const { return date.dayOfWeek(); }
548 /** returns day of the week.
549 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Satur
550 day".
552 const char *dayOfWeekName() const { return date.dayOfWeekName(); }
554 /** returns day of the week abbreviation
555 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
557 const char *dayOfWeekAbbr() const { return date.dayOfWeekAbbr(); }
559 /** returns the day of the month. Values are 1 to 31
561 int dayOfMonth() const { return date.dayOfMonth(); }
562 int dayOfYear() const { return date.dayOfYear(); }
564 /** returns the month. Values are 1 to 12.
567 int month() const { return date.month(); }
569 /** returns the month name
570 * values are "January", "February", "March", "April", "May", "June",
571 * "July", "August", "September", "October", "November", "December"
573 const char *monthName() const { return date.monthName(); }
575 /** returns the month name abbreviation
576 * Values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
577 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
579 const char *monthAbbr() const { return date.monthAbbr(); }
581 /** returns the year
583 int year() const { return date.year(); }
585 int secondsSinceMidnight() const { return time.secondsSinceMidnight(); }
586 /** returns the seconds */
587 int seconds() const { return time.seconds(); }
588 /** returns the minutes */
589 int minutes() const { return time.minutes(); }
590 /** returns the hours */
591 int hours() const { return time.hours(); }
592 /** returns the millisecs */
593 int msec() const { return time.msec(); }
594 /** returns the microsecs */
595 int usec() const { return time.usec(); }
597 /** sets the millisecs */
598 int setMsec(int ms) { return time.setMsec(ms) ; }
599 /** sets the microsecs */
600 int setUsec(int us) { return time.setUsec(us) ; }
602 /** parses the date string passed and stores it
603 *It should of the format "mm/dd/yyyy"
605 int parseDateFrom(const char *s) { return date.parseFrom(s); }
607 /** parses the time string passed and stores it
608 *It should of the format "hh:mm::ss"
611 int parseTimeFrom(const char *s) { return time.parseFrom(s); }
613 friend int operator<(const TimeStamp &d1, const TimeStamp &d2);
614 friend int operator>(const TimeStamp &d1, const TimeStamp &d2);
615 friend int operator<=(const TimeStamp &d1, const TimeStamp &d2);
616 friend int operator>=(const TimeStamp &d1, const TimeStamp &d2);
617 friend int operator==(const TimeStamp &d1, const TimeStamp &d2);
618 friend int operator!=(const TimeStamp &d1, const TimeStamp &d2);
621 private:
622 Date date;
623 Time time;
627 #endif