master
E. Almqvist 3 years ago
parent 9e1bf5a520
commit 2d86e1d139
  1. 60
      src/database.rb
  2. 49
      src/db_models.rb

@ -2,8 +2,60 @@ DEFAULT_DB_PATH = "db/main.db"
require_relative "db_models.rb" require_relative "db_models.rb"
def db_handle class Database # Database class
db = SQLite3::Database.new DEFAULT_DB_PATH attr_reader :path
db.results_as_hash = true def initialize(table_structure, db_path=DEFAULT_DB_PATH)
return db @path = db_path
# generate table_structure if it doesn't exist
end
private def db
dbbuf = SQLite3::Database.new @path
dbbuf.results_as_hash = true
dbbuf
end
private def gen_update_query(vars) # generates part of the update query string
vars.join "= ?, "
end
private def gen_insert_query(vars) # generates part of the insert query string
entstr = "(#{vars.join ", "})"
valstr = "(#{(["?"] * vars.length).join ", "})"
return entstr, valstr
end
def query(q, *args) # query table with query string
db.execute( q, *args )
end
def get(table, filter="") # get data from table
query = "SELECT #{table} FROM #{table} " # create the query string
if filter != "" then query += "WHERE #{filter}" end
self.query query # execute query
end
def update(table, data, filter="")
self.query( "UPDATE #{table} SET #{ self.gen_update_query(data.keys) } WHERE #{filter}", *data.values )
end
def insert(table, data, filter="")
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
if self.get(table, filter).length > 0 then
self.update(table, data, filter)
else
self.insert(table, data, filter)
end
end
end
class TableModel
end end

@ -1,49 +0,0 @@
class TableModel # Base model
attr_reader :table_name
def initialize(table_name)
@table_name = table_name
end
private def gen_update_query(vars) # generates part of the update query string
vars.join "= ?, "
end
private def gen_insert_query(vars) # generates part of the insert query string
entstr = "(#{vars.join ", "})"
valstr = "(#{(["?"] * vars.length).join ", "})"
return entstr, valstr
end
private def query(q, *args) # query table with query string
db_handle.execute( q, *args )
end
private def get(attr, filter="") # get data from table
query = "SELECT #{attr} FROM #{@table_name} " # create the query string
if filter != "" then query += "WHERE #{filter}" end
self.query query # execute query
end
private def update(attr, data, filter="")
self.query( "UPDATE #{@table_name} SET #{ self.gen_update_query(data.keys) } WHERE #{filter}", *data.values )
end
private def insert(attr, data, filter="")
entstr, valstr = gen_insert_query data.keys
self.query( "INSERT INTO #{@table_name} #{entstr} VALUES #{valstr}", *data.values )
end
private def set(attr, data={}, filter="") # slow, shouldn't really be used
if self.get(attr, filter).length > 0 then
self.update(attr, data, filter)
else
self.insert(attr, data, filter)
end
end
end
class User < TableModel
end
Loading…
Cancel
Save