Add settings array to connection parameters that executes SET statements once connected

This commit is contained in:
Rémi Dewitte 2013-03-07 23:45:42 +01:00
parent b105318bcc
commit ab510d1700
5 changed files with 36 additions and 0 deletions

View file

@ -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 {

View file

@ -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),

View file

@ -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),

View file

@ -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),

View file

@ -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();
}
}