Skip to content

Commit b14a84e

Browse files
committed
make optional the second argument to SQLite3.status
and fully document the method.
1 parent a1ddbec commit b14a84e

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

ext/sqlite3/sqlite3.c

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,30 @@ threadsafe_p(VALUE UNUSED(klass))
8585
return INT2NUM(sqlite3_threadsafe());
8686
}
8787

88+
/*
89+
* call-seq:
90+
* status(parameter) → Hash
91+
* status(parameter, reset_flag = false) → Hash
92+
*
93+
* Queries the SQLite3 library for run-time status information. Passing a truthy +reset_flag+ will
94+
* reset the highwater mark to the current value.
95+
*
96+
* [Parameters]
97+
* - +parameter+ (Integer, SQLite3::Constants::Status): The status parameter to query.
98+
* - +reset_flag+ (Boolean): Whether to reset the highwater mark. (default is +false+)
99+
*
100+
* [Returns]
101+
* A Hash containing +:current+ and +:highwater+ keys for integer values.
102+
*/
88103
static VALUE
89-
status_p(VALUE UNUSED(klass), VALUE opArg, VALUE resetFlagArg)
104+
rb_sqlite3_status(int argc, VALUE *argv, VALUE klass)
90105
{
106+
VALUE opArg, resetFlagArg;
107+
108+
rb_scan_args(argc, argv, "11", &opArg, &resetFlagArg);
109+
91110
int op = NUM2INT(opArg);
92-
bool resetFlag = TYPE(resetFlagArg) == T_TRUE;
111+
bool resetFlag = RTEST(resetFlagArg);
93112

94113
int pCurrent = 0;
95114
int pHighwater = 0;
@@ -98,6 +117,7 @@ status_p(VALUE UNUSED(klass), VALUE opArg, VALUE resetFlagArg)
98117
VALUE hash = rb_hash_new();
99118
rb_hash_aset(hash, ID2SYM(rb_intern("current")), INT2FIX(pCurrent));
100119
rb_hash_aset(hash, ID2SYM(rb_intern("highwater")), INT2FIX(pHighwater));
120+
101121
return hash;
102122
}
103123

@@ -180,7 +200,7 @@ Init_sqlite3_native(void)
180200
rb_define_singleton_method(mSqlite3, "sqlcipher?", using_sqlcipher, 0);
181201
rb_define_singleton_method(mSqlite3, "libversion", libversion, 0);
182202
rb_define_singleton_method(mSqlite3, "threadsafe", threadsafe_p, 0);
183-
rb_define_singleton_method(mSqlite3, "status", status_p, 2);
203+
rb_define_singleton_method(mSqlite3, "status", rb_sqlite3_status, -1);
184204
rb_define_const(mSqlite3, "SQLITE_VERSION", rb_str_new2(SQLITE_VERSION));
185205
rb_define_const(mSqlite3, "SQLITE_VERSION_NUMBER", INT2FIX(SQLITE_VERSION_NUMBER));
186206
rb_define_const(mSqlite3, "SQLITE_LOADED_VERSION", rb_str_new2(sqlite3_libversion()));

test/test_sqlite3.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,19 @@ def test_compiled_version_and_loaded_version
2323
end
2424

2525
def test_status
26+
status = SQLite3.status(SQLite3::Constants::Status::MEMORY_USED)
27+
assert_operator(status.fetch(:current), :>=, 0)
28+
assert_operator(status.fetch(:highwater), :>=, status.fetch(:current))
29+
end
30+
31+
def test_status_reset_highwater_mark
2632
status = SQLite3.status(SQLite3::Constants::Status::MEMORY_USED, false)
27-
assert_not_nil(status.fetch(:current))
28-
assert_not_nil(status.fetch(:highwater))
33+
assert_operator(status.fetch(:current), :>=, 0)
34+
assert_operator(status.fetch(:highwater), :>=, status.fetch(:current))
35+
36+
status = SQLite3.status(SQLite3::Constants::Status::MEMORY_USED, true)
37+
assert_operator(status.fetch(:current), :>=, 0)
38+
assert_operator(status.fetch(:highwater), :>=, status.fetch(:current))
2939
end
3040
end
3141
end

0 commit comments

Comments
 (0)