summaryrefslogtreecommitdiff
path: root/tests/test_sqlite.cc
diff options
context:
space:
mode:
authorDaniel Burrows <dburrows@debian.org>2009-07-27 07:42:47 -0700
committerDaniel Burrows <dburrows@debian.org>2009-07-27 07:42:47 -0700
commitb198ccf4f06eca419a3ecaea9147eef461518a89 (patch)
tree49fdcd6eee4620e9d75412a52be82429d99f18ff /tests/test_sqlite.cc
parent8cc867d48715e1e2a1064a644a51356b20bb50af (diff)
downloadaptitude-b198ccf4f06eca419a3ecaea9147eef461518a89.tar.gz
Implement a system for caching prepared SQL statements.
Prepared statements created via this mechanism are placed into the cache for reuse once the user is done with them. Statements won't be reused as long as at least one proxy object still exists. This gives us a mechanism for safe reuse of statements without having to worry that two different pieces of code might use the same statement object and step on each other. Also, boost::multi_index is awesome.
Diffstat (limited to 'tests/test_sqlite.cc')
-rw-r--r--tests/test_sqlite.cc45
1 files changed, 45 insertions, 0 deletions
diff --git a/tests/test_sqlite.cc b/tests/test_sqlite.cc
index 598bd194..ce2f62b6 100644
--- a/tests/test_sqlite.cc
+++ b/tests/test_sqlite.cc
@@ -167,3 +167,48 @@ BOOST_FIXTURE_TEST_CASE(testGetString, test_db_fixture)
BOOST_CHECK_THROW(stmt->get_string(0),
exception);
}
+
+BOOST_FIXTURE_TEST_CASE(testGetCachedStatement, memory_db_fixture)
+{
+ tmpdb->set_statement_cache_limit(2);
+ db::statement_proxy p1(tmpdb->get_cached_statement("create table foo(bar int)"));
+ db::statement_proxy p2(tmpdb->get_cached_statement("create table foo(bar int)"));
+ p2.reset();
+ p1.reset();
+
+ db::statement_proxy p3(tmpdb->get_cached_statement("create table foo(bar int)"));
+ db::statement_proxy p4(tmpdb->get_cached_statement("create table bar(foo int)"));
+
+ // Test that statements are being reused.
+ statement * const stmt1(&*p4);
+
+ p3.reset();
+ p4.reset();
+
+ db::statement_proxy p5(tmpdb->get_cached_statement("create table bar(foo int)"));
+
+ statement * const stmt2(&*p5);
+
+ BOOST_CHECK_EQUAL(stmt1, stmt2);
+
+ // Use the statements for some trivial operations.
+ db::statement_proxy p6(tmpdb->get_cached_statement("create table foo(bar int)"));
+ p6->exec();
+
+ db::statement_proxy p7(tmpdb->get_cached_statement("insert into foo (bar) values (5)"));
+ db::statement_proxy p8(tmpdb->get_cached_statement("select bar from foo"));
+
+ p7->exec();
+
+ BOOST_REQUIRE(p8->step());
+ BOOST_CHECK_EQUAL(p8->get_int(0), 5);
+ BOOST_CHECK(!p8->step());
+}
+
+BOOST_FIXTURE_TEST_CASE(getCachedStatementFail, memory_db_fixture)
+{
+ BOOST_REQUIRE_THROW(statement::prepare(*tmpdb, "select * from bar"),
+ exception);
+ db::statement_proxy p1(tmpdb->get_cached_statement("create table foo(bar int)"));
+ db::statement_proxy p2(tmpdb->get_cached_statement("create table foo(bar int)"));
+}