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') ⇒ DRbSQLite

Returns a new instance of DRbSQLite.



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/drb_sqlite.rb', line 12

def initialize(raw_dbfile=nil, host: nil, port: '57000')
      
  @port = port
  
  DRb.start_service
  
  if raw_dbfile then
    load(raw_dbfile)
  else
    @server = DRbObject.new nil, "druby://#{host}:#{port}"
  end    
      
end

Instance Method Details

#execute(*args, &blk) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/drb_sqlite.rb', line 26

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)


37
38
39
# File 'lib/drb_sqlite.rb', line 37

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

#load(raw_dbfile) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/drb_sqlite.rb', line 41

def load(raw_dbfile)
  
  if raw_dbfile =~ /^sqlite:\/\// then
    host, @dbfile = raw_dbfile\
        .match(/(?<=^sqlite:\/\/)([^\/]+)\/(.*)/).captures
    @server = DRbObject.new nil, "druby://#{host}:#{@port}"
  else
    @dbfile = raw_dbfile
  end

  @server.load(@dbfile)
end

#query(*args, &blk) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/drb_sqlite.rb', line 54

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