Skip to content

Upsert + Refactor BaseBuilder *Batch() Methods #6407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
651 changes: 500 additions & 151 deletions system/Database/BaseBuilder.php

Large diffs are not rendered by default.

78 changes: 78 additions & 0 deletions system/Database/MySQLi/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace CodeIgniter\Database\MySQLi;

use CodeIgniter\Database\BaseBuilder;
use CodeIgniter\Database\Exceptions\DatabaseException;
use CodeIgniter\Database\RawSql;

/**
* Builder for MySQLi
Expand Down Expand Up @@ -53,4 +55,80 @@ protected function _fromTables(): string

return implode(', ', $this->QBFrom);
}

/**
* Generates a platform-specific batch update string from the supplied data
*/
protected function _updateBatch(string $table, array $keys, array $values): string
{
$sql = $this->QBOptions['sql'] ?? '';

// if this is the first iteration of batch then we need to build skeleton sql
if ($sql === '') {
$constraints = $this->QBOptions['constraints'] ?? [];

if ($constraints === []) {
if ($this->db->DBDebug) {
throw new DatabaseException('You must specify a constraint to match on for batch updates.'); // @codeCoverageIgnore
}

return ''; // @codeCoverageIgnore
}

$updateFields = $this->QBOptions['updateFields'] ??
$this->updateFields($keys, false, $constraints)->QBOptions['updateFields'] ??
[];

$alias = $this->QBOptions['alias'] ?? '`_u`';

$sql = 'UPDATE ' . $this->compileIgnore('update') . $table . "\n";

$sql .= 'INNER JOIN (' . "\n%s";

$sql .= ') ' . $alias . "\n";

$sql .= 'ON ' . implode(
' AND ',
array_map(
static fn ($key) => ($key instanceof RawSql ?
$key :
$table . '.' . $key . ' = ' . $alias . '.' . $key),
$constraints
)
) . "\n";

$sql .= 'SET' . "\n";

$sql .= implode(
",\n",
array_map(
static fn ($key, $value) => $table . '.' . $key . ($value instanceof RawSql ?
' = ' . $value :
' = ' . $alias . '.' . $value),
array_keys($updateFields),
$updateFields
)
);

$this->QBOptions['sql'] = $sql;
}

if (isset($this->QBOptions['fromQuery'])) {
$data = $this->QBOptions['fromQuery'];
} else {
$data = implode(
" UNION ALL\n",
array_map(
static fn ($value) => 'SELECT ' . implode(', ', array_map(
static fn ($key, $index) => $index . ' ' . $key,
$keys,
$value
)),
$values
)
) . "\n";
}

return sprintf($sql, $data);
}
}
222 changes: 207 additions & 15 deletions system/Database/OCI8/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use CodeIgniter\Database\BaseBuilder;
use CodeIgniter\Database\Exceptions\DatabaseException;
use CodeIgniter\Database\RawSql;

