Class: BEL::Resource::Search::Plugins::Sqlite::Sqlite3FTS

Inherits:
Object
  • Object
show all
Includes:
BEL::Resource::Search, API
Defined in:
lib/bel/resource/search/plugins/sqlite/sqlite3.rb

Constant Summary collapse

QUERY_TEMPLATE =
ERB.new(
  File.read(Pathname(File.dirname(__FILE__)) + 'sqlite3_template.erb'),
  nil,
  '>'
)
PREPARED_STATEMENTS =
{}

Instance Method Summary collapse

Constructor Details

#initialize(db) ⇒ Sqlite3FTS

Returns a new instance of Sqlite3FTS.



20
21
22
# File 'lib/bel/resource/search/plugins/sqlite/sqlite3.rb', line 20

def initialize(db)
  @db = db
end

Instance Method Details

#search(query_expression, concept_type = nil, scheme_uri = nil, species = nil, options = {}) ⇒ Object

See Also:

  • API#search


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/bel/resource/search/plugins/sqlite/sqlite3.rb', line 25

def search(query_expression, concept_type = nil, scheme_uri = nil, species = nil, options = {})
  # required SQL parameters
  size  = options.delete(:size) || -1
  if size.to_i.zero?
    fail ArgumentError.new(":size is zero")
  end

  # optional SQL parameters
  params = {
    :match        => query_expression.encode('UTF-8'),
    :start        => (options.delete(:start) || 0).to_i,
    :size         => size,
    :concept_type => (concept_type ? concept_type.to_s.encode('UTF-8') : nil),
    :scheme_uri   => (scheme_uri ? scheme_uri.to_s.encode('UTF-8') : nil),
    :species      => (species ? species.to_s.encode('UTF-8') : nil),
  }

  query   = QUERY_TEMPLATE.result(binding)
  enum    = @db.fetch(query, params)

  if enum.respond_to? :lazy
    enum = enum.lazy
  end

  enum.map { |row|
    row_hash = row.to_hash
    row_hash[:alt_labels] = (row_hash[:alt_labels] || '').split('|')
    SearchResult.new(row_hash)
  }
end