allocator fixes
[csql.git] / include / DataTypeInline.h
blob274c774ec8587e6f2528d55af7226caf82a677b1
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 return;
77 }else if (typeShort == type) {
78 *(short*)dest = *(short*)src;
79 }else if (typeDouble == type) {
80 *(double*)dest = *(double*)src;
81 }else if (typeTimeStamp == type) {
82 *(TimeStamp*)dest = *(TimeStamp*)src;
83 }else if (typeDate == type) {
84 *(Date*)dest = *(Date*)src;
85 }else if (typeFloat == type) {
86 *(float*)dest = *(float*)src;
87 }else if (typeTime == type) {
88 *(Time*)dest = *(Time*)src;
89 }else if (typeLong == type) {
90 *(long*)dest = *(long*)src;
91 }else if (typeLongLong == type) {
92 if(1 == dbFlag)
93 convertToLongLong(dest, src,typeString);
94 else
95 *(long long*)dest = *(long long*)src;
96 }else if (typeByteInt == type) {
97 *(char*)dest = *(char*)src;
98 }else if (typeBinary == type) {
99 os::memcpy(dest, src, length);
100 }else if (typeComposite == type) {
101 os::memcpy(dest, src, length);
103 return;
106 void AllDataType::copyVal(void* dest, void *src, DataType type, int length,int dbFlag)
108 //Performance optimization. putting likely case first
109 if (typeInt == type )
111 *(int*)dest = *(int*)src;
112 return;
113 }else if (typeString == type || typeVarchar == type)
115 strncpy((char*)dest, (char*)src, length);
116 char *d =(char*)dest;
117 d[length-1] = '\0';
118 return;
119 }else if (typeShort == type) {
120 *(short*)dest = *(short*)src;
121 }else if (typeDouble == type) {
122 *(double*)dest = *(double*)src;
123 }else if (typeTimeStamp == type) {
124 *(TimeStamp*)dest = *(TimeStamp*)src;
125 }else if (typeDate == type) {
126 *(Date*)dest = *(Date*)src;
127 }else if (typeFloat == type) {
128 *(float*)dest = *(float*)src;
129 }else if (typeTime == type) {
130 *(Time*)dest = *(Time*)src;
131 }else if (typeLong == type) {
132 *(long*)dest = *(long*)src;
133 }else if (typeLongLong == type) {
134 *(long long*)dest = *(long long*)src;
135 }else if (typeByteInt == type) {
136 *(char*)dest = *(char*)src;
137 }else if (typeBinary == type) {
138 os::memcpy(dest, src, length);
139 }else if (typeComposite == type) {
140 os::memcpy(dest, src, length);
142 return;
144 void AllDataType::copyZeroVal(void *dest, DataType type, int length)
146 if (typeInt == type )
148 *(int*)dest = 0;
149 }else if (typeString == type || typeVarchar == type)
151 char *d =(char*)dest;
152 d[0] = '\0';
153 }else if (typeShort == type) {
154 *(short*)dest = 0;
155 }else if (typeDouble == type) {
156 *(double*)dest = 0;
157 }else if (typeTimeStamp == type) {
158 ((TimeStamp*)dest)->setDate(0,0,0);
159 ((TimeStamp*)dest)->setTime(0,0,0);
160 }else if (typeDate == type) {
161 ((Date*)dest)->set(0,0,0);
162 }else if (typeFloat == type) {
163 *(float*)dest = 0;
164 }else if (typeTime == type) {
165 ((Time*)dest)->set(0,0,0);
166 }else if (typeLong == type) {
167 *(long*)dest = 0;
168 }else if (typeLongLong == type) {
169 *(long long*)dest = 0;
170 }else if (typeByteInt == type) {
171 *(char*)dest = 0;
172 }else if (typeBinary == type) {
173 os::memset(dest, 0, length);
174 }else if (typeComposite == type) {
175 os::memset(dest, 0, length);
177 return;
181 void AllDataType::addVal(void* dest, void *src, DataType type)
183 if (type == typeInt)
185 *(int*)dest = *(int*)dest + *(int*)src;
186 return;
188 switch(type)
190 case typeInt:
191 *(int*)dest = *(int*)dest + *(int*)src;
192 break;
193 case typeLong:
194 *(long*)dest = *(long*)dest + *(long*)src;
195 break;
196 case typeLongLong:
197 *(long long*)dest = *(long long*)dest + *(long long*)src;
198 break;
199 case typeShort:
200 *(short*)dest = *(short*)dest + *(short*)src;
201 break;
202 case typeByteInt:
203 *(char*)dest = *(char*)dest + *(char*)src;
204 break;
205 case typeDouble:
206 *(double*)dest = *(double*)dest + *(double*)src;
207 break;
208 case typeFloat:
209 *(float*)dest = *(float*)dest + *(float*)src;
210 break;
211 case typeDecimal:
212 //TODO::for porting
213 case typeDate:
214 case typeTime:
215 case typeTimeStamp:
216 case typeBinary:
217 default:
218 break;
220 return;
223 bool AllDataType::compareVal(void *val1, void *val2, ComparisionOp op,
224 DataType type, long length)
226 //Performance optimization.
227 //do not convert compareXXXVal to virtual functions. it takes more time
228 if (typeInt == type)
230 //as int is the heavily used type, hardcoding the compare here itself
231 if (OpEquals == op) {
232 if (*(int*)val1 == *(int*)val2) return true;
233 else return false;
234 }else if (OpLessThan == op) {
235 if (*(int*)val1 < *(int*)val2) return true;
236 else return false;
237 }else if (OpGreaterThan == op) {
238 if (*(int*)val1 > *(int*)val2) return true;
239 else return false;
240 }else if (OpLessThanEquals == op) {
241 if (*(int*)val1 <= *(int*)val2) return true;
242 else return false;
243 }else if (OpGreaterThanEquals == op) {
244 if (*(int*)val1 >= *(int*)val2) return true;
245 else return false;
246 }else if (OpNotEquals == op) {
247 if (*(int*)val1 != *(int*)val2) return true;
248 else return false;
251 }else if(typeString == type || typeVarchar == type) {
252 return AllDataType::compareStringVal(val1, val2, op);
253 } else if (typeShort == type) {
254 return AllDataType::compareShortVal(val1, val2, op);
255 } else if (typeDouble == type) {
256 return AllDataType::compareDoubleVal(val1, val2, op);
257 } else if (typeFloat == type) {
258 return AllDataType::compareFloatVal(val1, val2, op);
259 } else if (typeLong == type) {
260 return AllDataType::compareLongVal(val1, val2, op);
261 } else if (typeLongLong == type) {
262 return AllDataType::compareLongLongVal(val1, val2, op);
263 } else if (typeByteInt == type) {
264 return AllDataType::compareByteIntVal(val1, val2, op);
265 } else if (typeTimeStamp == type) {
266 return AllDataType::compareTimeStampVal(val1, val2, op);
267 } else if (typeDate == type) {
268 return AllDataType::compareDateVal(val1, val2, op);
269 } else if (typeTime == type) {
270 return AllDataType::compareTimeVal(val1, val2, op);
271 } else if (typeBinary == type) {
272 return AllDataType::compareBinaryVal(val1, val2, op, length);
273 } else if (typeComposite == type) {
274 return AllDataType::compareBinaryVal(val1, val2, op, length);
277 ComparisionOp AllDataType::getComparisionOperator(char *str)
279 ComparisionOp op;
280 if (strcmp(str, "<=") == 0)
281 op = OpLessThanEquals;
282 else if (strcmp(str, ">=") == 0)
283 op = OpGreaterThanEquals;
284 else if (strcmp(str, "<") == 0)
285 op = OpLessThan;
286 else if (strcmp(str, ">") == 0)
287 op = OpGreaterThan;
288 else if (strcmp(str, "=") == 0)
289 op = OpEquals;
290 else if (strcmp(str, "!=") == 0 || strcmp(str, "<>") == 0 )
291 op = OpNotEquals;
292 else if (strcasecmp(str, "LIKE") == 0 )
293 op = OpLike;
294 else
295 op = OpInvalidComparisionOp;
296 return op;
299 bool AllDataType::isValidFieldForAvg(DataType type)
301 switch(type) {
302 case typeInt: case typeLong: case typeLongLong: case typeShort:
303 case typeByteInt: case typeDouble: case typeFloat: case typeDecimal:
304 //TODO::for porting
305 return true;
306 case typeDate:
307 case typeTime:
308 case typeTimeStamp:
309 case typeBinary:
310 case typeString:
311 default:
312 return false;