diff options
author | Emilio Tagua <miloops@gmail.com> | 2010-02-15 17:20:24 -0300 |
---|---|---|
committer | Emilio Tagua <miloops@gmail.com> | 2010-02-15 17:20:24 -0300 |
commit | 6e9cf0cb4390f1d210edbecc660290aaea6b4d72 (patch) | |
tree | b4dfbdc70a5efdd7866ee81275f46d8c03760033 /lib/arel/engines/sql/compilers | |
parent | 7caf242cf484f8875a0cfdf2c252a2fefc273358 (diff) | |
download | rails-6e9cf0cb4390f1d210edbecc660290aaea6b4d72.tar.gz rails-6e9cf0cb4390f1d210edbecc660290aaea6b4d72.tar.bz2 rails-6e9cf0cb4390f1d210edbecc660290aaea6b4d72.zip |
Extract SQL logic from Arel::Relation into compilers.
Diffstat (limited to 'lib/arel/engines/sql/compilers')
-rw-r--r-- | lib/arel/engines/sql/compilers/mysql_compiler.rb | 7 | ||||
-rw-r--r-- | lib/arel/engines/sql/compilers/postgresql_compiler.rb | 33 | ||||
-rw-r--r-- | lib/arel/engines/sql/compilers/sqlite_compiler.rb | 9 |
3 files changed, 49 insertions, 0 deletions
diff --git a/lib/arel/engines/sql/compilers/mysql_compiler.rb b/lib/arel/engines/sql/compilers/mysql_compiler.rb new file mode 100644 index 0000000000..e3cf1f2add --- /dev/null +++ b/lib/arel/engines/sql/compilers/mysql_compiler.rb @@ -0,0 +1,7 @@ +module Arel + module SqlCompiler + class MySQLCompiler < GenericCompiler + end + end +end + diff --git a/lib/arel/engines/sql/compilers/postgresql_compiler.rb b/lib/arel/engines/sql/compilers/postgresql_compiler.rb new file mode 100644 index 0000000000..c3360b53a5 --- /dev/null +++ b/lib/arel/engines/sql/compilers/postgresql_compiler.rb @@ -0,0 +1,33 @@ +module Arel + module SqlCompiler + class PostgreSQLCompiler < GenericCompiler + + def select_sql + if !orders.blank? && using_distinct_on? + # PostgreSQL does not allow arbitrary ordering when using DISTINCT ON, so we work around this + # by wrapping the +sql+ string as a sub-select and ordering in that query. + order = order_clauses.join(', ').split(',').map { |s| s.strip }.reject(&:blank?) + order = order.zip((0...order.size).to_a).map { |s,i| "id_list.alias_#{i} #{'DESC' if s =~ /\bdesc$/i}" }.join(', ') + + query = build_query \ + "SELECT #{select_clauses.kind_of?(::Array) ? select_clauses.join("") : select_clauses.to_s}", + "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? ), + ("#{locked}" unless locked.blank? ) + + build_query \ + "SELECT * FROM (#{query}) AS id_list", + "ORDER BY #{order}", + ("LIMIT #{taken}" unless taken.blank? ), + ("OFFSET #{skipped}" unless skipped.blank? ) + + else + super + end + end + end + end +end diff --git a/lib/arel/engines/sql/compilers/sqlite_compiler.rb b/lib/arel/engines/sql/compilers/sqlite_compiler.rb new file mode 100644 index 0000000000..c2f78cd235 --- /dev/null +++ b/lib/arel/engines/sql/compilers/sqlite_compiler.rb @@ -0,0 +1,9 @@ +module Arel + module SqlCompiler + class SQLiteCompiler < GenericCompiler + def locked + nil + end + end + end +end |