Skip to content

PDO_Firebird: Add connection level SESSION_TIMEZONE attribute #15480

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 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
22 changes: 20 additions & 2 deletions ext/pdo_firebird/firebird_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -1437,9 +1437,21 @@ static int pdo_firebird_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /*
}

do {
#if FB_API_VER >= 40
zend_string *session_timezone = pdo_attr_strval(driver_options, PDO_FB_SESSION_TIMEZONE, NULL);
#endif

static char const dpb_flags[] = {
isc_dpb_user_name, isc_dpb_password, isc_dpb_lc_ctype, isc_dpb_sql_role_name };
char const *dpb_values[] = { dbh->username, dbh->password, vars[1].optval, vars[2].optval };
isc_dpb_user_name, isc_dpb_password, isc_dpb_lc_ctype, isc_dpb_sql_role_name
#if FB_API_VER >= 40
, isc_dpb_session_time_zone
#endif
};
char const *dpb_values[] = { dbh->username, dbh->password, vars[1].optval, vars[2].optval
#if FB_API_VER >= 40
, session_timezone ? ZSTR_VAL(session_timezone) : NULL
#endif
};
char dpb_buffer[256] = { isc_dpb_version1 }, *dpb;

dpb = dpb_buffer + 1;
Expand All @@ -1454,6 +1466,12 @@ static int pdo_firebird_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /*
}
}

#if FB_API_VER >= 40
if (session_timezone) {
zend_string_release_ex(session_timezone, 0);
}
#endif

H->sql_dialect = PDO_FB_DIALECT;
if (vars[3].optval) {
H->sql_dialect = atoi(vars[3].optval);
Expand Down
3 changes: 3 additions & 0 deletions ext/pdo_firebird/pdo_firebird.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class Firebird extends \PDO

/** @cvalue PDO_FB_WRITABLE_TRANSACTION */
public const int WRITABLE_TRANSACTION = UNKNOWN;

/** @cvalue PDO_FB_SESSION_TIMEZONE */
public const int SESSION_TIMEZONE = UNKNOWN;

public static function getApiVersion(): int {}
}
8 changes: 7 additions & 1 deletion ext/pdo_firebird/pdo_firebird_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions ext/pdo_firebird/php_pdo_firebird_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ enum {

/* transaction access mode */
PDO_FB_WRITABLE_TRANSACTION,

/* session time zone */
PDO_FB_SESSION_TIMEZONE,
};

#endif /* PHP_PDO_FIREBIRD_INT_H */
8 changes: 1 addition & 7 deletions ext/pdo_firebird/tests/fb4_datatypes.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,13 @@ if (Pdo\Firebird::getApiVersion() < 40) {
die('skip: Firebird API version must be greater than or equal to 40');
}
require 'testdb.inc';
$dbh = getDbConnection();
$stmt = $dbh->query("SELECT RDB\$get_context('SYSTEM', 'ENGINE_VERSION') AS VERSION FROM RDB\$DATABASE");
$data = $stmt->fetch(\PDO::FETCH_ASSOC);
if (!$data || !array_key_exists('VERSION', $data) || version_compare($data['VERSION'], '4.0.0') < 0) {
die("skip Firebird Server version must be greater than or equal to 4.0.0");
}
checkMinServerVersion('4.0.0');
?>
--XLEAK--
A bug in firebird causes a memory leak when calling `isc_attach_database()`.
See https://github.com/FirebirdSQL/firebird/issues/7849
--FILE--
<?php
require 'testdb.inc';

$sql = <<<'SQL'
SELECT
Expand Down
8 changes: 1 addition & 7 deletions ext/pdo_firebird/tests/fb4_datatypes_params.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,13 @@ if (Pdo\Firebird::getApiVersion() < 40) {
die('skip: Firebird API version must be greater than or equal to 40');
}
require 'testdb.inc';
$dbh = getDbConnection();
$stmt = $dbh->query("SELECT RDB\$get_context('SYSTEM', 'ENGINE_VERSION') AS VERSION FROM RDB\$DATABASE");
$data = $stmt->fetch(\PDO::FETCH_ASSOC);
if (!$data || !array_key_exists('VERSION', $data) || version_compare($data['VERSION'], '4.0.0') < 0) {
die("skip Firebird Server version must be greater than or equal to 4.0.0");
}
checkMinServerVersion('4.0.0');
?>
--XLEAK--
A bug in firebird causes a memory leak when calling `isc_attach_database()`.
See https://github.com/FirebirdSQL/firebird/issues/7849
--FILE--
<?php
require 'testdb.inc';

