Commit 0c1b77e1 authored by mkanat%kerio.com's avatar mkanat%kerio.com

Bug 284598: INSTR function is not supported by postgres

Patch By Tomas Kopal <Tomas.Kopal@altap.cz> r=mkanat, a=justdave
parent 8b471846
...@@ -201,6 +201,12 @@ sub import { ...@@ -201,6 +201,12 @@ sub import {
$Exporter::ExportLevel-- if $is_exporter; $Exporter::ExportLevel-- if $is_exporter;
} }
sub sql_position {
my ($self, $fragment, $text) = @_;
return "POSITION($fragment IN $text)";
}
# XXX - Needs to be documented. # XXX - Needs to be documented.
sub bz_server_version { sub bz_server_version {
my ($self) = @_; my ($self) = @_;
...@@ -674,6 +680,16 @@ formatted SQL command have prefix C<sql_>. All other methods have prefix C<bz_>. ...@@ -674,6 +680,16 @@ formatted SQL command have prefix C<sql_>. All other methods have prefix C<bz_>.
(scalar) (scalar)
Returns: formatted SQL for interval function (scalar) Returns: formatted SQL for interval function (scalar)
=item C<sql_position>
Description: Outputs proper SQL syntax determinig position of a substring
(fragment) withing a string (text). Note: if the substring or
text are string constants, they must be properly quoted
(e.g. "'pattern'").
Params: $fragment = the string fragment we are searching for (scalar)
$text = the text to search (scalar)
Returns: formatted SQL for substring search (scalar)
=item C<bz_lock_tables> =item C<bz_lock_tables>
Description: Performs a table lock operation on specified tables. Description: Performs a table lock operation on specified tables.
......
...@@ -117,6 +117,20 @@ sub sql_interval { ...@@ -117,6 +117,20 @@ sub sql_interval {
return "INTERVAL $interval"; return "INTERVAL $interval";
} }
sub sql_position {
my ($self, $fragment, $text) = @_;
# mysql 4.0.1 and lower do not support CAST
# mysql 3.*.* had a case-sensitive INSTR
# (checksetup has a check for unsupported versions)
my $server_version = $self->bz_server_version;
if ($server_version =~ /^3\./) {
return "INSTR($text, $fragment)";
} else {
return "INSTR(CAST($text AS BINARY), CAST($fragment AS BINARY))";
}
}
sub bz_lock_tables { sub bz_lock_tables {
my ($self, @tables) = @_; my ($self, @tables) = @_;
......
...@@ -951,24 +951,16 @@ sub init { ...@@ -951,24 +951,16 @@ sub init {
$term = "$ff != $q"; $term = "$ff != $q";
}, },
",casesubstring" => sub { ",casesubstring" => sub {
# mysql 4.0.1 and lower do not support CAST $term = $dbh->sql_position($q, $ff);
# mysql 3.*.* had a case-sensitive INSTR
# (checksetup has a check for unsupported versions)
my $server_version = $dbh->bz_server_version;
if ($dbh->isa('Bugzilla::DB::Mysql') && $server_version =~ /^3\./) {
$term = "INSTR($ff ,$q)";
} else {
$term = "INSTR(CAST($ff AS BINARY), CAST($q AS BINARY))";
}
}, },
",substring" => sub { ",substring" => sub {
$term = "INSTR(LOWER($ff), " . lc($q) . ")"; $term = $dbh->sql_position(lc($q), "LOWER($ff)");
}, },
",substr" => sub { ",substr" => sub {
$funcsbykey{",substring"}->(); $funcsbykey{",substring"}->();
}, },
",notsubstring" => sub { ",notsubstring" => sub {
$term = "INSTR(LOWER($ff), " . lc($q) . ") = 0"; $term = $dbh->sql_position(lc($q), "LOWER($ff)") . " = 0";
}, },
",regexp" => sub { ",regexp" => sub {
$term = "LOWER($ff) " . $dbh->sql_regexp() . " $q"; $term = "LOWER($ff) " . $dbh->sql_regexp() . " $q";
...@@ -1426,8 +1418,9 @@ sub ListIDsForEmail { ...@@ -1426,8 +1418,9 @@ sub ListIDsForEmail {
} }
$list = join(',', @list); $list = join(',', @list);
} elsif ($type eq 'substring') { } elsif ($type eq 'substring') {
&::SendSQL("SELECT userid FROM profiles WHERE INSTR(login_name, " . &::SendSQL("SELECT userid FROM profiles WHERE " .
&::SqlQuote($email) . ") " . $dbh->sql_limit(51)); $dbh->sql_position(lc(::SqlQuote($email)), "LOWER(login_name)") .
" " . $dbh->sql_limit(51));
while (&::MoreSQLData()) { while (&::MoreSQLData()) {
my ($id) = &::FetchSQLData(); my ($id) = &::FetchSQLData();
push(@list, $id); push(@list, $id);
...@@ -1478,10 +1471,12 @@ sub GetByWordList { ...@@ -1478,10 +1471,12 @@ sub GetByWordList {
sub GetByWordListSubstr { sub GetByWordListSubstr {
my ($field, $strs) = (@_); my ($field, $strs) = (@_);
my @list; my @list;
my $dbh = Bugzilla->dbh;
foreach my $word (split(/[\s,]+/, $strs)) { foreach my $word (split(/[\s,]+/, $strs)) {
if ($word ne "") { if ($word ne "") {
push(@list, "INSTR(LOWER($field), " . lc(&::SqlQuote($word)) . ")"); push(@list, $dbh->sql_position(lc(::SqlQuote($word)),
"LOWER($field)"));
} }
} }
......
...@@ -618,23 +618,25 @@ sub match { ...@@ -618,23 +618,25 @@ sub match {
my $sqlstr = &::SqlQuote(uc($str)); my $sqlstr = &::SqlQuote(uc($str));
my $query = "SELECT DISTINCT userid, realname, login_name " . my $query = "SELECT DISTINCT userid, realname, login_name " .
"FROM profiles "; "FROM profiles";
if (&::Param('usevisibilitygroups')) { if (&::Param('usevisibilitygroups')) {
$query .= ", user_group_map "; $query .= ", user_group_map";
} }
$query .= "WHERE (INSTR(UPPER(login_name), $sqlstr) " . $query .= " WHERE " . $dbh->sql_position($sqlstr,
"OR INSTR(UPPER(realname), $sqlstr)) "; "UPPER(login_name)") .
" OR " . $dbh->sql_position($sqlstr,
"UPPER(realname)");
if (&::Param('usevisibilitygroups')) { if (&::Param('usevisibilitygroups')) {
$query .= "AND user_group_map.user_id = userid " . $query .= " AND user_group_map.user_id = userid" .
"AND isbless = 0 " . " AND isbless = 0" .
"AND group_id IN(" . " AND group_id IN(" .
join(', ', (-1, @{$user->visible_groups_inherited})) . ") " . join(', ', (-1, @{$user->visible_groups_inherited})) . ")" .
"AND grant_type <> " . GRANT_DERIVED; " AND grant_type <> " . GRANT_DERIVED;
} }
$query .= " AND disabledtext = '' " if $exclude_disabled; $query .= " AND disabledtext = ''" if $exclude_disabled;
$query .= "ORDER BY length(login_name) "; $query .= " ORDER BY length(login_name)";
$query .= $dbh->sql_limit($limit) if $limit; $query .= " " . $dbh->sql_limit($limit) if $limit;
&::PushGlobalSQLState(); &::PushGlobalSQLState();
&::SendSQL($query); &::SendSQL($query);
push(@users, new Bugzilla::User(&::FetchSQLData())) while &::MoreSQLData(); push(@users, new Bugzilla::User(&::FetchSQLData())) while &::MoreSQLData();
......
...@@ -4175,8 +4175,8 @@ if (!$dbh->bz_get_field_def('longdescs', 'already_wrapped')) { ...@@ -4175,8 +4175,8 @@ if (!$dbh->bz_get_field_def('longdescs', 'already_wrapped')) {
# at display-time. # at display-time.
print "Fixing old, mis-wrapped comments...\n"; print "Fixing old, mis-wrapped comments...\n";
$dbh->do(q{UPDATE longdescs SET already_wrapped = 0 $dbh->do(q{UPDATE longdescs SET already_wrapped = 0
WHERE ( POSITION('\n' IN thetext ) > 80 WHERE (} . $dbh->sql_position(q{'\n'}, 'thetext') . q{ > 80
OR POSITION('\n' IN thetext ) = 0 ) OR } . $dbh->sql_position(q{'\n'}, 'thetext') . q{ = 0)
AND SUBSTRING(thetext FROM 1 FOR 80) LIKE '% %'}); AND SUBSTRING(thetext FROM 1 FOR 80) LIKE '% %'});
} }
...@@ -4547,8 +4547,8 @@ if (!defined $dbh->bz_get_index_def('longdescs', 'thetext')) { ...@@ -4547,8 +4547,8 @@ if (!defined $dbh->bz_get_index_def('longdescs', 'thetext')) {
print "Removing paths from filenames in attachments table...\n"; print "Removing paths from filenames in attachments table...\n";
$sth = $dbh->prepare("SELECT attach_id, filename FROM attachments " . $sth = $dbh->prepare("SELECT attach_id, filename FROM attachments " .
"WHERE INSTR(filename, '/') " . "WHERE " . $dbh->sql_position(q{'/'}, 'filename') .
"OR INSTR(filename, '\\\\')"); " OR " . $dbh->sql_position(q{'\\\\'}, 'filename'));
$sth->execute; $sth->execute;
while (my ($attach_id, $filename) = $sth->fetchrow_array) { while (my ($attach_id, $filename) = $sth->fetchrow_array) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment