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 addColumn ( mixed $table, mixed $column, mixed $type ) | ||
| $table | mixed | |
| $column | mixed | |
| $type | mixed | |
public function addColumn($table, $column, $type)
{
$sql = $this->db->getQueryBuilder()->addColumn($table, $column, $type);
return $this->setSql($sql);;
}
| public addForeignKey ( mixed $name, mixed $table, mixed $columns, mixed $refTable, mixed $refColumns, mixed $delete = null, mixed $update = null ) | ||
| $name | mixed | |
| $table | mixed | |
| $columns | mixed | |
| $refTable | mixed | |
| $refColumns | mixed | |
| $delete | mixed | |
| $update | mixed | |
public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null)
{
throw new NotSupportedException('"' . __METHOD__ . '" is not supported.');
}
| public addPrimaryKey ( mixed $name, mixed $table, mixed $columns ) | ||
| $name | mixed | |
| $table | mixed | |
| $columns | mixed | |
public function addPrimaryKey($name, $table, $columns)
{
throw new NotSupportedException('"' . __METHOD__ . '" is not supported.');
}
| public alterColumn ( mixed $table, mixed $column, mixed $type ) | ||
| $table | mixed | |
| $column | mixed | |
| $type | mixed | |
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 ( string $index, array $columns, array $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 ( string $index, array $columns, array $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 bindValue ( mixed $name, mixed $value, mixed $dataType = null ) | ||
| $name | mixed | |
| $value | mixed | |
| $dataType | mixed | |
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 bindValues ( mixed $values ) | ||
| $values | mixed | |
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 ( string $index, string $text, boolean $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 ( string $index, string|array $source, string $match, array $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 checkIntegrity ( mixed $check = true, mixed $schema = '', mixed $table = '' ) | ||
| $check | mixed | |
| $schema | mixed | |
| $table | mixed | |
public function checkIntegrity($check = true, $schema = '', $table = '')
{
throw new NotSupportedException('"' . __METHOD__ . '" is not supported.');
}
| public createIndex ( mixed $name, mixed $table, mixed $columns, mixed $unique = false ) | ||
| $name | mixed | |
| $table | mixed | |
| $columns | mixed | |
| $unique | mixed | |
public function createIndex($name, $table, $columns, $unique = false)
{
throw new NotSupportedException('"' . __METHOD__ . '" is not supported.');
}
| public createTable ( mixed $table, mixed $columns, mixed $options = null ) | ||
| $table | mixed | |
| $columns | mixed | |
| $options | mixed | |
public function createTable($table, $columns, $options = null)
{
$sql = $this->db->getQueryBuilder()->createTable($table, $columns, $options);
return $this->setSql($sql);
}
| public dropColumn ( mixed $table, mixed $column ) | ||
| $table | mixed | |
| $column | mixed | |
public function dropColumn($table, $column)
{
$sql = $this->db->getQueryBuilder()->dropColumn($table, $column);
return $this->setSql($sql);
}
| public dropForeignKey ( mixed $name, mixed $table ) | ||
| $name | mixed | |
| $table | mixed | |
public function dropForeignKey($name, $table)
{
throw new NotSupportedException('"' . __METHOD__ . '" is not supported.');
}
| public dropIndex ( mixed $name, mixed $table ) | ||
| $name | mixed | |
| $table | mixed | |
public function dropIndex($name, $table)
{
throw new NotSupportedException('"' . __METHOD__ . '" is not supported.');
}
| public dropPrimaryKey ( mixed $name, mixed $table ) | ||
| $name | mixed | |
| $table | mixed | |
public function dropPrimaryKey($name, $table)
{
throw new NotSupportedException('"' . __METHOD__ . '" is not supported.');
}
| public dropTable ( mixed $table ) | ||
| $table | mixed | |
public function dropTable($table)
{
$sql = $this->db->getQueryBuilder()->dropTable($table);
return $this->setSql($sql);
}
| public getRawSql ( ) |
public function getRawSql()
{
return $this->parseFloatParams(parent::getRawSql());
}
| public prepare ( mixed $forRead = null ) | ||
| $forRead | mixed | |
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 renameColumn ( mixed $table, mixed $oldName, mixed $newName ) | ||
| $table | mixed | |
| $oldName | mixed | |
| $newName | mixed | |
public function renameColumn($table, $oldName, $newName)
{
throw new NotSupportedException('"' . __METHOD__ . '" is not supported.');
}
| public renameTable ( mixed $table, mixed $newName ) | ||
| $table | mixed | |
| $newName | mixed | |
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 ( string $index, array $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 resetSequence ( mixed $table, mixed $value = null ) | ||
| $table | mixed | |
| $value | mixed | |
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 ( string $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 truncateTable ( mixed $table ) | ||
| $table | mixed | |
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 ( string $index, array $columns, string|array $condition = '', array $params = [], array $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);
}