- Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathCoercion.cpp
371 lines (318 loc) · 9.08 KB
/
Coercion.cpp
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
* PROGRAM: JRD access method
* MODULE: Coercion.cpp
* DESCRIPTION: Automatically coercing user datatypes
*
* The contents of this file are subject to the Initial
* Developer's Public License Version 1.0 (the "License");
* you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
* http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
*
* Software distributed under the License is distributed AS IS,
* WITHOUT WARRANTY OF ANY KIND, either express or implied.
* See the License for the specific language governing rights
* and limitations under the License.
*
* The Original Code was created by Alex Peshkov
* for the Firebird Open Source RDBMS project.
*
* Copyright (c) 2019 Alex Peshkov <peshkoff@mail.ru>
* and all contributors signed below.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
*
*/
#include"firebird.h"
#include"../jrd/Coercion.h"
#include"../jrd/cvt_proto.h"
#include"../dsql/dsql.h"
#include"../dsql/make_proto.h"
#include"../jrd/align.h"
#include"../jrd/DataTypeUtil.h"
usingnamespaceJrd;
usingnamespaceFirebird;
staticconst USHORT FROM_MASK = FLD_has_len | FLD_has_chset | FLD_has_scale |
FLD_has_sub | FLD_has_prec;
staticconst USHORT TO_MASK = FLD_has_len | FLD_has_chset | FLD_has_scale |
FLD_legacy | FLD_native | FLD_has_sub | FLD_has_prec | FLD_extended;
boolCoercionArray::coerce(thread_db* tdbb, dsc* d, unsigned startItem) const
{
// move down through array to ensure correct order: newer rule overrides older one
for (unsigned n = getCount(); n-- > startItem; )
{
if (getElement(n).coerce(tdbb, d))
returntrue;
}
returnfalse;
}
voidCoercionArray::setRule(const TypeClause* from, const TypeClause *to)
{
CoercionRule newRule;
newRule.setRule(from, to);
for (unsigned n = 0; n < getCount(); ++n)
{
if (getElement(n) == newRule)
{
remove(n);
break;
}
}
add(newRule);
}
voidCoercionRule::raiseError()
{
// Do not use ERR_post here - old error should be overwritten
(Arg::Gds(isc_bind_convert) << fromDsc.typeToText() << toDsc.typeToText()).raise();
}
voidCoercionRule::setRule(const TypeClause* from, const TypeClause *to)
{
fromMask = from->flags & FROM_MASK;
DsqlDescMaker::fromField(&fromDsc, from);
toMask = to->flags & TO_MASK;
DsqlDescMaker::fromField(&toDsc, to);
// Check for datatype compatibility
// No checks for special case
if (toMask & (FLD_native | FLD_legacy))
return;
// Extending timezone info
if (toMask & FLD_extended)
{
if (fromDsc.isDateTimeTz())
return;
raiseError();
}
// Exceptions - enable blob2blob & blob2string casts
if ((toDsc.dsc_dtype == dtype_blob && fromDsc.isText()) ||
(fromDsc.dsc_dtype == dtype_blob && toDsc.isText()) ||
(toDsc.isBlob() && fromDsc.isBlob()))
{
return;
}
// Disable the rest of casts with blobs
if (toDsc.isBlob() || fromDsc.isBlob())
raiseError();
// Generic check
classBufSize
{
public:
staticconstexprunsignedmakeSize(unsigned len)
{
return (len + FB_ALIGNMENT) * 2;
}
};
constunsigned DATASIZE = 256;
HalfStaticArray<UCHAR, BufSize::makeSize(DATASIZE)> buffer;
unsigned datasize = MAX(toMask & FLD_has_len ? toDsc.dsc_length : 0,
fromMask & FLD_has_len ? fromDsc.dsc_length : 0);
datasize = MAX(DATASIZE, datasize);
UCHAR* buf = buffer.getBuffer(BufSize::makeSize(datasize));
memset(buf, 0, buffer.getCount());
toDsc.dsc_address = FB_ALIGN(buf, FB_ALIGNMENT);
if (! (toMask & FLD_has_len))
{
toDsc.dsc_length = datasize - 2;
}
fromDsc.dsc_address = FB_ALIGN(toDsc.dsc_address + datasize, FB_ALIGNMENT);
try
{
CVT_move(&fromDsc, &toDsc, 0);
}
catch(const Exception&)
{
raiseError();
}
}
dsc* CoercionRule::makeLegacy(USHORT mask)
{
toMask = FLD_legacy;
fromMask = mask;
return &fromDsc;
}
bool CoercionRule::operator==(const CoercionRule& rule) const
{
if (fromMask != rule.fromMask)
returnfalse;
returnmatch(&rule.fromDsc);
}
boolCoercionRule::match(const dsc* d) const
{
// check for exact match (taking flags into an account)
if ((d->dsc_dtype == fromDsc.dsc_dtype) &&
((d->dsc_length == fromDsc.dsc_length) || (!(fromMask & FLD_has_len))) &&
((d->getCharSet() == fromDsc.getCharSet()) || (!(fromMask & FLD_has_chset))) &&
((d->getSubType() == fromDsc.getSubType()) || (!(fromMask & FLD_has_sub))) &&
((d->dsc_scale == fromDsc.dsc_scale) || (!(fromMask & FLD_has_scale))))
{
returntrue;
}
// check for inexact datatype match when FLD_has_prec is not set
if (!(fromMask & FLD_has_prec))
{
switch(fromDsc.dsc_dtype)
{
case dtype_dec64:
case dtype_dec128:
if (DTYPE_IS_DECFLOAT(d->dsc_dtype))
returntrue;
break;
case dtype_short:
case dtype_long:
case dtype_int64:
case dtype_int128:
if (d->isExact() && (fromMask & FLD_has_sub) && (d->dsc_sub_type == fromDsc.dsc_sub_type))
returntrue;
break;
case dtype_timestamp_tz:
case dtype_sql_time_tz:
if (d->isDateTimeTz())
returntrue;
break;
}
}
returnfalse;
}
staticconst USHORT COMPATIBLE_TEXT = 1;
staticconst USHORT COMPATIBLE_INT = 2;
staticconst USHORT subTypeCompatibility[DTYPE_TYPE_MAX] =
{
0, // dtype_unknown
COMPATIBLE_TEXT, // dtype_text
0, // dtype_cstring
COMPATIBLE_TEXT, // dtype_varying
0,
0,
0, // dtype_packed
0, // dtype_byte
COMPATIBLE_INT, // dtype_short -32768
COMPATIBLE_INT, // dtype_long -2147483648
0, // dtype_quad -9223372036854775808
0, // dtype_real -1.23456789e+12
0, // dtype_double -1.2345678901234567e+123
0, // dtype_d_float (believed to have this range) -1.2345678901234567e+123
0, // dtype_sql_date YYYY-MM-DD
0, // dtype_sql_time HH:MM:SS.MMMM
0, // dtype_timestamp YYYY-MM-DD HH:MM:SS.MMMM
0, // dtype_blob FFFF:FFFF
0, // dtype_array FFFF:FFFF
COMPATIBLE_INT, // dtype_int64 -9223372036854775808
0, // dtype_dbkey
0, // dtype_boolean
0, // dtype_dec64 1 + 1 + 1 + 1 + 16(34) + 3(4)
0, // dtype_dec128 +- . e +- coeff + exp
COMPATIBLE_INT, // dtype_int128
0, // dtype_sql_time_tz HH:MM:SS.MMMM +NN:NN
0, // dtype_timestamp_tz YYYY-MM-DD HH:MM:SS.MMMM +NN:NN
0, // dtype_ex_time_tz HH:MM:SS.MMMM +NN:NN
0, // dtype_ex_timestamp_tz YYYY-MM-DD HH:MM:SS.MMMM +NN:NN
};
boolCoercionRule::coerce(thread_db* tdbb, dsc* d) const
{
// check does descriptor match FROM clause
if (! match(d))
returnfalse;
// native binding - do not touch descriptor at all
if (toMask & FLD_native)
returntrue;
// process legacy case
if (toMask & FLD_legacy)
{
bool found = true;
switch(d->dsc_dtype)
{
case dtype_dec64:
case dtype_dec128:
d->dsc_dtype = dtype_double;
d->dsc_length = sizeof(double);
break;
case dtype_sql_time_tz:
d->dsc_dtype = dtype_sql_time;
d->dsc_length = sizeof(ISC_TIME);
break;
case dtype_timestamp_tz:
d->dsc_dtype = dtype_timestamp;
d->dsc_length = sizeof(ISC_TIMESTAMP);
break;
case dtype_int128:
d->dsc_dtype = dtype_int64;
d->dsc_length = sizeof(SINT64);
break;
case dtype_boolean:
d->dsc_dtype = dtype_text;
d->dsc_length = 5;
break;
default:
found = false;
break;
}
return found;
}
// extending time zone
if (toMask & FLD_extended)
{
bool found = true;
switch(d->dsc_dtype)
{
case dtype_timestamp_tz:
d->dsc_dtype = dtype_ex_timestamp_tz;
d->dsc_length = sizeof(ISC_TIMESTAMP_TZ_EX);
break;
case dtype_sql_time_tz:
d->dsc_dtype = dtype_ex_time_tz;
d->dsc_length = sizeof(ISC_TIME_TZ_EX);
break;
default:
found = false;
break;
}
return found;
}
// final pass - order is important
constauto srcCharSet = d->getCharSet();
// scale
if (toMask & FLD_has_scale)
d->dsc_scale = toDsc.dsc_scale;
elseif (!(DTYPE_IS_EXACT(d->dsc_dtype) && DTYPE_IS_EXACT(toDsc.dsc_dtype)))
d->dsc_scale = 0;
// subtype
if (toMask & FLD_has_sub ||
d->dsc_dtype >= DTYPE_TYPE_MAX || toDsc.dsc_dtype >= DTYPE_TYPE_MAX ||
subTypeCompatibility[d->dsc_dtype] == 0 ||
subTypeCompatibility[d->dsc_dtype] != subTypeCompatibility[toDsc.dsc_dtype])
{
d->dsc_sub_type = toDsc.dsc_sub_type;
}
// length
if (toMask & FLD_has_len)
d->dsc_length = toDsc.dsc_length;
// type
if (toMask & FLD_has_prec ||
subTypeCompatibility[d->dsc_dtype] != COMPATIBLE_INT ||
subTypeCompatibility[toDsc.dsc_dtype] != COMPATIBLE_INT)
{
if (!(toMask & FLD_has_len))
{
if (!type_lengths[toDsc.dsc_dtype])
{
fb_assert(toDsc.isText());
d->dsc_length = d->getStringLength();
}
else
d->dsc_length = type_lengths[toDsc.dsc_dtype];
}
d->dsc_dtype = toDsc.dsc_dtype;
}
// charset
if (toMask & FLD_has_chset)
d->setTextType(toDsc.getTextType());
if (d->isText())
d->dsc_length = DataTypeUtil(tdbb).convertLength(d->dsc_length, srcCharSet, toDsc.getCharSet());
// varchar length
if (d->dsc_dtype == dtype_varying && !(toMask & FLD_has_len))
d->dsc_length += sizeof(USHORT);
// subtype - special processing for BLOBs
if (toMask & FLD_has_sub)
d->setBlobSubType(toDsc.getBlobSubType());
returntrue;
}