class ConceptQL::Query

Attributes

db[RW]
statement[R]

Public Class Methods

new(db, statement, opts={}) click to toggle source
# 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

Public Instance Methods

annotate(opts = {}) click to toggle source
# File lib/conceptql/query.rb, line 49
def annotate(opts = {})
  operator.annotate(db, opts)
end
code_list(db) click to toggle source
# File lib/conceptql/query.rb, line 80
def code_list(db)
  operator.code_list(db)
end
domains() click to toggle source
# File lib/conceptql/query.rb, line 64
def domains
  operator.domains(db)
end
operator() click to toggle source
# 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
optimized() click to toggle source
# File lib/conceptql/query.rb, line 58
def optimized
  n = dup
  n.instance_variable_set(:@operator, operator.optimized)
  n
end
query() click to toggle source
# File lib/conceptql/query.rb, line 27
def query
  nodifier.scope.with_ctes(operator.evaluate(db), db)
end
query_cols(opts = {}) click to toggle source
# 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 = {}) click to toggle source
# File lib/conceptql/query.rb, line 53
def scope_annotate(opts = {})
  annotate(opts)
  nodifier.scope.annotation
end
sql() click to toggle source
# 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

Private Instance Methods

extract_statement(stmt) click to toggle source
# File lib/conceptql/query.rb, line 88
def extract_statement(stmt)
  if stmt.is_a?(Array) && stmt.length == 1 && stmt.first.is_a?(Array)
    stmt.first
  else
    stmt
  end
end