Class Sequel::JDBC::Database
In: lib/sequel/adapters/jdbc/db2.rb
lib/sequel/adapters/jdbc.rb
Parent: Object

JDBC Databases offer a fairly uniform interface that does not change much based on the sub adapter.

Methods

External Aliases

schema_parse_table -> jdbc_schema_parse_table
  Alias the generic JDBC versions so they can be called directly later
tables -> jdbc_tables
views -> jdbc_views
indexes -> jdbc_indexes

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.
database_type  [R]  The type of database we are connecting to
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

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

[Source]

     # File lib/sequel/adapters/jdbc.rb, line 170
170:       def call_sproc(name, opts = OPTS)
171:         args = opts[:args] || []
172:         sql = "{call #{name}(#{args.map{'?'}.join(',')})}"
173:         synchronize(opts[:server]) do |conn|
174:           cps = conn.prepareCall(sql)
175: 
176:           i = 0
177:           args.each{|arg| set_ps_arg(cps, arg, i+=1)}
178: 
179:           begin
180:             if block_given?
181:               yield log_connection_yield(sql, conn){cps.executeQuery}
182:             else
183:               case opts[:type]
184:               when :insert
185:                 log_connection_yield(sql, conn){cps.executeUpdate}
186:                 last_insert_id(conn, opts)
187:               else
188:                 log_connection_yield(sql, conn){cps.executeUpdate}
189:               end
190:             end
191:           rescue NativeException, JavaSQL::SQLException => e
192:             raise_error(e)
193:           ensure
194:             cps.close
195:           end
196:         end
197:       end

Connect to the database using JavaSQL::DriverManager.getConnection.

[Source]

     # File lib/sequel/adapters/jdbc.rb, line 200
200:       def connect(server)
201:         opts = server_opts(server)
202:         conn = if jndi?
203:           get_connection_from_jndi
204:         else
205:           args = [uri(opts)]
206:           args.concat([opts[:user], opts[:password]]) if opts[:user] && opts[:password]
207:           begin
208:             JavaSQL::DriverManager.setLoginTimeout(opts[:login_timeout]) if opts[:login_timeout]
209:             raise StandardError, "skipping regular connection" if opts[:jdbc_properties]
210:             JavaSQL::DriverManager.getConnection(*args)
211:           rescue JavaSQL::SQLException, NativeException, StandardError => e
212:             raise e unless driver
213:             # If the DriverManager can't get the connection - use the connect
214:             # method of the driver. (This happens under Tomcat for instance)
215:             props = java.util.Properties.new
216:             if opts && opts[:user] && opts[:password]
217:               props.setProperty("user", opts[:user])
218:               props.setProperty("password", opts[:password])
219:             end
220:             opts[:jdbc_properties].each{|k,v| props.setProperty(k.to_s, v)} if opts[:jdbc_properties]
221:             begin
222:               c = driver.new.connect(args[0], props)
223:               raise(Sequel::DatabaseError, 'driver.new.connect returned nil: probably bad JDBC connection string') unless c
224:               c
225:             rescue JavaSQL::SQLException, NativeException, StandardError => e2
226:               unless e2.message == e.message
227:                 e2.message = "#{e2.message}\n#{e.class.name}: #{e.message}"
228:               end
229:               raise e2
230:             end
231:           end
232:         end
233:         setup_connection(conn)
234:       end

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

[Source]

     # File lib/sequel/adapters/jdbc.rb, line 237
237:       def disconnect_connection(c)
238:         @connection_prepared_statements_mutex.synchronize{@connection_prepared_statements.delete(c)}
239:         c.close
240:       end

Execute the given SQL. If a block is given, if should be a SELECT statement or something else that returns rows.

[Source]

     # File lib/sequel/adapters/jdbc.rb, line 244
