class Sequel::JDBC::Database

  1. lib/sequel/adapters/jdbc.rb
  2. lib/sequel/adapters/jdbc/db2.rb
  3. lib/sequel/adapters/jdbc/mssql.rb
  4. show all
Parent: JDBC

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

Public Instance Aliases

execute_dui -> execute
jdbc_indexes -> indexes
jdbc_schema_parse_table -> schema_parse_table

Alias the generic JDBC versions so they can be called directly later

jdbc_tables -> tables
jdbc_views -> views

Attributes

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

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, line 201
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)

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

[show source]
# File lib/sequel/adapters/jdbc.rb, line 231
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.message == e.message
          e2.message << "\n#{e.class.name}: #{e.message}"
        end
        raise e2
      end
    end
  end
  setup_connection(conn)
end
disconnect_connection (c)

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

[show source]
# File lib/sequel/adapters/jdbc.rb, line 267
def disconnect_connection(c)
  @connection_prepared_statements_mutex.synchronize{@connection_prepared_statements.delete(c)}
  c.close
end
execute (sql, opts=OPTS, &block)

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

[show source]
# File lib/sequel/adapters/jdbc.rb, line 274
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
        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)

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

[show source]
# File lib/sequel/adapters/jdbc.rb, line 299
def execute_ddl(sql, opts=OPTS)
  execute(sql, {:type=>:ddl}.merge(opts))
end
execute_insert (sql, opts=OPTS)

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

[show source]
# File lib/sequel/adapters/jdbc.rb, line 305
def execute_insert(sql, opts=OPTS)
  execute(sql, {:type=>:insert}.merge(opts))
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, line 310
def foreign_key_list(table, opts=OPTS)
  m = output_identifier_meth
  schema, table = metadata_schema_and_table(table, opts)
  foreign_keys = {}
  metadata(: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)

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

[show source]
# File lib/sequel/adapters/jdbc.rb, line 330
def indexes(table, opts=OPTS)
  m = output_identifier_meth
  schema, table = metadata_schema_and_table(table, opts)
  indexes = {}
  metadata(: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? ()

Whether or not JNDI is being used for this connection.

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

All tables in this database

[show source]
# File lib/sequel/adapters/jdbc.rb, line 349
def tables(opts=OPTS)
  get_tables('TABLE', opts)
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, line 357
def uri(opts=OPTS)
  opts = @opts.merge(opts)
  ur = opts[:uri] || opts[:url] || opts[:database]
  ur =~ /^\Ajdbc:/ ? ur : "jdbc:#{ur}"
end
views (opts=OPTS)

All views in this database

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