class Sequel::JDBC::Dataset

Constants

BLOB_METHOD
BUFFERED_READER_METHOD
BYTE_ARRAY_METHOD
CLOB_METHOD
DATE_METHOD
DECIMAL_METHOD

Cache type translator methods so that duplicate Method objects are not created.

JAVA_BIG_DECIMAL
JAVA_BUFFERED_READER
JAVA_BYTE_ARRAY
JAVA_SQL_BLOB
JAVA_SQL_CLOB
JAVA_SQL_DATE
JAVA_SQL_TIME
JAVA_SQL_TIMESTAMP

Cache Java class constants to speed up lookups

JAVA_UUID
TIME_METHOD
TYPE_TRANSLATOR_INSTANCE
UUID_METHOD

Attributes

convert_types[RW]

Whether to convert some Java types to ruby types when retrieving rows. Uses the database's setting by default, can be set to false to roughly double performance when fetching rows.

Public Instance Methods

fetch_rows(sql, &block) click to toggle source

Correctly return rows from the database and return them as hashes.

# File lib/sequel/adapters/jdbc.rb, line 666
def fetch_rows(sql, &block)
  execute(sql){|result| process_result_set(result, &block)}
  self
end
prepare(type, name=nil, *values) click to toggle source

Create a named prepared statement that is stored in the database (and connection) for reuse.

# File lib/sequel/adapters/jdbc.rb, line 673
def prepare(type, name=nil, *values)
  ps = to_prepared_statement(type, values)
  ps.extend(PreparedStatementMethods)
  if name
    ps.prepared_statement_name = name
    db.set_prepared_statement(name, ps)
  end
  ps
end

Private Instance Methods

convert_type_proc(v) click to toggle source

Return a callable object that will convert any value of v's class to a ruby object. If no callable object can handle v's class, return false so that the negative lookup is cached.

# File lib/sequel/adapters/jdbc.rb, line 738
def convert_type_proc(v)
  case v
  when JAVA_BIG_DECIMAL
    DECIMAL_METHOD
  when JAVA_SQL_TIMESTAMP
    method(:convert_type_timestamp)
  when JAVA_SQL_TIME
    TIME_METHOD
  when JAVA_SQL_DATE
    DATE_METHOD
  when JAVA_BUFFERED_READER
    BUFFERED_READER_METHOD
  when JAVA_BYTE_ARRAY
    BYTE_ARRAY_METHOD
  when JAVA_SQL_BLOB
    BLOB_METHOD
  when JAVA_SQL_CLOB
    CLOB_METHOD
  when JAVA_UUID
    UUID_METHOD
  else
    false
  end
end
convert_type_timestamp(v) click to toggle source

Convert the given Java timestamp to an instance of Sequel.datetime_class.

# File lib/sequel/adapters/jdbc.rb, line 731
def convert_type_timestamp(v)
  db.to_application_timestamp([v.getYear + 1900, v.getMonth + 1, v.getDate, v.getHours, v.getMinutes, v.getSeconds, v.getNanos])
end
prepare_extend_sproc(ds) click to toggle source

Extend the dataset with the JDBC stored procedure methods.

# File lib/sequel/adapters/jdbc.rb, line 764
def prepare_extend_sproc(ds)
  ds.extend(StoredProcedureMethods)
end
process_result_set(result, &block) click to toggle source

Split out from fetch rows to allow processing of JDBC result sets that don't come from issuing an SQL string.

# File lib/sequel/adapters/jdbc.rb, line 770
def process_result_set(result, &block)
  # get column names
  meta = result.getMetaData
  cols = []
  i = 0
  meta.getColumnCount.times{cols << [output_identifier(meta.getColumnLabel(i+=1)), i]}
  columns = cols.map{|c| c.at(0)}
  @columns = columns
  ct = @convert_types
  if (ct.nil? ? db.convert_types : ct)
    cols.each{|c| c << nil}
    process_result_set_convert(cols, result, &block)
  else
    process_result_set_no_convert(cols, result, &block)
  end
ensure
  result.close
end
process_result_set_convert(cols, result) { |row| ... } click to toggle source

Use conversion procs to convert data retrieved from the database. This has been optimized, the algorithm it uses is roughly, for each column value in each row:

  • check if the value is truthy (not false/nil)

  • if not truthy, return object

  • otherwise, see if a conversion method exists for the column. All columns start with a nil conversion proc, since unlike other adapters, Sequel doesn't get the type of the column when parsing the column metadata.

  • if a conversion proc is not false/nil, call it with the object and return the result.

  • if a conversion proc has already been looked up and doesn't exist (false value), return object.

  • if a conversion proc hasn't been looked up yet (nil value), call #convert_type_proc to get the conversion method. Cache the result of as the column's conversion proc to speed up later processing. If the conversion proc exists, call it and return the result, otherwise, return the object.

# File lib/sequel/adapters/jdbc.rb, line 807
def process_result_set_convert(cols, result)
  while result.next
    row = {}
    cols.each do |n, i, p|
      v = result.getObject(i)
      row[n] = if v
        if p
          p.call(v)
        elsif p.nil?
          cols[i-1][2] = p = convert_type_proc(v)
          if p
            p.call(v)
          else
            v
          end
        else
          v
        end
      else
        v
      end
    end
    yield row
  end
end
process_result_set_no_convert(cols, result) { |row| ... } click to toggle source

Yield rows without calling any conversion procs. This may yield Java values and not ruby values.

# File lib/sequel/adapters/jdbc.rb, line 835
def process_result_set_no_convert(cols, result)
  while result.next
    row = {}
    cols.each{|n, i| row[n] = result.getObject(i)}
    yield row
  end
end