Skip to content

pdo_firebird: Formatting time zone types #15230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion ext/pdo_firebird/config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,24 @@ if test "$PHP_PDO_FIREBIRD" != "no"; then

PHP_CHECK_PDO_INCLUDES


PHP_PDO_FIREBIRD_COMMON_FLAGS=""
PHP_NEW_EXTENSION([pdo_firebird],
[pdo_firebird.c firebird_driver.c firebird_statement.c],
[$ext_shared])
[$ext_shared],,
[$PHP_PDO_FIREBIRD_COMMON_FLAGS],
[cxx])
PHP_SUBST([PDO_FIREBIRD_SHARED_LIBADD])
PHP_ADD_EXTENSION_DEP(pdo_firebird, pdo)

PHP_PDO_FIREBIRD_CXX_SOURCES="pdo_firebird_utils.cpp"

PHP_REQUIRE_CXX()
PHP_CXX_COMPILE_STDCXX(11, mandatory, PHP_PDO_FIREBIRD_STDCXX)
PHP_PDO_FIREBIRD_CXX_FLAGS="$PHP_PDO_FIREBIRD_COMMON_FLAGS $PHP_PDO_FIREBIRD_STDCXX"

PHP_ADD_SOURCES([$ext_dir],
[$PHP_PDO_FIREBIRD_CXX_SOURCES],
[$PHP_PDO_FIREBIRD_CXX_FLAGS])

fi
3 changes: 2 additions & 1 deletion ext/pdo_firebird/config.w32
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ if (PHP_PDO_FIREBIRD != "no") {
PHP_PHP_BUILD + "\\include\\interbase;" + PHP_PHP_BUILD + "\\interbase\\include;" + PHP_PDO_FIREBIRD)
) {

EXTENSION("pdo_firebird", "pdo_firebird.c firebird_driver.c firebird_statement.c");
EXTENSION("pdo_firebird", "pdo_firebird.c firebird_driver.c firebird_statement.c pdo_firebird_utils.cpp");
ADD_FLAG("CFLAGS_PDO_FIREBIRD", "/EHsc");
} else {
WARNING("pdo_firebird not enabled; libraries and headers not found");
}
Expand Down
78 changes: 68 additions & 10 deletions ext/pdo_firebird/firebird_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "ext/pdo/php_pdo_driver.h"
#include "php_pdo_firebird.h"
#include "php_pdo_firebird_int.h"
#include "pdo_firebird_utils.h"

static int php_firebird_alloc_prepare_stmt(pdo_dbh_t*, const zend_string*, XSQLDA*, isc_stmt_handle*,
HashTable*);
Expand Down Expand Up @@ -462,14 +463,13 @@ static int php_firebird_preprocess(const zend_string* sql, char* sql_out, HashTa

#if FB_API_VER >= 40
/* set coercing a data type */
static void set_coercing_data_type(XSQLDA* sqlda)
static void set_coercing_input_data_types(XSQLDA* sqlda)
{
/* Data types introduced in Firebird 4.0 are difficult to process using the Firebird Legacy API. */
/* These data types include DECFLOAT(16), DECFLOAT(34), INT128 (NUMERIC/DECIMAL(38, x)), */
/* TIMESTAMP WITH TIME ZONE, and TIME WITH TIME ZONE. In any case, at least the first three data types */
/* can only be mapped to strings. The last two too, but for them it is potentially possible to set */
/* the display format, as is done for TIMESTAMP. This function allows you to ensure minimal performance */
/* of queries if they contain columns and parameters of the above types. */
/* TIMESTAMP WITH TIME ZONE, and TIME WITH TIME ZONE. */
/* This function allows you to ensure minimal performance */
/* of queries if they contain parameters of the above types. */
unsigned int i;
short dtype;
short nullable;
Expand All @@ -485,7 +485,7 @@ static void set_coercing_data_type(XSQLDA* sqlda)
break;

case SQL_DEC16:
var->sqltype = SQL_VARYING + nullable;
var->sqltype = SQL_VARYING + nullable;
var->sqllen = 24;
break;

Expand All @@ -503,8 +503,65 @@ static void set_coercing_data_type(XSQLDA* sqlda)
var->sqltype = SQL_VARYING + nullable;
var->sqllen = 46;
break;

default:
break;
}
}
}

