Class: Rufus::Edo::TableQuery
- Inherits:
-
Object
- Object
- Rufus::Edo::TableQuery
- Includes:
- Tokyo::QueryConstants
- Defined in:
- lib/rufus/edo/tabcore.rb
Overview
A query on a Tokyo Cabinet table db
Constant Summary
Constants included from Tokyo::QueryConstants
Tokyo::QueryConstants::DIRECTIONS, Tokyo::QueryConstants::OPERATORS, Tokyo::QueryConstants::TDBQCNEGATE, Tokyo::QueryConstants::TDBQCNOIDX
Instance Method Summary collapse
-
#add(colname, operator, val, affirmative = true, no_index = false) ⇒ Object
(also: #add_condition)
Adds a condition.
-
#count ⇒ Object
Returns the count of results this query return when last run.
-
#delete ⇒ Object
Runs this query AND immediately let the matching records get deleted.
-
#free ⇒ Object
(also: #close, #destroy)
Frees this data structure.
-
#initialize(query_class, table) ⇒ TableQuery
constructor
Creates a query for a given Rufus::Tokyo::Table.
-
#limit(i, offset = -1)) ⇒ Object
Sets the max number of records to return for this query.
-
#no_pk(on = true) ⇒ Object
When set to true, the :pk (primary key) is not inserted in the record (hashes) returned.
-
#order_by(colname, direction = :strasc) ⇒ Object
Sets the sort order for the result of the query.
-
#original ⇒ Object
Returns the original, underlying RDBQUERY instance.
-
#pk_only(on = true) ⇒ Object
When set to true, only the primary keys of the matching records will be returned.
-
#process(&block) ⇒ Object
Process each record using the supplied block, which will be passed two parameters, the primary key and the value hash.
-
#run ⇒ Object
Runs this query (returns a TableResultSet instance).
Constructor Details
#initialize(query_class, 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
437 438 439 440 441 442 443 |
# File 'lib/rufus/edo/tabcore.rb', line 437 def initialize (query_class, table) @table = table @query = query_class.new(table.original) @opts = {} @has_run = false 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
:ftsph # full-text phrase search
:ftsphrase
:phrase
:ftsand # full-text AND
:ftsor # full-text OR
:ftsex # full-text with 'compound' expression
516 517 518 519 520 521 522 523 524 525 526 |
# File 'lib/rufus/edo/tabcore.rb', line 516 def add (colname, operator, val, affirmative=true, no_index=false) colname = colname.to_s val = val.to_s op = operator.is_a?(Fixnum) ? operator : OPERATORS[operator] op = op | TDBQCNEGATE unless affirmative op = op | TDBQCNOIDX if no_index @query.addcond(colname, op, val) end |
#count ⇒ Object
Returns the count of results this query return when last run. Returns 0 if the query was not yet run.
632 633 634 635 636 637 638 |
# File 'lib/rufus/edo/tabcore.rb', line 632 def count #@query.count # not yet implemented by Hirabayashi-san run.free unless @has_run @last_resultset ? @last_resultset.size : 0 end |
#delete ⇒ Object
Runs this query AND immediately let the matching records get deleted.
624 625 626 627 |
# File 'lib/rufus/edo/tabcore.rb', line 624 def delete @query.searchout || @table.raise_error end |
#free ⇒ Object Also known as: close, destroy
Frees this data structure
642 643 644 645 |
# File 'lib/rufus/edo/tabcore.rb', line 642 def free # nothing ... :( I hope there's no memory leak 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)
534 535 536 537 |
# File 'lib/rufus/edo/tabcore.rb', line 534 def limit (i, offset=-1) @query.setlimit(i, offset) end |
#no_pk(on = true) ⇒ Object
When set to true, the :pk (primary key) is not inserted in the record (hashes) returned
566 567 568 569 |
# File 'lib/rufus/edo/tabcore.rb', line 566 def no_pk (on=true) @opts[:no_pk] = on 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
550 551 552 553 |
# File 'lib/rufus/edo/tabcore.rb', line 550 def order_by (colname, direction=:strasc) @query.setorder(colname.to_s, DIRECTIONS[direction]) end |
#original ⇒ Object
Returns the original, underlying RDBQUERY instance.
447 448 449 450 |
# File 'lib/rufus/edo/tabcore.rb', line 447 def original @query end |
#pk_only(on = true) ⇒ Object
When set to true, only the primary keys of the matching records will be returned.
558 559 560 561 |
# File 'lib/rufus/edo/tabcore.rb', line 558 def pk_only (on=true) @opts[:pk_only] = on end |
#process(&block) ⇒ Object
Process each record using the supplied block, which will be passed two parameters, the primary key and the value hash.
The block passed to this method accepts two parameters : the [String] primary key and a Hash of the values for the record.
The return value of the passed block does matter. Three different values are expected :stop, :delete or a Hash instance.
:stop will make the iteration stop, further matching records will not be passed to the block
:delete will let Tokyo Cabinet delete the record just seen.
a Hash is passed to let TC update the values for the record just seen.
Passing an array is possible : [ :stop, { ‘name’ => ‘Toto’ } ] will update the record just seen to a unique column ‘name’ and will stop the iteration. Likewise, returning [ :stop, :delete ] will work as well.
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 |
# File 'lib/rufus/edo/tabcore.rb', line 591 def process (&block) @query.proc() do |key, val| r = block.call(key, val) r = [ r ] unless r.is_a?(Array) if updated_value = r.find { |e| e.is_a?(Hash) } val.merge!(updated_value) end r.inject(0) { |i, v| case v when :stop then i = i | 1 << 24 when :delete then i = i | 2 when Hash then i = i | 1 end i } end self end |
#run ⇒ Object
Runs this query (returns a TableResultSet instance)
617 618 619 620 |
# File 'lib/rufus/edo/tabcore.rb', line 617 def run @has_run = true @last_resultset = TableResultSet.new(@table, @query.search, @opts) end |