Skip to content
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

Added support for time zone config setting for PostgreSql adapter. #792

Merged
merged 1 commit into from
Jan 22, 2013
Merged
Show file tree
Hide file tree
Changes from all 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
30 changes: 29 additions & 1 deletion data/source/database/adapter/PostgreSql.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,12 @@ class PostgreSql extends \lithium\data\source\Database {
* list of active connections.
*/
public function __construct(array $config = array()) {
$defaults = array('host' => 'localhost:5432', 'encoding' => null, 'schema' => 'public');
$defaults = array(
'host' => 'localhost:5432',
'encoding' => null,
'schema' => 'public',
'timezone' => null
);
parent::__construct($config + $defaults);
}

Expand Down Expand Up @@ -127,6 +132,10 @@ public function connect() {
if ($this->_config['schema']) {
$this->search_path($this->_config['schema']);
}

if ($this->_config['timezone']) {
$this->timezone($this->_config['timezone']);
}
return true;
}

Expand Down Expand Up @@ -235,6 +244,25 @@ public function search_path($search_path) {
}
}

/**
* Gets or sets the time zone for the connection
* @param $timezone
* @return mixed If setting the time zone; returns true on success, else false
* When getting, returns the time zone
*/
public function timezone($timezone = null) {
if (empty($timezone)) {
$query = $this->connection->query('SHOW TIME ZONE');
return $query->fetchColumn();
}
try {
$this->connection->exec("SET TIME ZONE '{$timezone}'");
return true;
} catch (PDOException $e) {
return false;
}
}

/**
* Gets or sets the encoding for the connection.
*
Expand Down
18 changes: 16 additions & 2 deletions tests/cases/data/source/database/adapter/PostgreSqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function testConstructorDefaults() {
'autoConnect' => false, 'encoding' => null,'persistent' => true,
'host' => 'localhost:5432', 'login' => 'root', 'password' => '',
'database' => null, 'dsn' => null, 'options' => array(),
'init' => true, 'schema' => 'public'
'init' => true, 'schema' => 'public', 'timezone' => null
);
$this->assertEqual($expected, $result);
}
Expand Down Expand Up @@ -93,6 +93,15 @@ public function testDatabaseEncoding() {
$this->assertEqual('UTF-8', $this->db->encoding());
}

public function testDatabaseTimezone() {
$this->assertTrue($this->db->isConnected());
$this->assertTrue($this->db->timezone('UTC'));
$this->assertEqual('UTC', $this->db->timezone());

$this->assertTrue($this->db->timezone('US/Eastern'));
$this->assertEqual('US/Eastern', $this->db->timezone());
}

public function testValueByIntrospect() {
$expected = "'string'";
$result = $this->db->value("string");
Expand Down Expand Up @@ -149,7 +158,12 @@ public function testRawSqlQuerying() {
$this->assertTrue(is_numeric($result[0]['id']));
unset($result[0]['id']);

$expected = array('name' => 'Test', 'active' => true, 'created' => null, 'modified' => null);
$expected = array(
'name' => 'Test',
'active' => true,
'created' => null,
'modified' => null
);
$this->assertIdentical($expected, $result[0]);

$this->assertTrue($this->db->delete('DELETE From companies WHERE name = {:name}', array(
Expand Down