module Sequel::JDBC::Postgres::DatabaseMethods
Public Class Methods
Source
# File lib/sequel/adapters/jdbc/postgresql.rb 22 def self.extended(db) 23 super 24 db.send(:initialize_postgres_adapter) 25 end
Add the primary_keys and primary_key_sequences instance variables, so we can get the correct return values for inserted rows.
Public Instance Methods
Source
# File lib/sequel/adapters/jdbc/postgresql.rb 28 def add_conversion_proc(oid, *) 29 super 30 Sequel.synchronize{@oid_convertor_map.delete(oid)} 31 end
Remove any current entry for the oid in the oid_convertor_map.
Sequel::Postgres::DatabaseMethods#add_conversion_proc
Source
# File lib/sequel/adapters/jdbc/postgresql.rb 34 def copy_into(table, opts=OPTS) 35 data = opts[:data] 36 data = Array(data) if data.is_a?(String) 37 38 if defined?(yield) && data 39 raise Error, "Cannot provide both a :data option and a block to copy_into" 40 elsif !defined?(yield) && !data 41 raise Error, "Must provide either a :data option or a block to copy_into" 42 end 43 44 synchronize(opts[:server]) do |conn| 45 begin 46 copy_manager = Java::OrgPostgresqlCopy::CopyManager.new(conn) 47 copier = copy_manager.copy_in(copy_into_sql(table, opts)) 48 if defined?(yield) 49 while buf = yield 50 java_bytes = buf.to_java_bytes 51 copier.writeToCopy(java_bytes, 0, java_bytes.length) 52 end 53 else 54 data.each do |d| 55 java_bytes = d.to_java_bytes 56 copier.writeToCopy(java_bytes, 0, java_bytes.length) 57 end 58 end 59 rescue Exception => e 60 copier.cancelCopy if copier 61 raise 62 ensure 63 unless e 64 begin 65 copier.endCopy 66 rescue NativeException => e2 67 raise_error(e2) 68 end 69 end 70 end 71 end 72 end
See Sequel::Postgres::Adapter#copy_into
Source
# File lib/sequel/adapters/jdbc/postgresql.rb 75 def copy_table(table, opts=OPTS) 76 synchronize(opts[:server]) do |conn| 77 copy_manager = Java::OrgPostgresqlCopy::CopyManager.new(conn) 78 sql = copy_table_sql(table, opts) 79 80 begin 81 copier = copy_manager.copy_out(sql) 82 rescue *DATABASE_ERROR_CLASSES => e 83 raise_error(e) 84 end 85 86 begin 87 if defined?(yield) 88 while buf = copier.readFromCopy 89 yield(String.from_java_bytes(buf)) 90 end 91 nil 92 else 93 b = String.new 94 while buf = copier.readFromCopy 95 b << String.from_java_bytes(buf) 96 end 97 b 98 end 99 rescue => e 100 raise_error(e, :disconnect=>true) 101 ensure 102 if buf && !e 103 raise DatabaseDisconnectError, "disconnecting as a partial COPY may leave the connection in an unusable state" 104 end 105 end 106 end 107 end
See Sequel::Postgres::Adapter#copy_table
Source
# File lib/sequel/adapters/jdbc/postgresql.rb 109 def oid_convertor_proc(oid) 110 if (conv = Sequel.synchronize{@oid_convertor_map[oid]}).nil? 111 conv = if pr = conversion_procs[oid] 112 lambda do |r, i| 113 if v = r.getString(i) 114 pr.call(v) 115 end 116 end 117 else 118 false 119 end 120 Sequel.synchronize{@oid_convertor_map[oid] = conv} 121 end 122 conv 123 end
Private Instance Methods
Source
# File lib/sequel/adapters/jdbc/postgresql.rb 134 def bound_variable_arg(arg, conn) 135 nil 136 end
For PostgreSQL-specific types, return the string that should be used as the PGObject value. Returns nil by default, loading pg_* extensions will override this to add support for specific types.
Source
# File lib/sequel/adapters/jdbc/postgresql.rb 127 def disconnect_error?(exception, opts) 128 super || exception.message =~ /\A(This connection has been closed\.|FATAL: terminating connection due to administrator command|An I\/O error occurred while sending to the backend\.)\z/ 129 end
Source
# File lib/sequel/adapters/jdbc/postgresql.rb 145 def prepare_jdbc_statement(conn, sql, opts) 146 ps = super 147 unless opts[:name] 148 ps.prepare_threshold = 0 149 end 150 ps 151 end
Work around issue when using Sequel’s bound variable support where the same SQL is used in different bound variable calls, but the schema has changed between the calls. This is necessary as jdbc-postgres versions after 9.4.1200 violate the JDBC API. These versions cache separate PreparedStatement instances, which are eventually prepared server side after the prepareThreshold is met. The JDBC API violation is that PreparedStatement#close does not release the server side prepared statement.
Source
# File lib/sequel/adapters/jdbc/postgresql.rb 156 def set_ps_arg(cps, arg, i) 157 unless v = bound_variable_arg(arg, nil) 158 v = arg.strftime("%F %T.%6N%z") if arg.is_a?(DateTime) 159 end 160 161 if v 162 obj = Java::OrgPostgresqlUtil::PGobject.new 163 obj.setType("unknown") 164 obj.setValue(v) 165 cps.setObject(i, obj) 166 else 167 super 168 end 169 end
If the given argument is a recognized PostgreSQL-specific type, create a PGObject instance with unknown type and the bound argument string value, and set that as the prepared statement argument.
Source
# File lib/sequel/adapters/jdbc/postgresql.rb 173 def set_ps_arg_nil(cps, i) 174 cps.setNull(i, JavaSQL::Types::NULL) 175 end
Use setNull for nil arguments as the default behavior of setString with nil doesn’t appear to work correctly on PostgreSQL.
Source
# File lib/sequel/adapters/jdbc/postgresql.rb 178 def setup_connection_with_opts(conn, opts) 179 conn = super 180 statement(conn) do |stmt| 181 connection_configuration_sqls(opts).each{|sql| log_connection_yield(sql, conn){stmt.execute(sql)}} 182 end 183 conn 184 end
Execute the connection configuration SQL queries on the connection.
Source
# File lib/sequel/adapters/jdbc/postgresql.rb 186 def setup_type_convertor_map 187 super 188 @oid_convertor_map = {} 189 end