|
|
|
@ -1,24 +1,23 @@ |
|
|
|
|
DEFAULT_DB_PATH = "db/main.db" |
|
|
|
|
|
|
|
|
|
class Database # Database class |
|
|
|
|
def db |
|
|
|
|
dbbuf = SQLite3::Database.new DB_PATH |
|
|
|
|
dbbuf.results_as_hash = true |
|
|
|
|
dbbuf |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
class Entity |
|
|
|
|
attr_reader :name, :path |
|
|
|
|
attr_accessor :tables |
|
|
|
|
def initialize(name, load_tables=[], db_path=DEFAULT_DB_PATH) |
|
|
|
|
@name = name |
|
|
|
|
@path = db_path |
|
|
|
|
|
|
|
|
|
@tables = {} |
|
|
|
|
# generate table objects |
|
|
|
|
load_tables.each do |table_model| |
|
|
|
|
tblobj = table_model.new(self) |
|
|
|
|
@tables[tblobj.name.to_sym] = tblobj |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
# Template |
|
|
|
|
private def create_table |
|
|
|
|
sql_file = "sql/tables/#{self.class.name}.sql" |
|
|
|
|
|
|
|
|
|
private def db |
|
|
|
|
dbbuf = SQLite3::Database.new @path |
|
|
|
|
dbbuf.results_as_hash = true |
|
|
|
|
dbbuf |
|
|
|
|
begin |
|
|
|
|
q = File.read sql_file # get SQL script |
|
|
|
|
self.db.query q # run query |
|
|
|
|
rescue Errno::ENOENT => err |
|
|
|
|
error "#{err}" |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
private def gen_update_query(vars) # generates part of the update query string |
|
|
|
@ -37,73 +36,34 @@ class Database # Database class |
|
|
|
|
query |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def query(q, *args) # query table with query string |
|
|
|
|
private def query(q, *args) # query table with query string |
|
|
|
|
db.execute( q, *args ) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def get(table, attr, filter="", *args) # get data from table |
|
|
|
|
q = "SELECT #{attr} FROM #{table}" # create the query string |
|
|
|
|
private def get(attr, filter="", *args) # get data from table |
|
|
|
|
q = "SELECT #{attr} FROM #{self.class.table_name}" # create the query string |
|
|
|
|
q = apply_filter(q, filter) |
|
|
|
|
|
|
|
|
|
self.query q, *args # execute query |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def update(table, data, filter="") # Updates the table with specified data hash |
|
|
|
|
q = "UPDATE #{table} SET #{self.gen_update_query(data.keys)}" |
|
|
|
|
private def update(data, filter="") # Updates the table with specified data hash |
|
|
|
|
q = "UPDATE #{self.class.table_name} SET #{self.gen_update_query(data.keys)}" |
|
|
|
|
q = apply_filter(q, filter) |
|
|
|
|
|
|
|
|
|
self.query(q, *data.values ) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def insert(table, data) # Inserts new data into the table |
|
|
|
|
private def insert(data) # Inserts new data into the table |
|
|
|
|
entstr, valstr = gen_insert_query data.keys |
|
|
|
|
self.query( "INSERT INTO #{table} #{entstr} VALUES #{valstr}", *data.values ) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def get_table(tablesym) |
|
|
|
|
@tables[tablesym] |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
class Table |
|
|
|
|
attr_reader :name |
|
|
|
|
attr_accessor :db |
|
|
|
|
|
|
|
|
|
def initialize(db, name) |
|
|
|
|
@db = db |
|
|
|
|
@name = name |
|
|
|
|
@sql_file = "sql/tables/#{name}.sql" |
|
|
|
|
|
|
|
|
|
begin |
|
|
|
|
q = File.read @sql_file # get SQL script |
|
|
|
|
@db.query q # run query |
|
|
|
|
rescue Errno::ENOENT => err |
|
|
|
|
error "#{err}" |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
# these methods are private because they |
|
|
|
|
# are intended to be accessed through a |
|
|
|
|
# "Table Model". |
|
|
|
|
# See "db_models.rb" |
|
|
|
|
private def get(attr, filter="", *args) |
|
|
|
|
@db.get(@name, attr, filter, *args) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
private def insert(data) |
|
|
|
|
@db.insert(@name, data) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
private def update(data, filter="") |
|
|
|
|
@db.update(@name, data, filter) |
|
|
|
|
self.query( "INSERT INTO #{self.class.table_name} #{entstr} VALUES #{valstr}", *data.values ) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
# sets or updates a specific field in the table |
|
|
|
|
private def set(attr, data, filter="") # slower but more lazy |
|
|
|
|
if @db.get(@name, attr, filter).length > 0 then |
|
|
|
|
@db.update(@name, data, filter) |
|
|
|
|
if db.get(self.class.table_name, attr, filter).length > 0 then |
|
|
|
|
db.update(self.class.table_name, data, filter) |
|
|
|
|
else |
|
|
|
|
@db.insert(@name, data, filter) |
|
|
|
|
db.insert(self.class.table_name, data, filter) |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|