<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -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</diff>
      <filename>CHANGELOG.txt</filename>
    </modified>
    <modified>
      <diff>@@ -220,6 +220,15 @@ module Rufus::Edo
       prepare_query(&amp;block).delete
     end
 
+    # Prepares, runs AND counts all the matching records.
+    #
+    def query_count (&amp;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 @@ module Rufus::Edo
     #
     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 @@ module Rufus::Edo
     # 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 @@ module Rufus::Edo
 
       #@query.count
         # not yet implemented by Hirabayashi-san
-
+      run.free unless @has_run
       @last_resultset ? @last_resultset.size : 0
     end
 </diff>
      <filename>lib/rufus/edo/tabcore.rb</filename>
    </modified>
    <modified>
      <diff>@@ -389,6 +389,18 @@ module Rufus::Tokyo
       q &amp;&amp; q.free
     end
 
+    # Prepares a query and then runs it and deletes all the results.
+    #
+    def query_count (&amp;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 @@ module Rufus::Tokyo
     #
     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 @@ module Rufus::Tokyo
     #
     def run
 
+      @has_run = true
       #@last_resultset =
       TableResultSet.new(@table, lib.qry_search(@query), @opts)
     end
@@ -819,6 +833,7 @@ module Rufus::Tokyo
     def count
 
       #if lib.respond_to?(:qry_count)
+      run.free unless @has_run
       lib.qry_count(@query)
       #else
       #  @last_resultset ? @last_resultset.size : 0</diff>
      <filename>lib/rufus/tokyo/cabinet/table.rb</filename>
    </modified>
    <modified>
      <diff>@@ -274,6 +274,20 @@ shared 'table query' do
     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|</diff>
      <filename>spec/shared_table_spec.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>3bbc937e508fee0757106db3130c53ef384679a2</id>
    </parent>
  </parents>
  <author>
    <name>James Edward Gray II</name>
    <email>james@graysoftinc.com</email>
  </author>
  <url>http://github.com/jmettraux/rufus-tokyo/commit/a98eae01eaed70d4159bcb1e1e47f040c58bf44e</url>
  <id>a98eae01eaed70d4159bcb1e1e47f040c58bf44e</id>
  <committed-date>2009-10-13T17:08:34-07:00</committed-date>
  <authored-date>2009-10-13T17:08:34-07:00</authored-date>
  <message>Adding a query_count() method to all tables.</message>
  <tree>1ad939e75f1c750b05d7d339c028801c551dad09</tree>
  <committer>
    <name>James Edward Gray II</name>
    <email>james@graysoftinc.com</email>
  </committer>
</commit>