/**
* Builder for OCI8
Expand Down Expand Up @@ -67,29 +68,141 @@ class Builder extends BaseBuilder
*/
protected function _insertBatch(string $table, array $keys, array $values): string
{
$insertKeys = implode(', ', $keys);
$hasPrimaryKey = in_array('PRIMARY', array_column($this->db->getIndexData($table), 'type'), true);
$sql = $this->QBOptions['sql'] ?? '';

// ORA-00001 measures
if ($hasPrimaryKey) {
$sql = 'INSERT INTO ' . $table . ' (' . $insertKeys . ") \n SELECT * FROM (\n";
$selectQueryValues = [];
// if this is the first iteration of batch then we need to build skeleton sql
if ($sql === '') {
$insertKeys = implode(', ', $keys);
$hasPrimaryKey = in_array('PRIMARY', array_column($this->db->getIndexData($table), 'type'), true);

foreach ($values as $value) {
$selectValues = implode(',', array_map(static fn ($value, $key) => $value . ' as ' . $key, explode(',', substr(substr($value, 1), 0, -1)), $keys));
$selectQueryValues[] = 'SELECT ' . $selectValues . ' FROM DUAL';
}
// ORA-00001 measures
$sql = 'INSERT' . ($hasPrimaryKey ? '' : ' ALL') . ' INTO ' . $table . ' (' . $insertKeys . ")\n%s";

$this->QBOptions['sql'] = $sql;
}

return $sql . implode("\n UNION ALL \n", $selectQueryValues) . "\n)";
if (isset($this->QBOptions['fromQuery'])) {
$data = $this->QBOptions['fromQuery'];
} else {
$data = implode(
" FROM DUAL UNION ALL\n",
array_map(
static fn ($value) => 'SELECT ' . implode(', ', array_map(
static fn ($key, $index) => $index . ' ' . $key,
$keys,
$value
)),
$values
)
) . " FROM DUAL\n";
}

$sql = "INSERT ALL\n";
return sprintf($sql, $data);
}

/**
* Generates a platform-specific upsertBatch string from the supplied data
*
* @throws DatabaseException
*/
protected function _upsertBatch(string $table, array $keys, array $values): string
{
$sql = $this->QBOptions['sql'] ?? '';

// if this is the first iteration of batch then we need to build skeleton sql
if ($sql === '') {
$constraints = $this->QBOptions['constraints'] ?? [];

if (empty($constraints)) {
$fieldNames = array_map(static fn ($columnName) => trim($columnName, '"'), $keys);

$uniqueIndexes = array_filter($this->db->getIndexData($table), static function ($index) use ($fieldNames) {
$hasAllFields = count(array_intersect($index->fields, $fieldNames)) === count($index->fields);

return ($index->type === 'PRIMARY' || $index->type === 'UNIQUE') && $hasAllFields;
});

// only take first index
foreach ($uniqueIndexes as $index) {
$constraints = $index->fields;
break;
}

$constraints = $this->onConstraint($constraints)->QBOptions['constraints'] ?? [];
}

if (empty($constraints)) {
if ($this->db->DBDebug) {
throw new DatabaseException('No constraint found for upsert.');
}

return ''; // @codeCoverageIgnore
}

$updateFields = $this->QBOptions['updateFields'] ?? $this->updateFields($keys, false, $constraints)->QBOptions['updateFields'] ?? [];

if (empty($updateFields)) {
$updateFields = array_filter(
$keys,
static fn ($columnName) => ! (in_array($columnName, $constraints, true))
);

$this->QBOptions['updateFields'] = $updateFields;
}

$sql = 'MERGE INTO ' . $table . "\nUSING (\n%s";

$sql .= ') "_upsert"' . "\nON (";

$sql .= implode(
' AND ',
array_map(
static fn ($key) => ($key instanceof RawSql ?
$key :
$table . '.' . $key . ' = ' . '"_upsert".' . $key),
$constraints
)
) . ")\n";

$sql .= "WHEN MATCHED THEN UPDATE SET\n";

foreach ($values as $value) {
$sql .= ' INTO ' . $table . ' (' . $insertKeys . ') VALUES ' . $value . "\n";
$sql .= implode(
",\n",
array_map(
static fn ($key, $value) => $key . ($value instanceof RawSql ?
' = ' . $value :
' = ' . '"_upsert".' . $value),
array_keys($updateFields),
$updateFields
)
);

$sql .= "\nWHEN NOT MATCHED THEN INSERT (" . implode(', ', $keys) . ")\nVALUES ";

$sql .= (' ('
. implode(', ', array_map(static fn ($columnName) => '"_upsert".' . $columnName, $keys))
. ')');

$this->QBOptions['sql'] = $sql;
}

if (isset($this->QBOptions['fromQuery'])) {
$data = $this->QBOptions['fromQuery'];
} else {
$data = implode(
" FROM DUAL UNION ALL\n",
array_map(
static fn ($value) => 'SELECT ' . implode(', ', array_map(
static fn ($key, $index) => $index . ' ' . $key,
$keys,
$value
)),
$values
)
) . " FROM DUAL\n";
}

return $sql . 'SELECT * FROM DUAL';
return sprintf($sql, $data);
}

/**
Expand Down Expand Up @@ -227,4 +340,83 @@ protected function resetSelect()
$this->limitUsed = false;
parent::resetSelect();
}

/**
* Generates a platform-specific batch update string from the supplied data
*/
protected function _updateBatch(string $table, array $keys, array $values): string
{
$sql = $this->QBOptions['sql'] ?? '';

// if this is the first iteration of batch then we need to build skeleton sql
if ($sql === '') {
$constraints = $this->QBOptions['constraints'] ?? [];

if ($constraints === []) {
if ($this->db->DBDebug) {
throw new DatabaseException('You must specify a constraint to match on for batch updates.');
}

return ''; // @codeCoverageIgnore
}

$updateFields = $this->QBOptions['updateFields'] ??
$this->updateFields($keys, false, $constraints)->QBOptions['updateFields'] ??
[];

$alias = $this->QBOptions['alias'] ?? '"_u"';

// Oracle doesn't support ignore on updates so we will use MERGE
$sql = 'MERGE INTO ' . $table . "\n";

$sql .= 'USING (' . "\n%s";

$sql .= ') ' . $alias . "\n";

$sql .= 'ON (' . implode(
' AND ',
array_map(
static fn ($key) => ($key instanceof RawSql ?
$key :
$table . '.' . $key . ' = ' . $alias . '.' . $key),
$constraints
)
) . ")\n";

$sql .= "WHEN MATCHED THEN UPDATE\n";

$sql .= 'SET' . "\n";

$sql .= implode(
",\n",
array_map(
static fn ($key, $value) => $table . '.' . $key . ($value instanceof RawSql ?
' = ' . $value :
' = ' . $alias . '.' . $value),
array_keys($updateFields),
$updateFields
)
);

$this->QBOptions['sql'] = $sql;
}

if (isset($this->QBOptions['fromQuery'])) {
$data = $this->QBOptions['fromQuery'];
} else {
$data = implode(
" UNION ALL\n",
array_map(
static fn ($value) => 'SELECT ' . implode(', ', array_map(
static fn ($key, $index) => $index . ' ' . $key,
$keys,
$value
)) . ' FROM DUAL',
$values
)
) . "\n";
}

return sprintf($sql, $data);
}
}
2 changes: 1 addition & 1 deletion system/Database/OCI8/Forge.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ protected function _attributeAutoIncrement(array &$attributes, array &$field)
&& stripos($field['type'], 'NUMBER') !== false
&& version_compare($this->db->getVersion(), '12.1', '>=')
) {
$field['auto_increment'] = ' GENERATED BY DEFAULT AS IDENTITY';
$field['auto_increment'] = ' GENERATED BY DEFAULT ON NULL AS IDENTITY';
}
}

Expand Down
Loading