diff --git a/system/Database/OCI8/Builder.php b/system/Database/OCI8/Builder.php index 8b5318a588bf..d5366c6a170a 100644 --- a/system/Database/OCI8/Builder.php +++ b/system/Database/OCI8/Builder.php @@ -11,6 +11,7 @@ namespace CodeIgniter\Database\OCI8; +use CodeIgniter\Database\Exceptions\DatabaseException; use CodeIgniter\Database\BaseBuilder; /** @@ -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'; @@ -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; } } @@ -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; } @@ -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); } /** diff --git a/system/Database/OCI8/Connection.php b/system/Database/OCI8/Connection.php index 674f614d13d5..c0d1beee4218 100644 --- a/system/Database/OCI8/Connection.php +++ b/system/Database/OCI8/Connection.php @@ -19,6 +19,9 @@ /** * Connection for Postgre + * + * @property string|null $latestInsertedTableName + * @property int|null $rowId */ class Connection extends BaseConnection implements ConnectionInterface { @@ -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]; } @@ -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) { @@ -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); @@ -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); @@ -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, @@ -488,13 +491,13 @@ 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;'; @@ -502,7 +505,7 @@ public function storedProcedure(string $package, string $procedure, array $param $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; @@ -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); } diff --git a/system/Database/OCI8/Forge.php b/system/Database/OCI8/Forge.php index 382162b65fd0..c428b40f9825 100644 --- a/system/Database/OCI8/Forge.php +++ b/system/Database/OCI8/Forge.php @@ -74,40 +74,40 @@ class Forge extends \CodeIgniter\Database\Forge /** * ALTER TABLE - * - * @param string $alter_type ALTER type - * @param string $table Table name - * @param mixed $field Column definition - * + * + * @param string $alterType ALTER type + * @param string $table Table name + * @param mixed $field Column definition + * * @return string|string[] */ - protected function _alterTable(string $alter_type, string $table, $field) + protected function _alterTable(string $alterType, string $table, $field) { $sql = 'ALTER TABLE ' . $this->db->escapeIdentifiers($table); - if ($alter_type === 'DROP') { + if ($alterType === 'DROP') { $fields = array_map(function ($field) { return $this->db->escapeIdentifiers(trim($field)); }, (is_string($field)) ? explode(',', $field) : $field); return $sql . ' DROP (' . implode(',', $fields) . ') CASCADE CONSTRAINT INVALIDATE'; } - if ($alter_type === 'CHANGE') { - $alter_type = 'MODIFY'; + if ($alterType === 'CHANGE') { + $alterType = 'MODIFY'; } - $nullable_map = array_column($this->db->getFieldData($table), 'nullable', 'name'); + $nullableMap = array_column($this->db->getFieldData($table), 'nullable', 'name'); $sqls = []; for ($i = 0, $c = count($field); $i < $c; $i++) { - if ($alter_type === 'MODIFY') { + if ($alterType === 'MODIFY') { // If a null constraint is added to a column with a null constraint, // ORA-01451 will occur, // so add null constraint is used only when it is different from the current null constraint. - $is_want_to_add_null = (strpos($field[$i]['null'], ' NOT') === false); - $current_null_addable = $nullable_map[$field[$i]['name']]; + $isWantToAddNull = (strpos($field[$i]['null'], ' NOT') === false); + $currentNullAddable = $nullableMap[$field[$i]['name']]; - if ($is_want_to_add_null === $current_null_addable) { + if ($isWantToAddNull === $currentNullAddable) { $field[$i]['null'] = ''; } } @@ -123,7 +123,7 @@ protected function _alterTable(string $alter_type, string $table, $field) . ' IS ' . $field[$i]['comment']; } - if ($alter_type === 'MODIFY' && ! empty($field[$i]['new_name'])) { + if ($alterType === 'MODIFY' && ! empty($field[$i]['new_name'])) { $sqls[] = $sql . ' RENAME COLUMN ' . $this->db->escapeIdentifiers($field[$i]['name']) . ' TO ' . $this->db->escapeIdentifiers($field[$i]['new_name']); } @@ -132,7 +132,7 @@ protected function _alterTable(string $alter_type, string $table, $field) } } - $sql .= ' ' . $alter_type . ' '; + $sql .= ' ' . $alterType . ' '; $sql .= (count($field) === 1) ? $field[0] : '(' . implode(',', $field) . ')'; @@ -248,23 +248,23 @@ protected function _attributeType(array &$attributes) return; - default: return; + default: } } /** * Drop Table - * + * * Generates a platform-specific DROP TABLE string - * - * @param string $table Table name - * @param bool $if_exists Whether to add an IF EXISTS condition - * + * + * @param string $table Table name + * @param bool $ifExists Whether to add an IF EXISTS condition + * * @return bool|string */ - protected function _dropTable(string $table, bool $if_exists, bool $cascade) + protected function _dropTable(string $table, bool $ifExists, bool $cascade) { - $sql = parent::_dropTable($table, $if_exists, $cascade); + $sql = parent::_dropTable($table, $ifExists, $cascade); if ($sql !== true && $cascade === true) { $sql .= ' CASCADE CONSTRAINTS PURGE'; @@ -290,20 +290,18 @@ protected function _processForeignKeys(string $table): string 'NO ACTION', ]; - if (count($this->foreignKeys) > 0) { - foreach ($this->foreignKeys as $field => $fkey) { - $nameIndex = $table . '_' . implode('_', $fkey['field']) . '_fk'; - $nameIndexFilled = $this->db->escapeIdentifiers($nameIndex); - $foreignKeyFilled = implode(', ', $this->db->escapeIdentifiers($fkey['field'])); - $referenceTableFilled = $this->db->escapeIdentifiers($this->db->DBPrefix . $fkey['referenceTable']); - $referenceFieldFilled = implode(', ', $this->db->escapeIdentifiers($fkey['referenceField'])); + foreach ($this->foreignKeys as $fkey) { + $nameIndex = $table . '_' . implode('_', $fkey['field']) . '_fk'; + $nameIndexFilled = $this->db->escapeIdentifiers($nameIndex); + $foreignKeyFilled = implode(', ', $this->db->escapeIdentifiers($fkey['field'])); + $referenceTableFilled = $this->db->escapeIdentifiers($this->db->DBPrefix . $fkey['referenceTable']); + $referenceFieldFilled = implode(', ', $this->db->escapeIdentifiers($fkey['referenceField'])); - $formatSql = ",\n\tCONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s(%s)"; - $sql .= sprintf($formatSql, $nameIndexFilled, $foreignKeyFilled, $referenceTableFilled, $referenceFieldFilled); + $formatSql = ",\n\tCONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s(%s)"; + $sql .= sprintf($formatSql, $nameIndexFilled, $foreignKeyFilled, $referenceTableFilled, $referenceFieldFilled); - if ($fkey['onDelete'] !== false && in_array($fkey['onDelete'], $allowActions, true)) { - $sql .= ' ON DELETE ' . $fkey['onDelete']; - } + if ($fkey['onDelete'] !== false && in_array($fkey['onDelete'], $allowActions, true)) { + $sql .= ' ON DELETE ' . $fkey['onDelete']; } } diff --git a/system/Database/OCI8/PreparedQuery.php b/system/Database/OCI8/PreparedQuery.php index 36b4325df485..952593a2f167 100644 --- a/system/Database/OCI8/PreparedQuery.php +++ b/system/Database/OCI8/PreparedQuery.php @@ -11,6 +11,7 @@ namespace CodeIgniter\Database\OCI8; +use BadMethodCallException; use CodeIgniter\Database\BasePreparedQuery; use CodeIgniter\Database\PreparedQueryInterface; @@ -90,18 +91,18 @@ public function _prepare(string $sql, array $options = []) public function _execute(array $data): bool { if (null === $this->statement) { - throw new \BadMethodCallException('You must call prepare before trying to execute a prepared statement.'); + throw new BadMethodCallException('You must call prepare before trying to execute a prepared statement.'); } - $last_key = 0; + $lastKey = 0; foreach (array_keys($data) as $key) { oci_bind_by_name($this->statement, ':' . $key, $data[$key]); - $last_key = $key; + $lastKey = $key; } if ($this->isCollectRowId) { - oci_bind_by_name($this->statement, ':' . (++$last_key), $this->db->rowId, 255); + oci_bind_by_name($this->statement, ':' . (++$lastKey), $this->db->rowId, 255); } return oci_execute($this->statement, $this->db->commitMode); @@ -126,10 +127,8 @@ public function parameterize(string $sql): string // Track our current value $count = 0; - $sql = preg_replace_callback('/\?/', static function ($matches) use (&$count) { + return preg_replace_callback('/\?/', static function ($matches) use (&$count) { return ':' . ($count++); }, $sql); - - return $sql; } } diff --git a/system/Database/OCI8/Result.php b/system/Database/OCI8/Result.php index 0f7f3456d15f..a798b9b12edb 100644 --- a/system/Database/OCI8/Result.php +++ b/system/Database/OCI8/Result.php @@ -11,6 +11,7 @@ namespace CodeIgniter\Database\OCI8; +use stdClass; use CodeIgniter\Database\BaseResult; use CodeIgniter\Database\ResultInterface; use CodeIgniter\Entity; @@ -33,8 +34,8 @@ public function getFieldCount(): int */ public function getFieldNames(): array { - return array_map(function ($field_index) { - return oci_field_name($this->resultID, $field_index); + return array_map(function ($fieldIndex) { + return oci_field_name($this->resultID, $fieldIndex); }, range(1, $this->getFieldCount())); } @@ -43,11 +44,11 @@ public function getFieldNames(): array */ public function getFieldData(): array { - return array_map(function ($field_index) { + return array_map(function ($fieldIndex) { return (object) [ - 'name' => oci_field_name($this->resultID, $field_index), - 'type' => oci_field_type($this->resultID, $field_index), - 'max_length' => oci_field_size($this->resultID, $field_index), + 'name' => oci_field_name($this->resultID, $fieldIndex), + 'type' => oci_field_type($this->resultID, $fieldIndex), + 'max_length' => oci_field_size($this->resultID, $fieldIndex), // 'primary_key' = (int) ($data->flags & 2), // 'default' = $data->def, ]; @@ -99,7 +100,7 @@ protected function fetchAssoc() * * @return bool|Entity|object */ - protected function fetchObject(string $className = \stdClass::class) + protected function fetchObject(string $className = stdClass::class) { $row = oci_fetch_object($this->resultID); diff --git a/tests/system/Database/Live/UpdateTest.php b/tests/system/Database/Live/UpdateTest.php index 13b299ee7ee4..8369dc37b50c 100644 --- a/tests/system/Database/Live/UpdateTest.php +++ b/tests/system/Database/Live/UpdateTest.php @@ -11,6 +11,7 @@ namespace CodeIgniter\Database\Live; +use Config\Database; use CodeIgniter\Database\Exceptions\DatabaseException; use CodeIgniter\Test\CIUnitTestCase; use CodeIgniter\Test\DatabaseTestTrait; @@ -203,7 +204,7 @@ public function testUpdatePeriods() public function testSetWithoutEscape() { if ($this->db->DBDriver === 'OCI8') { - $forge = \Config\Database::forge($this->DBGroup); + $forge = Database::forge($this->DBGroup); $forge->modifyColumn('job', [ 'description' => [ 'name' => 'DESCRIPTION',