Class: Sack::Database::Data

Inherits:
Object
  • Object
show all
Defined in:
lib/sack/database/data.rb

Overview

Data Class: Internal Class presented as Data Access Layer when yielding in Database.open.

Instance Method Summary collapse

Constructor Details

#initialize(connector, db, schema) ⇒ Data

Construct: Builds a Data object around a db instance, set to operate on schema.

Parameters:

  • db (Object)

    Database instance obtained by DBMS file

  • schema (Hash)

    Schema definition - see README



27
28
29
30
31
# File 'lib/sack/database/data.rb', line 27

def initialize connector, db, schema
	@connector = connector
	@db = db
	@schema = schema
end

Instance Method Details

#alter_table(name, fields) ⇒ Object

Alter Table: Adds missing fields to an already-existing table (ONLY adds, does not remove or modify existing fields).

Parameters:

  • name (Symbol)

    Table name

  • fields (Hash)

    A hash mapping of field names to type definition arrays (from FTYPES)



46
47
48
# File 'lib/sack/database/data.rb', line 46

def alter_table name, fields
	fields.each { |fname, ftype| @connector.exec @db, "alter table #{Sanitizer.table @schema, name} add #{Sanitizer.field_name fname} #{ftype.respond_to?(:each) ? ftype.collect { |e| FTYPES[Sanitizer.ftype e] }.join(' ') : FTYPES[Sanitizer.ftype ftype]};" rescue nil }
end

#count(table) ⇒ Fixnum

Count: Counts the number of rows for a given table.

Parameters:

  • table (Symbol)

    Table name

Returns:

  • (Fixnum)

    The number of rows present in the given table



54
55
56
# File 'lib/sack/database/data.rb', line 54

def count table
	@connector.exec(@db, "select count(*) from #{Sanitizer.table @schema, table};")[0][0]
end

#create(table, fields) ⇒ Object

Create: Inserts fields as a row into a given table.

Parameters:

  • table (Symbol)

    Table name

  • fields (Hash)

    Fields to be inserted



101
102
103
# File 'lib/sack/database/data.rb', line 101

def create table, fields
	@connector.exec @db, Statement.prep("insert into #{Sanitizer.table @schema, table} (#{Generator.fields @schema, table, fields}) values (#{Generator.marks fields});", Generator.values(fields.values))
end

#create_table(name, fields) ⇒ Object

Create Table: Creates a table name defined by the fields hash.

Parameters:

  • name (Symbol)

    Table name

  • fields (Hash)

    A hash mapping of field names to type definition arrays (from FTYPES)



37
38
39
40
# File 'lib/sack/database/data.rb', line 37

def create_table name, fields
	fq = fields.collect { |fname, ftype| "#{Sanitizer.field_name fname} #{ftype.respond_to?(:each) ? ftype.collect { |e| FTYPES[Sanitizer.ftype e] }.join(' ') : FTYPES[Sanitizer.ftype ftype]}" }.join ', '
	@connector.exec @db, "create table #{Sanitizer.table @schema, name} (#{fq});"
end

#delete(table, id) ⇒ Object

Destroy By Field: Removes rows identified by id from a given table.

Parameters:

  • table (Symbol)

    Table name

  • id (Object)

    ID of rows to be removed



149
150
151
# File 'lib/sack/database/data.rb', line 149

def delete table, id
	@connector.exec @db, Statement.prep("delete from #{Sanitizer.table @schema, table} where id = ?;", [id])
end

#delete_by(table, field, val) ⇒ Object

Destroy: Removes rows from a given table where field matches val.

Parameters:

  • table (Symbol)

    Table name

  • field (Symbol)

    Field name

  • val (Object)

    Field value



141
142
143
# File 'lib/sack/database/data.rb', line 141

def delete_by table, field, val
	@connector.exec @db, Statement.prep("delete from #{Sanitizer.table @schema, table} where #{Sanitizer.field @schema, table, field} = ?;", [val])
end

#exec(q, params = []) ⇒ Object

Execute statement: Generic method to execute any SQL statement.

Parameters:

  • q (String)

    Statement to be executed

  • params (Array) (defaults to: [])

    Statement parameters

Returns:

  • (Object)

    Whatever the statement returned



