Skip to content

Commit

Permalink
Adding default timezone for mysql and ability to run init commands
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Mar 16, 2013
1 parent b084f74 commit 2a89ec0
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/Cake/Model/Datasource/Database/Driver/Mysql.php
Expand Up @@ -39,21 +39,30 @@ class Mysql extends \Cake\Model\Datasource\Database\Driver {
'port' => '3306',
'flags' => [],
'encoding' => 'utf8',
'timezone' => 'UTC',
'init' => [],
'dsn' => null
];

/**
* Establishes a connection to the databse server
* Establishes a connection to the database server
*
* @param array $config configuration to be used for creating connection
* @return boolean true on success
*/
public function connect(array $config) {
$config += $this->_baseConfig;

if ($config['timezone'] === 'UTC') {

This comment has been minimized.

Copy link
@jrbasso

jrbasso Mar 31, 2013

Member

As your default value is a string, what do you think in convert timezone strings to numbers? PHP can do it with DateTimeZone.

$config['timezone'] = '+0:00';
}

$config['init'][] = "SET time_zone = '+0:00'";

This comment has been minimized.

Copy link
@jrbasso

jrbasso Mar 31, 2013

Member

Shouldn't the value be from $config['timezone'] instead of hard coded?

This comment has been minimized.

Copy link
@lorenzo

lorenzo Mar 31, 2013

Author Member

Oops, yes

$config['flags'] += [
PDO::ATTR_PERSISTENT => $config['persistent'],
PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::MYSQL_ATTR_INIT_COMMAND => implode(';', (array)$config['init'])

This comment has been minimized.

Copy link
@markstory

markstory Mar 17, 2013

Member

In 2.4 there was a recent merge/pr for a settings array that allowed any number of SET commands to be issued after connection, would having something similar here be more generally useful?

This comment has been minimized.

Copy link
@lorenzo

lorenzo Mar 17, 2013

Author Member

Should we do a ['config' => 'value'] type of thing too? I'd also like to maintain 'init' for arbitrary SQL, but I don't see why both cannot live together :)

];

if (empty($config['dsn'])) {
Expand Down
@@ -0,0 +1,95 @@
<?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 Cake\Model\Datasource\Database\Driver;
use \PDO;

/**
* Tests Mysql driver
*
*/
class MysqlTests extends \Cake\TestSuite\TestCase {

This comment has been minimized.

Copy link
@markstory

markstory Mar 17, 2013

Member

Extra s in the class name.


/**
* Test connecting to Mysql with default configuration
*
* @return void
*/
public function testConnectionConfigDefault() {
$driver = $this->getMock('Cake\Model\Datasource\Database\driver\Mysql', ['_connect']);
$expected = [
'persistent' => true,
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'cake',
'port' => '3306',
'flags' => [],
'encoding' => 'utf8',
'timezone' => '+0:00',
'init' => ["SET time_zone = '+0:00'"],
'dsn' => 'mysql:host=localhost;port=3306;dbname=cake;charset=utf8'
];

$expected['flags'] += [
PDO::ATTR_PERSISTENT => true,
PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::MYSQL_ATTR_INIT_COMMAND => "SET time_zone = '+0:00'"
];
$driver->expects($this->once())->method('_connect')
->with($expected);
$driver->connect([]);
}

/**
* Test connecting to Mysql with custom configuration
*
* @return void
*/
public function testConnectionConfigCustom() {
$driver = $this->getMock('Cake\Model\Datasource\Database\driver\Mysql', ['_connect']);
$config = [
'persistent' => false,
'host' => 'foo',
'database' => 'bar',
'login' => 'user',
'password' => 'pass',
'port' => 3440,
'flags' => [1 => true, 2 => false],
'encoding' => 'a-language',
'timezone' => 'Antartica',
'init' => ['Execute this', 'this too']
];

$expected = $config;
$expected['dsn'] = 'mysql:host=foo;port=3440;dbname=bar;charset=a-language';
$expected['init'][] = "SET time_zone = '+0:00'";
$expected['flags'] += [
PDO::ATTR_PERSISTENT => false,
PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::MYSQL_ATTR_INIT_COMMAND => "Execute this;this too;SET time_zone = '+0:00'"
];
$driver->expects($this->once())->method('_connect')
->with($expected);
$driver->connect($config);
}

}

0 comments on commit 2a89ec0

Please sign in to comment.