aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/utils/sqlite3/sqlite3.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/utils/sqlite3/sqlite3.cpp')
-rw-r--r--src/lib/utils/sqlite3/sqlite3.cpp46
1 files changed, 26 insertions, 20 deletions
diff --git a/src/lib/utils/sqlite3/sqlite3.cpp b/src/lib/utils/sqlite3/sqlite3.cpp
index 7f6626759..0ed8df83c 100644
--- a/src/lib/utils/sqlite3/sqlite3.cpp
+++ b/src/lib/utils/sqlite3/sqlite3.cpp
@@ -5,13 +5,13 @@
* Released under the terms of the Botan license
*/
-#include <botan/internal/sqlite3.h>
+#include <botan/sqlite3.h>
#include <stdexcept>
#include <sqlite3.h>
namespace Botan {
-sqlite3_database::sqlite3_database(const std::string& db_filename)
+Sqlite3_Database::Sqlite3_Database(const std::string& db_filename)
{
int rc = ::sqlite3_open(db_filename.c_str(), &m_db);
@@ -24,24 +24,29 @@ sqlite3_database::sqlite3_database(const std::string& db_filename)
}
}
-sqlite3_database::~sqlite3_database()
+Sqlite3_Database::~Sqlite3_Database()
{
if(m_db)
::sqlite3_close(m_db);
m_db = nullptr;
}
-size_t sqlite3_database::row_count(const std::string& table_name)
+std::shared_ptr<SQL_Database::Statement> Sqlite3_Database::new_statement(const std::string& base_sql) const
{
- sqlite3_statement stmt(this, "select count(*) from " + table_name);
+ return std::make_shared<Sqlite3_Statement>(m_db, base_sql);
+ }
+
+size_t Sqlite3_Database::row_count(const std::string& table_name)
+ {
+ auto stmt = new_statement("select count(*) from " + table_name);
- if(stmt.step())
- return stmt.get_size_t(0);
+ if(stmt->step())
+ return stmt->get_size_t(0);
else
throw std::runtime_error("Querying size of table " + table_name + " failed");
}
-void sqlite3_database::create_table(const std::string& table_schema)
+void Sqlite3_Database::create_table(const std::string& table_schema)
{
char* errmsg = nullptr;
int rc = ::sqlite3_exec(m_db, table_schema.c_str(), nullptr, nullptr, &errmsg);
@@ -56,44 +61,45 @@ void sqlite3_database::create_table(const std::string& table_schema)
}
}
-
-sqlite3_statement::sqlite3_statement(sqlite3_database* db, const std::string& base_sql)
+Sqlite3_Database::Sqlite3_Statement::Sqlite3_Statement(sqlite3* db, const std::string& base_sql)
{
- int rc = ::sqlite3_prepare_v2(db->m_db, base_sql.c_str(), -1, &m_stmt, nullptr);
+ int rc = ::sqlite3_prepare_v2(db, base_sql.c_str(), -1, &m_stmt, nullptr);
if(rc != SQLITE_OK)
throw std::runtime_error("sqlite3_prepare failed " + base_sql +
", code " + std::to_string(rc));
}
-void sqlite3_statement::bind(int column, const std::string& val)
+void Sqlite3_Database::Sqlite3_Statement::bind(int column, const std::string& val)
{
int rc = ::sqlite3_bind_text(m_stmt, column, val.c_str(), -1, SQLITE_TRANSIENT);
if(rc != SQLITE_OK)
throw std::runtime_error("sqlite3_bind_text failed, code " + std::to_string(rc));
}
-void sqlite3_statement::bind(int column, int val)
+void Sqlite3_Database::Sqlite3_Statement::bind(int column, size_t val)
{
+ if(val != static_cast<size_t>(static_cast<int>(val))) // is this legit?
+ throw std::runtime_error("sqlite3 cannot store " + std::to_string(val) + " without truncation");
int rc = ::sqlite3_bind_int(m_stmt, column, val);
if(rc != SQLITE_OK)
throw std::runtime_error("sqlite3_bind_int failed, code " + std::to_string(rc));
}
-void sqlite3_statement::bind(int column, std::chrono::system_clock::time_point time)
+void Sqlite3_Database::Sqlite3_Statement::bind(int column, std::chrono::system_clock::time_point time)
{
const int timeval = std::chrono::duration_cast<std::chrono::seconds>(time.time_since_epoch()).count();
bind(column, timeval);
}
-void sqlite3_statement::bind(int column, const std::vector<byte>& val)
+void Sqlite3_Database::Sqlite3_Statement::bind(int column, const std::vector<byte>& val)
{
int rc = ::sqlite3_bind_blob(m_stmt, column, &val[0], val.size(), SQLITE_TRANSIENT);
if(rc != SQLITE_OK)
throw std::runtime_error("sqlite3_bind_text failed, code " + std::to_string(rc));
}
-std::pair<const byte*, size_t> sqlite3_statement::get_blob(int column)
+std::pair<const byte*, size_t> Sqlite3_Database::Sqlite3_Statement::get_blob(int column)
{
BOTAN_ASSERT(::sqlite3_column_type(m_stmt, 0) == SQLITE_BLOB,
"Return value is a blob");
@@ -107,7 +113,7 @@ std::pair<const byte*, size_t> sqlite3_statement::get_blob(int column)
static_cast<size_t>(session_blob_size));
}
-size_t sqlite3_statement::get_size_t(int column)
+size_t Sqlite3_Database::Sqlite3_Statement::get_size_t(int column)
{
BOTAN_ASSERT(::sqlite3_column_type(m_stmt, column) == SQLITE_INTEGER,
"Return count is an integer");
@@ -119,17 +125,17 @@ size_t sqlite3_statement::get_size_t(int column)
return static_cast<size_t>(sessions_int);
}
-void sqlite3_statement::spin()
+void Sqlite3_Database::Sqlite3_Statement::spin()
{
while(step()) {}
}
-bool sqlite3_statement::step()
+bool Sqlite3_Database::Sqlite3_Statement::step()
{
return (::sqlite3_step(m_stmt) == SQLITE_ROW);
}
-sqlite3_statement::~sqlite3_statement()
+Sqlite3_Database::Sqlite3_Statement::~Sqlite3_Statement()
{
::sqlite3_finalize(m_stmt);
}