Skip to content

Instantly share code, notes, and snippets.

@zigorou
Created April 18, 2011 11:59
Show Gist options
  • Select an option

  • Save zigorou/925194 to your computer and use it in GitHub Desktop.

Select an option

Save zigorou/925194 to your computer and use it in GitHub Desktop.
from and join
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), '';
};
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