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


725
726
727
728
729
# File 'lib/xamplr/persisters/tokyo-cabinet.rb', line 725

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


817
818
819
820
821
822
823
824
825
# File 'lib/xamplr/persisters/tokyo-cabinet.rb', line 817

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



857
858
859
# File 'lib/xamplr/persisters/tokyo-cabinet.rb', line 857

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)



836
837
838
# File 'lib/xamplr/persisters/tokyo-cabinet.rb', line 836

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


853
854
855
# File 'lib/xamplr/persisters/tokyo-cabinet.rb', line 853

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

#search(hint = false) ⇒ Object

Performs the search



735
736
737
738
739
740
741
742
# File 'lib/xamplr/persisters/tokyo-cabinet.rb', line 735

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



748
749
750
# File 'lib/xamplr/persisters/tokyo-cabinet.rb', line 748

def searchout
  r = @query.searchout
end

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

limits the search



754
755
756
# File 'lib/xamplr/persisters/tokyo-cabinet.rb', line 754

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

#to_sObject



861
862
863
# File 'lib/xamplr/persisters/tokyo-cabinet.rb', line 861

def to_s
  inspect
end