From ab510d17009a677230ca63045f02a95717d68ae3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Dewitte?= Date: Thu, 7 Mar 2013 23:45:42 +0100 Subject: [PATCH] Add settings array to connection parameters that executes SET statements once connected --- app/Config/database.php.default | 6 ++++++ lib/Cake/Model/Datasource/Database/Mysql.php | 5 +++++ lib/Cake/Model/Datasource/Database/Postgres.php | 5 +++++ lib/Cake/Model/Datasource/Database/Sqlserver.php | 5 +++++ .../Model/Datasource/Database/PostgresTest.php | 15 +++++++++++++++ 5 files changed, 36 insertions(+) diff --git a/app/Config/database.php.default b/app/Config/database.php.default index 69dd26e87..d1585cdbf 100644 --- a/app/Config/database.php.default +++ b/app/Config/database.php.default @@ -52,6 +52,12 @@ * * unix_socket => * For MySQL to connect via socket specify the `unix_socket` parameter instead of `host` and `port` + * + * settings => + * Array of key/value pairs, on connection it executes SET statements for each pair + * For MySQL : http://dev.mysql.com/doc/refman/5.6/en/set-statement.html + * For Postgres : http://www.postgresql.org/docs/9.2/static/sql-set.html + * For Sql Server : http://msdn.microsoft.com/en-us/library/ms190356.aspx */ class DATABASE_CONFIG { diff --git a/lib/Cake/Model/Datasource/Database/Mysql.php b/lib/Cake/Model/Datasource/Database/Mysql.php index f153fc7c1..9d004fd53 100644 --- a/lib/Cake/Model/Datasource/Database/Mysql.php +++ b/lib/Cake/Model/Datasource/Database/Mysql.php @@ -157,6 +157,11 @@ class Mysql extends DboSource { $flags ); $this->connected = true; + if (!empty($config['settings'])) { + foreach ($config['settings'] as $key => $value) { + $this->_execute("SET $key=$value"); + } + } } catch (PDOException $e) { throw new MissingConnectionException(array( 'class' => get_class($this), diff --git a/lib/Cake/Model/Datasource/Database/Postgres.php b/lib/Cake/Model/Datasource/Database/Postgres.php index 747ad8b85..3f0f576fe 100644 --- a/lib/Cake/Model/Datasource/Database/Postgres.php +++ b/lib/Cake/Model/Datasource/Database/Postgres.php @@ -129,6 +129,11 @@ class Postgres extends DboSource { if (!empty($config['schema'])) { $this->_execute('SET search_path TO ' . $config['schema']); } + if (!empty($config['settings'])) { + foreach ($config['settings'] as $key => $value) { + $this->_execute("SET $key TO $value"); + } + } } catch (PDOException $e) { throw new MissingConnectionException(array( 'class' => get_class($this), diff --git a/lib/Cake/Model/Datasource/Database/Sqlserver.php b/lib/Cake/Model/Datasource/Database/Sqlserver.php index bd81ee4a0..a6d398110 100644 --- a/lib/Cake/Model/Datasource/Database/Sqlserver.php +++ b/lib/Cake/Model/Datasource/Database/Sqlserver.php @@ -132,6 +132,11 @@ class Sqlserver extends DboSource { $flags ); $this->connected = true; + if (!empty($config['settings'])) { + foreach ($config['settings'] as $key => $value) { + $this->_execute("SET $key $value"); + } + } } catch (PDOException $e) { throw new MissingConnectionException(array( 'class' => get_class($this), diff --git a/lib/Cake/Test/Case/Model/Datasource/Database/PostgresTest.php b/lib/Cake/Test/Case/Model/Datasource/Database/PostgresTest.php index 066cb75ba..096d6c33d 100644 --- a/lib/Cake/Test/Case/Model/Datasource/Database/PostgresTest.php +++ b/lib/Cake/Test/Case/Model/Datasource/Database/PostgresTest.php @@ -1003,4 +1003,19 @@ class PostgresTest extends CakeTestCase { $this->assertTrue($new['currval'] > $original['nextval'], 'Sequence did not update'); } + public function testSettings() { + Configure::write('Cache.disable', true); + $this->Dbo = ConnectionManager::getDataSource('test'); + $this->skipIf(!($this->Dbo instanceof Postgres)); + + $config2 = $this->Dbo->config; + $config2['settings']['datestyle'] = 'sql, dmy'; + ConnectionManager::create('test2', $config2); + $dbo2 = new Postgres($config2, true); + $expected = array(array('r' => date('d/m/Y'))); + $r = $dbo2->fetchRow('SELECT now()::date AS "r"'); + $this->assertEquals($expected, $r); + $dbo2->disconnect(); + } + }