# File lib/postgres_upsert/write_adapters/table_adapter.rb, line 22 def primary_key @primary_key ||= begin query = <<-SELECT_KEY SELECT pg_attribute.attname, format_type(pg_attribute.atttypid, pg_attribute.atttypmod) FROM pg_index, pg_class, pg_attribute WHERE pg_class.oid = '#{@destination}'::regclass AND indrelid = pg_class.oid AND pg_attribute.attrelid = pg_class.oid AND pg_attribute.attnum = any(pg_index.indkey) AND indisprimary SELECT_KEY pg_result = ActiveRecord::Base.connection.execute query pg_result.each { |row| return row['attname'] } end end
class PostgresUpsert::WriteAdapters::TableAdapter
Public Class Methods
new(destination, options)
click to toggle source
# File lib/postgres_upsert/write_adapters/table_adapter.rb, line 4 def initialize(destination, options) @destination = destination @options = sanitize_options(options) end
Public Instance Methods
column_names()
click to toggle source
# File lib/postgres_upsert/write_adapters/table_adapter.rb, line 42 def column_names @column_names ||= begin query = "SELECT * FROM information_schema.columns WHERE TABLE_NAME = '#{@destination}'" pg_result = ActiveRecord::Base.connection.execute query pg_result.map { |row| row['column_name'] } end end
database_connection()
click to toggle source
# File lib/postgres_upsert/write_adapters/table_adapter.rb, line 18 def database_connection ActiveRecord::Base.connection end
primary_key()
click to toggle source
quoted_table_name()
click to toggle source
# File lib/postgres_upsert/write_adapters/table_adapter.rb, line 50 def quoted_table_name @quoted_table_name ||= database_connection.quote_table_name(@destination) end
sanitize_options(options)
click to toggle source
# File lib/postgres_upsert/write_adapters/table_adapter.rb, line 9 def sanitize_options(options) options.slice( :delimiter, :unique_key ).reverse_merge( delimiter: ',', unique_key: [primary_key], ) end