diff --git a/lib/Data/ObjectDriver/Driver/DBI.pm b/lib/Data/ObjectDriver/Driver/DBI.pm index 148db0d..d51cc8e 100644 --- a/lib/Data/ObjectDriver/Driver/DBI.pm +++ b/lib/Data/ObjectDriver/Driver/DBI.pm @@ -184,7 +184,7 @@ sub fetch { my @bind; my $map = $stmt->select_map; for my $col (@{ $stmt->select }) { - push @bind, \$rec->{ $map->{$col} }; + push @bind, \$rec->{ $map->{$col} || $col }; } my $dbh = $driver->r_handle($class->properties->{db}); diff --git a/lib/Data/ObjectDriver/SQL.pm b/lib/Data/ObjectDriver/SQL.pm index 8c43856..86cf4f1 100644 --- a/lib/Data/ObjectDriver/SQL.pm +++ b/lib/Data/ObjectDriver/SQL.pm @@ -33,10 +33,11 @@ sub new { sub add_select { my $stmt = shift; my($term, $col) = @_; - $col ||= $term; push @{ $stmt->select }, $term; - $stmt->select_map->{$term} = $col; - $stmt->select_map_reverse->{$col} = $term; + if ($col) { + $stmt->select_map->{$term} = $col; + $stmt->select_map_reverse->{$col} = $term; + } } sub add_join { @@ -64,8 +65,12 @@ sub as_sql { $sql .= 'SELECT '; $sql .= 'DISTINCT ' if $stmt->distinct; $sql .= join(', ', map { - my $alias = $stmt->select_map->{$_}; - $alias && /(?:^|\.)\Q$alias\E$/ ? $_ : "$_ $alias"; + my $col = $_; + if (my $alias = $stmt->select_map->{$col}) { + /(?:^|\.)\Q$alias\E$/ ? $col : "$col $alias"; + } else { + $col; + } } @{ $stmt->select }) . "\n"; } $sql .= 'FROM '; diff --git a/t/11-sql.t b/t/11-sql.t index 56a21ad..93510bc 100644 --- a/t/11-sql.t +++ b/t/11-sql.t @@ -3,7 +3,7 @@ use strict; use Data::ObjectDriver::SQL; -use Test::More tests => 113; +use Test::More tests => 110; my $stmt = ns(); ok($stmt, 'Created SQL object'); @@ -284,15 +284,37 @@ $stmt->add_select('bar'); $stmt->from([ qw( baz ) ]); is($stmt->as_sql, "SELECT foo, bar\nFROM baz\n"); -$stmt = ns(); -$stmt->add_select('f.foo' => 'foo'); -$stmt->add_select('COUNT(*)' => 'count'); -$stmt->from([ qw( baz ) ]); -is($stmt->as_sql, "SELECT f.foo, COUNT(*) count\nFROM baz\n"); -my $map = $stmt->select_map; -is(scalar(keys %$map), 2); -is($map->{'f.foo'}, 'foo'); -is($map->{'COUNT(*)'}, 'count'); +subtest 'SQL functions' => sub { + $stmt = ns(); + $stmt->add_select('f.foo' => 'foo'); + $stmt->add_select('f.bar'); + $stmt->add_select('COUNT(*)' => 'count'); + $stmt->from([ qw( baz ) ]); + is($stmt->as_sql, "SELECT f.foo, f.bar, COUNT(*) count\nFROM baz\n"); + my $map = $stmt->select_map; + is(scalar(keys %$map), 2); + is($map->{'f.foo'}, 'foo'); + is($map->{'COUNT(*)'}, 'count'); + + $stmt = ns(); + $stmt->add_select('count(foo)'); + $stmt->add_select('count(bar)'); + $stmt->add_select('count(baz)', 'bazcount'); + $stmt->from([qw( baz )]); + is($stmt->as_sql, "SELECT count(foo), count(bar), count(baz) bazcount\nFROM baz\n"); + my $map = $stmt->select_map; + is(scalar(keys %$map), 1); + is_deeply($map, {'count(baz)' => 'bazcount'}, 'right map'); + + $stmt = ns(); + $stmt->add_select('count(foo)', 'count1'); + $stmt->add_select('count(bar)', 'count2'); + $stmt->from([qw( baz )]); + is($stmt->as_sql, "SELECT count(foo) count1, count(bar) count2\nFROM baz\n"); + my $map = $stmt->select_map; + is(scalar(keys %$map), 2); + is_deeply($map, {'count(foo)' => 'count1', 'count(bar)' => 'count2'}, 'right map'); +}; # HAVING $stmt = ns();