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(__WIN__)
17//#include <windows.h>
18#else // !__WIN__
19#include "osutil.h"
20#endif // !__WIN__
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;
30TYPCONV GetTypeConv(void);
31
32/************************************************************************/
33/* Convert from MySQL type name to PlugDB type number */
34/************************************************************************/
35int 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/************************************************************************/
109enum 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/************************************************************************/
156const 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 "CHAR(0)";
172 } // endswitch mytype
173
174 return "CHAR(0)";
175 } // end of PLGtoMYSQLtype
176
177/************************************************************************/
178/* Convert from MySQL type to PlugDB type number */
179/************************************************************************/
180int 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 default: // TPC_NO
241 type = TYPE_ERROR;
242 } // endswitch xconv
243
244 return type;
245 } // endif var
246
247 default:
248 type = TYPE_ERROR;
249 } // endswitch mytype
250
251 if (var) switch (mytype) {
252 // This is to make the difference between CHAR and VARCHAR
253#if !defined(ALPHA)
254 case MYSQL_TYPE_VARCHAR:
255#endif // !ALPHA)
256 case MYSQL_TYPE_VAR_STRING: *var = 'V'; break;
257 // This is to make the difference between temporal values
258 case MYSQL_TYPE_TIMESTAMP: *var = 'S'; break;
259 case MYSQL_TYPE_DATE: *var = 'D'; break;
260 case MYSQL_TYPE_DATETIME: *var = 'A'; break;
261 case MYSQL_TYPE_YEAR: *var = 'Y'; break;
262 case MYSQL_TYPE_TIME: *var = 'T'; break;
263 default: *var = 0;
264 } // endswitch mytype
265
266 return type;
267 } // end of MYSQLtoPLG
268
269/************************************************************************/
270/* Returns the format corresponding to a MySQL date type number. */
271/************************************************************************/
272PCSZ MyDateFmt(int mytype)
273 {
274 PCSZ fmt;
275
276 switch (mytype) {
277 case MYSQL_TYPE_TIMESTAMP:
278 case MYSQL_TYPE_DATETIME:
279 fmt = "YYYY-MM-DD hh:mm:ss";
280 break;
281 case MYSQL_TYPE_DATE:
282 fmt = "YYYY-MM-DD";
283 break;
284 case MYSQL_TYPE_YEAR:
285 fmt = "YYYY";
286 break;
287 case MYSQL_TYPE_TIME:
288 fmt = "hh:mm:ss";
289 break;
290 default:
291 fmt = NULL;
292 } // endswitch mytype
293
294 return fmt;
295 } // end of MyDateFmt
296
297/************************************************************************/
298/* Returns the format corresponding to a MySQL date type name. */
299/************************************************************************/
300PCSZ MyDateFmt(char *typname)
301 {
302 PCSZ fmt;
303
304 if (!stricmp(typname, "datetime") || !stricmp(typname, "timestamp"))
305 fmt = "YYYY-MM-DD hh:mm:ss";
306 else if (!stricmp(typname, "date"))
307 fmt = "YYYY-MM-DD";
308 else if (!stricmp(typname, "year"))
309 fmt = "YYYY";
310 else if (!stricmp(typname, "time"))
311 fmt = "hh:mm:ss";
312 else
313 fmt = NULL;
314
315 return fmt;
316 } // end of MyDateFmt
317
318