Class | Sequel::JDBC::Database |
In: |
lib/sequel/adapters/jdbc.rb
|
Parent: | Sequel::Database |
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. |
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 165 165: def call_sproc(name, opts = OPTS) 166: args = opts[:args] || [] 167: sql = "{call #{name}(#{args.map{'?'}.join(',')})}" 168: synchronize(opts[:server]) do |conn| 169: cps = conn.prepareCall(sql) 170: 171: i = 0 172: args.each{|arg| set_ps_arg(cps, arg, i+=1)} 173: 174: begin 175: if block_given? 176: yield log_connection_yield(sql, conn){cps.executeQuery} 177: else 178: log_connection_yield(sql, conn){cps.executeUpdate} 179: if opts[:type] == :insert 180: last_insert_id(conn, opts) 181: end 182: end 183: rescue *DATABASE_ERROR_CLASSES => e 184: raise_error(e) 185: ensure 186: cps.close 187: end 188: end 189: end
Connect to the database using JavaSQL::DriverManager.getConnection, and falling back to driver.new.connect if the driver is known.
# File lib/sequel/adapters/jdbc.rb, line 193 193: def connect(server) 194: opts = server_opts(server) 195: conn = if jndi? 196: get_connection_from_jndi 197: else 198: args = [uri(opts)] 199: args.concat([opts[:user], opts[:password]]) if opts[:user] && opts[:password] 200: begin 201: JavaSQL::DriverManager.setLoginTimeout(opts[:login_timeout]) if opts[:login_timeout] 202: raise StandardError, "skipping regular connection" if opts[:jdbc_properties] 203: JavaSQL::DriverManager.getConnection(*args) 204: rescue StandardError, *DATABASE_ERROR_CLASSES => e 205: raise e unless driver 206: # If the DriverManager can't get the connection - use the connect 207: # method of the driver. (This happens under Tomcat for instance) 208: props = java.util.Properties.new 209: if opts && opts[:user] && opts[:password] 210: props.setProperty("user", opts[:user]) 211: props.setProperty("password", opts[:password]) 212: end 213: opts[:jdbc_properties].each{|k,v| props.setProperty(k.to_s, v)} if opts[:jdbc_properties] 214: begin 215: c = driver.new.connect(args[0], props) 216: raise(Sequel::DatabaseError, 'driver.new.connect returned nil: probably bad JDBC connection string') unless c 217: c 218: rescue StandardError, *DATABASE_ERROR_CLASSES => e2 219: if e2.respond_to?(:message=) && e2.message != e.message 220: e2.message = "#{e2.message}\n#{e.class.name}: #{e.message}" 221: end 222: raise e2 223: end 224: end 225: end 226: setup_connection_with_opts(conn, opts) 227: end
Close given adapter connections, and delete any related prepared statements.
# File lib/sequel/adapters/jdbc.rb, line 230 230: def disconnect_connection(c) 231: @connection_prepared_statements_mutex.synchronize{@connection_prepared_statements.delete(c)} 232: c.close 233: end
# File lib/sequel/adapters/jdbc.rb, line 235 235: def execute(sql, opts=OPTS, &block) 236: return call_sproc(sql, opts, &block) if opts[:sproc] 237: return execute_prepared_statement(sql, opts, &block) if [Symbol, Dataset].any?{|c| sql.is_a?(c)} 238: synchronize(opts[:server]) do |conn| 239: statement(conn) do |stmt| 240: if block 241: if size = fetch_size 242: stmt.setFetchSize(size) 243: end 244: yield log_connection_yield(sql, conn){stmt.executeQuery(sql)} 245: else 246: case opts[:type] 247: when :ddl 248: log_connection_yield(sql, conn){stmt.execute(sql)} 249: when :insert 250: log_connection_yield(sql, conn){execute_statement_insert(stmt, sql)} 251: last_insert_id(conn, Hash[opts].merge!(:stmt=>stmt)) 252: else 253: log_connection_yield(sql, conn){stmt.executeUpdate(sql)} 254: end 255: end 256: end 257: end 258: end
# File lib/sequel/adapters/jdbc.rb, line 261 261: def execute_ddl(sql, opts=OPTS) 262: opts = Hash[opts] 263: opts[:type] = :ddl 264: execute(sql, opts) 265: end
# File lib/sequel/adapters/jdbc.rb, line 267 267: def execute_insert(sql, opts=OPTS) 268: opts = Hash[opts] 269: opts[:type] = :insert 270: execute(sql, opts) 271: 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
# File lib/sequel/adapters/jdbc.rb, line 273 273: def freeze 274: @type_convertor_map.freeze 275: @basic_type_convertor_map.freeze 276: super 277: 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