Skip to content

Commit

Permalink
refactor: vendor/bin/rector
Browse files Browse the repository at this point in the history
  • Loading branch information
ytetsuro committed Sep 15, 2021
1 parent d48912a commit a410c77
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 106 deletions.
69 changes: 35 additions & 34 deletions system/Database/OCI8/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace CodeIgniter\Database\OCI8;

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

/**
Expand Down Expand Up @@ -73,24 +74,24 @@ class Builder extends BaseBuilder
protected function _insertBatch(string $table, array $keys, array $values): string
{
$keys = implode(', ', $keys);
$has_primary_key = in_array('PRIMARY', array_column($this->db->getIndexData($table), 'type'), true);
$hasPrimaryKey = in_array('PRIMARY', array_column($this->db->getIndexData($table), 'type'), true);

// ORA-00001 measures
if ($has_primary_key) {
if ($hasPrimaryKey) {
$sql = 'INSERT INTO ' . $table . ' (' . $keys . ") \n SELECT * FROM (\n";
$select_query_values = [];
$selectQueryValues = [];

for ($i = 0, $c = count($values); $i < $c; $i++) {
$select_query_values[] = 'SELECT ' . substr(substr($values[$i], 1), 0, -1) . ' FROM DUAL';
foreach ($values as $value) {
$selectQueryValues[] = 'SELECT ' . substr(substr($value, 1), 0, -1) . ' FROM DUAL';
}

return $sql . implode("\n UNION ALL \n", $select_query_values) . "\n)";
return $sql . implode("\n UNION ALL \n", $selectQueryValues) . "\n)";
}

$sql = "INSERT ALL\n";

for ($i = 0, $c = count($values); $i < $c; $i++) {
$sql .= ' INTO ' . $table . ' (' . $keys . ') VALUES ' . $values[$i] . "\n";
foreach ($values as $value) {
$sql .= ' INTO ' . $table . ' (' . $keys . ') VALUES ' . $value . "\n";
}

return $sql . 'SELECT * FROM dual';
Expand All @@ -107,18 +108,18 @@ protected function _insertBatch(string $table, array $keys, array $values): stri
*/
protected function _replace(string $table, array $keys, array $values): string
{
$field_names = array_map(static function ($column_name) {
return trim($column_name, '"');
$fieldNames = array_map(static function ($columnName) {
return trim($columnName, '"');
}, $keys);

$unique_indexes = array_filter($this->db->getIndexData($table), static function ($index) use ($field_names) {
$has_all_fields = count(array_intersect($index->fields, $field_names)) === count($index->fields);
$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') && $has_all_fields;
return ($index->type === 'PRIMARY') && $hasAllFields;
});
$replaceable_fields = array_filter($keys, static function ($column_name) use ($unique_indexes) {
foreach ($unique_indexes as $index) {
if (in_array(trim($column_name, '"'), $index->fields, true)) {
$replaceableFields = array_filter($keys, static function ($columnName) use ($uniqueIndexes) {
foreach ($uniqueIndexes as $index) {
if (in_array(trim($columnName, '"'), $index->fields, true)) {
return false;
}
}
Expand All @@ -128,31 +129,31 @@ protected function _replace(string $table, array $keys, array $values): string

$sql = 'MERGE INTO ' . $table . "\n USING (SELECT ";

$sql .= implode(', ', array_map(static function ($column_name, $value) {
return $value . ' ' . $column_name;
$sql .= implode(', ', array_map(static function ($columnName, $value) {
return $value . ' ' . $columnName;
}, $keys, $values));

$sql .= ' FROM DUAL) "_replace" ON ( ';

$on_list = [];
$on_list[] = '1 != 1';
$onList = [];
$onList[] = '1 != 1';

foreach ($unique_indexes as $index) {
$on_list[] = '(' . implode(' AND ', array_map(static function ($column_name) use ($table) {
return $table . '."' . $column_name . '" = "_replace"."' . $column_name . '"';
foreach ($uniqueIndexes as $index) {
$onList[] = '(' . implode(' AND ', array_map(static function ($columnName) use ($table) {
return $table . '."' . $columnName . '" = "_replace"."' . $columnName . '"';
}, $index->fields)) . ')';
}

$sql .= implode(' OR ', $on_list) . ') WHEN MATCHED THEN UPDATE SET ';
$sql .= implode(' OR ', $onList) . ') WHEN MATCHED THEN UPDATE SET ';

$sql .= implode(', ', array_map(static function ($column_name) {
return $column_name . ' = "_replace".' . $column_name;
}, $replaceable_fields));
$sql .= implode(', ', array_map(static function ($columnName) {
return $columnName . ' = "_replace".' . $columnName;
}, $replaceableFields));

$sql .= ' WHEN NOT MATCHED THEN INSERT (' . implode(', ', $replaceable_fields) . ') VALUES ';
$sql .= ' (' . implode(', ', array_map(static function ($column_name) {
return '"_replace".' . $column_name;
}, $replaceable_fields)) . ')';
$sql .= ' WHEN NOT MATCHED THEN INSERT (' . implode(', ', $replaceableFields) . ') VALUES ';
$sql .= ' (' . implode(', ', array_map(static function ($columnName) {
return '"_replace".' . $columnName;
}, $replaceableFields)) . ')';

return $sql;
}
Expand Down Expand Up @@ -180,17 +181,17 @@ protected function _truncate(string $table): string
* @param mixed $where The where clause
* @param int $limit The limit clause
*
* @throws \CodeIgniter\Database\Exceptions\DatabaseException
* @throws DatabaseException
*
* @return mixed
*/
public function delete($where = '', ?int $limit = null, bool $reset_data = true)
public function delete($where = '', ?int $limit = null, bool $resetData = true)
{
if (! empty($limit)) {
$this->QBLimit = $limit;
}

return parent::delete($where, null, $reset_data);
return parent::delete($where, null, $resetData);
}

/**
Expand Down
45 changes: 24 additions & 21 deletions system/Database/OCI8/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

/**
* Connection for Postgre
*
* @property string|null $latestInsertedTableName
* @property int|null $rowId
*/
class Connection extends BaseConnection implements ConnectionInterface
{
Expand Down Expand Up @@ -173,10 +176,10 @@ public function getVersion(): string
return $this->dataCache['version'];
}

if (! $this->connID || ($version_string = oci_server_version($this->connID)) === false) {
if (! $this->connID || ($versionString = oci_server_version($this->connID)) === false) {
return '';
}
if (preg_match('#Release\s(\d+(?:\.\d+)+)#', $version_string, $match)) {
if (preg_match('#Release\s(\d+(?:\.\d+)+)#', $versionString, $match)) {
return $this->dataCache['version'] = $match[1];
}

Expand All @@ -188,7 +191,7 @@ public function getVersion(): string
*
* @return bool|resource
*/
public function execute(string $sql)
protected function execute(string $sql)
{
try {
if ($this->resetStmtId === true) {
Expand Down Expand Up @@ -261,9 +264,9 @@ protected function _listColumns(string $table = ''): string
*
* @throws DatabaseException
*
* @return \stdClass[]
* @return stdClass[]
*/
public function _fieldData(string $table): array
protected function _fieldData(string $table): array
{
if (strpos($table, '.') !== false) {
sscanf($table, '%[^.].%s', $owner, $table);
Expand Down Expand Up @@ -311,9 +314,9 @@ public function _fieldData(string $table): array
*
* @throws DatabaseException
*
* @return \stdClass[]
* @return stdClass[]
*/
public function _indexData(string $table): array
protected function _indexData(string $table): array
{
if (strpos($table, '.') !== false) {
sscanf($table, '%[^.].%s', $owner, $table);
Expand Down Expand Up @@ -360,9 +363,9 @@ public function _indexData(string $table): array
*
* @throws DatabaseException
*
* @return \stdClass[]
* @return stdClass[]
*/
public function _foreignKeyData(string $table): array
protected function _foreignKeyData(string $table): array
{
$sql = 'SELECT
acc.constraint_name,
Expand Down Expand Up @@ -488,21 +491,21 @@ public function storedProcedure(string $package, string $procedure, array $param
// Build the query string
$sql = 'BEGIN ' . $package . '.' . $procedure . '(';

$have_cursor = false;
$haveCursor = false;

foreach ($params as $param) {
$sql .= $param['name'] . ',';

if (isset($param['type']) && $param['type'] === OCI_B_CURSOR) {
$have_cursor = true;
$haveCursor = true;
}
}
$sql = trim($sql, ',') . '); END;';

$this->resetStmtId = false;
$this->stmtId = oci_parse($this->connID, $sql);
$this->bindParams($params);
$result = $this->query($sql, false, $have_cursor);
$result = $this->query($sql, false, $haveCursor);
$this->resetStmtId = true;

return $result;
Expand Down Expand Up @@ -572,34 +575,34 @@ public function insertID(): int
}

$indexs = $this->getIndexData($this->latestInsertedTableName);
$field_datas = $this->getFieldData($this->latestInsertedTableName);
$fieldDatas = $this->getFieldData($this->latestInsertedTableName);

if (! $indexs || ! $field_datas) {
if (! $indexs || ! $fieldDatas) {
return 0;
}

$column_type_list = array_column($field_datas, 'type', 'name');
$primary_column_name = '';
$columnTypeList = array_column($fieldDatas, 'type', 'name');
$primaryColumnName = '';

foreach ($indexs as $index) {
if ($index->type !== 'PRIMARY' || count($index->fields) !== 1) {
continue;
}

$primary_column_name = $this->protectIdentifiers($index->fields[0], false, false);
$primary_column_type = $column_type_list[$primary_column_name];
$primaryColumnName = $this->protectIdentifiers($index->fields[0], false, false);
$primaryColumnType = $columnTypeList[$primaryColumnName];

if ($primary_column_type !== 'NUMBER') {
if ($primaryColumnType !== 'NUMBER') {
continue;
}
}

if (! $primary_column_name) {
if (! $primaryColumnName) {
return 0;
}

$table = $this->protectIdentifiers($this->latestInsertedTableName, true);
$query = $this->query('SELECT ' . $this->protectIdentifiers($primary_column_name, false) . ' SEQ FROM ' . $table . ' WHERE ROWID = ?', $this->rowId)->getRow();
$query = $this->query('SELECT ' . $this->protectIdentifiers($primaryColumnName, false) . ' SEQ FROM ' . $table . ' WHERE ROWID = ?', $this->rowId)->getRow();

return (int) ($query->SEQ ?? 0);
}
Expand Down
Loading

0 comments on commit a410c77

Please sign in to comment.