244:       def execute(sql, opts=OPTS, &block)
245:         return call_sproc(sql, opts, &block) if opts[:sproc]
246:         return execute_prepared_statement(sql, opts, &block) if [Symbol, Dataset].any?{|c| sql.is_a?(c)}
247:         synchronize(opts[:server]) do |conn|
248:           statement(conn) do |stmt|
249:             if block
250:               if size = fetch_size
251:                 stmt.setFetchSize(size)
252:               end
253:               yield log_connection_yield(sql, conn){stmt.executeQuery(sql)}
254:             else
255:               case opts[:type]
256:               when :ddl
257:                 log_connection_yield(sql, conn){stmt.execute(sql)}
258:               when :insert
259:                 log_connection_yield(sql, conn){execute_statement_insert(stmt, sql)}
260:                 last_insert_id(conn, Hash[opts].merge!(:stmt=>stmt))
261:               else
262:                 log_connection_yield(sql, conn){stmt.executeUpdate(sql)}
263:               end
264:             end
265:           end
266:         end
267:       end

Execute the given DDL SQL, which should not return any values or rows.

[Source]

     # File lib/sequel/adapters/jdbc.rb, line 272
272:       def execute_ddl(sql, opts=OPTS)
273:         opts = Hash[opts]
274:         opts[:type] = :ddl
275:         execute(sql, opts)
276:       end
execute_dui(sql, opts=OPTS, &block)

Alias for execute

Execute the given INSERT SQL, returning the last inserted row id.

[Source]

     # File lib/sequel/adapters/jdbc.rb, line 280
280:       def execute_insert(sql, opts=OPTS)
281:         opts = Hash[opts]
282:         opts[:type] = :insert
283:         execute(sql, opts)
284:       end

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

[Source]

     # File lib/sequel/adapters/jdbc.rb, line 287
287:       def foreign_key_list(table, opts=OPTS)
288:         m = output_identifier_meth
289:         schema, table = metadata_schema_and_table(table, opts)
290:         foreign_keys = {}
291:         metadata(:getImportedKeys, nil, schema, table) do |r|
292:           if fk = foreign_keys[r[:fk_name]]
293:             fk[:columns] << [r[:key_seq], m.call(r[:fkcolumn_name])]
294:             fk[:key] << [r[:key_seq], m.call(r[:pkcolumn_name])]
295:           elsif r[:fk_name]
296:             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])]]}
297:           end
298:         end
299:         foreign_keys.values.each do |fk|
300:           [:columns, :key].each do |k|
301:             fk[k] = fk[k].sort.map{|_, v| v}
302:           end
303:         end
304:       end

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

[Source]

     # File lib/sequel/adapters/jdbc.rb, line 307
307:       def indexes(table, opts=OPTS)
308:         m = output_identifier_meth
309:         schema, table = metadata_schema_and_table(table, opts)
310:         indexes = {}
311:         metadata(:getIndexInfo, nil, schema, table, false, true) do |r|
312:           next unless name = r[:column_name]
313:           next if respond_to?(:primary_key_index_re, true) and r[:index_name] =~ primary_key_index_re 
314:           i = indexes[m.call(r[:index_name])] ||= {:columns=>[], :unique=>[false, 0].include?(r[:non_unique])}
315:           i[:columns] << m.call(name)
316:         end
317:         indexes
318:       end

Whether or not JNDI is being used for this connection.

[Source]

     # File lib/sequel/adapters/jdbc.rb, line 321
321:       def jndi?
322:         !!(uri =~ JNDI_URI_REGEXP)
323:       end

All tables in this database

[Source]

     # File lib/sequel/adapters/jdbc.rb, line 326
326:       def tables(opts=OPTS)
327:         get_tables('TABLE', opts)
328:       end

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.

[Source]

     # File lib/sequel/adapters/jdbc.rb, line 334
334:       def uri(opts=OPTS)
335:         opts = @opts.merge(opts)
336:         ur = opts[:uri] || opts[:url] || opts[:database]
337:         ur =~ /^\Ajdbc:/ ? ur : "jdbc:#{ur}"
338:       end

All views in this database

[Source]

     # File lib/sequel/adapters/jdbc.rb, line 341
341:       def views(opts=OPTS)
342:         get_tables('VIEW', opts)
343:       end

[Validate]