1 /***************************************************************************
3 * Copyright (C) Lakshya Solutions Ltd. All rights reserved. *
5 ***************************************************************************/
10 long AllDataType::size(DataType type
, int length
)
12 if (type
== typeInt
) return sizeof(int);
13 else if (type
== typeString
) return length
;
25 size
= sizeof(long long);
34 size
= sizeof(double);
41 //fldDef.length_ = sizeof(long double);
50 size
= sizeof(TimeStamp
);
63 void AllDataType::cachecopyVal(void* dest
, void *src
, DataType type
, int length
,int dbFlag
)
67 *(int*)dest
= *(int*)src
;
69 }else if (typeString
== type
)
71 //null is always put at the last byte by insert
72 //so using strcpy is safe
73 //strcpy((char*)dest, (char*)src);
74 Util::trimRight((char*)src
);
75 strncpy((char*)dest
, (char*)src
, length
);
76 //strncpy((char*)dest, (char*)src, length);
77 //char *d =(char*)dest;
80 }else if (typeShort
== type
) {
81 *(short*)dest
= *(short*)src
;
82 }else if (typeDouble
== type
) {
83 *(double*)dest
= *(double*)src
;
84 }else if (typeTimeStamp
== type
) {
85 *(TimeStamp
*)dest
= *(TimeStamp
*)src
;
86 }else if (typeDate
== type
) {
87 *(Date
*)dest
= *(Date
*)src
;
88 }else if (typeFloat
== type
) {
89 *(float*)dest
= *(float*)src
;
90 }else if (typeTime
== type
) {
91 *(Time
*)dest
= *(Time
*)src
;
92 }else if (typeLong
== type
) {
93 *(long*)dest
= *(long*)src
;
94 }else if (typeLongLong
== type
) {
96 convertToLongLong(dest
, src
,typeString
);
98 *(long long*)dest
= *(long long*)src
;
99 }else if (typeByteInt
== type
) {
100 *(char*)dest
= *(char*)src
;
101 }else if (typeBinary
== type
) {
102 os::memcpy(dest
, src
, length
);
103 }else if (typeComposite
== type
) {
104 os::memcpy(dest
, src
, length
);
109 void AllDataType::copyVal(void* dest
, void *src
, DataType type
, int length
,int dbFlag
)
111 //Performance optimization. putting likely case first
112 if (typeInt
== type
)
114 *(int*)dest
= *(int*)src
;
116 }else if (typeString
== type
)
118 //null is always put at the last byte by insert
119 //so using strcpy is safe
120 //strcpy((char*)dest, (char*)src);
121 memcpy(dest
, src
, length
);
122 //strncpy((char*)dest, (char*)src, length);
123 char *d
=(char*)dest
;
126 }else if (typeShort
== type
) {
127 *(short*)dest
= *(short*)src
;
128 }else if (typeDouble
== type
) {
129 *(double*)dest
= *(double*)src
;
130 }else if (typeTimeStamp
== type
) {
131 *(TimeStamp
*)dest
= *(TimeStamp
*)src
;
132 }else if (typeDate
== type
) {
133 *(Date
*)dest
= *(Date
*)src
;
134 }else if (typeFloat
== type
) {
135 *(float*)dest
= *(float*)src
;
136 }else if (typeTime
== type
) {
137 *(Time
*)dest
= *(Time
*)src
;
138 }else if (typeLong
== type
) {
139 *(long*)dest
= *(long*)src
;
140 }else if (typeLongLong
== type
) {
141 *(long long*)dest
= *(long long*)src
;
142 }else if (typeByteInt
== type
) {
143 *(char*)dest
= *(char*)src
;
144 }else if (typeBinary
== type
) {
145 os::memcpy(dest
, src
, length
);
146 }else if (typeComposite
== type
) {
147 os::memcpy(dest
, src
, length
);
151 void AllDataType::copyZeroVal(void *dest
, DataType type
, int length
)
153 if (typeInt
== type
)
156 }else if (typeString
== type
)
158 char *d
=(char*)dest
;
160 }else if (typeShort
== type
) {
162 }else if (typeDouble
== type
) {
164 }else if (typeTimeStamp
== type
) {
165 ((TimeStamp
*)dest
)->setDate(0,0,0);
166 ((TimeStamp
*)dest
)->setTime(0,0,0);
167 }else if (typeDate
== type
) {
168 ((Date
*)dest
)->set(0,0,0);
169 }else if (typeFloat
== type
) {
171 }else if (typeTime
== type
) {
172 ((Time
*)dest
)->set(0,0,0);
173 }else if (typeLong
== type
) {
175 }else if (typeLongLong
== type
) {
176 *(long long*)dest
= 0;
177 }else if (typeByteInt
== type
) {
179 }else if (typeBinary
== type
) {
180 os::memset(dest
, 0, length
);
181 }else if (typeComposite
== type
) {
182 os::memset(dest
, 0, length
);
188 void AllDataType::addVal(void* dest
, void *src
, DataType type
)
192 *(int*)dest
= *(int*)dest
+ *(int*)src
;
198 *(int*)dest
= *(int*)dest
+ *(int*)src
;
201 *(long*)dest
= *(long*)dest
+ *(long*)src
;
204 *(long long*)dest
= *(long long*)dest
+ *(long long*)src
;
207 *(short*)dest
= *(short*)dest
+ *(short*)src
;
210 *(char*)dest
= *(char*)dest
+ *(char*)src
;
213 *(double*)dest
= *(double*)dest
+ *(double*)src
;
216 *(float*)dest
= *(float*)dest
+ *(float*)src
;
230 bool AllDataType::compareVal(void *val1
, void *val2
, ComparisionOp op
,
231 DataType type
, long length
)
233 //Performance optimization.
234 //do not convert compareXXXVal to virtual functions. it takes more time
237 //as int is the heavily used type, hardcoding the compare here itself
238 if (OpEquals
== op
) {
239 if (*(int*)val1
== *(int*)val2
) return true;
241 }else if (OpLessThan
== op
) {
242 if (*(int*)val1
< *(int*)val2
) return true;
244 }else if (OpGreaterThan
== op
) {
245 if (*(int*)val1
> *(int*)val2
) return true;
247 }else if (OpLessThanEquals
== op
) {
248 if (*(int*)val1
<= *(int*)val2
) return true;
250 }else if (OpGreaterThanEquals
== op
) {
251 if (*(int*)val1
>= *(int*)val2
) return true;
253 }else if (OpNotEquals
== op
) {
254 if (*(int*)val1
!= *(int*)val2
) return true;
258 }else if(typeString
== type
) {
259 return AllDataType::compareStringVal(val1
, val2
, op
);
260 } else if (typeShort
== type
) {
261 return AllDataType::compareShortVal(val1
, val2
, op
);
262 } else if (typeDouble
== type
) {
263 return AllDataType::compareDoubleVal(val1
, val2
, op
);
264 } else if (typeFloat
== type
) {
265 return AllDataType::compareFloatVal(val1
, val2
, op
);
266 } else if (typeLong
== type
) {
267 return AllDataType::compareLongVal(val1
, val2
, op
);
268 } else if (typeLongLong
== type
) {
269 return AllDataType::compareLongLongVal(val1
, val2
, op
);
270 } else if (typeByteInt
== type
) {
271 return AllDataType::compareByteIntVal(val1
, val2
, op
);
272 } else if (typeTimeStamp
== type
) {
273 return AllDataType::compareTimeStampVal(val1
, val2
, op
);
274 } else if (typeDate
== type
) {
275 return AllDataType::compareDateVal(val1
, val2
, op
);
276 } else if (typeTime
== type
) {
277 return AllDataType::compareTimeVal(val1
, val2
, op
);
278 } else if (typeBinary
== type
) {
279 return AllDataType::compareBinaryVal(val1
, val2
, op
, length
);
280 } else if (typeComposite
== type
) {
281 return AllDataType::compareBinaryVal(val1
, val2
, op
, length
);
284 ComparisionOp
AllDataType::getComparisionOperator(char *str
)
287 if (strcmp(str
, "<=") == 0)
288 op
= OpLessThanEquals
;
289 else if (strcmp(str
, ">=") == 0)
290 op
= OpGreaterThanEquals
;
291 else if (strcmp(str
, "<") == 0)
293 else if (strcmp(str
, ">") == 0)
295 else if (strcmp(str
, "=") == 0)
297 else if (strcmp(str
, "!=") == 0 || strcmp(str
, "<>") == 0 )
299 else if (strcasecmp(str
, "LIKE") == 0 )
302 op
= OpInvalidComparisionOp
;
306 bool AllDataType::isValidFieldForAvg(DataType type
)
309 case typeInt
: case typeLong
: case typeLongLong
: case typeShort
:
310 case typeByteInt
: case typeDouble
: case typeFloat
: case typeDecimal
: