From 2d86e1d139e5b550e0e4d6b67eb3e5f41d27bf72 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Fri, 4 Feb 2022 14:09:10 +0100 Subject: [PATCH] Refactor --- src/database.rb | 60 ++++++++++++++++++++++++++++++++++++++++++++---- src/db_models.rb | 49 --------------------------------------- 2 files changed, 56 insertions(+), 53 deletions(-) diff --git a/src/database.rb b/src/database.rb index a45079f..dd954ef 100644 --- a/src/database.rb +++ b/src/database.rb @@ -2,8 +2,60 @@ DEFAULT_DB_PATH = "db/main.db" require_relative "db_models.rb" -def db_handle - db = SQLite3::Database.new DEFAULT_DB_PATH - db.results_as_hash = true - return db +class Database # Database class + attr_reader :path + def initialize(table_structure, db_path=DEFAULT_DB_PATH) + @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 diff --git a/src/db_models.rb b/src/db_models.rb index 190d1f9..e69de29 100644 --- a/src/db_models.rb +++ b/src/db_models.rb @@ -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