$sql = <<<'SQL'
SELECT
Expand Down
49 changes: 49 additions & 0 deletions ext/pdo_firebird/tests/fb4_session_timezone.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
--TEST--
PDO_Firebird: Firebird 4.0 set session time zone
--EXTENSIONS--
pdo_firebird
--SKIPIF--
<?php require('skipif.inc');
if (Pdo\Firebird::getApiVersion() < 40) {
die('skip: Firebird API version must be greater than or equal to 40');
}
require 'testdb.inc';
checkMinServerVersion('4.0.0');
?>
--XLEAK--
A bug in firebird causes a memory leak when calling `isc_attach_database()`.
See https://github.com/FirebirdSQL/firebird/issues/7849
--FILE--
<?php

$sql = <<<'SQL'
SELECT
rdb$get_context('SYSTEM', 'SESSION_TIMEZONE') as tz,
trim(replace(current_timestamp, localtimestamp, '')) as tz1,
trim(replace(current_time, localtime, '')) as tz2
FROM rdb$database
SQL;

$dbh = connectToDb([
Pdo\Firebird::SESSION_TIMEZONE => 'Europe/Rome',
]);

$stmt = $dbh->prepare($sql);
$stmt->execute();
$data = $stmt->fetch(\PDO::FETCH_ASSOC);
$stmt->closeCursor();

var_dump($data);
echo "\ndone\n";
?>
--EXPECT--
array(3) {
["TZ"]=>
string(11) "Europe/Rome"
["TZ1"]=>
string(11) "Europe/Rome"
["TZ2"]=>
string(11) "Europe/Rome"
}

done
18 changes: 14 additions & 4 deletions ext/pdo_firebird/tests/testdb.inc
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,25 @@ define('PDO_FIREBIRD_TEST_DSN', getenv('PDO_FIREBIRD_TEST_DSN') ?: '');

if(!PDO_FIREBIRD_TEST_DSN)
{
die('Error: PDO_FIREBIRD_TEST_DSN must be set');
die('Error: PDO_FIREBIRD_TEST_DSN must be set');
Copy link
Member

Choose a reason for hiding this comment

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

I'd stick with 4 spaces as indentation in PHP files, but I don't think we have any CS in this regard.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK

}

function getDbConnection($class = PDO::class): PDO {
return new $class(PDO_FIREBIRD_TEST_DSN, PDO_FIREBIRD_TEST_USER, PDO_FIREBIRD_TEST_PASS);
return new $class(PDO_FIREBIRD_TEST_DSN, PDO_FIREBIRD_TEST_USER, PDO_FIREBIRD_TEST_PASS);
}

function connectToDb(): Pdo\Firebird {
return Pdo\Firebird::connect(PDO_FIREBIRD_TEST_DSN, PDO_FIREBIRD_TEST_USER, PDO_FIREBIRD_TEST_PASS);
function connectToDb(array $params = []): Pdo\Firebird {
return Pdo\Firebird::connect(PDO_FIREBIRD_TEST_DSN, PDO_FIREBIRD_TEST_USER, PDO_FIREBIRD_TEST_PASS, $params);
}

function checkMinServerVersion(string $version): void
{
$dbh = connectToDb();
$stmt = $dbh->query("SELECT RDB\$GET_CONTEXT('SYSTEM', 'ENGINE_VERSION') AS VERSION FROM RDB\$DATABASE");
$data = $stmt->fetch(\PDO::FETCH_ASSOC);
if (!$data || !array_key_exists('VERSION', $data) || version_compare($data['VERSION'], $version) < 0) {
die("skip Firebird Server version must be greater than or equal to $version");
}
}

?>
Loading