util: Sqlite3Statement helper
Change-Id: Ia555dff99efa7d217f0b6153b1333b6dd97dd610
diff --git a/src/util/sqlite3-statement.cpp b/src/util/sqlite3-statement.cpp
new file mode 100644
index 0000000..243088b
--- /dev/null
+++ b/src/util/sqlite3-statement.cpp
@@ -0,0 +1,115 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2015 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#include "sqlite3-statement.hpp"
+
+#include <sqlite3.h>
+
+namespace ndn {
+namespace util {
+
+Sqlite3Statement::~Sqlite3Statement()
+{
+ sqlite3_finalize(m_stmt);
+}
+
+Sqlite3Statement::Sqlite3Statement(sqlite3* database, const std::string& statement)
+{
+ int res = sqlite3_prepare_v2(database, statement.c_str(), -1, &m_stmt, nullptr);
+ if (res != SQLITE_OK)
+ throw std::domain_error("bad SQL statement: " + statement);
+}
+
+int
+Sqlite3Statement::bind(int index, const char* value, size_t size, void(*destructor)(void*))
+{
+ return sqlite3_bind_text(m_stmt, index, value, size, destructor);
+}
+
+int
+Sqlite3Statement::bind(int index, const std::string& value, void(*destructor)(void*))
+{
+ return sqlite3_bind_text(m_stmt, index, value.c_str(), value.size(), destructor);
+}
+
+int
+Sqlite3Statement::bind(int index, const void* buf, size_t size, void(*destructor)(void*))
+{
+ return sqlite3_bind_blob(m_stmt, index, buf, size, destructor);
+}
+
+int
+Sqlite3Statement::bind(int index, const Block& block, void(*destructor)(void*))
+{
+ return sqlite3_bind_blob(m_stmt, index, block.wire(), block.size(), destructor);
+}
+
+int
+Sqlite3Statement::bind(int index, int number)
+{
+ return sqlite3_bind_int(m_stmt, index, number);
+}
+
+std::string
+Sqlite3Statement::getString(int column)
+{
+ return std::string(reinterpret_cast<const char*>(sqlite3_column_text(m_stmt, column)),
+ sqlite3_column_bytes(m_stmt, column));
+}
+
+Block
+Sqlite3Statement::getBlock(int column)
+{
+ return Block(sqlite3_column_blob(m_stmt, column), sqlite3_column_bytes(m_stmt, column));
+}
+
+int
+Sqlite3Statement::getInt(int column)
+{
+ return sqlite3_column_int(m_stmt, column);
+}
+
+
+const uint8_t*
+Sqlite3Statement::getBlob(int column)
+{
+ return static_cast<const uint8_t*>(sqlite3_column_blob(m_stmt, column));
+}
+
+int
+Sqlite3Statement::getSize(int column)
+{
+ return sqlite3_column_bytes(m_stmt, column);
+}
+
+int
+Sqlite3Statement::step()
+{
+ return sqlite3_step(m_stmt);
+}
+
+Sqlite3Statement::operator sqlite3_stmt*()
+{
+ return m_stmt;
+}
+
+} // namespace util
+} // namespace ndn
diff --git a/src/util/sqlite3-statement.hpp b/src/util/sqlite3-statement.hpp
new file mode 100644
index 0000000..dbad1d2
--- /dev/null
+++ b/src/util/sqlite3-statement.hpp
@@ -0,0 +1,158 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2015 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#ifndef NDN_UTIL_SQLITE3_STATEMENT_HPP
+#define NDN_UTIL_SQLITE3_STATEMENT_HPP
+
+#include "../encoding/block.hpp"
+#include <string>
+
+struct sqlite3;
+struct sqlite3_stmt;
+
+namespace ndn {
+namespace util {
+
+/**
+ * @brief wrap an SQLite3 prepared statement
+ * @warning This class is implementation detail of ndn-cxx library.
+ */
+class Sqlite3Statement : noncopyable
+{
+public:
+ /**
+ * @brief initialize and prepare Sqlite3 statement
+ * @param database handler to open sqlite3 database
+ * @param statement SQL statement
+ * @throw std::domain_error SQL statement is bad
+ */
+ Sqlite3Statement(sqlite3* database, const std::string& statement);
+
+ /**
+ * @brief finalize the statement
+ */
+ ~Sqlite3Statement();
+
+ /**
+ * @brief bind a string to the statement
+ *
+ * @param index The binding position
+ * @param value The pointer of the binding string
+ * @param value The size of the binding string
+ * @param destructor SQLite3 destructor, e.g., SQLITE_TRANSIENT
+ * @return SQLite result value.
+ */
+ int
+ bind(int index, const char* value, size_t size, void(*destructor)(void*));
+
+ /**
+ * @brief bind a string to the statement
+ *
+ * @param index The binding position
+ * @param value The binding string
+ * @param destructor SQLite3 destructor, e.g., SQLITE_TRANSIENT
+ * @return SQLite result value.
+ */
+ int
+ bind(int index, const std::string& value, void(*destructor)(void*));
+
+ /**
+ * @brief bind a byte blob to the statement
+ *
+ * @param index The binding position
+ * @param value The pointer of the blob
+ * @param value The size of the blob
+ * @param destructor SQLite3 destructor, e.g., SQLITE_TRANSIENT
+ * @return SQLite result value.
+ */
+ int
+ bind(int index, const void* value, size_t size, void(*destructor)(void*));
+
+ /**
+ * @brief bind a byte blob to the statement
+ *
+ * @param index The binding position
+ * @param block The binding block
+ * @param destructor SQLite3 destructor, e.g., SQLITE_TRANSIENT
+ * @return SQLite result value
+ */
+ int
+ bind(int index, const Block& block, void(*destructor)(void*));
+
+ /**
+ * @brief bind an integer to the statement
+ *
+ * @param index The binding position
+ * @param number The binding integer
+ * @return SQLite result value
+ */
+ int
+ bind(int index, int number);
+
+ /**
+ * @brief get a string from @p column.
+ */
+ std::string
+ getString(int column);
+
+ /**
+ * @brief get a block from @p column.
+ */
+ Block
+ getBlock(int column);
+
+ /**
+ * @brief get an integer from @p column.
+ */
+ int
+ getInt(int column);
+
+ /**
+ * @brief get a pointer of byte blob from @p column.
+ */
+ const uint8_t*
+ getBlob(int column);
+
+ /**
+ * @brief get the size of @p column.
+ */
+ int
+ getSize(int column);
+
+ /**
+ * @brief wrapper of sqlite3_step
+ */
+ int
+ step();
+
+ /**
+ * @brief implicitly converts to sqlite3_stmt* to be used in SQLite C API
+ */
+ operator sqlite3_stmt*();
+
+private:
+ sqlite3_stmt* m_stmt;
+};
+
+} // namespace util
+} // namespace ndn
+
+#endif // NDN_UTIL_SQLITE3_STATEMENT_HPP
diff --git a/tests/unit-tests/util/sqlite3-statement.t.cpp b/tests/unit-tests/util/sqlite3-statement.t.cpp
new file mode 100644
index 0000000..a7fa29a
--- /dev/null
+++ b/tests/unit-tests/util/sqlite3-statement.t.cpp
@@ -0,0 +1,156 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2015 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#include "util/sqlite3-statement.hpp"
+
+#include <boost/filesystem.hpp>
+#include "boost-test.hpp"
+
+#include <sqlite3.h>
+
+namespace ndn {
+namespace util {
+namespace tests {
+
+class Sqlite3StatementTestFixture
+{
+public:
+ Sqlite3StatementTestFixture()
+ : m_path(boost::filesystem::path(UNIT_TEST_CONFIG_PATH))
+ {
+ boost::filesystem::create_directories(m_path);
+ int result = sqlite3_open_v2((m_path / "sqlite3-statement.db").string().c_str(), &db,
+ SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
+#ifdef NDN_CXX_DISABLE_SQLITE3_FS_LOCKING
+ "unix-dotfile"
+#else
+ nullptr
+#endif
+ );
+
+ if (result != SQLITE_OK) {
+ BOOST_FAIL("Sqlite3 database cannot be opened/created: " + m_path.string());
+ }
+ }
+
+ ~Sqlite3StatementTestFixture()
+ {
+ sqlite3_close(db);
+ boost::filesystem::remove_all(m_path);
+ }
+
+private:
+ boost::filesystem::path m_path;
+
+public:
+ sqlite3* db;
+};
+
+BOOST_FIXTURE_TEST_SUITE(UtilSqlite3Statement, Sqlite3StatementTestFixture)
+
+BOOST_AUTO_TEST_CASE(Basic)
+{
+ // create table
+ BOOST_CHECK_NO_THROW(Sqlite3Statement(db, "CREATE TABLE test (t1 int, t2 text)").step());
+
+ // insert data into table
+ BOOST_CHECK_NO_THROW(Sqlite3Statement(db, "INSERT INTO test VALUES (1, 'test1')").step());
+
+ {
+ Sqlite3Statement stmt(db, "INSERT INTO test VALUES (2, ?)");
+ stmt.bind(1, "test2", std::strlen("test2"), SQLITE_STATIC);
+ stmt.step();
+ }
+
+ {
+ Sqlite3Statement stmt(db, "INSERT INTO test VALUES (3, ?)");
+ stmt.bind(1, "test3", SQLITE_TRANSIENT);
+ stmt.step();
+ }
+
+ Block block(100);
+ block.encode();
+ {
+ Sqlite3Statement stmt(db, "INSERT INTO test VALUES (4, ?)");
+ stmt.bind(1, block, SQLITE_STATIC);
+ stmt.step();
+ }
+
+ {
+ Sqlite3Statement stmt(db, "INSERT INTO test VALUES (5, ?)");
+ stmt.bind(1, reinterpret_cast<const void*>(block.wire()), block.size(), SQLITE_STATIC);
+ stmt.step();
+ }
+
+ {
+ Sqlite3Statement stmt(db, "INSERT INTO test VALUES (?, ?)");
+ stmt.bind(1, 6);
+ stmt.bind(2, "test", SQLITE_TRANSIENT);
+ stmt.step();
+ }
+
+ // check content of the table
+
+ {
+ Sqlite3Statement stmt(db, "SELECT count(*) FROM test");
+ BOOST_CHECK_EQUAL(stmt.step(), SQLITE_ROW);
+ BOOST_CHECK_EQUAL(stmt.getInt(0), 6);
+ BOOST_CHECK_EQUAL(stmt.step(), SQLITE_DONE);
+ }
+
+ {
+ Sqlite3Statement stmt(db, "SELECT t1, t2 FROM test ORDER BY t1");
+ BOOST_CHECK_EQUAL(stmt.step(), SQLITE_ROW);
+ BOOST_CHECK_EQUAL(stmt.getInt(0), 1);
+ BOOST_CHECK_EQUAL(stmt.getString(1), "test1");
+
+ BOOST_CHECK_EQUAL(stmt.step(), SQLITE_ROW);
+ BOOST_CHECK_EQUAL(stmt.getInt(0), 2);
+ BOOST_CHECK_EQUAL(stmt.getString(1), "test2");
+
+ BOOST_CHECK_EQUAL(stmt.step(), SQLITE_ROW);
+ BOOST_CHECK_EQUAL(stmt.getInt(0), 3);
+ BOOST_CHECK_EQUAL(stmt.getString(1), "test3");
+
+ BOOST_CHECK_EQUAL(stmt.step(), SQLITE_ROW);
+ BOOST_CHECK_EQUAL(stmt.getInt(0), 4);
+
+ Block newBlock;
+ BOOST_CHECK_NO_THROW(newBlock = stmt.getBlock(1));
+ BOOST_CHECK_EQUAL(newBlock.type(), 100);
+ BOOST_CHECK(newBlock == block);
+
+ BOOST_CHECK_EQUAL(stmt.step(), SQLITE_ROW);
+ BOOST_CHECK_EQUAL(stmt.getInt(0), 5);
+ BOOST_CHECK_EQUAL(stmt.getSize(1), block.size());
+ BOOST_CHECK_EQUAL_COLLECTIONS(block.begin(), block.end(),
+ stmt.getBlob(1), stmt.getBlob(1) + stmt.getSize(1));
+
+ BOOST_CHECK_EQUAL(stmt.step(), SQLITE_ROW);
+ BOOST_CHECK_EQUAL(stmt.step(), SQLITE_DONE);
+ }
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace tests
+} // namespace util
+} // namespace ndn
diff --git a/tests/wscript b/tests/wscript
index f3c1ccf..89a06cc 100644
--- a/tests/wscript
+++ b/tests/wscript
@@ -30,6 +30,7 @@
excl=['**/*-osx.t.cpp', '**/*-sqlite3.t.cpp']),
use='ndn-cxx tests-base BOOST',
includes='.',
+ defines='UNIT_TEST_CONFIG_PATH=\"%s/tmp-files/\"' %(bld.bldnode),
install_path=None)
if bld.env['HAVE_OSX_SECURITY']: