Browse Source

Options support for "yii\sphinx\Command::callSnippet()" added.

tags/2.0.0-beta
Paul Klimov 11 years ago
parent
commit
6b5b99764f
  1. 18
      extensions/sphinx/Command.php
  2. 37
      extensions/sphinx/QueryBuilder.php
  3. 22
      tests/unit/extensions/sphinx/CommandTest.php

18
extensions/sphinx/Command.php

@ -496,13 +496,29 @@ class Command extends Component
return $this->setSql($sql); return $this->setSql($sql);
} }
public function callSnippets($index, $source, $query, array $options = []) /**
* 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.
* @param string $query the full-text query to build snippets for.
* @param array $options list of options in format: optionName => optionValue
* @return static the command object itself
*/
public function callSnippets($index, $source, $query, $options = [])
{ {
$params = []; $params = [];
$sql = $this->db->getQueryBuilder()->callSnippets($index, $source, $query, $options, $params); $sql = $this->db->getQueryBuilder()->callSnippets($index, $source, $query, $options, $params);
return $this->setSql($sql)->bindValues($params); return $this->setSql($sql)->bindValues($params);
} }
/**
* 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.
*/
public function callKeywords($index, $text, $fetchStatistic = false) public function callKeywords($index, $text, $fetchStatistic = false)
{ {
$params = []; $params = [];

37
extensions/sphinx/QueryBuilder.php

@ -269,15 +269,26 @@ class QueryBuilder extends Object
} }
/** /**
* Builds a SQL statement for truncating a DB index. * Builds a SQL statement for truncating an index.
* @param string $index the index to be truncated. The name will be properly quoted by the method. * @param string $index the index to be truncated. The name will be properly quoted by the method.
* @return string the SQL statement for truncating a DB index. * @return string the SQL statement for truncating an index.
*/ */
public function truncateIndex($index) public function truncateIndex($index)
{ {
return 'TRUNCATE RTINDEX ' . $this->db->quoteIndexName($index); return 'TRUNCATE RTINDEX ' . $this->db->quoteIndexName($index);
} }
/**
* Builds a SQL statement for call 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.
* @param string $query the full-text query to build snippets for.
* @param array $options list of options in format: optionName => optionValue
* @param array $params the binding parameters that will be modified by this method
* so that they can be bound to the Sphinx command later.
* @return string the SQL statement for call snippets.
*/
public function callSnippets($index, $source, $query, $options, &$params) public function callSnippets($index, $source, $query, $options, &$params)
{ {
if (is_array($source)) { if (is_array($source)) {
@ -297,10 +308,30 @@ class QueryBuilder extends Object
$params[$indexParamName] = $index; $params[$indexParamName] = $index;
$queryParamName = self::PARAM_PREFIX . count($params); $queryParamName = self::PARAM_PREFIX . count($params);
$params[$queryParamName] = $query; $params[$queryParamName] = $query;
$optionSql = ''; // @todo if (!empty($options)) {
$optionParts = [];
foreach ($options as $name => $value) {
$phName = self::PARAM_PREFIX . count($params);
$params[$phName] = $value;
$optionParts[] = $phName . ' AS ' . $name;
}
$optionSql = ', ' . implode(', ', $optionParts);
} else {
$optionSql = '';
}
return 'CALL SNIPPETS(' . $dataSql. ', ' . $indexParamName . ', ' . $queryParamName . $optionSql. ')'; return 'CALL SNIPPETS(' . $dataSql. ', ' . $indexParamName . ', ' . $queryParamName . $optionSql. ')';
} }
/**
* Builds a SQL statement for returning 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
* @param array $params the binding parameters that will be modified by this method
* so that they can be bound to the Sphinx command later.
* @return string the SQL statement for call keywords.
*/
public function callKeywords($index, $text, $fetchStatistic, &$params) public function callKeywords($index, $text, $fetchStatistic, &$params)
{ {
$indexParamName = self::PARAM_PREFIX . count($params); $indexParamName = self::PARAM_PREFIX . count($params);

22
tests/unit/extensions/sphinx/CommandTest.php

@ -253,17 +253,35 @@ class CommandTest extends SphinxTestCase
$this->assertEquals(0, count($rows), 'Unable to delete record!'); $this->assertEquals(0, count($rows), 'Unable to delete record!');
} }
/**
* @depends testQuery
*/
public function testCallSnippets() public function testCallSnippets()
{ {
$db = $this->getConnection(); $db = $this->getConnection();
$query = 'pencil'; $query = 'pencil';
$data = ['Some data sentence about ' . $query]; $source = 'Some data sentence about ' . $query;
$rows = $db->createCommand()->callSnippets('yii2_test_item_index', $data, $query)->queryColumn();
$rows = $db->createCommand()->callSnippets('yii2_test_item_index', $source, $query)->queryColumn();
$this->assertNotEmpty($rows, 'Unable to call snippets!'); $this->assertNotEmpty($rows, 'Unable to call snippets!');
$this->assertContains('<b>' . $query . '</b>', $rows[0], 'Query not present in the snippet!'); $this->assertContains('<b>' . $query . '</b>', $rows[0], 'Query not present in the snippet!');
$rows = $db->createCommand()->callSnippets('yii2_test_item_index', [$source], $query)->queryColumn();
$this->assertNotEmpty($rows, 'Unable to call snippets for array source!');
$options = [
'before_match' => '[',
'after_match' => ']',
'limit' => 20,
];
$snippet = $db->createCommand()->callSnippets('yii2_test_item_index', $source, $query, $options)->queryScalar();
$this->assertContains($options['before_match'] . $query . $options['after_match'], $snippet, 'Unable to apply options!');
} }
/**
* @depends testQuery
*/
public function testCallKeywords() public function testCallKeywords()
{ {
$db = $this->getConnection(); $db = $this->getConnection();

Loading…
Cancel
Save