Skip to content

Commit

Permalink
Refactor the MySQL connector.
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Jan 2, 2017
1 parent 91ea408 commit ae63d73
Showing 1 changed file with 77 additions and 25 deletions.
102 changes: 77 additions & 25 deletions Connectors/MySqlConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,61 @@ public function connect(array $config)
$connection->exec("use `{$config['database']}`;");
}

$collation = $config['collation'];
$this->configureEncoding($connection, $config);

// Next we will set the "names" and "collation" on the clients connections so
// a correct character set will be used by this client. The collation also
// is set on the server but needs to be set here on this client objects.
if (isset($config['charset'])) {
$charset = $config['charset'];

$names = "set names '{$charset}'".
(! is_null($collation) ? " collate '{$collation}'" : '');

$connection->prepare($names)->execute();
}
// Next, we will check to see if a timezone has been specified in this config
// and if it has we will issue a statement to modify the timezone with the
// database. Setting this DB timezone is an optional configuration item.
if (isset($config['timezone'])) {
$connection->prepare(
'set time_zone="'.$config['timezone'].'"'
)->execute();
}
$this->configureTimezone($connection, $config);

$this->setModes($connection, $config);

return $connection;
}

/**
* Set the connection character set and collation.
*
* @param \PDO $connection
* @param array $config
* @return void
*/
protected function configureEncoding($connection, array $config)
{
if (! isset($config['charset'])) {
return $connection;
}

$connection->prepare(
"set names '{$config['charset']}'".$this->getCollation($config)
)->execute();
}

/**
* Get the collation for the configuration.
*
* @param array $config
* @return string
*/
protected function getCollation(array $config)
{
return ! is_null($config['collation']) ? " collate '{$config['collation']}'" : '';
}

/**
* Set the timezone on the connection.
*
* @param \PDO $connection
* @param array $config
* @return void
*/
protected function configureTimezone($connection, array $config)
{
if (isset($config['timezone'])) {
$connection->prepare('set time_zone="'.$config['timezone'].'"')->execute();
}
}

/**
* Create a DSN string from a configuration.
*
Expand All @@ -64,7 +92,9 @@ public function connect(array $config)
*/
protected function getDsn(array $config)
{
return $this->configHasSocket($config) ? $this->getSocketDsn($config) : $this->getHostDsn($config);
return $this->hasSocket($config)
? $this->getSocketDsn($config)
: $this->getHostDsn($config);
}

/**
Expand All @@ -73,7 +103,7 @@ protected function getDsn(array $config)
* @param array $config
* @return bool
*/
protected function configHasSocket(array $config)
protected function hasSocket(array $config)
{
return isset($config['unix_socket']) && ! empty($config['unix_socket']);
}
Expand All @@ -100,8 +130,8 @@ protected function getHostDsn(array $config)
extract($config, EXTR_SKIP);

return isset($port)
? "mysql:host={$host};port={$port};dbname={$database}"
: "mysql:host={$host};dbname={$database}";
? "mysql:host={$host};port={$port};dbname={$database}"
: "mysql:host={$host};dbname={$database}";
}

/**
Expand All @@ -114,15 +144,37 @@ protected function getHostDsn(array $config)
protected function setModes(PDO $connection, array $config)
{
if (isset($config['modes'])) {
$modes = implode(',', $config['modes']);

$connection->prepare("set session sql_mode='{$modes}'")->execute();
$this->setCustomModes($connection, $config);
} elseif (isset($config['strict'])) {
if ($config['strict']) {
$connection->prepare("set session sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'")->execute();
$connection->prepare($this->strictMode())->execute();
} else {
$connection->prepare("set session sql_mode='NO_ENGINE_SUBSTITUTION'")->execute();
}
}
}

/**
* Set the custom modes on the connection.
*
* @param \PDO $connection
* @param array $config
* @return void
*/
protected function setCustomModes(PDO $connection, array $config)
{
$modes = implode(',', $config['modes']);

$connection->prepare("set session sql_mode='{$modes}'")->execute();
}

/**
* Get the query to enable strict mode.
*
* @return string
*/
protected function strictMode()
{
return "set session sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'";
}
}

0 comments on commit ae63d73

Please sign in to comment.