Class: Sack::Database
- Inherits:
-
Object
- Object
- Sack::Database
- Defined in:
- lib/sack/database.rb,
lib/sack/database/data.rb,
lib/sack/database/model.rb,
lib/sack/database/ftypes.rb,
lib/sack/database/schema.rb,
lib/sack/database/generator.rb,
lib/sack/database/sanitizer.rb,
lib/sack/database/statement.rb,
lib/sack/database/model/data.rb,
lib/sack/database/model/validation.rb,
lib/sack/database/model/relationships.rb,
lib/sack/database/model/relationships/has_many.rb,
lib/sack/database/model/relationships/belongs_to.rb
Overview
Database Class
Defined Under Namespace
Modules: Generator, Model, Sanitizer, Schema, Statement Classes: Data
Constant Summary collapse
- ACTIONS =
Actions: Allowed data methods
[ :create_table, :count, :find, :find_by, :fetch, :fetch_by, :fetch_all, :create, :update, :update_by, :save, :delete, :delete_by ]
- FTYPES =
Field Types: Available field types to be used when defining schemas.
{ str: 'VARCHAR(255)', txt: 'TEXT', int: 'INTEGER', flo: 'REAL', uniq: 'UNIQUE', pk: 'PRIMARY KEY', ai: 'AUTOINCREMENT' }
- FTYPES_CLASSES =
Ruby Type Conversions: Maps Field Types to Ruby Classes.
{ str: String, txt: String, int: Fixnum, flo: Float }
Instance Method Summary collapse
-
#check_schema(data) ⇒ Object
Check Schema: Verifies the supplied schema against the actual database.
-
#initialize(connector, connstring, schema) ⇒ Database
constructor
Construct: Builds a Database on top of a backend connector and connstring, set to operate on schema.
-
#method_missing(name, *args) ⇒ Object
Method Missing: Catches and routes database actions through data access interface.
-
#open ⇒ Object
Open: Opens a session on the database for yielding.
-
#rebuild_schema(data) ⇒ Object
Rebuild Schema: Re-creates the database according to the supplied schema.
-
#verify_schema ⇒ Object
Verify Schema: Verifies the supplied schema against the actual database & re-creates it if necessary.
Constructor Details
#initialize(connector, connstring, schema) ⇒ Database
Construct: Builds a Database on top of a backend connector and connstring, set to operate on schema.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/sack/database.rb', line 40 def initialize connector, connstring, schema # Set Connector & Connstring @connector = connector @connstring = connstring # Set Schema @schema = schema # Create Lock @lock = Mutex.new # Verify Schema verify_schema end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
Method Missing: Catches and routes database actions through data access interface.
98 99 100 101 102 103 104 105 |
# File 'lib/sack/database.rb', line 98 def method_missing name, *args # Check Action raise "Unknown action [#{name}]" unless ACTIONS.include? name # Open Database { Perform Action } open { |db| return db.send name, *args } end |
Instance Method Details
#check_schema(data) ⇒ Object
Check Schema: Verifies the supplied schema against the actual database.
84 85 86 |
# File 'lib/sack/database.rb', line 84 def check_schema data @schema.keys.inject(true) { |a, table| a && (data.exec "select count(*) from #{table};" rescue nil) } end |
#open ⇒ Object
Open: Opens a session on the database for yielding.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/sack/database.rb', line 58 def open # Lock DB @lock.synchronize do # Open Database db = @connector.open @connstring # Yield Block yield Data.new(@connector, db, @schema) if block_given? # Close Database db.close end end |
#rebuild_schema(data) ⇒ Object
Rebuild Schema: Re-creates the database according to the supplied schema.
91 92 93 94 |
# File 'lib/sack/database.rb', line 91 def rebuild_schema data @schema.each { |table, fields| data.create_table table, fields rescue data.alter_table table, fields } raise "Rebuilding Schema Failed - Check Database & Access Permissions" unless check_schema data end |
#verify_schema ⇒ Object
Verify Schema: Verifies the supplied schema against the actual database & re-creates it if necessary.
76 77 78 |
# File 'lib/sack/database.rb', line 76 def verify_schema open { |data| rebuild_schema data unless check_schema data } end |