Class: Sack::Database

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(connector, connstring, schema) ⇒ Database

Construct: Builds a Database on top of a backend connector and connstring, set to operate on schema.

Parameters:

  • connector (Object)

    Generic database backend connector

  • connstring (String)

    Connector-specific connection string

  • schema (Hash)

    Schema definition - see README



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/sack/database.rb', line 41

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.



99
100
101
102
103
104
105
106
# File 'lib/sack/database.rb', line 99

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.

Parameters:

  • data (Data)

    Data access interface

Returns:

  • (Object)

    True if schema is valid, False otherwise



85
86
87
# File 'lib/sack/database.rb', line 85

def check_schema data
	@schema.keys.inject(true) { |a, table| a && (data.exec "select count(*) from #{table};" rescue nil) }
end

#openObject

Open: Opens a session on the database for yielding.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/sack/database.rb', line 59

def open

	# Lock DB
	@lock.synchronize do

		# Open Database
		db = @connector.open @connstring

		# Yield Block
		yield Data.new(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.

Parameters:

  • data (Data)

    Data access interface



92
93
94
95
# File 'lib/sack/database.rb', line 92

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_schemaObject

Verify Schema: Verifies the supplied schema against the actual database & re-creates it if necessary.



77
78
79
# File 'lib/sack/database.rb', line 77

def verify_schema
	open { |data| rebuild_schema data unless check_schema data }
end