class Baza::JdbcResult

This class controls the result for the Java-MySQL-driver.

Constants

DATE_TYPES
FLOAT_TYPES
INT_TYPES
NIL_TYPES
STRING_TYPES
TIME_TYPES

Public Class Methods

new(driver, stmt, result_set, preload_results) click to toggle source

Constructor. This should not be called manually.

# File lib/baza/jdbc_result.rb, line 11
def initialize(driver, stmt, result_set, preload_results)
  @result_set = result_set
  @stmt = stmt
  @type_translation = driver.db.opts[:type_translation]
  @rows = []
  @index = -1
  read_results if preload_results
end

Public Instance Methods

each() { |data| ... } click to toggle source
# File lib/baza/jdbc_result.rb, line 29
def each
  loop do
    data = fetch

    if data
      yield data
    else
      break
    end
  end
end
fetch() click to toggle source
# File lib/baza/jdbc_result.rb, line 20
def fetch
  if @read_results
    return false if @rows.empty?
    @rows.shift
  else
    read_row
  end
end

Private Instance Methods

destroy() click to toggle source
# File lib/baza/jdbc_result.rb, line 80
def destroy
  @stmt.close
  @result_set.close
end
read_meta() click to toggle source

Reads meta-data about the query like keys and count.

# File lib/baza/jdbc_result.rb, line 44
def read_meta
  @result_set.before_first
  meta = @result_set.meta_data
  @count = meta.column_count

  @keys = []

  if @type_translation == true
    @types = []
    @type_names = []
  end

  1.upto(@count) do |count|
    @keys << meta.column_label(count).to_sym

    if @type_translation == true
      @types << meta.column_type(count)
      @type_names << meta.column_type_name(count).downcase.to_sym
    end
  end
end
read_results() click to toggle source
# File lib/baza/jdbc_result.rb, line 66
def read_results
  @read_results = true

  loop do
    row = read_row

    if row
      @rows << row
    else
      break
    end
  end
end
read_row() click to toggle source
# File lib/baza/jdbc_result.rb, line 85
def read_row
  return false unless @result_set

  unless @result_set.next
    destroy
    @result_set = nil
    return false
  end

  read_meta unless @keys

  hash = {}
  @count.times do |count|
    if @type_translation
      value = translate_type(@result_set, count)
    else
      value = @result_set.object(count + 1)
    end

    hash[@keys[count]] = value
  end

  hash
end
translate_type(result, count) click to toggle source
# File lib/baza/jdbc_result.rb, line 110
def translate_type(result, count)
  java_count = count + 1

  return result.string(java_count) if @type_translation == :string

  type = @types[count]

  if INT_TYPES[type]
    return result.int(java_count)
  elsif STRING_TYPES[type]
    return result.string(java_count)
  elsif FLOAT_TYPES[type]
    return result.float(java_count)
  elsif TIME_TYPES[type] || @type_names[count] == :datetime # Important to do both in SQLite...
    return Time.parse(result.string(java_count))
  elsif DATE_TYPES[type]
    return Date.parse(result.string(java_count))
  elsif NIL_TYPES[type]
    return nil
  else
    return result.object(java_count)
  end
end