Debug & Database: Refactor debug module & database

master
E. Almqvist 3 years ago
parent 98b16a9008
commit 31f72e5608
  1. 1
      src/Gemfile
  2. 2
      src/Gemfile.lock
  3. 2
      src/app.rb
  4. 38
      src/database.rb
  5. 30
      src/debug.rb

@ -11,3 +11,4 @@ gem "sinatra-reloader"
gem "sqlite3"
gem "slim"
gem "sassc"
gem "colorize"

@ -1,6 +1,7 @@
GEM
remote: https://rubygems.org/
specs:
colorize (0.8.1)
ffi (1.15.5)
multi_json (1.15.0)
mustermann (1.1.1)
@ -35,6 +36,7 @@ PLATFORMS
x86_64-linux
DEPENDENCIES
colorize
sassc
sinatra
sinatra-reloader

@ -7,7 +7,9 @@ require "sinatra/reloader" if DEBUG
require "slim"
require "sqlite3"
require "sassc"
require "colorize"
require_relative "debug.rb"
require_relative "database.rb"
require_relative "db_models.rb"
require_relative "func.rb"

@ -7,6 +7,24 @@ class Table
def initialize(db, name, sql_file)
@db = db
@name = name
@sql_file = sql_file
end
def create_table
begin
q = File.read @sql_file # get SQL script
@db.query q # run query
rescue Errno::ENOENT => err
error "#{err}"
end
end
def get(attr, filter="")
@db.get(@name, attr, filter)
end
def insert(data, filter="")
@db.insert(@name, data, filter)
end
end
@ -40,19 +58,27 @@ class Database # Database class
return entstr, valstr
end
private def apply_filter(query, filter)
if filter != "" then query += " WHERE #{filter}" end
query
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
def get(table, attr, filter="") # get data from table
q = "SELECT #{attr} FROM #{table}" # create the query string
q = apply_filter(q, filter)
self.query query # execute query
end
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 )
q = "UPDATE #{table} SET #{self.gen_update_query(data.keys)}"
q = apply_filter(q, filter)
self.query(q, *data.values )
end
def insert(table, data, filter="") # Inserts new data into the table
@ -61,8 +87,8 @@ class Database # Database class
end
# sets or updates a specific field in the table
def set(table, data, filter="") # slower but more lazy
if self.get(table, filter).length > 0 then
def set(table, attr, data, filter="") # slower but more lazy
if self.get(table, attr, filter).length > 0 then
self.update(table, data, filter)
else
self.insert(table, data, filter)

@ -1,14 +1,24 @@
def log(str, *args)
puts "#{str}"
args.each do |s|
puts " :: #{s}"
module Console
def self.log(str, indent=4, *args)
puts "#{str}"
indentstr = " "*indent
args.each do |s|
puts "#{indentstr}:: #{s}"
end
end
end
def debug(str, *args)
if DEBUG then
print "DEBUG :: "
log "#{str}", *args
def self.debug(str, *args)
if DEBUG then
prefix = "[DEBUG] "
print prefix.bold
log(str, prefix.length, *args)
end
end
end
def self.error(str, *args)
prefix = "[ERROR] "
print prefix.red.bold
log(str.bold, prefix.length, *args)
end
end

Loading…
Cancel
Save