network support for csql client
[csql.git] / include / DataTypeInline.h
blob337bdf683b1eb65a7d53ab364226acab55f26d1c
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 #include<os.h>
17 #include<DataType.h>
18 long AllDataType::size(DataType type, int length )
20 if (type == typeInt) return sizeof(int);
21 else if (type == typeString) return length;
23 long size = 0;
24 switch(type)
26 case typeInt:
27 size = sizeof(int);
28 break;
29 case typeLong:
30 size = sizeof(long);
31 break;
32 case typeLongLong:
33 size = sizeof(long long);
34 break;
35 case typeShort:
36 size = sizeof(short);
37 break;
38 case typeByteInt:
39 size = sizeof(char);
40 break;
41 case typeDouble:
42 size = sizeof(double);
43 break;
44 case typeFloat:
45 size = sizeof(float);
46 break;
47 case typeDecimal:
48 //TODO::for porting
49 //fldDef.length_ = sizeof(long double);
50 break;
51 case typeDate:
52 size = sizeof(Date);
53 break;
54 case typeTime:
55 size = sizeof(Time);
56 break;
57 case typeTimeStamp:
58 size = sizeof(TimeStamp);
59 break;
60 case typeString:
61 case typeBinary:
62 case typeComposite:
63 size = length;
64 break;
65 default:
66 size = 0;
67 break;
69 return size;
71 void AllDataType::copyVal(void* dest, void *src, DataType type, int length)
73 //Performance optimization. putting likely case first
74 if (typeInt == type )
76 *(int*)dest = *(int*)src;
77 return;
78 }else if (typeString == type)
80 //null is always put at the last byte by insert
81 //so using strcpy is safe
82 //strcpy((char*)dest, (char*)src);
83 memcpy((char*)dest, (char*)src, length);
84 //strncpy((char*)dest, (char*)src, length);
85 //char *d =(char*)dest;
86 //d[length-1] = '\0';
87 return;
88 }else if (typeShort == type) {
89 *(short*)dest = *(short*)src;
90 }else if (typeDouble == type) {
91 *(double*)dest = *(double*)src;
92 }else if (typeTimeStamp == type) {
93 *(TimeStamp*)dest = *(TimeStamp*)src;
94 }else if (typeDate == type) {
95 *(Date*)dest = *(Date*)src;
96 }else if (typeFloat == type) {
97 *(float*)dest = *(float*)src;
98 }else if (typeTime == type) {
99 *(Time*)dest = *(Time*)src;
100 }else if (typeLong == type) {
101 *(long*)dest = *(long*)src;
102 }else if (typeLongLong == type) {
103 *(long long*)dest = *(long long*)src;
104 }else if (typeByteInt == type) {
105 *(char*)dest = *(char*)src;
106 }else if (typeBinary == type) {
107 os::memcpy(dest, src, length);
108 }else if (typeComposite == type) {
109 os::memcpy(dest, src, length);
111 return;
113 void AllDataType::addVal(void* dest, void *src, DataType type)
115 if (type == typeInt)
117 *(int*)dest = *(int*)dest + *(int*)src;
118 return;
120 switch(type)
122 case typeInt:
123 *(int*)dest = *(int*)dest + *(int*)src;
124 break;
125 case typeLong:
126 *(long*)dest = *(long*)dest + *(long*)src;
127 break;
128 case typeLongLong:
129 *(long long*)dest = *(long long*)dest + *(long long*)src;
130 break;
131 case typeShort:
132 *(short*)dest = *(short*)dest + *(short*)src;
133 break;
134 case typeByteInt:
135 *(char*)dest = *(char*)dest + *(char*)src;
136 break;
137 case typeDouble:
138 *(double*)dest = *(double*)dest + *(double*)src;
139 break;
140 case typeFloat:
141 *(float*)dest = *(float*)dest + *(float*)src;
142 break;
143 case typeDecimal:
144 //TODO::for porting
145 case typeDate:
146 case typeTime:
147 case typeTimeStamp:
148 case typeBinary:
149 default:
150 break;
152 return;
155 bool AllDataType::compareVal(void *val1, void *val2, ComparisionOp op,
156 DataType type, long length)
158 //Performance optimization.
159 //do not convert compareXXXVal to virtual functions. it takes more time
160 if (typeInt == type)
162 //as int is the heavily used type, hardcoding the compare here itself
163 if (OpEquals == op) {
164 if (*(int*)val1 == *(int*)val2) return true;
165 else return false;
166 }else if (OpLessThanEquals == op) {
167 if (*(int*)val1 <= *(int*)val2) return true;
168 else return false;
169 }else if (OpGreaterThanEquals == op) {
170 if (*(int*)val1 >= *(int*)val2) return true;
171 else return false;
172 }else if (OpGreaterThan == op) {
173 if (*(int*)val1 > *(int*)val2) return true;
174 else return false;
175 }else if (OpLessThan == op) {
176 if (*(int*)val1 < *(int*)val2) return true;
177 else return false;
178 }else if (OpNotEquals == op) {
179 if (*(int*)val1 != *(int*)val2) return true;
180 else return false;
183 }else if(typeString == type) {
184 return AllDataType::compareStringVal(val1, val2, op);
185 } else if (typeShort == type) {
186 return AllDataType::compareShortVal(val1, val2, op);
187 } else if (typeDouble == type) {
188 return AllDataType::compareDoubleVal(val1, val2, op);
189 } else if (typeFloat == type) {
190 return AllDataType::compareFloatVal(val1, val2, op);
191 } else if (typeLong == type) {
192 return AllDataType::compareLongVal(val1, val2, op);
193 } else if (typeLongLong == type) {
194 return AllDataType::compareLongLongVal(val1, val2, op);
195 } else if (typeByteInt == type) {
196 return AllDataType::compareByteIntVal(val1, val2, op);
197 } else if (typeTimeStamp == type) {
198 return AllDataType::compareTimeStampVal(val1, val2, op);
199 } else if (typeDate == type) {
200 return AllDataType::compareDateVal(val1, val2, op);
201 } else if (typeTime == type) {
202 return AllDataType::compareTimeVal(val1, val2, op);
203 } else if (typeBinary == type) {
204 return AllDataType::compareBinaryVal(val1, val2, op, length);
205 } else if (typeComposite == type) {
206 return AllDataType::compareBinaryVal(val1, val2, op, length);
209 ComparisionOp AllDataType::getComparisionOperator(char *str)
211 ComparisionOp op;
212 if (strcmp(str, "<=") == 0)
213 op = OpLessThanEquals;
214 else if (strcmp(str, ">=") == 0)
215 op = OpGreaterThanEquals;
216 else if (strcmp(str, "<") == 0)
217 op = OpLessThan;
218 else if (strcmp(str, ">") == 0)
219 op = OpGreaterThan;
220 else if (strcmp(str, "=") == 0)
221 op = OpEquals;
222 else if (strcmp(str, "!=") == 0 || strcmp(str, "<>") == 0 )
223 op = OpNotEquals;
224 else if (strcasecmp(str, "LIKE") == 0 )
225 op = OpLike;
226 else
227 op = OpInvalidComparisionOp;
228 return op;