add trashes
This commit is contained in:
@@ -0,0 +1,821 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* This file is part of CodeIgniter 4 framework.
|
||||
*
|
||||
* (c) CodeIgniter Foundation <admin@codeigniter.com>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Database\SQLSRV;
|
||||
|
||||
use CodeIgniter\Database\BaseBuilder;
|
||||
use CodeIgniter\Database\Exceptions\DatabaseException;
|
||||
use CodeIgniter\Database\Exceptions\DataException;
|
||||
use CodeIgniter\Database\RawSql;
|
||||
use CodeIgniter\Database\ResultInterface;
|
||||
use Config\Feature;
|
||||
|
||||
/**
|
||||
* Builder for SQLSRV
|
||||
*
|
||||
* @todo auto check for TextCastToInt
|
||||
* @todo auto check for InsertIndexValue
|
||||
* @todo replace: delete index entries before insert
|
||||
*/
|
||||
class Builder extends BaseBuilder
|
||||
{
|
||||
/**
|
||||
* ORDER BY random keyword
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $randomKeyword = [
|
||||
'NEWID()',
|
||||
'RAND(%d)',
|
||||
];
|
||||
|
||||
/**
|
||||
* Quoted identifier flag
|
||||
*
|
||||
* Whether to use SQL-92 standard quoted identifier
|
||||
* (double quotes) or brackets for identifier escaping.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $_quoted_identifier = true;
|
||||
|
||||
/**
|
||||
* Handle increment/decrement on text
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public $castTextToInt = true;
|
||||
|
||||
/**
|
||||
* Handle IDENTITY_INSERT property/
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public $keyPermission = false;
|
||||
|
||||
/**
|
||||
* Groups tables in FROM clauses if needed, so there is no confusion
|
||||
* about operator precedence.
|
||||
*/
|
||||
protected function _fromTables(): string
|
||||
{
|
||||
$from = [];
|
||||
|
||||
foreach ($this->QBFrom as $value) {
|
||||
$from[] = str_starts_with($value, '(SELECT') ? $value : $this->getFullName($value);
|
||||
}
|
||||
|
||||
return implode(', ', $from);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a platform-specific truncate string from the supplied data
|
||||
*
|
||||
* If the database does not support the truncate() command,
|
||||
* then this method maps to 'DELETE FROM table'
|
||||
*/
|
||||
protected function _truncate(string $table): string
|
||||
{
|
||||
return 'TRUNCATE TABLE ' . $this->getFullName($table);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates the JOIN portion of the query
|
||||
*
|
||||
* @param RawSql|string $cond
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function join(string $table, $cond, string $type = '', ?bool $escape = null)
|
||||
{
|
||||
if ($type !== '') {
|
||||
$type = strtoupper(trim($type));
|
||||
|
||||
if (! in_array($type, $this->joinTypes, true)) {
|
||||
$type = '';
|
||||
} else {
|
||||
$type .= ' ';
|
||||
}
|
||||
}
|
||||
|
||||
// Extract any aliases that might exist. We use this information
|
||||
// in the protectIdentifiers to know whether to add a table prefix
|
||||
$this->trackAliases($table);
|
||||
|
||||
if (! is_bool($escape)) {
|
||||
$escape = $this->db->protectIdentifiers;
|
||||
}
|
||||
|
||||
if (! $this->hasOperator($cond)) {
|
||||
$cond = ' USING (' . ($escape ? $this->db->escapeIdentifiers($cond) : $cond) . ')';
|
||||
} elseif ($escape === false) {
|
||||
$cond = ' ON ' . $cond;
|
||||
} else {
|
||||
// Split multiple conditions
|
||||
if (preg_match_all('/\sAND\s|\sOR\s/i', $cond, $joints, PREG_OFFSET_CAPTURE) >= 1) {
|
||||
$conditions = [];
|
||||
$joints = $joints[0];
|
||||
array_unshift($joints, ['', 0]);
|
||||
|
||||
for ($i = count($joints) - 1, $pos = strlen($cond); $i >= 0; $i--) {
|
||||
$joints[$i][1] += strlen($joints[$i][0]); // offset
|
||||
$conditions[$i] = substr($cond, $joints[$i][1], $pos - $joints[$i][1]);
|
||||
$pos = $joints[$i][1] - strlen($joints[$i][0]);
|
||||
$joints[$i] = $joints[$i][0];
|
||||
}
|
||||
|
||||
ksort($conditions);
|
||||
} else {
|
||||
$conditions = [$cond];
|
||||
$joints = [''];
|
||||
}
|
||||
|
||||
$cond = ' ON ';
|
||||
|
||||
foreach ($conditions as $i => $condition) {
|
||||
$operator = $this->getOperator($condition);
|
||||
|
||||
// Workaround for BETWEEN
|
||||
if ($operator === false) {
|
||||
$cond .= $joints[$i] . $condition;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$cond .= $joints[$i];
|
||||
$cond .= preg_match('/(\(*)?([\[\]\w\.\'-]+)' . preg_quote($operator, '/') . '(.*)/i', $condition, $match) ? $match[1] . $this->db->protectIdentifiers($match[2]) . $operator . $this->db->protectIdentifiers($match[3]) : $condition;
|
||||
}
|
||||
}
|
||||
|
||||
// Do we want to escape the table name?
|
||||
if ($escape === true) {
|
||||
$table = $this->db->protectIdentifiers($table, true, null, false);
|
||||
}
|
||||
|
||||
// Assemble the JOIN statement
|
||||
$this->QBJoin[] = $type . 'JOIN ' . $this->getFullName($table) . $cond;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a platform-specific insert string from the supplied data
|
||||
*
|
||||
* @todo implement check for this instead static $insertKeyPermission
|
||||
*/
|
||||
protected function _insert(string $table, array $keys, array $unescapedKeys): string
|
||||
{
|
||||
$fullTableName = $this->getFullName($table);
|
||||
|
||||
// insert statement
|
||||
$statement = 'INSERT INTO ' . $fullTableName . ' (' . implode(',', $keys) . ') VALUES (' . implode(', ', $unescapedKeys) . ')';
|
||||
|
||||
return $this->keyPermission ? $this->addIdentity($fullTableName, $statement) : $statement;
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert batch statement
|
||||
*
|
||||
* Generates a platform-specific insert string from the supplied data.
|
||||
*/
|
||||
protected function _insertBatch(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 === '') {
|
||||
$sql = 'INSERT ' . $this->compileIgnore('insert') . 'INTO ' . $this->getFullName($table)
|
||||
. ' (' . implode(', ', $keys) . ")\n{:_table_:}";
|
||||
|
||||
$this->QBOptions['sql'] = $sql;
|
||||
}
|
||||
|
||||
if (isset($this->QBOptions['setQueryAsData'])) {
|
||||
$data = $this->QBOptions['setQueryAsData'];
|
||||
} else {
|
||||
$data = 'VALUES ' . implode(', ', $this->formatValues($values));
|
||||
}
|
||||
|
||||
return str_replace('{:_table_:}', $data, $sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a platform-specific update string from the supplied data
|
||||
*/
|
||||
protected function _update(string $table, array $values): string
|
||||
{
|
||||
$valstr = [];
|
||||
|
||||
foreach ($values as $key => $val) {
|
||||
$valstr[] = $key . ' = ' . $val;
|
||||
}
|
||||
|
||||
$fullTableName = $this->getFullName($table);
|
||||
|
||||
$statement = sprintf('UPDATE %s%s SET ', empty($this->QBLimit) ? '' : 'TOP(' . $this->QBLimit . ') ', $fullTableName);
|
||||
|
||||
$statement .= implode(', ', $valstr)
|
||||
. $this->compileWhereHaving('QBWhere')
|
||||
. $this->compileOrderBy();
|
||||
|
||||
return $this->keyPermission ? $this->addIdentity($fullTableName, $statement) : $statement;
|
||||
}
|
||||
|
||||
/**
|
||||
* Increments a numeric column by the specified value.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function increment(string $column, int $value = 1)
|
||||
{
|
||||
$column = $this->db->protectIdentifiers($column);
|
||||
|
||||
if ($this->castTextToInt) {
|
||||
$values = [$column => "CONVERT(VARCHAR(MAX),CONVERT(INT,CONVERT(VARCHAR(MAX), {$column})) + {$value})"];
|
||||
} else {
|
||||
$values = [$column => "{$column} + {$value}"];
|
||||
}
|
||||
|
||||
$sql = $this->_update($this->QBFrom[0], $values);
|
||||
|
||||
if (! $this->testMode) {
|
||||
$this->resetWrite();
|
||||
|
||||
return $this->db->query($sql, $this->binds, false);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrements a numeric column by the specified value.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function decrement(string $column, int $value = 1)
|
||||
{
|
||||
$column = $this->db->protectIdentifiers($column);
|
||||
|
||||
if ($this->castTextToInt) {
|
||||
$values = [$column => "CONVERT(VARCHAR(MAX),CONVERT(INT,CONVERT(VARCHAR(MAX), {$column})) - {$value})"];
|
||||
} else {
|
||||
$values = [$column => "{$column} + {$value}"];
|
||||
}
|
||||
|
||||
$sql = $this->_update($this->QBFrom[0], $values);
|
||||
|
||||
if (! $this->testMode) {
|
||||
$this->resetWrite();
|
||||
|
||||
return $this->db->query($sql, $this->binds, false);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get full name of the table
|
||||
*/
|
||||
private function getFullName(string $table): string
|
||||
{
|
||||
$alias = '';
|
||||
|
||||
if (str_contains($table, ' ')) {
|
||||
$alias = explode(' ', $table);
|
||||
$table = array_shift($alias);
|
||||
$alias = ' ' . implode(' ', $alias);
|
||||
}
|
||||
|
||||
if ($this->db->escapeChar === '"') {
|
||||
if (str_contains($table, '.') && ! str_starts_with($table, '.') && ! str_ends_with($table, '.')) {
|
||||
$dbInfo = explode('.', $table);
|
||||
$database = $this->db->getDatabase();
|
||||
$table = $dbInfo[0];
|
||||
|
||||
if (count($dbInfo) === 3) {
|
||||
$database = str_replace('"', '', $dbInfo[0]);
|
||||
$schema = str_replace('"', '', $dbInfo[1]);
|
||||
$tableName = str_replace('"', '', $dbInfo[2]);
|
||||
} else {
|
||||
$schema = str_replace('"', '', $dbInfo[0]);
|
||||
$tableName = str_replace('"', '', $dbInfo[1]);
|
||||
}
|
||||
|
||||
return '"' . $database . '"."' . $schema . '"."' . str_replace('"', '', $tableName) . '"' . $alias;
|
||||
}
|
||||
|
||||
return '"' . $this->db->getDatabase() . '"."' . $this->db->schema . '"."' . str_replace('"', '', $table) . '"' . $alias;
|
||||
}
|
||||
|
||||
return '[' . $this->db->getDatabase() . '].[' . $this->db->schema . '].[' . str_replace('"', '', $table) . ']' . str_replace('"', '', $alias);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add permision statements for index value inserts
|
||||
*/
|
||||
private function addIdentity(string $fullTable, string $insert): string
|
||||
{
|
||||
return 'SET IDENTITY_INSERT ' . $fullTable . " ON\n" . $insert . "\nSET IDENTITY_INSERT " . $fullTable . ' OFF';
|
||||
}
|
||||
|
||||
/**
|
||||
* Local implementation of limit
|
||||
*/
|
||||
protected function _limit(string $sql, bool $offsetIgnore = false): string
|
||||
{
|
||||
// SQL Server cannot handle `LIMIT 0`.
|
||||
// DatabaseException:
|
||||
// [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]The number of
|
||||
// rows provided for a FETCH clause must be greater then zero.
|
||||
$limitZeroAsAll = config(Feature::class)->limitZeroAsAll ?? true;
|
||||
if (! $limitZeroAsAll && $this->QBLimit === 0) {
|
||||
return "SELECT * \nFROM " . $this->_fromTables() . ' WHERE 1=0 ';
|
||||
}
|
||||
|
||||
if (empty($this->QBOrderBy)) {
|
||||
$sql .= ' ORDER BY (SELECT NULL) ';
|
||||
}
|
||||
|
||||
if ($offsetIgnore) {
|
||||
$sql .= ' OFFSET 0 ';
|
||||
} else {
|
||||
$sql .= is_int($this->QBOffset) ? ' OFFSET ' . $this->QBOffset : ' OFFSET 0 ';
|
||||
}
|
||||
|
||||
return $sql . ' ROWS FETCH NEXT ' . $this->QBLimit . ' ROWS ONLY ';
|
||||
}
|
||||
|
||||
/**
|
||||
* Compiles a replace into string and runs the query
|
||||
*
|
||||
* @return mixed
|
||||
*
|
||||
* @throws DatabaseException
|
||||
*/
|
||||
public function replace(?array $set = null)
|
||||
{
|
||||
if ($set !== null) {
|
||||
$this->set($set);
|
||||
}
|
||||
|
||||
if ($this->QBSet === []) {
|
||||
if ($this->db->DBDebug) {
|
||||
throw new DatabaseException('You must use the "set" method to update an entry.');
|
||||
}
|
||||
|
||||
return false; // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
$table = $this->QBFrom[0];
|
||||
|
||||
$sql = $this->_replace($table, array_keys($this->QBSet), array_values($this->QBSet));
|
||||
|
||||
$this->resetWrite();
|
||||
|
||||
if ($this->testMode) {
|
||||
return $sql;
|
||||
}
|
||||
|
||||
$this->db->simpleQuery('SET IDENTITY_INSERT ' . $this->getFullName($table) . ' ON');
|
||||
|
||||
$result = $this->db->query($sql, $this->binds, false);
|
||||
$this->db->simpleQuery('SET IDENTITY_INSERT ' . $this->getFullName($table) . ' OFF');
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a platform-specific replace string from the supplied data
|
||||
* on match delete and insert
|
||||
*/
|
||||
protected function _replace(string $table, array $keys, array $values): string
|
||||
{
|
||||
// check whether the existing keys are part of the primary key.
|
||||
// if so then use them for the "ON" part and exclude them from the $values and $keys
|
||||
$pKeys = $this->db->getIndexData($table);
|
||||
$keyFields = [];
|
||||
|
||||
foreach ($pKeys as $key) {
|
||||
if ($key->type === 'PRIMARY') {
|
||||
$keyFields = array_merge($keyFields, $key->fields);
|
||||
}
|
||||
|
||||
if ($key->type === 'UNIQUE') {
|
||||
$keyFields = array_merge($keyFields, $key->fields);
|
||||
}
|
||||
}
|
||||
|
||||
// Get the unique field names
|
||||
$escKeyFields = array_map(fn (string $field): string => $this->db->protectIdentifiers($field), array_values(array_unique($keyFields)));
|
||||
|
||||
// Get the binds
|
||||
$binds = $this->binds;
|
||||
array_walk($binds, static function (&$item): void {
|
||||
$item = $item[0];
|
||||
});
|
||||
|
||||
// Get the common field and values from the keys data and index fields
|
||||
$common = array_intersect($keys, $escKeyFields);
|
||||
$bingo = [];
|
||||
|
||||
foreach ($common as $v) {
|
||||
$k = array_search($v, $keys, true);
|
||||
|
||||
$bingo[$keys[$k]] = $binds[trim($values[$k], ':')];
|
||||
}
|
||||
|
||||
// Querying existing data
|
||||
$builder = $this->db->table($table);
|
||||
|
||||
foreach ($bingo as $k => $v) {
|
||||
$builder->where($k, $v);
|
||||
}
|
||||
|
||||
$q = $builder->get()->getResult();
|
||||
|
||||
// Delete entries if we find them
|
||||
if ($q !== []) {
|
||||
$delete = $this->db->table($table);
|
||||
|
||||
foreach ($bingo as $k => $v) {
|
||||
$delete->where($k, $v);
|
||||
}
|
||||
|
||||
$delete->delete();
|
||||
}
|
||||
|
||||
return sprintf('INSERT INTO %s (%s) VALUES (%s);', $this->getFullName($table), implode(',', $keys), implode(',', $values));
|
||||
}
|
||||
|
||||
/**
|
||||
* SELECT [MAX|MIN|AVG|SUM|COUNT]()
|
||||
*
|
||||
* Handle float return value
|
||||
*
|
||||
* @return BaseBuilder
|
||||
*/
|
||||
protected function maxMinAvgSum(string $select = '', string $alias = '', string $type = 'MAX')
|
||||
{
|
||||
// int functions can be handled by parent
|
||||
if ($type !== 'AVG') {
|
||||
return parent::maxMinAvgSum($select, $alias, $type);
|
||||
}
|
||||
|
||||
if ($select === '') {
|
||||
throw DataException::forEmptyInputGiven('Select');
|
||||
}
|
||||
|
||||
if (str_contains($select, ',')) {
|
||||
throw DataException::forInvalidArgument('Column name not separated by comma');
|
||||
}
|
||||
|
||||
if ($alias === '') {
|
||||
$alias = $this->createAliasFromTable(trim($select));
|
||||
}
|
||||
|
||||
$sql = $type . '( CAST( ' . $this->db->protectIdentifiers(trim($select)) . ' AS FLOAT ) ) AS ' . $this->db->escapeIdentifiers(trim($alias));
|
||||
|
||||
$this->QBSelect[] = $sql;
|
||||
$this->QBNoEscape[] = null;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* "Count All" query
|
||||
*
|
||||
* Generates a platform-specific query string that counts all records in
|
||||
* the particular table
|
||||
*
|
||||
* @param bool $reset Are we want to clear query builder values?
|
||||
*
|
||||
* @return int|string when $test = true
|
||||
*/
|
||||
public function countAll(bool $reset = true)
|
||||
{
|
||||
$table = $this->QBFrom[0];
|
||||
|
||||
$sql = $this->countString . $this->db->escapeIdentifiers('numrows') . ' FROM ' . $this->getFullName($table);
|
||||
|
||||
if ($this->testMode) {
|
||||
return $sql;
|
||||
}
|
||||
|
||||
$query = $this->db->query($sql, null, false);
|
||||
if (empty($query->getResult())) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$query = $query->getRow();
|
||||
|
||||
if ($reset) {
|
||||
$this->resetSelect();
|
||||
}
|
||||
|
||||
return (int) $query->numrows;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete statement
|
||||
*/
|
||||
protected function _delete(string $table): string
|
||||
{
|
||||
return 'DELETE' . (empty($this->QBLimit) ? '' : ' TOP (' . $this->QBLimit . ') ') . ' FROM ' . $this->getFullName($table) . $this->compileWhereHaving('QBWhere');
|
||||
}
|
||||
|
||||
/**
|
||||
* Compiles a delete string and runs the query
|
||||
*
|
||||
* @param mixed $where
|
||||
*
|
||||
* @return mixed
|
||||
*
|
||||
* @throws DatabaseException
|
||||
*/
|
||||
public function delete($where = '', ?int $limit = null, bool $resetData = true)
|
||||
{
|
||||
$table = $this->db->protectIdentifiers($this->QBFrom[0], true, null, false);
|
||||
|
||||
if ($where !== '') {
|
||||
$this->where($where);
|
||||
}
|
||||
|
||||
if ($this->QBWhere === []) {
|
||||
if ($this->db->DBDebug) {
|
||||
throw new DatabaseException('Deletes are not allowed unless they contain a "where" or "like" clause.');
|
||||
}
|
||||
|
||||
return false; // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
if ($limit !== null && $limit !== 0) {
|
||||
$this->QBLimit = $limit;
|
||||
}
|
||||
|
||||
$sql = $this->_delete($table);
|
||||
|
||||
if ($resetData) {
|
||||
$this->resetWrite();
|
||||
}
|
||||
|
||||
return $this->testMode ? $sql : $this->db->query($sql, $this->binds, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compile the SELECT statement
|
||||
*
|
||||
* Generates a query string based on which functions were used.
|
||||
*
|
||||
* @param bool $selectOverride
|
||||
*/
|
||||
protected function compileSelect($selectOverride = false): string
|
||||
{
|
||||
// Write the "select" portion of the query
|
||||
if ($selectOverride !== false) {
|
||||
$sql = $selectOverride;
|
||||
} else {
|
||||
$sql = (! $this->QBDistinct) ? 'SELECT ' : 'SELECT DISTINCT ';
|
||||
|
||||
// SQL Server can't work with select * if group by is specified
|
||||
if (empty($this->QBSelect) && $this->QBGroupBy !== [] && is_array($this->QBGroupBy)) {
|
||||
foreach ($this->QBGroupBy as $field) {
|
||||
$this->QBSelect[] = is_array($field) ? $field['field'] : $field;
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($this->QBSelect)) {
|
||||
$sql .= '*';
|
||||
} else {
|
||||
// Cycle through the "select" portion of the query and prep each column name.
|
||||
// The reason we protect identifiers here rather than in the select() function
|
||||
// is because until the user calls the from() function we don't know if there are aliases
|
||||
foreach ($this->QBSelect as $key => $val) {
|
||||
$noEscape = $this->QBNoEscape[$key] ?? null;
|
||||
$this->QBSelect[$key] = $this->db->protectIdentifiers($val, false, $noEscape);
|
||||
}
|
||||
|
||||
$sql .= implode(', ', $this->QBSelect);
|
||||
}
|
||||
}
|
||||
|
||||
// Write the "FROM" portion of the query
|
||||
if ($this->QBFrom !== []) {
|
||||
$sql .= "\nFROM " . $this->_fromTables();
|
||||
}
|
||||
|
||||
// Write the "JOIN" portion of the query
|
||||
if (! empty($this->QBJoin)) {
|
||||
$sql .= "\n" . implode("\n", $this->QBJoin);
|
||||
}
|
||||
|
||||
$sql .= $this->compileWhereHaving('QBWhere')
|
||||
. $this->compileGroupBy()
|
||||
. $this->compileWhereHaving('QBHaving')
|
||||
. $this->compileOrderBy(); // ORDER BY
|
||||
|
||||
// LIMIT
|
||||
$limitZeroAsAll = config(Feature::class)->limitZeroAsAll ?? true;
|
||||
if ($limitZeroAsAll) {
|
||||
if ($this->QBLimit) {
|
||||
$sql = $this->_limit($sql . "\n");
|
||||
}
|
||||
} elseif ($this->QBLimit !== false || $this->QBOffset) {
|
||||
$sql = $this->_limit($sql . "\n");
|
||||
}
|
||||
|
||||
return $this->unionInjection($sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compiles the select statement based on the other functions called
|
||||
* and runs the query
|
||||
*
|
||||
* @return ResultInterface
|
||||
*/
|
||||
public function get(?int $limit = null, int $offset = 0, bool $reset = true)
|
||||
{
|
||||
$limitZeroAsAll = config(Feature::class)->limitZeroAsAll ?? true;
|
||||
if ($limitZeroAsAll && $limit === 0) {
|
||||
$limit = null;
|
||||
}
|
||||
|
||||
if ($limit !== null) {
|
||||
$this->limit($limit, $offset);
|
||||
}
|
||||
|
||||
$result = $this->testMode ? $this->getCompiledSelect($reset) : $this->db->query($this->compileSelect(), $this->binds, false);
|
||||
|
||||
if ($reset) {
|
||||
$this->resetSelect();
|
||||
|
||||
// Clear our binds so we don't eat up memory
|
||||
$this->binds = [];
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 === '') {
|
||||
$fullTableName = $this->getFullName($table);
|
||||
|
||||
$constraints = $this->QBOptions['constraints'] ?? [];
|
||||
|
||||
$tableIdentity = $this->QBOptions['tableIdentity'] ?? '';
|
||||
$sql = "SELECT name from syscolumns where id = Object_ID('" . $table . "') and colstat = 1";
|
||||
if (($query = $this->db->query($sql)) === false) {
|
||||
throw new DatabaseException('Failed to get table identity');
|
||||
}
|
||||
$query = $query->getResultObject();
|
||||
|
||||
foreach ($query as $row) {
|
||||
$tableIdentity = '"' . $row->name . '"';
|
||||
}
|
||||
$this->QBOptions['tableIdentity'] = $tableIdentity;
|
||||
|
||||
$identityInFields = in_array($tableIdentity, $keys, true);
|
||||
|
||||
$fieldNames = array_map(static fn ($columnName): string => trim($columnName, '"'), $keys);
|
||||
|
||||
if (empty($constraints)) {
|
||||
$tableIndexes = $this->db->getIndexData($table);
|
||||
|
||||
$uniqueIndexes = array_filter($tableIndexes, static function ($index) use ($fieldNames): bool {
|
||||
$hasAllFields = count(array_intersect($index->fields, $fieldNames)) === count($index->fields);
|
||||
|
||||
return $index->type === 'PRIMARY' && $hasAllFields;
|
||||
});
|
||||
|
||||
// if no primary found then look for unique - since indexes have no order
|
||||
if ($uniqueIndexes === []) {
|
||||
$uniqueIndexes = array_filter($tableIndexes, static function ($index) use ($fieldNames): bool {
|
||||
$hasAllFields = count(array_intersect($index->fields, $fieldNames)) === count($index->fields);
|
||||
|
||||
return $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
|
||||
}
|
||||
|
||||
$alias = $this->QBOptions['alias'] ?? '"_upsert"';
|
||||
|
||||
$updateFields = $this->QBOptions['updateFields'] ?? $this->updateFields($keys, false, $constraints)->QBOptions['updateFields'] ?? [];
|
||||
|
||||
$sql = 'MERGE INTO ' . $fullTableName . "\nUSING (\n";
|
||||
|
||||
$sql .= '{:_table_:}';
|
||||
|
||||
$sql .= ") {$alias} (";
|
||||
|
||||
$sql .= implode(', ', $keys);
|
||||
|
||||
$sql .= ')';
|
||||
|
||||
$sql .= "\nON (";
|
||||
|
||||
$sql .= implode(
|
||||
' AND ',
|
||||
array_map(
|
||||
static fn ($key, $value) => (
|
||||
($value instanceof RawSql && is_string($key))
|
||||
?
|
||||
$fullTableName . '.' . $key . ' = ' . $value
|
||||
:
|
||||
(
|
||||
$value instanceof RawSql
|
||||
?
|
||||
$value
|
||||
:
|
||||
$fullTableName . '.' . $value . ' = ' . $alias . '.' . $value
|
||||
)
|
||||
),
|
||||
array_keys($constraints),
|
||||
$constraints,
|
||||
),
|
||||
) . ")\n";
|
||||
|
||||
$sql .= "WHEN MATCHED THEN UPDATE SET\n";
|
||||
|
||||
$sql .= implode(
|
||||
",\n",
|
||||
array_map(
|
||||
static fn ($key, $value): string => $key . ($value instanceof RawSql ?
|
||||
' = ' . $value :
|
||||
" = {$alias}.{$value}"),
|
||||
array_keys($updateFields),
|
||||
$updateFields,
|
||||
),
|
||||
);
|
||||
|
||||
$sql .= "\nWHEN NOT MATCHED THEN INSERT (" . implode(', ', $keys) . ")\nVALUES ";
|
||||
|
||||
$sql .= (
|
||||
'(' . implode(
|
||||
', ',
|
||||
array_map(
|
||||
static fn ($columnName): string => $columnName === $tableIdentity
|
||||
? "CASE WHEN {$alias}.{$columnName} IS NULL THEN (SELECT "
|
||||
. 'isnull(IDENT_CURRENT(\'' . $fullTableName . '\')+IDENT_INCR(\''
|
||||
. $fullTableName . "'),1)) ELSE {$alias}.{$columnName} END"
|
||||
: "{$alias}.{$columnName}",
|
||||
$keys,
|
||||
),
|
||||
) . ');'
|
||||
);
|
||||
|
||||
$sql = $identityInFields ? $this->addIdentity($fullTableName, $sql) : $sql;
|
||||
|
||||
$this->QBOptions['sql'] = $sql;
|
||||
}
|
||||
|
||||
if (isset($this->QBOptions['setQueryAsData'])) {
|
||||
$data = $this->QBOptions['setQueryAsData'];
|
||||
} else {
|
||||
$data = 'VALUES ' . implode(', ', $this->formatValues($values)) . "\n";
|
||||
}
|
||||
|
||||
return str_replace('{:_table_:}', $data, $sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets column names from a select query
|
||||
*/
|
||||
protected function fieldsFromQuery(string $sql): array
|
||||
{
|
||||
return $this->db->query('SELECT TOP 1 * FROM (' . $sql . ') _u_')->getFieldNames();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,590 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* This file is part of CodeIgniter 4 framework.
|
||||
*
|
||||
* (c) CodeIgniter Foundation <admin@codeigniter.com>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Database\SQLSRV;
|
||||
|
||||
use CodeIgniter\Database\BaseConnection;
|
||||
use CodeIgniter\Database\Exceptions\DatabaseException;
|
||||
use CodeIgniter\Database\TableName;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* Connection for SQLSRV
|
||||
*
|
||||
* @extends BaseConnection<resource, resource>
|
||||
*/
|
||||
class Connection extends BaseConnection
|
||||
{
|
||||
/**
|
||||
* Database driver
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $DBDriver = 'SQLSRV';
|
||||
|
||||
/**
|
||||
* Database name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $database;
|
||||
|
||||
/**
|
||||
* Scrollable flag
|
||||
*
|
||||
* Determines what cursor type to use when executing queries.
|
||||
*
|
||||
* FALSE or SQLSRV_CURSOR_FORWARD would increase performance,
|
||||
* but would disable num_rows() (and possibly insert_id())
|
||||
*
|
||||
* @var false|string
|
||||
*/
|
||||
public $scrollable;
|
||||
|
||||
/**
|
||||
* Identifier escape character
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $escapeChar = '"';
|
||||
|
||||
/**
|
||||
* Database schema
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $schema = 'dbo';
|
||||
|
||||
/**
|
||||
* Quoted identifier flag
|
||||
*
|
||||
* Whether to use SQL-92 standard quoted identifier
|
||||
* (double quotes) or brackets for identifier escaping.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $_quoted_identifier = true;
|
||||
|
||||
/**
|
||||
* List of reserved identifiers
|
||||
*
|
||||
* Identifiers that must NOT be escaped.
|
||||
*
|
||||
* @var list<string>
|
||||
*/
|
||||
protected $_reserved_identifiers = ['*'];
|
||||
|
||||
/**
|
||||
* Class constructor
|
||||
*/
|
||||
public function __construct(array $params)
|
||||
{
|
||||
parent::__construct($params);
|
||||
|
||||
// This is only supported as of SQLSRV 3.0
|
||||
if ($this->scrollable === null) {
|
||||
$this->scrollable = defined('SQLSRV_CURSOR_CLIENT_BUFFERED') ? SQLSRV_CURSOR_CLIENT_BUFFERED : false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Connect to the database.
|
||||
*
|
||||
* @return false|resource
|
||||
*
|
||||
* @throws DatabaseException
|
||||
*/
|
||||
public function connect(bool $persistent = false)
|
||||
{
|
||||
$charset = in_array(strtolower($this->charset), ['utf-8', 'utf8'], true) ? 'UTF-8' : SQLSRV_ENC_CHAR;
|
||||
|
||||
$connection = [
|
||||
'UID' => empty($this->username) ? '' : $this->username,
|
||||
'PWD' => empty($this->password) ? '' : $this->password,
|
||||
'Database' => $this->database,
|
||||
'ConnectionPooling' => $persistent ? 1 : 0,
|
||||
'CharacterSet' => $charset,
|
||||
'Encrypt' => $this->encrypt === true ? 1 : 0,
|
||||
'ReturnDatesAsStrings' => 1,
|
||||
];
|
||||
|
||||
// If the username and password are both empty, assume this is a
|
||||
// 'Windows Authentication Mode' connection.
|
||||
if (empty($connection['UID']) && empty($connection['PWD'])) {
|
||||
unset($connection['UID'], $connection['PWD']);
|
||||
}
|
||||
|
||||
if (! str_contains($this->hostname, ',') && $this->port !== '') {
|
||||
$this->hostname .= ', ' . $this->port;
|
||||
}
|
||||
|
||||
sqlsrv_configure('WarningsReturnAsErrors', 0);
|
||||
$this->connID = sqlsrv_connect($this->hostname, $connection);
|
||||
|
||||
if ($this->connID !== false) {
|
||||
// Determine how identifiers are escaped
|
||||
$query = $this->query('SELECT CASE WHEN (@@OPTIONS | 256) = @@OPTIONS THEN 1 ELSE 0 END AS qi');
|
||||
$query = $query->getResultObject();
|
||||
|
||||
$this->_quoted_identifier = empty($query) ? false : (bool) $query[0]->qi;
|
||||
$this->escapeChar = ($this->_quoted_identifier) ? '"' : ['[', ']'];
|
||||
|
||||
return $this->connID;
|
||||
}
|
||||
|
||||
throw new DatabaseException($this->getAllErrorMessages());
|
||||
}
|
||||
|
||||
/**
|
||||
* For exception message
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public function getAllErrorMessages(): string
|
||||
{
|
||||
$errors = [];
|
||||
|
||||
foreach (sqlsrv_errors() as $error) {
|
||||
$errors[] = $error['message']
|
||||
. ' SQLSTATE: ' . $error['SQLSTATE'] . ', code: ' . $error['code'];
|
||||
}
|
||||
|
||||
return implode("\n", $errors);
|
||||
}
|
||||
|
||||
/**
|
||||
* Keep or establish the connection if no queries have been sent for
|
||||
* a length of time exceeding the server's idle timeout.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function reconnect()
|
||||
{
|
||||
$this->close();
|
||||
$this->initialize();
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the database connection.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function _close()
|
||||
{
|
||||
sqlsrv_close($this->connID);
|
||||
}
|
||||
|
||||
/**
|
||||
* Platform-dependant string escape
|
||||
*/
|
||||
protected function _escapeString(string $str): string
|
||||
{
|
||||
return str_replace("'", "''", remove_invisible_characters($str, false));
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert ID
|
||||
*/
|
||||
public function insertID(): int
|
||||
{
|
||||
return (int) ($this->query('SELECT SCOPE_IDENTITY() AS insert_id')->getRow()->insert_id ?? 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates the SQL for listing tables in a platform-dependent manner.
|
||||
*
|
||||
* @param string|null $tableName If $tableName is provided will return only this table if exists.
|
||||
*/
|
||||
protected function _listTables(bool $prefixLimit = false, ?string $tableName = null): string
|
||||
{
|
||||
$sql = 'SELECT [TABLE_NAME] AS "name"'
|
||||
. ' FROM [INFORMATION_SCHEMA].[TABLES] '
|
||||
. ' WHERE '
|
||||
. " [TABLE_SCHEMA] = '" . $this->schema . "' ";
|
||||
|
||||
if ($tableName !== null) {
|
||||
return $sql .= ' AND [TABLE_NAME] LIKE ' . $this->escape($tableName);
|
||||
}
|
||||
|
||||
if ($prefixLimit && $this->DBPrefix !== '') {
|
||||
$sql .= " AND [TABLE_NAME] LIKE '" . $this->escapeLikeString($this->DBPrefix) . "%' "
|
||||
. sprintf($this->likeEscapeStr, $this->likeEscapeChar);
|
||||
}
|
||||
|
||||
return $sql;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a platform-specific query string so that the column names can be fetched.
|
||||
*
|
||||
* @param string|TableName $table
|
||||
*/
|
||||
protected function _listColumns($table = ''): string
|
||||
{
|
||||
if ($table instanceof TableName) {
|
||||
$tableName = $this->escape(strtolower($table->getActualTableName()));
|
||||
} else {
|
||||
$tableName = $this->escape($this->DBPrefix . strtolower($table));
|
||||
}
|
||||
|
||||
return 'SELECT [COLUMN_NAME] '
|
||||
. ' FROM [INFORMATION_SCHEMA].[COLUMNS]'
|
||||
. ' WHERE [TABLE_NAME] = ' . $tableName
|
||||
. ' AND [TABLE_SCHEMA] = ' . $this->escape($this->schema);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of objects with index data
|
||||
*
|
||||
* @return array<string, stdClass>
|
||||
*
|
||||
* @throws DatabaseException
|
||||
*/
|
||||
protected function _indexData(string $table): array
|
||||
{
|
||||
$sql = 'EXEC sp_helpindex ' . $this->escape($this->schema . '.' . $table);
|
||||
|
||||
if (($query = $this->query($sql)) === false) {
|
||||
throw new DatabaseException(lang('Database.failGetIndexData'));
|
||||
}
|
||||
$query = $query->getResultObject();
|
||||
|
||||
$retVal = [];
|
||||
|
||||
foreach ($query as $row) {
|
||||
$obj = new stdClass();
|
||||
$obj->name = $row->index_name;
|
||||
|
||||
$_fields = explode(',', trim($row->index_keys));
|
||||
$obj->fields = array_map(static fn ($v): string => trim($v), $_fields);
|
||||
|
||||
if (str_contains($row->index_description, 'primary key located on')) {
|
||||
$obj->type = 'PRIMARY';
|
||||
} else {
|
||||
$obj->type = (str_contains($row->index_description, 'nonclustered, unique')) ? 'UNIQUE' : 'INDEX';
|
||||
}
|
||||
|
||||
$retVal[$obj->name] = $obj;
|
||||
}
|
||||
|
||||
return $retVal;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of objects with Foreign key data
|
||||
* referenced_object_id parent_object_id
|
||||
*
|
||||
* @return array<string, stdClass>
|
||||
*
|
||||
* @throws DatabaseException
|
||||
*/
|
||||
protected function _foreignKeyData(string $table): array
|
||||
{
|
||||
$sql = 'SELECT
|
||||
f.name as constraint_name,
|
||||
OBJECT_NAME (f.parent_object_id) as table_name,
|
||||
COL_NAME(fc.parent_object_id,fc.parent_column_id) column_name,
|
||||
OBJECT_NAME(f.referenced_object_id) foreign_table_name,
|
||||
COL_NAME(fc.referenced_object_id,fc.referenced_column_id) foreign_column_name,
|
||||
rc.delete_rule,
|
||||
rc.update_rule,
|
||||
rc.match_option
|
||||
FROM
|
||||
sys.foreign_keys AS f
|
||||
INNER JOIN sys.foreign_key_columns AS fc ON f.OBJECT_ID = fc.constraint_object_id
|
||||
INNER JOIN sys.tables t ON t.OBJECT_ID = fc.referenced_object_id
|
||||
INNER JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc ON rc.CONSTRAINT_NAME = f.name
|
||||
WHERE OBJECT_NAME (f.parent_object_id) = ' . $this->escape($table);
|
||||
|
||||
if (($query = $this->query($sql)) === false) {
|
||||
throw new DatabaseException(lang('Database.failGetForeignKeyData'));
|
||||
}
|
||||
|
||||
$query = $query->getResultObject();
|
||||
$indexes = [];
|
||||
|
||||
foreach ($query as $row) {
|
||||
$indexes[$row->constraint_name]['constraint_name'] = $row->constraint_name;
|
||||
$indexes[$row->constraint_name]['table_name'] = $row->table_name;
|
||||
$indexes[$row->constraint_name]['column_name'][] = $row->column_name;
|
||||
$indexes[$row->constraint_name]['foreign_table_name'] = $row->foreign_table_name;
|
||||
$indexes[$row->constraint_name]['foreign_column_name'][] = $row->foreign_column_name;
|
||||
$indexes[$row->constraint_name]['on_delete'] = $row->delete_rule;
|
||||
$indexes[$row->constraint_name]['on_update'] = $row->update_rule;
|
||||
$indexes[$row->constraint_name]['match'] = $row->match_option;
|
||||
}
|
||||
|
||||
return $this->foreignKeyDataToObjects($indexes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Disables foreign key checks temporarily.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function _disableForeignKeyChecks()
|
||||
{
|
||||
return 'EXEC sp_MSforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT ALL"';
|
||||
}
|
||||
|
||||
/**
|
||||
* Enables foreign key checks temporarily.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function _enableForeignKeyChecks()
|
||||
{
|
||||
return 'EXEC sp_MSforeachtable "ALTER TABLE ? WITH CHECK CHECK CONSTRAINT ALL"';
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of objects with field data
|
||||
*
|
||||
* @return list<stdClass>
|
||||
*
|
||||
* @throws DatabaseException
|
||||
*/
|
||||
protected function _fieldData(string $table): array
|
||||
{
|
||||
$sql = 'SELECT
|
||||
COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION,
|
||||
COLUMN_DEFAULT, IS_NULLABLE
|
||||
FROM INFORMATION_SCHEMA.COLUMNS
|
||||
WHERE TABLE_NAME= ' . $this->escape(($table));
|
||||
|
||||
if (($query = $this->query($sql)) === false) {
|
||||
throw new DatabaseException(lang('Database.failGetFieldData'));
|
||||
}
|
||||
|
||||
$query = $query->getResultObject();
|
||||
$retVal = [];
|
||||
|
||||
for ($i = 0, $c = count($query); $i < $c; $i++) {
|
||||
$retVal[$i] = new stdClass();
|
||||
|
||||
$retVal[$i]->name = $query[$i]->COLUMN_NAME;
|
||||
$retVal[$i]->type = $query[$i]->DATA_TYPE;
|
||||
|
||||
$retVal[$i]->max_length = $query[$i]->CHARACTER_MAXIMUM_LENGTH > 0
|
||||
? $query[$i]->CHARACTER_MAXIMUM_LENGTH
|
||||
: (
|
||||
$query[$i]->CHARACTER_MAXIMUM_LENGTH === -1
|
||||
? 'max'
|
||||
: $query[$i]->NUMERIC_PRECISION
|
||||
);
|
||||
|
||||
$retVal[$i]->nullable = $query[$i]->IS_NULLABLE !== 'NO';
|
||||
$retVal[$i]->default = $query[$i]->COLUMN_DEFAULT;
|
||||
}
|
||||
|
||||
return $retVal;
|
||||
}
|
||||
|
||||
/**
|
||||
* Begin Transaction
|
||||
*/
|
||||
protected function _transBegin(): bool
|
||||
{
|
||||
return sqlsrv_begin_transaction($this->connID);
|
||||
}
|
||||
|
||||
/**
|
||||
* Commit Transaction
|
||||
*/
|
||||
protected function _transCommit(): bool
|
||||
{
|
||||
return sqlsrv_commit($this->connID);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rollback Transaction
|
||||
*/
|
||||
protected function _transRollback(): bool
|
||||
{
|
||||
return sqlsrv_rollback($this->connID);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the last error code and message.
|
||||
* Must return this format: ['code' => string|int, 'message' => string]
|
||||
* intval(code) === 0 means "no error".
|
||||
*
|
||||
* @return array<string, int|string>
|
||||
*/
|
||||
public function error(): array
|
||||
{
|
||||
$error = [
|
||||
'code' => '00000',
|
||||
'message' => '',
|
||||
];
|
||||
|
||||
$sqlsrvErrors = sqlsrv_errors(SQLSRV_ERR_ERRORS);
|
||||
|
||||
if (! is_array($sqlsrvErrors)) {
|
||||
return $error;
|
||||
}
|
||||
|
||||
$sqlsrvError = array_shift($sqlsrvErrors);
|
||||
if (isset($sqlsrvError['SQLSTATE'])) {
|
||||
$error['code'] = isset($sqlsrvError['code']) ? $sqlsrvError['SQLSTATE'] . '/' . $sqlsrvError['code'] : $sqlsrvError['SQLSTATE'];
|
||||
} elseif (isset($sqlsrvError['code'])) {
|
||||
$error['code'] = $sqlsrvError['code'];
|
||||
}
|
||||
|
||||
if (isset($sqlsrvError['message'])) {
|
||||
$error['message'] = $sqlsrvError['message'];
|
||||
}
|
||||
|
||||
return $error;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the total number of rows affected by this query.
|
||||
*/
|
||||
public function affectedRows(): int
|
||||
{
|
||||
if ($this->resultID === false) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return sqlsrv_rows_affected($this->resultID);
|
||||
}
|
||||
|
||||
/**
|
||||
* Select a specific database table to use.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function setDatabase(?string $databaseName = null)
|
||||
{
|
||||
if ($databaseName === null || $databaseName === '') {
|
||||
$databaseName = $this->database;
|
||||
}
|
||||
|
||||
if (empty($this->connID)) {
|
||||
$this->initialize();
|
||||
}
|
||||
|
||||
if ($this->execute('USE ' . $this->_escapeString($databaseName))) {
|
||||
$this->database = $databaseName;
|
||||
$this->dataCache = [];
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the query against the database.
|
||||
*
|
||||
* @return false|resource
|
||||
*/
|
||||
protected function execute(string $sql)
|
||||
{
|
||||
$stmt = ($this->scrollable === false || $this->isWriteType($sql)) ?
|
||||
sqlsrv_query($this->connID, $sql) :
|
||||
sqlsrv_query($this->connID, $sql, [], ['Scrollable' => $this->scrollable]);
|
||||
|
||||
if ($stmt === false) {
|
||||
$error = $this->error();
|
||||
|
||||
log_message('error', $error['message']);
|
||||
|
||||
if ($this->DBDebug) {
|
||||
throw new DatabaseException($error['message']);
|
||||
}
|
||||
}
|
||||
|
||||
return $stmt;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the last error encountered by this connection.
|
||||
*
|
||||
* @return array<string, int|string>
|
||||
*
|
||||
* @deprecated Use `error()` instead.
|
||||
*/
|
||||
public function getError()
|
||||
{
|
||||
$error = [
|
||||
'code' => '00000',
|
||||
'message' => '',
|
||||
];
|
||||
|
||||
$sqlsrvErrors = sqlsrv_errors(SQLSRV_ERR_ERRORS);
|
||||
|
||||
if (! is_array($sqlsrvErrors)) {
|
||||
return $error;
|
||||
}
|
||||
|
||||
$sqlsrvError = array_shift($sqlsrvErrors);
|
||||
if (isset($sqlsrvError['SQLSTATE'])) {
|
||||
$error['code'] = isset($sqlsrvError['code']) ? $sqlsrvError['SQLSTATE'] . '/' . $sqlsrvError['code'] : $sqlsrvError['SQLSTATE'];
|
||||
} elseif (isset($sqlsrvError['code'])) {
|
||||
$error['code'] = $sqlsrvError['code'];
|
||||
}
|
||||
|
||||
if (isset($sqlsrvError['message'])) {
|
||||
$error['message'] = $sqlsrvError['message'];
|
||||
}
|
||||
|
||||
return $error;
|
||||
}
|
||||
|
||||
/**
|
||||
* The name of the platform in use (MySQLi, mssql, etc)
|
||||
*/
|
||||
public function getPlatform(): string
|
||||
{
|
||||
return $this->DBDriver;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string containing the version of the database being used.
|
||||
*/
|
||||
public function getVersion(): string
|
||||
{
|
||||
$info = [];
|
||||
if (isset($this->dataCache['version'])) {
|
||||
return $this->dataCache['version'];
|
||||
}
|
||||
|
||||
if (! $this->connID) {
|
||||
$this->initialize();
|
||||
}
|
||||
|
||||
if (($info = sqlsrv_server_info($this->connID)) === []) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return isset($info['SQLServerVersion']) ? $this->dataCache['version'] = $info['SQLServerVersion'] : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if a query is a "write" type.
|
||||
*
|
||||
* Overrides BaseConnection::isWriteType, adding additional read query types.
|
||||
*
|
||||
* @param string $sql
|
||||
*/
|
||||
public function isWriteType($sql): bool
|
||||
{
|
||||
if (preg_match('/^\s*"?(EXEC\s*sp_rename)\s/i', $sql)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return parent::isWriteType($sql);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,453 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* This file is part of CodeIgniter 4 framework.
|
||||
*
|
||||
* (c) CodeIgniter Foundation <admin@codeigniter.com>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Database\SQLSRV;
|
||||
|
||||
use CodeIgniter\Database\BaseConnection;
|
||||
use CodeIgniter\Database\Exceptions\DatabaseException;
|
||||
use CodeIgniter\Database\Forge as BaseForge;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Forge for SQLSRV
|
||||
*/
|
||||
class Forge extends BaseForge
|
||||
{
|
||||
/**
|
||||
* DROP CONSTRAINT statement
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $dropConstraintStr;
|
||||
|
||||
/**
|
||||
* DROP INDEX statement
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $dropIndexStr;
|
||||
|
||||
/**
|
||||
* CREATE DATABASE IF statement
|
||||
*
|
||||
* @todo missing charset, collat & check for existent
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $createDatabaseIfStr = "DECLARE @DBName VARCHAR(255) = '%s'\nDECLARE @SQL VARCHAR(max) = 'IF DB_ID( ''' + @DBName + ''' ) IS NULL CREATE DATABASE %s'\nEXEC( @SQL )";
|
||||
|
||||
/**
|
||||
* CREATE DATABASE IF statement
|
||||
*
|
||||
* @todo missing charset & collat
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $createDatabaseStr = 'CREATE DATABASE %s ';
|
||||
|
||||
/**
|
||||
* CHECK DATABASE EXIST statement
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $checkDatabaseExistStr = 'IF DB_ID( %s ) IS NOT NULL SELECT 1';
|
||||
|
||||
/**
|
||||
* RENAME TABLE statement
|
||||
*
|
||||
* While the below statement would work, it returns an error.
|
||||
* Also MS recommends dropping and dropping and re-creating the table.
|
||||
*
|
||||
* @see https://docs.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-rename-transact-sql?view=sql-server-2017
|
||||
* 'EXEC sp_rename %s , %s ;'
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $renameTableStr;
|
||||
|
||||
/**
|
||||
* UNSIGNED support
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $unsigned = [
|
||||
'TINYINT' => 'SMALLINT',
|
||||
'SMALLINT' => 'INT',
|
||||
'INT' => 'BIGINT',
|
||||
'REAL' => 'FLOAT',
|
||||
];
|
||||
|
||||
/**
|
||||
* Foreign Key Allowed Actions
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fkAllowActions = ['CASCADE', 'SET NULL', 'NO ACTION', 'RESTRICT', 'SET DEFAULT'];
|
||||
|
||||
/**
|
||||
* CREATE TABLE IF statement
|
||||
*
|
||||
* @var string
|
||||
*
|
||||
* @deprecated This is no longer used.
|
||||
*/
|
||||
protected $createTableIfStr;
|
||||
|
||||
/**
|
||||
* CREATE TABLE statement
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $createTableStr;
|
||||
|
||||
public function __construct(BaseConnection $db)
|
||||
{
|
||||
parent::__construct($db);
|
||||
|
||||
$this->createTableStr = '%s ' . $this->db->escapeIdentifiers($this->db->schema) . ".%s (%s\n) ";
|
||||
$this->renameTableStr = 'EXEC sp_rename [' . $this->db->escapeIdentifiers($this->db->schema) . '.%s] , %s ;';
|
||||
|
||||
$this->dropConstraintStr = 'ALTER TABLE ' . $this->db->escapeIdentifiers($this->db->schema) . '.%s DROP CONSTRAINT %s';
|
||||
$this->dropIndexStr = 'DROP INDEX %s ON ' . $this->db->escapeIdentifiers($this->db->schema) . '.%s';
|
||||
}
|
||||
|
||||
/**
|
||||
* Create database
|
||||
*
|
||||
* @param bool $ifNotExists Whether to add IF NOT EXISTS condition
|
||||
*
|
||||
* @throws DatabaseException
|
||||
*/
|
||||
public function createDatabase(string $dbName, bool $ifNotExists = false): bool
|
||||
{
|
||||
if ($ifNotExists) {
|
||||
$sql = sprintf(
|
||||
$this->createDatabaseIfStr,
|
||||
$dbName,
|
||||
$this->db->escapeIdentifier($dbName),
|
||||
);
|
||||
} else {
|
||||
$sql = sprintf(
|
||||
$this->createDatabaseStr,
|
||||
$this->db->escapeIdentifier($dbName),
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
if (! $this->db->query($sql)) {
|
||||
// @codeCoverageIgnoreStart
|
||||
if ($this->db->DBDebug) {
|
||||
throw new DatabaseException('Unable to create the specified database.');
|
||||
}
|
||||
|
||||
return false;
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
|
||||
if (isset($this->db->dataCache['db_names'])) {
|
||||
$this->db->dataCache['db_names'][] = $dbName;
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (Throwable $e) {
|
||||
if ($this->db->DBDebug) {
|
||||
throw new DatabaseException('Unable to create the specified database.', 0, $e);
|
||||
}
|
||||
|
||||
return false; // @codeCoverageIgnore
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* CREATE TABLE attributes
|
||||
*/
|
||||
protected function _createTableAttributes(array $attributes): string
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array|string $processedFields Processed column definitions
|
||||
* or column names to DROP
|
||||
*
|
||||
* @return false|list<string>|string SQL string or false
|
||||
* @phpstan-return ($alterType is 'DROP' ? string : list<string>|false)
|
||||
*/
|
||||
protected function _alterTable(string $alterType, string $table, $processedFields)
|
||||
{
|
||||
// Handle DROP here
|
||||
if ($alterType === 'DROP') {
|
||||
$columnNamesToDrop = $processedFields;
|
||||
|
||||
// check if fields are part of any indexes
|
||||
$indexData = $this->db->getIndexData($table);
|
||||
|
||||
foreach ($indexData as $index) {
|
||||
if (is_string($columnNamesToDrop)) {
|
||||
$columnNamesToDrop = explode(',', $columnNamesToDrop);
|
||||
}
|
||||
|
||||
$fld = array_intersect($columnNamesToDrop, $index->fields);
|
||||
|
||||
// Drop index if field is part of an index
|
||||
if ($fld !== []) {
|
||||
$this->_dropIndex($table, $index);
|
||||
}
|
||||
}
|
||||
|
||||
$fullTable = $this->db->escapeIdentifiers($this->db->schema) . '.' . $this->db->escapeIdentifiers($table);
|
||||
|
||||
// Drop default constraints
|
||||
$fields = implode(',', $this->db->escape((array) $columnNamesToDrop));
|
||||
|
||||
$sql = <<<SQL
|
||||
SELECT name
|
||||
FROM sys.default_constraints
|
||||
WHERE parent_object_id = OBJECT_ID('{$fullTable}')
|
||||
AND parent_column_id IN (
|
||||
SELECT column_id FROM sys.columns WHERE name IN ({$fields}) AND object_id = OBJECT_ID(N'{$fullTable}')
|
||||
)
|
||||
SQL;
|
||||
|
||||
foreach ($this->db->query($sql)->getResultArray() as $index) {
|
||||
$this->db->query('ALTER TABLE ' . $fullTable . ' DROP CONSTRAINT ' . $index['name'] . '');
|
||||
}
|
||||
|
||||
$sql = 'ALTER TABLE ' . $fullTable . ' DROP ';
|
||||
|
||||
$fields = array_map(static fn ($item): string => 'COLUMN [' . trim($item) . ']', (array) $columnNamesToDrop);
|
||||
|
||||
return $sql . implode(',', $fields);
|
||||
}
|
||||
|
||||
$sql = 'ALTER TABLE ' . $this->db->escapeIdentifiers($this->db->schema) . '.' . $this->db->escapeIdentifiers($table);
|
||||
$sql .= ($alterType === 'ADD') ? 'ADD ' : ' ';
|
||||
|
||||
$sqls = [];
|
||||
|
||||
if ($alterType === 'ADD') {
|
||||
foreach ($processedFields as $field) {
|
||||
$sqls[] = $sql . ($field['_literal'] !== false ? $field['_literal'] : $this->_processColumn($field));
|
||||
}
|
||||
|
||||
return $sqls;
|
||||
}
|
||||
|
||||
foreach ($processedFields as $field) {
|
||||
if ($field['_literal'] !== false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (isset($field['type'])) {
|
||||
$sqls[] = $sql . ' ALTER COLUMN ' . $this->db->escapeIdentifiers($field['name'])
|
||||
. " {$field['type']}{$field['length']}";
|
||||
}
|
||||
|
||||
if (! empty($field['default'])) {
|
||||
$sqls[] = $sql . ' ALTER COLUMN ADD CONSTRAINT ' . $this->db->escapeIdentifiers($field['name']) . '_def'
|
||||
. " DEFAULT {$field['default']} FOR " . $this->db->escapeIdentifiers($field['name']);
|
||||
}
|
||||
|
||||
$nullable = true; // Nullable by default.
|
||||
if (isset($field['null']) && ($field['null'] === false || $field['null'] === ' NOT ' . $this->null)) {
|
||||
$nullable = false;
|
||||
}
|
||||
$sqls[] = $sql . ' ALTER COLUMN ' . $this->db->escapeIdentifiers($field['name'])
|
||||
. " {$field['type']}{$field['length']} " . ($nullable ? '' : 'NOT') . ' NULL';
|
||||
|
||||
if (! empty($field['comment'])) {
|
||||
$sqls[] = 'EXEC sys.sp_addextendedproperty '
|
||||
. "@name=N'Caption', @value=N'" . $field['comment'] . "' , "
|
||||
. "@level0type=N'SCHEMA',@level0name=N'" . $this->db->schema . "', "
|
||||
. "@level1type=N'TABLE',@level1name=N'" . $this->db->escapeIdentifiers($table) . "', "
|
||||
. "@level2type=N'COLUMN',@level2name=N'" . $this->db->escapeIdentifiers($field['name']) . "'";
|
||||
}
|
||||
|
||||
if (! empty($field['new_name'])) {
|
||||
$sqls[] = "EXEC sp_rename '[" . $this->db->schema . '].[' . $table . '].[' . $field['name'] . "]' , '" . $field['new_name'] . "', 'COLUMN';";
|
||||
}
|
||||
}
|
||||
|
||||
return $sqls;
|
||||
}
|
||||
|
||||
/**
|
||||
* Drop index for table
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
protected function _dropIndex(string $table, object $indexData)
|
||||
{
|
||||
if ($indexData->type === 'PRIMARY') {
|
||||
$sql = 'ALTER TABLE [' . $this->db->schema . '].[' . $table . '] DROP [' . $indexData->name . ']';
|
||||
} else {
|
||||
$sql = 'DROP INDEX [' . $indexData->name . '] ON [' . $this->db->schema . '].[' . $table . ']';
|
||||
}
|
||||
|
||||
return $this->db->simpleQuery($sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates SQL to add indexes
|
||||
*
|
||||
* @param bool $asQuery When true returns stand alone SQL, else partial SQL used with CREATE TABLE
|
||||
*/
|
||||
protected function _processIndexes(string $table, bool $asQuery = false): array
|
||||
{
|
||||
$sqls = [];
|
||||
|
||||
for ($i = 0, $c = count($this->keys); $i < $c; $i++) {
|
||||
for ($i2 = 0, $c2 = count($this->keys[$i]['fields']); $i2 < $c2; $i2++) {
|
||||
if (! isset($this->fields[$this->keys[$i]['fields'][$i2]])) {
|
||||
unset($this->keys[$i]['fields'][$i2]);
|
||||
}
|
||||
}
|
||||
|
||||
if (count($this->keys[$i]['fields']) <= 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$keyName = $this->db->escapeIdentifiers(($this->keys[$i]['keyName'] === '') ?
|
||||
$table . '_' . implode('_', $this->keys[$i]['fields']) :
|
||||
$this->keys[$i]['keyName']);
|
||||
|
||||
if (in_array($i, $this->uniqueKeys, true)) {
|
||||
$sqls[] = 'ALTER TABLE '
|
||||
. $this->db->escapeIdentifiers($this->db->schema) . '.' . $this->db->escapeIdentifiers($table)
|
||||
. ' ADD CONSTRAINT ' . $keyName
|
||||
. ' UNIQUE (' . implode(', ', $this->db->escapeIdentifiers($this->keys[$i]['fields'])) . ');';
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$sqls[] = 'CREATE INDEX '
|
||||
. $keyName
|
||||
. ' ON ' . $this->db->escapeIdentifiers($this->db->schema) . '.' . $this->db->escapeIdentifiers($table)
|
||||
. ' (' . implode(', ', $this->db->escapeIdentifiers($this->keys[$i]['fields'])) . ');';
|
||||
}
|
||||
|
||||
return $sqls;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process column
|
||||
*/
|
||||
protected function _processColumn(array $processedField): string
|
||||
{
|
||||
return $this->db->escapeIdentifiers($processedField['name'])
|
||||
. (empty($processedField['new_name']) ? '' : ' ' . $this->db->escapeIdentifiers($processedField['new_name']))
|
||||
. ' ' . $processedField['type'] . ($processedField['type'] === 'text' ? '' : $processedField['length'])
|
||||
. $processedField['default']
|
||||
. $processedField['null']
|
||||
. $processedField['auto_increment']
|
||||
. ''
|
||||
. $processedField['unique'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a data type mapping between different databases.
|
||||
*/
|
||||
protected function _attributeType(array &$attributes)
|
||||
{
|
||||
// Reset field lengths for data types that don't support it
|
||||
if (isset($attributes['CONSTRAINT']) && str_contains(strtolower($attributes['TYPE']), 'int')) {
|
||||
$attributes['CONSTRAINT'] = null;
|
||||
}
|
||||
|
||||
switch (strtoupper($attributes['TYPE'])) {
|
||||
case 'MEDIUMINT':
|
||||
$attributes['TYPE'] = 'INTEGER';
|
||||
$attributes['UNSIGNED'] = false;
|
||||
break;
|
||||
|
||||
case 'INTEGER':
|
||||
$attributes['TYPE'] = 'INT';
|
||||
break;
|
||||
|
||||
case 'ENUM':
|
||||
// in char(n) and varchar(n), the n defines the string length in
|
||||
// bytes (0 to 8,000).
|
||||
// https://learn.microsoft.com/en-us/sql/t-sql/data-types/char-and-varchar-transact-sql?view=sql-server-ver16#remarks
|
||||
$maxLength = max(
|
||||
array_map(
|
||||
static fn ($value): int => strlen($value),
|
||||
$attributes['CONSTRAINT'],
|
||||
),
|
||||
);
|
||||
|
||||
$attributes['TYPE'] = 'VARCHAR';
|
||||
$attributes['CONSTRAINT'] = $maxLength;
|
||||
break;
|
||||
|
||||
case 'TIMESTAMP':
|
||||
$attributes['TYPE'] = 'DATETIME';
|
||||
break;
|
||||
|
||||
case 'BOOLEAN':
|
||||
$attributes['TYPE'] = 'BIT';
|
||||
break;
|
||||
|
||||
case 'BLOB':
|
||||
$attributes['TYPE'] = 'VARBINARY';
|
||||
$attributes['CONSTRAINT'] ??= 'MAX';
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Field attribute AUTO_INCREMENT
|
||||
*/
|
||||
protected function _attributeAutoIncrement(array &$attributes, array &$field)
|
||||
{
|
||||
if (! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === true && str_contains(strtolower($field['type']), strtolower('INT'))) {
|
||||
$field['auto_increment'] = ' IDENTITY(1,1)';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a platform-specific DROP TABLE string
|
||||
*
|
||||
* @todo Support for cascade
|
||||
*/
|
||||
protected function _dropTable(string $table, bool $ifExists, bool $cascade): string
|
||||
{
|
||||
$sql = 'DROP TABLE';
|
||||
|
||||
if ($ifExists) {
|
||||
$sql .= ' IF EXISTS ';
|
||||
}
|
||||
|
||||
$table = ' [' . $this->db->database . '].[' . $this->db->schema . '].[' . $table . '] ';
|
||||
|
||||
$sql .= $table;
|
||||
|
||||
if ($cascade) {
|
||||
$sql .= '';
|
||||
}
|
||||
|
||||
return $sql;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs sql to check if key is a constraint.
|
||||
*/
|
||||
protected function _dropKeyAsConstraint(string $table, string $constraintName): string
|
||||
{
|
||||
return "SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
|
||||
WHERE TABLE_NAME= '" . trim($table, '"') . "'
|
||||
AND CONSTRAINT_NAME = '" . trim($constraintName, '"') . "'";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* This file is part of CodeIgniter 4 framework.
|
||||
*
|
||||
* (c) CodeIgniter Foundation <admin@codeigniter.com>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Database\SQLSRV;
|
||||
|
||||
use CodeIgniter\Database\BasePreparedQuery;
|
||||
use CodeIgniter\Database\Exceptions\DatabaseException;
|
||||
use CodeIgniter\Exceptions\BadMethodCallException;
|
||||
|
||||
/**
|
||||
* Prepared query for Postgre
|
||||
*
|
||||
* @extends BasePreparedQuery<resource, resource, resource>
|
||||
*/
|
||||
class PreparedQuery extends BasePreparedQuery
|
||||
{
|
||||
/**
|
||||
* Parameters array used to store the dynamic variables.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $parameters = [];
|
||||
|
||||
/**
|
||||
* A reference to the db connection to use.
|
||||
*
|
||||
* @var Connection
|
||||
*/
|
||||
protected $db;
|
||||
|
||||
public function __construct(Connection $db)
|
||||
{
|
||||
parent::__construct($db);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares the query against the database, and saves the connection
|
||||
* info necessary to execute the query later.
|
||||
*
|
||||
* NOTE: This version is based on SQL code. Child classes should
|
||||
* override this method.
|
||||
*
|
||||
* @param array $options Options takes an associative array;
|
||||
*
|
||||
* @throws DatabaseException
|
||||
*/
|
||||
public function _prepare(string $sql, array $options = []): PreparedQuery
|
||||
{
|
||||
// Prepare parameters for the query
|
||||
$queryString = $this->getQueryString();
|
||||
|
||||
$parameters = $this->parameterize($queryString, $options);
|
||||
|
||||
// Prepare the query
|
||||
$this->statement = sqlsrv_prepare($this->db->connID, $sql, $parameters);
|
||||
|
||||
if (! $this->statement) {
|
||||
if ($this->db->DBDebug) {
|
||||
throw new DatabaseException($this->db->getAllErrorMessages());
|
||||
}
|
||||
|
||||
$info = $this->db->error();
|
||||
$this->errorCode = $info['code'];
|
||||
$this->errorString = $info['message'];
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes a new set of data and runs it against the currently
|
||||
* prepared query.
|
||||
*/
|
||||
public function _execute(array $data): bool
|
||||
{
|
||||
if (! isset($this->statement)) {
|
||||
throw new BadMethodCallException('You must call prepare before trying to execute a prepared statement.');
|
||||
}
|
||||
|
||||
foreach ($data as $key => $value) {
|
||||
$this->parameters[$key] = $value;
|
||||
}
|
||||
|
||||
$result = sqlsrv_execute($this->statement);
|
||||
|
||||
if ($result === false && $this->db->DBDebug) {
|
||||
throw new DatabaseException($this->db->getAllErrorMessages());
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the statement resource for the prepared query or false when preparing failed.
|
||||
*
|
||||
* @return resource|null
|
||||
*/
|
||||
public function _getResult()
|
||||
{
|
||||
return $this->statement;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deallocate prepared statements.
|
||||
*/
|
||||
protected function _close(): bool
|
||||
{
|
||||
return sqlsrv_free_stmt($this->statement);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle parameters.
|
||||
*
|
||||
* @param array<int, mixed> $options
|
||||
*/
|
||||
protected function parameterize(string $queryString, array $options): array
|
||||
{
|
||||
$numberOfVariables = substr_count($queryString, '?');
|
||||
|
||||
$params = [];
|
||||
|
||||
for ($c = 0; $c < $numberOfVariables; $c++) {
|
||||
$this->parameters[$c] = null;
|
||||
if (isset($options[$c])) {
|
||||
$params[] = [&$this->parameters[$c], SQLSRV_PARAM_IN, $options[$c]];
|
||||
} else {
|
||||
$params[] = &$this->parameters[$c];
|
||||
}
|
||||
}
|
||||
|
||||
return $params;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,176 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* This file is part of CodeIgniter 4 framework.
|
||||
*
|
||||
* (c) CodeIgniter Foundation <admin@codeigniter.com>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Database\SQLSRV;
|
||||
|
||||
use CodeIgniter\Database\BaseResult;
|
||||
use CodeIgniter\Entity\Entity;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* Result for SQLSRV
|
||||
*
|
||||
* @extends BaseResult<resource, resource>
|
||||
*/
|
||||
class Result extends BaseResult
|
||||
{
|
||||
/**
|
||||
* Gets the number of fields in the result set.
|
||||
*/
|
||||
public function getFieldCount(): int
|
||||
{
|
||||
return @sqlsrv_num_fields($this->resultID);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates an array of column names in the result set.
|
||||
*/
|
||||
public function getFieldNames(): array
|
||||
{
|
||||
$fieldNames = [];
|
||||
|
||||
foreach (sqlsrv_field_metadata($this->resultID) as $field) {
|
||||
$fieldNames[] = $field['Name'];
|
||||
}
|
||||
|
||||
return $fieldNames;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates an array of objects representing field meta-data.
|
||||
*/
|
||||
public function getFieldData(): array
|
||||
{
|
||||
static $dataTypes = [
|
||||
SQLSRV_SQLTYPE_BIGINT => 'bigint',
|
||||
SQLSRV_SQLTYPE_BIT => 'bit',
|
||||
SQLSRV_SQLTYPE_CHAR => 'char',
|
||||
|
||||
SQLSRV_SQLTYPE_DATE => 'date',
|
||||
SQLSRV_SQLTYPE_DATETIME => 'datetime',
|
||||
SQLSRV_SQLTYPE_DATETIME2 => 'datetime2',
|
||||
SQLSRV_SQLTYPE_DATETIMEOFFSET => 'datetimeoffset',
|
||||
|
||||
SQLSRV_SQLTYPE_DECIMAL => 'decimal',
|
||||
SQLSRV_SQLTYPE_FLOAT => 'float',
|
||||
|
||||
SQLSRV_SQLTYPE_IMAGE => 'image',
|
||||
SQLSRV_SQLTYPE_INT => 'int',
|
||||
SQLSRV_SQLTYPE_MONEY => 'money',
|
||||
SQLSRV_SQLTYPE_NCHAR => 'nchar',
|
||||
SQLSRV_SQLTYPE_NUMERIC => 'numeric',
|
||||
|
||||
SQLSRV_SQLTYPE_NVARCHAR => 'nvarchar',
|
||||
SQLSRV_SQLTYPE_NTEXT => 'ntext',
|
||||
|
||||
SQLSRV_SQLTYPE_REAL => 'real',
|
||||
SQLSRV_SQLTYPE_SMALLDATETIME => 'smalldatetime',
|
||||
SQLSRV_SQLTYPE_SMALLINT => 'smallint',
|
||||
SQLSRV_SQLTYPE_SMALLMONEY => 'smallmoney',
|
||||
SQLSRV_SQLTYPE_TEXT => 'text',
|
||||
|
||||
SQLSRV_SQLTYPE_TIME => 'time',
|
||||
SQLSRV_SQLTYPE_TIMESTAMP => 'timestamp',
|
||||
SQLSRV_SQLTYPE_TINYINT => 'tinyint',
|
||||
SQLSRV_SQLTYPE_UNIQUEIDENTIFIER => 'uniqueidentifier',
|
||||
SQLSRV_SQLTYPE_UDT => 'udt',
|
||||
SQLSRV_SQLTYPE_VARBINARY => 'varbinary',
|
||||
SQLSRV_SQLTYPE_VARCHAR => 'varchar',
|
||||
SQLSRV_SQLTYPE_XML => 'xml',
|
||||
];
|
||||
|
||||
$retVal = [];
|
||||
|
||||
foreach (sqlsrv_field_metadata($this->resultID) as $i => $field) {
|
||||
$retVal[$i] = new stdClass();
|
||||
|
||||
$retVal[$i]->name = $field['Name'];
|
||||
$retVal[$i]->type = $field['Type'];
|
||||
$retVal[$i]->type_name = $dataTypes[$field['Type']] ?? null;
|
||||
$retVal[$i]->max_length = $field['Size'];
|
||||
}
|
||||
|
||||
return $retVal;
|
||||
}
|
||||
|
||||
/**
|
||||
* Frees the current result.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function freeResult()
|
||||
{
|
||||
if (is_resource($this->resultID)) {
|
||||
sqlsrv_free_stmt($this->resultID);
|
||||
$this->resultID = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves the internal pointer to the desired offset. This is called
|
||||
* internally before fetching results to make sure the result set
|
||||
* starts at zero.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function dataSeek(int $n = 0)
|
||||
{
|
||||
if ($n > 0) {
|
||||
for ($i = 0; $i < $n; $i++) {
|
||||
if (sqlsrv_fetch($this->resultID) === false) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the result set as an array.
|
||||
*
|
||||
* Overridden by driver classes.
|
||||
*
|
||||
* @return array|false|null
|
||||
*/
|
||||
protected function fetchAssoc()
|
||||
{
|
||||
return sqlsrv_fetch_array($this->resultID, SQLSRV_FETCH_ASSOC);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the result set as an object.
|
||||
*
|
||||
* @return Entity|false|object|stdClass
|
||||
*/
|
||||
protected function fetchObject(string $className = 'stdClass')
|
||||
{
|
||||
if (is_subclass_of($className, Entity::class)) {
|
||||
return empty($data = $this->fetchAssoc()) ? false : (new $className())->injectRawData($data);
|
||||
}
|
||||
|
||||
return sqlsrv_fetch_object($this->resultID, $className);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of rows in the resultID (i.e., SQLSRV query result resource)
|
||||
*/
|
||||
public function getNumRows(): int
|
||||
{
|
||||
if (! is_int($this->numRows)) {
|
||||
$this->numRows = sqlsrv_num_rows($this->resultID);
|
||||
}
|
||||
|
||||
return $this->numRows;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* This file is part of CodeIgniter 4 framework.
|
||||
*
|
||||
* (c) CodeIgniter Foundation <admin@codeigniter.com>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Database\SQLSRV;
|
||||
|
||||
use CodeIgniter\Database\BaseUtils;
|
||||
use CodeIgniter\Database\ConnectionInterface;
|
||||
use CodeIgniter\Database\Exceptions\DatabaseException;
|
||||
|
||||
/**
|
||||
* Utils for SQLSRV
|
||||
*/
|
||||
class Utils extends BaseUtils
|
||||
{
|
||||
/**
|
||||
* List databases statement
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $listDatabases = 'EXEC sp_helpdb'; // Can also be: EXEC sp_databases
|
||||
|
||||
/**
|
||||
* OPTIMIZE TABLE statement
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $optimizeTable = 'ALTER INDEX all ON %s REORGANIZE';
|
||||
|
||||
public function __construct(ConnectionInterface $db)
|
||||
{
|
||||
parent::__construct($db);
|
||||
|
||||
$this->optimizeTable = 'ALTER INDEX all ON ' . $this->db->schema . '.%s REORGANIZE';
|
||||
}
|
||||
|
||||
/**
|
||||
* Platform dependent version of the backup function.
|
||||
*
|
||||
* @return never
|
||||
*/
|
||||
public function _backup(?array $prefs = null)
|
||||
{
|
||||
throw new DatabaseException('Unsupported feature of the database platform you are using.');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user