From 51134d2177199d007f56ff90d52639e78da84fa0 Mon Sep 17 00:00:00 2001 From: Aristotle Pagaltzis Date: Tue, 29 Aug 2023 12:32:10 +0200 Subject: [PATCH] Revert "Simplify SQL for selecting an entire table" This reverts commit d8efe64fdfa9d51a687122cb4f62e484e8c59584. This closes #3. --- lib/Fey/Role/TableLike.pm | 2 -- lib/Fey/SQL/Select.pm | 20 +++++++++++--------- lib/Fey/Table.pm | 2 -- lib/Fey/Table/Alias.pm | 4 ---- t/SQL/Select-from-clause.t | 4 ++-- t/SQL/Select-select-clause.t | 10 ++++++---- 6 files changed, 19 insertions(+), 23 deletions(-) diff --git a/lib/Fey/Role/TableLike.pm b/lib/Fey/Role/TableLike.pm index 91328d8..ca96f4c 100644 --- a/lib/Fey/Role/TableLike.pm +++ b/lib/Fey/Role/TableLike.pm @@ -10,8 +10,6 @@ use Moose::Role; with 'Fey::Role::Joinable'; -requires 'sql_for_select_clause'; - 1; # ABSTRACT: A role for things that are like a table diff --git a/lib/Fey/SQL/Select.pm b/lib/Fey/SQL/Select.pm index 9e1b2df..9d24f22 100644 --- a/lib/Fey/SQL/Select.pm +++ b/lib/Fey/SQL/Select.pm @@ -116,9 +116,15 @@ sub select { MX_PARAMS_VALIDATE_NO_CACHE => 1, ); - for my $elt (@select) { - $self->_add_select_element( - blessed $elt ? $elt : Fey::Literal->new_from_scalar($elt) ); + for my $elt ( + map { + $_->can('columns') + ? sort { $a->name() cmp $b->name() } $_->columns() + : $_ + } + map { blessed $_ ? $_ : Fey::Literal->new_from_scalar($_) } @select + ) { + $self->_add_select_element($elt); } return $self; @@ -399,11 +405,7 @@ sub select_clause { $sql .= ( join ', ', - map { - $_->can('sql_for_select_clause') - ? $_->sql_for_select_clause($dbh) - : $_->sql_with_alias($dbh) - } $self->select_clause_elements() + map { $_->sql_with_alias($dbh) } $self->select_clause_elements() ); return $sql; @@ -564,7 +566,7 @@ These will be passed to C<< Fey::Literal->new_from_scalar() >>. =item * C objects If a table is passed, then all of its columns will be included in the -C clause, sorted alphanumerically. =item * C objects, and aliases diff --git a/lib/Fey/Table.pm b/lib/Fey/Table.pm index 46fb6f6..7b8a3fc 100644 --- a/lib/Fey/Table.pm +++ b/lib/Fey/Table.pm @@ -303,8 +303,6 @@ sub sql { return $_[1]->quote_identifier( $_[0]->name() ); } -sub sql_for_select_clause { $_[0]->sql( $_[1] ) . '.*' } - sub sql_with_alias { goto &sql } sub _build_id { $_[0]->name() } diff --git a/lib/Fey/Table/Alias.pm b/lib/Fey/Table/Alias.pm index 3fb3768..58bb8a8 100644 --- a/lib/Fey/Table/Alias.pm +++ b/lib/Fey/Table/Alias.pm @@ -101,10 +101,6 @@ sub primary_key { sub is_alias {1} -sub sql_for_select_clause { - return $_[1]->quote_identifier( $_[0]->alias_name() ) . '.*'; -} - sub sql_with_alias { return ( $_[1]->quote_identifier( $_[0]->table()->name() ) . ' AS ' . $_[1]->quote_identifier( $_[0]->alias_name() ) ); diff --git a/t/SQL/Select-from-clause.t b/t/SQL/Select-from-clause.t index 28ab93c..113e2c4 100644 --- a/t/SQL/Select-from-clause.t +++ b/t/SQL/Select-from-clause.t @@ -659,7 +659,7 @@ my $dbh = Fey::Test->mock_dbh(); ->where( $first->column('first_id'), '=', Fey::Placeholder->new() ); #>> - my $expect = q{SELECT "fourth".*}; + my $expect = q{SELECT "fourth"."third_id", "fourth"."fourth_id"}; $expect .= q{ FROM "second"}; $expect .= q{ JOIN "first" ON ("second"."first_id" = "first"."first_id")}; $expect .= q{ JOIN "third" ON ("third"."second_id" = "second"."second_id")}; @@ -764,7 +764,7 @@ TODO: ->where( $t1->column('t1_id'), '=', Fey::Placeholder->new() ); #>> - my $expect = q{SELECT "t4".*}; + my $expect = q{SELECT "t4"."t3_id", "t4"."t4_id"}; $expect .= q{ FROM "t2"}; $expect .= q{ JOIN "t1" ON ("t2"."t1_id" = "t1"."t1_id")}; $expect .= q{ JOIN "t3" ON ("t3"."t2_id" = "t2"."t2_id")}; diff --git a/t/SQL/Select-select-clause.t b/t/SQL/Select-select-clause.t index f8f0fd4..6e29200 100644 --- a/t/SQL/Select-select-clause.t +++ b/t/SQL/Select-select-clause.t @@ -18,7 +18,7 @@ my $dbh = Fey::Test->mock_dbh(); isa_ok( $select, 'Fey::SQL::Select' ); - my $sql = q{SELECT "User".*}; + my $sql = q{SELECT "User"."email", "User"."user_id", "User"."username"}; is( $select->select_clause($dbh), $sql, 'select_clause with one table' @@ -26,7 +26,7 @@ my $dbh = Fey::Test->mock_dbh(); is_deeply( [ map { $_->name() } $select->select_clause_elements() ], - [qw( User )], + [qw( email user_id username )], 'select_clause_elements with one table' ); } @@ -39,7 +39,8 @@ my $dbh = Fey::Test->mock_dbh(); my $user_alias = $s->table('User')->alias( alias_name => 'UserA' ); $select->select($user_alias); - my $sql = q{SELECT "User".*, "UserA".*}; + my $sql = q{SELECT "User"."email", "User"."user_id", "User"."username"}; + $sql .= q{, "UserA"."email", "UserA"."user_id", "UserA"."username"}; is( $select->select_clause($dbh), $sql, @@ -53,7 +54,8 @@ my $dbh = Fey::Test->mock_dbh(); $select->select( $s->table('User')->column('user_id') ); $select->select( $s->table('User') ); - my $sql = q{SELECT "User"."user_id", "User".*}; + my $sql + = q{SELECT "User"."user_id", "User"."email", "User"."user_id", "User"."username"}; is( $select->select_clause($dbh), $sql, 'select_clause when first adding column and then table for that column'