class Sack::Database::Data

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

Public Class Methods

new(connector, db, schema) click to toggle source

Construct: Builds a Data object around a db instance, set to operate on schema. @param [Object] db Database instance obtained by DBMS file @param [Hash] schema Schema definition - see README

# File lib/sack/database/data.rb, line 27
def initialize connector, db, schema
        @connector = connector
        @db = db
        @schema = schema
end

Public Instance Methods

alter_table(name, fields) click to toggle source

Alter Table: Adds missing fields to an already-existing table (ONLY adds, does not remove or modify existing fields). @param [Symbol] name Table name @param [Hash] fields A hash mapping of field names to type definition arrays (from FTYPES)

# 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) click to toggle source

Count: Counts the number of rows for a given table. @param [Symbol] table Table name @return [Fixnum] The number of rows present in the given table

# 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) click to toggle source

Create: Inserts fields as a row into a given table. @param [Symbol] table Table name @param [Hash] fields Fields to be inserted

# 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) click to toggle source

Create Table: Creates a table name defined by the fields hash. @param [Symbol] name Table name @param [Hash] fields A hash mapping of field names to type definition arrays (from FTYPES)

# 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) click to toggle source

Destroy By Field: Removes rows identified by id from a given table. @param [Symbol] table Table name @param [Object] id ID of rows to be removed

# 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) click to toggle source

Destroy: Removes rows from a given table where field matches val. @param [Symbol] table Table name @param [Symbol] field Field name @param [Object] val Field value

# 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 = []) click to toggle source

Execute statement: Generic method to execute any SQL statement. @param [String] q Statement to be executed @param [Array] params Statement parameters @return [Object] Whatever the statement returned

# File lib/sack/database/data.rb, line 158
def exec q, params = []
        @connector.exec @db, Statement.prep(q, params)
end
fetch(table, id) click to toggle source

Fetch: Fetches rows from a given table by id. @param [Symbol] table Table name @param [Object] id ID on which to filter @return [Array] An array of Hashes, each representing one row

# 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) click to toggle source

Fetch All: Fetches all rows from a given table. @param [Symbol] table Table name @return [Array] An array of Hashes, each representing one row

# 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) click to toggle source

Fetch By Field: Fetches rows from a given table where field matches val. @param [Symbol] table Table name @param [Symbol] field Field name @param [Object] val Field value @return [Array] An array of Hashes, each representing one row

# 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) click to toggle source

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

# File lib/sack/database/data.rb, line 60
def find table, id
        fetch(table, id).try :first
end
find_by(table, field, val) click to toggle source

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

# 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) click to toggle source

Pull Results into Hash: Converts rows returned by DBMS into Hashes matching the provided schema. @param [Symbol] table Table name @param [Array] x Results returned by DBMS @return [Array] An array of Hashes, each representing a single row

# 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) click to toggle source

Save: Creates or updates fields in a given table, depending on the presence of an id. @param [Symbol] table Table name @param [Hash] fields Fields to be inserted / updated

# 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) click to toggle source

Update: Updates fields in rows identified by id in a given table. @param [Symbol] table Table name @param [Object] id ID on which to filter @param [Hash] fields Fields to be updated

# 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) click to toggle source

Update By Field: Updates fields in rows where field matches val. @param [Symbol] table Table name @param [Symbol] field Field name @param [Object] val Field value @param [Hash] fields Fields to be updated

# 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