Created
April 18, 2011 11:59
-
-
Save zigorou/925194 to your computer and use it in GitHub Desktop.
from and join
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| subtest 'from and using left outer join with condition' => sub { | |
| my ($sql, @binds) = $builder->select( [ [ [ 'foo', 'f' ], +{ table => 'bar', alias => 'b', type => 'LEFT OUTER', condition => 'f.id = b.foo_id' } ], [ 'baz' => 'bz' ] ], ['*'], +{}, ); | |
| is $sql, qq{SELECT *\nFROM "foo" "f" LEFT OUTER JOIN "bar" "b" ON f.id = b.foo_id, "baz" "bz"}; | |
| is join(',', @binds), ''; | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| sub select_query { | |
| my ($self, $table, $fields, $where, $opt) = @_; | |
| unless (ref $fields eq 'ARRAY') { | |
| Carp::croak("SQL::Maker::select_query: \$fields should be ArrayRef[Str]"); | |
| } | |
| my $stmt = $self->new_select( | |
| select => $fields, | |
| ); | |
| unless ( ref $table ) { | |
| # 'foo' | |
| $stmt->add_from( $table ); | |
| } | |
| else { | |
| for ( @$table ) { | |
| if ( ref $_ eq 'ARRAY' ) { | |
| if ( ref $_->[1] eq 'HASH' ) { | |
| # [ [ 'foo', +{ ... } ], ... ] | |
| $stmt->add_join( @$_ ); | |
| } | |
| else { | |
| # [ [ 'foo', 'f' ], ... ] | |
| $stmt->add_from( @$_ ); | |
| } | |
| } | |
| else { | |
| # [ 'foo', ... ] | |
| $stmt->add_from( $_ ); | |
| } | |
| } | |
| } | |
| $stmt->prefix($opt->{prefix}) if $opt->{prefix}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment