Class: Sequel::JDBC::Database
- Defined in:
- lib/sequel/adapters/jdbc.rb,
lib/sequel/adapters/jdbc/db2.rb,
lib/sequel/adapters/jdbc/mssql.rb
Overview
JDBC Databases offer a fairly uniform interface that does not change much based on the sub adapter.
Constant Summary collapse
- DatasetClass =
self
Constants inherited from Database
Database::ADAPTERS, Database::AUTOINCREMENT, Database::COLUMN_DEFINITION_ORDER, Database::COLUMN_SCHEMA_DATETIME_TYPES, Database::COLUMN_SCHEMA_STRING_TYPES, Database::COMBINABLE_ALTER_TABLE_OPS, Database::COMMA_SEPARATOR, Database::CURRENT_TIMESTAMP_RE, Database::DEFAULT_DATABASE_ERROR_REGEXPS, Database::DEFAULT_JOIN_TABLE_COLUMN_OPTIONS, Database::DEFAULT_STRING_COLUMN_SIZE, Database::EXTENSIONS, Database::NOT_NULL, Database::NULL, Database::OPTS, Database::PRIMARY_KEY, Database::SCHEMA_TYPE_CLASSES, Database::SQL_BEGIN, Database::SQL_COMMIT, Database::SQL_RELEASE_SAVEPOINT, Database::SQL_ROLLBACK, Database::SQL_ROLLBACK_TO_SAVEPOINT, Database::SQL_SAVEPOINT, Database::STRING_DEFAULT_RE, Database::TEMPORARY, Database::TRANSACTION_BEGIN, Database::TRANSACTION_COMMIT, Database::TRANSACTION_ISOLATION_LEVELS, Database::TRANSACTION_ROLLBACK, Database::UNDERSCORE, Database::UNIQUE, Database::UNSIGNED
Instance Attribute Summary collapse
-
#basic_type_convertor_map ⇒ Object
readonly
Map of JDBC type ids to callable objects that return appropriate ruby or java values.
-
#convert_types ⇒ Object
Whether to convert some Java types to ruby types when retrieving rows.
-
#database_type ⇒ Object
readonly
The type of database we are connecting to.
-
#driver ⇒ Object
readonly
The Java database driver we are using (should be a Java class).
-
#fetch_size ⇒ Object
The fetch size to use for JDBC Statement objects created by this database.
-
#type_convertor_map ⇒ Object
readonly
Map of JDBC type ids to callable objects that return appropriate ruby values.
Attributes inherited from Database
#cache_schema, #dataset_class, #default_string_column_size, #identifier_input_method, #identifier_output_method, #log_warn_duration, #loggers, #opts, #pool, #prepared_statements, #sql_log_level, #timezone, #transaction_isolation_level
Instance Method Summary collapse
-
#call_sproc(name, opts = OPTS) ⇒ Object
Execute the given stored procedure with the give name.
-
#connect(server) ⇒ Object
Connect to the database using JavaSQL::DriverManager.getConnection.
-
#disconnect_connection(c) ⇒ Object
Close given adapter connections, and delete any related prepared statements.
-
#execute(sql, opts = OPTS, &block) ⇒ Object
(also: #execute_dui)
Execute the given SQL.
-
#execute_ddl(sql, opts = OPTS) ⇒ Object
Execute the given DDL SQL, which should not return any values or rows.
-
#execute_insert(sql, opts = OPTS) ⇒ Object
Execute the given INSERT SQL, returning the last inserted row id.
-
#foreign_key_list(table, opts = OPTS) ⇒ Object
Use the JDBC metadata to get a list of foreign keys for the table.
-
#indexes(table, opts = OPTS) ⇒ Object
(also: #jdbc_indexes)
Use the JDBC metadata to get the index information for the table.
-
#jndi? ⇒ Boolean
Whether or not JNDI is being used for this connection.
-
#tables(opts = OPTS) ⇒ Object
(also: #jdbc_tables)
All tables in this database.
-
#uri(opts = OPTS) ⇒ Object
The uri for this connection.
-
#views(opts = OPTS) ⇒ Object
(also: #jdbc_views)
All views in this database.
Methods inherited from Database
#<<, #[], adapter_class, adapter_scheme, #adapter_scheme, #add_column, #add_index, #add_servers, #after_commit, after_initialize, #after_rollback, #alter_table, #alter_table_generator, #call, #cast_type_literal, connect, #create_join_table, #create_join_table!, #create_join_table?, #create_or_replace_view, #create_table, #create_table!, #create_table?, #create_table_generator, #create_view, #dataset, #disconnect, #drop_column, #drop_index, #drop_join_table, #drop_table, #drop_table?, #drop_view, #each_server, #extend_datasets, extension, #extension, #fetch, #from, #from_application_timestamp, #get, #global_index_namespace?, #in_transaction?, #initialize, #inspect, #literal, #literal_symbol, #literal_symbol_set, #log_exception, #log_info, #log_yield, #logger=, #prepared_statement, #quote_identifier, #quote_identifiers=, #quote_identifiers?, register_extension, #remove_servers, #rename_column, #rename_table, #run, run_after_initialize, #schema, #schema_type_class, #select, #serial_primary_key_options, #servers, #set_column_default, #set_column_type, #set_prepared_statement, #single_threaded?, #supports_create_table_if_not_exists?, #supports_deferrable_constraints?, #supports_deferrable_foreign_key_constraints?, #supports_drop_table_if_exists?, #supports_foreign_key_parsing?, #supports_index_parsing?, #supports_partial_indexes?, #supports_prepared_transactions?, #supports_savepoints?, #supports_savepoints_in_prepared_transactions?, #supports_schema_parsing?, #supports_table_listing?, #supports_transaction_isolation_levels?, #supports_transactional_ddl?, #supports_view_listing?, #supports_views_with_check_option?, #supports_views_with_local_check_option?, #synchronize, #table_exists?, #test_connection, #to_application_timestamp, #transaction, #typecast_value, #url, #valid_connection?
Methods included from Metaprogramming
Constructor Details
This class inherits a constructor from Sequel::Database
Instance Attribute Details
#basic_type_convertor_map ⇒ Object (readonly)
Map of JDBC type ids to callable objects that return appropriate ruby or java values.
287 288 289 |
# File 'lib/sequel/adapters/jdbc.rb', line 287 def basic_type_convertor_map @basic_type_convertor_map end |
#convert_types ⇒ Object
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.
277 278 279 |
# File 'lib/sequel/adapters/jdbc.rb', line 277 def convert_types @convert_types end |
#database_type ⇒ Object (readonly)
The type of database we are connecting to
269 270 271 |
# File 'lib/sequel/adapters/jdbc.rb', line 269 def database_type @database_type end |
#driver ⇒ Object (readonly)
The Java database driver we are using (should be a Java class)
272 273 274 |
# File 'lib/sequel/adapters/jdbc.rb', line 272 def driver @driver end |
#fetch_size ⇒ Object
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.
281 282 283 |
# File 'lib/sequel/adapters/jdbc.rb', line 281 def fetch_size @fetch_size end |
#type_convertor_map ⇒ Object (readonly)
Map of JDBC type ids to callable objects that return appropriate ruby values.
284 285 286 |
# File 'lib/sequel/adapters/jdbc.rb', line 284 def type_convertor_map @type_convertor_map end |
Instance Method Details
#call_sproc(name, opts = OPTS) ⇒ Object
Execute the given stored procedure with the give name. If a block is given, the stored procedure should return rows.
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/sequel/adapters/jdbc.rb', line 291 def call_sproc(name, opts = OPTS) args = opts[:args] || [] sql = "{call #{name}(#{args.map{'?'}.join(',')})}" synchronize(opts[:server]) do |conn| cps = conn.prepareCall(sql) i = 0 args.each{|arg| set_ps_arg(cps, arg, i+=1)} begin if block_given? yield log_yield(sql){cps.executeQuery} else case opts[:type] when :insert log_yield(sql){cps.executeUpdate} last_insert_id(conn, opts) else log_yield(sql){cps.executeUpdate} end end rescue NativeException, JavaSQL::SQLException => e raise_error(e) ensure cps.close end end end |
#connect(server) ⇒ Object
Connect to the database using JavaSQL::DriverManager.getConnection.
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
# File 'lib/sequel/adapters/jdbc.rb', line 321 def connect(server) opts = server_opts(server) conn = if jndi? get_connection_from_jndi else args = [uri(opts)] args.concat([opts[:user], opts[:password]]) if opts[:user] && opts[:password] begin JavaSQL::DriverManager.setLoginTimeout(opts[:login_timeout]) if opts[:login_timeout] JavaSQL::DriverManager.getConnection(*args) rescue JavaSQL::SQLException, NativeException, StandardError => e raise e unless driver # If the DriverManager can't get the connection - use the connect # method of the driver. (This happens under Tomcat for instance) props = java.util.Properties.new if opts && opts[:user] && opts[:password] props.setProperty("user", opts[:user]) props.setProperty("password", opts[:password]) end opts[:jdbc_properties].each{|k,v| props.setProperty(k.to_s, v)} if opts[:jdbc_properties] begin c = driver.new.connect(args[0], props) raise(Sequel::DatabaseError, 'driver.new.connect returned nil: probably bad JDBC connection string') unless c c rescue JavaSQL::SQLException, NativeException, StandardError => e2 unless e2. == e. e2. << "\n#{e.class.name}: #{e.}" end raise e2 end end end setup_connection(conn) end |
#disconnect_connection(c) ⇒ Object
Close given adapter connections, and delete any related prepared statements.
357 358 359 360 |
# File 'lib/sequel/adapters/jdbc.rb', line 357 def disconnect_connection(c) @connection_prepared_statements_mutex.synchronize{@connection_prepared_statements.delete(c)} c.close end |
#execute(sql, opts = OPTS, &block) ⇒ Object Also known as: execute_dui
Execute the given SQL. If a block is given, if should be a SELECT statement or something else that returns rows.
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 |
# File 'lib/sequel/adapters/jdbc.rb', line 364 def execute(sql, opts=OPTS, &block) return call_sproc(sql, opts, &block) if opts[:sproc] return execute_prepared_statement(sql, opts, &block) if [Symbol, Dataset].any?{|c| sql.is_a?(c)} synchronize(opts[:server]) do |conn| statement(conn) do |stmt| if block if size = fetch_size stmt.setFetchSize(size) end yield log_yield(sql){stmt.executeQuery(sql)} else case opts[:type] when :ddl log_yield(sql){stmt.execute(sql)} when :insert log_yield(sql){execute_statement_insert(stmt, sql)} last_insert_id(conn, opts.merge(:stmt=>stmt)) else log_yield(sql){stmt.executeUpdate(sql)} end end end end end |
#execute_ddl(sql, opts = OPTS) ⇒ Object
Execute the given DDL SQL, which should not return any values or rows.
392 393 394 |
# File 'lib/sequel/adapters/jdbc.rb', line 392 def execute_ddl(sql, opts=OPTS) execute(sql, {:type=>:ddl}.merge(opts)) end |
#execute_insert(sql, opts = OPTS) ⇒ Object
Execute the given INSERT SQL, returning the last inserted row id.
398 399 400 |
# File 'lib/sequel/adapters/jdbc.rb', line 398 def execute_insert(sql, opts=OPTS) execute(sql, {:type=>:insert}.merge(opts)) end |
#foreign_key_list(table, opts = OPTS) ⇒ Object
Use the JDBC metadata to get a list of foreign keys for the table.
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 |
# File 'lib/sequel/adapters/jdbc.rb', line 403 def foreign_key_list(table, opts=OPTS) m = output_identifier_meth schema, table = (table, opts) foreign_keys = {} (:getImportedKeys, nil, schema, table) do |r| if fk = foreign_keys[r[:fk_name]] fk[:columns] << [r[:key_seq], m.call(r[:fkcolumn_name])] fk[:key] << [r[:key_seq], m.call(r[:pkcolumn_name])] elsif r[:fk_name] 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])]]} end end foreign_keys.values.each do |fk| [:columns, :key].each do |k| fk[k] = fk[k].sort.map{|_, v| v} end end end |
#indexes(table, opts = OPTS) ⇒ Object Also known as: jdbc_indexes
Use the JDBC metadata to get the index information for the table.
423 424 425 426 427 428 429 430 431 432 433 434 |
# File 'lib/sequel/adapters/jdbc.rb', line 423 def indexes(table, opts=OPTS) m = output_identifier_meth schema, table = (table, opts) indexes = {} (:getIndexInfo, nil, schema, table, false, true) do |r| next unless name = r[:column_name] next if respond_to?(:primary_key_index_re, true) and r[:index_name] =~ primary_key_index_re i = indexes[m.call(r[:index_name])] ||= {:columns=>[], :unique=>[false, 0].include?(r[:non_unique])} i[:columns] << m.call(name) end indexes end |
#jndi? ⇒ Boolean
Whether or not JNDI is being used for this connection.
437 438 439 |
# File 'lib/sequel/adapters/jdbc.rb', line 437 def jndi? !!(uri =~ JNDI_URI_REGEXP) end |
#tables(opts = OPTS) ⇒ Object Also known as: jdbc_tables
All tables in this database
442 443 444 |
# File 'lib/sequel/adapters/jdbc.rb', line 442 def tables(opts=OPTS) get_tables('TABLE', opts) end |
#uri(opts = OPTS) ⇒ Object
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.
450 451 452 453 454 |
# File 'lib/sequel/adapters/jdbc.rb', line 450 def uri(opts=OPTS) opts = @opts.merge(opts) ur = opts[:uri] || opts[:url] || opts[:database] ur =~ /^\Ajdbc:/ ? ur : "jdbc:#{ur}" end |
#views(opts = OPTS) ⇒ Object Also known as: jdbc_views
All views in this database
457 458 459 |
# File 'lib/sequel/adapters/jdbc.rb', line 457 def views(opts=OPTS) get_tables('VIEW', opts) end |