Class: Insert

Inherits:
Object
  • Object
show all
Defined in:
lib/insert.rb,
lib/insert/version.rb

Constant Summary collapse

VERSION =
"0.0.3"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ Insert

Returns a new instance of Insert.



7
8
9
10
11
# File 'lib/insert.rb', line 7

def initialize(connection)
  connection.is_a?(PG::Connection) or raise ArgumentError, "expected PG::Connection, got #{connection.inspect}"
  @connection = connection
  @statements = {}
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



6
7
8
# File 'lib/insert.rb', line 6

def connection
  @connection
end

Instance Method Details

#deallocateObject



22
23
24
25
26
27
28
# File 'lib/insert.rb', line 22

def deallocate
  raise "can't deallocate if already deallocated!" if @deallocated
  @deallocated = true
  @statements.each do |_, name|
    connection.exec "DEALLOCATE #{name}"
  end
end

#insert(quoted_table_name, attrs) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/insert.rb', line 13

def insert(quoted_table_name, attrs)
  raise "can't insert if already deallocated!" if @deallocated
  attrs = attrs.map do |k, v|
    [ k.to_s, v ]
  end.sort_by { |k, _| k }
  attrs = Hash[attrs]
  connection.exec_prepared statement_for(quoted_table_name, attrs.keys), attrs.values
end