Class: SQLiteServer

Inherits:
Object
  • Object
show all
Includes:
SQLite3
Defined in:
lib/sqlite_server2018.rb

Instance Method Summary collapse

Constructor Details

#initialize(cache: 5, debug: false, filepath: '.') ⇒ SQLiteServer

Returns a new instance of SQLiteServer.



14
15
16
17
18
19
20
21
# 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

Instance Method Details

#execute(dbfile, *args, &blk) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 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) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
51
52
# 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

#load_db(dbfile) ⇒ Object



54
55
56
57
# File 'lib/sqlite_server2018.rb', line 54

def load_db(dbfile)
  read dbfile
  'loaded ' + dbfile
end

#pingObject



59
60
61
# File 'lib/sqlite_server2018.rb', line 59

def ping()
  :pong
end

#query(dbfile, *args, &blk) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# File 'lib/sqlite_server2018.rb', line 85

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) ⇒ Object



63
64
65
# File 'lib/sqlite_server2018.rb', line 63

def results_as_hash(dbfile)    
  read(dbfile).results_as_hash
end

#results_as_hash=(args) ⇒ Object



67
68
69
70
71
# File 'lib/sqlite_server2018.rb', line 67

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) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/sqlite_server2018.rb', line 73

def table_info(args)
  
  dbfile, s = args
  
  begin
    read(dbfile).table_info(s)
  rescue
    'SQLiteServerError: ' + ($!).inspect
  end    
  
end