Class: DRbSQLite

Inherits:
Object
  • Object
show all
Defined in:
lib/drb_sqlite.rb

Instance Method Summary collapse

Constructor Details

#initialize(raw_dbfile = nil, host: nil, port: '57000', debug: false) ⇒ DRbSQLite

Returns a new instance of DRbSQLite.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/drb_sqlite.rb', line 15

def initialize(raw_dbfile=nil, host: nil, port: '57000', debug: false)
  
    
  @host, @port, @debug = host, port, debug
  
  puts 'inside DRbSQLite'.info if @debug
  
  DRb.start_service
  
  if raw_dbfile then
    load_db(raw_dbfile)
  else
    @server = DRbObject.new nil, "druby://#{host}:#{port}"
  end    
      
end

Instance Method Details

#execute(*args, &blk) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/drb_sqlite.rb', line 32

def execute(*args, &blk)
  
  r = @server.execute @dbfile, *args, &blk
  
  if r.is_a? String and r =~ /^SQLiteServerError:/ then
    raise SQLiteServerError, r[/(?<=^SQLiteServerError: )(.*)/]
  end
  
  r
end

#exists?(dbfile) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/drb_sqlite.rb', line 43

def exists?(dbfile)
  @server.exists? dbfile
end

#fields(table) ⇒ Object



47
48
49
# File 'lib/drb_sqlite.rb', line 47

def fields(table)
  @server.fields @dbfile, table
end

#load_db(raw_dbfile) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/drb_sqlite.rb', line 51

def load_db(raw_dbfile)
  
  puts 'DRbSQLite | inside load_db'.info if @debug
  
  if raw_dbfile =~ /^sqlite:\/\// then
    host, @dbfile = raw_dbfile\
        .match(/(?<=^sqlite:\/\/)([^\/]+)\/(.*)/).captures
    
    if @debug then
      puts ('host: ' + host.inspect).debug
      puts ('@dbfile: ' + @dbfile.inspect).debug
    end
    
    @server = DRbObject.new nil, "druby://#{host}:#{@port}"
  else
    @dbfile = raw_dbfile
    @server = DRbObject.new nil, "druby://#{@host}:#{@port}"
  end

  @server.load_db(@dbfile)
end

#query(*args, &blk) ⇒ Object



90
91
92
93
94
95
96
97
98
99
# File 'lib/drb_sqlite.rb', line 90

def query(*args, &blk)
  
  r = @server.query @dbfile, *args, &blk
  
  if r.is_a? String and r =~ /^SQLiteServerError:/ then
    raise SQLiteServerError, r[/(?<=^SQLiteServerError: )(.*)/]
  end
  
  r    
end

#results_as_hashObject



73
74
75
# File 'lib/drb_sqlite.rb', line 73

def results_as_hash()
  @server.results_as_hash @dbfile
end

#results_as_hash=(val) ⇒ Object



77
78
79
80
# File 'lib/drb_sqlite.rb', line 77

def results_as_hash=(val)
  puts 'inside results_as_hash=()'.info if @debug
  @server.results_as_hash = [@dbfile, val]
end

#table_info(s) ⇒ Object



82
83
84
# File 'lib/drb_sqlite.rb', line 82

def table_info(s)
  @server.table_info @dbfile, s
end

#tablesObject



86
87
88
# File 'lib/drb_sqlite.rb', line 86

def tables()
  @server.tables @dbfile
end