Class: Montage::Query

Inherits:
Object
  • Object
show all
Includes:
Support
Defined in:
lib/montage/query.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Support

#is_f?, #is_i?, #nillify

Constructor Details

#initialize(params = {}) ⇒ Query

Initializes the query instance via a params hash

  • Attributes :

    • schema -> The name of the schema you wish to query. Alphanumeric characters and underscores are allowed.

    • options -> A query hash containing desired options

  • Returns :

    • A valid Montage::Query instance

  • Raises :

    • InvalidAttributeFormat -> If the declared schema is not a string or contains non-alphanumeric characters. Underscore (“_”) is allowed!

  • Examples :

    @query = Montage::Query.new(schema: 'testing')
    => <Montage::Query:ID @query={"$schema"=>"testing", "$query"=>[["$filter", []]]}, @schema="testing">
    


29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/montage/query.rb', line 29

def initialize(params = {})
  @schema = params[:schema]
  @options = {
    "$schema" => @schema,
    "$query" => [
      ["$filter", []]
    ]
  }
  fail(
    InvalidAttributeFormat, "Schema attribute must be declared and valid!"
  ) unless schema_valid?
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



11
12
13
# File 'lib/montage/query.rb', line 11

def options
  @options
end

#schemaObject (readonly)

Returns the value of attribute schema.



12
13
14
# File 'lib/montage/query.rb', line 12

def schema
  @schema
end

Instance Method Details

#between(args = {}) ⇒ Object

Perform a ‘between` filter on the query

  • Attributes :

    • from -> The lower bound for the between filter. Can be a number, a letter, or a string representation of a date. to -> The upper bound for the between filter. Can be a number, a letter, or a string representation of a date. index -> The index to use when performing this filter

  • Example :

    • @query.between(from: 1, to: 3, index: ‘rank’)

    > “$query”=>[[“$filter”, []], [“$between”, [1, 3, ‘rank’]]]

  • Returns :

    • A copy of self



169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/montage/query.rb', line 169

def between(args = {})
  unless args[:from] && args[:to]
    fail(
      MissingAttributeError,
      "You must supply the from and to attributes"
    )
  end

  clone.tap do |r|
    r.merge_array([
      "$between", [args[:from], args[:to], args[:index]].compact
    ])
  end
end

#index(field) ⇒ Object

Specifies an index to use on a query.

  • Notes :

    • RethinkDB isn’t as smart as some other database engines when selecting a query plan, but it does let you specify which index to use

  • Args :

    • field -> The index value in string format

  • Example :

    • @query.index(‘foo’).options

    => {"$schema"=>"test", "$query"=>[["$filter", []], ["$index", "foo"]]}
    
  • Returns :

    • A copy of self



197
198
199
# File 'lib/montage/query.rb', line 197

def index(field)
  clone.tap { |r| r.merge_array(["$index", field]) }
end

#limit(max = nil) ⇒ Object

Defines the limit to apply to the query, defaults to nil

  • Args :

    • max -> The max number of desired results

  • Returns :

    • An updated copy of self

  • Examples :

    @query.limit(99).options
    => {"$schema"=>"testing", "$query"=>[["$filter", []], ["$limit", 99]]}
    


83
84
85
# File 'lib/montage/query.rb', line 83

def limit(max = nil)
  clone.tap { |r| r.merge_array(["$limit", max]) }
end

#merge_array(query_param) ⇒ Object

Adds a query parameter to Montage::Query instances in the form of an array. Checks for existing array elements and replaces them if found.

  • Args :

    • query_param -> A query-modifing parameter in the form of an array. Composed of a ReQON supported string as a designator and an associated value.

  • Returns :

    • The updated array



62
63
64
65
66
67
68
69
70
71
# File 'lib/montage/query.rb', line 62

def merge_array(query_param)
  arr = options["$query"]
  position = arr.index(arr.assoc(query_param[0]))

  if position.nil?
    arr.push(query_param)
  else
    arr[position] = query_param
  end
end

#offset(value = nil) ⇒ Object

Defines the offset to apply to the query, defaults to nil

  • Args :

    • value -> The desired offset value

  • Returns :

    • An updated copy of self

  • Examples :

    @query.offset(14).options
    => {"$schema"=>"testing", "$query"=>[["$filter", []], ["$offset", 14]]}
    


97
98
99
# File 'lib/montage/query.rb', line 97

def offset(value = nil)
  clone.tap { |r| r.merge_array(["$offset", value]) }
end

#order(clause = {}) ⇒ Object

Defines the order clause for the query and merges it into the query array. See Montage::OrderParser for specifics

  • Args :

    • clause -> A hash or string value containing the field to order by and the direction. Valid directions are “asc” and “desc”. String values will default to “asc” if omitted or incorrect.

  • Returns :

    • An updated copy of self

  • Examples :

    • String

    @query.order("foo asc").options
    => {"$schema"=>"testing", "$query"=>[["$filter", []], ["$order_by", ["$asc", "foo"]]]}
    
    • Hash

    @query.order(:foo => :asc).options
    => {"$schema"=>"testing", "$query"=>[["$filter", []], ["$order_by", ["$asc", "foo"]]]}
    


118
119
120
# File 'lib/montage/query.rb', line 118

def order(clause = {})
  clone.tap { |r| r.merge_array(OrderParser.new(clause).parse) }
end

#pluck(column_name) ⇒ Object

Pluck just one column from the result set

  • Args :

    • column_name -> Accepts a single string or symbol value for the column

  • Example :

    @query.pluck(:id)
    @query.pluck("id")
    => {"$schema"=>"test", "$query"=>[["$filter", []], ["$pluck", ["id"]]]}
    
  • Returns :

    • A copy of self



212
213
214
# File 'lib/montage/query.rb', line 212

def pluck(column_name)
  clone.tap { |r| r.merge_array(["$pluck", [column_name.to_s]]) }
end

#schema_valid?Boolean

Validates the Montage::Query schema attribute

  • Returns :

    • A boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/montage/query.rb', line 47

def schema_valid?
  @schema.is_a?(String) && @schema.index(/\W+/).nil?
end

#select(*args) ⇒ Object

Select a set of columns from the result set

  • Args :

    • Array -> Accepts multiple column names as a string or symbol

  • Example : @query.select(“id”, “name”) @query.select(:id, :name)

    > “$query”=>[[“$filter”, []], [“$pluck”, [“id”, “name”]]]

  • Returns :

    • A copy of self



152
153
154
# File 'lib/montage/query.rb', line 152

def select(*args)
  clone.tap { |r| r.merge_array(["$pluck", args.map(&:to_s)]) }
end

#to_jsonObject

Parses the current query hash and returns a JSON string



218
219
220
# File 'lib/montage/query.rb', line 218

def to_json
  @options.to_json
end

#where(clause) ⇒ Object

Adds a where clause to the ReQON filter array. See Montage::QueryParser for specifics

  • Args :

    • clause -> A hash or string containing desired options

  • Returns :

    • A copy of self

  • Examples :

    • String

    @query.where("tree_happiness_level >= 99").options
    => {"$schema"=>"test", "$query"=>[["$filter", [["tree_happiness_level", ["$__gte", 99]]]]]}
    
    • Hash

    @query.where(cloud_type: "almighty").options
    => {"$schema"=>"test", "$query"=>[["$filter", [["cloud_type", "almighty"]]]]}
    


137
138
139
# File 'lib/montage/query.rb', line 137

def where(clause)
  clone.tap { |r| r.merge_array(["$filter", QueryParser.new(clause).parse]) }
end