Skip to content

Commit 089d00e

Browse files
committed
Implement separate, private stats_as_hash and stat_for methods
1 parent b9bbeaa commit 089d00e

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

ext/sqlite3/statement.c

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -506,33 +506,40 @@ stmt_stat_internal(VALUE hash_or_sym, sqlite3_stmt *stmt)
506506
return 0;
507507
}
508508

509+
/* call-seq: stmt.stats_as_hash(hash)
510+
*
511+
* Returns a Hash containing information about the statement.
512+
*/
513+
static VALUE
514+
stats_as_hash(VALUE self)
515+
{
516+
sqlite3StmtRubyPtr ctx;
517+
TypedData_Get_Struct(self, sqlite3StmtRuby, &statement_type, ctx);
518+
REQUIRE_OPEN_STMT(ctx);
519+
VALUE arg = rb_hash_new();
520+
521+
stmt_stat_internal(arg, ctx->st);
522+
return arg;
523+
}
524+
509525
/* call-seq: stmt.stmt_stat(hash_or_key)
510526
*
511527
* Returns a Hash containing information about the statement.
512528
*/
513529
static VALUE
514-
stmt_stat(VALUE self, VALUE arg) // arg is (nil || hash || symbol)
530+
stat_for(VALUE self, VALUE key)
515531
{
516532
sqlite3StmtRubyPtr ctx;
517533
TypedData_Get_Struct(self, sqlite3StmtRuby, &statement_type, ctx);
518534
REQUIRE_OPEN_STMT(ctx);
519535

520-
if (NIL_P(arg)) {
521-
arg = rb_hash_new();
522-
}
523-
else if (SYMBOL_P(arg)) {
524-
size_t value = stmt_stat_internal(arg, ctx->st);
536+
if (SYMBOL_P(key)) {
537+
size_t value = stmt_stat_internal(key, ctx->st);
525538
return SIZET2NUM(value);
526539
}
527-
else if (RB_TYPE_P(arg, T_HASH)) {
528-
// ok
529-
}
530540
else {
531-
rb_raise(rb_eTypeError, "non-hash or symbol given");
541+
rb_raise(rb_eTypeError, "non-symbol given");
532542
}
533-
534-
stmt_stat_internal(arg, ctx->st);
535-
return arg;
536543
}
537544

538545
#ifdef SQLITE_STMTSTATUS_MEMUSED
@@ -587,10 +594,11 @@ init_sqlite3_statement(void)
587594
rb_define_method(cSqlite3Statement, "column_name", column_name, 1);
588595
rb_define_method(cSqlite3Statement, "column_decltype", column_decltype, 1);
589596
rb_define_method(cSqlite3Statement, "bind_parameter_count", bind_parameter_count, 0);
590-
rb_define_method(cSqlite3Statement, "stmt_stat", stmt_stat, 1);
591597
#ifdef SQLITE_STMTSTATUS_MEMUSED
592598
rb_define_method(cSqlite3Statement, "memused", memused, 0);
593599
#endif
594600

595601
rb_define_private_method(cSqlite3Statement, "prepare", prepare, 2);
602+
rb_define_private_method(cSqlite3Statement, "stats_as_hash", stats_as_hash, 0);
603+
rb_define_private_method(cSqlite3Statement, "stat_for", stat_for, 1);
596604
}

lib/sqlite3/statement.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,12 @@ def must_be_open! # :nodoc:
164164
# a find, and thus the join step had to be processed as normal
165165
# - +filter_hits+: the number of times that a join step was bypassed
166166
# because a Bloom filter returned not-found
167-
def stat hash_or_key = nil
168-
stmt_stat hash_or_key
167+
def stat key = nil
168+
if key
169+
stat_for(key)
170+
else
171+
stats_as_hash
172+
end
169173
end
170174

171175
private

0 commit comments

Comments
 (0)