-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from sixapart/introduce-as_escape
introduce as_escape
- Loading branch information
Showing
5 changed files
with
156 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# $Id$ | ||
|
||
use strict; | ||
use warnings; | ||
use lib 't/lib'; | ||
use lib 't/lib/escape'; | ||
use Test::More; | ||
use DodTestUtil; | ||
|
||
BEGIN { | ||
DodTestUtil->check_driver; | ||
} | ||
|
||
plan tests => 6; | ||
|
||
use Foo; | ||
|
||
setup_dbs({ global => ['foo'] }); | ||
|
||
my $percent = Foo->new; | ||
$percent->name('percent'); | ||
$percent->text('100%'); | ||
$percent->save; | ||
|
||
my $underscore = Foo->new; | ||
$underscore->name('underscore'); | ||
$underscore->text('100_'); | ||
$underscore->save; | ||
|
||
my $exclamation = Foo->new; | ||
$exclamation->name('exclamation'); | ||
$exclamation->text('100!'); | ||
$exclamation->save; | ||
|
||
subtest 'escape_char 1' => sub { | ||
my @got = Foo->search({ text => { op => 'LIKE', value => '100!%', escape => '!' } }); | ||
is scalar(@got), 1, 'right number'; | ||
is $got[0]->name, 'percent', 'right name'; | ||
}; | ||
|
||
subtest 'escape_char 2' => sub { | ||
my @got = Foo->search({ text => { op => 'LIKE', value => '100#_', escape => '#' } }); | ||
is scalar(@got), 1, 'right number'; | ||
is $got[0]->name, 'underscore', 'right name'; | ||
}; | ||
|
||
subtest 'self escape' => sub { | ||
my @got = Foo->search({ text => { op => 'LIKE', value => '100!!', escape => '!' } }); | ||
is scalar(@got), 1, 'right number'; | ||
is $got[0]->name, 'exclamation', 'right name'; | ||
}; | ||
|
||
subtest 'use wildcard charactor as escapr_char' => sub { | ||
plan skip_all => 'MariaDB does not support it' if Foo->driver->dbh->{Driver}->{Name} eq 'MariaDB'; | ||
my @got = Foo->search({ text => { op => 'LIKE', value => '100_%', escape => '_' } }); | ||
is scalar(@got), 1, 'right number'; | ||
is $got[0]->name, 'percent', 'right name'; | ||
}; | ||
|
||
subtest 'use of special characters' => sub { | ||
subtest 'escape_char single quote' => sub { | ||
my @got = Foo->search({ text => { op => 'LIKE', value => "100'_", escape => "''" } }); | ||
is scalar(@got), 1, 'right number'; | ||
is $got[0]->name, 'underscore', 'right name'; | ||
}; | ||
|
||
if (Foo->driver->dbh->{Driver}->{Name} =~ /mysql|mariadb/i) { | ||
subtest 'escape_char single quote' => sub { | ||
my @got = Foo->search({ text => { op => 'LIKE', value => "100'_", escape => "\\'" } }); | ||
is scalar(@got), 1, 'right number'; | ||
is $got[0]->name, 'underscore', 'right name'; | ||
}; | ||
|
||
subtest 'escape_char backslash' => sub { | ||
my @got = Foo->search({ text => { op => 'LIKE', value => '100\\_', escape => '\\\\' } }); | ||
is scalar(@got), 1, 'right number'; | ||
is $got[0]->name, 'underscore', 'right name'; | ||
}; | ||
} else { | ||
subtest 'escape_char backslash' => sub { | ||
my @got = Foo->search({ text => { op => 'LIKE', value => '100\\_', escape => '\\' } }); | ||
is scalar(@got), 1, 'right number'; | ||
is $got[0]->name, 'underscore', 'right name'; | ||
}; | ||
} | ||
}; | ||
|
||
subtest 'is safe' => sub { | ||
eval { Foo->search({ text => { op => 'LIKE', value => '_', escape => q{!');select 'vulnerable'; -- } } }); }; | ||
like $@, qr/escape_char length must be up to two characters/, 'error occurs'; | ||
}; | ||
|
||
END { | ||
disconnect_all(qw/Foo/); | ||
teardown_dbs(qw( global )); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# $Id$ | ||
|
||
package Foo; | ||
use strict; | ||
use warnings; | ||
use Data::ObjectDriver::Driver::DBI; | ||
use DodTestUtil; | ||
use base qw( Data::ObjectDriver::BaseObject ); | ||
|
||
__PACKAGE__->install_properties({ | ||
columns => ['id', 'name', 'text'], | ||
column_defs => { | ||
'id' => 'integer not null auto_increment', | ||
'name' => 'string(25)', | ||
'text' => 'text', | ||
}, | ||
datasource => 'foo', | ||
primary_key => 'id', | ||
driver => Data::ObjectDriver::Driver::DBI->new(dsn => DodTestUtil::dsn('global')), | ||
}); | ||
|
||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
CREATE TABLE foo ( | ||
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, | ||
name VARCHAR(25), | ||
text MEDIUMTEXT | ||
) |