Skip to content

Commit

Permalink
Add returning kwarg to insert
Browse files Browse the repository at this point in the history
This is to accomodate rails commit rails/rails@3421e89

The trouble is primary keys are not returned with this code. The
Column#auto_incremented_by_db? method would have to return true for any
column with an associated sequence, or perhaps marked as the primary
key.
  • Loading branch information
andynu committed Mar 28, 2024
1 parent b92399b commit 4707dfa
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ def initialize(name, default, sql_type_metadata = nil, null = true, comment: nil
def virtual?
virtual
end

def auto_incremented_by_db?
# TODO: Identify if a column is the primary key and is auto-incremented (e.g. by a sequence)
super
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,22 +78,22 @@ def explain(arel, binds = [])

# New method in ActiveRecord 3.1
# Will add RETURNING clause in case of trigger generated primary keys
def sql_for_insert(sql, pk, binds)
def sql_for_insert(sql, pk, binds, _returning)
unless pk == false || pk.nil? || pk.is_a?(Array) || pk.is_a?(String)
sql = "#{sql} RETURNING #{quote_column_name(pk)} INTO :returning_id"
(binds = binds.dup) << ActiveRecord::Relation::QueryAttribute.new("returning_id", nil, Type::OracleEnhanced::Integer.new)
end
super
end

def insert(arel, name = nil, pk = nil, id_value = nil, sequence_name = nil, binds = [])
def insert(arel, name = nil, pk = nil, id_value = nil, sequence_name = nil, binds = [], returning: nil)
pk = nil if id_value
super
end

# New method in ActiveRecord 3.1
def exec_insert(sql, name = nil, binds = [], pk = nil, sequence_name = nil)
sql, binds = sql_for_insert(sql, pk, binds)
def exec_insert(sql, name = nil, binds = [], pk = nil, sequence_name = nil, returning: nil)
sql, binds = sql_for_insert(sql, pk, binds, returning)
type_casted_binds = type_casted_binds(binds)

log(sql, name, binds, type_casted_binds) do
Expand Down Expand Up @@ -164,6 +164,10 @@ def exec_update(sql, name = nil, binds = [])

alias :exec_delete :exec_update

def returning_column_values(result)
result.rows.first
end

def begin_db_transaction # :nodoc:
_connection.autocommit = false
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ def create_table_definition(name, **options)
OracleEnhanced::TableDefinition.new(self, name, **options)
end

def new_column_from_field(table_name, field)
def new_column_from_field(table_name, field, definitions)
limit, scale = field["limit"], field["scale"]
if limit || scale
field["sql_type"] += "(#{(limit || 38).to_i}" + ((scale = scale.to_i) > 0 ? ",#{scale})" : ")")
Expand Down

0 comments on commit 4707dfa

Please sign in to comment.