Class: Solis::Query

Inherits:
Object
  • Object
show all
Includes:
Enumerable, QueryFilter
Defined in:
lib/solis/query.rb,
lib/solis/query/construct.rb

Defined Under Namespace

Classes: Construct, Runner

Class Method Summary collapse

Instance Method Summary collapse

Methods included from QueryFilter

#filter

Constructor Details

#initialize(model) ⇒ Query

Returns a new instance of Query.



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/solis/query.rb', line 57

def initialize(model)
  @construct_cache = File.absolute_path(Solis::Options.instance.get[:cache])
  @model = model
  @shapes = @model.class.shapes
  @metadata = @model.class.
  @sparql_endpoint = @model.class.sparql_endpoint
  @sparql_client = SPARQL::Client.new(@sparql_endpoint, graph: @model.class.graph_name)
  @filter = {values: ["VALUES ?type {#{target_class}}"], concepts: ['?concept a ?type .'] }
  @sort = 'ORDER BY ?s'
  @sort_select = ''
  @language = Graphiti.context[:object]&.language || Solis::Options.instance.get[:language] || 'en'
  @query_cache = Moneta.new(:HashFile, dir: @construct_cache)
end

Class Method Details

.run(entity, query) ⇒ Object



14
15
16
# File 'lib/solis/query.rb', line 14

def self.run(entity, query)
  Solis::Query::Runner.run(entity, query)
end

.run_construct(query, id_name, entity, ids, from_cache = '1') ⇒ Object



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
55
# File 'lib/solis/query.rb', line 27

def self.run_construct(query, id_name, entity, ids, from_cache = '1')
  raise 'Please supply one or more uuid\'s' if ids.nil? || ids.empty?

  result = {}

  key = uuid("#{entity}-#{ids}")

  if result.nil? || result.empty? || (from_cache.eql?('0'))
    ids = ids.split(',') if ids.is_a?(String)
    ids = [ids] unless ids.is_a?(Array)
    ids = ids.map do |m|
      if URI(m).class.eql?(URI::Generic)
        "<#{Solis::Options.instance.get[:graph_name]}#{entity.tableize}/#{m}>"
      else
        "<#{m}>"
      end
    end
    ids = ids.join(" ")

    language = Graphiti.context[:object]&.language || Solis::Options.instance.get[:language] || 'en'
    q = query.gsub(/{ ?{ ?VALUES ?} ?}/, "VALUES ?#{id_name} { #{ids} }").gsub(/{ ?{ ?LANGUAGE ?} ?}/, "bind(\"#{language}\" as ?filter_language).")

    result = Solis::Query.run(entity, q)
  end
  result
rescue StandardError => e
  puts e.message
  raise e
end

.run_construct_with_file(filename, id_name, entity, ids, from_cache = '1') ⇒ Object



18
19
20
21
# File 'lib/solis/query.rb', line 18

def self.run_construct_with_file(filename, id_name, entity, ids, from_cache = '1')
  f = File.read(filename)
  run_construct(f, id_name, entity, ids, from_cache)
end

.uuid(key) ⇒ Object



23
24
25
# File 'lib/solis/query.rb', line 23

def self.uuid(key)
  UUIDTools::UUID.sha1_create(UUIDTools::UUID_URL_NAMESPACE, key).to_s
end

Instance Method Details

#countObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/solis/query.rb', line 112

def count
  sparql_client = @sparql_client
  if model_construct?
    sparql_client = Solis::Query::Construct.new(@model).run
  end

  relationship = ''
  core_query = core_query(relationship)
  count_query = core_query.gsub(/SELECT .* WHERE/, 'SELECT (COUNT(distinct ?concept) as ?count) WHERE')

  # count_query = count_query.split('a ?type')[0]+'a ?type }'
  result = sparql_client.query(count_query)
  solution = result.first
  solution.nil? ? 0 : solution[:count].object || 0
end

#each(&block) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/solis/query.rb', line 71

def each(&block)
  data = query
  return unless data.methods.include?(:each)
  data.each(&block)
rescue StandardError => e
  message = "Unable to get next record: #{e.message}"
  LOGGER.error(message)
  raise Error::CursorError, message
end

#paging(params = {}) ⇒ Object



102
103
104
105
106
107
108
109
110
# File 'lib/solis/query.rb', line 102

def paging(params = {})
  current_page = params[:current_page] || 1
  per_page = params[:per_page] || 10

  @offset = 0
  @offset = current_page * per_page if current_page > 1
  @limit = per_page
  self
end

#sort(params) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/solis/query.rb', line 81

def sort(params)
  @sort = ''
  @sort_select = ''
  if params.key?(:sort)
    i = 0
    params[:sort].each do |attribute, direction|
      path = @model.class.[:attributes][attribute.to_s][:path]
      @sort_select += "optional {\n" if @model.class.[:attributes][attribute.to_s][:mincount] == 0
      @sort_select += "?concept <#{path}> ?__#{attribute} . "
      @sort_select += "}\n" if @model.class.[:attributes][attribute.to_s][:mincount] == 0
      @sort += ',' if i.positive?
      @sort += "#{direction.to_s.upcase}(?__#{attribute})"
      i += 1
    end

    @sort = "ORDER BY #{@sort}" if i.positive?
  end

  self
end