Class yii\sphinx\Command
Inheritance | yii\sphinx\Command » yii\db\Command |
---|---|
Available since extension's version | 2.0 |
Source Code | https://github.com/yiisoft/yii2-sphinx/blob/master/src/Command.php |
Command represents a SQL statement to be executed against a Sphinx.
A command object is usually created by calling yii\sphinx\Connection::createCommand(). The SQL statement it represents can be set via the sql property.
To execute a non-query SQL (such as INSERT, REPLACE, DELETE, UPDATE), call execute(). To execute a SQL statement that returns result data set (such as SELECT, CALL SNIPPETS, CALL KEYWORDS), use queryAll(), queryOne(), queryColumn(), queryScalar(), or query(). For example,
$articles = $connection->createCommand("SELECT * FROM `idx_article` WHERE MATCH('programming')")->queryAll();
Command supports SQL statement preparation and parameter binding just as \yii\db\Command does.
Command also supports building SQL statements by providing methods such as insert(), update(), etc. For example,
$connection->createCommand()->update('idx_article', [
'genre_id' => 15,
'author_id' => 157,
])->execute();
To build SELECT SQL statements, please use yii\sphinx\Query and yii\sphinx\QueryBuilder instead.
Public Properties
Property | Type | Description | Defined By |
---|---|---|---|
$db | yii\sphinx\Connection | The Sphinx connection that this command is associated with. | yii\sphinx\Command |
Public Methods
Property Details
The Sphinx connection that this command is associated with.
Method Details
public void addColumn ( $table, $column, $type ) | ||
$table | ||
$column | ||
$type |
public function addColumn($table, $column, $type)
{
$sql = $this->db->getQueryBuilder()->addColumn($table, $column, $type);
return $this->setSql($sql);;
}
public void addForeignKey ( $name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null ) | ||
$name | ||
$table | ||
$columns | ||
$refTable | ||
$refColumns | ||
$delete | ||
$update |
public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null)
{
throw new NotSupportedException('"' . __METHOD__ . '" is not supported.');
}
public void addPrimaryKey ( $name, $table, $columns ) | ||
$name | ||
$table | ||
$columns |
public function addPrimaryKey($name, $table, $columns)
{
throw new NotSupportedException('"' . __METHOD__ . '" is not supported.');
}
public void alterColumn ( $table, $column, $type ) | ||
$table | ||
$column | ||
$type |
public function alterColumn($table, $column, $type)
{
throw new NotSupportedException('"' . __METHOD__ . '" is not supported.');
}
Creates a batch INSERT command.
For example,
$connection->createCommand()->batchInsert('idx_user', ['name', 'age'], [
['Tom', 30],
['Jane', 20],
['Linda', 25],
])->execute();
Note that the values in each row must match the corresponding column names.
public $this batchInsert ( $index, $columns, $rows ) | ||
$index | string |
The index that new rows will be inserted into. |
$columns | array |
The column names |
$rows | array |
The rows to be batch inserted into the index |
return | $this |
The command object itself |
---|
public function batchInsert($index, $columns, $rows)
{
$params = [];
$sql = $this->db->getQueryBuilder()->batchInsert($index, $columns, $rows, $params);
return $this->setSql($sql)->bindValues($params);
}
Creates a batch REPLACE command.
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.
public $this batchReplace ( $index, $columns, $rows ) | ||
$index | string |
The index that new rows will be replaced. |
$columns | array |
The column names |
$rows | array |
The rows to be batch replaced in the index |
return | $this |
The command object itself |
---|
public function batchReplace($index, $columns, $rows)
{
$params = [];
$sql = $this->db->getQueryBuilder()->batchReplace($index, $columns, $rows, $params);
return $this->setSql($sql)->bindValues($params);
}
public void bindValue ( $name, $value, $dataType = null ) | ||
$name | ||
$value | ||
$dataType |
public function bindValue($name, $value, $dataType = null)
{
if ($this->db->enableFloatConversion && $dataType === null && is_float($value)) {
$this->floatParams[$name] = $value;
return $this;
}
return parent::bindValue($name, $value, $dataType);
}
public void bindValues ( $values ) | ||
$values |
public function bindValues($values)
{
if ($this->db->enableFloatConversion) {
if (empty($values)) {
return $this;
}
foreach ($values as $name => $value) {
if (is_float($value)) {
$this->floatParams[$name] = $value;
unset($values[$name]);
}
}
}
return parent::bindValues($values);
}
Returns tokenized and normalized forms of the keywords, and, optionally, keyword statistics.
public $this callKeywords ( $index, $text, $fetchStatistic = false ) | ||
$index | string |
The name of the index from which to take the text processing settings |
$text | string |
The text to break down to keywords. |
$fetchStatistic | boolean |
Whether to return document and hit occurrence statistics |
return | $this |
The command object itself |
---|
public function callKeywords($index, $text, $fetchStatistic = false)
{
$params = [];
$sql = $this->db->getQueryBuilder()->callKeywords($index, $text, $fetchStatistic, $params);
return $this->setSql($sql)->bindValues($params);
}
Builds a snippet from provided data and query, using specified index settings.
public $this callSnippets ( $index, $source, $match, $options = [] ) | ||
$index | string |
Name of the index, from which to take the text processing settings. |
$source | string|array |
Is the source data to extract a snippet from. It could be either a single string or array of strings. |
$match | string |
The full-text query to build snippets for. |
$options | array |
List of options in format: optionName => optionValue |
return | $this |
The command object itself |
---|
public function callSnippets($index, $source, $match, $options = [])
{
$params = [];
$sql = $this->db->getQueryBuilder()->callSnippets($index, $source, $match, $options, $params);
return $this->setSql($sql)->bindValues($params);
}
public void checkIntegrity ( $check = true, $schema = '', $table = '' ) | ||
$check | ||
$schema | ||
$table |
public function checkIntegrity($check = true, $schema = '', $table = '')
{
throw new NotSupportedException('"' . __METHOD__ . '" is not supported.');
}
public void createIndex ( $name, $table, $columns, $unique = false ) | ||
$name | ||
$table | ||
$columns | ||
$unique |
public function createIndex($name, $table, $columns, $unique = false)
{
throw new NotSupportedException('"' . __METHOD__ . '" is not supported.');
}
public void createTable ( $table, $columns, $options = null ) | ||
$table | ||
$columns | ||
$options |
public function createTable($table, $columns, $options = null)
{
$sql = $this->db->getQueryBuilder()->createTable($table, $columns, $options);
return $this->setSql($sql);
}
public void dropColumn ( $table, $column ) | ||
$table | ||
$column |
public function dropColumn($table, $column)
{
$sql = $this->db->getQueryBuilder()->dropColumn($table, $column);
return $this->setSql($sql);
}
public void dropForeignKey ( $name, $table ) | ||
$name | ||
$table |
public function dropForeignKey($name, $table)
{
throw new NotSupportedException('"' . __METHOD__ . '" is not supported.');
}
public void dropIndex ( $name, $table ) | ||
$name | ||
$table |
public function dropIndex($name, $table)
{
throw new NotSupportedException('"' . __METHOD__ . '" is not supported.');
}
public void dropPrimaryKey ( $name, $table ) | ||
$name | ||
$table |
public function dropPrimaryKey($name, $table)
{
throw new NotSupportedException('"' . __METHOD__ . '" is not supported.');
}
public void dropTable ( $table ) | ||
$table |
public function dropTable($table)
{
$sql = $this->db->getQueryBuilder()->dropTable($table);
return $this->setSql($sql);
}
public void getRawSql ( ) |
public function getRawSql()
{
return $this->parseFloatParams(parent::getRawSql());
}
public void prepare ( $forRead = null ) | ||
$forRead |
public function prepare($forRead = null)
{
if ($this->pdoStatement && empty($this->floatParams)) {
$this->bindPendingParams();
return;
}
$sql = $this->getSql();
if ($this->db->getTransaction()) {
// master is in a transaction. use the same connection.
$forRead = false;
}
if ($forRead || $forRead === null && $this->db->getSchema()->isReadQuery($sql)) {
$pdo = $this->db->getSlavePdo();
} else {
$pdo = $this->db->getMasterPdo();
}
if (!empty($this->floatParams)) {
$sql = $this->parseFloatParams($sql);
}
try {
$this->pdoStatement = $pdo->prepare($sql);
$this->bindPendingParams();
} catch (\Exception $e) {
$message = $e->getMessage() . "\nFailed to prepare SphinxQL: $sql";
$errorInfo = $e instanceof \PDOException ? $e->errorInfo : null;
throw new Exception($message, $errorInfo, (int) $e->getCode(), $e);
}
}
public void renameColumn ( $table, $oldName, $newName ) | ||
$table | ||
$oldName | ||
$newName |
public function renameColumn($table, $oldName, $newName)
{
throw new NotSupportedException('"' . __METHOD__ . '" is not supported.');
}
public void renameTable ( $table, $newName ) | ||
$table | ||
$newName |
public function renameTable($table, $newName)
{
throw new NotSupportedException('"' . __METHOD__ . '" is not supported.');
}
Creates an REPLACE command.
For example,
$connection->createCommand()->replace('idx_user', [
'name' => 'Sam',
'age' => 30,
])->execute();
The method will properly escape the column names, and bind the values to be replaced.
Note that the created command is not executed until execute() is called.
public $this replace ( $index, $columns ) | ||
$index | string |
The index that new rows will be replaced into. |
$columns | array |
The column data (name => value) to be replaced into the index. |
return | $this |
The command object itself |
---|
public function replace($index, $columns)
{
$params = [];
$sql = $this->db->getQueryBuilder()->replace($index, $columns, $params);
return $this->setSql($sql)->bindValues($params);
}
public void resetSequence ( $table, $value = null ) | ||
$table | ||
$value |
public function resetSequence($table, $value = null)
{
throw new NotSupportedException('"' . __METHOD__ . '" is not supported.');
}
Creates a SQL command for truncating a runtime index.
public $this truncateIndex ( $index ) | ||
$index | string |
The index to be truncated. The name will be properly quoted by the method. |
return | $this |
The command object itself |
---|
public function truncateIndex($index)
{
$sql = $this->db->getQueryBuilder()->truncateIndex($index);
return $this->setSql($sql);
}
public void truncateTable ( $table ) | ||
$table |
public function truncateTable($table)
{
$sql = $this->db->getQueryBuilder()->truncateIndex($table);
return $this->setSql($sql);
}
Creates an UPDATE command.
For example,
$connection->createCommand()->update('user', ['status' => 1], 'age > 30')->execute();
The method will properly escape the column names and bind the values to be updated.
Note that the created command is not executed until execute() is called.
public $this update ( $index, $columns, $condition = '', $params = [], $options = [] ) | ||
$index | string |
The index to be updated. |
$columns | array |
The column data (name => value) to be updated. |
$condition | string|array |
The condition that will be put in the WHERE part. Please refer to Query::where() on how to specify condition. |
$params | array |
The parameters to be bound to the command |
$options | array |
List of options in format: optionName => optionValue |
return | $this |
The command object itself |
---|
public function update($index, $columns, $condition = '', $params = [], $options = [])
{
$sql = $this->db->getQueryBuilder()->update($index, $columns, $condition, $params, $options);
return $this->setSql($sql)->bindValues($params);
}