Class: Sack::Database::Data
- Inherits:
-
Object
- Object
- Sack::Database::Data
- 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
-
#alter_table(name, fields) ⇒ Object
Alter Table: Adds missing fields to an already-existing table (ONLY adds, does not remove or modify existing fields).
-
#count(table) ⇒ Fixnum
Count: Counts the number of rows for a given table.
-
#create(table, fields) ⇒ Object
Create: Inserts fields as a row into a given table.
-
#create_table(name, fields) ⇒ Object
Create Table: Creates a table name defined by the fields hash.
-
#delete(table, id) ⇒ Object
Destroy By Field: Removes rows identified by id from a given table.
-
#delete_by(table, field, val) ⇒ Object
Destroy: Removes rows from a given table where field matches val.
-
#exec(q, params = []) ⇒ Object
Execute statement: Generic method to execute any SQL statement.
-
#fetch(table, id) ⇒ Array
Fetch: Fetches rows from a given table by id.
-
#fetch_all(table) ⇒ Array
Fetch All: Fetches all rows from a given table.
-
#fetch_by(table, field, val) ⇒ Array
Fetch By Field: Fetches rows from a given table where field matches val.
-
#find(table, id) ⇒ Object
Find: Fetches the first matching row from a given table by id.
-
#find_by(table, field, val) ⇒ Object
Find By Field: Fetches the first matching row from a given table where field matches val.
-
#hash_res(table, x) ⇒ Array
Pull Results into Hash: Converts rows returned by DBMS into Hashes matching the provided schema.
-
#initialize(connector, db, schema) ⇒ Data
constructor
Construct: Builds a Data object around a db instance, set to operate on schema.
-
#save(table, fields) ⇒ Object
Save: Creates or updates fields in a given table, depending on the presence of an id.
-
#update(table, id, fields) ⇒ Object
Update: Updates fields in rows identified by id in a given table.
-
#update_by(table, field, val, fields) ⇒ Object
Update By Field: Updates fields in rows where field matches val.
Constructor Details
#initialize(connector, db, schema) ⇒ Data
Construct: Builds a Data object around a db instance, set to operate on schema.
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).
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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 |