What are your thoughts on this?
It will now support queries like:
SELECT Cols FROM Table WHERE Col1='Foo' AND Col2='Bar'
or
SELECT Cols FROM Table WHERE Col1='Foo' AND Col2 LIKE '%Bar%'
or
SELECT Cols FROM Table WHERE Col1 LIKE '%Foo' AND Col2 LIKE 'Bar%'
Does the code look ok or messy? Hmm this should probably be in the perl forum now...
if ($where) {
ref $where eq 'HASH' or $self->error ($ERRORS->{NOTHASHREF}, $where);
$query .= ' WHERE ';
if ($like) {
ref $like eq 'HASH' or $self->error ($ERRORS->{NOTHASHREF}, $like);
for (@{$like->{LIKE}}) {
$query .= ' ' . $_ . ' LIKE ' . $self->{dbh}->quote($where->{$_}) . ' ' . $bool . ' ';
delete $where->{$_};
}
$query .= join ($bool . ' ' , map { $_ . '=' . $self->{dbh}->quote($where->{$_}) } keys %$where);
}
else {
$query .= join ($bool . ' ', map { $_ . '=' . $self->{dbh}->quote($where->{$_}) } keys %$where);
}
$query =~ s,\Q$bool\E\s*$,,o;
}
Something like:
$DB->select( ['ID','Name'], 'Forums', { SubCat => 'Hello', Foo => '%bar%' }, 'AND', { LIKE => ['Foo'] } );
...would create.......
SELECT ID,Name FROM Forums WHERE SubCat='Hello' AND Foo LIKE '%bar%'
It will now support queries like:
SELECT Cols FROM Table WHERE Col1='Foo' AND Col2='Bar'
or
SELECT Cols FROM Table WHERE Col1='Foo' AND Col2 LIKE '%Bar%'
or
SELECT Cols FROM Table WHERE Col1 LIKE '%Foo' AND Col2 LIKE 'Bar%'
Does the code look ok or messy? Hmm this should probably be in the perl forum now...
Code:
# Append the WHERE clause if it exists as a hashref. if ($where) {
ref $where eq 'HASH' or $self->error ($ERRORS->{NOTHASHREF}, $where);
$query .= ' WHERE ';
if ($like) {
ref $like eq 'HASH' or $self->error ($ERRORS->{NOTHASHREF}, $like);
for (@{$like->{LIKE}}) {
$query .= ' ' . $_ . ' LIKE ' . $self->{dbh}->quote($where->{$_}) . ' ' . $bool . ' ';
delete $where->{$_};
}
$query .= join ($bool . ' ' , map { $_ . '=' . $self->{dbh}->quote($where->{$_}) } keys %$where);
}
else {
$query .= join ($bool . ' ', map { $_ . '=' . $self->{dbh}->quote($where->{$_}) } keys %$where);
}
$query =~ s,\Q$bool\E\s*$,,o;
}
Something like:
$DB->select( ['ID','Name'], 'Forums', { SubCat => 'Hello', Foo => '%bar%' }, 'AND', { LIKE => ['Foo'] } );
...would create.......
SELECT ID,Name FROM Forums WHERE SubCat='Hello' AND Foo LIKE '%bar%'