Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

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

Merged
merged 1 commit into from

3 participants

@nealerickson

Added support for a timezone parameter in the connection config for the PostgreSql adapter. Allows you to control the time zone that is used for your database connection. Important to be able to enforce the connection time zone when dealing with time zone aware timestamps.

@nealerickson

Alright, this PR actually has the missing call to timezone() in the connect() method.

@jails
Collaborator

Perfect, thanks.

@nateabele nateabele merged commit 3b0c1d5 into UnionOfRAD:dev
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 20, 2013
  1. @nealerickson
This page is out of date. Refresh to see the latest.
View
30 data/source/database/adapter/PostgreSql.php
@@ -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);
}
@@ -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;
}
@@ -236,6 +245,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.
*
* @param $encoding
View
18 tests/cases/data/source/database/adapter/PostgreSqlTest.php
@@ -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);
}
@@ -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");
@@ -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(
Something went wrong with that request. Please try again.