Browse Source

Methods "callSnippet" and "callKeywords" added to "yii\sphinx\Command"

tags/2.0.0-beta
Paul Klimov 11 years ago
parent
commit
cc156ba812
  1. 14
      extensions/sphinx/Command.php
  2. 3
      extensions/sphinx/Connection.php
  3. 32
      extensions/sphinx/QueryBuilder.php
  4. 28
      tests/unit/extensions/sphinx/CommandTest.php

14
extensions/sphinx/Command.php

@ -495,4 +495,18 @@ class Command extends Component
$sql = $this->db->getQueryBuilder()->truncateIndex($index); $sql = $this->db->getQueryBuilder()->truncateIndex($index);
return $this->setSql($sql); 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);
}
} }

3
extensions/sphinx/Connection.php

@ -11,7 +11,10 @@ namespace yii\sphinx;
* Class Connection * Class Connection
* *
* @property Schema $schema The schema information for this Sphinx connection. This property is read-only. * @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 Schema getSchema() The schema information for this Sphinx connection
* @method QueryBuilder getQueryBuilder() he query builder for this Sphinx connection
* *
* @author Paul Klimov <klimov.paul@gmail.com> * @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0 * @since 2.0

32
extensions/sphinx/QueryBuilder.php

@ -278,6 +278,38 @@ class QueryBuilder extends Object
return 'TRUNCATE RTINDEX ' . $this->db->quoteIndexName($index); 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 array $columns
* @param boolean $distinct * @param boolean $distinct

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

@ -252,4 +252,32 @@ class CommandTest extends SphinxTestCase
$rows = $db->createCommand('SELECT * FROM yii2_test_rt_index')->queryAll(); $rows = $db->createCommand('SELECT * FROM yii2_test_rt_index')->queryAll();
$this->assertEquals(0, count($rows), 'Unable to delete record!'); $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('<b>' . $query . '</b>', $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!');
}
} }
Loading…
Cancel
Save