From cc156ba812d0952ccc22e1b82caf2c0e6bb8e73b Mon Sep 17 00:00:00 2001 From: Paul Klimov Date: Wed, 13 Nov 2013 14:04:27 +0200 Subject: [PATCH] Methods "callSnippet" and "callKeywords" added to "yii\sphinx\Command" --- extensions/sphinx/Command.php | 14 ++++++++++++ extensions/sphinx/Connection.php | 3 +++ extensions/sphinx/QueryBuilder.php | 32 ++++++++++++++++++++++++++++ tests/unit/extensions/sphinx/CommandTest.php | 28 ++++++++++++++++++++++++ 4 files changed, 77 insertions(+) diff --git a/extensions/sphinx/Command.php b/extensions/sphinx/Command.php index 2a0d1e3..a31f79a 100644 --- a/extensions/sphinx/Command.php +++ b/extensions/sphinx/Command.php @@ -495,4 +495,18 @@ class Command extends Component $sql = $this->db->getQueryBuilder()->truncateIndex($index); return $this->setSql($sql); } + + public function callSnippets($index, $source, $query, array $options = []) + { + $params = []; + $sql = $this->db->getQueryBuilder()->callSnippets($index, $source, $query, $options, $params); + return $this->setSql($sql)->bindValues($params); + } + + public function callKeywords($index, $text, $fetchStatistic = false) + { + $params = []; + $sql = $this->db->getQueryBuilder()->callKeywords($index, $text, $fetchStatistic, $params); + return $this->setSql($sql)->bindValues($params); + } } \ No newline at end of file diff --git a/extensions/sphinx/Connection.php b/extensions/sphinx/Connection.php index 37a4b59..bb498d4 100644 --- a/extensions/sphinx/Connection.php +++ b/extensions/sphinx/Connection.php @@ -11,7 +11,10 @@ namespace yii\sphinx; * Class Connection * * @property Schema $schema The schema information for this Sphinx connection. This property is read-only. + * @property QueryBuilder $queryBuilder The query builder for this Sphinx connection. This property is + * read-only. * @method Schema getSchema() The schema information for this Sphinx connection + * @method QueryBuilder getQueryBuilder() he query builder for this Sphinx connection * * @author Paul Klimov * @since 2.0 diff --git a/extensions/sphinx/QueryBuilder.php b/extensions/sphinx/QueryBuilder.php index a7bd6be..f463a0e 100644 --- a/extensions/sphinx/QueryBuilder.php +++ b/extensions/sphinx/QueryBuilder.php @@ -278,6 +278,38 @@ class QueryBuilder extends Object return 'TRUNCATE RTINDEX ' . $this->db->quoteIndexName($index); } + public function callSnippets($index, $source, $query, $options, &$params) + { + if (is_array($source)) { + $dataSqlParts = []; + foreach ($source as $sourceRow) { + $phName = self::PARAM_PREFIX . count($params); + $params[$phName] = $sourceRow; + $dataSqlParts[] = $phName; + } + $dataSql = '(' . implode(',', $dataSqlParts) . ')'; + } else { + $phName = self::PARAM_PREFIX . count($params); + $params[$phName] = $source; + $dataSql = $phName; + } + $indexParamName = self::PARAM_PREFIX . count($params); + $params[$indexParamName] = $index; + $queryParamName = self::PARAM_PREFIX . count($params); + $params[$queryParamName] = $query; + $optionSql = ''; // @todo + return 'CALL SNIPPETS(' . $dataSql. ', ' . $indexParamName . ', ' . $queryParamName . $optionSql. ')'; + } + + public function callKeywords($index, $text, $fetchStatistic, &$params) + { + $indexParamName = self::PARAM_PREFIX . count($params); + $params[$indexParamName] = $index; + $textParamName = self::PARAM_PREFIX . count($params); + $params[$textParamName] = $text; + return 'CALL KEYWORDS(' . $textParamName . ', ' . $indexParamName . ($fetchStatistic ? ', 1' : '') . ')'; + } + /** * @param array $columns * @param boolean $distinct diff --git a/tests/unit/extensions/sphinx/CommandTest.php b/tests/unit/extensions/sphinx/CommandTest.php index ebca709..9183b6c 100644 --- a/tests/unit/extensions/sphinx/CommandTest.php +++ b/tests/unit/extensions/sphinx/CommandTest.php @@ -252,4 +252,32 @@ class CommandTest extends SphinxTestCase $rows = $db->createCommand('SELECT * FROM yii2_test_rt_index')->queryAll(); $this->assertEquals(0, count($rows), 'Unable to delete record!'); } + + public function testCallSnippets() + { + $db = $this->getConnection(); + + $query = 'pencil'; + $data = ['Some data sentence about ' . $query]; + $rows = $db->createCommand()->callSnippets('yii2_test_item_index', $data, $query)->queryColumn(); + $this->assertNotEmpty($rows, 'Unable to call snippets!'); + $this->assertContains('' . $query . '', $rows[0], 'Query not present in the snippet!'); + } + + public function testCallKeywords() + { + $db = $this->getConnection(); + + $text = 'table pencil'; + $rows = $db->createCommand()->callKeywords('yii2_test_item_index', $text)->queryAll(); + $this->assertNotEmpty($rows, 'Unable to call keywords!'); + $this->assertArrayHasKey('tokenized', $rows[0], 'No tokenized keyword!'); + $this->assertArrayHasKey('normalized', $rows[0], 'No normalized keyword!'); + + $text = 'table pencil'; + $rows = $db->createCommand()->callKeywords('yii2_test_item_index', $text, true)->queryAll(); + $this->assertNotEmpty($rows, 'Unable to call keywords with statistic!'); + $this->assertArrayHasKey('docs', $rows[0], 'No docs!'); + $this->assertArrayHasKey('hits', $rows[0], 'No hits!'); + } } \ No newline at end of file