|
|
|
@ -83,11 +83,48 @@ class QueryBuilder extends Object
|
|
|
|
|
* @param string $index the index that new rows will be inserted into. |
|
|
|
|
* @param array $columns the column data (name => value) to be inserted into the index. |
|
|
|
|
* @param array $params the binding parameters that will be generated by this method. |
|
|
|
|
* They should be bound to the DB command later. |
|
|
|
|
* They should be bound to the Sphinx command later. |
|
|
|
|
* @return string the INSERT SQL |
|
|
|
|
*/ |
|
|
|
|
public function insert($index, $columns, &$params) |
|
|
|
|
{ |
|
|
|
|
return $this->generateInsertReplace('INSERT', $index, $columns, $params); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Creates an REPLACE SQL statement. |
|
|
|
|
* For example, |
|
|
|
|
* |
|
|
|
|
* ~~~ |
|
|
|
|
* $sql = $queryBuilder->replace('idx_user', [ |
|
|
|
|
* 'name' => 'Sam', |
|
|
|
|
* 'age' => 30, |
|
|
|
|
* ], $params); |
|
|
|
|
* ~~~ |
|
|
|
|
* |
|
|
|
|
* The method will properly escape the index and column names. |
|
|
|
|
* |
|
|
|
|
* @param string $index the index that new rows will be replaced. |
|
|
|
|
* @param array $columns the column data (name => value) to be replaced in the index. |
|
|
|
|
* @param array $params the binding parameters that will be generated by this method. |
|
|
|
|
* They should be bound to the Sphinx command later. |
|
|
|
|
* @return string the INSERT SQL |
|
|
|
|
*/ |
|
|
|
|
public function replace($index, $columns, &$params) |
|
|
|
|
{ |
|
|
|
|
return $this->generateInsertReplace('REPLACE', $index, $columns, $params); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Generates INSERT/REPLACE SQL statement. |
|
|
|
|
* @param string $statement statement ot be generated. |
|
|
|
|
* @param string $index the affected index name. |
|
|
|
|
* @param array $columns the column data (name => value). |
|
|
|
|
* @param array $params the binding parameters that will be generated by this method. |
|
|
|
|
* @return string generated SQL |
|
|
|
|
*/ |
|
|
|
|
protected function generateInsertReplace($statement, $index, $columns, &$params) |
|
|
|
|
{ |
|
|
|
|
if (($indexSchema = $this->db->getIndexSchema($index)) !== null) { |
|
|
|
|
$columnSchemas = $indexSchema->columns; |
|
|
|
|
} else { |
|
|
|
@ -120,7 +157,7 @@ class QueryBuilder extends Object
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return 'INSERT INTO ' . $this->db->quoteIndexName($index) |
|
|
|
|
return $statement . ' INTO ' . $this->db->quoteIndexName($index) |
|
|
|
|
. ' (' . implode(', ', $names) . ') VALUES (' |
|
|
|
|
. implode(', ', $placeholders) . ')'; |
|
|
|
|
} |
|
|
|
@ -148,6 +185,46 @@ class QueryBuilder extends Object
|
|
|
|
|
*/ |
|
|
|
|
public function batchInsert($index, $columns, $rows, &$params) |
|
|
|
|
{ |
|
|
|
|
return $this->generateBatchInsertReplace('INSERT', $index, $columns, $rows, $params); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Generates a batch REPLACE SQL statement. |
|
|
|
|
* For example, |
|
|
|
|
* |
|
|
|
|
* ~~~ |
|
|
|
|
* $connection->createCommand()->batchReplace('idx_user', ['name', 'age'], [ |
|
|
|
|
* ['Tom', 30], |
|
|
|
|
* ['Jane', 20], |
|
|
|
|
* ['Linda', 25], |
|
|
|
|
* ])->execute(); |
|
|
|
|
* ~~~ |
|
|
|
|
* |
|
|
|
|
* Note that the values in each row must match the corresponding column names. |
|
|
|
|
* |
|
|
|
|
* @param string $index the index that new rows will be replaced. |
|
|
|
|
* @param array $columns the column names |
|
|
|
|
* @param array $rows the rows to be batch replaced in the index |
|
|
|
|
* @param array $params the binding parameters that will be generated by this method. |
|
|
|
|
* They should be bound to the DB command later. |
|
|
|
|
* @return string the batch INSERT SQL statement |
|
|
|
|
*/ |
|
|
|
|
public function batchReplace($index, $columns, $rows, &$params) |
|
|
|
|
{ |
|
|
|
|
return $this->generateBatchInsertReplace('REPLACE', $index, $columns, $rows, $params); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Generates a batch INSERT/REPLACE SQL statement. |
|
|
|
|
* @param string $statement statement ot be generated. |
|
|
|
|
* @param string $index the affected index name. |
|
|
|
|
* @param array $columns the column data (name => value). |
|
|
|
|
* @param array $rows the rows to be batch inserted into the index |
|
|
|
|
* @param array $params the binding parameters that will be generated by this method. |
|
|
|
|
* @return string generated SQL |
|
|
|
|
*/ |
|
|
|
|
protected function generateBatchInsertReplace($statement, $index, $columns, $rows, &$params) |
|
|
|
|
{ |
|
|
|
|
if (($indexSchema = $this->db->getIndexSchema($index)) !== null) { |
|
|
|
|
$columnSchemas = $indexSchema->columns; |
|
|
|
|
} else { |
|
|
|
@ -183,7 +260,7 @@ class QueryBuilder extends Object
|
|
|
|
|
$values[] = '(' . implode(', ', $vs) . ')'; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return 'INSERT INTO ' . $this->db->quoteIndexName($index) |
|
|
|
|
return $statement . ' INTO ' . $this->db->quoteIndexName($index) |
|
|
|
|
. ' (' . implode(', ', $columns) . ') VALUES ' . implode(', ', $values); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|