static void set_coercing_output_data_types(XSQLDA* sqlda)
{
/* Data types introduced in Firebird 4.0 are difficult to process using the Firebird Legacy API. */
/* These data types include DECFLOAT(16), DECFLOAT(34), INT128 (NUMERIC/DECIMAL(38, x)). */
/* In any case, at this data types can only be mapped to strings. */
/* This function allows you to ensure minimal performance of queries if they contain columns of the above types. */
unsigned int i;
short dtype;
short nullable;
XSQLVAR* var;
unsigned fb_client_version = fb_get_client_version();
unsigned fb_client_major_version = (fb_client_version >> 8) & 0xFF;
for (i=0, var = sqlda->sqlvar; i < sqlda->sqld; i++, var++) {
dtype = (var->sqltype & ~1); /* drop flag bit */
nullable = (var->sqltype & 1);
switch(dtype) {
case SQL_INT128:
var->sqltype = SQL_VARYING + nullable;
var->sqllen = 46;
var->sqlscale = 0;
break;

case SQL_DEC16:
var->sqltype = SQL_VARYING + nullable;
var->sqllen = 24;
break;

case SQL_DEC34:
var->sqltype = SQL_VARYING + nullable;
var->sqllen = 43;
break;

case SQL_TIMESTAMP_TZ:
if (fb_client_major_version < 4) {
/* If the client version is below 4.0, then it is impossible to handle time zones natively, */
/* so we convert these types to a string. */
var->sqltype = SQL_VARYING + nullable;
var->sqllen = 58;
}
break;

case SQL_TIME_TZ:
if (fb_client_major_version < 4) {
/* If the client version is below 4.0, then it is impossible to handle time zones natively, */
/* so we convert these types to a string. */
var->sqltype = SQL_VARYING + nullable;
var->sqllen = 46;
}
break;

default:
break;
break;
}
}
}
Expand Down Expand Up @@ -657,7 +714,7 @@ static bool firebird_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, /* {{{ */

#if FB_API_VER >= 40
/* set coercing a data type */
set_coercing_data_type(&S->out_sqlda);
set_coercing_output_data_types(&S->out_sqlda);
#endif

/* allocate the input descriptors */
Expand All @@ -676,7 +733,7 @@ static bool firebird_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, /* {{{ */

#if FB_API_VER >= 40
/* set coercing a data type */
set_coercing_data_type(S->in_sqlda);
set_coercing_input_data_types(S->in_sqlda);
#endif
}

Expand Down Expand Up @@ -1245,7 +1302,8 @@ static int pdo_firebird_get_attribute(pdo_dbh_t *dbh, zend_long attr, zval *val)
ZVAL_STRING(val, tmp);
return 1;
}
return -1;
/* TODO Check this is correct? */
ZEND_FALLTHROUGH;

case PDO_ATTR_FETCH_TABLE_NAMES:
ZVAL_BOOL(val, H->fetch_table_names);
Expand Down
83 changes: 83 additions & 0 deletions ext/pdo_firebird/firebird_statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "ext/pdo/php_pdo_driver.h"
#include "php_pdo_firebird.h"
#include "php_pdo_firebird_int.h"
#include "pdo_firebird_utils.h"

#include <time.h>

Expand Down Expand Up @@ -64,6 +65,78 @@ static zend_always_inline ISC_QUAD php_get_isc_quad_from_sqldata(const ISC_SCHAR
READ_AND_RETURN_USING_MEMCPY(ISC_QUAD, sqldata);
}

#if FB_API_VER >= 40

