1#ifndef s11n_net_SQ3_HPP_INCLUDED
2#define s11n_net_SQ3_HPP_INCLUDED 1
9# define SQ3_USE_WCHAR 0
16# ifdef _GLIBCXX_USE_WCHAR_T
17# define SQ3_USE_WCHAR 1
18# elif defined(UNICODE)
19# define SQ3_USE_WCHAR 1
21# define SQ3_USE_WCHAR 0
26# warning SQ3_USE_WCHAR: INCOMPLETE/BROKEN code is enabled!
35#include "refcount.hpp"
293 explicit database( std::string
const & filename );
310 std::string
name()
const;
378 virtual int open(
char const *,
long flags = 0 );
386 int open( std::string
const &,
long flags = 0 );
417 int close(
bool force =
false );
448 int execute(
const std::string &sql);
466 int execute(std::string
const & sql,
int & tgt);
471 int execute(
char const * sql,
int & tgt);
486 int execute(
char const * sql,
double & tgt);
490 int execute(std::string
const & sql,
double & tgt);
495 int execute(
char const * sql, std::string & tgt);
499 int execute(std::string
const & sql, std::string & tgt);
527 int execute(std::string
const & sql,
void const ** tgt,
int & sz );
533 int execute(
char const * sql,
void const ** tgt,
int & sz );
543 int execute( std::string
const & sql, sqlite3_callback callback,
void * data, std::string & errmsg );
547 int execute(
char const * sql, sqlite3_callback callback,
void * data, std::string & errmsg );
554 int execute( std::string
const & sql, sqlite3_callback callback,
void * data = 0 );
560 int execute(
char const * sql, sqlite3_callback callback,
void * data = 0 );
569 int pragma(
char const * code );
579 int open(
wchar_t const * dbname );
580 int open( std::wstring
const & dbname );
693 typedef std::map<std::string,int> NameToIndexMap;
694 NameToIndexMap * m_cn;
701 void copy(
cursor const & rhs );
720 int index_colnames();
813 int isnull(
int index,
bool & tgt );
821 int get(
int index,
int & tgt );
829 int get(
int index,
double & tgt );
833 int get(
int index, std::string & tgt );
851 int get(
int index,
void const ** tgt,
int & sz );
871 int get( std::string
const & key,
int & tgt );
875 int get( std::string
const & key,
int64_t & tgt );
879 int get( std::string
const & key,
double & tgt );
883 int get( std::string
const & key, std::string & tgt );
901 int get( std::string
const & key,
void const ** tgt,
int & sz );
909 int colname(
int index, std::string & str );
922 int colname(
int index,
char const ** str );
1013 int prepare( std::string
const & sql );
1019 int prepare(
char const * sql,
int byteCount = -1 );
1029 int prepare( sqlite3_wstring_t
const sql,
int byteCount = -1 );
1055 int bind(
int index );
1060 int bind(
int index,
int data );
1070 int bind(
int index,
double data );
1075 int bind(
int index,
char const * data,
int len );
1080 int bind(
int index,
void const * data,
int len );
1086 int bind(
int index, std::string
const & data );
1105 int bind(
char const * index );
1110 int bind(
char const * index,
int data );
1120 int bind(
char const * index,
double data );
1126 int bind(
char const * index,
char const * data,
int len );
1132 int bind(
char const * index,
void const * data,
int len );
1138 int bind(
char const * index, std::string
const & data );
1188 int execute( std::string & tgt );
1217 int execute(
void const ** tgt,
int & len );
1259 char const *
colname(
int index );
1270 int colname(
int index,
char const ** cn );
This type is for stepping through a db query result.
int isnull(int index, bool &tgt)
If column index (0-based) is in bounds then this function check if the value of the given column inde...
int reset()
This is functionally the same as calling reset on the underlying prepared statement object to which t...
void close()
"Disconnects" this object from the underlying result set, making this object useless for anything but...
int step()
Uses sqlite3_step() to step through this object's data set by one step.
int get(int index, int &tgt)
If column index (0-based) is in bounds then this function assigns tgt to the value of the given colum...
~cursor()
A curious side-effect which one needs to be aware of but very rarely is an issue:
cursor()
Creates an empty cursor, whose only valid use is to assign it from another cursor.
int colcount()
Returns the column count of the underlying prepared statement.
cursor & operator=(cursor const &)
See the copy ctor.
int colname(int index, std::string &str)
Sets str to the column name as the given index (0-based).
Encapsulates a connection to an sqlite database.
database()
Creates an unopened database.
int setbusytimeout(int ms)
See sqlite3_busy_timeout().
bool is_open() const
Returns true if this db is opened.
sqlite3 * handle() const
The low-level handle to the sqlite db.
int vacuum()
Convenience wrapper around execute("vacuum").
virtual ~database()
Closes this db.
int execute(const std::string &sql)
Functionally identical to execute(char const *).
int changes()
Returns the number of database rows that were changed (or inserted or deleted) by the most recently c...
sqlite3 * take_handle()
Transfers ownership of this->handle() to the caller.
std::string name() const
Returns the name of the db file.
std::string errormsg() const
Returns the last error message from sqlite, or an empty string if this object is not opened.
virtual int open(char const *, long flags=0)
Creates/opens the given db file.
virtual int clear()
Looks through sqlite_master for a list of views, triggers, and tables, and drops them all (in that or...
int pragma(char const *code)
This is a convenience wrapper for execute( "pragma ..." ).
int64_t insertid()
Returns the rowid of the most recently inserted row on this db.
virtual int on_open()
This function is called when open() succeeds.
int close(bool force=false)
"Closes" this db.
cursor get_cursor()
Returns a cursor object ready to step over the result set from this object.
int colcount()
Returns the column count of this prepared statement, or -1 on error.
int bind(int index)
Binds NULL to the given placeholder index (1-based, not 0-based!).
~statement()
Calls this->finalize()
int prepare(std::string const &sql)
(Re-)prepares an SQL statement.
int execute()
Assumes this object's SQL statement is a single statement.
int finalize()
Finizalizes the underlying prepared statement, freeing its resources.
char const * colname(int index)
On success, it returns the null-terminated column name of the given column.
int reset()
Calls sqlite3_reset() on the underlying statement handle and returns the result.
bool is_prepared() const
Use after construction to ensure that a statement was compiled.
This type represents a transaction block in an SQL session.
int commit()
Commits the active transaction.
int begin()
Starts the transaction.
~transaction()
Calls this->rollback()
int rollback()
Initiates a rollback and returns the result of executing a rollback command.
The sq3 namespace encapsulates an OO sqlite3 API very similar to the sqlite3x API,...
char unsigned sqlite3_text_char_t
A char type used by some of the sqlite3 API to represent text data.
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.
sqlite_int64 int64_t
The type used for signed 64-bit integer operations.
sqlite_uint64 uint64_t
The type used for unsigned 64-bit integer operations.
A specialized dtor to close sqlite3 handles, for use with refcount::rcptr<sqlite3,...
void operator()(sqlite3 *&t)
Calls sqlite3_close(t) and assigns t to 0.
A specialized dtor to finalize sqlite3_stmt handles, for use with refcount::rcptr<sqlite3,...
void operator()(sqlite3_stmt *&t)
Calls sqlite3_finalize(t) and assigns t to 0.
A specialized dtor to call reset sqlite3_stmt handles(), for use with refcount::rcptr<sqlite3,...
void operator()(sqlite3_stmt *&t)
Calls sqlite3_reset(t) and assigns t to 0.
A specialized dtor to reset (not close) sq3::statement objects, for use with refcount::rcptr<sqlite3,...
void operator()(::sq3::statement *&t)
Calls t->reset() and assigns t to 0.