Database: Entity class

master
E. Almqvist 3 years ago
parent 64480a7dca
commit b76fccd3e3
  1. 6
      src/db_init.rb
  2. 36
      src/lib/database.rb

@ -1,9 +1,11 @@
require_relative "db_models.rb" require_relative "db_models.rb"
LOAD_TABLES = [ LOAD_MODELS = [
User User
] ]
def db_init def db_init
db = Database.new("main", LOAD_TABLES) LOAD_MODELS.each do |model|
model.init_table # init all tables
end
end end

@ -8,9 +8,9 @@ class Entity
attr_reader :name, :path attr_reader :name, :path
attr_accessor :tables attr_accessor :tables
# Template # Creates the table
private def create_table def self.init_table
sql_file = "sql/tables/#{self.class.name}.sql" sql_file = "sql/tables/#{self.name}.sql"
begin begin
q = File.read sql_file # get SQL script q = File.read sql_file # get SQL script
@ -20,50 +20,50 @@ class Entity
end end
end end
private def gen_update_query(vars) # generates part of the update query string private def self.gen_update_query(vars) # generates part of the update query string
vars.join "= ?, " vars.join "= ?, "
end end
private def gen_insert_query(vars) # generates part of the insert query string private def self.gen_insert_query(vars) # generates part of the insert query string
entstr = "(#{vars.join ", "})" entstr = "(#{vars.join ", "})"
valstr = "(#{(["?"] * vars.length).join ", "})" valstr = "(#{(["?"] * vars.length).join ", "})"
return entstr, valstr return entstr, valstr
end end
private def apply_filter(query, filter) private def self.apply_filter(query, filter)
if filter != "" then query += " WHERE #{filter}" end if filter != "" then query += " WHERE #{filter}" end
query query
end end
private def query(q, *args) # query table with query string def self.query(q, *args) # query table with query string
db.execute( q, *args ) db.execute( q, *args )
end end
private def get(attr, filter="", *args) # get data from table def self.get(attr, filter="", *args) # get data from table
q = "SELECT #{attr} FROM #{self.class.name}" # create the query string q = "SELECT #{attr} FROM #{self.name}" # create the query string
q = apply_filter(q, filter) q = apply_filter(q, filter)
self.query q, *args # execute query self.query q, *args # execute query
end end
private def update(data, filter="") # Updates the table with specified data hash def self.update(data, filter="") # Updates the table with specified data hash
q = "UPDATE #{self.class.name} SET #{self.gen_update_query(data.keys)}" q = "UPDATE #{self.name} SET #{self.gen_update_query(data.keys)}"
q = apply_filter(q, filter) q = apply_filter(q, filter)
self.query(q, *data.values ) self.query(q, *data.values )
end end
private def insert(data) # Inserts new data into the table def self.insert(data) # Inserts new data into the table
entstr, valstr = gen_insert_query data.keys entstr, valstr = self.gen_insert_query data.keys
self.query( "INSERT INTO #{self.class.name} #{entstr} VALUES #{valstr}", *data.values ) self.query( "INSERT INTO #{self.name} #{entstr} VALUES #{valstr}", *data.values )
end end
private def set(attr, data, filter="") # slower but more lazy def self.set(attr, data, filter="") # slower but more lazy
if db.get(self.class.name, attr, filter).length > 0 then if self.get(attr, filter).length > 0 then
db.update(self.class.name, data, filter) self.update(data, filter)
else else
db.insert(self.class.name, data, filter) self.insert(data, filter)
end end
end end
end end

Loading…
Cancel
Save