|
|
|
@ -44,9 +44,13 @@ class QueryBuilder extends \yii\base\Object
|
|
|
|
|
public $separator = " "; |
|
|
|
|
/** |
|
|
|
|
* @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; |
|
|
|
|
/** |
|
|
|
|
* @var boolean whether to automatically quote table and column names when generating SQL statements. |
|
|
|
|
*/ |
|
|
|
|
public $autoQuote = true; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Constructor. |
|
|
|
@ -99,8 +103,8 @@ class QueryBuilder extends \yii\base\Object
|
|
|
|
|
* ~~~ |
|
|
|
|
* $params = array(); |
|
|
|
|
* $sql = $queryBuilder->insert('tbl_user', array( |
|
|
|
|
* 'name' => 'Sam', |
|
|
|
|
* 'age' => 30, |
|
|
|
|
* 'name' => 'Sam', |
|
|
|
|
* 'age' => 30, |
|
|
|
|
* ), $params); |
|
|
|
|
* ~~~ |
|
|
|
|
* |
|
|
|
@ -116,7 +120,7 @@ class QueryBuilder extends \yii\base\Object
|
|
|
|
|
$placeholders = array(); |
|
|
|
|
$count = 0; |
|
|
|
|
foreach ($columns as $name => $value) { |
|
|
|
|
$names[] = $this->driver->quoteColumnName($name); |
|
|
|
|
$names[] = $this->quoteColumnName($name); |
|
|
|
|
if ($value instanceof Expression) { |
|
|
|
|
$placeholders[] = $value->expression; |
|
|
|
|
foreach ($value->params as $n => $v) { |
|
|
|
@ -132,7 +136,7 @@ class QueryBuilder extends \yii\base\Object
|
|
|
|
|
$this->query->addParams($params); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return 'INSERT INTO ' . $this->driver->quoteTableName($table) |
|
|
|
|
return 'INSERT INTO ' . $this->quoteTableName($table) |
|
|
|
|
. ' (' . implode(', ', $names) . ') VALUES (' |
|
|
|
|
. implode(', ', $placeholders) . ')'; |
|
|
|
|
} |
|
|
|
@ -145,7 +149,7 @@ class QueryBuilder extends \yii\base\Object
|
|
|
|
|
* ~~~ |
|
|
|
|
* $params = array(); |
|
|
|
|
* $sql = $queryBuilder->update('tbl_user', array( |
|
|
|
|
* 'status' => 1, |
|
|
|
|
* 'status' => 1, |
|
|
|
|
* ), 'age > 30', $params); |
|
|
|
|
* ~~~ |
|
|
|
|
* |
|
|
|
@ -163,12 +167,12 @@ class QueryBuilder extends \yii\base\Object
|
|
|
|
|
$count = 0; |
|
|
|
|
foreach ($columns as $name => $value) { |
|
|
|
|
if ($value instanceof Expression) { |
|
|
|
|
$lines[] = $this->driver->quoteSimpleColumnName($name) . '=' . $value->expression; |
|
|
|
|
$lines[] = $this->quoteColumnName($name, true) . '=' . $value->expression; |
|
|
|
|
foreach ($value->params as $n => $v) { |
|
|
|
|
$params[$n] = $v; |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
$lines[] = $this->driver->quoteSimpleColumnName($name) . '=:p' . $count; |
|
|
|
|
$lines[] = $this->quoteColumnName($name, true) . '=:p' . $count; |
|
|
|
|
$params[':p' . $count] = $value; |
|
|
|
|
$count++; |
|
|
|
|
} |
|
|
|
@ -176,7 +180,7 @@ class QueryBuilder extends \yii\base\Object
|
|
|
|
|
if ($this->query instanceof Query) { |
|
|
|
|
$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)) != '') { |
|
|
|
|
$sql .= ' WHERE ' . $where; |
|
|
|
|
} |
|
|
|
@ -199,7 +203,7 @@ class QueryBuilder extends \yii\base\Object
|
|
|
|
|
*/ |
|
|
|
|
public function delete($table, $condition = '') |
|
|
|
|
{ |
|
|
|
|
$sql = 'DELETE FROM ' . $this->driver->quoteTableName($table); |
|
|
|
|
$sql = 'DELETE FROM ' . $this->quoteTableName($table); |
|
|
|
|
if (($where = $this->buildCondition($condition)) != '') { |
|
|
|
|
$sql .= ' WHERE ' . $where; |
|
|
|
|
} |
|
|
|
@ -221,9 +225,9 @@ class QueryBuilder extends \yii\base\Object
|
|
|
|
|
* |
|
|
|
|
* ~~~ |
|
|
|
|
* $sql = $queryBuilder->createTable('tbl_user', array( |
|
|
|
|
* 'id' => 'pk', |
|
|
|
|
* 'name' => 'string', |
|
|
|
|
* 'age' => 'integer', |
|
|
|
|
* 'id' => 'pk', |
|
|
|
|
* 'name' => 'string', |
|
|
|
|
* 'age' => 'integer', |
|
|
|
|
* )); |
|
|
|
|
* ~~~ |
|
|
|
|
* |
|
|
|
@ -237,13 +241,12 @@ class QueryBuilder extends \yii\base\Object
|
|
|
|
|
$cols = array(); |
|
|
|
|
foreach ($columns as $name => $type) { |
|
|
|
|
if (is_string($name)) { |
|
|
|
|
$cols[] = "\t" . $this->driver->quoteColumnName($name) . ' ' . $this->getColumnType($type); |
|
|
|
|
} else |
|
|
|
|
{ |
|
|
|
|
$cols[] = "\t" . $this->quoteColumnName($name) . ' ' . $this->getColumnType($type); |
|
|
|
|
} else { |
|
|
|
|
$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; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -255,7 +258,7 @@ class QueryBuilder extends \yii\base\Object
|
|
|
|
|
*/ |
|
|
|
|
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) |
|
|
|
|
{ |
|
|
|
|
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) |
|
|
|
|
{ |
|
|
|
|
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) |
|
|
|
|
{ |
|
|
|
|
return 'ALTER TABLE ' . $this->driver->quoteTableName($table) |
|
|
|
|
. ' ADD ' . $this->driver->quoteColumnName($column) . ' ' |
|
|
|
|
return 'ALTER TABLE ' . $this->quoteTableName($table) |
|
|
|
|
. ' ADD ' . $this->quoteColumnName($column) . ' ' |
|
|
|
|
. $this->getColumnType($type); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -302,8 +305,8 @@ class QueryBuilder extends \yii\base\Object
|
|
|
|
|
*/ |
|
|
|
|
public function dropColumn($table, $column) |
|
|
|
|
{ |
|
|
|
|
return "ALTER TABLE " . $this->driver->quoteTableName($table) |
|
|
|
|
. " DROP COLUMN " . $this->driver->quoteSimpleColumnName($column); |
|
|
|
|
return "ALTER TABLE " . $this->quoteTableName($table) |
|
|
|
|
. " DROP COLUMN " . $this->quoteColumnName($column, true); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -315,9 +318,9 @@ class QueryBuilder extends \yii\base\Object
|
|
|
|
|
*/ |
|
|
|
|
public function renameColumn($table, $oldName, $newName) |
|
|
|
|
{ |
|
|
|
|
return "ALTER TABLE " . $this->driver->quoteTableName($table) |
|
|
|
|
. " RENAME COLUMN " . $this->driver->quoteSimpleColumnName($oldName) |
|
|
|
|
. " TO " . $this->driver->quoteSimpleColumnName($newName); |
|
|
|
|
return "ALTER TABLE " . $this->quoteTableName($table) |
|
|
|
|
. " RENAME COLUMN " . $this->quoteColumnName($oldName, true) |
|
|
|
|
. " TO " . $this->quoteColumnName($newName, true); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -332,9 +335,9 @@ class QueryBuilder extends \yii\base\Object
|
|
|
|
|
*/ |
|
|
|
|
public function alterColumn($table, $column, $type) |
|
|
|
|
{ |
|
|
|
|
return 'ALTER TABLE ' . $this->driver->quoteTableName($table) . ' CHANGE ' |
|
|
|
|
. $this->driver->quoteSimpleColumnName($column) . ' ' |
|
|
|
|
. $this->driver->quoteSimpleColumnName($column) . ' ' |
|
|
|
|
return 'ALTER TABLE ' . $this->quoteTableName($table) . ' CHANGE ' |
|
|
|
|
. $this->quoteColumnName($column, true) . ' ' |
|
|
|
|
. $this->quoteColumnName($column, true) . ' ' |
|
|
|
|
. $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) |
|
|
|
|
{ |
|
|
|
|
if (!is_array($columns)) { |
|
|
|
|
$columns = preg_split('/\s*,\s*/', $columns, -1, PREG_SPLIT_NO_EMPTY); |
|
|
|
|
} |
|
|
|
|
foreach ($columns as $i => $col) { |
|
|
|
|
$columns[$i] = $this->driver->quoteColumnName($col); |
|
|
|
|
} |
|
|
|
|
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) . ')'; |
|
|
|
|
$sql = 'ALTER TABLE ' . $this->quoteTableName($table) |
|
|
|
|
. ' ADD CONSTRAINT ' . $this->quoteColumnName($name) |
|
|
|
|
. ' FOREIGN KEY (' . $this->buildColumns($columns) . ')' |
|
|
|
|
. ' REFERENCES ' . $this->quoteTableName($refTable) |
|
|
|
|
. ' (' . $this->buildColumns($refColumns) . ')'; |
|
|
|
|
if ($delete !== null) { |
|
|
|
|
$sql .= ' ON DELETE ' . $delete; |
|
|
|
|
} |
|
|
|
@ -388,8 +379,8 @@ class QueryBuilder extends \yii\base\Object
|
|
|
|
|
*/ |
|
|
|
|
public function dropForeignKey($name, $table) |
|
|
|
|
{ |
|
|
|
|
return 'ALTER TABLE ' . $this->driver->quoteTableName($table) |
|
|
|
|
. ' DROP CONSTRAINT ' . $this->driver->quoteColumnName($name); |
|
|
|
|
return 'ALTER TABLE ' . $this->quoteTableName($table) |
|
|
|
|
. ' DROP CONSTRAINT ' . $this->quoteColumnName($name); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -404,19 +395,10 @@ class QueryBuilder extends \yii\base\Object
|
|
|
|
|
*/ |
|
|
|
|
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 ') |
|
|
|
|
. $this->driver->quoteTableName($name) . ' ON ' |
|
|
|
|
. $this->driver->quoteTableName($table) . ' (' . implode(', ', $columns) . ')'; |
|
|
|
|
. $this->quoteTableName($name) . ' ON ' |
|
|
|
|
. $this->quoteTableName($table) |
|
|
|
|
. ' (' . $this->buildColumns($columns) . ')'; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -427,7 +409,7 @@ class QueryBuilder extends \yii\base\Object
|
|
|
|
|
*/ |
|
|
|
|
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); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -456,7 +438,7 @@ class QueryBuilder extends \yii\base\Object
|
|
|
|
|
* The conversion is done using the type map specified in [[typeMap]]. |
|
|
|
|
* The following abstract column types are supported (using MySQL as an example to explain the corresponding |
|
|
|
|
* physical types): |
|
|
|
|
* |
|
|
|
|
* |
|
|
|
|
* - `pk`: an auto-incremental primary key type, will be converted into "int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY" |
|
|
|
|
* - `string`: string type, will be converted into "varchar(255)" |
|
|
|
|
* - `text`: a long string type, will be converted into "text" |
|
|
|
@ -525,7 +507,7 @@ class QueryBuilder extends \yii\base\Object
|
|
|
|
|
|
|
|
|
|
$column = $condition[1]; |
|
|
|
|
if (strpos($column, '(') === false) { |
|
|
|
|
$column = $this->connection->quoteColumnName($column); |
|
|
|
|
$column = $this->quoteColumnName($column); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if ($operator === 'BETWEEN' || $operator === 'NOT BETWEEN') { |
|
|
|
@ -592,26 +574,32 @@ class QueryBuilder extends \yii\base\Object
|
|
|
|
|
return $select . ' *'; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (is_string($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]); |
|
|
|
|
if ($this->autoQuote) { |
|
|
|
|
if (!is_array($columns)) { |
|
|
|
|
if (strpos($columns, '(') !== false) { |
|
|
|
|
return $select . ' ' . $columns; |
|
|
|
|
} 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; |
|
|
|
|
if (is_string($tables) && strpos($tables, '(') !== false) { |
|
|
|
|
return 'FROM ' . $tables; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (!is_array($tables)) { |
|
|
|
|
$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->connection->quoteTableName($matches[1]) . ' ' . $this->connection->quoteTableName($matches[2]); |
|
|
|
|
if ($this->autoQuote) { |
|
|
|
|
if (!is_array($tables)) { |
|
|
|
|
if (strpos($tables, '(') !== false) { |
|
|
|
|
return 'FROM ' . $tables; |
|
|
|
|
} 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 (isset($join[0], $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 |
|
|
|
|
$table = $this->connection->quoteTableName($matches[1]) . ' ' . $this->connection->quoteTableName($matches[2]); |
|
|
|
|
$table = $this->driver->quoteTableName($matches[1]) . ' ' . $this->driver->quoteTableName($matches[2]); |
|
|
|
|
} else { |
|
|
|
|
$table = $this->connection->quoteTableName($table); |
|
|
|
|
$table = $this->driver->quoteTableName($table); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
$joins[$i] = strtoupper($join[0]) . ' ' . $table; |
|
|
|
@ -695,25 +690,11 @@ class QueryBuilder extends \yii\base\Object
|
|
|
|
|
*/ |
|
|
|
|
protected function buildGroupBy() |
|
|
|
|
{ |
|
|
|
|
$columns = $this->query->groupBy; |
|
|
|
|
if (empty($columns)) { |
|
|
|
|
if (empty($this->query->groupBy)) { |
|
|
|
|
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() |
|
|
|
|
{ |
|
|
|
|
$columns = $this->query->orderBy; |
|
|
|
|
if (empty($columns)) { |
|
|
|
|
if (empty($this->query->orderBy)) { |
|
|
|
|
return ''; |
|
|
|
|
} |
|
|
|
|
if (is_string($columns) && strpos($columns, '(') !== false) { |
|
|
|
|
return 'ORDER 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) { |
|
|
|
|
if (preg_match('/^(.*?)\s+(asc|desc)$/i', $column, $matches)) { |
|
|
|
|
$columns[$i] = $this->connection->quoteColumnName($matches[1]) . ' ' . strtoupper($matches[2]); |
|
|
|
|
$columns = $this->query->orderBy; |
|
|
|
|
if ($this->autoQuote) { |
|
|
|
|
if (!is_array($columns)) { |
|
|
|
|
if (strpos($columns, '(') !== false) { |
|
|
|
|
return 'ORDER BY ' . $columns; |
|
|
|
|
} 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)"; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 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; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|