Command.php 10 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\sphinx;

10
use Yii;
11
use yii\base\NotSupportedException;
12

13
/**
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
 * Command represents a SQL statement to be executed against a Sphinx.
 *
 * A command object is usually created by calling [[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 [[Query]] and [[QueryBuilder]] instead.
 *
42 43 44
 * @author Paul Klimov <klimov.paul@gmail.com>
 * @since 2.0
 */
45
class Command extends \yii\db\Command
46
{
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
    /**
     * @var \yii\sphinx\Connection the Sphinx connection that this command is associated with.
     */
    public $db;

    /**
     * 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.
     *
66 67 68
     * @param string $index the index that new rows will be inserted into.
     * @param array $columns the column names
     * @param array $rows the rows to be batch inserted into the index
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
     * @return static 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 an REPLACE command.
     * For example,
     *
     * ~~~
     * $connection->createCommand()->insert('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.
     *
94 95
     * @param string $index the index that new rows will be replaced into.
     * @param array $columns the column data (name => value) to be replaced into the index.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
     * @return static the command object itself
     */
    public function replace($index, $columns)
    {
        $params = [];
        $sql = $this->db->getQueryBuilder()->replace($index, $columns, $params);

        return $this->setSql($sql)->bindValues($params);
    }

    /**
     * Creates a batch REPLACE 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.
     *
120 121 122
     * @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
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
     * @return static 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);
    }

    /**
     * Creates an UPDATE command.
     * For example,
     *
     * ~~~
138
     * $connection->createCommand()->update('user', ['status' => 1], 'age > 30')->execute();
139 140 141 142 143 144
     * ~~~
     *
     * 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.
     *
145 146 147 148 149 150 151
     * @param string $index the index to be updated.
     * @param array $columns the column data (name => value) to be updated.
     * @param string|array $condition the condition that will be put in the WHERE part. Please
     * refer to [[Query::where()]] on how to specify condition.
     * @param array $params the parameters to be bound to the command
     * @param array $options list of options in format: optionName => optionValue
     * @return static the command object itself
152 153 154 155 156 157 158 159 160 161
     */
    public function update($index, $columns, $condition = '', $params = [], $options = [])
    {
        $sql = $this->db->getQueryBuilder()->update($index, $columns, $condition, $params, $options);

        return $this->setSql($sql)->bindValues($params);
    }

    /**
     * Creates a SQL command for truncating a runtime index.
162
     * @param string $index the index to be truncated. The name will be properly quoted by the method.
163 164 165 166 167 168 169 170 171 172 173
     * @return static the command object itself
     */
    public function truncateIndex($index)
    {
        $sql = $this->db->getQueryBuilder()->truncateIndex($index);

        return $this->setSql($sql);
    }

    /**
     * Builds a snippet from provided data and query, using specified index settings.
174 175 176 177 178 179
     * @param string $index name of the index, from which to take the text processing settings.
     * @param string|array $source is the source data to extract a snippet from.
     * It could be either a single string or array of strings.
     * @param string $match the full-text query to build snippets for.
     * @param array $options list of options in format: optionName => optionValue
     * @return static the command object itself
180 181 182 183 184 185 186 187 188 189 190
     */
    public function callSnippets($index, $source, $match, $options = [])
    {
        $params = [];
        $sql = $this->db->getQueryBuilder()->callSnippets($index, $source, $match, $options, $params);

        return $this->setSql($sql)->bindValues($params);
    }

    /**
     * Returns tokenized and normalized forms of the keywords, and, optionally, keyword statistics.
191 192 193 194
     * @param string $index the name of the index from which to take the text processing settings
     * @param string $text the text to break down to keywords.
     * @param boolean $fetchStatistic whether to return document and hit occurrence statistics
     * @return string the SQL statement for call keywords.
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
     */
    public function callKeywords($index, $text, $fetchStatistic = false)
    {
        $params = [];
        $sql = $this->db->getQueryBuilder()->callKeywords($index, $text, $fetchStatistic, $params);

        return $this->setSql($sql)->bindValues($params);
    }

    // Not Supported :

    /**
     * @inheritdoc
     */
    public function createTable($table, $columns, $options = null)
    {
        throw new NotSupportedException('"' . __METHOD__ . '" is not supported.');
    }

    /**
     * @inheritdoc
     */
    public function renameTable($table, $newName)
    {
        throw new NotSupportedException('"' . __METHOD__ . '" is not supported.');
    }

    /**
     * @inheritdoc
     */
    public function dropTable($table)
    {
        throw new NotSupportedException('"' . __METHOD__ . '" is not supported.');
    }

    /**
     * @inheritdoc
     */
    public function truncateTable($table)
    {
        throw new NotSupportedException('"' . __METHOD__ . '" is not supported.');
    }

    /**
     * @inheritdoc
     */
    public function addColumn($table, $column, $type)
    {
        throw new NotSupportedException('"' . __METHOD__ . '" is not supported.');
    }

    /**
     * @inheritdoc
     */
    public function dropColumn($table, $column)
    {
        throw new NotSupportedException('"' . __METHOD__ . '" is not supported.');
    }

    /**
     * @inheritdoc
     */
    public function renameColumn($table, $oldName, $newName)
    {
        throw new NotSupportedException('"' . __METHOD__ . '" is not supported.');
    }

    /**
     * @inheritdoc
     */
    public function alterColumn($table, $column, $type)
    {
        throw new NotSupportedException('"' . __METHOD__ . '" is not supported.');
    }

    /**
     * @inheritdoc
     */
    public function addPrimaryKey($name, $table, $columns)
    {
        throw new NotSupportedException('"' . __METHOD__ . '" is not supported.');
    }

    /**
     * @inheritdoc
     */
    public function dropPrimaryKey($name, $table)
    {
        throw new NotSupportedException('"' . __METHOD__ . '" is not supported.');
    }

    /**
     * @inheritdoc
     */
    public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null)
    {
        throw new NotSupportedException('"' . __METHOD__ . '" is not supported.');
    }

    /**
     * @inheritdoc
     */
    public function dropForeignKey($name, $table)
    {
        throw new NotSupportedException('"' . __METHOD__ . '" is not supported.');
    }

    /**
     * @inheritdoc
     */
    public function createIndex($name, $table, $columns, $unique = false)
    {
        throw new NotSupportedException('"' . __METHOD__ . '" is not supported.');
    }

    /**
     * @inheritdoc
     */
    public function dropIndex($name, $table)
    {
        throw new NotSupportedException('"' . __METHOD__ . '" is not supported.');
    }

    /**
     * @inheritdoc
     */
    public function resetSequence($table, $value = null)
    {
        throw new NotSupportedException('"' . __METHOD__ . '" is not supported.');
    }

    /**
     * @inheritdoc
     */
    public function checkIntegrity($check = true, $schema = '', $table = '')
    {
        throw new NotSupportedException('"' . __METHOD__ . '" is not supported.');
    }
Qiang Xue committed
333
}