Skip to content

Commit

Permalink
Item13122: set connection to UTF-8
Browse files Browse the repository at this point in the history
convert perl word-boundaries to the mysql equivalent
remove lazy-repetion-flag
remove non-capturing-group-flag
disable double-escape-sign-regex

overwrite safe_id
  • Loading branch information
prmdhost authored and gac410 committed Nov 28, 2014
1 parent 9ebb0c5 commit 795c765
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions lib/Foswiki/Contrib/DBIStoreContrib/Personality/mysql.pm
Expand Up @@ -50,6 +50,9 @@ sub startup {
# MySQL has to be kicked in the ANSIs
$this->{dbh}->do("SET sql_mode='ANSI'");
$this->{dbh}->do('SELECT @sql_mode');

#set to UTF8
$this->{dbh}->do("SET NAMES utf8");
}

sub regexp {
Expand All @@ -58,7 +61,7 @@ sub regexp {
unless ( $rhs =~ s/^'(.*)'$/$1/s ) {

# Somewhat risky....
return "$lhs REGEXP $rhs";
return "$lhs REGEXP ($rhs)";
}

# MySQL uses POSIX regular expressions.
Expand All @@ -72,6 +75,11 @@ sub regexp {
# Nor \d, \D
$rhs =~ s/(^|(?<=[^\\]))\\d/[0-9]/g;
$rhs =~ s/(^|(?<=[^\\]))\\D/[^0-9]/g;

# Nor \b, \B
$rhs =~ s/\\\\[bB](.*?)\\\\[bB]/\[\[:<:\]\]$1\[\[:>:\]\]/g;
$rhs =~ s/\\\\[bB]($|\|)/\[\[:>:\]\]$1/g;
$rhs =~ s/(^|\|)\\\\[bB]/$1\[\[:<:\]\]/g;

# Nor \s, \S, \w, \W
$rhs =~ s/(^|(?<=[^\\]))\\s/[ \011\012\015]/g;
Expand All @@ -80,17 +88,19 @@ sub regexp {
$rhs =~ s/(^|(?<=[^\\]))\\W/[^a-zA-Z0-9_]/g;

# Convert X? to (X|)
$rhs =~ s/(?<=[^\\])(\(.*\)|\[.*?\]|\\.|.)\?/($1|)/g; # ?
#$rhs =~ s/(?<=[^\\])(\(.*\)|\[.*?\]|\\.|.)\?/($1|)/g; # ?
$rhs =~ s/([\+\*])\?/$1/g;
$rhs =~ s/\?://g;
# Handle special characters
$rhs =~ s/(?<=[^\\])\\n/\n/g; # will this work?
$rhs =~ s/(?<=[^\\])\\r/\r/g;
$rhs =~ s/(?<=[^\\])\\t/\t/g;
$rhs =~ s/(?<=[^\\])\\b//g; # not supported
$rhs =~ s/(?<=[^\\])\{\d+(,\d*)?\}//g; # not supported
# Escape '
$rhs =~ s/\\/\\\\/g;
#$rhs =~ s/\\/\\\\/g;

return "$lhs REGEXP '$rhs'";
return "$lhs REGEXP ('$rhs')";
}

sub cast_to_numeric {
Expand All @@ -107,6 +117,13 @@ sub length {
return "char_length($_[1])";
}

sub safe_id {
my ( $this, $id ) = @_;
$id =~ s/[^A-Za-z0-9_]//gs; # protect against bad data
$id = "`$id`";
return $id;
}

1;
__DATA__
Expand Down

0 comments on commit 795c765

Please sign in to comment.