Class: Wlog::DbRegistry

Inherits:
Object
  • Object
show all
Includes:
StaticConfigurations
Defined in:
lib/wlog/db_registry.rb

Overview

Author:

  • Simon Symeonidis

Constant Summary

Constants included from StaticConfigurations

StaticConfigurations::AppDirectory, StaticConfigurations::AppName, StaticConfigurations::ConfigDirectory, StaticConfigurations::ConfigFile, StaticConfigurations::DataDirectory, StaticConfigurations::DefaultDb, StaticConfigurations::TaintFile, StaticConfigurations::TemplateDir

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dbname) ⇒ DbRegistry

Returns a new instance of DbRegistry.



12
13
14
# File 'lib/wlog/db_registry.rb', line 12

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

Instance Attribute Details

#handleObject

the database handle



33
34
35
# File 'lib/wlog/db_registry.rb', line 33

def handle
  @handle
end

Instance Method Details

#execute(*sql) ⇒ Object

execute a sql with varargs parameters

Examples:

Simple Usage

DbRegistry.execute("SELECT * FROM table WHERE id = ?", 1)

Parameters:

  • *sql,

    first the sql string, then the parameters if the statement is to be prepared.



21
22
23
# File 'lib/wlog/db_registry.rb', line 21

def execute(*sql)
  @handle.execute(*sql)
end

#last_row_from(tablename) ⇒ Object

Get the last row, given a table name. The table needs to have an id



26
27
28
29
30
# File 'lib/wlog/db_registry.rb', line 26

def last_row_from(tablename)
  query = "SELECT * FROM #{tablename} WHERE id =(SELECT MAX(id) FROM"\
    " #{tablename});"
  @handle.execute(query)
end