static zend_always_inline ISC_TIME_TZ php_get_isc_time_tz_from_sqldata(const ISC_SCHAR *sqldata)
{
READ_AND_RETURN_USING_MEMCPY(ISC_TIME_TZ, sqldata);
}

static zend_always_inline ISC_TIMESTAMP_TZ php_get_isc_timestamp_tz_from_sqldata(const ISC_SCHAR *sqldata)
{
READ_AND_RETURN_USING_MEMCPY(ISC_TIMESTAMP_TZ, sqldata);
}

/* fetch formatted time with time zone */
static int get_formatted_time_tz(pdo_stmt_t *stmt, const ISC_TIME_TZ* timeTz, zval *result)
{
pdo_firebird_stmt *S = (pdo_firebird_stmt*)stmt->driver_data;
unsigned hours = 0, minutes = 0, seconds = 0, fractions = 0;
char timeZoneBuffer[40] = {0};
char *fmt;
struct tm t;
ISC_TIME time;
char timeBuf[80] = {0};
char timeTzBuf[124] = {0};
if (fb_decode_time_tz(S->H->isc_status, timeTz, &hours, &minutes, &seconds, &fractions, sizeof(timeZoneBuffer), timeZoneBuffer)) {
return 1;
}
time = fb_encode_time(hours, minutes, seconds, fractions);
isc_decode_sql_time(&time, &t);
fmt = S->H->time_format ? S->H->time_format : PDO_FB_DEF_TIME_FMT;

size_t len = strftime(timeBuf, sizeof(timeBuf), fmt, &t);
if (len == 0) {
return 1;
}

size_t time_tz_len = sprintf(timeTzBuf, "%s %s", timeBuf, timeZoneBuffer);
ZVAL_STRINGL(result, timeTzBuf, time_tz_len);
return 0;
}

/* fetch formatted timestamp with time zone */
static int get_formatted_timestamp_tz(pdo_stmt_t *stmt, const ISC_TIMESTAMP_TZ* timestampTz, zval *result)
{
pdo_firebird_stmt *S = (pdo_firebird_stmt*)stmt->driver_data;
unsigned year, month, day, hours, minutes, seconds, fractions;
char timeZoneBuffer[40] = {0};
char *fmt;
struct tm t;
ISC_TIMESTAMP ts;
char timestampBuf[80] = {0};
char timestampTzBuf[124] = {0};
if (fb_decode_timestamp_tz(S->H->isc_status, timestampTz, &year, &month, &day, &hours, &minutes, &seconds, &fractions, sizeof(timeZoneBuffer), timeZoneBuffer)) {
return 1;
}
ts.timestamp_date = fb_encode_date(year, month, day);
ts.timestamp_time = fb_encode_time(hours, minutes, seconds, fractions);
isc_decode_timestamp(&ts, &t);

fmt = S->H->timestamp_format ? S->H->timestamp_format : PDO_FB_DEF_TIMESTAMP_FMT;

size_t len = strftime(timestampBuf, sizeof(timestampBuf), fmt, &t);
if (len == 0) {
return 1;
}

size_t timestamp_tz_len = sprintf(timestampTzBuf, "%s %s", timestampBuf, timeZoneBuffer);
ZVAL_STRINGL(result, timestampTzBuf, timestamp_tz_len);
return 0;
}

#endif

