28#include "sqlite3x.hpp"
36 return ((SQLITE_DONE==rc) || (SQLITE_OK==rc) || (SQLITE_ROW==rc));
42 : m_db(NULL), m_name(dbn)
56 throw database_error(
"sqlite3_connection(sqlite3*) ctor was passed a null db handle." );
77 if( this->m_db == dbh )
return;
80 if( this->m_db || (!dbh) )
99 sqlite3 * ret = this->m_db;
116 char const * m = this->m_db ? sqlite3_errmsg(this->m_db) :
"";
126 this->m_name =
db ?
db :
"";
127 if(sqlite3_open(
db, &this->m_db)!=SQLITE_OK)
141 try { this->
close(); }
149 return this->
open( db.c_str() );
152#if SQLITE3X_USE_WCHAR
154 if(sqlite3_open16(
db, &this->m_db)!=SQLITE_OK)
162 try { this->
close(); }
171 sqlite3 * x = this->m_db;
173 if(sqlite3_close(x)!=SQLITE_OK)
180 return sqlite3_last_insert_rowid(this->m_db);
185 return sqlite3_changes(this->m_db);
192 if(sqlite3_busy_timeout(this->m_db, ms)!=SQLITE_OK)
205#if SQLITE3X_USE_WCHAR
220#if SQLITE3X_USE_WCHAR
236#if SQLITE3X_USE_WCHAR
252#if SQLITE3X_USE_WCHAR
264#if SQLITE3X_USE_WCHAR
271#if SQLITE3X_USE_WCHAR
272 std::wstring sqlite3_connection::executestring16(
const std::string &sql) {
273 if(!this->m_db)
throw database_error(
"database is not open");
274 return sqlite3_command(*
this, sql).executestring16();
278#if SQLITE3X_USE_WCHAR
279 std::wstring sqlite3_connection::executestring16(
const std::wstring &sql) {
280 if(!this->m_db)
throw database_error(
"database is not open");
281 return sqlite3_command(*
this, sql).executestring16();
290#if SQLITE3X_USE_WCHAR
298 sqlite3_callback callback,
300 std::string & errmsg )
307 ret = sqlite3_exec( this->m_db, sql.c_str(), callback, data, &cerrmsg );
314 sqlite3_free( cerrmsg );
321 sqlite3_free( cerrmsg );
327 sqlite3_callback func,
343 std::vector<std::string> list;
375 int check = con.
executeint(
"select count(*) from sqlite_master where type like 'table' and name like '"+n+
"'" );
379 throw database_error(
"table_generator() db table '%s' already exists.", n.c_str() );
381 this->m_pimpl->db = &con;
382 this->m_pimpl->name = n;
387 delete this->m_pimpl;
392 this->m_pimpl->list.push_back( fld );
398 size_t sz = this->m_pimpl->list.size();
401 throw database_error(
"table_generator::operator(): cannot create a table with no fields. Try using operator()(string) to add fields." );
403 std::ostringstream os;
404 os <<
"create table "<< this->m_pimpl->name <<
"(";
405 for(
size_t i = 0; i < sz; ++i )
407 os << this->m_pimpl->list[i];
408 if( i < (sz-1) ) os <<
",";
Exception type used by the sqlite3x classes.
Encapsulates a command to send to an sqlite3_connection.
int executeint()
Executes the query, which is expected to have an integer field as the first result field.
void executenonquery()
Executes the query and provides no way to get the results.
std::string executeblob()
Executes the query, which is expected to have a string or blob field as the first result field.
double executedouble()
Executes the query, which is expected to have a double field as the first result field.
std::string executestring()
Executes the query, which is expected to have a string or blob field as the first result field.
int64_t executeint64()
Executes the query, which is expected to have a (int64_t) field as the first result field.
Represents a connection to an sqlite3 database.
void close()
Closes this database.
sqlite3_connection()
Default ctor.
sqlite3 * db() const
Returns a handle to the underlying sqlite3 database.
void executenonquery(const std::string &sql)
Executes a command which is assumed to have a single step and a void result.
virtual void open(char const *)
Creates/opens the given db, throwing on error.
int64_t executeint64(const std::string &sql)
Executes the query, which is expected to have a (int64_t) field as the first result field.
int64_t insertid()
Returns the rowid of the most recently inserted row on this db.
std::string name() const
Returns this object's name.
int changes()
Returns the number of database rows that were changed (or inserted or deleted) by the most recently c...
sqlite3 * take()
Transfers ownership of the returned handle to the caller.
virtual ~sqlite3_connection()
Calls this->close() if close() has not already been called.
std::string executeblob(const std::string &sql)
Executes the query, which is expected to have a string or blob field as the first result field.
double executedouble(const std::string &sql)
Executes the query, which is expected to have a double field as the first result field.
int executecallback(std::string const &sql, sqlite3_callback callback, void *data, std::string &errmsg)
Executes the given SQL code, calling callback for each row of the data set.
void setbusytimeout(int ms)
See sqlite3_busy_timeout().
std::string executestring(const std::string &sql)
Executes the query, which is expected to have a string or blob field as the first result field.
std::string errormsg() const
Returns the equivalent of sqlite3_errmsg(), or an empty string if that function returns null.
virtual void on_open()
This function is called when open() succeeds.
int executeint(const std::string &sql)
Executes the query, which is expected to have an integer field as the first result field.
An internal implementation detail of table_generator.
table_generator(sqlite3_connection &con, std::string const &name)
Initializes the table generation process.
~table_generator()
Frees up internal resources.
void create()
Executes the 'create table' statements.
table_generator & operator()(std::string const &field_name)
Adds field_name as a field of this table.
This namespace encapsulates a C++ API wrapper for sqlite3 databases.
sqlite_int64 int64_t
64-bit integer type used by this code.
bool rc_is_okay(int rc)
rc_is_okay() is an easy way to check if rc is one of SQLITE_OK, SQLITE_ROW, or SQLITE_DONE.