mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Add settings array to connection parameters that executes SET statements once connected
This commit is contained in:
parent
b105318bcc
commit
ab510d1700
5 changed files with 36 additions and 0 deletions
|
@ -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 {
|
||||
|
||||
|
|
|
@ -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),
|
||||
|
|
|
@ -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),
|
||||
|
|
|
@ -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),
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue