class Sequel::JDBC::Database

  1. lib/sequel/adapters/jdbc.rb
Superclass: Sequel::Database

Public Instance Aliases

execute_dui -> execute

Attributes

basic_type_convertor_map [R]

Map of JDBC type ids to callable objects that return appropriate ruby or java values.

convert_types [RW]

Whether to convert some Java types to ruby types when retrieving rows. True by default, can be set to false to roughly double performance when fetching rows.

driver [R]

The Java database driver we are using (should be a Java class)

fetch_size [RW]

The fetch size to use for JDBC Statement objects created by this database. By default, this is nil so a fetch size is not set explicitly.

type_convertor_map [R]

Map of JDBC type ids to callable objects that return appropriate ruby values.

Public Instance methods

call_sproc(name, opts = OPTS)

Execute the given stored procedure with the give name. If a block is given, the stored procedure should return rows.

[show source]
    # File lib/sequel/adapters/jdbc.rb
187 def call_sproc(name, opts = OPTS)
188   args = opts[:args] || []
189   sql = "{call #{name}(#{args.map{'?'}.join(',')})}"
190   synchronize(opts[:server]) do |conn|
191     begin
192       cps = conn.prepareCall(sql)
193 
194       i = 0
195       args.each{|arg| set_ps_arg(cps, arg, i+=1)}
196 
197       if defined?(yield)
198         yield log_connection_yield(sql, conn){cps.executeQuery}
199       else
200         log_connection_yield(sql, conn){cps.executeUpdate}
201         if opts[:type] == :insert
202           last_insert_id(conn, opts)
203         end
204       end
205     rescue *DATABASE_ERROR_CLASSES => e
206       raise_error(e)
207     ensure
208       cps.close if cps
209     end
210   end
211 end
connect(server)

Connect to the database using JavaSQL::DriverManager.getConnection, and falling back to driver.new.connect if the driver is known.

[show source]
    # File lib/sequel/adapters/jdbc.rb
215 def connect(server)
216   opts = server_opts(server)
217   conn = if jndi?
218     get_connection_from_jndi
219   else
220     args = [uri(opts)]
221     args.concat([opts[:user], opts[:password]]) if opts[:user] && opts[:password]
222     begin
223       JavaSQL::DriverManager.setLoginTimeout(opts[:login_timeout]) if opts[:login_timeout]
224       raise StandardError, "skipping regular connection" if opts[:jdbc_properties]
225       JavaSQL::DriverManager.getConnection(*args)
226     rescue StandardError, *DATABASE_ERROR_CLASSES => e
227       raise e unless driver
228       # If the DriverManager can't get the connection - use the connect
229       # method of the driver. (This happens under Tomcat for instance)
230       props = java.util.Properties.new
231       if opts && opts[:user] && opts[:password]
232         props.setProperty("user", opts[:user])
233         props.setProperty("password", opts[:password])
234       end
235       opts[:jdbc_properties].each{|k,v| props.setProperty(k.to_s, v)} if opts[:jdbc_properties]
236       begin
237         c = driver.new.connect(args[0], props)
238         raise(Sequel::DatabaseError, 'driver.new.connect returned nil: probably bad JDBC connection string') unless c
239         c
240       rescue StandardError, *DATABASE_ERROR_CLASSES => e2
241         if e2.respond_to?(:message=) && e2.message != e.message
242           e2.message = "#{e2.message}\n#{e.class.name}: #{e.message}"
243         end
244         raise e2
245       end
246     end
247   end
248   setup_connection_with_opts(conn, opts)
249 end
disconnect_connection(c)

Close given adapter connections, and delete any related prepared statements.

[show source]
    # File lib/sequel/adapters/jdbc.rb
252 def disconnect_connection(c)
253   @connection_prepared_statements_mutex.synchronize{@connection_prepared_statements.delete(c)}
254   c.close
255 end
execute(sql, opts=OPTS, &block)
[show source]
    # File lib/sequel/adapters/jdbc.rb
