Class: ConceptQL::Query

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/conceptql/query.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db, statement, opts = {}) ⇒ Query

Returns a new instance of Query.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/conceptql/query.rb', line 15

def initialize(db, statement, opts={})
  @db = db
  @statement = extract_statement(statement)
  opts = opts.dup
  opts[:algorithm_fetcher] ||= proc do |alg|
    statement, description = db[:concepts].where(concept_id: alg).get([:statement, :label])
    statement = JSON.parse(statement) if statement.is_a?(String)
    [statement, description]
  end
  @nodifier = opts[:nodifier] || Nodifier.new({ database_type: db.database_type}.merge(opts))
end

Instance Attribute Details

#statementObject (readonly)

Returns the value of attribute statement.



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

def statement
  @statement
end

Instance Method Details

#annotate(opts = {}) ⇒ Object



49
50
51
# File 'lib/conceptql/query.rb', line 49

def annotate(opts = {})
  operator.annotate(db, opts)
end

#code_list(db) ⇒ Object



80
81
82
# File 'lib/conceptql/query.rb', line 80

def code_list(db)
  operator.code_list(db)
end

#domainsObject



64
65
66
# File 'lib/conceptql/query.rb', line 64

def domains
  operator.domains(db)
end

#operatorObject



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/conceptql/query.rb', line 68

def operator
  @operator ||= if statement.is_a?(Array)
    if statement.first.is_a?(Array)
      Operators::Invalid.new(nodifier, "invalid", errors: [["incomplete statement"]])
    else
      nodifier.create(*statement)
    end
  else
    Operators::Invalid.new(nodifier, "invalid", errors: [["invalid root operator", statement.inspect]])
  end
end

#optimizedObject



58
59
60
61
62
# File 'lib/conceptql/query.rb', line 58

def optimized
  n = dup
  n.instance_variable_set(:@operator, operator.optimized)
  n
end

#queryObject



27
28
29
# File 'lib/conceptql/query.rb', line 27

def query
  nodifier.scope.with_ctes(operator.evaluate(db), db)
end

#query_cols(opts = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/conceptql/query.rb', line 31

def query_cols(opts = {})
  cols = operator.dynamic_columns
  if opts[:cast]
    cols = query_cols.each_with_object({}) do |column, h|
      h[column] = operator.cast_column(column)
    end
  end
  cols
end

#scope_annotate(opts = {}) ⇒ Object



53
54
55
56
# File 'lib/conceptql/query.rb', line 53

def scope_annotate(opts = {})
  annotate(opts)
  nodifier.scope.annotation
end

#sqlObject



41
42
43
44
45
46
47
# File 'lib/conceptql/query.rb', line 41

def sql
  SqlFormatter.new.format(query.sql)
rescue
  puts $!.message
  puts $!.backtrace.join("\n")
  return "SQL unavailable for this statement"
end