code reorg
[csql.git] / include / DataTypeInline.h
blob0da9f2e563d3e7e1c892289a2dc06db0fcfa61cc
1 /***************************************************************************
2 * *
3 * Copyright (C) Lakshya Solutions Ltd. All rights reserved. *
4 * *
5 ***************************************************************************/
7 #include<os.h>
8 #include<DataType.h>
9 #include<Config.h>
10 long AllDataType::size(DataType type, int length )
12 if (type == typeInt) return sizeof(int);
13 else if (type == typeString) return length;
15 long size = 0;
16 switch(type)
18 case typeInt:
19 size = sizeof(int);
20 break;
21 case typeLong:
22 size = sizeof(long);
23 break;
24 case typeLongLong:
25 size = sizeof(long long);
26 break;
27 case typeShort:
28 size = sizeof(short);
29 break;
30 case typeByteInt:
31 size = sizeof(char);
32 break;
33 case typeDouble:
34 size = sizeof(double);
35 break;
36 case typeFloat:
37 size = sizeof(float);
38 break;
39 case typeDecimal:
40 //TODO::for porting
41 //fldDef.length_ = sizeof(long double);
42 break;
43 case typeDate:
44 size = sizeof(Date);
45 break;
46 case typeTime:
47 size = sizeof(Time);
48 break;
49 case typeTimeStamp:
50 size = sizeof(TimeStamp);
51 break;
52 case typeString:
53 case typeVarchar:
54 case typeBinary:
55 case typeComposite:
56 size = length;
57 break;
58 default:
59 size = 0;
60 break;
62 return size;
64 void AllDataType::cachecopyVal(void* dest, void *src, DataType type, int length,int dbFlag)
66 if (typeInt == type )
68 *(int*)dest = *(int*)src;
69 return;
70 }else if (typeString == type || typeVarchar == type)
72 Util::trimRight((char*)src);
73 //strncpy((char*)dest, (char*)src, length);
74 //char *d =(char*)dest;
75 //d[length-1] = '\0';
76 strcpy((char *)dest, (char *)src);
77 return;
78 }else if (typeShort == type) {
79 *(short*)dest = *(short*)src;
80 }else if (typeDouble == type) {
81 *(double*)dest = *(double*)src;
82 }else if (typeTimeStamp == type) {
83 *(TimeStamp*)dest = *(TimeStamp*)src;
84 }else if (typeDate == type) {
85 *(Date*)dest = *(Date*)src;
86 }else if (typeFloat == type) {
87 *(float*)dest = *(float*)src;
88 }else if (typeTime == type) {
89 *(Time*)dest = *(Time*)src;
90 }else if (typeLong == type) {
91 *(long*)dest = *(long*)src;
92 }else if (typeLongLong == type) {
93 if(1 == dbFlag)
94 convertToLongLong(dest, src,typeString);
95 else
96 *(long long*)dest = *(long long*)src;
97 }else if (typeByteInt == type) {
98 *(char*)dest = *(char*)src;
99 }else if (typeBinary == type) {
100 os::memcpy(dest, src, length);
101 }else if (typeComposite == type) {
102 os::memcpy(dest, src, length);
104 return;
107 void AllDataType::copyVal(void* dest, void *src, DataType type, int length,int dbFlag)
109 //Performance optimization. putting likely case first
110 if (typeInt == type )
112 *(int*)dest = *(int*)src;
113 return;
114 }else if (typeString == type || typeVarchar == type)
116 //strncpy((char*)dest, (char*)src, length);
117 //char *d =(char*)dest;
118 //d[length-1] = '\0';
119 strcpy((char*)dest, (char*)src);
120 return;
121 }else if (typeShort == type) {
122 *(short*)dest = *(short*)src;
123 }else if (typeDouble == type) {
124 *(double*)dest = *(double*)src;
125 }else if (typeTimeStamp == type) {
126 *(TimeStamp*)dest = *(TimeStamp*)src;
127 }else if (typeDate == type) {
128 *(Date*)dest = *(Date*)src;
129 }else if (typeFloat == type) {
130 *(float*)dest = *(float*)src;
131 }else if (typeTime == type) {
132 *(Time*)dest = *(Time*)src;
133 }else if (typeLong == type) {
134 *(long*)dest = *(long*)src;
135 }else if (typeLongLong == type) {
136 *(long long*)dest = *(long long*)src;
137 }else if (typeByteInt == type) {
138 *(char*)dest = *(char*)src;
139 }else if (typeBinary == type) {
140 os::memcpy(dest, src, length);
141 }else if (typeComposite == type) {
142 os::memcpy(dest, src, length);
144 return;
146 void AllDataType::copyZeroVal(void *dest, DataType type, int length)
148 if (typeInt == type )
150 *(int*)dest = 0;
151 }else if (typeString == type || typeVarchar == type)
153 char *d =(char*)dest;
154 d[0] = '\0';
155 }else if (typeShort == type) {
156 *(short*)dest = 0;
157 }else if (typeDouble == type) {
158 *(double*)dest = 0;
159 }else if (typeTimeStamp == type) {
160 ((TimeStamp*)dest)->setDate(0,0,0);
161 ((TimeStamp*)dest)->setTime(0,0,0);
162 }else if (typeDate == type) {
163 ((Date*)dest)->set(0,0,0);
164 }else if (typeFloat == type) {
165 *(float*)dest = 0;
166 }else if (typeTime == type) {
167 ((Time*)dest)->set(0,0,0);
168 }else if (typeLong == type) {
169 *(long*)dest = 0;
170 }else if (typeLongLong == type) {
171 *(long long*)dest = 0;
172 }else if (typeByteInt == type) {
173 *(char*)dest = 0;
174 }else if (typeBinary == type) {
175 os::memset(dest, 0, length);
176 }else if (typeComposite == type) {
177 os::memset(dest, 0, length);
179 return;
183 void AllDataType::addVal(void* dest, void *src, DataType type)
185 if (type == typeInt)
187 *(int*)dest = *(int*)dest + *(int*)src;
188 return;
190 switch(type)
192 case typeInt:
193 *(int*)dest = *(int*)dest + *(int*)src;
194 break;
195 case typeLong:
196 *(long*)dest = *(long*)dest + *(long*)src;
197 break;
198 case typeLongLong:
199 *(long long*)dest = *(long long*)dest + *(long long*)src;
200 break;
201 case typeShort:
202 *(short*)dest = *(short*)dest + *(short*)src;
203 break;
204 case typeByteInt:
205 *(char*)dest = *(char*)dest + *(char*)src;
206 break;
207 case typeDouble:
208 *(double*)dest = *(double*)dest + *(double*)src;
209 break;
210 case typeFloat:
211 *(float*)dest = *(float*)dest + *(float*)src;
212 break;
213 case typeDecimal:
214 //TODO::for porting
215 case typeDate:
216 case typeTime:
217 case typeTimeStamp:
218 case typeBinary:
219 default:
220 break;
222 return;
225 bool AllDataType::compareVal(void *val1, void *val2, ComparisionOp op,
226 DataType type, long length)
228 //Performance optimization.
229 //do not convert compareXXXVal to virtual functions. it takes more time
230 if (typeInt == type)
232 //as int is the heavily used type, hardcoding the compare here itself
233 if (OpEquals == op) {
234 if (*(int*)val1 == *(int*)val2) return true;
235 else return false;
236 }else if (OpLessThan == op) {
237 if (*(int*)val1 < *(int*)val2) return true;
238 else return false;
239 }else if (OpGreaterThan == op) {
240 if (*(int*)val1 > *(int*)val2) return true;
241 else return false;
242 }else if (OpLessThanEquals == op) {
243 if (*(int*)val1 <= *(int*)val2) return true;
244 else return false;
245 }else if (OpGreaterThanEquals == op) {
246 if (*(int*)val1 >= *(int*)val2) return true;
247 else return false;
248 }else if (OpNotEquals == op) {
249 if (*(int*)val1 != *(int*)val2) return true;
250 else return false;
253 }else if(typeString == type || typeVarchar == type) {
254 return AllDataType::compareStringVal(val1, val2, op);
255 } else if (typeShort == type) {
256 return AllDataType::compareShortVal(val1, val2, op);
257 } else if (typeDouble == type) {
258 return AllDataType::compareDoubleVal(val1, val2, op);
259 } else if (typeFloat == type) {
260 return AllDataType::compareFloatVal(val1, val2, op);
261 } else if (typeLong == type) {
262 return AllDataType::compareLongVal(val1, val2, op);
263 } else if (typeLongLong == type) {
264 return AllDataType::compareLongLongVal(val1, val2, op);
265 } else if (typeByteInt == type) {
266 return AllDataType::compareByteIntVal(val1, val2, op);
267 } else if (typeTimeStamp == type) {
268 return AllDataType::compareTimeStampVal(val1, val2, op);
269 } else if (typeDate == type) {
270 return AllDataType::compareDateVal(val1, val2, op);
271 } else if (typeTime == type) {
272 return AllDataType::compareTimeVal(val1, val2, op);
273 } else if (typeBinary == type) {
274 return AllDataType::compareBinaryVal(val1, val2, op, length);
275 } else if (typeComposite == type) {
276 return AllDataType::compareBinaryVal(val1, val2, op, length);
279 ComparisionOp AllDataType::getComparisionOperator(char *str)
281 ComparisionOp op;
282 if (strcmp(str, "<=") == 0)
283 op = OpLessThanEquals;
284 else if (strcmp(str, ">=") == 0)
285 op = OpGreaterThanEquals;
286 else if (strcmp(str, "<") == 0)
287 op = OpLessThan;
288 else if (strcmp(str, ">") == 0)
289 op = OpGreaterThan;
290 else if (strcmp(str, "=") == 0)
291 op = OpEquals;
292 else if (strcmp(str, "!=") == 0 || strcmp(str, "<>") == 0 )
293 op = OpNotEquals;
294 else if (strcasecmp(str, "LIKE") == 0 )
295 op = OpLike;
296 else
297 op = OpInvalidComparisionOp;
298 return op;
301 bool AllDataType::isValidFieldForAvg(DataType type)
303 switch(type) {
304 case typeInt: case typeLong: case typeLongLong: case typeShort:
305 case typeByteInt: case typeDouble: case typeFloat: case typeDecimal:
306 //TODO::for porting
307 return true;
308 case typeDate:
309 case typeTime:
310 case typeTimeStamp:
311 case typeBinary:
312 case typeString:
313 default:
314 return false;