Class: Turntables::DbRegistry

Inherits:
Object
  • Object
show all
Includes:
Singleton, DbRegistrySql
Defined in:
lib/turntables/db_registry.rb

Overview

Author:

  • Simon Symeonidis

Constant Summary

Constants included from DbRegistrySql

Turntables::DbRegistrySql::ExistsSql

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dbname = "default.db") ⇒ DbRegistry

Init with default db name TODO we need to be able to set this somehow differently - applications might require to name their database with their own specific name.

Parameters:

  • dbname (defaults to: "default.db")

    is the name of the database is if it not specified



17
18
19
# File 'lib/turntables/db_registry.rb', line 17

def initialize(dbname="default.db")
  @name = dbname
end

Instance Attribute Details

#nameObject

The database name



69
70
71
# File 'lib/turntables/db_registry.rb', line 69

def name
  @name
end

Instance Method Details

#close!Object

Close the current database.



57
58
59
# File 'lib/turntables/db_registry.rb', line 57

def close!
  get_handle.close unless get_handle.closed?
end

#execute(*sql) ⇒ Object

Execute (any sort) of sql

Examples:

Simple usage

sql = "INSERT INTO person (name, surname) values (?,?)"
DbRegistry.instance.execute(sql,"jon","doe")

Parameters:

  • sql

    is the multiple arguments of the function to execute. Usually you should give it first the sql you want that is to be prepared. Then you specify the variables to be set in the query, in the right order.

Returns:

  • sql data



29
30
31
32
33
# File 'lib/turntables/db_registry.rb', line 29

def execute(*sql)
  get_handle.execute(*sql)
rescue => ex
  print_exception ex
end

#execute_batch(sql) ⇒ Object

For special queries that may contain multiple statements. For example a query that contains first a ‘create table’ query, and then some inserts to poppulate that table. Ideally this should be used in order to create the tables in sequence.

Parameters:

  • sql

    is the sql that contains multiple statements



40
41
42
43
44
# File 'lib/turntables/db_registry.rb', line 40

def execute_batch(sql)
  get_handle.execute_batch(sql)
rescue => ex
  print_exception ex
end

#open!Object

Open the database, with the name given previously



64
65
66
# File 'lib/turntables/db_registry.rb', line 64

def open!
  @handle = SQLite3::Database.new(@name) if get_handle.closed?
end

#table_exists?(name) ⇒ Boolean

Check if a table exists in the database

Parameters:

  • name

    is the name of the table to check if exists

Returns:

  • (Boolean)

    true if table exists, false if not



49
50
51
52
# File 'lib/turntables/db_registry.rb', line 49

def table_exists?(name)
  val = get_handle.execute(ExistsSql, "table", name) 
  1 == val.flatten[0]
end