Class: DbchainClient::Querier

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

Instance Method Summary collapse

Constructor Details

#initialize(base_url, private_key, app_code = nil) ⇒ Querier

Returns a new instance of Querier.



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/dbchain_client/querier.rb', line 3

def initialize(base_url, private_key, app_code=nil)
  @base_url = base_url

  if private_key.instance_of? String
    @private_key = PrivateKey.new(private_key)
  else
    @private_key = private_key
  end

  set_app_code unless app_code.nil?
  @single_value = false
end

Instance Method Details

#equal(field_name, value) ⇒ Object



72
73
74
# File 'lib/dbchain_client/querier.rb', line 72

def equal(field_name, value)
  where(field_name, value, '=')
end

#find(id) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/dbchain_client/querier.rb', line 29

def find(id)
  @single_value = true
  h = {
    method: 'find',
    id: id
  }
  finish(h)
end

#find_firstObject



38
39
40
41
42
43
44
# File 'lib/dbchain_client/querier.rb', line 38

def find_first
  @single_value = true
  h = {
    method: 'first',
  }
  finish(h)
end

#find_lastObject



46
47
48
49
50
51
52
# File 'lib/dbchain_client/querier.rb', line 46

def find_last
  @single_value = true
  h = {
    method: 'last',
  }
  finish(h)
end

#ownObject



76
77
78
79
# File 'lib/dbchain_client/querier.rb', line 76

def own()
  from_address = @private_key.public_key.address
  equal('created_by', from_address)
end

#runObject



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/dbchain_client/querier.rb', line 81

def run
  reader = DbchainClient::Reader.new(@base_url, @private_key)
  result = reader.querier(@q[:app_code], @q[:commands])
  if @single_value
    if result.size > 0
      result[0]
    else
      nil
    end
  else
    result
  end
end

#select(*args) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/dbchain_client/querier.rb', line 54

def select(*args)
  h = {
    method: 'select',
    fields: args.join(',')
  }
  finish(h)
end

#set_app_code(app_code) ⇒ Object



16
17
18
19
# File 'lib/dbchain_client/querier.rb', line 16

def set_app_code(app_code)
  initialize_internal_querier if @q.nil?
  @q[:app_code] = app_code
end

#table(table_name) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/dbchain_client/querier.rb', line 21

def table(table_name)
  h = {
    method: 'table',
    table: table_name
  }
  finish(h)
end

#where(field_name, value, operator) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/dbchain_client/querier.rb', line 62

def where(field_name, value, operator)
  h = {
    method: 'where',
    field: field_name,
    value: value,
    operator: operator
  }
  finish(h)
end