Permalink
Browse files

Added support to the PostgreSql adapter for getting and setting the t…

…ime zone for the connection.
  • Loading branch information...
1 parent 7c7832b commit bb4dce26e7836a98a3e830f31f5db4b8a0a3562a @nealerickson nealerickson committed Jan 20, 2013
@@ -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
@@ -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(

0 comments on commit bb4dce2

Please sign in to comment.