Skip to content

Commit

Permalink
make optional the second argument to SQLite3.status
Browse files Browse the repository at this point in the history
and fully document the method.
  • Loading branch information
flavorjones committed Apr 16, 2024
1 parent a1ddbec commit b14a84e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
26 changes: 23 additions & 3 deletions ext/sqlite3/sqlite3.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down Expand Up @@ -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()));
Expand Down
14 changes: 12 additions & 2 deletions test/test_sqlite3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit b14a84e

Please sign in to comment.