Database: Rename to Entity & .name for models

master
E. Almqvist 3 years ago
parent e0a927449d
commit f734030169
  1. 1
      src/app.rb
  2. 4
      src/config.rb
  3. 6
      src/db_models.rb
  4. 92
      src/lib/database.rb
  5. 1
      src/views/404.slim

@ -21,7 +21,6 @@ require_relative "db_models.rb"
enable :sessions
db = db_init
# Routes
get "/style.css" do
sass :"stylesheets/style", style: :compressed

@ -1,3 +1,6 @@
# DB stuff
DB_PATH = "db/main.db"
# Register stuff
MIN_PASSWORD_LEN = 8
MIN_NAME_LEN = 2
@ -16,6 +19,7 @@ REGISTER_ERRORS = {
email_fake: "Please use a valid email address"
}
# Login stuff
LOGIN_ERRORS = {
fields: "Please fill all of the fields",
fail: "Wrong password and/or email"

@ -1,9 +1,5 @@
# User table model
class User < Table
def initialize(db)
super db, "User"
end
class User < Entity
# Find user by ID, returns multiple results if multiple IDs exist
# (which wont happen since IDs are unique)
def find_by_id(id)

@ -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

Loading…
Cancel
Save