Skip to content

Commit 1ffbb21

Browse files
authored
Merge pull request #24 from djerius/table_alias
Support table name aliases in update and delete statements
2 parents 94aceed + cffd32e commit 1ffbb21

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

lib/SQL/Abstract/More.pm

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -883,7 +883,10 @@ sub update {
883883

884884
# compute join info if the datasource is a join
885885
$join_info = $self->_compute_join_info($args{-table});
886-
$args{-table} = \($join_info->{sql}) if $join_info;
886+
$args{-table} =
887+
defined $join_info
888+
? \($join_info->{sql})
889+
: \($self->_parse_table( $args{-table})->{sql});
887890

888891
@old_API_args = @args{qw/-table -set -where/};
889892

@@ -897,6 +900,7 @@ sub update {
897900
}
898901

899902
# call parent method and merge with bind values from $join_info
903+
900904
my ($sql, @bind) = $self->_parent_update(@old_API_args);
901905

902906
unshift @bind, @{$join_info->{bind}} if $join_info;
@@ -938,6 +942,8 @@ sub delete {
938942
@old_API_args = @_;
939943
}
940944

945+
$old_API_args[0] = \($self->_parse_table( $old_API_args[0] )->{sql});
946+
941947
# call parent method
942948
my ($sql, @bind) = $self->next::method(@old_API_args);
943949

t/01-sql_abstract_more.t

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,17 @@ is_same_sql_bind(
948948
[2, 1, 3],
949949
);
950950

951+
# support for table aliases
952+
($sql, @bind) = $sqla->update(
953+
-table => 'Foo|a',
954+
-set => {foo => 1, bar => 2},
955+
-where => {buz => 3},
956+
);
957+
is_same_sql_bind(
958+
$sql, \@bind,
959+
'UPDATE Foo SET bar = ?, foo = ? WHERE buz = ?',
960+
[2, 1, 3],
961+
);
951962

952963
# MySQL supports -limit and -order_by in updates !
953964
# see http://dev.mysql.com/doc/refman/5.6/en/update.html
@@ -1057,6 +1068,17 @@ is_same_sql_bind(
10571068
[3],
10581069
);
10591070

1071+
# support for table aliases
1072+
($sql, @bind) = $sqla->delete(
1073+
-from => 'Foo|a',
1074+
-where => {buz => 3},
1075+
);
1076+
is_same_sql_bind(
1077+
$sql, \@bind,
1078+
'DELETE FROM Foo AS a WHERE buz = ?',
1079+
[3],
1080+
);
1081+
10601082
# MySQL supports -limit and -order_by in deletes !
10611083
# see http://dev.mysql.com/doc/refman/5.6/en/delete.html
10621084
($sql, @bind) = $sqla->delete(

0 commit comments

Comments
 (0)