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