Class: Xampl::TableQuery

Inherits:
Object
  • Object
show all
Includes:
TokyoCabinet
Defined in:
lib/xamplr/persisters/tokyo-cabinet.rb

Overview

Derrived from rufus-tyrant, but simplified significantly, and using the TokyoCabinet named constants rather than numbers

Constant Summary collapse

OPERATORS =
{
        # strings...

        :streq => TDBQRY::QCSTREQ, # string equality
        :eq => TDBQRY::QCSTREQ,
        :eql => TDBQRY::QCSTREQ,
        :equals => TDBQRY::QCSTREQ,

        :strinc => TDBQRY::QCSTRINC, # string include
        :inc => TDBQRY::QCSTRINC, # string include
        :includes => TDBQRY::QCSTRINC, # string include

        :strbw => TDBQRY::QCSTRBW, # string begins with
        :bw => TDBQRY::QCSTRBW,
        :starts_with => TDBQRY::QCSTRBW,
        :strew => TDBQRY::QCSTREW, # string ends with
        :ew => TDBQRY::QCSTREW,
        :ends_with => TDBQRY::QCSTREW,

        :strand => TDBQRY::QCSTRAND, # string which include all the tokens in the given exp
        :and => TDBQRY::QCSTRAND,

        :stror => TDBQRY::QCSTROR, # string which include at least one of the tokens
        :or => TDBQRY::QCSTROR,

        :stroreq => TDBQRY::QCSTROREQ, # string which is equal to at least one token

        :strorrx => TDBQRY::QCSTRRX, # string which matches the given regex
        :regex => TDBQRY::QCSTRRX,
        :matches => TDBQRY::QCSTRRX,

        # numbers...

        :numeq => TDBQRY::QCNUMEQ, # equal
        :numequals => TDBQRY::QCNUMEQ,
        :numgt => TDBQRY::QCNUMGT, # greater than
        :gt => TDBQRY::QCNUMGT,
        :numge => TDBQRY::QCNUMGE, # greater or equal
        :ge => TDBQRY::QCNUMGE,
        :gte => TDBQRY::QCNUMGE,
        :numlt => TDBQRY::QCNUMLT, # greater or equal
        :lt => TDBQRY::QCNUMLT,
        :numle => TDBQRY::QCNUMLE, # greater or equal
        :le => TDBQRY::QCNUMLE,
        :lte => TDBQRY::QCNUMLE,
        :numbt => TDBQRY::QCNUMBT, # a number between two tokens in the given exp
        :bt => TDBQRY::QCNUMBT,
        :between => TDBQRY::QCNUMBT,

        :numoreq => TDBQRY::QCNUMOREQ # number which is equal to at least one token
}
TDBQCNEGATE =
TDBQRY::QCNEGATE
TDBQCNOIDX =
TDBQRY::QCNOIDX
DIRECTIONS =
{
        :strasc => TDBQRY::QOSTRASC,
        :strdesc => TDBQRY::QOSTRDESC,
        :asc => TDBQRY::QOSTRASC,
        :desc => TDBQRY::QOSTRDESC,
        :numasc => TDBQRY::QONUMASC,
        :numdesc => TDBQRY::QONUMDESC
}

Instance Method Summary collapse

Constructor Details

#initialize(table) ⇒ TableQuery

Creates a query for a given Rufus::Tokyo::Table

Queries are usually created via the #query (#prepare_query #do_query) of the Table instance.

Methods of interest here are :

* #add (or #add_condition)
* #order_by
* #limit

also

* #pk_only
* #no_pk


784
785
786
787
788
# File 'lib/xamplr/persisters/tokyo-cabinet.rb', line 784

def initialize (table)
  @query = TDBQRY::new(table)
  @text_form = []
  @opts = {}
end

Instance Method Details

#add(colname, operator, val, affirmative = true, no_index = false) ⇒ Object Also known as: add_condition

Adds a condition

table.query { |q|
  q.add 'name', :equals, 'Oppenheimer'
  q.add 'age', :numgt, 35
}

Understood ‘operators’ :

:streq # string equality
:eq
:eql
:equals

:strinc # string include
:inc # string include
:includes # string include

:strbw # string begins with
:bw
:starts_with
:strew # string ends with
:ew
:ends_with

:strand # string which include all the tokens in the given exp
:and

:stror # string which include at least one of the tokens
:or

:stroreq # string which is equal to at least one token

:strorrx # string which matches the given regex
:regex
:matches

# numbers...

:numeq # equal
:numequals
:numgt # greater than
:gt
:numge # greater or equal
:ge
:gte
:numlt # greater or equal
:lt
:numle # greater or equal
:le
:lte
:numbt # a number between two tokens in the given exp
:bt
:between

:numoreq # number which is equal to at least one token


876
877
878
879
880
881
882
883
884
# File 'lib/xamplr/persisters/tokyo-cabinet.rb', line 876

def add (colname, operator, val, affirmative=true, no_index=false)
  op = operator.is_a?(Fixnum) ? operator : OPERATORS[operator]
  op = op | TDBQRY::QCNEGATE unless affirmative
  op = op | TDBQRY::QCNOIDX if no_index

  @text_form << "operator: #{ operator }#{ affirmative ? '' : ' NEGATED'}#{ no_index ? ' NO INDEX' : ''} -- col: '#{ colname }', val: '#{ val }'"

  @query.addcond(colname, op, val)
end

#inspectObject



916
917
918
# File 'lib/xamplr/persisters/tokyo-cabinet.rb', line 916

def inspect
  "TableQuery:\n#{ @text_form.join("\n") }"
end

#limit(i, offset = -1)) ⇒ Object

Sets the max number of records to return for this query.

(If you’re using TC >= 1.4.10 the optional ‘offset’ (skip) parameter is accepted)



895
896
897
# File 'lib/xamplr/persisters/tokyo-cabinet.rb', line 895

def limit (i, offset=-1)
  @query.setlimit(i, offset)
end

#order_by(colname, direction = :strasc) ⇒ Object

Sets the sort order for the result of the query

The ‘direction’ may be :

:strasc # string ascending
:strdesc
:asc # string ascending
:desc
:numasc # number ascending
:numdesc


912
913
914
# File 'lib/xamplr/persisters/tokyo-cabinet.rb', line 912

def order_by (colname, direction=:strasc)
  @query.setorder(colname, DIRECTIONS[direction])
end

#search(hint = false) ⇒ Object

Performs the search



794
795
796
797
798
799
800
801
# File 'lib/xamplr/persisters/tokyo-cabinet.rb', line 794

def search(hint=false)
  r = @query.search
  if hint then
    return r, @query.hint
  else
    return r
  end
end

#searchoutObject

Performs the search and removes whatever’s found



807
808
809
# File 'lib/xamplr/persisters/tokyo-cabinet.rb', line 807

def searchout
  r = @query.searchout
end

#setlimit(max = nil, skip = nil) ⇒ Object

limits the search



813
814
815
# File 'lib/xamplr/persisters/tokyo-cabinet.rb', line 813

def setlimit(max=nil, skip=nil)
  @query.setlimit(max, skip)
end

#to_sObject



920
921
922
# File 'lib/xamplr/persisters/tokyo-cabinet.rb', line 920

def to_s
  inspect
end