new: [internal] Experimental MysqlExtended driver

pull/8373/head
Jakub Onderka 2022-05-14 11:26:26 +02:00
parent 197544e5c6
commit 0245034373
1 changed files with 64 additions and 0 deletions

View File

@ -7,6 +7,16 @@ App::uses('Mysql', 'Model/Datasource/Database');
*/
class MysqlExtended extends Mysql
{
/**
* Output MD5 as binary, that is faster and uses less memory
* @param string $value
* @return string
*/
public function cacheMethodHasher($value)
{
return md5($value, true);
}
/**
* Builds and generates an SQL statement from an array. Handles final clean-up before conversion.
*
@ -113,4 +123,58 @@ class MysqlExtended extends Mysql
}
return $index;
}
/**
* 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);
$holder = implode(',', array_fill(0, count($fields), '?'));
$fields = implode(',', array_map([$this, 'name'], $fields));
$pdoMap = [
'integer' => PDO::PARAM_INT,
'float' => PDO::PARAM_STR,
'boolean' => PDO::PARAM_BOOL,
'string' => PDO::PARAM_STR,
'text' => PDO::PARAM_STR
];
$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);
$columnMap[$key] = $pdoMap[$type];
}
}
$sql = "INSERT INTO $table ($fields) VALUES ";
$sql .= implode(',', array_fill(0, count($values), "($holder)"));
$statement = $this->_connection->prepare($sql);
$valuesList = array();
$i = 1;
foreach ($values as $value) {
foreach ($value as $col => $val) {
if ($this->fullDebug) {
$valuesList[] = $val;
}
$statement->bindValue($i, $val, $columnMap[$col]);
$i++;
}
}
$result = $statement->execute();
$statement->closeCursor();
if ($this->fullDebug) {
$this->logQuery($sql, $valuesList);
}
return $result;
}
}