Permalink
Browse files

Merge branch 'master' of https://github.com/mrcsparker/activerecord-j…

…dbcteradata-adapter

Conflicts:
	activerecord-jdbcteradata-adapter.gemspec
  • Loading branch information...
2 parents 821fa11 + 810c15d commit 69d78555ced3ab89a1b23affcaeb49bf42609340 @mrcsparker committed Jan 31, 2014
View
@@ -1,9 +1,9 @@
PATH
remote: .
specs:
- activerecord-jdbcteradata-adapter (0.4.2)
+ activerecord-jdbcteradata-adapter (0.5.1)
activerecord (<= 3.2.13)
- activerecord-jdbc-adapter
+ activerecord-jdbc-adapter (<= 1.2.9.1)
jdbc-teradata
GEM
@@ -21,12 +21,12 @@ GEM
activesupport (3.2.13)
i18n (= 0.6.1)
multi_json (~> 1.0)
- arel (3.0.2)
+ arel (3.0.3)
builder (3.0.4)
diff-lcs (1.2.4)
i18n (0.6.1)
jdbc-teradata (0.3.0)
- multi_json (1.7.7)
+ multi_json (1.8.2)
rake (10.0.4)
rspec (2.13.0)
rspec-core (~> 2.13.0)
@@ -36,7 +36,7 @@ GEM
rspec-expectations (2.13.0)
diff-lcs (>= 1.1.3, < 2.0)
rspec-mocks (2.13.1)
- tzinfo (0.3.37)
+ tzinfo (0.3.38)
PLATFORMS
java
@@ -35,7 +35,7 @@ Gem::Specification.new do |s|
s.add_development_dependency 'rake'
s.add_development_dependency 'rspec'
- s.add_dependency 'activerecord-jdbc-adapter'
- s.add_dependency 'activerecord', '~> 3.2.13'
+ s.add_dependency 'activerecord-jdbc-adapter', '<=1.2.9.1'
+ s.add_dependency 'activerecord', '<= 3.2.13'
s.add_dependency 'jdbc-teradata'
end
@@ -1,5 +1,5 @@
module ArJdbc
module Teradata
- VERSION = '0.2.0'
+ VERSION = '0.5.2'
end
end
@@ -3,37 +3,36 @@
require 'arel'
module Arel
-
module Nodes
-
# Extending the Ordering class to be comparrison friendly which allows us to call #uniq on a
# collection of them. See SelectManager#order for more details.
class Ordering < Arel::Nodes::Unary
def hash
expr.hash
end
+
def ==(other)
other.is_a?(Arel::Nodes::Ordering) && self.expr == other.expr
end
+
def eql?(other)
self == other
end
end
-
end
class SelectManager < Arel::TreeManager
-
AR_CA_SQLSA_NAME = 'Teradata'.freeze
# Getting real Ordering objects is very important for us. We need to be able to call #uniq on
# a colleciton of them reliably as well as using their true object attributes to mutate them
# to grouping objects for the inner sql during a select statment with an offset/rownumber. So this
# is here till ActiveRecord & ARel does this for us instead of using SqlLiteral objects.
alias :order_without_teradata :order
+
def order(*expr)
return order_without_teradata(*expr) unless engine_activerecord_teradata_adapter?
- @ast.orders.concat(expr.map{ |x|
+ @ast.orders.concat(expr.map { |x|
case x
when Arel::Attributes::Attribute
table = Arel::Table.new(x.relation.table_alias || x.relation.name)
@@ -61,7 +60,8 @@ def order(*expr)
# A friendly over ride that allows us to put a special lock object that can have a default or pass
# custom string hints down. See the visit_Arel_Nodes_LockWithTeradata delegation method.
alias :lock_without_teradata :lock
- def lock(locking=true)
+
+ def lock(locking = true)
if engine_activerecord_teradata_adapter?
case locking
when true
@@ -82,12 +82,10 @@ def lock(locking=true)
def engine_activerecord_teradata_adapter?
@engine.connection && @engine.connection.adapter_name == AR_CA_SQLSA_NAME
end
-
end
module Visitors
class Teradata < Arel::Visitors::ToSql
-
private
# Teradata ToSql/Visitor (Overides)
@@ -135,7 +133,7 @@ def visit_Arel_Nodes_Bin(o)
# Teradata ToSql/Visitor (Additions)
- def visit_Arel_Nodes_SelectStatementWithOutOffset(o, windowed=false)
+ def visit_Arel_Nodes_SelectStatementWithOutOffset(o, windowed = false)
find_and_fix_uncorrelated_joins_in_select_statement(o)
core = o.cores.first
projections = core.projections
@@ -155,52 +153,52 @@ def visit_Arel_Nodes_SelectStatementWithOutOffset(o, windowed=false)
elsif top_one_everything_for_through_join?(o)
projections = projections.map { |x| projection_without_expression(x) }
end
- [ ('SELECT' if !windowed),
- (visit(core.set_quantifier) if core.set_quantifier && !windowed),
- (visit(o.limit) if o.limit && !windowed),
- (projections.map{ |x| v = visit(x); v == '1' ? '1 AS __wrp' : v }.join(', ')),
- (source_with_lock_for_select_statement(o)),
- ("WHERE #{core.wheres.map{ |x| visit(x) }.join ' AND ' }" unless core.wheres.empty?),
- ("GROUP BY #{groups.map { |x| visit x }.join ', ' }" unless groups.empty?),
- (visit(core.having) if core.having),
- ("ORDER BY #{orders.map{ |x| visit(x) }.join(', ')}" if !orders.empty? && !windowed)
- ].compact.join ' '
+ [('SELECT' if !windowed),
+ (visit(core.set_quantifier) if core.set_quantifier && !windowed),
+ (visit(o.limit) if o.limit && !windowed),
+ (projections.map { |x| v = visit(x); v == '1' ? '1 AS __wrp' : v }.join(', ')),
+ (source_with_lock_for_select_statement(o)),
+ ("WHERE #{core.wheres.map { |x| visit(x) }.join ' AND ' }" unless core.wheres.empty?),
+ ("GROUP BY #{groups.map { |x| visit x }.join ', ' }" unless groups.empty?),
+ (visit(core.having) if core.having),
+ ("ORDER BY #{orders.map { |x| visit(x) }.join(', ')}" if !orders.empty? && !windowed)
+ ].compact.join ' '
end
def visit_Arel_Nodes_SelectStatementWithOffset(o)
core = o.cores.first
o.limit ||= Arel::Nodes::Limit.new(214748364)
orders = rowtable_orders(o)
- [ 'SELECT',
- (visit(o.limit) if o.limit && !windowed_single_distinct_select_statement?(o)),
- (rowtable_projections(o).map{ |x| visit(x) }.join(', ')),
- 'FROM (',
- "SELECT #{core.set_quantifier ? 'DISTINCT DENSE_RANK()' : 'ROW_NUMBER()'} OVER (ORDER BY #{orders.map{ |x| visit(x) }.join(', ')}) AS __rn,",
- visit_Arel_Nodes_SelectStatementWithOutOffset(o,true),
- ') AS __rnt',
- (visit(o.offset) if o.offset),
- 'ORDER BY __rnt.__rn ASC'
- ].compact.join ' '
+ ['SELECT',
+ (visit(o.limit) if o.limit && !windowed_single_distinct_select_statement?(o)),
+ (rowtable_projections(o).map{ |x| visit(x) }.join(', ')),
+ 'FROM (',
+ "SELECT #{core.set_quantifier ? 'DISTINCT DENSE_RANK()' : 'ROW_NUMBER()'} OVER (ORDER BY #{orders.map{ |x| visit(x) }.join(', ')}) AS __rn,",
+ visit_Arel_Nodes_SelectStatementWithOutOffset(o,true),
+ ') AS __rnt',
+ (visit(o.offset) if o.offset),
+ 'ORDER BY __rnt.__rn ASC'
+ ].compact.join ' '
end
def visit_Arel_Nodes_SelectStatementForComplexCount(o)
core = o.cores.first
o.limit.expr = Arel.sql("#{o.limit.expr} + #{o.offset ? o.offset.expr : 0}") if o.limit
orders = rowtable_orders(o)
- [ 'SELECT COUNT(count) AS count_id',
- 'FROM (',
- 'SELECT',
- (visit(o.limit) if o.limit),
- "ROW_NUMBER() OVER (ORDER BY #{orders.map{ |x| visit(x) }.join(', ')}) AS __rn,",
- '1 AS count',
- (source_with_lock_for_select_statement(o)),
- ("WHERE #{core.wheres.map{ |x| visit(x) }.join ' AND ' }" unless core.wheres.empty?),
- ("GROUP BY #{core.groups.map { |x| visit x }.join ', ' }" unless core.groups.empty?),
- (visit(core.having) if core.having),
- ("ORDER BY #{o.orders.map{ |x| visit(x) }.join(', ')}" if !o.orders.empty?),
- ') AS __rnt',
- (visit(o.offset) if o.offset)
- ].compact.join ' '
+ ['SELECT COUNT(count) AS count_id',
+ 'FROM (',
+ 'SELECT',
+ (visit(o.limit) if o.limit),
+ "ROW_NUMBER() OVER (ORDER BY #{orders.map{ |x| visit(x) }.join(', ')}) AS __rn,",
+ '1 AS count',
+ (source_with_lock_for_select_statement(o)),
+ ("WHERE #{core.wheres.map{ |x| visit(x) }.join ' AND ' }" unless core.wheres.empty?),
+ ("GROUP BY #{core.groups.map { |x| visit x }.join ', ' }" unless core.groups.empty?),
+ (visit(core.having) if core.having),
+ ("ORDER BY #{o.orders.map{ |x| visit(x) }.join(', ')}" if !o.orders.empty?),
+ ') AS __rnt',
+ (visit(o.offset) if o.offset)
+ ].compact.join ' '
end
# Teradata Helpers
@@ -245,7 +243,7 @@ def single_distinct_select_statement?(o)
p1 = projections.first
projections.size == 1 &&
((p1.respond_to?(:distinct) && p1.distinct) ||
- p1.respond_to?(:include?) && p1.include?('DISTINCT'))
+ p1.respond_to?(:include?) && p1.include?('DISTINCT'))
end
def windowed_single_distinct_select_statement?(o)
@@ -351,7 +349,7 @@ def rowtable_projections(o)
end
elsif join_in_select_statement?(o) && all_projections_aliased_in_select_statement?(o)
core.projections.map do |x|
- Arel.sql visit(x).split(',').map{ |y| y.split(' AS ').last.strip }.join(', ')
+ Arel.sql visit(x).split(',').map { |y| y.split(' AS ').last.strip }.join(', ')
end
elsif select_primary_key_sql?(o)
[Arel.sql("__rnt.#{quote_column_name(core.projections.first.name)}")]
@@ -375,16 +373,14 @@ def rowtable_orders(o)
def projection_without_expression(projection)
Arel.sql(visit(projection).split(',').map do |x|
x.strip!
- x.sub!(/^(COUNT|SUM|MAX|MIN|AVG)\s*(\((.*)\))?/,'\3')
- x.sub!(/^DISTINCT\s*/,'')
- x.sub!(/TOP\s*\(\d+\)\s*/i,'')
+ x.sub!(/^(COUNT|SUM|MAX|MIN|AVG)\s*(\((.*)\))?/, '\3')
+ x.sub!(/^DISTINCT\s*/, '')
+ x.sub!(/TOP\s*\(\d+\)\s*/i, '')
x.strip
end.join(', '))
end
-
end
end
-
end
Arel::Visitors::VISITORS['teradata'] = Arel::Visitors::Teradata
View
@@ -1,6 +1,6 @@
module ::ArJdbc
extension :Teradata do |name|
- if name =~ /teradata/i
+ if name =~ /teradata/i
require 'arjdbc/teradata'
true
end
View
@@ -6,4 +6,3 @@
# Load the java-specific parts
require 'arjdbc/teradata/teradata_java'
-
Oops, something went wrong.

0 comments on commit 69d7855

Please sign in to comment.