257 def execute(sql, opts=OPTS, &block)
258   return call_sproc(sql, opts, &block) if opts[:sproc]
259   return execute_prepared_statement(sql, opts, &block) if [Symbol, Dataset].any?{|c| sql.is_a?(c)}
260   synchronize(opts[:server]) do |conn|
261     statement(conn) do |stmt|
262       if block
263         if size = fetch_size
264           stmt.setFetchSize(size)
265         end
266         yield log_connection_yield(sql, conn){stmt.executeQuery(sql)}
267       else
268         case opts[:type]
269         when :ddl
270           log_connection_yield(sql, conn){stmt.execute(sql)}
271         when :insert
272           log_connection_yield(sql, conn){execute_statement_insert(stmt, sql)}
273           opts = Hash[opts]
274           opts[:stmt] = stmt
275           last_insert_id(conn, opts)
276         else
277           log_connection_yield(sql, conn){stmt.executeUpdate(sql)}
278         end
279       end
280     end
281   end
282 end
execute_ddl(sql, opts=OPTS)
[show source]
    # File lib/sequel/adapters/jdbc.rb
285 def execute_ddl(sql, opts=OPTS)
286   opts = Hash[opts]
287   opts[:type] = :ddl
288   execute(sql, opts)
289 end
execute_insert(sql, opts=OPTS)
[show source]
    # File lib/sequel/adapters/jdbc.rb
291 def execute_insert(sql, opts=OPTS)
292   opts = Hash[opts]
293   opts[:type] = :insert
294   execute(sql, opts)
295 end
foreign_key_list(table, opts=OPTS)

Use the JDBC metadata to get a list of foreign keys for the table.

[show source]
    # File lib/sequel/adapters/jdbc.rb
304 def foreign_key_list(table, opts=OPTS)
305   m = output_identifier_meth
306   schema, table = metadata_schema_and_table(table, opts)
307   foreign_keys = {}
308   metadata(:getImportedKeys, nil, schema, table) do |r|
309     if fk = foreign_keys[r[:fk_name]]
310       fk[:columns] << [r[:key_seq], m.call(r[:fkcolumn_name])]
311       fk[:key] << [r[:key_seq], m.call(r[:pkcolumn_name])]
312     elsif r[:fk_name]
313       foreign_keys[r[:fk_name]] = {:name=>m.call(r[:fk_name]), :columns=>[[r[:key_seq], m.call(r[:fkcolumn_name])]], :table=>m.call(r[:pktable_name]), :key=>[[r[:key_seq], m.call(r[:pkcolumn_name])]]}
314     end
315   end
316   foreign_keys.values.each do |fk|
317     [:columns, :key].each do |k|
318       fk[k] = fk[k].sort.map{|_, v| v}
319     end
320   end
321 end
freeze()
[show source]
    # File lib/sequel/adapters/jdbc.rb
297 def freeze
298   @type_convertor_map.freeze
299   @basic_type_convertor_map.freeze
300   super
301 end
indexes(table, opts=OPTS)

Use the JDBC metadata to get the index information for the table.

[show source]
    # File lib/sequel/adapters/jdbc.rb
324 def indexes(table, opts=OPTS)
325   m = output_identifier_meth
326   schema, table = metadata_schema_and_table(table, opts)
327   indexes = {}
328   metadata(:getIndexInfo, nil, schema, table, false, true) do |r|
329     next unless name = r[:column_name]
330     next if respond_to?(:primary_key_index_re, true) and r[:index_name] =~ primary_key_index_re 
331     i = indexes[m.call(r[:index_name])] ||= {:columns=>[], :unique=>[false, 0].include?(r[:non_unique])}
332     i[:columns] << m.call(name)
333   end
334   indexes
335 end
jndi?()

Whether or not JNDI is being used for this connection.

[show source]
    # File lib/sequel/adapters/jdbc.rb
338 def jndi?
339   !!(uri =~ JNDI_URI_REGEXP)
340 end
tables(opts=OPTS)

All tables in this database

[show source]
    # File lib/sequel/adapters/jdbc.rb
343 def tables(opts=OPTS)
344   get_tables('TABLE', opts)
345 end
uri(opts=OPTS)

The uri for this connection. You can specify the uri using the :uri, :url, or :database options. You don’t need to worry about this if you use Sequel.connect with the JDBC connectrion strings.

[show source]
    # File lib/sequel/adapters/jdbc.rb
351 def uri(opts=OPTS)
352   opts = @opts.merge(opts)
353   ur = opts[:uri] || opts[:url] || opts[:database]
354   ur =~ /^\Ajdbc:/ ? ur : "jdbc:#{ur}"
355 end
views(opts=OPTS)

All views in this database

[show source]
    # File lib/sequel/adapters/jdbc.rb
358 def views(opts=OPTS)
359   get_tables('VIEW', opts)
360 end