Method: Sequel::JDBC::Postgres::DatabaseMethods#copy_into

Defined in:
lib/sequel/adapters/jdbc/postgresql.rb

#copy_into(table, opts = OPTS) ⇒ Object

See Sequel::Postgres::Adapter#copy_into



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/sequel/adapters/jdbc/postgresql.rb', line 34

def copy_into(table, opts=OPTS)
  data = opts[:data]
  data = Array(data) if data.is_a?(String)

  if defined?(yield) && data
    raise Error, "Cannot provide both a :data option and a block to copy_into"
  elsif !defined?(yield) && !data
    raise Error, "Must provide either a :data option or a block to copy_into"
  end

  synchronize(opts[:server]) do |conn|
    begin
      copy_manager = Java::OrgPostgresqlCopy::CopyManager.new(conn)
      copier = copy_manager.copy_in(copy_into_sql(table, opts))
      if defined?(yield)
        while buf = yield
          java_bytes = buf.to_java_bytes
          copier.writeToCopy(java_bytes, 0, java_bytes.length)
        end
      else
        data.each do |d|
          java_bytes = d.to_java_bytes
          copier.writeToCopy(java_bytes, 0, java_bytes.length)
        end
      end
    rescue Exception => e
      copier.cancelCopy if copier
      raise
    ensure
      unless e
        begin
          copier.endCopy
        rescue NativeException => e2
          raise_error(e2)
        end
      end
    end
  end
end