Class: Turntables::DbRegistry
- Inherits:
-
Object
- Object
- Turntables::DbRegistry
- Includes:
- Singleton, DbRegistrySql
- Defined in:
- lib/turntables/db_registry.rb
Overview
Constant Summary
Constants included from DbRegistrySql
Turntables::DbRegistrySql::ExistsSql
Instance Attribute Summary collapse
-
#name ⇒ Object
The database name.
Instance Method Summary collapse
-
#close! ⇒ Object
Close the current database.
-
#execute(*sql) ⇒ Object
Execute (any sort) of sql.
-
#execute_batch(sql) ⇒ Object
For special queries that may contain multiple statements.
-
#initialize(dbname = "default.db") ⇒ DbRegistry
constructor
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.
-
#open! ⇒ Object
Open the database, with the name given previously.
-
#table_exists?(name) ⇒ Boolean
Check if a table exists in the database.
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.
17 18 19 |
# File 'lib/turntables/db_registry.rb', line 17 def initialize(dbname="default.db") @name = dbname end |
Instance Attribute Details
#name ⇒ Object
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
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.
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
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 |