Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
6b5b9976
Commit
6b5b9976
authored
Nov 13, 2013
by
Paul Klimov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Options support for "yii\sphinx\Command::callSnippet()" added.
parent
cc156ba8
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
71 additions
and
6 deletions
+71
-6
Command.php
extensions/sphinx/Command.php
+17
-1
QueryBuilder.php
extensions/sphinx/QueryBuilder.php
+34
-3
CommandTest.php
tests/unit/extensions/sphinx/CommandTest.php
+20
-2
No files found.
extensions/sphinx/Command.php
View file @
6b5b9976
...
...
@@ -496,13 +496,29 @@ class Command extends Component
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
=
[];
$sql
=
$this
->
db
->
getQueryBuilder
()
->
callSnippets
(
$index
,
$source
,
$query
,
$options
,
$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
)
{
$params
=
[];
...
...
extensions/sphinx/QueryBuilder.php
View file @
6b5b9976
...
...
@@ -269,15 +269,26 @@ class QueryBuilder extends Object
}
/**
* Builds a SQL statement for truncating a
DB
index.
* Builds a SQL statement for truncating a
n
index.
* @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 a
n
index.
*/
public
function
truncateIndex
(
$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
)
{
if
(
is_array
(
$source
))
{
...
...
@@ -297,10 +308,30 @@ class QueryBuilder extends Object
$params
[
$indexParamName
]
=
$index
;
$queryParamName
=
self
::
PARAM_PREFIX
.
count
(
$params
);
$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
.
')'
;
}
/**
* 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
)
{
$indexParamName
=
self
::
PARAM_PREFIX
.
count
(
$params
);
...
...
tests/unit/extensions/sphinx/CommandTest.php
View file @
6b5b9976
...
...
@@ -253,17 +253,35 @@ class CommandTest extends SphinxTestCase
$this
->
assertEquals
(
0
,
count
(
$rows
),
'Unable to delete record!'
);
}
/**
* @depends testQuery
*/
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
();
$source
=
'Some data sentence about '
.
$query
;
$rows
=
$db
->
createCommand
()
->
callSnippets
(
'yii2_test_item_index'
,
$source
,
$query
)
->
queryColumn
();
$this
->
assertNotEmpty
(
$rows
,
'Unable to call snippets!'
);
$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
()
{
$db
=
$this
->
getConnection
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment