Browse files

Fix deleting from joined datasets on MySQL

While here, no longer include joins in delete_from_sql by default.
Since all of the adapters that support modifying joins override it,
there's no reason to include it.

Also, remove the order on the joined dataset in the integration
test so that updating works on MySQL (which allows order clauses
in update statements, but not when multiple tables are used).

Note that MySQL allows deleting from multiple tables at once, but
Sequel doesn't support this.  Sequel assumes the first table in
FROM is the one being deleted from, and the rest are just for
selecting rows.  This is for consistency with how Sequel works on
PostgreSQL and MSSQL.
  • Loading branch information...
1 parent 77c0483 commit 883aebd03e965c65e56b73320c33d5a340ca482f @jeremyevans committed Nov 23, 2009
@@ -352,6 +352,17 @@ def _insert_sql
def delete_clause_methods
+ # Consider the first table in the joined dataset is the table to delete
+ # from, but include the others for the purposes of selecting rows.
+ def delete_from_sql(sql)
+ if joined_dataset?
+ sql << " #{source_list(@opts[:from][0..0])} FROM #{source_list(@opts[:from])}"
+ select_join_sql(sql)
+ else
+ super
+ end
+ end
# MySQL supports the IGNORE and ON DUPLICATE KEY UPDATE clauses for INSERT statements
def insert_clause_methods
@@ -679,12 +679,6 @@ def delete_clause_methods
- # Included both from a join tables if modifying joins is allowed
- def delete_from_sql(sql)
- select_from_sql(sql)
- select_join_sql(sql) if supports_modifying_joins?
- end
# Converts an array of expressions into a comma separated string of
# expressions.
def expression_list(columns)
@@ -984,6 +978,7 @@ def select_compounds_sql(sql)
def select_from_sql(sql)
sql << " FROM #{source_list(@opts[:from])}" if @opts[:from]
+ alias delete_from_sql select_from_sql
# Modify the sql to add the expressions to GROUP BY
def select_group_sql(sql)
@@ -3564,7 +3564,7 @@ def @ds.fetch_rows(sql)
specify "should allow deleting from joined datasets" do
- @ds.db.sqls.should == ['DELETE FROM b, c INNER JOIN d USING (id) WHERE (id = 2)']
+ @ds.db.sqls.should == ['DELETE FROM b, c WHERE (id = 2)']
specify "should allow updating joined datasets" do
@@ -878,7 +878,7 @@ def uprev
@db.create_table!(:a){Integer :a; Integer :d}
@db.create_table!(:b){Integer :b; Integer :e}
@db.create_table!(:c){Integer :c; Integer :f}
- @ds = @db.from(:a, :b).join(:c, :c=>:e.identifier).where(:d=>:b, :f=>6).order(:a)
+ @ds = @db.from(:a, :b).join(:c, :c=>:e.identifier).where(:d=>:b, :f=>6)
@db[:a].insert(1, 2)
@db[:a].insert(3, 4)
@db[:b].insert(2, 5)

0 comments on commit 883aebd

Please sign in to comment.