Permalink
Browse files

Adding a query_count() method to all tables.

  • Loading branch information...
1 parent 3bbc937 commit a98eae01eaed70d4159bcb1e1e47f040c58bf44e @JEG2 JEG2 committed Oct 14, 2009
Showing with 48 additions and 9 deletions.
  1. +1 −0 CHANGELOG.txt
  2. +15 −6 lib/rufus/edo/tabcore.rb
  3. +18 −3 lib/rufus/tokyo/cabinet/table.rb
  4. +14 −0 spec/shared_table_spec.rb
View
@@ -24,6 +24,7 @@
- added : can set built-in or custom comparison functions for b-tree databases
(Edo interface only)
- todo : reenabled #copy for Tyrant (Edo and Tokyo)
+- added : query_count() to match query_delete() for tables (Edo and Tokyo)
== rufus-tokyo - 1.0.1 released 2009/09/18
View
@@ -220,6 +220,15 @@ def query_delete (&block)
prepare_query(&block).delete
end
+ # Prepares, runs AND counts all the matching records.
+ #
+ def query_count (&block)
+
+ prepare_query { |q|
+ q.pk_only # improve efficiency, since we have to do the query
+ }.count
+ end
+
# Warning : this method is low-level, you probably only need
# to use #transaction and a block.
#
@@ -427,10 +436,10 @@ class TableQuery
#
def initialize (query_class, table)
- @table = table
- @query = query_class.new(table.original)
-
- @opts = {}
+ @table = table
+ @query = query_class.new(table.original)
+ @opts = {}
+ @has_run = false
end
# Returns the original, underlying RDBQUERY instance.
@@ -608,7 +617,7 @@ def process (&block)
# Runs this query (returns a TableResultSet instance)
#
def run
-
+ @has_run = true
@last_resultset = TableResultSet.new(@table, @query.search, @opts)
end
@@ -626,7 +635,7 @@ def count
#@query.count
# not yet implemented by Hirabayashi-san
-
+ run.free unless @has_run
@last_resultset ? @last_resultset.size : 0
end
@@ -389,6 +389,18 @@ def query_delete (&block)
q && q.free
end
+ # Prepares a query and then runs it and deletes all the results.
+ #
+ def query_count (&block)
+
+ q = prepare_query { |q|
+ q.pk_only # improve efficiency, since we have to do the query
+ }
+ q.count
+ ensure
+ q.free if q
+ end
+
# Warning : this method is low-level, you probably only need
# to use #transaction and a block.
#
@@ -607,9 +619,10 @@ class TableQuery
#
def initialize (table)
- @table = table
- @query = @table.lib.qry_new(@table.pointer)
- @opts = {}
+ @table = table
+ @query = @table.lib.qry_new(@table.pointer)
+ @opts = {}
+ @has_run = false
end
# Returns the FFI lib the table uses.
@@ -801,6 +814,7 @@ def process (&block)
#
def run
+ @has_run = true
#@last_resultset =
TableResultSet.new(@table, lib.qry_search(@query), @opts)
end
@@ -819,6 +833,7 @@ def delete
def count
#if lib.respond_to?(:qry_count)
+ run.free unless @has_run
lib.qry_count(@query)
#else
# @last_resultset ? @last_resultset.size : 0
View
@@ -274,6 +274,20 @@
q.count.should.equal(4)
end
+ it 'can be counted without being explicitly run' do
+
+ @t.prepare_query { |qq|
+ qq.add 'lang', :includes, 'en'
+ }.count.should.equal(4)
+ end
+
+ it 'can be counted immediately (qrycount table#query_count)' do
+
+ @t.query_count { |qq|
+ qq.add 'lang', :includes, 'en'
+ }.should.equal(4)
+ end
+
it 'can be limited' do
@t.query { |q|

0 comments on commit a98eae0

Please sign in to comment.