|
|
|
@ -2,11 +2,24 @@ DEFAULT_DB_PATH = "db/main.db" |
|
|
|
|
|
|
|
|
|
require_relative "db_models.rb" |
|
|
|
|
|
|
|
|
|
class Table |
|
|
|
|
attr_reader :name |
|
|
|
|
attr_accessor :db |
|
|
|
|
def initialize(db, name) |
|
|
|
|
@db = db |
|
|
|
|
@name = name |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
class Database # Database class |
|
|
|
|
attr_reader :path |
|
|
|
|
def initialize(table_structure, db_path=DEFAULT_DB_PATH) |
|
|
|
|
attr_reader :name, :path |
|
|
|
|
def initialize(name, table_structure, db_path=DEFAULT_DB_PATH) |
|
|
|
|
@name = name |
|
|
|
|
@path = db_path |
|
|
|
|
# generate table_structure if it doesn't exist |
|
|
|
|
|
|
|
|
|
@tables = {} |
|
|
|
|
# generate table objects |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
private def db |
|
|
|
@ -37,17 +50,17 @@ class Database # Database class |
|
|
|
|
self.query query # execute query |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def update(table, data, filter="") |
|
|
|
|
def update(table, data, filter="") # Updates the table with specified data hash |
|
|
|
|
self.query( "UPDATE #{table} SET #{ self.gen_update_query(data.keys) } WHERE #{filter}", *data.values ) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def insert(table, data, filter="") |
|
|
|
|
def insert(table, data, filter="") # Inserts new data into the table |
|
|
|
|
entstr, valstr = gen_insert_query data.keys |
|
|
|
|
self.query( "INSERT INTO #{table} #{entstr} VALUES #{valstr}", *data.values ) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
# sets or updates a specific field in the table |
|
|
|
|
def set(table, data={}, filter="") # slow, shouldn't really be used |
|
|
|
|
def set(table, data, filter="") # slower but more lazy |
|
|
|
|
if self.get(table, filter).length > 0 then |
|
|
|
|
self.update(table, data, filter) |
|
|
|
|
else |
|
|
|
@ -55,7 +68,3 @@ class Database # Database class |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
class TableModel |
|
|
|
|
|
|
|
|
|
end |
|
|
|
|