class DBI::DBD::SQLRelay::Statement

Public Class Methods

new(handle, stmt) click to toggle source
Calls superclass method
# File SQLRelay.rb, line 176
def initialize(handle, stmt)
  super(nil)  # attribs

  @db = handle 
  @handle = SQLRCursor.new(@db)
  #@handle.setResultSetBufferSize(100)
  @handle.prepareQuery(stmt)
end

Public Instance Methods

bind_param(param, value, attribs) click to toggle source

Binds value to param defined earlier in the prepare call. attribs is currently unused.

# File SQLRelay.rb, line 188
def bind_param(param, value, attribs)

  # in SQL Relay, bind variable names can be names or numbers and values
  # can be either strings, integers or floats.  Floats come with precision
  # and scale as well.
  if value.kind_of? Float then

    # for float binds, check attribs for precision and scale
    if attribs
      precision = attribs['precision'].to_i
      scale     = attribs['scale'].to_i
    end

    # if either of precision or scale is not passed in, extract them by
    # parsing the value around the decimal point or using defaults
    if precision.nil? or scale.nil?
      pr, sc = value.to_s.split(".")
      precision ||= pr.length || 8
      scale     ||= sc.length || 2
    end

    @handle.inputBind(param.to_s, value.to_f, precision, scale)
  else
    @handle.inputBind(param.to_s, value)
  end
end
column_info() click to toggle source

Returns an array of hashes. Each hash contains a 'name', 'type_name' and 'precision' key.

# File SQLRelay.rb, line 329
def column_info

  # build a column info hash
  (0...@handle.colCount).collect do |nr|
    {
      'name'      => @handle.getColumnName(nr),
      'type_name' => @handle.getColumnType(nr),
      'precision' => @handle.getColumnLength(nr)
    }
  end
end
execute() click to toggle source

Send a SQL query to the server and returns the number of rows returned. Raises a DBI::ProgrammingError if it failed.

# File SQLRelay.rb, line 218
def execute

  # otherwise execute the already-prepared query, raising an error if it fails
  if @handle.executeQuery == 0 then 
    raise DBI::ProgrammingError.new(@handle.errorMessage)
  end

  # initialize some values
  @row_index = 0
  @row_count = @handle.rowCount
  @affected_rows = @handle.affectedRows

  # clear bind values so the statement can be re-bound and re-executed
  @handle.clearBinds
end
fetch() click to toggle source

Returns one row of the result set.

# File SQLRelay.rb, line 242
def fetch

  # if we're already at the end of the result set, return nil
  if @row_index >= @row_count 
    return nil
  end

  # otherwise get the current row, increment
  # the row index and return the current row
  row = @handle.getRow(@row_index)
  @row_index += 1
  return row
end
fetch_all() click to toggle source

Returns the rest of the result set.

# File SQLRelay.rb, line 314
def fetch_all

  # otherwise, fetch the rest of the rows and return them
  *rows = []
  index = 0
  while @row_index < @row_count
    *rows[index] = fetch()
    index = index+1
  end
  return *rows
end
fetch_many(cnt) click to toggle source

Returns the next “cnt” rows rest of the result set.

# File SQLRelay.rb, line 300
def fetch_many(cnt)

  # fetch the next "cnt" rows and return them
  *rows = []
  index = 0
  while index < cnt and @row_index < @row_count
    *rows[index] = fetch()
    index = index+1
  end
  return *rows
end
fetch_scroll(direction, offset=1) click to toggle source

Returns one row of the result set. Which row depends on direction and offset. For DBI::SQL_FETCH_NEXT the next row is returned. For DBI::SQL_FETCH_PRIOR the prior row is returned. For DBI::SQL_FETCH_FIRST the first row is returned. For DBI::SQL_FETCH_LAST the last row is returned. For DBI::SQL_FETCH_ABSOLUTE the “offset” row is returned. For DBI::SQL_FETCH_RELATIVE the row “offset” rows from the current row is returned.

# File SQLRelay.rb, line 266
def fetch_scroll(direction, offset=1)

  # decide which row to fetch, take into account that the standard behavior
  # of a fetch command is to fetch the row and then skip to the next row
  # of the result set afterward
  fetch_row = case direction
    when DBI::SQL_FETCH_NEXT     then @row_index
    when DBI::SQL_FETCH_PRIOR    then @row_index-2
    when DBI::SQL_FETCH_FIRST    then 0
    when DBI::SQL_FETCH_LAST     then @row_count-1
    when DBI::SQL_FETCH_ABSOLUTE then offset
    when DBI::SQL_FETCH_RELATIVE then @row_index+offset-1
  end
  
  # fetch the row
  row = nil
  if fetch_row > -1 and fetch_row < @row_count
    row = @handle.getRow(fetch_row)
  end

  # set the current row, avoid running past 
  # the end or beginning of the result set
  @row_index = fetch_row + 1
  if @row_index < 0 then
    @row_index = 0
  elsif @row_index > @row_count
    @row_index = @row_count
  end

  return row
end
finish() click to toggle source

Invalidates the statement.

# File SQLRelay.rb, line 236
def finish
  @handle = nil
end
rows() click to toggle source

Returns the number of rows in the current result set.

# File SQLRelay.rb, line 343
def rows

  # For DML or DDL queries, row_count is 0 but affected_rows could
  # be non-zero.  For selects, affected_rows will always be zero or
  # equal to row_count (for select into queries, for example).  So,
  # if row_count is 0, send affected_rows.
  @row_count == 0 ? @affected_rows : @row_count
end