public
Created

  • Download Gist
arel_compiler_patch.diff
Diff
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
From 8812b05e24aeb70099e50bb2716b647555902365 Mon Sep 17 00:00:00 2001
From: Praveen Devarao <praveen@falcon>
Date: Thu, 25 Feb 2010 12:49:19 +0530
Subject: [PATCH] Moved adding of limit on deletion and adding of limit on update conditions to GenericCompiler, to provide flexibility to handle db specifics
 
---
lib/arel/engines/sql/compilers/ibm_db_compiler.rb | 62 +++++++++++++++++++++
lib/arel/engines/sql/relations/compiler.rb | 7 ++-
lib/arel/engines/sql/relations/writes.rb | 6 +-
3 files changed, 70 insertions(+), 5 deletions(-)
create mode 100644 lib/arel/engines/sql/compilers/ibm_db_compiler.rb
 
diff --git a/lib/arel/engines/sql/compilers/ibm_db_compiler.rb b/lib/arel/engines/sql/compilers/ibm_db_compiler.rb
new file mode 100644
index 0000000..e6be73d
--- /dev/null
+++ b/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
diff --git a/lib/arel/engines/sql/relations/compiler.rb b/lib/arel/engines/sql/relations/compiler.rb
index c974e43..597ed88 100644
--- a/lib/arel/engines/sql/relations/compiler.rb
+++ b/lib/arel/engines/sql/relations/compiler.rb
@@ -21,11 +21,16 @@ module Arel
("#{locked}" unless locked.blank?)
end
- def limited_update_conditions(conditions)
+ 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})"
end
+ def add_limit_on_delete(taken)
+ "LIMIT #{taken}"
+ end
+
def supports_insert_with_returning?
false
end
diff --git a/lib/arel/engines/sql/relations/writes.rb b/lib/arel/engines/sql/relations/writes.rb
index 0a6250a..54eaee9 100644
--- a/lib/arel/engines/sql/relations/writes.rb
+++ b/lib/arel/engines/sql/relations/writes.rb
@@ -5,7 +5,7 @@ module Arel
"DELETE",
"FROM #{table_sql}",
("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
@@ -68,9 +68,7 @@ module Arel
conditions << " ORDER BY #{order_clauses.join(', ')}" unless orders.blank?
unless taken.blank?
- conditions << " LIMIT #{taken}"
-
- conditions = compiler.limited_update_conditions(conditions)
+ conditions = compiler.limited_update_conditions(conditions,taken)
end
conditions
--
1.6.0
 
 
From 860ad2230ce658432bf62578c7d65285994e1cfa Mon Sep 17 00:00:00 2001
From: Praveen Devarao <praveen@falcon>
Date: Thu, 25 Feb 2010 14:35:16 +0530
Subject: [PATCH] raise an error if limit for deletion is specified while using IBM_DB
 
---
lib/arel/engines/sql/compilers/ibm_db_compiler.rb | 2 +-
1 files changed, 1 insertions(+), 1 deletions(-)
 
diff --git a/lib/arel/engines/sql/compilers/ibm_db_compiler.rb b/lib/arel/engines/sql/compilers/ibm_db_compiler.rb
index e6be73d..e15e783 100644
--- a/lib/arel/engines/sql/compilers/ibm_db_compiler.rb
+++ b/lib/arel/engines/sql/compilers/ibm_db_compiler.rb
@@ -54,7 +54,7 @@ module Arel
end
def add_limit_on_delete(taken)
- "" # Limiting the number of rows to be deleted is not supported by IBM_DB
+ raise "IBM_DB does not support limit on deletion" # Limiting the number of rows to be deleted is not supported by IBM_DB
end
end
--
1.6.0

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.