Commit cc156ba8 by Paul Klimov

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

parent 07ad008d
......@@ -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
......@@ -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 <klimov.paul@gmail.com>
* @since 2.0
......
......@@ -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
......
......@@ -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('<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!');
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment