Skip to content

Commit

Permalink
workaround for queries like
Browse files Browse the repository at this point in the history
"select distinct category from products order by category"
  • Loading branch information
racke committed Dec 14, 2004
1 parent 174d219 commit 410a616
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions lib/Vend/Table/Shadow.pm
@@ -1,6 +1,6 @@
# Vend::Table::Shadow - Access a virtual "Shadow" table
#
# $Id: Shadow.pm,v 1.46 2004-07-23 03:03:39 mheins Exp $
# $Id: Shadow.pm,v 1.47 2004-12-14 23:03:47 racke Exp $
#
# Copyright (C) 2002-2003 Stefan Hornburg (Racke) <racke@linuxia.de>
#
Expand All @@ -20,7 +20,7 @@
# MA 02111-1307 USA.

package Vend::Table::Shadow;
$VERSION = substr(q$Revision: 1.46 $, 10);
$VERSION = substr(q$Revision: 1.47 $, 10);

# CREDITS
#
Expand Down Expand Up @@ -333,6 +333,19 @@ sub query {
unless (@map_matches = $s->_map_entries($colref, \@map_entries)) {
return $s->[$OBJ]->query($opt, $text, @arg);
}

if (@$colref == 1 && $qref->{distinct}->[0]) {
# workaround for queries like
# "select distinct category from products order by category"
my ($dt, $dc) = $s->_map_field($colref->[0]);

$opt->{query} = "select distinct $dc from " . $dt->config('name');
if (@{$qref->{order}}) {
$opt->{query} .= " ORDER BY " . join(',', @{$qref->{order}});
}

return $s->[$OBJ]->query($opt, $text, @arg);
}

# scan columns for key field
my $keyname = $s->[$OBJ]->config('KEY');
Expand Down Expand Up @@ -466,13 +479,18 @@ sub _parse_sql {
}

$sqlinfo{command} = $stmt->command();

for ($stmt->tables()) {
push (@{$sqlinfo{tables}}, $_->name());
}
for ($stmt->columns()) {
push (@{$sqlinfo{columns}}, $_->name());
push (@{$sqlinfo{distinct}}, $_->distinct());
}

for ($stmt->order()) {
push (@{$sqlinfo{order}}, $_->column() . ($_->desc ? ' DESC' : ''));
}

\%sqlinfo;
}

Expand Down

0 comments on commit 410a616

Please sign in to comment.