# HG changeset patch # User Sebastien Jodogne # Date 1349880783 -7200 # Node ID f333c0398f6edfab2e01e8e5d7eb890b58db4183 # Parent 0e97abc7b9506040a2fb9af5db9d7d63668f3c2b some hiding diff -r 0e97abc7b950 -r f333c0398f6e Core/SQLite/Statement.h --- a/Core/SQLite/Statement.h Wed Oct 10 13:13:14 2012 +0200 +++ b/Core/SQLite/Statement.h Wed Oct 10 16:53:03 2012 +0200 @@ -43,6 +43,7 @@ #include #include #include +#include struct sqlite3_stmt; @@ -67,6 +68,8 @@ class Statement : public boost::noncopyable { friend class Connection; + FRIEND_TEST(SQLStatementTest, Run); + FRIEND_TEST(SQLStatementTest, Reset); private: StatementReference reference_; @@ -80,6 +83,10 @@ return reference_.GetWrappedObject(); } + // Resets the statement to its initial condition. This includes any current + // result row, and also the bound variables if the |clear_bound_vars| is true. + void Reset(bool clear_bound_vars = true); + public: Statement(Connection& database, const std::string& sql); @@ -104,10 +111,6 @@ bool Step(); - // Resets the statement to its initial condition. This includes any current - // result row, and also the bound variables if the |clear_bound_vars| is true. - void Reset(bool clear_bound_vars = true); - // Diagnostics -------------------------------------------------------------- std::string GetOriginalSQLStatement(); diff -r 0e97abc7b950 -r f333c0398f6e OrthancServer/ServerIndex.cpp --- a/OrthancServer/ServerIndex.cpp Wed Oct 10 13:13:14 2012 +0200 +++ b/OrthancServer/ServerIndex.cpp Wed Oct 10 16:53:03 2012 +0200 @@ -154,12 +154,10 @@ void ServerIndex::StoreMainDicomTags(const std::string& uuid, const DicomMap& map) { - SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO MainDicomTags VALUES(?, ?, ?, ?)"); - DicomArray flattened(map); for (size_t i = 0; i < flattened.GetSize(); i++) { - s.Reset(); + SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO MainDicomTags VALUES(?, ?, ?, ?)"); s.BindString(0, uuid); s.BindInt(1, flattened.GetElement(i).GetTag().GetGroup()); s.BindInt(2, flattened.GetElement(i).GetTag().GetElement()); @@ -616,15 +614,15 @@ switch (status) { case StoreStatus_Success: - LOG(INFO) << "New instance stored: " << GetTotalSize() << " bytes"; + LOG(WARNING) << "New instance stored: " << GetTotalSize() << " bytes"; break; case StoreStatus_AlreadyStored: - LOG(INFO) << "Already stored"; + LOG(WARNING) << "Already stored"; break; case StoreStatus_Failure: - LOG(INFO) << "Store failure"; + LOG(ERROR) << "Store failure"; break; } diff -r 0e97abc7b950 -r f333c0398f6e UnitTests/SQLiteChromium.cpp --- a/UnitTests/SQLiteChromium.cpp Wed Oct 10 13:13:14 2012 +0200 +++ b/UnitTests/SQLiteChromium.cpp Wed Oct 10 16:53:03 2012 +0200 @@ -7,8 +7,9 @@ #include + using namespace Orthanc; - +using namespace Orthanc::SQLite; /******************************************************************** @@ -37,13 +38,13 @@ db_.Close(); } - SQLite::Connection& db() + Connection& db() { return db_; } private: - SQLite::Connection db_; + Connection db_; }; @@ -67,17 +68,17 @@ db().ExecuteAndReturnErrorCode("CREATE TABLE TABLE")); ASSERT_EQ(SQLITE_ERROR, db().ExecuteAndReturnErrorCode( - "INSERT INTO foo(a, b) VALUES (1, 2, 3, 4)")); + "INSERT INTO foo(a, b) VALUES (1, 2, 3, 4)")); } TEST_F(SQLConnectionTest, CachedStatement) { - SQLite::StatementId id1("foo", 12); + StatementId id1("foo", 12); ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); ASSERT_TRUE(db().Execute("INSERT INTO foo(a, b) VALUES (12, 13)")); // Create a new cached statement. { - SQLite::Statement s(db(), id1, "SELECT a FROM foo"); + Statement s(db(), id1, "SELECT a FROM foo"); ASSERT_TRUE(s.Step()); EXPECT_EQ(12, s.ColumnInt(0)); } @@ -88,7 +89,7 @@ { // Get the same statement using different SQL. This should ignore our // SQL and use the cached one (so it will be valid). - SQLite::Statement s(db(), id1, "something invalid("); + Statement s(db(), id1, "something invalid("); ASSERT_TRUE(s.Step()); EXPECT_EQ(12, s.ColumnInt(0)); } @@ -132,7 +133,7 @@ EXPECT_LT(0, row); // It should be the primary key of the row we just inserted. - SQLite::Statement s(db(), "SELECT value FROM foo WHERE id=?"); + Statement s(db(), "SELECT value FROM foo WHERE id=?"); s.BindInt64(0, row); ASSERT_TRUE(s.Step()); EXPECT_EQ(12, s.ColumnInt(0)); @@ -155,65 +156,70 @@ ** http://src.chromium.org/viewvc/chrome/trunk/src/sql/statement_unittest.cc ********************************************************************/ -class SQLStatementTest : public SQLConnectionTest +namespace Orthanc { -}; - + namespace SQLite + { + class SQLStatementTest : public SQLConnectionTest + { + }; -TEST_F(SQLStatementTest, Run) { - ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); - ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (3, 12)")); + TEST_F(SQLStatementTest, Run) { + ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); + ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (3, 12)")); - SQLite::Statement s(db(), "SELECT b FROM foo WHERE a=?"); - // Stepping it won't work since we haven't bound the value. - EXPECT_FALSE(s.Step()); + Statement s(db(), "SELECT b FROM foo WHERE a=?"); + // Stepping it won't work since we haven't bound the value. + EXPECT_FALSE(s.Step()); - // Run should fail since this produces output, and we should use Step(). This - // gets a bit wonky since sqlite says this is OK so succeeded is set. - s.Reset(true); - s.BindInt(0, 3); - EXPECT_FALSE(s.Run()); - EXPECT_EQ(SQLITE_ROW, db().GetErrorCode()); + // Run should fail since this produces output, and we should use Step(). This + // gets a bit wonky since sqlite says this is OK so succeeded is set. + s.Reset(true); + s.BindInt(0, 3); + EXPECT_FALSE(s.Run()); + EXPECT_EQ(SQLITE_ROW, db().GetErrorCode()); - // Resetting it should put it back to the previous state (not runnable). - s.Reset(true); + // Resetting it should put it back to the previous state (not runnable). + s.Reset(true); - // Binding and stepping should produce one row. - s.BindInt(0, 3); - EXPECT_TRUE(s.Step()); - EXPECT_EQ(12, s.ColumnInt(0)); - EXPECT_FALSE(s.Step()); -} + // Binding and stepping should produce one row. + s.BindInt(0, 3); + EXPECT_TRUE(s.Step()); + EXPECT_EQ(12, s.ColumnInt(0)); + EXPECT_FALSE(s.Step()); + } -TEST_F(SQLStatementTest, BasicErrorCallback) { - ASSERT_TRUE(db().Execute("CREATE TABLE foo (a INTEGER PRIMARY KEY, b)")); - // Insert in the foo table the primary key. It is an error to insert - // something other than an number. This error causes the error callback - // handler to be called with SQLITE_MISMATCH as error code. - SQLite::Statement s(db(), "INSERT INTO foo (a) VALUES (?)"); - s.BindCString(0, "bad bad"); - EXPECT_THROW(s.Run(), OrthancException); -} + TEST_F(SQLStatementTest, BasicErrorCallback) { + ASSERT_TRUE(db().Execute("CREATE TABLE foo (a INTEGER PRIMARY KEY, b)")); + // Insert in the foo table the primary key. It is an error to insert + // something other than an number. This error causes the error callback + // handler to be called with SQLITE_MISMATCH as error code. + Statement s(db(), "INSERT INTO foo (a) VALUES (?)"); + s.BindCString(0, "bad bad"); + EXPECT_THROW(s.Run(), OrthancException); + } -TEST_F(SQLStatementTest, Reset) { - ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); - ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (3, 12)")); - ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (4, 13)")); + TEST_F(SQLStatementTest, Reset) { + ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); + ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (3, 12)")); + ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (4, 13)")); - SQLite::Statement s(db(), "SELECT b FROM foo WHERE a = ? "); - s.BindInt(0, 3); - ASSERT_TRUE(s.Step()); - EXPECT_EQ(12, s.ColumnInt(0)); - ASSERT_FALSE(s.Step()); + Statement s(db(), "SELECT b FROM foo WHERE a = ? "); + s.BindInt(0, 3); + ASSERT_TRUE(s.Step()); + EXPECT_EQ(12, s.ColumnInt(0)); + ASSERT_FALSE(s.Step()); - s.Reset(false); - // Verify that we can get all rows again. - ASSERT_TRUE(s.Step()); - EXPECT_EQ(12, s.ColumnInt(0)); - EXPECT_FALSE(s.Step()); + s.Reset(false); + // Verify that we can get all rows again. + ASSERT_TRUE(s.Step()); + EXPECT_EQ(12, s.ColumnInt(0)); + EXPECT_FALSE(s.Step()); - s.Reset(true); - ASSERT_FALSE(s.Step()); + s.Reset(true); + ASSERT_FALSE(s.Step()); + } + } } @@ -221,7 +227,6 @@ - /******************************************************************** ** Tests from ** http://src.chromium.org/viewvc/chrome/trunk/src/sql/transaction_unittest.cc @@ -239,7 +244,7 @@ // Returns the number of rows in table "foo". int CountFoo() { - SQLite::Statement count(db(), "SELECT count(*) FROM foo"); + Statement count(db(), "SELECT count(*) FROM foo"); count.Step(); return count.ColumnInt(0); } @@ -248,7 +253,7 @@ TEST_F(SQLTransactionTest, Commit) { { - SQLite::Transaction t(db()); + Transaction t(db()); EXPECT_FALSE(t.IsOpen()); t.Begin(); EXPECT_TRUE(t.IsOpen()); @@ -266,7 +271,7 @@ // Test some basic initialization, and that rollback runs when you exit the // scope. { - SQLite::Transaction t(db()); + Transaction t(db()); EXPECT_FALSE(t.IsOpen()); t.Begin(); EXPECT_TRUE(t.IsOpen()); @@ -278,7 +283,7 @@ EXPECT_EQ(0, CountFoo()); // Test explicit rollback. - SQLite::Transaction t2(db()); + Transaction t2(db()); EXPECT_FALSE(t2.IsOpen()); t2.Begin(); @@ -296,13 +301,13 @@ // Outermost transaction. { - SQLite::Transaction outer(db()); + Transaction outer(db()); outer.Begin(); EXPECT_EQ(1, db().GetTransactionNesting()); // The first inner one gets committed. { - SQLite::Transaction inner1(db()); + Transaction inner1(db()); inner1.Begin(); EXPECT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (1, 2)")); EXPECT_EQ(2, db().GetTransactionNesting()); @@ -316,7 +321,7 @@ // The second inner one gets rolled back. { - SQLite::Transaction inner2(db()); + Transaction inner2(db()); inner2.Begin(); EXPECT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (1, 2)")); EXPECT_EQ(2, db().GetTransactionNesting()); @@ -329,7 +334,7 @@ // back. EXPECT_EQ(1, db().GetTransactionNesting()); { - SQLite::Transaction inner3(db()); + Transaction inner3(db()); EXPECT_THROW(inner3.Begin(), OrthancException); EXPECT_EQ(1, db().GetTransactionNesting()); }