2014-09-15 13:23:55 +02:00
|
|
|
<?php
|
|
|
|
|
2014-12-24 03:28:26 +01:00
|
|
|
namespace PicoDb\Driver;
|
2014-09-15 13:23:55 +02:00
|
|
|
|
2014-12-24 03:28:26 +01:00
|
|
|
use PDO;
|
2015-08-15 03:33:39 +02:00
|
|
|
use PDOException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mysql Driver
|
|
|
|
*
|
2016-07-31 00:41:42 +02:00
|
|
|
* @package PicoDb\Driver
|
|
|
|
* @author Frederic Guillot
|
2015-08-15 03:33:39 +02:00
|
|
|
*/
|
|
|
|
class Mysql extends Base
|
2014-12-24 03:28:26 +01:00
|
|
|
{
|
2015-08-15 03:33:39 +02:00
|
|
|
/**
|
|
|
|
* List of required settings options
|
|
|
|
*
|
|
|
|
* @access protected
|
|
|
|
* @var array
|
|
|
|
*/
|
2016-07-31 00:41:42 +02:00
|
|
|
protected $requiredAttributes = array(
|
2015-08-15 03:33:39 +02:00
|
|
|
'hostname',
|
|
|
|
'username',
|
|
|
|
'password',
|
|
|
|
'database',
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Table to store the schema version
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $schemaTable = 'schema_version';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new PDO connection
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param array $settings
|
|
|
|
*/
|
|
|
|
public function createConnection(array $settings)
|
2016-07-31 00:41:42 +02:00
|
|
|
{
|
|
|
|
$this->pdo = new PDO(
|
|
|
|
$this->buildDsn($settings),
|
|
|
|
$settings['username'],
|
|
|
|
$settings['password'],
|
|
|
|
$this->buildOptions($settings)
|
|
|
|
);
|
|
|
|
|
|
|
|
if (isset($settings['schema_table'])) {
|
|
|
|
$this->schemaTable = $settings['schema_table'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build connection DSN
|
|
|
|
*
|
|
|
|
* @access protected
|
|
|
|
* @param array $settings
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function buildDsn(array $settings)
|
2014-09-15 13:23:55 +02:00
|
|
|
{
|
2015-08-15 03:33:39 +02:00
|
|
|
$charset = empty($settings['charset']) ? 'utf8' : $settings['charset'];
|
|
|
|
$dsn = 'mysql:host='.$settings['hostname'].';dbname='.$settings['database'].';charset='.$charset;
|
2014-12-24 03:28:26 +01:00
|
|
|
|
2015-06-21 15:56:36 +02:00
|
|
|
if (! empty($settings['port'])) {
|
|
|
|
$dsn .= ';port='.$settings['port'];
|
|
|
|
}
|
|
|
|
|
2016-07-31 00:41:42 +02:00
|
|
|
return $dsn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build connection options
|
|
|
|
*
|
|
|
|
* @access protected
|
|
|
|
* @param array $settings
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function buildOptions(array $settings)
|
|
|
|
{
|
2014-09-15 13:23:55 +02:00
|
|
|
$options = array(
|
2014-12-24 03:28:26 +01:00
|
|
|
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET sql_mode = STRICT_ALL_TABLES',
|
2014-09-15 13:23:55 +02:00
|
|
|
);
|
|
|
|
|
2016-07-31 00:41:42 +02:00
|
|
|
if (! empty($settings['ssl_key'])) {
|
|
|
|
$options[PDO::MYSQL_ATTR_SSL_KEY] = $settings['ssl_key'];
|
|
|
|
}
|
2014-09-15 13:23:55 +02:00
|
|
|
|
2016-07-31 00:41:42 +02:00
|
|
|
if (! empty($settings['ssl_cert'])) {
|
|
|
|
$options[PDO::MYSQL_ATTR_SSL_CERT] = $settings['ssl_cert'];
|
2015-08-15 03:33:39 +02:00
|
|
|
}
|
2016-07-31 00:41:42 +02:00
|
|
|
|
|
|
|
if (! empty($settings['ssl_ca'])) {
|
|
|
|
$options[PDO::MYSQL_ATTR_SSL_CA] = $settings['ssl_ca'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $options;
|
2015-08-15 03:33:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enable foreign keys
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
public function enableForeignKeys()
|
|
|
|
{
|
|
|
|
$this->pdo->exec('SET FOREIGN_KEY_CHECKS=1');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disable foreign keys
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
public function disableForeignKeys()
|
|
|
|
{
|
|
|
|
$this->pdo->exec('SET FOREIGN_KEY_CHECKS=0');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return true if the error code is a duplicate key
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param integer $code
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function isDuplicateKeyError($code)
|
|
|
|
{
|
|
|
|
return $code == 23000;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Escape identifier
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $identifier
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function escape($identifier)
|
|
|
|
{
|
|
|
|
return '`'.$identifier.'`';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get non standard operator
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $operator
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getOperator($operator)
|
|
|
|
{
|
|
|
|
if ($operator === 'LIKE') {
|
|
|
|
return 'LIKE BINARY';
|
|
|
|
}
|
|
|
|
else if ($operator === 'ILIKE') {
|
|
|
|
return 'LIKE';
|
2014-09-15 13:23:55 +02:00
|
|
|
}
|
2015-08-15 03:33:39 +02:00
|
|
|
|
|
|
|
return '';
|
2014-09-15 13:23:55 +02:00
|
|
|
}
|
|
|
|
|
2015-08-15 03:33:39 +02:00
|
|
|
/**
|
|
|
|
* Get last inserted id
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return integer
|
|
|
|
*/
|
|
|
|
public function getLastId()
|
|
|
|
{
|
|
|
|
return $this->pdo->lastInsertId();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get current schema version
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return integer
|
|
|
|
*/
|
2014-09-15 13:23:55 +02:00
|
|
|
public function getSchemaVersion()
|
|
|
|
{
|
2016-07-31 00:41:42 +02:00
|
|
|
$this->pdo->exec("CREATE TABLE IF NOT EXISTS `".$this->schemaTable."` (`version` INT DEFAULT '0') ENGINE=InnoDB CHARSET=utf8");
|
2014-09-15 13:23:55 +02:00
|
|
|
|
2015-08-15 03:33:39 +02:00
|
|
|
$rq = $this->pdo->prepare('SELECT `version` FROM `'.$this->schemaTable.'`');
|
2014-09-15 13:23:55 +02:00
|
|
|
$rq->execute();
|
2015-08-15 03:33:39 +02:00
|
|
|
$result = $rq->fetchColumn();
|
2014-09-15 13:23:55 +02:00
|
|
|
|
2015-08-15 03:33:39 +02:00
|
|
|
if ($result !== false) {
|
|
|
|
return (int) $result;
|
2014-09-15 13:23:55 +02:00
|
|
|
}
|
|
|
|
else {
|
2015-08-15 03:33:39 +02:00
|
|
|
$this->pdo->exec('INSERT INTO `'.$this->schemaTable.'` VALUES(0)');
|
2014-09-15 13:23:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-15 03:33:39 +02:00
|
|
|
/**
|
|
|
|
* Set current schema version
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param integer $version
|
|
|
|
*/
|
2014-09-15 13:23:55 +02:00
|
|
|
public function setSchemaVersion($version)
|
|
|
|
{
|
2015-08-15 03:33:39 +02:00
|
|
|
$rq = $this->pdo->prepare('UPDATE `'.$this->schemaTable.'` SET `version`=?');
|
2014-09-15 13:23:55 +02:00
|
|
|
$rq->execute(array($version));
|
|
|
|
}
|
|
|
|
|
2015-08-15 03:33:39 +02:00
|
|
|
/**
|
|
|
|
* Upsert for a key/value variable
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $table
|
|
|
|
* @param string $keyColumn
|
|
|
|
* @param string $valueColumn
|
|
|
|
* @param array $dictionary
|
|
|
|
* @return bool False on failure
|
|
|
|
*/
|
|
|
|
public function upsert($table, $keyColumn, $valueColumn, array $dictionary)
|
2014-09-15 13:23:55 +02:00
|
|
|
{
|
2015-08-15 03:33:39 +02:00
|
|
|
try {
|
2014-09-15 13:23:55 +02:00
|
|
|
|
2015-08-15 03:33:39 +02:00
|
|
|
$sql = sprintf(
|
|
|
|
'REPLACE INTO %s (%s, %s) VALUES %s',
|
|
|
|
$this->escape($table),
|
|
|
|
$this->escape($keyColumn),
|
|
|
|
$this->escape($valueColumn),
|
|
|
|
implode(', ', array_fill(0, count($dictionary), '(?, ?)'))
|
|
|
|
);
|
2014-12-29 22:52:36 +01:00
|
|
|
|
2015-08-15 03:33:39 +02:00
|
|
|
$values = array();
|
2014-12-29 22:52:36 +01:00
|
|
|
|
2015-08-15 03:33:39 +02:00
|
|
|
foreach ($dictionary as $key => $value) {
|
|
|
|
$values[] = $key;
|
|
|
|
$values[] = $value;
|
|
|
|
}
|
2015-01-07 01:08:10 +01:00
|
|
|
|
2015-08-15 03:33:39 +02:00
|
|
|
$rq = $this->pdo->prepare($sql);
|
|
|
|
$rq->execute($values);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch (PDOException $e) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-01-07 01:08:10 +01:00
|
|
|
}
|
2015-08-15 03:33:39 +02:00
|
|
|
}
|