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.
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 |
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. |
Execute the given stored procedure with the give name. If a block is given, the stored procedure should return rows.
# File lib/sequel/adapters/jdbc.rb, line 168 168: def call_sproc(name, opts = OPTS) 169: args = opts[:args] || [] 170: sql = "{call #{name}(#{args.map{'?'}.join(',')})}" 171: synchronize(opts[:server]) do |conn| 172: cps = conn.prepareCall(sql) 173: 174: i = 0 175: args.each{|arg| set_ps_arg(cps, arg, i+=1)} 176: 177: begin 178: if block_given? 179: yield log_yield(sql){cps.executeQuery} 180: else 181: case opts[:type] 182: when :insert 183: log_yield(sql){cps.executeUpdate} 184: last_insert_id(conn, opts) 185: else 186: log_yield(sql){cps.executeUpdate} 187: end 188: end 189: rescue NativeException, JavaSQL::SQLException => e 190: raise_error(e) 191: ensure 192: cps.close 193: end 194: end 195: end
Connect to the database using JavaSQL::DriverManager.getConnection.
# File lib/sequel/adapters/jdbc.rb, line 198 198: def connect(server) 199: opts = server_opts(server) 200: conn = if jndi? 201: get_connection_from_jndi 202: else 203: args = [uri(opts)] 204: args.concat([opts[:user], opts[:password]]) if opts[:user] && opts[:password] 205: begin 206: JavaSQL::DriverManager.setLoginTimeout(opts[:login_timeout]) if opts[:login_timeout] 207: JavaSQL::DriverManager.getConnection(*args) 208: rescue JavaSQL::SQLException, NativeException, StandardError => e 209: raise e unless driver 210: # If the DriverManager can't get the connection - use the connect 211: # method of the driver. (This happens under Tomcat for instance) 212: props = java.util.Properties.new 213: if opts && opts[:user] && opts[:password] 214: props.setProperty("user", opts[:user]) 215: props.setProperty("password", opts[:password]) 216: end 217: opts[:jdbc_properties].each{|k,v| props.setProperty(k.to_s, v)} if opts[:jdbc_properties] 218: begin 219: c = driver.new.connect(args[0], props) 220: raise(Sequel::DatabaseError, 'driver.new.connect returned nil: probably bad JDBC connection string') unless c 221: c 222: rescue JavaSQL::SQLException, NativeException, StandardError => e2 223: unless e2.message == e.message 224: e2.message << "\n#{e.class.name}: #{e.message}" 225: end 226: raise e2 227: end 228: end 229: end 230: setup_connection(conn) 231: end
Close given adapter connections, and delete any related prepared statements.
# File lib/sequel/adapters/jdbc.rb, line 234 234: def disconnect_connection(c) 235: @connection_prepared_statements_mutex.synchronize{@connection_prepared_statements.delete(c)} 236: c.close 237: end
Execute the given SQL. If a block is given, if should be a SELECT statement or something else that returns rows.
# File lib/sequel/adapters/jdbc.rb, line 241 241: def execute(sql, opts=OPTS, &block) 242: return call_sproc(sql, opts, &block) if opts[:sproc] 243: return execute_prepared_statement(sql, opts, &block) if [Symbol, Dataset].any?{|c| sql.is_a?(c)} 244: synchronize(opts[:server]) do |conn| 245: statement(conn) do |stmt| 246: if block 247: if size = fetch_size 248: stmt.setFetchSize(size) 249: end 250: yield log_yield(sql){stmt.executeQuery(sql)} 251: else 252: case opts[:type] 253: when :ddl 254: log_yield(sql){stmt.execute(sql)} 255: when :insert 256: log_yield(sql){execute_statement_insert(stmt, sql)} 257: last_insert_id(conn, opts.merge(:stmt=>stmt)) 258: else 259: log_yield(sql){stmt.executeUpdate(sql)} 260: end 261: end 262: end 263: end 264: end
Use the JDBC metadata to get a list of foreign keys for the table.
# File lib/sequel/adapters/jdbc.rb, line 280 280: def foreign_key_list(table, opts=OPTS) 281: m = output_identifier_meth 282: schema, table = metadata_schema_and_table(table, opts) 283: foreign_keys = {} 284: metadata(:getImportedKeys, nil, schema, table) do |r| 285: if fk = foreign_keys[r[:fk_name]] 286: fk[:columns] << [r[:key_seq], m.call(r[:fkcolumn_name])] 287: fk[:key] << [r[:key_seq], m.call(r[:pkcolumn_name])] 288: elsif r[:fk_name] 289: 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])]]} 290: end 291: end 292: foreign_keys.values.each do |fk| 293: [:columns, :key].each do |k| 294: fk[k] = fk[k].sort.map{|_, v| v} 295: end 296: end 297: end
Use the JDBC metadata to get the index information for the table.
# File lib/sequel/adapters/jdbc.rb, line 300 300: def indexes(table, opts=OPTS) 301: m = output_identifier_meth 302: schema, table = metadata_schema_and_table(table, opts) 303: indexes = {} 304: metadata(:getIndexInfo, nil, schema, table, false, true) do |r| 305: next unless name = r[:column_name] 306: next if respond_to?(:primary_key_index_re, true) and r[:index_name] =~ primary_key_index_re 307: i = indexes[m.call(r[:index_name])] ||= {:columns=>[], :unique=>[false, 0].include?(r[:non_unique])} 308: i[:columns] << m.call(name) 309: end 310: indexes 311: end
Whether or not JNDI is being used for this connection.
# File lib/sequel/adapters/jdbc.rb, line 314 314: def jndi? 315: !!(uri =~ JNDI_URI_REGEXP) 316: 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.
# File lib/sequel/adapters/jdbc.rb, line 327 327: def uri(opts=OPTS) 328: opts = @opts.merge(opts) 329: ur = opts[:uri] || opts[:url] || opts[:database] 330: ur =~ /^\Ajdbc:/ ? ur : "jdbc:#{ur}" 331: end