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 "sqlite3"
gem "slim" gem "slim"
gem "sassc" gem "sassc"
gem "colorize"

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

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

@ -7,6 +7,24 @@ class Table
def initialize(db, name, sql_file) def initialize(db, name, sql_file)
@db = db @db = db
@name = name @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
end end
@ -40,19 +58,27 @@ class Database # Database class
return entstr, valstr return entstr, valstr
end 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 def query(q, *args) # query table with query string
db.execute( q, *args ) db.execute( q, *args )
end end
def get(table, filter="") # get data from table def get(table, attr, filter="") # get data from table
query = "SELECT #{table} FROM #{table} " # create the query string q = "SELECT #{attr} FROM #{table}" # create the query string
if filter != "" then query += "WHERE #{filter}" end q = apply_filter(q, filter)
self.query query # execute query self.query query # execute query
end end
def update(table, data, filter="") # Updates the table with specified data hash 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 end
def insert(table, data, filter="") # Inserts new data into the table def insert(table, data, filter="") # Inserts new data into the table
@ -61,8 +87,8 @@ class Database # Database class
end end
# sets or updates a specific field in the table # sets or updates a specific field in the table
def set(table, data, filter="") # slower but more lazy def set(table, attr, data, filter="") # slower but more lazy
if self.get(table, filter).length > 0 then if self.get(table, attr, filter).length > 0 then
self.update(table, data, filter) self.update(table, data, filter)
else else
self.insert(table, data, filter) self.insert(table, data, filter)

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