From ecfae5a3e713848b533e7f08aa219846cd38a071 Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Tue, 16 Apr 2024 10:37:58 -0400 Subject: [PATCH] make optional the second argument to SQLite3.status and fully document the method. --- ext/sqlite3/sqlite3.c | 26 +++++++++++++++++++++++--- test/test_sqlite3.rb | 14 ++++++++++++-- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/ext/sqlite3/sqlite3.c b/ext/sqlite3/sqlite3.c index 122be270..652c8db5 100644 --- a/ext/sqlite3/sqlite3.c +++ b/ext/sqlite3/sqlite3.c @@ -85,11 +85,30 @@ threadsafe_p(VALUE UNUSED(klass)) return INT2NUM(sqlite3_threadsafe()); } +/* + * call-seq: + * status(parameter) → Hash + * status(parameter, reset_flag = false) → Hash + * + * Queries the SQLite3 library for run-time status information. Passing a truthy +reset_flag+ will + * reset the highwater mark to the current value. + * + * [Parameters] + * - +parameter+ (Integer, SQLite3::Constants::Status): The status parameter to query. + * - +reset_flag+ (Boolean): Whether to reset the highwater mark. (default is +false+) + * + * [Returns] + * A Hash containing +:current+ and +:highwater+ keys for integer values. + */ static VALUE -status_p(VALUE UNUSED(klass), VALUE opArg, VALUE resetFlagArg) +rb_sqlite3_status(int argc, VALUE *argv, VALUE klass) { + VALUE opArg, resetFlagArg; + + rb_scan_args(argc, argv, "11", &opArg, &resetFlagArg); + int op = NUM2INT(opArg); - bool resetFlag = TYPE(resetFlagArg) == T_TRUE; + bool resetFlag = RTEST(resetFlagArg); int pCurrent = 0; int pHighwater = 0; @@ -98,6 +117,7 @@ status_p(VALUE UNUSED(klass), VALUE opArg, VALUE resetFlagArg) VALUE hash = rb_hash_new(); rb_hash_aset(hash, ID2SYM(rb_intern("current")), INT2FIX(pCurrent)); rb_hash_aset(hash, ID2SYM(rb_intern("highwater")), INT2FIX(pHighwater)); + return hash; } @@ -180,7 +200,7 @@ Init_sqlite3_native(void) rb_define_singleton_method(mSqlite3, "sqlcipher?", using_sqlcipher, 0); rb_define_singleton_method(mSqlite3, "libversion", libversion, 0); rb_define_singleton_method(mSqlite3, "threadsafe", threadsafe_p, 0); - rb_define_singleton_method(mSqlite3, "status", status_p, 2); + rb_define_singleton_method(mSqlite3, "status", rb_sqlite3_status, -1); rb_define_const(mSqlite3, "SQLITE_VERSION", rb_str_new2(SQLITE_VERSION)); rb_define_const(mSqlite3, "SQLITE_VERSION_NUMBER", INT2FIX(SQLITE_VERSION_NUMBER)); rb_define_const(mSqlite3, "SQLITE_LOADED_VERSION", rb_str_new2(sqlite3_libversion())); diff --git a/test/test_sqlite3.rb b/test/test_sqlite3.rb index 59fe91df..364734b2 100644 --- a/test/test_sqlite3.rb +++ b/test/test_sqlite3.rb @@ -23,9 +23,19 @@ def test_compiled_version_and_loaded_version end def test_status + status = SQLite3.status(SQLite3::Constants::Status::MEMORY_USED) + assert_operator(status.fetch(:current), :>=, 0) + assert_operator(status.fetch(:highwater), :>=, status.fetch(:current)) + end + + def test_status_reset_highwater_mark status = SQLite3.status(SQLite3::Constants::Status::MEMORY_USED, false) - assert_not_nil(status.fetch(:current)) - assert_not_nil(status.fetch(:highwater)) + assert_operator(status.fetch(:current), :>=, 0) + assert_operator(status.fetch(:highwater), :>=, status.fetch(:current)) + + status = SQLite3.status(SQLite3::Constants::Status::MEMORY_USED, true) + assert_operator(status.fetch(:current), :>=, 0) + assert_operator(status.fetch(:highwater), :>=, status.fetch(:current)) end end end