Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Simplify SQL for selecting an entire table" #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions lib/Fey/Role/TableLike.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 11 additions & 9 deletions lib/Fey/SQL/Select.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -564,7 +566,7 @@ These will be passed to C<< Fey::Literal->new_from_scalar() >>.
=item * C<Fey::Table> objects

If a table is passed, then all of its columns will be included in the
C<SELECT> clause.
C<SELECT> clause, sorted alphanumerically.

=item * C<Fey::Column> objects, and aliases

Expand Down
2 changes: 0 additions & 2 deletions lib/Fey/Table.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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() }
Expand Down
4 changes: 0 additions & 4 deletions lib/Fey/Table/Alias.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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() ) );
Expand Down
4 changes: 2 additions & 2 deletions t/SQL/Select-from-clause.t
Original file line number Diff line number Diff line change
Expand Up @@ -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")};
Expand Down Expand Up @@ -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")};
Expand Down
10 changes: 6 additions & 4 deletions t/SQL/Select-select-clause.t
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ 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'
);

is_deeply(
[ map { $_->name() } $select->select_clause_elements() ],
[qw( User )],
[qw( email user_id username )],
'select_clause_elements with one table'
);
}
Expand All @@ -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,
Expand All @@ -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'
Expand Down