2022-05-19 10:58:25 +02:00
|
|
|
<?php
|
|
|
|
App::uses('Mysql', 'Model/Datasource/Database');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Overrides the default MySQL database implementation to support the following features:
|
|
|
|
* - Set query hints to optimize queries
|
|
|
|
*/
|
|
|
|
class MysqlExtended extends Mysql
|
|
|
|
{
|
2024-01-27 12:43:49 +01:00
|
|
|
const PDO_MAP = [
|
|
|
|
'integer' => PDO::PARAM_INT,
|
|
|
|
'float' => PDO::PARAM_STR,
|
|
|
|
'boolean' => PDO::PARAM_BOOL,
|
|
|
|
'string' => PDO::PARAM_STR,
|
|
|
|
'text' => PDO::PARAM_STR
|
|
|
|
];
|
|
|
|
|
2022-05-14 11:26:26 +02:00
|
|
|
/**
|
|
|
|
* Output MD5 as binary, that is faster and uses less memory
|
|
|
|
* @param string $value
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function cacheMethodHasher($value)
|
|
|
|
{
|
|
|
|
return md5($value, true);
|
|
|
|
}
|
|
|
|
|
2022-05-19 10:58:25 +02:00
|
|
|
/**
|
|
|
|
* Builds and generates an SQL statement from an array. Handles final clean-up before conversion.
|
|
|
|
*
|
|
|
|
* @param array $query An array defining an SQL query.
|
|
|
|
* @param Model $Model The model object which initiated the query.
|
|
|
|
* @return string An executable SQL statement.
|
|
|
|
* @see DboSource::renderStatement()
|
|
|
|
*/
|
|
|
|
public function buildStatement($query, Model $Model)
|
|
|
|
{
|
|
|
|
$query = array_merge($this->_queryDefaults, $query);
|
|
|
|
|
|
|
|
if (!empty($query['joins'])) {
|
|
|
|
$count = count($query['joins']);
|
|
|
|
for ($i = 0; $i < $count; $i++) {
|
|
|
|
if (is_array($query['joins'][$i])) {
|
|
|
|
$query['joins'][$i] = $this->buildJoinStatement($query['joins'][$i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->renderStatement('select', array(
|
|
|
|
'conditions' => $this->conditions($query['conditions'], true, true, $Model),
|
|
|
|
'fields' => implode(', ', $query['fields']),
|
|
|
|
'table' => $query['table'],
|
|
|
|
'alias' => $this->alias . $this->name($query['alias']),
|
|
|
|
'order' => $this->order($query['order'], 'ASC', $Model),
|
|
|
|
'limit' => $this->limit($query['limit'], $query['offset']),
|
|
|
|
'joins' => implode(' ', $query['joins']),
|
|
|
|
'group' => $this->group($query['group'], $Model),
|
|
|
|
'having' => $this->having($query['having'], true, $Model),
|
|
|
|
'lock' => $this->getLockingHint($query['lock']),
|
2022-10-04 16:22:34 +02:00
|
|
|
'indexHint' => $this->__buildIndexHint($query['forceIndexHint'] ?? null),
|
2024-03-20 13:04:36 +01:00
|
|
|
'ignoreIndexHint' => $this->__buildIgnoreIndexHint($query['ignoreIndexHint'] ?? null)
|
2022-05-19 10:58:25 +02:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds an SQL statement.
|
|
|
|
*
|
|
|
|
* This is merely a convenient wrapper to DboSource::buildStatement().
|
|
|
|
*
|
|
|
|
* @param Model $Model The model to build an association query for.
|
|
|
|
* @param array $queryData An array of queryData information containing keys similar to Model::find().
|
|
|
|
* @return string String containing an SQL statement.
|
|
|
|
* @see DboSource::buildStatement()
|
|
|
|
* @see DboSource::buildAssociationQuery()
|
|
|
|
*/
|
|
|
|
public function buildAssociationQuery(Model $Model, $queryData)
|
|
|
|
{
|
|
|
|
$queryData = $this->_scrubQueryData($queryData);
|
|
|
|
|
|
|
|
return $this->buildStatement(
|
|
|
|
array(
|
|
|
|
'fields' => $this->prepareFields($Model, $queryData),
|
|
|
|
'table' => $this->fullTableName($Model),
|
|
|
|
'alias' => $Model->alias,
|
|
|
|
'limit' => $queryData['limit'],
|
|
|
|
'offset' => $queryData['offset'],
|
|
|
|
'joins' => $queryData['joins'],
|
|
|
|
'conditions' => $queryData['conditions'],
|
|
|
|
'order' => $queryData['order'],
|
|
|
|
'group' => $queryData['group'],
|
|
|
|
'having' => $queryData['having'],
|
|
|
|
'lock' => $queryData['lock'],
|
2022-10-04 16:22:34 +02:00
|
|
|
'forceIndexHint' => $queryData['forceIndexHint'] ?? null,
|
2024-03-20 13:04:36 +01:00
|
|
|
'ignoreIndexHint' => $queryData['ignoreIndexHint'] ?? null,
|
2022-05-19 10:58:25 +02:00
|
|
|
),
|
|
|
|
$Model
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders a final SQL statement by putting together the component parts in the correct order
|
|
|
|
*
|
|
|
|
* Edit: Added support for query hints
|
|
|
|
*
|
|
|
|
* @param string $type type of query being run. e.g select, create, update, delete, schema, alter.
|
|
|
|
* @param array $data Array of data to insert into the query.
|
|
|
|
* @return string|null Rendered SQL expression to be run, otherwise null.\
|
|
|
|
* @see DboSource::renderStatement()
|
|
|
|
*/
|
|
|
|
public function renderStatement($type, $data)
|
|
|
|
{
|
2022-05-21 09:40:29 +02:00
|
|
|
if ($type === 'select') {
|
2022-05-19 10:58:25 +02:00
|
|
|
extract($data);
|
|
|
|
$having = !empty($having) ? " $having" : '';
|
2022-05-21 09:40:29 +02:00
|
|
|
$lock = !empty($lock) ? " $lock" : '';
|
|
|
|
return rtrim("SELECT {$fields} FROM {$table} {$alias} {$indexHint} {$joins} {$conditions} {$group}{$having} {$order} {$limit}{$lock}");
|
2022-05-19 10:58:25 +02:00
|
|
|
}
|
2022-05-21 09:40:29 +02:00
|
|
|
return parent::renderStatement($type, $data);
|
2022-05-19 10:58:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-03-20 13:04:36 +01:00
|
|
|
* Builds the force index hint for the query
|
2022-05-19 10:58:25 +02:00
|
|
|
*
|
2024-03-20 13:04:36 +01:00
|
|
|
* @param string|null $forceIndexHint INDEX hint
|
2022-05-19 10:58:25 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2022-10-04 16:22:34 +02:00
|
|
|
private function __buildIndexHint($forceIndexHint = null): ?string
|
2022-05-19 10:58:25 +02:00
|
|
|
{
|
2024-03-20 13:04:36 +01:00
|
|
|
return isset($forceIndexHint) ? ('FORCE INDEX (' . $forceIndexHint . ')') : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds the ignore index hint for the query
|
|
|
|
*
|
|
|
|
* @param string|null $ignoreIndexHint INDEX hint
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function __buildIgnoreIndexHint($ignoreIndexHint = null): ?string
|
|
|
|
{
|
|
|
|
return isset($ignoreIndexHint) ? ('IGNORE INDEX (' . $ignoreIndexHint . ')') : null;
|
2022-05-19 10:58:25 +02:00
|
|
|
}
|
2022-05-14 11:26:26 +02:00
|
|
|
|
2022-11-13 17:37:51 +01:00
|
|
|
/**
|
|
|
|
* - Do not call microtime when not necessary
|
|
|
|
* - Count query count even when logging is disabled
|
|
|
|
*
|
|
|
|
* @param string $sql
|
|
|
|
* @param array $options
|
|
|
|
* @param array $params
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function execute($sql, $options = [], $params = [])
|
|
|
|
{
|
|
|
|
$log = $options['log'] ?? $this->fullDebug;
|
|
|
|
|
|
|
|
if ($log) {
|
|
|
|
$t = microtime(true);
|
|
|
|
$this->_result = $this->_execute($sql, $params);
|
|
|
|
$this->took = round((microtime(true) - $t) * 1000);
|
|
|
|
$this->numRows = $this->affected = $this->lastAffected();
|
|
|
|
$this->logQuery($sql, $params);
|
|
|
|
} else {
|
|
|
|
$this->_result = $this->_execute($sql, $params);
|
|
|
|
$this->_queriesCnt++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->_result;
|
|
|
|
}
|
|
|
|
|
2022-05-14 11:26:26 +02:00
|
|
|
/**
|
|
|
|
* Reduce memory usage for insertMulti
|
|
|
|
*
|
|
|
|
* @param string $table
|
|
|
|
* @param array $fields
|
|
|
|
* @param array $values
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function insertMulti($table, $fields, $values)
|
|
|
|
{
|
|
|
|
$table = $this->fullTableName($table);
|
2024-01-27 12:43:49 +01:00
|
|
|
$holder = substr(str_repeat('?,', count($fields)), 0, -1);
|
2022-05-14 11:26:26 +02:00
|
|
|
$fields = implode(',', array_map([$this, 'name'], $fields));
|
2024-01-27 12:43:49 +01:00
|
|
|
|
2022-05-14 11:26:26 +02:00
|
|
|
$columnMap = [];
|
|
|
|
foreach ($values[key($values)] as $key => $val) {
|
|
|
|
if (is_int($val)) {
|
|
|
|
$columnMap[$key] = PDO::PARAM_INT;
|
|
|
|
} elseif (is_bool($val)) {
|
|
|
|
$columnMap[$key] = PDO::PARAM_BOOL;
|
|
|
|
} else {
|
|
|
|
$type = $this->introspectType($val);
|
2024-01-27 12:43:49 +01:00
|
|
|
$columnMap[$key] = self::PDO_MAP[$type];
|
2022-05-14 11:26:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$sql = "INSERT INTO $table ($fields) VALUES ";
|
2024-01-27 12:43:49 +01:00
|
|
|
$sql .= substr(str_repeat("($holder),", count($values)), 0, -1);
|
2022-05-14 11:26:26 +02:00
|
|
|
$statement = $this->_connection->prepare($sql);
|
|
|
|
$valuesList = array();
|
2024-01-27 12:43:49 +01:00
|
|
|
$i = 0;
|
2022-05-14 11:26:26 +02:00
|
|
|
foreach ($values as $value) {
|
|
|
|
foreach ($value as $col => $val) {
|
|
|
|
if ($this->fullDebug) {
|
|
|
|
$valuesList[] = $val;
|
|
|
|
}
|
2024-01-27 12:43:49 +01:00
|
|
|
$statement->bindValue(++$i, $val, $columnMap[$col]);
|
2022-05-14 11:26:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$result = $statement->execute();
|
|
|
|
$statement->closeCursor();
|
|
|
|
if ($this->fullDebug) {
|
|
|
|
$this->logQuery($sql, $valuesList);
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
2022-05-21 09:40:29 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function value($data, $column = null, $null = true)
|
|
|
|
{
|
|
|
|
// Fast check if data is int, then return value
|
|
|
|
if (is_int($data)) {
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No need to quote bool values
|
|
|
|
if (is_bool($data)) {
|
|
|
|
return $data ? '1' : '0';
|
|
|
|
}
|
|
|
|
|
|
|
|
// No need to call expensive array_map
|
|
|
|
if (is_array($data) && !empty($data)) {
|
|
|
|
$output = [];
|
|
|
|
foreach ($data as $d) {
|
|
|
|
if (is_int($d)) {
|
|
|
|
$output[] = $d;
|
|
|
|
} else {
|
|
|
|
$output[] = parent::value($d, $column);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent::value($data, $column, $null);
|
|
|
|
}
|
2022-05-19 10:58:25 +02:00
|
|
|
}
|