From 20a10e5302ce8f912d27c57702b542a019c7f9f9 Mon Sep 17 00:00:00 2001 From: Raimonds Simanovskis Date: Wed, 10 Feb 2010 19:50:59 +0200 Subject: changes for Oracle support - OracleCompiler and corresponding tests with Oracle syntax --- lib/arel/engines/sql/relations/compiler.rb | 76 ++++++++++++++++++++++++++++-- 1 file changed, 72 insertions(+), 4 deletions(-) (limited to 'lib/arel/engines/sql/relations/compiler.rb') diff --git a/lib/arel/engines/sql/relations/compiler.rb b/lib/arel/engines/sql/relations/compiler.rb index 597ed88683..fc8d484276 100644 --- a/lib/arel/engines/sql/relations/compiler.rb +++ b/lib/arel/engines/sql/relations/compiler.rb @@ -21,20 +21,55 @@ module Arel ("#{locked}" unless locked.blank?) end - def limited_update_conditions(conditions, taken) - conditions << " LIMIT #{taken}" - 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})" + def delete_sql + build_query \ + "DELETE", + "FROM #{table_sql}", + ("WHERE #{wheres.collect(&:to_sql).join(' AND ')}" unless wheres.blank? ), + (add_limit_on_delete(taken) unless taken.blank? ) end def add_limit_on_delete(taken) "LIMIT #{taken}" end + def insert_sql(include_returning = true) + insertion_attributes_values_sql = if record.is_a?(Value) + record.value + else + attributes = record.keys.sort_by do |attribute| + attribute.name.to_s + end + + first = attributes.collect do |key| + engine.quote_column_name(key.name) + end.join(', ') + + second = attributes.collect do |key| + key.format(record[key]) + end.join(', ') + + build_query "(#{first})", "VALUES (#{second})" + end + + build_query \ + "INSERT", + "INTO #{table_sql}", + insertion_attributes_values_sql, + ("RETURNING #{engine.quote_column_name(primary_key)}" if include_returning && compiler.supports_insert_with_returning?) + end + def supports_insert_with_returning? false end + def update_sql + build_query \ + "UPDATE #{table_sql} SET", + assignment_sql, + build_update_conditions_sql + end + protected def method_missing(method, *args, &block) relation.send(method, *args, &block) @@ -44,6 +79,39 @@ module Arel parts.compact.join(" ") end + def assignment_sql + if assignments.respond_to?(:collect) + attributes = assignments.keys.sort_by do |attribute| + attribute.name.to_s + end + + attributes.map do |attribute| + value = assignments[attribute] + "#{engine.quote_column_name(attribute.name)} = #{attribute.format(value)}" + end.join(", ") + else + assignments.value + end + end + + def build_update_conditions_sql + conditions = "" + conditions << " WHERE #{wheres.collect(&:to_sql).join(' AND ')}" unless wheres.blank? + conditions << " ORDER BY #{order_clauses.join(', ')}" unless orders.blank? + + unless taken.blank? + conditions = limited_update_conditions(conditions, taken) + end + + conditions + end + + def limited_update_conditions(conditions, taken) + conditions << " LIMIT #{taken}" + quoted_primary_key = engine.quote_column_name(primary_key) + "WHERE #{quoted_primary_key} IN (SELECT #{quoted_primary_key} FROM #{engine.connection.quote_table_name table.name} #{conditions})" + end + end end -- cgit v1.2.3