Skip to content

Commit

Permalink
Moved adding of limit on deletion and adding of limit on update condi…
Browse files Browse the repository at this point in the history
…tions to GenericCompiler, to provide flexibility to handle db specifics

Signed-off-by: Emilio Tagua <miloops@gmail.com>
  • Loading branch information
Praveen Devarao authored and miloops committed Feb 25, 2010
1 parent debd111 commit 4525b1f
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 5 deletions.
62 changes: 62 additions & 0 deletions lib/arel/engines/sql/compilers/ibm_db_compiler.rb
@@ -0,0 +1,62 @@
# +-----------------------------------------------------------------------+
# | |
# | Copyright (c) 2010 IBM Corporation |
# | |
# | Permission is hereby granted, free of charge, to any person obtaining |
# | a copy of this software and associated documentation files (the |
# | "Software"), to deal in the Software without restriction, including |
# | without limitation the rights to use, copy, modify, merge, publish, |
# | distribute, sublicense, and/or sell copies of the Software, and to |
# | permit persons to whom the Software is furnished to do so, subject to |
# | the following conditions: |
# | |
# | The above copyright notice and this permission notice shall be |
# | included in all copies or substantial portions of the Software. |
# | |
# | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
# | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
# | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.|
# | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR |
# | ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
# | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
# | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
# | |
# +-----------------------------------------------------------------------+

#
# Author: Praveen Devarao <praveendrl@in.ibm.com>
#

module Arel
module SqlCompiler
class IBM_DBCompiler < GenericCompiler

def select_sql
query = build_query \
"SELECT #{select_clauses.join(', ')}",
"FROM #{from_clauses}",
(joins(self) unless joins(self).blank? ),
("WHERE #{where_clauses.join(" AND ")}" unless wheres.blank? ),
("GROUP BY #{group_clauses.join(', ')}" unless groupings.blank? ),
("HAVING #{having_clauses.join(', ')}" unless havings.blank? ),
("ORDER BY #{order_clauses.join(', ')}" unless orders.blank? )
engine.add_limit_offset!(query,{:limit=>taken,:offset=>skipped}) unless taken.blank?
query << "#{locked}" unless locked.blank?
query
end

def limited_update_conditions(conditions, taken)
quoted_primary_key = engine.quote_table_name(primary_key)
update_conditions = "WHERE #{quoted_primary_key} IN (SELECT #{quoted_primary_key} FROM #{engine.connection.quote_table_name table.name} #{conditions} " #Note: - ')' not added, limit segment is to be appended
engine.add_limit_offset!(update_conditions,{:limit=>taken,:offset=>nil})
update_conditions << ")" # Close the sql segment
update_conditions
end

def add_limit_on_delete(taken)
"" # Limiting the number of rows to be deleted is not supported by IBM_DB
end

end
end
end
7 changes: 6 additions & 1 deletion lib/arel/engines/sql/relations/compiler.rb
Expand Up @@ -21,11 +21,16 @@ def select_sql
("#{locked}" unless locked.blank?) ("#{locked}" unless locked.blank?)
end end


def limited_update_conditions(conditions) def limited_update_conditions(conditions, taken)
conditions << " LIMIT #{taken}"
quoted_primary_key = engine.quote_table_name(primary_key) quoted_primary_key = engine.quote_table_name(primary_key)
"WHERE #{quoted_primary_key} IN (SELECT #{quoted_primary_key} FROM #{engine.connection.quote_table_name table.name} #{conditions})" "WHERE #{quoted_primary_key} IN (SELECT #{quoted_primary_key} FROM #{engine.connection.quote_table_name table.name} #{conditions})"
end end


def add_limit_on_delete(taken)
"LIMIT #{taken}"
end

def supports_insert_with_returning? def supports_insert_with_returning?
false false
end end
Expand Down
6 changes: 2 additions & 4 deletions lib/arel/engines/sql/relations/writes.rb
Expand Up @@ -5,7 +5,7 @@ def to_sql
"DELETE", "DELETE",
"FROM #{table_sql}", "FROM #{table_sql}",
("WHERE #{wheres.collect(&:to_sql).join(' AND ')}" unless wheres.blank? ), ("WHERE #{wheres.collect(&:to_sql).join(' AND ')}" unless wheres.blank? ),
("LIMIT #{taken}" unless taken.blank? ) (compiler.add_limit_on_delete(taken) unless taken.blank? )
end end
end end


Expand Down Expand Up @@ -68,9 +68,7 @@ def build_update_conditions_sql
conditions << " ORDER BY #{order_clauses.join(', ')}" unless orders.blank? conditions << " ORDER BY #{order_clauses.join(', ')}" unless orders.blank?


unless taken.blank? unless taken.blank?
conditions << " LIMIT #{taken}" conditions = compiler.limited_update_conditions(conditions,taken)

conditions = compiler.limited_update_conditions(conditions)
end end


conditions conditions
Expand Down

0 comments on commit 4525b1f

Please sign in to comment.