Skip to content

Commit

Permalink
Fix deleting from joined datasets on MySQL
Browse files Browse the repository at this point in the history
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
jeremyevans committed Nov 23, 2009
1 parent 77c0483 commit 883aebd
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
11 changes: 11 additions & 0 deletions lib/sequel/adapters/shared/mysql.rb
Expand Up @@ -352,6 +352,17 @@ def _insert_sql
def delete_clause_methods def delete_clause_methods
DELETE_CLAUSE_METHODS DELETE_CLAUSE_METHODS
end end

# 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 # MySQL supports the IGNORE and ON DUPLICATE KEY UPDATE clauses for INSERT statements
def insert_clause_methods def insert_clause_methods
Expand Down
7 changes: 1 addition & 6 deletions lib/sequel/dataset/sql.rb
Expand Up @@ -679,12 +679,6 @@ def delete_clause_methods
DELETE_CLAUSE_METHODS DELETE_CLAUSE_METHODS
end end


# 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 # Converts an array of expressions into a comma separated string of
# expressions. # expressions.
def expression_list(columns) def expression_list(columns)
Expand Down Expand Up @@ -984,6 +978,7 @@ def select_compounds_sql(sql)
def select_from_sql(sql) def select_from_sql(sql)
sql << " FROM #{source_list(@opts[:from])}" if @opts[:from] sql << " FROM #{source_list(@opts[:from])}" if @opts[:from]
end end
alias delete_from_sql select_from_sql


# Modify the sql to add the expressions to GROUP BY # Modify the sql to add the expressions to GROUP BY
def select_group_sql(sql) def select_group_sql(sql)
Expand Down
2 changes: 1 addition & 1 deletion spec/core/dataset_spec.rb
Expand Up @@ -3564,7 +3564,7 @@ def @ds.fetch_rows(sql)


specify "should allow deleting from joined datasets" do specify "should allow deleting from joined datasets" do
@ds.delete @ds.delete
@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)']
end end


specify "should allow updating joined datasets" do specify "should allow updating joined datasets" do
Expand Down
2 changes: 1 addition & 1 deletion spec/integration/dataset_test.rb
Expand Up @@ -878,7 +878,7 @@ def uprev
@db.create_table!(:a){Integer :a; Integer :d} @db.create_table!(:a){Integer :a; Integer :d}
@db.create_table!(:b){Integer :b; Integer :e} @db.create_table!(:b){Integer :b; Integer :e}
@db.create_table!(:c){Integer :c; Integer :f} @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(1, 2)
@db[:a].insert(3, 4) @db[:a].insert(3, 4)
@db[:b].insert(2, 5) @db[:b].insert(2, 5)
Expand Down

0 comments on commit 883aebd

Please sign in to comment.