From f59d139935f7a4790703a83528c7c642337d7b38 Mon Sep 17 00:00:00 2001 From: Keita Jamadam Sugama Date: Fri, 19 Sep 2025 11:53:37 +0900 Subject: [PATCH 1/3] revert uuv fix for ommitted alias --- lib/Data/ObjectDriver/Driver/DBI.pm | 2 +- t/11-sql-with-models.t | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Data/ObjectDriver/Driver/DBI.pm b/lib/Data/ObjectDriver/Driver/DBI.pm index d51cc8e..148db0d 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} || $col }; + push @bind, \$rec->{ $map->{$col} }; } my $dbh = $driver->r_handle($class->properties->{db}); diff --git a/t/11-sql-with-models.t b/t/11-sql-with-models.t index c99df13..1c935e4 100644 --- a/t/11-sql-with-models.t +++ b/t/11-sql-with-models.t @@ -331,7 +331,7 @@ sub search_by_prepared_statement { my @bind; my $map = $stmt->select_map; for my $col (@{ $stmt->select }) { - push @bind, \$rec->{ $map->{$col} || $col }; + push @bind, \$rec->{ $map->{$col} }; } my $dbh = $driver->r_handle($class->properties->{db}); From 1799fc017af1291ace706f0fcbf2bf267d804722 Mon Sep 17 00:00:00 2001 From: Keita Jamadam Sugama Date: Fri, 19 Sep 2025 12:10:24 +0900 Subject: [PATCH 2/3] remove select_map and make select_map_reverse mandatory --- lib/Data/ObjectDriver/SQL.pm | 47 ++++++++++++++++++------------------ t/11-sql-with-models.t | 32 ++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 24 deletions(-) diff --git a/lib/Data/ObjectDriver/SQL.pm b/lib/Data/ObjectDriver/SQL.pm index 5464ea6..4b4ef5c 100644 --- a/lib/Data/ObjectDriver/SQL.pm +++ b/lib/Data/ObjectDriver/SQL.pm @@ -8,7 +8,7 @@ use Scalar::Util 'blessed'; use base qw( Class::Accessor::Fast ); __PACKAGE__->mk_accessors(qw( - select distinct select_map select_map_reverse + select distinct select_map_reverse from joins where bind limit offset group order having where_values column_mutator index_hint comment as aggrigated @@ -19,7 +19,6 @@ sub new { my $stmt = $class->SUPER::new(@_); $stmt->select([]); $stmt->distinct(0); - $stmt->select_map({}); $stmt->select_map_reverse({}); $stmt->bind([]); $stmt->from([]); @@ -31,17 +30,20 @@ sub new { $stmt; } +sub select_map { + return { $_[0]->{select_map_reverse} ? reverse %{ $_[0]->{select_map_reverse} } : () }; +} + sub add_select { my $stmt = shift; my($term, $col) = @_; - $col ||= $term; push @{ $stmt->select }, $term; - if (blessed($term) && $col->isa('Data::ObjectDriver::SQL')) { - die 'Sub-query requires an alias by setting $stmt->as(...)' unless $term->as; - $stmt->select_map->{$term} = $term->as; + if (blessed($term) && $term->isa('Data::ObjectDriver::SQL')) { + my $alias = $col || $term->as; + die 'Sub-query requires an alias by setting $stmt->as(...)' unless $alias; + $stmt->select_map_reverse->{$alias} = $term; } else { - $stmt->select_map->{$term} = $col; - $stmt->select_map_reverse->{$col} = $term; + $stmt->select_map_reverse->{$col || $term} = $term; } } @@ -71,13 +73,15 @@ sub as_sql { if (@{ $stmt->select }) { $sql .= 'SELECT '; $sql .= 'DISTINCT ' if $stmt->distinct; + my $select_map = $stmt->select_map; $sql .= join(', ', map { my $col = $_; + my $alias = $select_map->{$col}; if (blessed($col) && $col->isa('Data::ObjectDriver::SQL')) { push @bind_for_select, @{ $col->{bind} }; - $col->as_subquery; + $col->as_subquery($alias); } else { - if (my $alias = $stmt->select_map->{$col}) { + if ($alias) { /(?:^|\.)\Q$alias\E$/ ? $col : "$col $alias"; } else { $col; @@ -144,10 +148,11 @@ sub as_sql { } sub as_subquery { - my $stmt = shift; - my $subquery = '('. $stmt->as_sql. ')'; - if ($stmt->as) { - $subquery .= ' AS ' . $stmt->as; + my ($stmt, $alias) = @_; + my $subquery = '(' . $stmt->as_sql . ')'; + $alias ||= $stmt->as; + if ($alias) { + $subquery .= ' AS ' . $alias; } $subquery; } @@ -273,8 +278,8 @@ sub add_having { # Carp::croak("Invalid/unsafe column name $col") unless $col =~ /^[\w\.]+$/; if (my $orig = $stmt->select_map_reverse->{$col}) { - $col = $orig; - } + $col = $orig; + } my($term, $bind) = $stmt->_mk_term($col, $val); push @{ $stmt->{having} }, "($term)"; @@ -429,15 +434,9 @@ The database columns to select in a C query should return DISTINCT rows only. -=head2 C (hashref) - -The map of database column names to object fields in a C list to column names. - =head2 C (hashref) -The map of object fields to database column names in a C query. =head2 C (arrayref) @@ -568,7 +567,7 @@ Creates a new, empty SQL statement. Adds the database column C<$column> to the list of fields to return in a C