diff options
Diffstat (limited to 'lib/arel')
-rw-r--r-- | lib/arel/table.rb | 14 | ||||
-rw-r--r-- | lib/arel/visitors/mssql.rb | 21 |
2 files changed, 18 insertions, 17 deletions
diff --git a/lib/arel/table.rb b/lib/arel/table.rb index 01d4561ff1..fb86c03404 100644 --- a/lib/arel/table.rb +++ b/lib/arel/table.rb @@ -17,7 +17,6 @@ module Arel @columns = nil @aliases = [] @table_alias = nil - @primary_key = nil if Hash === engine @engine = engine[:engine] || Table.engine @@ -29,19 +28,6 @@ module Arel end end - def primary_key - if $VERBOSE - warn <<-eowarn -primary_key (#{caller.first}) is deprecated and will be removed in Arel 4.0.0 - eowarn - end - @primary_key ||= begin - primary_key_name = @engine.connection.primary_key(name) - # some tables might be without primary key - primary_key_name && self[primary_key_name] - end - end - def alias name = "#{self.name}_2" Nodes::TableAlias.new(self, name).tap do |node| @aliases << node diff --git a/lib/arel/visitors/mssql.rb b/lib/arel/visitors/mssql.rb index 0e5b75ec59..7c65ad33f2 100644 --- a/lib/arel/visitors/mssql.rb +++ b/lib/arel/visitors/mssql.rb @@ -3,6 +3,11 @@ module Arel class MSSQL < Arel::Visitors::ToSql RowNumber = Struct.new :children + def initialize(*) + @primary_keys = {} + super + end + private # `top` wouldn't really work here. I.e. User.select("distinct first_name").limit(10) would generate @@ -81,10 +86,20 @@ module Arel end # FIXME raise exception of there is no pk? - # FIXME!! Table.primary_key will be deprecated. What is the replacement?? def find_left_table_pk o - return o.primary_key if o.instance_of? Arel::Table - find_left_table_pk o.left if o.kind_of? Arel::Nodes::Join + if o.kind_of?(Arel::Nodes::Join) + find_left_table_pk(o.left) + elsif o.instance_of?(Arel::Table) + find_primary_key(o) + end + end + + def find_primary_key(o) + @primary_keys[o.name] ||= begin + primary_key_name = @connection.primary_key(o.name) + # some tables might be without primary key + primary_key_name && o[primary_key_name] + end end end end |