File: sqltype.ec

package info (click to toggle)
libdbd-informix-perl 2003.04-3
  • links: PTS
  • area: contrib
  • in suites: etch, etch-m68k, sarge
  • size: 1,232 kB
  • ctags: 467
  • sloc: perl: 7,349; ansic: 5,340; sh: 184; makefile: 58
file content (341 lines) | stat: -rw-r--r-- 7,909 bytes parent folder | download
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
@(#)Purpose:         Convert type and length from Syscolumns to string
@(#)Author:          J Leffler
@(#)Copyright:       1988-1993,1995-98 Jonathan Leffler (JLSS)
@(#)Copyright:       2002              IBM
@(#)Product:         IBM Informix Database Driver for Perl Version 2003.04 (2003-03-05)
*/

/*TABSTOP=4*/
/*LINTLIBRARY*/

#ifndef lint
static const char rcs[] = "@(#)$Id: sqltype.ec,v 100.2 2002/12/06 22:18:25 jleffler Exp $";
#endif

#include <string.h>
#include "esqlc.h"
#include "esqlutil.h"

static const char * const sqltypes[] =
{
	"CHAR",
	"SMALLINT",
	"INTEGER",
	"FLOAT",
	"SMALLFLOAT",
	"DECIMAL",
	"SERIAL",
	"DATE",
	"MONEY",
	"NULL",
	"DATETIME",
	"BYTE",
	"TEXT",
	"VARCHAR",
	"INTERVAL",
	"NCHAR",
	"NVARCHAR",
	"INT8",
	"SERIAL8",
	"SET",
	"MULTISET",
	"LIST",
	"ROW",
	"COLLECTION",
	"ROWREF",
	"[reserved25]",
	"[reserved26]",
	"[reserved27]",
	"[reserved28]",
	"[reserved29]",
	"[reserved30]",
	"[reserved31]",
	"[reserved32]",
	"[reserved33]",
	"[reserved34]",
	"[reserved35]",
	"[reserved36]",
	"[reserved37]",
	"[reserved38]",
	"[reserved39]",
	"FIXED UDT",
	"VARIABLE UDT",
	"REFSER8",
	"LVARCHAR",
	"SENDRECV",
	"BOOLEAN",
	"IMPEXP",
	"IMPEXPBIN",
};

static const char dt_day[] = "DAY";
static const char dt_fraction1[] = "FRACTION(1)";
static const char dt_fraction2[] = "FRACTION(2)";
static const char dt_fraction3[] = "FRACTION(3)";
static const char dt_fraction4[] = "FRACTION(4)";
static const char dt_fraction5[] = "FRACTION(5)";
static const char dt_fraction[] = "FRACTION";
static const char dt_hour[] = "HOUR";
static const char dt_minute[] = "MINUTE";
static const char dt_month[] = "MONTH";
static const char dt_second[] = "SECOND";
static const char dt_unknown[] = "{unknown}";
static const char dt_year[] = "YEAR";

static const char * const dt_fr_ext[] =
{
	dt_year,
	dt_unknown,
	dt_month,
	dt_unknown,
	dt_day,
	dt_unknown,
	dt_hour,
	dt_unknown,
	dt_minute,
	dt_unknown,
	dt_second,
	dt_unknown,
	dt_fraction,
	dt_unknown,
	dt_unknown,
	dt_unknown
};

static const char * const dt_to_ext[] =
{
	dt_year,
	dt_unknown,
	dt_month,
	dt_unknown,
	dt_day,
	dt_unknown,
	dt_hour,
	dt_unknown,
	dt_minute,
	dt_unknown,
	dt_second,
	dt_fraction1,
	dt_fraction2,
	dt_fraction3,
	dt_fraction4,
	dt_fraction5
};

static char	typestr[SQLTYPENAME_BUFSIZ];
static int sqlmode = 0;

/*
** Get/Set Type Formatting mode
** If the mode is set to 1, then sqltypename() formats
** INTERVAL HOUR(6) TO HOUR as INTERVAL HOUR(6).
** Otherwise it uses the standard Informix type name.
*/
int sqltypemode(int mode)
{
	int	oldmode = sqlmode;
	sqlmode = mode;
	return(oldmode);
}

char	*sqltypename(int coltype, int collen, char *buffer)
{
	int		precision;
	int		dt_fr;
	int		dt_to;
	int		dt_ld;
	int		vc_min;
	int		vc_max;
	int		scale;
	int		type = MASKNONULL(coltype);
	char   *start = buffer;

	if (coltype & SQLDISTINCT)
	{
		strcpy(start, "DISTINCT ");
		start += strlen(start);
	}

	switch (type)
	{
	case SQLCHAR:
	case SQLNCHAR:
		sprintf(start, "%s(%d)", sqltypes[type], collen);
		break;

	case SQLSMINT:
	case SQLINT:
	case SQLFLOAT:
	case SQLSMFLOAT:
	case SQLDATE:
	case SQLSERIAL:
	case SQLNULL:
	case SQLTEXT:
	case SQLBYTES:
	case SQLINT8:
	case SQLSERIAL8:
		strcpy(start, sqltypes[type]);
		break;

	/* IUS types -- may need more work in future */
	case SQLSET:
	case SQLLIST:
	case SQLMULTISET:
	case SQLCOLLECTION:
	case SQLROW:
	case SQLROWREF:
		strcpy(start, sqltypes[type]);
		break;

	case SQLDECIMAL:
	case SQLMONEY:
		precision = (collen >> 8) & 0xFF;
		scale = (collen & 0xFF);
		if (scale == 0xFF)
			sprintf(start, "%s(%d)", sqltypes[type], precision);
		else
			sprintf(start, "%s(%d,%d)", sqltypes[type], precision, scale);
		break;

	case SQLVCHAR:
	case SQLNVCHAR:
		vc_min = VCMIN(collen);
		vc_max = VCMAX(collen);
		if (vc_min == 0)
			sprintf(start, "%s(%d)", sqltypes[type], vc_max);
		else
			sprintf(start, "%s(%d,%d)", sqltypes[type], vc_max, vc_min);
		break;

	case SQLDTIME:
		dt_fr = TU_START(collen);
		dt_to = TU_END(collen);
		if (sqlmode != 1)
			sprintf(start, "%s %s TO %s", sqltypes[type], dt_fr_ext[dt_fr],
					dt_to_ext[dt_to]);
		else if (dt_fr == TU_FRAC)
			sprintf(start, "%s %s", sqltypes[type], dt_to_ext[dt_to]);
		else if (dt_fr == dt_to)
			sprintf(start, "%s %s", sqltypes[type], dt_to_ext[dt_to]);
		else
			sprintf(start, "%s %s TO %s", sqltypes[type], dt_fr_ext[dt_fr],
					dt_to_ext[dt_to]);
		break;

	case SQLINTERVAL:
		dt_fr = TU_START(collen);
		dt_to = TU_END(collen);
		dt_ld = TU_FLEN(collen);
		if (sqlmode != 1 && dt_fr == TU_FRAC)
			sprintf(start, "%s %s TO %s", sqltypes[type],
					dt_fr_ext[dt_fr], dt_to_ext[dt_to]);
		else if (sqlmode != 1)
			sprintf(start, "%s %s(%d) TO %s", sqltypes[type],
					dt_fr_ext[dt_fr], dt_ld, dt_to_ext[dt_to]);
		else if (dt_fr == TU_FRAC)
			sprintf(start, "%s %s", sqltypes[type], dt_to_ext[dt_to]);
		else if (dt_fr == dt_to)
			sprintf(start, "%s %s(%d)", sqltypes[type], dt_to_ext[dt_to],
					dt_ld);
		else
			sprintf(start, "%s %s(%d) TO %s", sqltypes[type],
					dt_fr_ext[dt_fr], dt_ld, dt_to_ext[dt_to]);
		break;

	default:
		sprintf(start, "Unknown (type %d, len %d)", coltype, collen);
		ESQLC_VERSION_CHECKER();
		break;
	}
	return(buffer);
}

/* For backwards compatability only */
/* Not thread-safe because it uses static return data */
const char	*sqltype(int coltype, int collen)
{
	return(sqltypename(coltype, collen, typestr));
}

#ifdef TEST

#define DIM(x)	(sizeof(x)/sizeof(*(x)))

typedef struct	Typelist
{
	char	*code;
	int		coltype;
	int		collen;
}	Typelist;

static Typelist	types[] =
{
	{	"serial",							262,		4		},
	{	"char",								0,			10		},
	{	"date",								7,			4		},
	{	"decimal",							5,			4351	},
	{	"decimal(16)",						5,			4351	},
	{	"decimal(32,14)",					5,			8206	},
	{	"float",							3,			8		},
	{	"integer",							2,			4		},
	{	"money",							8,			4098	},
	{	"money(16,2)",						8,			4098	},
	{	"smallfloat",						4,			4		},
	{	"smallint",							1,			2		},
	{	"varchar(128)",						13,			128		},
	{	"varchar(128,64)",					13,			16512	},
	{	"datetime day to day",				10,			580		},
	{	"datetime hour to fraction(3)",		10,			2413	},
	{	"datetime minute to fraction(3)",	10,			1933	},
	{	"datetime month to fraction(3)",	10,			3373	},
	{	"datetime second to fraction(5)",	10,			1967	},
	{	"datetime second to second",		10,			682		},
	{	"datetime year to fraction(3)",		10,			4365	},
	{	"datetime year to fraction(5)",		10,			4879	},
	{	"datetime year to year",			10,			1024	},
	{	"interval day(4) to fraction(3)",	14,			3405	},
	{	"interval day(9) to fraction(5)",	14,			5199	},
	{	"interval day to fraction(5)",		14,			3407	},
	{	"interval hour(4) to fraction(3)",	14,			2925	},
	{	"interval hour(6) to fraction(5)",	14,			3951	},
	{	"interval hour to fraction(5)",		14,			2927	},
	{	"byte in table",					11,			56		},
	{	"text in table",					12,			56		},
	{	"datetime fraction to fraction", 10,			973		},
	{	"datetime fraction to fraction(1)", 10,			459		},
	{	"datetime fraction to fraction(2)", 10,			716		},
	{	"datetime fraction to fraction(3)", 10,			973		},
	{	"datetime fraction to fraction(4)", 10,			1230	},
	{	"datetime fraction to fraction(5)", 10,			1487	},
	{	"interval fraction to fraction",	14,			973		},
	{	"interval fraction to fraction(1)", 14,			459		},
	{	"interval fraction to fraction(2)", 14,			716		},
	{	"interval fraction to fraction(3)", 14,			973		},
	{	"interval fraction to fraction(4)", 14,			1230	},
	{	"interval fraction to fraction(5)", 14,			1487	},
};

static void printtypes(int mode)
{
	int             i;

	sqltypemode(mode);
	printf("%-32s %4s %6s   %s\n", "Code", "Type", "Length", "Full type");
	for (i = 0; i < DIM(types); i++)
	{
		printf("%-32s %4d %6d = %s\n",
			   types[i].code, types[i].coltype, types[i].collen,
			   sqltype(types[i].coltype, types[i].collen));
		fflush(stdout);
	}
}

int main()
{
	printtypes(0);
	printtypes(1);
	return (0);
}

#endif	/* TEST */