Command.php 9.18 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
	/**
	 * @var \yii\sphinx\Connection the Sphinx connection that this command is associated with.
	 */
	public $db;

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
	/**
	 * 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.
	 *
	 * @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
	 * @return static the command object itself
	 */
	public function batchInsert($index, $columns, $rows)
	{
73 74 75
		$params = [];
		$sql = $this->db->getQueryBuilder()->batchInsert($index, $columns, $rows, $params);
		return $this->setSql($sql)->bindValues($params);
76 77
	}

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
	/**
	 * 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.
	 *
	 * @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.
	 * @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.
	 *
	 * @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
	 * @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);
	}

130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
	/**
	 * Creates an UPDATE command.
	 * For example,
	 *
	 * ~~~
	 * $connection->createCommand()->update('tbl_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.
	 *
	 * @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
147
	 * @param array $options list of options in format: optionName => optionValue
148 149
	 * @return static the command object itself
	 */
150
	public function update($index, $columns, $condition = '', $params = [], $options = [])
151
	{
152
		$sql = $this->db->getQueryBuilder()->update($index, $columns, $condition, $params, $options);
153 154 155 156 157 158 159 160 161 162 163 164 165
		return $this->setSql($sql)->bindValues($params);
	}

	/**
	 * Creates a SQL command for truncating a runtime index.
	 * @param string $index the index to be truncated. The name will be properly quoted by the method.
	 * @return static the command object itself
	 */
	public function truncateIndex($index)
	{
		$sql = $this->db->getQueryBuilder()->truncateIndex($index);
		return $this->setSql($sql);
	}
166

167 168 169 170 171
	/**
	 * Builds a snippet from provided data and query, using specified index settings.
	 * @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.
172
	 * @param string $match the full-text query to build snippets for.
173 174 175
	 * @param array $options list of options in format: optionName => optionValue
	 * @return static the command object itself
	 */
176
	public function callSnippets($index, $source, $match, $options = [])
177 178
	{
		$params = [];
179
		$sql = $this->db->getQueryBuilder()->callSnippets($index, $source, $match, $options, $params);
180 181 182
		return $this->setSql($sql)->bindValues($params);
	}

183 184 185 186 187 188 189
	/**
	 * Returns tokenized and normalized forms of the keywords, and, optionally, keyword statistics.
	 * @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.
	 */
190 191 192 193 194 195
	public function callKeywords($index, $text, $fetchStatistic = false)
	{
		$params = [];
		$sql = $this->db->getQueryBuilder()->callKeywords($index, $text, $fetchStatistic, $params);
		return $this->setSql($sql)->bindValues($params);
	}
196 197 198 199

	// Not Supported :

	/**
Qiang Xue committed
200
	 * @inheritdoc
201 202 203 204 205 206 207
	 */
	public function createTable($table, $columns, $options = null)
	{
		throw new NotSupportedException('"' . __METHOD__ . '" is not supported.');
	}

	/**
Qiang Xue committed
208
	 * @inheritdoc
209 210 211 212 213 214 215
	 */
	public function renameTable($table, $newName)
	{
		throw new NotSupportedException('"' . __METHOD__ . '" is not supported.');
	}

	/**
Qiang Xue committed
216
	 * @inheritdoc
217 218 219 220 221 222 223
	 */
	public function dropTable($table)
	{
		throw new NotSupportedException('"' . __METHOD__ . '" is not supported.');
	}

	/**
Qiang Xue committed
224
	 * @inheritdoc
225 226 227 228 229 230 231
	 */
	public function truncateTable($table)
	{
		throw new NotSupportedException('"' . __METHOD__ . '" is not supported.');
	}

	/**
Qiang Xue committed
232
	 * @inheritdoc
233 234 235 236 237 238 239
	 */
	public function addColumn($table, $column, $type)
	{
		throw new NotSupportedException('"' . __METHOD__ . '" is not supported.');
	}

	/**
Qiang Xue committed
240
	 * @inheritdoc
241 242 243 244 245 246 247
	 */
	public function dropColumn($table, $column)
	{
		throw new NotSupportedException('"' . __METHOD__ . '" is not supported.');
	}

	/**
Qiang Xue committed
248
	 * @inheritdoc
249 250 251 252 253 254 255
	 */
	public function renameColumn($table, $oldName, $newName)
	{
		throw new NotSupportedException('"' . __METHOD__ . '" is not supported.');
	}

	/**
Qiang Xue committed
256
	 * @inheritdoc
257 258 259 260 261 262 263
	 */
	public function alterColumn($table, $column, $type)
	{
		throw new NotSupportedException('"' . __METHOD__ . '" is not supported.');
	}

	/**
Qiang Xue committed
264
	 * @inheritdoc
265 266 267 268 269 270 271
	 */
	public function addPrimaryKey($name, $table, $columns)
	{
		throw new NotSupportedException('"' . __METHOD__ . '" is not supported.');
	}

	/**
Qiang Xue committed
272
	 * @inheritdoc
273 274 275 276 277 278 279
	 */
	public function dropPrimaryKey($name, $table)
	{
		throw new NotSupportedException('"' . __METHOD__ . '" is not supported.');
	}

	/**
Qiang Xue committed
280
	 * @inheritdoc
281 282 283 284 285 286 287
	 */
	public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null)
	{
		throw new NotSupportedException('"' . __METHOD__ . '" is not supported.');
	}

	/**
Qiang Xue committed
288
	 * @inheritdoc
289 290 291 292 293 294 295
	 */
	public function dropForeignKey($name, $table)
	{
		throw new NotSupportedException('"' . __METHOD__ . '" is not supported.');
	}

	/**
Qiang Xue committed
296
	 * @inheritdoc
297 298 299 300 301 302 303
	 */
	public function createIndex($name, $table, $columns, $unique = false)
	{
		throw new NotSupportedException('"' . __METHOD__ . '" is not supported.');
	}

	/**
Qiang Xue committed
304
	 * @inheritdoc
305 306 307 308 309 310 311
	 */
	public function dropIndex($name, $table)
	{
		throw new NotSupportedException('"' . __METHOD__ . '" is not supported.');
	}

	/**
Qiang Xue committed
312
	 * @inheritdoc
313 314 315 316 317 318 319
	 */
	public function resetSequence($table, $value = null)
	{
		throw new NotSupportedException('"' . __METHOD__ . '" is not supported.');
	}

	/**
Qiang Xue committed
320
	 * @inheritdoc
321 322 323 324 325
	 */
	public function checkIntegrity($check = true, $schema = '')
	{
		throw new NotSupportedException('"' . __METHOD__ . '" is not supported.');
	}
Qiang Xue committed
326
}