Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
D
doctrine-dbal
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
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
Tomáš Trávníček
doctrine-dbal
Commits
1bc05367
Unverified
Commit
1bc05367
authored
Feb 05, 2018
by
Grégoire Paris
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Escape LIKE metacharacters
parent
63efa539
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
56 additions
and
0 deletions
+56
-0
AbstractPlatform.php
lib/Doctrine/DBAL/Platforms/AbstractPlatform.php
+26
-0
LikeWildcardsEscapingTest.php
...trine/Tests/DBAL/Functional/LikeWildcardsEscapingTest.php
+22
-0
AbstractPlatformTestCase.php
...octrine/Tests/DBAL/Platforms/AbstractPlatformTestCase.php
+8
-0
No files found.
lib/Doctrine/DBAL/Platforms/AbstractPlatform.php
View file @
1bc05367
...
...
@@ -42,6 +42,9 @@ use Doctrine\DBAL\Schema\TableDiff;
use
Doctrine\DBAL\TransactionIsolationLevel
;
use
Doctrine\DBAL\Types
;
use
Doctrine\DBAL\Types\Type
;
use
function
sprintf
;
use
function
strlen
;
use
function
strtr
;
/**
* Base class for all DatabasePlatforms. The DatabasePlatforms are the central
...
...
@@ -3578,4 +3581,27 @@ abstract class AbstractPlatform
{
return
"'"
;
}
/**
* Escapes metacharacters in a string intended to be used with a LIKE
* operator.
*
* @param string $inputString a literal, unquoted string
* @param string $escapeChar should be reused by the caller in the LIKE
* expression.
*/
final
public
function
escapeStringForLike
(
string
$inputString
,
string
$escapeChar
)
:
string
{
$replacePairs
=
[
$escapeChar
=>
$escapeChar
.
$escapeChar
];
foreach
(
$this
->
getLikeWildcardCharacters
()
as
$wildcardChar
)
{
$replacePairs
[
$wildcardChar
]
=
$escapeChar
.
$wildcardChar
;
}
return
strtr
(
$inputString
,
$replacePairs
);
}
protected
function
getLikeWildcardCharacters
()
:
iterable
{
return
[
'%'
,
'_'
];
}
}
tests/Doctrine/Tests/DBAL/Functional/LikeWildcardsEscapingTest.php
0 → 100644
View file @
1bc05367
<?php
namespace
Doctrine\Tests\DBAL\Functional
;
use
Doctrine\Tests\DbalFunctionalTestCase
;
final
class
LikeWildcardsEscapingTest
extends
DbalFunctionalTestCase
{
public
function
testFetchLikeExpressionResult
()
:
void
{
$string
=
'_25% off_ your next purchase \o/'
;
$escapeChar
=
'£'
;
$stmt
=
$this
->
_conn
->
prepare
(
sprintf
(
"SELECT '%s' LIKE '%s' ESCAPE '%s' as it_matches"
,
$string
,
$this
->
_conn
->
getDatabasePlatform
()
->
escapeStringForLike
(
$string
,
$escapeChar
),
$escapeChar
));
$stmt
->
execute
();
$this
->
assertTrue
((
bool
)
$stmt
->
fetch
()[
'it_matches'
]);
}
}
tests/Doctrine/Tests/DBAL/Platforms/AbstractPlatformTestCase.php
View file @
1bc05367
...
...
@@ -1468,4 +1468,12 @@ abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase
array
(
array
(
'precision'
=>
8
,
'scale'
=>
2
),
'DOUBLE PRECISION'
),
);
}
public
function
testItEscapesStringsForLike
()
:
void
{
self
::
assertSame
(
'\_25\% off\_ your next purchase \\\\o/'
,
$this
->
_platform
->
escapeStringForLike
(
'_25% off_ your next purchase \o/'
,
'\\'
)
);
}
}
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