158
159
160
# File 'lib/sack/database/data.rb', line 158

def exec q, params = []
	@connector.exec @db, Statement.prep(q, params)
end

#fetch(table, id) ⇒ Array

Fetch: Fetches rows from a given table by id.

Parameters:

  • table (Symbol)

    Table name

  • id (Object)

    ID on which to filter

Returns:

  • (Array)

    An array of Hashes, each representing one row



75
76
77
# File 'lib/sack/database/data.rb', line 75

def fetch table, id
	hash_res(table.to_sym, @connector.exec(@db, Statement.prep("select * from #{Sanitizer.table @schema, table} where id = ?;", [id])))
end

#fetch_all(table) ⇒ Array

Fetch All: Fetches all rows from a given table.

Parameters:

  • table (Symbol)

    Table name

Returns:

  • (Array)

    An array of Hashes, each representing one row



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

def fetch_all table
	hash_res(table.to_sym, @connector.exec(@db, "select * from #{Sanitizer.table @schema, table};"))
end

#fetch_by(table, field, val) ⇒ Array

Fetch By Field: Fetches rows from a given table where field matches val.

Parameters:

  • table (Symbol)

    Table name

  • field (Symbol)

    Field name

  • val (Object)

    Field value

Returns:

  • (Array)

    An array of Hashes, each representing one row



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

def fetch_by table, field, val
	hash_res(table.to_sym, @connector.exec(@db, Statement.prep("select * from #{Sanitizer.table @schema, table} where #{Sanitizer.field @schema, table, field} = ?;", [val])))
end

#find(table, id) ⇒ Object

Find: Fetches the first matching row from a given table by id.



60
61
62
# File 'lib/sack/database/data.rb', line 60

def find table, id
	fetch(table, id).try :first
end

#find_by(table, field, val) ⇒ Object

Find By Field: Fetches the first matching row from a given table where field matches val.



66
67
68
# File 'lib/sack/database/data.rb', line 66

def find_by table, field, val
	fetch_by(table, field, val).try :first
end

#hash_res(table, x) ⇒ Array

Pull Results into Hash: Converts rows returned by DBMS into Hashes matching the provided schema.

Parameters:

  • table (Symbol)

    Table name

  • x (Array)

    Results returned by DBMS

Returns:

  • (Array)

    An array of Hashes, each representing a single row



167
168
169
170
171
172
# File 'lib/sack/database/data.rb', line 167

def hash_res table, x
	x
		.collect { |r| @schema[table].keys.each_with_index.inject([]) { |a, e| a + [e[0], r[e[1]]] } }
		.collect { |r| Hash[*r] }
		.sym_keys rescue nil
end

#save(table, fields) ⇒ Object

Save: Creates or updates fields in a given table, depending on the presence of an id.

Parameters:

  • table (Symbol)

    Table name

  • fields (Hash)

    Fields to be inserted / updated



128
129
130
131
132
133
134
# File 'lib/sack/database/data.rb', line 128

def save table, fields
	if fields[:id]
		update table, fields.clone.delete(:id), fields
	else
		create table, fields
	end
end

#update(table, id, fields) ⇒ Object

Update: Updates fields in rows identified by id in a given table.

Parameters:

  • table (Symbol)

    Table name

  • id (Object)

    ID on which to filter

  • fields (Hash)

    Fields to be updated



110
111
112
# File 'lib/sack/database/data.rb', line 110

def update table, id, fields
	@connector.exec @db, Statement.prep("update #{Sanitizer.table @schema, table} set #{Generator.update_marks @schema, table, fields} where id = ?;", [Generator.values(fields.values), id].flatten)
end

#update_by(table, field, val, fields) ⇒ Object

Update By Field: Updates fields in rows where field matches val.

Parameters:

  • table (Symbol)

    Table name

  • field (Symbol)

    Field name

  • val (Object)

    Field value

  • fields (Hash)

    Fields to be updated



120
121
122
# File 'lib/sack/database/data.rb', line 120

def update_by table, field, val, fields
	@connector.exec @db, Statement.prep("update #{Sanitizer.table @schema, table} set #{Generator.update_marks @schema, table, fields} where #{Sanitizer.field @schema, table, field} = ?;", [Generator.values(fields.values), val].flatten)
end