Sha256: 8a7eece638f8eb2df30f94447f23ee111e01614383eb19795056f4b51091f9e2

Contents?: true

Size: 930 Bytes

Versions: 2

Compression:

Stored size: 930 Bytes

Contents

require 'sqlite3'
require 'fileutils'

require 'wlog/domain/static_configurations.rb'

module Wlog
# The db registry, using sqlite3
# @author Simon Symeonidis
class DbRegistry
  include StaticConfigurations

  def initialize(dbname)
    @handle = SQLite3::Database.new(dbname || "#{DataDirectory}#{ARGV[0] || DefaultDb}")
  end

  # execute a sql with varargs parameters
  # @param *sql, first the sql string, then the parameters if the statement is
  #   to be prepared. 
  # @example Simple Usage
  #   DbRegistry.execute("SELECT * FROM table WHERE id = ?", 1)
  def execute(*sql)
    @handle.execute(*sql)
  end

  # Get the last row, given a table name. The table needs to have an id
  def last_row_from(tablename)
    query = "SELECT * FROM #{tablename} WHERE id =(SELECT MAX(id) FROM"\
      " #{tablename});"
    @handle.execute(query)
  end

  # the database handle
  attr_accessor :handle

private
end
end # module Wlog

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
wlog-1.1.1 lib/wlog/db_registry.rb
wlog-1.0.5 lib/wlog/db_registry.rb