1 /************** MyUtil C++ Program Source Code File (.CPP) **************/
2 /* PROGRAM NAME: MYUTIL */
3 /* ------------- */
4 /* Version 1.2 */
5 /* */
6 /* Author Olivier BERTRAND 2014 */
7 /* */
8 /* WHAT THIS PROGRAM DOES: */
9 /* ----------------------- */
10 /* It contains utility functions to convert data types. */
11 /* It can optionally use the embedded MySQL library. */
12 /* */
13 /************************************************************************/
14 #include "my_global.h"
15 #include <mysql.h>
16 #if defined(_WIN32)
17 //#include <windows.h>
18 #else // !_WIN32
19 #include "osutil.h"
20 #endif // !_WIN32
21
22 #include "global.h"
23 #include "plgdbsem.h"
24 //#include "value.h"
25 //#include "valblk.h"
26 #include "myutil.h"
27 #define DLL_EXPORT // Items are exported from this DLL
28
29 //extern "C" int xconv;
30 TYPCONV GetTypeConv(void);
31
32 /************************************************************************/
33 /* Convert from MySQL type name to PlugDB type number */
34 /************************************************************************/
MYSQLtoPLG(char * typname,char * var)35 int MYSQLtoPLG(char *typname, char *var)
36 {
37 int type;
38 TYPCONV xconv = GetTypeConv();
39
40 if (!stricmp(typname, "int") || !stricmp(typname, "mediumint") ||
41 !stricmp(typname, "integer"))
42 type = TYPE_INT;
43 else if (!stricmp(typname, "smallint"))
44 type = TYPE_SHORT;
45 else if (!stricmp(typname, "char") || !stricmp(typname, "varchar") ||
46 !stricmp(typname, "enum") || !stricmp(typname, "set"))
47 type = TYPE_STRING;
48 else if (!stricmp(typname, "double") || !stricmp(typname, "float") ||
49 !stricmp(typname, "real"))
50 type = TYPE_DOUBLE;
51 else if (!stricmp(typname, "decimal") || !stricmp(typname, "numeric"))
52 type = TYPE_DECIM;
53 else if (!stricmp(typname, "date") || !stricmp(typname, "datetime") ||
54 !stricmp(typname, "time") || !stricmp(typname, "timestamp") ||
55 !stricmp(typname, "year"))
56 type = TYPE_DATE;
57 else if (!stricmp(typname, "bigint") || !stricmp(typname, "longlong"))
58 type = TYPE_BIGINT;
59 else if (!stricmp(typname, "tinyint"))
60 type = TYPE_TINY;
61 else if (!stricmp(typname, "text") && var) {
62 switch (xconv) {
63 case TPC_YES:
64 type = TYPE_STRING;
65 *var = 'X';
66 break;
67 case TPC_SKIP:
68 *var = 'K';
69 default: // TPC_NO
70 type = TYPE_ERROR;
71 } // endswitch xconv
72
73 return type;
74 } else
75 type = TYPE_ERROR;
76
77 if (var) {
78 if (type == TYPE_DATE) {
79 // This is to make the difference between temporal values
80 if (!stricmp(typname, "date"))
81 *var = 'D';
82 else if (!stricmp(typname, "datetime"))
83 *var = 'A';
84 else if (!stricmp(typname, "timestamp"))
85 *var = 'S';
86 else if (!stricmp(typname, "time"))
87 *var = 'T';
88 else if (!stricmp(typname, "year"))
89 *var = 'Y';
90
91 } else if (type == TYPE_STRING) {
92 if (!stricmp(typname, "varchar"))
93 // This is to make the difference between CHAR and VARCHAR
94 *var = 'V';
95
96 } else if (type == TYPE_ERROR && xconv == TPC_SKIP)
97 *var = 'K';
98 else
99 *var = 0;
100
101 } // endif var
102
103 return type;
104 } // end of MYSQLtoPLG
105
106 /************************************************************************/
107 /* Convert from PlugDB type to MySQL type number */
108 /************************************************************************/
PLGtoMYSQL(int type,bool dbf,char v)109 enum enum_field_types PLGtoMYSQL(int type, bool dbf, char v)
110 {
111 enum enum_field_types mytype;
112
113 switch (type) {
114 case TYPE_INT:
115 mytype = MYSQL_TYPE_LONG;
116 break;
117 case TYPE_SHORT:
118 mytype = MYSQL_TYPE_SHORT;
119 break;
120 case TYPE_DOUBLE:
121 mytype = MYSQL_TYPE_DOUBLE;
122 break;
123 case TYPE_DATE:
124 mytype = (dbf) ? MYSQL_TYPE_DATE :
125 (v == 'S') ? MYSQL_TYPE_TIMESTAMP :
126 (v == 'D') ? MYSQL_TYPE_NEWDATE :
127 (v == 'T') ? MYSQL_TYPE_TIME :
128 (v == 'Y') ? MYSQL_TYPE_YEAR : MYSQL_TYPE_DATETIME;
129 break;
130 case TYPE_STRING:
131 mytype = (v) ? MYSQL_TYPE_VARCHAR : MYSQL_TYPE_STRING;
132 break;
133 case TYPE_BIGINT:
134 mytype = MYSQL_TYPE_LONGLONG;
135 break;
136 case TYPE_TINY:
137 mytype = MYSQL_TYPE_TINY;
138 break;
139 case TYPE_DECIM:
140 #if !defined(ALPHA)
141 mytype = MYSQL_TYPE_NEWDECIMAL;
142 #else // ALPHA
143 mytype = MYSQL_TYPE_DECIMAL;
144 #endif // ALPHA
145 break;
146 default:
147 mytype = MYSQL_TYPE_NULL;
148 } // endswitch mytype
149
150 return mytype;
151 } // end of PLGtoMYSQL
152
153 /************************************************************************/
154 /* Convert from PlugDB type to MySQL type name */
155 /************************************************************************/
PLGtoMYSQLtype(int type,bool dbf,char v)156 const char *PLGtoMYSQLtype(int type, bool dbf, char v)
157 {
158 switch (type) {
159 case TYPE_INT: return "INT";
160 case TYPE_SHORT: return "SMALLINT";
161 case TYPE_DOUBLE: return "DOUBLE";
162 case TYPE_DATE: return dbf ? "DATE" :
163 (v == 'S') ? "TIMESTAMP" :
164 (v == 'D') ? "DATE" :
165 (v == 'T') ? "TIME" :
166 (v == 'Y') ? "YEAR" : "DATETIME";
167 case TYPE_STRING: return v ? "VARCHAR" : "CHAR";
168 case TYPE_BIGINT: return "BIGINT";
169 case TYPE_TINY: return "TINYINT";
170 case TYPE_DECIM: return "DECIMAL";
171 default: return (v) ? "VARCHAR" : "CHAR";
172 } // endswitch mytype
173
174 } // end of PLGtoMYSQLtype
175
176 /************************************************************************/
177 /* Convert from MySQL type to PlugDB type number */
178 /************************************************************************/
MYSQLtoPLG(int mytype,char * var)179 int MYSQLtoPLG(int mytype, char *var)
180 {
181 int type, xconv = GetTypeConv();
182
183 switch (mytype) {
184 case MYSQL_TYPE_SHORT:
185 type = TYPE_SHORT;
186 break;
187 case MYSQL_TYPE_LONG:
188 case MYSQL_TYPE_INT24:
189 case MYSQL_TYPE_ENUM: // ???
190 type = TYPE_INT;
191 break;
192 case MYSQL_TYPE_LONGLONG:
193 type = TYPE_BIGINT;
194 break;
195 case MYSQL_TYPE_TINY:
196 type = TYPE_TINY;
197 break;
198 case MYSQL_TYPE_DECIMAL:
199 #if !defined(ALPHA)
200 case MYSQL_TYPE_NEWDECIMAL:
201 #endif // !ALPHA)
202 type = TYPE_DECIM;
203 break;
204 case MYSQL_TYPE_FLOAT:
205 case MYSQL_TYPE_DOUBLE:
206 type = TYPE_DOUBLE;
207 break;
208 case MYSQL_TYPE_TIMESTAMP:
209 case MYSQL_TYPE_DATE:
210 case MYSQL_TYPE_DATETIME:
211 case MYSQL_TYPE_YEAR:
212 case MYSQL_TYPE_TIME:
213 type = TYPE_DATE;
214 break;
215 case MYSQL_TYPE_VAR_STRING:
216 #if !defined(ALPHA)
217 case MYSQL_TYPE_VARCHAR:
218 #endif // !ALPHA)
219 case MYSQL_TYPE_STRING:
220 type = (*var == 'B') ? TYPE_BIN : TYPE_STRING;
221 break;
222 case MYSQL_TYPE_BLOB:
223 case MYSQL_TYPE_TINY_BLOB:
224 case MYSQL_TYPE_MEDIUM_BLOB:
225 case MYSQL_TYPE_LONG_BLOB:
226 if (var) {
227 switch (xconv) {
228 case TPC_YES:
229 if (*var != 'B') {
230 // This is a TEXT column
231 type = TYPE_STRING;
232 *var = 'X';
233 } else
234 type = TYPE_BIN;
235
236 break;
237 case TPC_SKIP:
238 *var = 'K'; // Skip
239 default: // TPC_NO
240 type = TYPE_ERROR;
241 } // endswitch xconv
242
243 return type;
244 } // endif var
245
246 default:
247 type = TYPE_ERROR;
248 } // endswitch mytype
249
250 if (var) switch (mytype) {
251 // This is to make the difference between CHAR and VARCHAR
252 #if !defined(ALPHA)
253 case MYSQL_TYPE_VARCHAR:
254 #endif // !ALPHA)
255 case MYSQL_TYPE_VAR_STRING: *var = 'V'; break;
256 // This is to make the difference between temporal values
257 case MYSQL_TYPE_TIMESTAMP: *var = 'S'; break;
258 case MYSQL_TYPE_DATE: *var = 'D'; break;
259 case MYSQL_TYPE_DATETIME: *var = 'A'; break;
260 case MYSQL_TYPE_YEAR: *var = 'Y'; break;
261 case MYSQL_TYPE_TIME: *var = 'T'; break;
262 default: *var = 0;
263 } // endswitch mytype
264
265 return type;
266 } // end of MYSQLtoPLG
267
268 /************************************************************************/
269 /* Returns the format corresponding to a MySQL date type number. */
270 /************************************************************************/
MyDateFmt(int mytype)271 PCSZ MyDateFmt(int mytype)
272 {
273 PCSZ fmt;
274
275 switch (mytype) {
276 case MYSQL_TYPE_TIMESTAMP:
277 case MYSQL_TYPE_DATETIME:
278 fmt = "YYYY-MM-DD hh:mm:ss";
279 break;
280 case MYSQL_TYPE_DATE:
281 fmt = "YYYY-MM-DD";
282 break;
283 case MYSQL_TYPE_YEAR:
284 fmt = "YYYY";
285 break;
286 case MYSQL_TYPE_TIME:
287 fmt = "hh:mm:ss";
288 break;
289 default:
290 fmt = NULL;
291 } // endswitch mytype
292
293 return fmt;
294 } // end of MyDateFmt
295
296 /************************************************************************/
297 /* Returns the format corresponding to a MySQL date type name. */
298 /************************************************************************/
MyDateFmt(char * typname)299 PCSZ MyDateFmt(char *typname)
300 {
301 PCSZ fmt;
302
303 if (!stricmp(typname, "datetime") || !stricmp(typname, "timestamp"))
304 fmt = "YYYY-MM-DD hh:mm:ss";
305 else if (!stricmp(typname, "date"))
306 fmt = "YYYY-MM-DD";
307 else if (!stricmp(typname, "year"))
308 fmt = "YYYY";
309 else if (!stricmp(typname, "time"))
310 fmt = "hh:mm:ss";
311 else
312 fmt = NULL;
313
314 return fmt;
315 } // end of MyDateFmt
316
317