Class: KnjDB_mysql_result

Inherits:
Object show all
Defined in:
lib/knj/knjdb/drivers/mysql/knjdb_mysql.rb

Overview

This class controls the results for the normal MySQL-driver.

Instance Method Summary collapse

Constructor Details

#initialize(driver, result) ⇒ KnjDB_mysql_result

Constructor. This should not be called manually.



390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql.rb', line 390

def initialize(driver, result)
  @driver = driver
  @result = result
  @mutex = Mutex.new
  
  if @result
    @keys = []
    keys = @result.fetch_fields
    keys.each do |key|
      @keys << key.name.to_sym
    end
  end
end

Instance Method Details

#eachObject

Loops over every result yielding it.



437
438
439
440
441
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql.rb', line 437

def each
  while data = self.fetch_hash_symbols
    yield(data)
  end
end

#fetchObject

Returns a single result.



405
406
407
408
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql.rb', line 405

def fetch
  return self.fetch_hash_symbols if @driver.knjdb.opts[:return_keys] == "symbols"
  return self.fetch_hash_strings
end

#fetch_hash_stringsObject

Returns a single result as a hash with strings as keys.



411
412
413
414
415
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql.rb', line 411

def fetch_hash_strings
  @mutex.synchronize do
    return @result.fetch_hash
  end
end

#fetch_hash_symbolsObject

Returns a single result as a hash with symbols as keys.



418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql.rb', line 418

def fetch_hash_symbols
  fetched = nil
  @mutex.synchronize do
    fetched = @result.fetch_row
  end
  
  return false if !fetched
  
  ret = {}
  count = 0
  @keys.each do |key|
    ret[key] = fetched[count]
    count += 1
  end
  
  return ret
end