Skip to content

Commit

Permalink
Merge branch 'master' of github.com:brynary/arel
Browse files Browse the repository at this point in the history
* 'master' of github.com:brynary/arel:
  added missing method visit_Arel_Attributes_Decimal as alias for visit_Arel_Attributes_Attribute
  pass primary key name and value to ActiveRecord adapter insert method
  • Loading branch information
tenderlove committed Sep 28, 2010
2 parents 31e3033 + 6f8c4f1 commit 110f8f1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
12 changes: 10 additions & 2 deletions lib/arel/select_manager.rb
Expand Up @@ -163,9 +163,17 @@ def to_a # :nodoc:
# FIXME: this method should go away
def insert values
im = InsertManager.new @engine
im.into @ctx.froms
table = @ctx.froms
primary_key_name = (primary_key = table.primary_key) && primary_key.name
# FIXME: in AR tests values sometimes were Array and not Hash therefore is_a?(Hash) check is added
primary_key_value = primary_key && values.is_a?(Hash) && values[primary_key]
im.into table
im.insert values
@engine.connection.insert im.to_sql
# Oracle adapter needs primary key name to generate RETURNING ... INTO ... clause
# for tables which assign primary key value using trigger.
# RETURNING ... INTO ... clause will be added only if primary_key_value is nil
# therefore it is necessary to pass primary key value as well
@engine.connection.insert im.to_sql, 'AREL', primary_key_name, primary_key_value
end

private
Expand Down
6 changes: 5 additions & 1 deletion lib/arel/table.rb
Expand Up @@ -27,7 +27,11 @@ def initialize name, engine = Table.engine
end

def primary_key
@primary_key ||= self[@engine.connection.primary_key(name)]
@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
Expand Down
1 change: 1 addition & 0 deletions lib/arel/visitors/to_sql.rb
Expand Up @@ -234,6 +234,7 @@ def visit_Arel_Attributes_Attribute o
"#{quote_table_name join_name}.#{quote_column_name o.name}"
end
alias :visit_Arel_Attributes_Integer :visit_Arel_Attributes_Attribute
alias :visit_Arel_Attributes_Decimal :visit_Arel_Attributes_Attribute
alias :visit_Arel_Attributes_String :visit_Arel_Attributes_Attribute
alias :visit_Arel_Attributes_Time :visit_Arel_Attributes_Attribute
alias :visit_Arel_Attributes_Boolean :visit_Arel_Attributes_Attribute
Expand Down

0 comments on commit 110f8f1

Please sign in to comment.