/* free the allocated space for passing field values to the db and back */
static void php_firebird_free_sqlda(XSQLDA const *sqlda) /* {{{ */
{
Expand Down Expand Up @@ -494,6 +567,16 @@ static int pdo_firebird_stmt_get_col(
size_t len = strftime(buf, sizeof(buf), fmt, &t);
ZVAL_STRINGL(result, buf, len);
break;
#if FB_API_VER >= 40
case SQL_TIME_TZ: {
ISC_TIME_TZ time = php_get_isc_time_tz_from_sqldata(var->sqldata);
return get_formatted_time_tz(stmt, &time, result);
}
case SQL_TIMESTAMP_TZ: {
ISC_TIMESTAMP_TZ ts = php_get_isc_timestamp_tz_from_sqldata(var->sqldata);
return get_formatted_timestamp_tz(stmt, &ts, result);
}
#endif
case SQL_BLOB: {
ISC_QUAD quad = php_get_isc_quad_from_sqldata(var->sqldata);
return php_firebird_fetch_blob(stmt, colno, result, &quad);
Expand Down
92 changes: 92 additions & 0 deletions ext/pdo_firebird/pdo_firebird_utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
+----------------------------------------------------------------------+
| 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> |
+----------------------------------------------------------------------+
*/

#include "pdo_firebird_utils.h"
#include <firebird/Interface.h>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sim1984 @cmb69 It seems that firebird/Interface.h isn't included in firebird 2.5: is it correct? If so, what about adding a note to the UPGRADING file, declaring the new firebird supported versions (from 3.0 to 5? Is it correct?)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uh, yes, this needs to be mentioned in these docs files also. So, the 3.0 is the minimum required now?

And then also version needs to be checked in config.m4.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right, I completely forgot about that. However, see #15230 (comment). The question is: is fbclient 2.5 still relevant (i.e. used by any distros we support)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is fbclient 2.5 still relevant (i.e. used by any distros we support)?

I don't think that on Alpine Linux there's a ready to use firebird client (at least, I couldn't find it).
So, the only solution is to manually compile it: when doing so, users can choose the firebird version they are going to install.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minimum requirements: fbclient version 3.0 or higher. Firebird server itself can be version 2.5, usually if it is on another machine.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See PR #15498.

#include <cstring>

static void fb_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;
}
}
}

/* Returns the client version. 0 bytes are minor version, 1 bytes are major version. */
extern "C" unsigned fb_get_client_version(void)
{
Firebird::IMaster* master = Firebird::fb_get_master_interface();
Firebird::IUtil* util = master->getUtilInterface();
return util->getClientVersion();
}

extern "C" ISC_TIME fb_encode_time(unsigned hours, unsigned minutes, unsigned seconds, unsigned fractions)
{
Firebird::IMaster* master = Firebird::fb_get_master_interface();
Firebird::IUtil* util = master->getUtilInterface();
return util->encodeTime(hours, minutes, seconds, fractions);
}

extern "C" ISC_DATE fb_encode_date(unsigned year, unsigned month, unsigned day)
{
Firebird::IMaster* master = Firebird::fb_get_master_interface();
Firebird::IUtil* util = master->getUtilInterface();
return util->encodeDate(year, month, day);
}

#if FB_API_VER >= 40

/* Decodes a time with time zone into its time components. */
extern "C" ISC_STATUS fb_decode_time_tz(ISC_STATUS* isc_status, const ISC_TIME_TZ* timeTz, unsigned* hours, unsigned* minutes, unsigned* seconds, unsigned* fractions,
unsigned timeZoneBufferLength, char* timeZoneBuffer)
{
Firebird::IMaster* master = Firebird::fb_get_master_interface();
Firebird::IUtil* util = master->getUtilInterface();
Firebird::IStatus* status = master->getStatus();
Firebird::CheckStatusWrapper st(status);
util->decodeTimeTz(&st, timeTz, hours, minutes, seconds, fractions,
timeZoneBufferLength, timeZoneBuffer);
if (st.hasData()) {
fb_copy_status((const ISC_STATUS*)st.getErrors(), isc_status, 20);
}
status->dispose();
return isc_status[1];
}

/* Decodes a timestamp with time zone into its date and time components */
extern "C" ISC_STATUS fb_decode_timestamp_tz(ISC_STATUS* isc_status, 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::fb_get_master_interface();
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);
if (st.hasData()) {
fb_copy_status((const ISC_STATUS*)st.getErrors(), isc_status, 20);
}
status->dispose();
return isc_status[1];
}

#endif
Loading
Loading