Skip to content
Permalink
Browse files

Implementing init commands for postgres and defaulting timezone to UTC

  • Loading branch information...
lorenzo committed Mar 16, 2013
1 parent e6d4f12 commit fab7e0bce3cd0ac51307d68d95a293adb116f0ea
@@ -42,7 +42,9 @@ class Postgres extends \Cake\Model\Datasource\Database\Driver {
'schema' => 'public',
'port' => 5432,
'encoding' => 'utf8',
'timezone' => 'UTC',
'flags' => [],
'init' => [],
'dsn' => null
];
@@ -64,12 +66,22 @@ public function connect(array $config) {
}
$this->_connect($config);
$connection = $this->connection();
if (!empty($config['encoding'])) {
$this->setEncoding($config['encoding']);
}
if (!empty($config['schema'])) {
$this->setSchema($config['schema']);
}
if (!empty($config['timezone'])) {
$config['init'][] = sprintf("SET timezone = %s", $connection->quote($config['timezone']));
}
foreach ($config['init'] as $command) {
$connection->exec($command);
}
return true;
}
@@ -100,7 +112,8 @@ public function prepare($sql) {
* @return void
*/
public function setEncoding($encoding) {
$this->_connection->exec('SET NAMES ' . $this->_connection->quote($encoding));
$connection = $this->connection();
$connection->exec('SET NAMES ' . $connection->quote($encoding));
}
/**
@@ -110,7 +123,8 @@ public function setEncoding($encoding) {
* @return void
*/
public function setSchema($schema) {
$this->_connection->exec('SET search_path TO ' . $this->_connection->quote($schema));
$connection = $this->connection();
$connection->exec('SET search_path TO ' . $connection->quote($schema));
}
}
@@ -0,0 +1,129 @@
<?php
/**
* PHP Version 5.4
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package Cake.Model
* @since CakePHP(tm) v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Test\TestCase\Model\Datasource\Database\Driver;
use \PDO;
/**
* Tests Postgres driver
*
*/
class PostgresTest extends \Cake\TestSuite\TestCase {
/**
* Test connecting to Postgres with default configuration
*
* @return void
*/
public function testConnectionConfigDefault() {
$driver = $this->getMock('Cake\Model\Datasource\Database\driver\Postgres', ['_connect', 'connection']);
$expected = [
'persistent' => true,
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'cake',
'schema' => 'public',
'port' => 5432,
'encoding' => 'utf8',
'timezone' => 'UTC',
'flags' => [],
'init' => [],
'dsn' => 'pgsql:host=localhost;port=5432;dbname=cake'
];
$expected['flags'] += [
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
];
$connection = $this->getMock('stdClass', ['exec', 'quote']);
$connection->expects($this->any())
->method('quote')
->will($this->onConsecutiveCalls(
$this->returnArgument(0),
$this->returnArgument(0),
$this->returnArgument(0)
));
$connection->expects($this->at(1))->method('exec')->with('SET NAMES utf8');
$connection->expects($this->at(3))->method('exec')->with('SET search_path TO public');
$connection->expects($this->at(5))->method('exec')->with('SET timezone = UTC');
$connection->expects($this->exactly(3))->method('exec');
$driver->expects($this->once())->method('_connect')
->with($expected);
$driver->expects($this->any())->method('connection')
->will($this->returnValue($connection));
$driver->connect([]);
}
/**
* Test connecting to Postgres with custom configuration
*
* @return void
*/
public function testConnectionConfigCustom() {
$driver = $this->getMock('Cake\Model\Datasource\Database\driver\Postgres', ['_connect', 'connection']);
$config = [
'persistent' => false,
'host' => 'foo',
'database' => 'bar',
'login' => 'user',
'password' => 'pass',
'port' => 3440,
'flags' => [1 => true, 2 => false],
'encoding' => 'a-language',
'timezone' => 'Antartica',
'schema' => 'fooblic',
'init' => ['Execute this', 'this too']
];
$expected = $config;
$expected['dsn'] = 'pgsql:host=foo;port=3440;dbname=bar';
$expected['flags'] += [
PDO::ATTR_PERSISTENT => false,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
];
$connection = $this->getMock('stdClass', ['exec', 'quote']);
$connection->expects($this->any())
->method('quote')
->will($this->onConsecutiveCalls(
$this->returnArgument(0),
$this->returnArgument(0),
$this->returnArgument(0)
));
$connection->expects($this->at(1))->method('exec')->with('SET NAMES a-language');
$connection->expects($this->at(3))->method('exec')->with('SET search_path TO fooblic');
$connection->expects($this->at(5))->method('exec')->with('Execute this');
$connection->expects($this->at(6))->method('exec')->with('this too');
$connection->expects($this->at(7))->method('exec')->with('SET timezone = Antartica');
$connection->expects($this->exactly(5))->method('exec');
$driver->expects($this->once())->method('_connect')
->with($expected);
$driver->expects($this->any())->method('connection')
->will($this->returnValue($connection));
$driver->connect($config);
}
}
@@ -89,5 +89,3 @@ public function testConnectionConfigCustom() {
}
}

0 comments on commit fab7e0b

Please sign in to comment.
You can’t perform that action at this time.