class SQLiteServer

Public Class Methods

new(cache: 5, debug: false, filepath: '.') click to toggle source
# File lib/sqlite_server2018.rb, line 14
def initialize(cache: 5, debug: false, filepath: '.')
  
  @dbcache = HashCache.new(cache: cache)
  @h = {}
  @debug = debug
  Dir.chdir filepath    
  
end

Public Instance Methods

execute(dbfile, *args, &blk) click to toggle source
# File lib/sqlite_server2018.rb, line 23
def execute(dbfile, *args, &blk)
  
  p 'dbfile: ' + dbfile if @debug
  
  if @debug then
    p 'db: ' + db.inspect
    p 'args: ' + args.inspect
  end
  
  begin      
    
    r = read(dbfile).execute(*args).map(&:to_a)
    blk ? r.each {|*x| blk.call(*x) } : r

  rescue
    'SQLiteServerError: ' + ($!).inspect
  end

end
exists?(raw_dbfile) click to toggle source
# File lib/sqlite_server2018.rb, line 43
def exists?(raw_dbfile)
  
  dbfile = if raw_dbfile =~ /^sqlite:\/\// then    
    raw_dbfile[/^sqlite:\/\/[^\/]+\/(.*)/,1]
  else
    raw_dbfile      
  end
  
  File.exists? dbfile
end
fields(*args) click to toggle source
# File lib/sqlite_server2018.rb, line 54
def fields(*args)
  
  dbfile, table = args
  
  begin
    read(dbfile).table_info(table).map {|x| x['name'].to_sym }
  rescue
    'SQLiteServerError: ' + ($!).inspect
  end    
  
end
load_db(dbfile) click to toggle source
# File lib/sqlite_server2018.rb, line 66
def load_db(dbfile)
  read dbfile
  'loaded ' + dbfile
end
ping() click to toggle source
# File lib/sqlite_server2018.rb, line 71
def ping()
  :pong
end
query(dbfile, *args, &blk) click to toggle source
# File lib/sqlite_server2018.rb, line 110
def query(dbfile, *args, &blk)
  
  begin
    r = read(dbfile).query(*args).map(&:to_a)
    blk ? r.each {|*x| blk.call(*x) } : r      
  rescue
    'SQLiteServerError: ' + ($!).inspect
  end
  
end
results_as_hash(dbfile) click to toggle source
# File lib/sqlite_server2018.rb, line 75
def results_as_hash(dbfile)    
  read(dbfile).results_as_hash
end
results_as_hash=(args) click to toggle source
# File lib/sqlite_server2018.rb, line 79
def results_as_hash=(args)
  dbfile, val = args
  puts 'inside results_as_hash=' + val.inspect if @debug
  read(dbfile).results_as_hash = val
end
table_info(*args) click to toggle source
# File lib/sqlite_server2018.rb, line 85
def table_info(*args)
  
  dbfile, s = args
  
  begin
    read(dbfile).table_info(s)
  rescue
    'SQLiteServerError: ' + ($!).inspect
  end    
  
end
tables(dbfile) click to toggle source
# File lib/sqlite_server2018.rb, line 97
def tables(dbfile)
      
  begin
    sql = "SELECT name FROM sqlite_master WHERE type='table';"
    a = read(dbfile).execute(sql).flat_map(&:to_a)
    sys = a.grep /^sqlite_/
    (a - sys)
  rescue
    'SQLiteServerError: ' + ($!).inspect
  end    
      
end

Private Instance Methods

read(dbfile) click to toggle source
# File lib/sqlite_server2018.rb, line 123
def read(dbfile)
  @dbcache.read(dbfile) { Database.new dbfile }
end