Module: EnhancedSQLite3::SupportsInsertReturning::DatabaseStatements

Defined in:
lib/enhanced_sqlite3/supports_insert_returning.rb

Instance Method Summary collapse

Instance Method Details

#extract_table_ref_from_insert_sql(sql) ⇒ Object



49
50
51
52
53
# File 'lib/enhanced_sqlite3/supports_insert_returning.rb', line 49

def extract_table_ref_from_insert_sql(sql)
  if sql =~ /into\s("[A-Za-z0-9_."\[\]\s]+"|[A-Za-z0-9_."\[\]]+)\s*/im
    $1.strip
  end
end

#sql_for_insert(sql, pk, binds, returning) ⇒ Object

:nodoc:



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/enhanced_sqlite3/supports_insert_returning.rb', line 32

def sql_for_insert(sql, pk, binds, returning) # :nodoc:
  if supports_insert_returning?
    if pk.nil?
      # Extract the table from the insert sql. Yuck.
      table_ref = extract_table_ref_from_insert_sql(sql)
      pk = primary_key(table_ref) if table_ref
    end

    returning_columns = returning || Array(pk)

    returning_columns_statement = returning_columns.map { |c| quote_column_name(c) }.join(", ")
    sql = "#{sql} RETURNING #{returning_columns_statement}" if returning_columns.any?
  end

  [sql, binds]
end