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
60312771
Commit
60312771
authored
Jun 05, 2014
by
Klimov Paul
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Escaping of the special characters at 'MATCH' statement reworked
parent
38a6b4fa
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
25 additions
and
12 deletions
+25
-12
Connection.php
extensions/sphinx/Connection.php
+4
-3
Query.php
extensions/sphinx/Query.php
+10
-2
QueryBuilder.php
extensions/sphinx/QueryBuilder.php
+3
-1
QueryTest.php
tests/unit/extensions/sphinx/QueryTest.php
+8
-6
No files found.
extensions/sphinx/Connection.php
View file @
60312771
...
...
@@ -132,15 +132,16 @@ class Connection extends \yii\db\Connection
/**
* Escapes all special characters from 'MATCH' statement argument.
* Make sure you are using this method whenever composing 'MATCH' search statement.
* Note: this method does not perform quoting, you should place the result in the quotes manually.
* Note: this method does not perform quoting, you should place the result in the quotes
* an perform additional escaping for it manually, the best way to do it is using PDO parameter.
* @param string $str string to be escaped.
* @return string the properly escaped string.
*/
public
function
escapeMatchValue
(
$str
)
{
return
str_replace
(
[
'\\'
,
"'"
,
'/'
,
'"'
,
'('
,
')'
,
'|'
,
'-'
,
'!'
,
'@'
,
'~'
,
'&'
,
'^'
,
'$'
,
'=
'
,
"
\x00
"
,
"
\n
"
,
"
\r
"
,
"
\x1a
"
],
[
'\\\\'
,
"
\\
'"
,
'\\\\/'
,
'\\\\"'
,
'\\\\('
,
'\\\\)'
,
'\\\\|'
,
'\\\\-'
,
'\\\\!'
,
'\\\\@'
,
'\\\\~'
,
'\\\\&'
,
'\\\\^'
,
'\\\\$'
,
'\\\\='
,
"
\\
x00"
,
"
\\
n"
,
"
\\
r"
,
"
\\
x1a"
],
[
'\\'
,
'/'
,
'"'
,
'('
,
')'
,
'|'
,
'-'
,
'!'
,
'@'
,
'~'
,
'&'
,
'^'
,
'$'
,
'='
,
'>'
,
'<
'
,
"
\x00
"
,
"
\n
"
,
"
\r
"
,
"
\x1a
"
],
[
'\\\\'
,
'\\/'
,
'\\"'
,
'\\('
,
'\\)'
,
'\\|'
,
'\\-'
,
'\\!'
,
'\\@'
,
'\\~'
,
'\\&'
,
'\\^'
,
'\\$'
,
'\\='
,
'\\>'
,
'\\<'
,
"
\\
x00"
,
"
\\
n"
,
"
\\
r"
,
"
\\
x1a"
],
$str
);
}
...
...
extensions/sphinx/Query.php
View file @
60312771
...
...
@@ -75,7 +75,8 @@ class Query extends Component implements QueryInterface
* @var string|Expression text, which should be searched in fulltext mode.
* This value will be composed into MATCH operator inside the WHERE clause.
* Note: this value will be processed by [[Connection::escapeMatchValue()]],
* if you need to compose complex match condition use [[Expression]].
* if you need to compose complex match condition use [[Expression]],
* see [[match()]] for details.
*/
public
$match
;
/**
...
...
@@ -384,7 +385,14 @@ class Query extends Component implements QueryInterface
* Sets the fulltext query text. This text will be composed into
* MATCH operator inside the WHERE clause.
* Note: this value will be processed by [[Connection::escapeMatchValue()]],
* if you need to compose complex match condition use [[Expression]].
* if you need to compose complex match condition use [[Expression]]:
* ```
* $query = new Query;
* $query->from('my_index')
* ->match(new Expression(':match', ['match' => '@(content) ' . Yii::$app->sphinx->escapeMatchValue($matchValue)]))
* ->all();
* ```
*
* @param string $query fulltext query text.
* @return static the query object itself
*/
...
...
extensions/sphinx/QueryBuilder.php
View file @
60312771
...
...
@@ -67,7 +67,9 @@ class QueryBuilder extends Object
$query
->
andWhere
(
'MATCH('
.
$query
->
match
->
expression
.
')'
);
$params
=
array_merge
(
$params
,
$query
->
match
->
params
);
}
else
{
$query
->
andWhere
(
"MATCH('"
.
$this
->
db
->
escapeMatchValue
(
$query
->
match
)
.
"')"
);
$phName
=
self
::
PARAM_PREFIX
.
count
(
$params
);
$params
[
$phName
]
=
$this
->
db
->
escapeMatchValue
(
$query
->
match
);
$query
->
andWhere
(
'MATCH('
.
$phName
.
')'
);
}
}
...
...
tests/unit/extensions/sphinx/QueryTest.php
View file @
60312771
...
...
@@ -42,7 +42,7 @@ class QueryTest extends SphinxTestCase
$command
=
$query
->
createCommand
(
$this
->
getConnection
(
false
));
$this
->
assertContains
(
'MATCH('
,
$command
->
getSql
(),
'No MATCH operator present!'
);
$this
->
assertContains
(
$match
,
$command
->
getSql
(),
'No match query in SQL
!'
);
$this
->
assertContains
(
$match
,
$command
->
params
,
'No match query among params
!'
);
}
public
function
testWhere
()
...
...
@@ -258,14 +258,13 @@ class QueryTest extends SphinxTestCase
/**
* @depends testRun
*/
public
function
testWhere
Quoted
Value
()
public
function
testWhere
SpecialChar
Value
()
{
$connection
=
$this
->
getConnection
();
$query
=
new
Query
;
$rows
=
$query
->
from
(
'yii2_test_article_index'
)
->
andWhere
([
'author_id'
=>
'some"'
])
//->match('about"')
->
all
(
$connection
);
$this
->
assertEmpty
(
$rows
);
}
...
...
@@ -282,11 +281,14 @@ class QueryTest extends SphinxTestCase
[
'@'
],
[
'\\'
],
[
'()'
],
[
'\\'
.
"'"
],
[
'<<<'
],
[
'>>>'
],
[
"
\x00
"
],
[
"
\n
"
],
[
"
\r
"
],
[
"
\x1a
"
]
[
"
\x1a
"
],
[
'\\'
.
"'"
],
[
'\\'
.
'"'
],
];
}
...
...
@@ -318,7 +320,7 @@ class QueryTest extends SphinxTestCase
$query
=
new
Query
;
$rows
=
$query
->
from
(
'yii2_test_article_index'
)
->
match
(
new
Expression
(
"'@(content) "
.
$connection
->
escapeMatchValue
(
'about"'
)
.
"'"
))
->
match
(
new
Expression
(
':match'
,
[
'match'
=>
'@(content) '
.
$connection
->
escapeMatchValue
(
'about\\"'
)]
))
->
all
(
$connection
);
$this
->
assertNotEmpty
(
$rows
);
}
...
...
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