class SwissDB::Cursor

Constants

FIELD_TYPE_BLOB
FIELD_TYPE_FLOAT
FIELD_TYPE_INTEGER
FIELD_TYPE_NULL
FIELD_TYPE_STRING

Attributes

cursor[RW]
model[RW]

Public Class Methods

new(cursor, model) click to toggle source
# File lib/swiss_db/cursor.rb, line 16
def initialize(cursor, model)
  @cursor = cursor
  @model = model
  @values = {}
end

Public Instance Methods

[](pos) click to toggle source
# File lib/swiss_db/cursor.rb, line 52
def [](pos)
  begin
    return nil if count == 0
    cursor.moveToPosition(pos) ? self : nil
    swiss_model = model.new(to_hash)
  ensure
    cursor.close
  end
  swiss_model
end
column_names() click to toggle source
# File lib/swiss_db/cursor.rb, line 136
def column_names
  cursor.getColumnNames.map(&:to_sym)
end
count() click to toggle source
# File lib/swiss_db/cursor.rb, line 132
def count
  cursor.getCount
end
current() click to toggle source
# File lib/swiss_db/cursor.rb, line 48
def current
  model.new(to_hash)
end
each() { |m| ... } click to toggle source
# File lib/swiss_db/cursor.rb, line 152
def each(&block)
  return [] if count == 0
  arr = []
  (0...count).each do |i|
    # puts i
    cursor.moveToPosition(i)
    m = model.new(to_hash)
    yield(m)
    arr << m
  end

  arr
end
first() click to toggle source
# File lib/swiss_db/cursor.rb, line 26
def first
  begin
    return nil if count == 0
    cursor.moveToFirst ? self : nil
    swiss_model = model.new(to_hash)
  ensure
    cursor.close
  end
  swiss_model
end
get_method(method_name) click to toggle source
# File lib/swiss_db/cursor.rb, line 105
def get_method(method_name)
  index = cursor.getColumnIndex(method_name)
  type = cursor.getType(index)
  # puts "getting field #{method_name} at index #{index} of type #{type}"

  if type == FIELD_TYPE_STRING #also boolean
    str = cursor.getString(index).to_s

    if str =~ /[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3}/
      formatter = Java::Text::SimpleDateFormat.new('yyyy-MM-dd hh:mm:ss.SSS')
      str = formatter.parse(str)
    end

    str = true if str == "true"
    str = false if str == "false"
    str
  elsif type == FIELD_TYPE_INTEGER
    cursor.getInt(index).to_i
  elsif type == FIELD_TYPE_NULL
    nil #??
  elsif type == FIELD_TYPE_FLOAT
    cursor.getFloat(index).to_f
  elsif type == FIELD_TYPE_BLOB
    cursor.getBlob(index)
  end
end
is_setter?(method_name) click to toggle source
# File lib/swiss_db/cursor.rb, line 101
def is_setter?(method_name)
  method_name[-1] == '='
end
last() click to toggle source
# File lib/swiss_db/cursor.rb, line 37
def last
  begin
    return nil if count == 0
    cursor.moveToLast ? self : nil
    swiss_model = model.new(to_hash)
  ensure
    cursor.close
  end
  swiss_model
end
map() { |model| ... } click to toggle source
# File lib/swiss_db/cursor.rb, line 140
def map(&block)
  return [] if count == 0
  arr = []
  (0...count).each do |i|
    # puts i
    cursor.moveToPosition(i)
    arr << yield(model.new(to_hash))
  end

  arr
end
method_missing(method_name, *args) click to toggle source

todo: take out setter code. it's not used anymore. leave the getter code. it is used. (see to_hash)

Calls superclass method
# File lib/swiss_db/cursor.rb, line 88
def method_missing(method_name, *args)
  # puts "cursor method missing #{method_name}"
  if valid_getter?(method_name)
    get_method(method_name)
  else
    super
  end
end
moveToFirst() click to toggle source
# File lib/swiss_db/cursor.rb, line 175
def moveToFirst
  cursor.moveToFirst
end
moveToLast() click to toggle source
# File lib/swiss_db/cursor.rb, line 171
def moveToLast
  cursor.moveToLast
end
moveToPosition(i) click to toggle source

those methods allow the use of PMCursorAdapter with SwissDB

# File lib/swiss_db/cursor.rb, line 167
def moveToPosition(i)
  cursor.moveToPosition(i)
end
to_a() click to toggle source
# File lib/swiss_db/cursor.rb, line 71
def to_a
  begin
    return nil if count == 0
    arr = []
    (0...count).each do |i|
      # puts i
      cursor.moveToPosition(i)
      arr << model.new(to_hash)
    end
  ensure
    cursor.close
  end
  arr
end
to_hash() click to toggle source
# File lib/swiss_db/cursor.rb, line 63
def to_hash
  hash_obj = {}
  column_names.each do |k|
    hash_obj[k] = self.send(k)
  end
  hash_obj
end
valid_getter?(method_name) click to toggle source
# File lib/swiss_db/cursor.rb, line 97
def valid_getter?(method_name)
  column_names.include? method_name
end