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 /* falls through */
70 default: // TPC_NO
71 type = TYPE_ERROR;
72 } // endswitch xconv
73
74 return type;
75 } else
76 type = TYPE_ERROR;
77
78 if (var) {
79 if (type == TYPE_DATE) {
80 // This is to make the difference between temporal values
81 if (!stricmp(typname, "date"))
82 *var = 'D';
83 else if (!stricmp(typname, "datetime"))
84 *var = 'A';
85 else if (!stricmp(typname, "timestamp"))
86 *var = 'S';
87 else if (!stricmp(typname, "time"))
88 *var = 'T';
89 else if (!stricmp(typname, "year"))
90 *var = 'Y';
91
92 } else if (type == TYPE_STRING) {
93 if (!stricmp(typname, "varchar"))
94 // This is to make the difference between CHAR and VARCHAR
95 *var = 'V';
96
97 } else if (type == TYPE_ERROR && xconv == TPC_SKIP)
98 *var = 'K';
99 else
100 *var = 0;
101
102 } // endif var
103
104 return type;
105 } // end of MYSQLtoPLG
106
107 /************************************************************************/
108 /* Convert from PlugDB type to MySQL type number */
109 /************************************************************************/
PLGtoMYSQL(int type,bool dbf,char v)110 enum enum_field_types PLGtoMYSQL(int type, bool dbf, char v)
111 {
112 enum enum_field_types mytype;
113
114 switch (type) {
115 case TYPE_INT:
116 mytype = MYSQL_TYPE_LONG;
117 break;
118 case TYPE_SHORT:
119 mytype = MYSQL_TYPE_SHORT;
120 break;
121 case TYPE_DOUBLE:
122 mytype = MYSQL_TYPE_DOUBLE;
123 break;
124 case TYPE_DATE:
125 mytype = (dbf) ? MYSQL_TYPE_DATE :
126 (v == 'S') ? MYSQL_TYPE_TIMESTAMP :
127 (v == 'D') ? MYSQL_TYPE_NEWDATE :
128 (v == 'T') ? MYSQL_TYPE_TIME :
129 (v == 'Y') ? MYSQL_TYPE_YEAR : MYSQL_TYPE_DATETIME;
130 break;
131 case TYPE_STRING:
132 mytype = (v) ? MYSQL_TYPE_VARCHAR : MYSQL_TYPE_STRING;
133 break;
134 case TYPE_BIGINT:
135 mytype = MYSQL_TYPE_LONGLONG;
136 break;
137 case TYPE_TINY:
138 mytype = MYSQL_TYPE_TINY;
139 break;
140 case TYPE_DECIM:
141 #if !defined(ALPHA)
142 mytype = MYSQL_TYPE_NEWDECIMAL;
143 #else // ALPHA
144 mytype = MYSQL_TYPE_DECIMAL;
145 #endif // ALPHA
146 break;
147 default:
148 mytype = MYSQL_TYPE_NULL;
149 } // endswitch mytype
150
151 return mytype;
152 } // end of PLGtoMYSQL
153
154 /************************************************************************/
155 /* Convert from PlugDB type to MySQL type name */
156 /************************************************************************/
PLGtoMYSQLtype(int type,bool dbf,char v)157 const char *PLGtoMYSQLtype(int type, bool dbf, char v)
158 {
159 switch (type) {
160 case TYPE_INT: return "INT";
161 case TYPE_SHORT: return "SMALLINT";
162 case TYPE_DOUBLE: return "DOUBLE";
163 case TYPE_DATE: return dbf ? "DATE" :
164 (v == 'S') ? "TIMESTAMP" :
165 (v == 'D') ? "DATE" :
166 (v == 'T') ? "TIME" :
167 (v == 'Y') ? "YEAR" : "DATETIME";
168 case TYPE_STRING: return v ? "VARCHAR" : "CHAR";
169 case TYPE_BIGINT: return "BIGINT";
170 case TYPE_TINY: return "TINYINT";
171 case TYPE_DECIM: return "DECIMAL";
172 default: return (v) ? "VARCHAR" : "CHAR";
173 } // endswitch mytype
174
175 } // end of PLGtoMYSQLtype
176
177 /************************************************************************/
178 /* Convert from MySQL type to PlugDB type number */
179 /************************************************************************/
MYSQLtoPLG(int mytype,char * var)180 int MYSQLtoPLG(int mytype, char *var)
181 {
182 int type, xconv = GetTypeConv();
183
184 switch (mytype) {
185 case MYSQL_TYPE_SHORT:
186 type = TYPE_SHORT;
187 break;
188 case MYSQL_TYPE_LONG:
189 case MYSQL_TYPE_INT24:
190 case MYSQL_TYPE_ENUM: // ???
191 type = TYPE_INT;
192 break;
193 case MYSQL_TYPE_LONGLONG:
194 type = TYPE_BIGINT;
195 break;
196 case MYSQL_TYPE_TINY:
197 type = TYPE_TINY;
198 break;
199 case MYSQL_TYPE_DECIMAL:
200 #if !defined(ALPHA)
201 case MYSQL_TYPE_NEWDECIMAL:
202 #endif // !ALPHA)
203 type = TYPE_DECIM;
204 break;
205 case MYSQL_TYPE_FLOAT:
206 case MYSQL_TYPE_DOUBLE:
207 type = TYPE_DOUBLE;
208 break;
209 case MYSQL_TYPE_TIMESTAMP:
210 case MYSQL_TYPE_DATE:
211 case MYSQL_TYPE_DATETIME:
212 case MYSQL_TYPE_YEAR:
213 case MYSQL_TYPE_TIME:
214 type = TYPE_DATE;
215 break;
216 case MYSQL_TYPE_VAR_STRING:
217 #if !defined(ALPHA)
218 case MYSQL_TYPE_VARCHAR:
219 #endif // !ALPHA)
220 case MYSQL_TYPE_STRING:
221 type = (*var == 'B') ? TYPE_BIN : TYPE_STRING;
222 break;
223 case MYSQL_TYPE_BLOB:
224 case MYSQL_TYPE_TINY_BLOB:
225 case MYSQL_TYPE_MEDIUM_BLOB:
226 case MYSQL_TYPE_LONG_BLOB:
227 if (var) {
228 switch (xconv) {
229 case TPC_YES:
230 if (*var != 'B') {
231 // This is a TEXT column
232 type = TYPE_STRING;
233 *var = 'X';
234 } else
235 type = TYPE_BIN;
236
237 break;
238 case TPC_SKIP:
239 *var = 'K'; // Skip
240 /* falls through */
241 default: // TPC_NO
242 type = TYPE_ERROR;
243 } // endswitch xconv
244
245 return type;
246 } // endif var
247 /* falls through */
248 default:
249 type = TYPE_ERROR;
250 } // endswitch mytype
251
252 if (var) switch (mytype) {
253 // This is to make the difference between CHAR and VARCHAR
254 #if !defined(ALPHA)
255 case MYSQL_TYPE_VARCHAR:
256 #endif // !ALPHA)
257 case MYSQL_TYPE_VAR_STRING: *var = 'V'; break;
258 // This is to make the difference between temporal values
259 case MYSQL_TYPE_TIMESTAMP: *var = 'S'; break;
260 case MYSQL_TYPE_DATE: *var = 'D'; break;
261 case MYSQL_TYPE_DATETIME: *var = 'A'; break;
262 case MYSQL_TYPE_YEAR: *var = 'Y'; break;
263 case MYSQL_TYPE_TIME: *var = 'T'; break;
264 default: *var = 0;
265 } // endswitch mytype
266
267 return type;
268 } // end of MYSQLtoPLG
269
270 /************************************************************************/
271 /* Returns the format corresponding to a MySQL date type number. */
272 /************************************************************************/
MyDateFmt(int mytype)273 PCSZ MyDateFmt(int mytype)
274 {
275 PCSZ fmt;
276
277 switch (mytype) {
278 case MYSQL_TYPE_TIMESTAMP:
279 case MYSQL_TYPE_DATETIME:
280 fmt = "YYYY-MM-DD hh:mm:ss";
281 break;
282 case MYSQL_TYPE_DATE:
283 fmt = "YYYY-MM-DD";
284 break;
285 case MYSQL_TYPE_YEAR:
286 fmt = "YYYY";
287 break;
288 case MYSQL_TYPE_TIME:
289 fmt = "hh:mm:ss";
290 break;
291 default:
292 fmt = NULL;
293 } // endswitch mytype
294
295 return fmt;
296 } // end of MyDateFmt
297
298 /************************************************************************/
299 /* Returns the format corresponding to a MySQL date type name. */
300 /************************************************************************/
MyDateFmt(char * typname)301 PCSZ MyDateFmt(char *typname)
302 {
303 PCSZ fmt;
304
305 if (!stricmp(typname, "datetime") || !stricmp(typname, "timestamp"))
306 fmt = "YYYY-MM-DD hh:mm:ss";
307 else if (!stricmp(typname, "date"))
308 fmt = "YYYY-MM-DD";
309 else if (!stricmp(typname, "year"))
310 fmt = "YYYY";
311 else if (!stricmp(typname, "time"))
312 fmt = "hh:mm:ss";
313 else
314 fmt = NULL;
315
316 return fmt;
317 } // end of MyDateFmt
318
319