-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathfirebird_utils.cpp
More file actions
422 lines (367 loc) · 13.5 KB
/
firebird_utils.cpp
File metadata and controls
422 lines (367 loc) · 13.5 KB
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Simonov Denis <sim-mail@list.ru> |
| Author: Martins Lazdans <marrtins@dqdp.net> |
+----------------------------------------------------------------------+
*/
extern "C" {
#include <ibase.h>
#include "php.h"
#include "firebird_utils.h"
#include "php_ibase_includes.h"
#include "ext/date/php_date.h"
#include "zend_interfaces.h"
}
#if FB_API_VER >= 30
#include <firebird/Interface.h>
#include <cstring>
/* Returns the client version. 0 bytes are minor version, 1 bytes are major version. */
extern "C" unsigned fbu_get_client_version(void *master_ptr)
{
Firebird::IMaster* master = (Firebird::IMaster*)master_ptr;
Firebird::IUtil* util = master->getUtilInterface();
return util->getClientVersion();
}
extern "C" ISC_TIME fbu_encode_time(void *master_ptr, unsigned hours, unsigned minutes, unsigned seconds, unsigned fractions)
{
Firebird::IMaster* master = (Firebird::IMaster*)master_ptr;
Firebird::IUtil* util = master->getUtilInterface();
return util->encodeTime(hours, minutes, seconds, fractions);
}
extern "C" ISC_DATE fbu_encode_date(void *master_ptr, unsigned year, unsigned month, unsigned day)
{
Firebird::IMaster* master = (Firebird::IMaster*)master_ptr;
Firebird::IUtil* util = master->getUtilInterface();
return util->encodeDate(year, month, day);
}
extern "C" void fbu_decode_time(void *master_ptr, ISC_TIME time, unsigned* hours, unsigned* minutes, unsigned* seconds, unsigned* fractions)
{
Firebird::IMaster* master = (Firebird::IMaster*)master_ptr;
Firebird::IUtil* util = master->getUtilInterface();
util->decodeTime(time, hours, minutes, seconds, fractions);
}
extern "C" void fbu_decode_date(void *master_ptr, ISC_DATE date, unsigned* year, unsigned* month, unsigned* day)
{
Firebird::IMaster* master = (Firebird::IMaster*)master_ptr;
Firebird::IUtil* util = master->getUtilInterface();
util->decodeDate(date, year, month, day);
}
#else
extern "C" ISC_TIME fbu_encode_time(void *master_ptr, unsigned hours, unsigned minutes, unsigned seconds, unsigned fractions)
{
struct tm t = {};
t.tm_hour = (int)hours;
t.tm_min = (int)minutes;
t.tm_sec = (int)seconds;
ISC_TIME result;
isc_encode_sql_time(&t, &result);
return result;
}
extern "C" ISC_DATE fbu_encode_date(void *master_ptr, unsigned year, unsigned month, unsigned day)
{
struct tm t = {};
t.tm_year = (int)year - 1900;
t.tm_mon = (int)month - 1;
t.tm_mday = (int)day;
ISC_DATE result;
isc_encode_sql_date(&t, &result);
return result;
}
extern "C" void fbu_decode_time(void *master_ptr, ISC_TIME time, unsigned* hours, unsigned* minutes, unsigned* seconds, unsigned* fractions)
{
struct tm t = {};
isc_decode_sql_time(&time, &t);
*hours = (unsigned)t.tm_hour;
*minutes = (unsigned)t.tm_min;
*seconds = (unsigned)t.tm_sec;
*fractions = 0;
}
extern "C" void fbu_decode_date(void *master_ptr, ISC_DATE date, unsigned* year, unsigned* month, unsigned* day)
{
struct tm t = {};
isc_decode_sql_date(&date, &t);
*year = (unsigned)(t.tm_year + 1900);
*month = (unsigned)(t.tm_mon + 1);
*day = (unsigned)t.tm_mday;
}
#endif // FB_API_VER >= 30
#if FB_API_VER >= 40
static void fbu_copy_status(const ISC_STATUS* from, ISC_STATUS* to, size_t maxLength)
{
for(size_t i=0; i < maxLength; ++i) {
memcpy(to + i, from + i, sizeof(ISC_STATUS));
if (from[i] == isc_arg_end) {
break;
}
}
}
extern "C" void fbu_encode_time_tz(void *master_ptr, ISC_TIME_TZ* timeTz, unsigned hours, unsigned minutes, unsigned seconds, unsigned fractions, const char* timeZone)
{
Firebird::IMaster* master = (Firebird::IMaster*)master_ptr;
Firebird::IUtil* util = master->getUtilInterface();
Firebird::IStatus* status = master->getStatus();
Firebird::CheckStatusWrapper st(status);
util->encodeTimeTz(&st, timeTz, hours, minutes, seconds, fractions, timeZone);
}
extern "C" void fbu_encode_timestamp_tz(void *master_ptr, ISC_TIMESTAMP_TZ* timeStampTz, unsigned year, unsigned month, unsigned day, unsigned hours, unsigned minutes, unsigned seconds, unsigned fractions, const char* timeZone)
{
Firebird::IMaster* master = (Firebird::IMaster*)master_ptr;
Firebird::IUtil* util = master->getUtilInterface();
Firebird::IStatus* status = master->getStatus();
Firebird::CheckStatusWrapper st(status);
util->encodeTimeStampTz(&st, timeStampTz, year, month, day, hours, minutes, seconds, fractions, timeZone);
}
/* Decodes a time with time zone into its time components. */
extern "C" void fbu_decode_time_tz(void *master_ptr, const ISC_TIME_TZ* timeTz, unsigned* hours, unsigned* minutes, unsigned* seconds, unsigned* fractions,
unsigned timeZoneBufferLength, char* timeZoneBuffer)
{
Firebird::IMaster* master = (Firebird::IMaster*)master_ptr;
Firebird::IUtil* util = master->getUtilInterface();
Firebird::IStatus* status = master->getStatus();
Firebird::CheckStatusWrapper st(status);
util->decodeTimeTz(&st, timeTz, hours, minutes, seconds, fractions,
timeZoneBufferLength, timeZoneBuffer);
}
/* Decodes a timestamp with time zone into its date and time components */
extern "C" void fbu_decode_timestamp_tz(void *master_ptr, const ISC_TIMESTAMP_TZ* timestampTz,
unsigned* year, unsigned* month, unsigned* day,
unsigned* hours, unsigned* minutes, unsigned* seconds, unsigned* fractions,
unsigned timeZoneBufferLength, char* timeZoneBuffer)
{
Firebird::IMaster* master = (Firebird::IMaster*)master_ptr;
Firebird::IUtil* util = master->getUtilInterface();
Firebird::IStatus* status = master->getStatus();
Firebird::CheckStatusWrapper st(status);
util->decodeTimeStampTz(&st, timestampTz, year, month, day,
hours, minutes, seconds, fractions,
timeZoneBufferLength, timeZoneBuffer);
}
extern "C" void fbu_release_statement(void *statement_ptr)
{
Firebird::IStatement* statement = (Firebird::IStatement *)statement_ptr;
if (statement) statement->release();
}
extern "C" ISC_STATUS fbu_insert_aliases(void *master_ptr, ISC_STATUS* st, ibase_query *ib_query, void *statement_ptr)
{
Firebird::IMaster* master = (Firebird::IMaster*)master_ptr;
Firebird::ThrowStatusWrapper status(master->getStatus());
Firebird::IStatement* statement = (Firebird::IStatement *)statement_ptr;
Firebird::IMessageMetadata* meta = NULL;
try {
meta = statement->getOutputMetadata(&status);
unsigned cols = meta->getCount(&status);
assert(cols == ib_query->out_fields_count);
for (unsigned i = 0; i < cols; ++i)
{
_php_ibase_insert_alias(ib_query->ht_aliases, meta->getAlias(&status, i));
}
meta->release();
}
catch (Firebird::FbException&)
{
if (status.hasData()) {
fbu_copy_status((const ISC_STATUS*)status.getErrors(), st, 20);
return st[1];
}
}
status.dispose();
return 0;
}
extern "C" ISC_STATUS fbu_insert_field_info(void *master_ptr, ISC_STATUS* st, int is_outvar, unsigned int num,
zval *into_array, void *statement_ptr)
{
Firebird::IMaster* master = (Firebird::IMaster*)master_ptr;
Firebird::ThrowStatusWrapper status(master->getStatus());
Firebird::IStatement* statement = (Firebird::IStatement *)statement_ptr;
Firebird::IMessageMetadata* meta = NULL;
try {
if(is_outvar) {
meta = statement->getOutputMetadata(&status);
} else {
meta = statement->getInputMetadata(&status);
}
add_index_string(into_array, 0, meta->getField(&status, num));
add_assoc_string(into_array, "name", meta->getField(&status, num));
add_index_string(into_array, 1, meta->getAlias(&status, num));
add_assoc_string(into_array, "alias", meta->getAlias(&status, num));
add_index_string(into_array, 2, meta->getRelation(&status, num));
add_assoc_string(into_array, "relation", meta->getRelation(&status, num));
meta->release();
}
catch (Firebird::FbException&)
{
if (status.hasData()) {
fbu_copy_status((const ISC_STATUS*)status.getErrors(), st, 20);
return st[1];
}
}
status.dispose();
return 0;
}
#endif // FB_API_VER >= 40
#ifdef _MSC_VER
#include <intrin.h>
static int u64_mul10_gt(uint64_t r, uint64_t digit, uint64_t max, uint64_t *out)
{
#ifdef _WIN64
uint64_t hi, lo = _umul128(r, 10, &hi);
if (hi || lo > max - digit) return 1;
*out = lo + digit;
return 0;
#else
// TODO: needs testing
/* x86: no _umul128; equivalent overflow check without 128-bit arithmetic */
if (r > (max - digit) / 10) return 1;
*out = r * 10 + digit;
return 0;
#endif
}
#else
static int u64_mul10_gt(uint64_t r, uint64_t digit, uint64_t max, uint64_t *out)
{
unsigned __int128 v = (unsigned __int128)r * 10 + digit;
if (v > max) return 1;
*out = (uint64_t)v;
return 0;
}
#endif
// Parse NUMERIC() to signed int representation
// Currently not used, bet keep it around. Useful when converting NUMERIC fields
// in integers. Might be usefult in the future.
extern "C" int fbu_string_to_numeric(const char *s, size_t slen, int scale, uint64_t max,
int *sign, int *exp, uint64_t *res)
{
const char* p = s;
const char *end = s + slen;
*sign = *exp = 0;
*res = 0;
if (!slen) return STRNUM_PARSE_OK;
if (*p == '-') {
*sign = -1;
p++;
} else if (*p == '+') {
*sign = 1;
p++;
} else {
*sign = 1;
}
if (*sign == -1) max += 1;
int fraction = 0;
uint64_t r = *res;
while (p < end) {
if (*p >= '0' && *p <= '9') {
if (u64_mul10_gt(r, *p - '0', max, &r)) return STRNUM_PARSE_OVERFLOW;
p++;
if (fraction) {
scale++;
--*exp;
}
} else if (*p == '.') {
if (fraction) return STRNUM_PARSE_ERROR;
fraction = 1;
p++;
continue;
} else {
return STRNUM_PARSE_ERROR;
}
}
while (scale < 0) {
if (u64_mul10_gt(r, 0, max, &r)) return STRNUM_PARSE_OVERFLOW;
scale++;
--*exp;
}
*res = r;
return STRNUM_PARSE_OK;
}
// +-----------+------------------------+---------------------+
// | Precision | Data type | Dialect 3 |
// +-----------+------------------------+---------------------+
// | 1 - 4 | NUMERIC | SMALLINT |
// | 1 - 4 | DECIMAL | INTEGER |
// | 5 - 9 | NUMERIC or DECIMAL | INTEGER |
// | 10 - 18 | NUMERIC or DECIMAL | BIGINT |
// | 19 - 38 | NUMERIC or DECIMAL | INT128 |
// +-----------+------------------------+---------------------+
extern "C" const char* fbu_get_sql_type_name(XSQLVAR *var, char *buf, size_t buf_size)
{
unsigned short precision = 0;
if (var->sqlscale < 0) {
switch (var->sqltype & ~1)
{
case SQL_SHORT : precision = 4; break;
case SQL_LONG : precision = 9; break;
case SQL_INT64 : precision = 18; break;
#if FB_API_VER >= 40
case SQL_INT128 : precision = 38; break;
#endif
default: fbp_fatal("BUG: unhandled type: %d with scale: %d", var->sqltype, var->sqlscale);
}
slprintf(buf, buf_size, "NUMERIC(%d,%d)", precision, -var->sqlscale);
return buf;
} else {
switch (var->sqltype & ~1)
{
case SQL_TEXT: return "CHAR";
case SQL_VARYING: return "VARCHAR";
case SQL_SHORT: return "SMALLINT";
case SQL_LONG: return "INTEGER";
case SQL_INT64: return "BIGINT";
case SQL_FLOAT: return "FLOAT";
case SQL_DOUBLE: return "DOUBLE PRECISION";
case SQL_D_FLOAT: return "D_FLOAT";
case SQL_TIMESTAMP: return "TIMESTAMP";
case SQL_TYPE_DATE: return "DATE";
case SQL_TYPE_TIME: return "TIME";
case SQL_BLOB: return "BLOB";
case SQL_ARRAY: return "ARRAY";
case SQL_QUAD: return "QUAD";
case SQL_NULL: return "NULL";
#if FB_API_VER >= 30
case SQL_BOOLEAN: return "BOOLEAN";
#endif
#if FB_API_VER >= 40
case SQL_DEC16: return "DECFLOAT(16)";
case SQL_DEC34: return "DECFLOAT(34)";
case SQL_INT128: return "INT128";
case SQL_TIMESTAMP_TZ: return "TIMESTAMP WITH TIME ZONE";
case SQL_TIMESTAMP_TZ_EX: return "EXTENDED TIMESTAMP WITH TIME ZONE";
case SQL_TIME_TZ: return "TIME WITH TIME ZONE";
case SQL_TIME_TZ_EX: return "EXTENDED TIME WITH TIME ZONE";
#endif
default: return "UNKNOWN";
}
}
}
extern "C" void fbu_init_date_object(const char *tzbuff, zval *o)
{
zval tzo, tzo_arg;
php_date_instantiate(php_date_get_date_ce(), o);
if (tzbuff) {
ZVAL_STRING(&tzo_arg, tzbuff);
object_init_ex(&tzo, php_date_get_timezone_ce());
#if PHP_MAJOR_VERSION >= 8
zend_call_known_instance_method_with_1_params(
Z_OBJCE(tzo)->constructor, Z_OBJ(tzo), NULL, &tzo_arg);
#else
zend_call_method_with_1_params(&tzo, php_date_get_timezone_ce(), NULL, "__construct", NULL, &tzo_arg);
#endif
php_date_initialize(Z_PHPDATE_P(o), "", 0, NULL, &tzo, 0);
zval_ptr_dtor(&tzo_arg);
zval_ptr_dtor(&tzo);
} else {
php_date_initialize(Z_PHPDATE_P(o), "", 0, NULL, NULL, 0);
}
// zend_string *tt = php_format_date(ff, strlen(ff), ((ISC_TIME_TZ *)data)->utc_time, 0);
// php_date_time_set(&new_object, h, i, s, ms, return_value);
// php_date_initialize_from_ts_long(date_obj, 0, 0);
}