|
|
@ -44,9 +44,13 @@ class QueryBuilder extends \yii\base\Object |
|
|
|
public $separator = " "; |
|
|
|
public $separator = " "; |
|
|
|
/** |
|
|
|
/** |
|
|
|
* @var Query the Query object that is currently processed by the query builder to generate a SQL statement. |
|
|
|
* @var Query the Query object that is currently processed by the query builder to generate a SQL statement. |
|
|
|
* After the SQL statement is generated by [[build()]], this property will be set null. |
|
|
|
* This property will be set null upon completion of [[build()]]. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public $query; |
|
|
|
public $query; |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* @var boolean whether to automatically quote table and column names when generating SQL statements. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public $autoQuote = true; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Constructor. |
|
|
|
* Constructor. |
|
|
@ -99,8 +103,8 @@ class QueryBuilder extends \yii\base\Object |
|
|
|
* ~~~ |
|
|
|
* ~~~ |
|
|
|
* $params = array(); |
|
|
|
* $params = array(); |
|
|
|
* $sql = $queryBuilder->insert('tbl_user', array( |
|
|
|
* $sql = $queryBuilder->insert('tbl_user', array( |
|
|
|
* 'name' => 'Sam', |
|
|
|
* 'name' => 'Sam', |
|
|
|
* 'age' => 30, |
|
|
|
* 'age' => 30, |
|
|
|
* ), $params); |
|
|
|
* ), $params); |
|
|
|
* ~~~ |
|
|
|
* ~~~ |
|
|
|
* |
|
|
|
* |
|
|
@ -116,7 +120,7 @@ class QueryBuilder extends \yii\base\Object |
|
|
|
$placeholders = array(); |
|
|
|
$placeholders = array(); |
|
|
|
$count = 0; |
|
|
|
$count = 0; |
|
|
|
foreach ($columns as $name => $value) { |
|
|
|
foreach ($columns as $name => $value) { |
|
|
|
$names[] = $this->driver->quoteColumnName($name); |
|
|
|
$names[] = $this->quoteColumnName($name); |
|
|
|
if ($value instanceof Expression) { |
|
|
|
if ($value instanceof Expression) { |
|
|
|
$placeholders[] = $value->expression; |
|
|
|
$placeholders[] = $value->expression; |
|
|
|
foreach ($value->params as $n => $v) { |
|
|
|
foreach ($value->params as $n => $v) { |
|
|
@ -132,7 +136,7 @@ class QueryBuilder extends \yii\base\Object |
|
|
|
$this->query->addParams($params); |
|
|
|
$this->query->addParams($params); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return 'INSERT INTO ' . $this->driver->quoteTableName($table) |
|
|
|
return 'INSERT INTO ' . $this->quoteTableName($table) |
|
|
|
. ' (' . implode(', ', $names) . ') VALUES (' |
|
|
|
. ' (' . implode(', ', $names) . ') VALUES (' |
|
|
|
. implode(', ', $placeholders) . ')'; |
|
|
|
. implode(', ', $placeholders) . ')'; |
|
|
|
} |
|
|
|
} |
|
|
@ -145,7 +149,7 @@ class QueryBuilder extends \yii\base\Object |
|
|
|
* ~~~ |
|
|
|
* ~~~ |
|
|
|
* $params = array(); |
|
|
|
* $params = array(); |
|
|
|
* $sql = $queryBuilder->update('tbl_user', array( |
|
|
|
* $sql = $queryBuilder->update('tbl_user', array( |
|
|
|
* 'status' => 1, |
|
|
|
* 'status' => 1, |
|
|
|
* ), 'age > 30', $params); |
|
|
|
* ), 'age > 30', $params); |
|
|
|
* ~~~ |
|
|
|
* ~~~ |
|
|
|
* |
|
|
|
* |
|
|
@ -163,12 +167,12 @@ class QueryBuilder extends \yii\base\Object |
|
|
|
$count = 0; |
|
|
|
$count = 0; |
|
|
|
foreach ($columns as $name => $value) { |
|
|
|
foreach ($columns as $name => $value) { |
|
|
|
if ($value instanceof Expression) { |
|
|
|
if ($value instanceof Expression) { |
|
|
|
$lines[] = $this->driver->quoteSimpleColumnName($name) . '=' . $value->expression; |
|
|
|
$lines[] = $this->quoteColumnName($name, true) . '=' . $value->expression; |
|
|
|
foreach ($value->params as $n => $v) { |
|
|
|
foreach ($value->params as $n => $v) { |
|
|
|
$params[$n] = $v; |
|
|
|
$params[$n] = $v; |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
$lines[] = $this->driver->quoteSimpleColumnName($name) . '=:p' . $count; |
|
|
|
$lines[] = $this->quoteColumnName($name, true) . '=:p' . $count; |
|
|
|
$params[':p' . $count] = $value; |
|
|
|
$params[':p' . $count] = $value; |
|
|
|
$count++; |
|
|
|
$count++; |
|
|
|
} |
|
|
|
} |
|
|
@ -176,7 +180,7 @@ class QueryBuilder extends \yii\base\Object |
|
|
|
if ($this->query instanceof Query) { |
|
|
|
if ($this->query instanceof Query) { |
|
|
|
$this->query->addParams($params); |
|
|
|
$this->query->addParams($params); |
|
|
|
} |
|
|
|
} |
|
|
|
$sql = 'UPDATE ' . $this->driver->quoteTableName($table) . ' SET ' . implode(', ', $lines); |
|
|
|
$sql = 'UPDATE ' . $this->quoteTableName($table) . ' SET ' . implode(', ', $lines); |
|
|
|
if (($where = $this->buildCondition($condition)) != '') { |
|
|
|
if (($where = $this->buildCondition($condition)) != '') { |
|
|
|
$sql .= ' WHERE ' . $where; |
|
|
|
$sql .= ' WHERE ' . $where; |
|
|
|
} |
|
|
|
} |
|
|
@ -199,7 +203,7 @@ class QueryBuilder extends \yii\base\Object |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function delete($table, $condition = '') |
|
|
|
public function delete($table, $condition = '') |
|
|
|
{ |
|
|
|
{ |
|
|
|
$sql = 'DELETE FROM ' . $this->driver->quoteTableName($table); |
|
|
|
$sql = 'DELETE FROM ' . $this->quoteTableName($table); |
|
|
|
if (($where = $this->buildCondition($condition)) != '') { |
|
|
|
if (($where = $this->buildCondition($condition)) != '') { |
|
|
|
$sql .= ' WHERE ' . $where; |
|
|
|
$sql .= ' WHERE ' . $where; |
|
|
|
} |
|
|
|
} |
|
|
@ -221,9 +225,9 @@ class QueryBuilder extends \yii\base\Object |
|
|
|
* |
|
|
|
* |
|
|
|
* ~~~ |
|
|
|
* ~~~ |
|
|
|
* $sql = $queryBuilder->createTable('tbl_user', array( |
|
|
|
* $sql = $queryBuilder->createTable('tbl_user', array( |
|
|
|
* 'id' => 'pk', |
|
|
|
* 'id' => 'pk', |
|
|
|
* 'name' => 'string', |
|
|
|
* 'name' => 'string', |
|
|
|
* 'age' => 'integer', |
|
|
|
* 'age' => 'integer', |
|
|
|
* )); |
|
|
|
* )); |
|
|
|
* ~~~ |
|
|
|
* ~~~ |
|
|
|
* |
|
|
|
* |
|
|
@ -237,13 +241,12 @@ class QueryBuilder extends \yii\base\Object |
|
|
|
$cols = array(); |
|
|
|
$cols = array(); |
|
|
|
foreach ($columns as $name => $type) { |
|
|
|
foreach ($columns as $name => $type) { |
|
|
|
if (is_string($name)) { |
|
|
|
if (is_string($name)) { |
|
|
|
$cols[] = "\t" . $this->driver->quoteColumnName($name) . ' ' . $this->getColumnType($type); |
|
|
|
$cols[] = "\t" . $this->quoteColumnName($name) . ' ' . $this->getColumnType($type); |
|
|
|
} else |
|
|
|
} else { |
|
|
|
{ |
|
|
|
|
|
|
|
$cols[] = "\t" . $type; |
|
|
|
$cols[] = "\t" . $type; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
$sql = "CREATE TABLE " . $this->driver->quoteTableName($table) . " (\n" . implode(",\n", $cols) . "\n)"; |
|
|
|
$sql = "CREATE TABLE " . $this->quoteTableName($table) . " (\n" . implode(",\n", $cols) . "\n)"; |
|
|
|
return $options === null ? $sql : $sql . ' ' . $options; |
|
|
|
return $options === null ? $sql : $sql . ' ' . $options; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -255,7 +258,7 @@ class QueryBuilder extends \yii\base\Object |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function renameTable($oldName, $newName) |
|
|
|
public function renameTable($oldName, $newName) |
|
|
|
{ |
|
|
|
{ |
|
|
|
return 'RENAME TABLE ' . $this->driver->quoteTableName($oldName) . ' TO ' . $this->driver->quoteTableName($newName); |
|
|
|
return 'RENAME TABLE ' . $this->quoteTableName($oldName) . ' TO ' . $this->quoteTableName($newName); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -265,7 +268,7 @@ class QueryBuilder extends \yii\base\Object |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function dropTable($table) |
|
|
|
public function dropTable($table) |
|
|
|
{ |
|
|
|
{ |
|
|
|
return "DROP TABLE " . $this->driver->quoteTableName($table); |
|
|
|
return "DROP TABLE " . $this->quoteTableName($table); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -275,7 +278,7 @@ class QueryBuilder extends \yii\base\Object |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function truncateTable($table) |
|
|
|
public function truncateTable($table) |
|
|
|
{ |
|
|
|
{ |
|
|
|
return "TRUNCATE TABLE " . $this->driver->quoteTableName($table); |
|
|
|
return "TRUNCATE TABLE " . $this->quoteTableName($table); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -289,8 +292,8 @@ class QueryBuilder extends \yii\base\Object |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function addColumn($table, $column, $type) |
|
|
|
public function addColumn($table, $column, $type) |
|
|
|
{ |
|
|
|
{ |
|
|
|
return 'ALTER TABLE ' . $this->driver->quoteTableName($table) |
|
|
|
return 'ALTER TABLE ' . $this->quoteTableName($table) |
|
|
|
. ' ADD ' . $this->driver->quoteColumnName($column) . ' ' |
|
|
|
. ' ADD ' . $this->quoteColumnName($column) . ' ' |
|
|
|
. $this->getColumnType($type); |
|
|
|
. $this->getColumnType($type); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -302,8 +305,8 @@ class QueryBuilder extends \yii\base\Object |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function dropColumn($table, $column) |
|
|
|
public function dropColumn($table, $column) |
|
|
|
{ |
|
|
|
{ |
|
|
|
return "ALTER TABLE " . $this->driver->quoteTableName($table) |
|
|
|
return "ALTER TABLE " . $this->quoteTableName($table) |
|
|
|
. " DROP COLUMN " . $this->driver->quoteSimpleColumnName($column); |
|
|
|
. " DROP COLUMN " . $this->quoteColumnName($column, true); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -315,9 +318,9 @@ class QueryBuilder extends \yii\base\Object |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function renameColumn($table, $oldName, $newName) |
|
|
|
public function renameColumn($table, $oldName, $newName) |
|
|
|
{ |
|
|
|
{ |
|
|
|
return "ALTER TABLE " . $this->driver->quoteTableName($table) |
|
|
|
return "ALTER TABLE " . $this->quoteTableName($table) |
|
|
|
. " RENAME COLUMN " . $this->driver->quoteSimpleColumnName($oldName) |
|
|
|
. " RENAME COLUMN " . $this->quoteColumnName($oldName, true) |
|
|
|
. " TO " . $this->driver->quoteSimpleColumnName($newName); |
|
|
|
. " TO " . $this->quoteColumnName($newName, true); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -332,9 +335,9 @@ class QueryBuilder extends \yii\base\Object |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function alterColumn($table, $column, $type) |
|
|
|
public function alterColumn($table, $column, $type) |
|
|
|
{ |
|
|
|
{ |
|
|
|
return 'ALTER TABLE ' . $this->driver->quoteTableName($table) . ' CHANGE ' |
|
|
|
return 'ALTER TABLE ' . $this->quoteTableName($table) . ' CHANGE ' |
|
|
|
. $this->driver->quoteSimpleColumnName($column) . ' ' |
|
|
|
. $this->quoteColumnName($column, true) . ' ' |
|
|
|
. $this->driver->quoteSimpleColumnName($column) . ' ' |
|
|
|
. $this->quoteColumnName($column, true) . ' ' |
|
|
|
. $this->getColumnType($type); |
|
|
|
. $this->getColumnType($type); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -354,23 +357,11 @@ class QueryBuilder extends \yii\base\Object |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null) |
|
|
|
public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (!is_array($columns)) { |
|
|
|
$sql = 'ALTER TABLE ' . $this->quoteTableName($table) |
|
|
|
$columns = preg_split('/\s*,\s*/', $columns, -1, PREG_SPLIT_NO_EMPTY); |
|
|
|
. ' ADD CONSTRAINT ' . $this->quoteColumnName($name) |
|
|
|
} |
|
|
|
. ' FOREIGN KEY (' . $this->buildColumns($columns) . ')' |
|
|
|
foreach ($columns as $i => $col) { |
|
|
|
. ' REFERENCES ' . $this->quoteTableName($refTable) |
|
|
|
$columns[$i] = $this->driver->quoteColumnName($col); |
|
|
|
. ' (' . $this->buildColumns($refColumns) . ')'; |
|
|
|
} |
|
|
|
|
|
|
|
if (!is_array($refColumns)) { |
|
|
|
|
|
|
|
$refColumns = preg_split('/\s*,\s*/', $refColumns, -1, PREG_SPLIT_NO_EMPTY); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
foreach ($refColumns as $i => $col) { |
|
|
|
|
|
|
|
$refColumns[$i] = $this->driver->quoteColumnName($col); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
$sql = 'ALTER TABLE ' . $this->driver->quoteTableName($table) |
|
|
|
|
|
|
|
. ' ADD CONSTRAINT ' . $this->driver->quoteColumnName($name) |
|
|
|
|
|
|
|
. ' FOREIGN KEY (' . implode(', ', $columns) . ')' |
|
|
|
|
|
|
|
. ' REFERENCES ' . $this->driver->quoteTableName($refTable) |
|
|
|
|
|
|
|
. ' (' . implode(', ', $refColumns) . ')'; |
|
|
|
|
|
|
|
if ($delete !== null) { |
|
|
|
if ($delete !== null) { |
|
|
|
$sql .= ' ON DELETE ' . $delete; |
|
|
|
$sql .= ' ON DELETE ' . $delete; |
|
|
|
} |
|
|
|
} |
|
|
@ -388,8 +379,8 @@ class QueryBuilder extends \yii\base\Object |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function dropForeignKey($name, $table) |
|
|
|
public function dropForeignKey($name, $table) |
|
|
|
{ |
|
|
|
{ |
|
|
|
return 'ALTER TABLE ' . $this->driver->quoteTableName($table) |
|
|
|
return 'ALTER TABLE ' . $this->quoteTableName($table) |
|
|
|
. ' DROP CONSTRAINT ' . $this->driver->quoteColumnName($name); |
|
|
|
. ' DROP CONSTRAINT ' . $this->quoteColumnName($name); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -404,19 +395,10 @@ class QueryBuilder extends \yii\base\Object |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function createIndex($name, $table, $columns, $unique = false) |
|
|
|
public function createIndex($name, $table, $columns, $unique = false) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (!is_array($columns)) { |
|
|
|
|
|
|
|
$columns = preg_split('/\s*,\s*/', $columns, -1, PREG_SPLIT_NO_EMPTY); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
foreach ($columns as $i => $column) { |
|
|
|
|
|
|
|
if (strpos($column, '(') !== false) { |
|
|
|
|
|
|
|
$columns[$i] = $column; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
$columns[$i] = $this->driver->quoteColumnName($column); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return ($unique ? 'CREATE UNIQUE INDEX ' : 'CREATE INDEX ') |
|
|
|
return ($unique ? 'CREATE UNIQUE INDEX ' : 'CREATE INDEX ') |
|
|
|
. $this->driver->quoteTableName($name) . ' ON ' |
|
|
|
. $this->quoteTableName($name) . ' ON ' |
|
|
|
. $this->driver->quoteTableName($table) . ' (' . implode(', ', $columns) . ')'; |
|
|
|
. $this->quoteTableName($table) |
|
|
|
|
|
|
|
. ' (' . $this->buildColumns($columns) . ')'; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -427,7 +409,7 @@ class QueryBuilder extends \yii\base\Object |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function dropIndex($name, $table) |
|
|
|
public function dropIndex($name, $table) |
|
|
|
{ |
|
|
|
{ |
|
|
|
return 'DROP INDEX ' . $this->driver->quoteTableName($name) . ' ON ' . $this->driver->quoteTableName($table); |
|
|
|
return 'DROP INDEX ' . $this->quoteTableName($name) . ' ON ' . $this->quoteTableName($table); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -525,7 +507,7 @@ class QueryBuilder extends \yii\base\Object |
|
|
|
|
|
|
|
|
|
|
|
$column = $condition[1]; |
|
|
|
$column = $condition[1]; |
|
|
|
if (strpos($column, '(') === false) { |
|
|
|
if (strpos($column, '(') === false) { |
|
|
|
$column = $this->connection->quoteColumnName($column); |
|
|
|
$column = $this->quoteColumnName($column); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if ($operator === 'BETWEEN' || $operator === 'NOT BETWEEN') { |
|
|
|
if ($operator === 'BETWEEN' || $operator === 'NOT BETWEEN') { |
|
|
@ -592,26 +574,32 @@ class QueryBuilder extends \yii\base\Object |
|
|
|
return $select . ' *'; |
|
|
|
return $select . ' *'; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (is_string($columns)) { |
|
|
|
if ($this->autoQuote) { |
|
|
|
if (strpos($columns, '(') !== false) { |
|
|
|
if (!is_array($columns)) { |
|
|
|
return $select . ' ' . $columns; |
|
|
|
if (strpos($columns, '(') !== false) { |
|
|
|
} |
|
|
|
return $select . ' ' . $columns; |
|
|
|
$columns = preg_split('/\s*,\s*/', trim($columns), -1, PREG_SPLIT_NO_EMPTY); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foreach ($columns as $i => $column) { |
|
|
|
|
|
|
|
if (is_object($column)) { |
|
|
|
|
|
|
|
$columns[$i] = (string)$column; |
|
|
|
|
|
|
|
} elseif (strpos($column, '(') === false) { |
|
|
|
|
|
|
|
if (preg_match('/^(.*?)(?i:\s+as\s+|\s+)([\w\-\.])$/', $column, $matches)) { |
|
|
|
|
|
|
|
$columns[$i] = $this->connection->quoteColumnName($matches[1]) . ' AS ' . $this->driver->quoteSimpleColumnName($matches[2]); |
|
|
|
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
$columns[$i] = $this->connection->quoteColumnName($column); |
|
|
|
$columns = preg_split('/\s*,\s*/', trim($columns), -1, PREG_SPLIT_NO_EMPTY); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
foreach ($columns as $i => $column) { |
|
|
|
|
|
|
|
if (is_object($column)) { |
|
|
|
|
|
|
|
$columns[$i] = (string)$column; |
|
|
|
|
|
|
|
} elseif (strpos($column, '(') === false) { |
|
|
|
|
|
|
|
if (preg_match('/^(.*?)(?i:\s+as\s+|\s+)([\w\-\.])$/', $column, $matches)) { |
|
|
|
|
|
|
|
$columns[$i] = $this->driver->quoteColumnName($matches[1]) . ' AS ' . $this->driver->quoteSimpleColumnName($matches[2]); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
$columns[$i] = $this->driver->quoteColumnName($column); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return $select . ' ' . implode(', ', $columns); |
|
|
|
if (is_array($columns)) { |
|
|
|
|
|
|
|
$columns = implode(', ', $columns); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return $select . ' ' . $columns; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -624,24 +612,31 @@ class QueryBuilder extends \yii\base\Object |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$tables = $this->query->from; |
|
|
|
$tables = $this->query->from; |
|
|
|
if (is_string($tables) && strpos($tables, '(') !== false) { |
|
|
|
|
|
|
|
return 'FROM ' . $tables; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!is_array($tables)) { |
|
|
|
if ($this->autoQuote) { |
|
|
|
$tables = preg_split('/\s*,\s*/', trim($tables), -1, PREG_SPLIT_NO_EMPTY); |
|
|
|
if (!is_array($tables)) { |
|
|
|
} |
|
|
|
if (strpos($tables, '(') !== false) { |
|
|
|
foreach ($tables as $i => $table) { |
|
|
|
return 'FROM ' . $tables; |
|
|
|
if (strpos($table, '(') === false) { |
|
|
|
|
|
|
|
if (preg_match('/^(.*?)(?i:\s+as\s+|\s+)(.*)$/i', $table, $matches)) { // with alias |
|
|
|
|
|
|
|
$tables[$i] = $this->connection->quoteTableName($matches[1]) . ' ' . $this->connection->quoteTableName($matches[2]); |
|
|
|
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
$tables[$i] = $this->connection->quoteTableName($table); |
|
|
|
$tables = preg_split('/\s*,\s*/', trim($tables), -1, PREG_SPLIT_NO_EMPTY); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
foreach ($tables as $i => $table) { |
|
|
|
|
|
|
|
if (strpos($table, '(') === false) { |
|
|
|
|
|
|
|
if (preg_match('/^(.*?)(?i:\s+as\s+|\s+)(.*)$/i', $table, $matches)) { // with alias |
|
|
|
|
|
|
|
$tables[$i] = $this->driver->quoteTableName($matches[1]) . ' ' . $this->driver->quoteTableName($matches[2]); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
$tables[$i] = $this->driver->quoteTableName($table); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (is_array($tables)) { |
|
|
|
|
|
|
|
$tables = implode(', ', $tables); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return 'FROM ' . implode(', ', $tables); |
|
|
|
return 'FROM ' . $tables; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -661,11 +656,11 @@ class QueryBuilder extends \yii\base\Object |
|
|
|
if (is_array($join)) { // join type, table name, on-condition |
|
|
|
if (is_array($join)) { // join type, table name, on-condition |
|
|
|
if (isset($join[0], $join[1])) { |
|
|
|
if (isset($join[0], $join[1])) { |
|
|
|
$table = $join[1]; |
|
|
|
$table = $join[1]; |
|
|
|
if (strpos($table, '(') === false) { |
|
|
|
if ($this->autoQuote && strpos($table, '(') === false) { |
|
|
|
if (preg_match('/^(.*?)(?i:\s+as\s+|\s+)(.*)$/', $table, $matches)) { // with alias |
|
|
|
if (preg_match('/^(.*?)(?i:\s+as\s+|\s+)(.*)$/', $table, $matches)) { // with alias |
|
|
|
$table = $this->connection->quoteTableName($matches[1]) . ' ' . $this->connection->quoteTableName($matches[2]); |
|
|
|
$table = $this->driver->quoteTableName($matches[1]) . ' ' . $this->driver->quoteTableName($matches[2]); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
$table = $this->connection->quoteTableName($table); |
|
|
|
$table = $this->driver->quoteTableName($table); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
$joins[$i] = strtoupper($join[0]) . ' ' . $table; |
|
|
|
$joins[$i] = strtoupper($join[0]) . ' ' . $table; |
|
|
@ -695,25 +690,11 @@ class QueryBuilder extends \yii\base\Object |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
protected function buildGroupBy() |
|
|
|
protected function buildGroupBy() |
|
|
|
{ |
|
|
|
{ |
|
|
|
$columns = $this->query->groupBy; |
|
|
|
if (empty($this->query->groupBy)) { |
|
|
|
if (empty($columns)) { |
|
|
|
|
|
|
|
return ''; |
|
|
|
return ''; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
return 'GROUP BY ' . $this->buildColumns($this->query->groupBy); |
|
|
|
} |
|
|
|
} |
|
|
|
if (is_string($columns) && strpos($columns, '(') !== false) { |
|
|
|
|
|
|
|
return 'GROUP BY ' . $columns; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!is_array($columns)) { |
|
|
|
|
|
|
|
$columns = preg_split('/\s*,\s*/', trim($columns), -1, PREG_SPLIT_NO_EMPTY); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
foreach ($columns as $i => $column) { |
|
|
|
|
|
|
|
if (is_object($column)) { |
|
|
|
|
|
|
|
$columns[$i] = (string)$column; |
|
|
|
|
|
|
|
} elseif (strpos($column, '(') === false) { |
|
|
|
|
|
|
|
$columns[$i] = $this->connection->quoteColumnName($column); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return 'GROUP BY ' . implode(', ', $columns); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -730,29 +711,34 @@ class QueryBuilder extends \yii\base\Object |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
protected function buildOrderBy() |
|
|
|
protected function buildOrderBy() |
|
|
|
{ |
|
|
|
{ |
|
|
|
$columns = $this->query->orderBy; |
|
|
|
if (empty($this->query->orderBy)) { |
|
|
|
if (empty($columns)) { |
|
|
|
|
|
|
|
return ''; |
|
|
|
return ''; |
|
|
|
} |
|
|
|
} |
|
|
|
if (is_string($columns) && strpos($columns, '(') !== false) { |
|
|
|
$columns = $this->query->orderBy; |
|
|
|
return 'ORDER BY ' . $columns; |
|
|
|
if ($this->autoQuote) { |
|
|
|
} |
|
|
|
if (!is_array($columns)) { |
|
|
|
|
|
|
|
if (strpos($columns, '(') !== false) { |
|
|
|
if (!is_array($columns)) { |
|
|
|
return 'ORDER BY ' . $columns; |
|
|
|
$columns = preg_split('/\s*,\s*/', trim($columns), -1, PREG_SPLIT_NO_EMPTY); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
foreach ($columns as $i => $column) { |
|
|
|
|
|
|
|
if (is_object($column)) { |
|
|
|
|
|
|
|
$columns[$i] = (string)$column; |
|
|
|
|
|
|
|
} elseif (strpos($column, '(') === false) { |
|
|
|
|
|
|
|
if (preg_match('/^(.*?)\s+(asc|desc)$/i', $column, $matches)) { |
|
|
|
|
|
|
|
$columns[$i] = $this->connection->quoteColumnName($matches[1]) . ' ' . strtoupper($matches[2]); |
|
|
|
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
$columns[$i] = $this->connection->quoteColumnName($column); |
|
|
|
$columns = preg_split('/\s*,\s*/', trim($columns), -1, PREG_SPLIT_NO_EMPTY); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
foreach ($columns as $i => $column) { |
|
|
|
|
|
|
|
if (is_object($column)) { |
|
|
|
|
|
|
|
$columns[$i] = (string)$column; |
|
|
|
|
|
|
|
} elseif (strpos($column, '(') === false) { |
|
|
|
|
|
|
|
if (preg_match('/^(.*?)\s+(asc|desc)$/i', $column, $matches)) { |
|
|
|
|
|
|
|
$columns[$i] = $this->driver->quoteColumnName($matches[1]) . ' ' . $matches[2]; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
$columns[$i] = $this->driver->quoteColumnName($column); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (is_array($columns)) { |
|
|
|
|
|
|
|
$columns = implode(', ', $columns); |
|
|
|
} |
|
|
|
} |
|
|
|
return 'ORDER BY ' . implode(', ', $columns); |
|
|
|
return 'ORDER BY ' . $columns; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -789,4 +775,64 @@ class QueryBuilder extends \yii\base\Object |
|
|
|
} |
|
|
|
} |
|
|
|
return "UNION (\n" . implode("\n) UNION (\n", $unions) . "\n)"; |
|
|
|
return "UNION (\n" . implode("\n) UNION (\n", $unions) . "\n)"; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Processes columns and properly quote them if necessary. |
|
|
|
|
|
|
|
* This method will quote columns if [[autoQuote]] is true. |
|
|
|
|
|
|
|
* It will join all columns into a string with comma as separators. |
|
|
|
|
|
|
|
* @param string|array $columns the columns to be processed |
|
|
|
|
|
|
|
* @return string the processing result |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
protected function buildColumns($columns) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if ($this->autoQuote) { |
|
|
|
|
|
|
|
if (!is_array($columns)) { |
|
|
|
|
|
|
|
if (strpos($columns, '(') !== false) { |
|
|
|
|
|
|
|
return $columns; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
$columns = preg_split('/\s*,\s*/', $columns, -1, PREG_SPLIT_NO_EMPTY); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
foreach ($columns as $i => $column) { |
|
|
|
|
|
|
|
if (is_object($column)) { |
|
|
|
|
|
|
|
$columns[$i] = (string)$column; |
|
|
|
|
|
|
|
} elseif (strpos($column, '(') === false) { |
|
|
|
|
|
|
|
$columns[$i] = $this->driver->quoteColumnName($column); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return is_array($columns) ? implode(', ', $columns) : $columns; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Quotes a table name for use in a query. |
|
|
|
|
|
|
|
* This method will perform name quoting only when [[autoQuote]] is true. |
|
|
|
|
|
|
|
* @param string $name table name |
|
|
|
|
|
|
|
* @param boolean $simple whether the name should be treated as a simple table name without any prefix. |
|
|
|
|
|
|
|
* @return string the properly quoted table name |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
protected function quoteTableName($name, $simple = false) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if ($this->autoQuote) { |
|
|
|
|
|
|
|
return $simple ? $this->driver->quoteSimpleTableName($name) : $this->driver->quoteTableName($name); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
return $name; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Quotes a column name for use in a query. |
|
|
|
|
|
|
|
* This method will perform name quoting only when [[autoQuote]] is true. |
|
|
|
|
|
|
|
* @param string $name column name |
|
|
|
|
|
|
|
* @param boolean $simple whether the name should be treated as a simple column name without any prefix. |
|
|
|
|
|
|
|
* @return string the properly quoted column name |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
protected function quoteColumnName($name, $simple = false) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if ($this->autoQuote) { |
|
|
|
|
|
|
|
return $simple ? $this->driver->quoteSimpleColumnName($name) : $this->driver->quoteColumnName($name); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
return $name; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|