class TableSaw::Queries::PreparedInsertStatement

Constants

Statement

Attributes

options[R]
table_name[R]

Public Class Methods

new(table_name, options: {}) click to toggle source
# File lib/table_saw/queries/prepared_insert_statement.rb, line 10
def initialize(table_name, options: {})
  @table_name = table_name
  @options = options
end

Public Instance Methods

call() click to toggle source
# File lib/table_saw/queries/prepared_insert_statement.rb, line 15
def call
  Statement.new(name, table_name, sql)
end

Private Instance Methods

column_names() click to toggle source
# File lib/table_saw/queries/prepared_insert_statement.rb, line 33
def column_names
  TableSaw.schema_cache.columns(table_name)
    .map { |column| TableSaw.schema_cache.connection.quote_column_name(column.name) }
    .join(', ')
end
column_types() click to toggle source
# File lib/table_saw/queries/prepared_insert_statement.rb, line 29
def column_types
  TableSaw.schema_cache.columns(table_name).map(&:sql_type_metadata).map(&:sql_type).join(', ')
end
conflict_statement() click to toggle source
# File lib/table_saw/queries/prepared_insert_statement.rb, line 49
def conflict_statement
  return unless options['ignore_conflict'] == 'true'

  'ON CONFLICT DO NOTHING'
end
name() click to toggle source
# File lib/table_saw/queries/prepared_insert_statement.rb, line 21
def name
  "#{table_name}_insert_plan"
end
prepare_statement() click to toggle source
# File lib/table_saw/queries/prepared_insert_statement.rb, line 43
      def prepare_statement
        <<~SQL.squish
          PREPARE #{name} (#{column_types}) AS INSERT INTO #{table_name} (#{column_names}) VALUES (#{values_clause})
        SQL
      end
sql() click to toggle source
# File lib/table_saw/queries/prepared_insert_statement.rb, line 25
def sql
  "#{[prepare_statement, conflict_statement].compact.join(' ')};"
end
values_clause() click to toggle source
# File lib/table_saw/queries/prepared_insert_statement.rb, line 39
def values_clause
  1.upto(TableSaw.schema_cache.columns(table_name).size).map { |i| "$#{i}" }.join(', ')
end