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) // Windows uses this 19 # define SQ3_USE_WCHAR 1 21 # define SQ3_USE_WCHAR 0 // default 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 );
583 #endif // SQ3_USE_WCHAR 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 );
1034 #endif // SQ3_USE_WCHAR 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 );
1277 #endif // s11n_net_SQ3_HPP_INCLUDED int finalize()
Finizalizes the underlying prepared statement, freeing its resources.
Encapsulates a connection to an sqlite database.
~statement()
Calls this->finalize()
char const * colname(int index)
On success, it returns the null-terminated column name of the given column.
int execute(const std::string &sql)
Functionally identical to execute(char const *).
~cursor()
A curious side-effect which one needs to be aware of but very rarely is an issue:
char unsigned sqlite3_text_char_t
The published result codes from the sqlite3 API.
virtual int on_open()
This function is called when open() succeeds.
int bind(int index)
Binds NULL to the given placeholder index (1-based, not 0-based!).
void close()
"Disconnects" this object from the underlying result set, making this object useless for anything but...
sqlite_uint64 uint64_t
The type used for unsigned 64-bit integer operations.
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...
cursor & operator=(cursor const &)
See the copy ctor.
void operator()(sqlite3 *&t)
Calls sqlite3_close(t) and assigns t to 0.
int vacuum()
Convenience wrapper around execute("vacuum").
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 get_cursor()
Returns a cursor object ready to step over the result set from this object.
sqlite3 * handle() const
The low-level handle to the sqlite db.
This type represents a transaction block in an SQL session.
A specialized dtor to call reset sqlite3_stmt handles(), for use with refcount::rcptr<sqlite3,...
virtual int open(char const *, long flags=0)
Creates/opens the given db file.
int pragma(char const *code)
This is a convenience wrapper for execute( "pragma ..." ).
virtual ~database()
Closes this db.
void operator()(::sq3::statement *&t)
Calls t->reset() and assigns t to 0.
database()
Creates an unopened database.
cursor()
Creates an empty cursor, whose only valid use is to assign it from another cursor.
bool is_prepared() const
Use after construction to ensure that a statement was compiled.
int changes()
Returns the number of database rows that were changed (or inserted or deleted) by the most recently c...
std::string errormsg() const
Returns the last error message from sqlite, or an empty string if this object is not opened.
int execute()
Assumes this object's SQL statement is a single statement.
virtual int clear()
Looks through sqlite_master for a list of views, triggers, and tables, and drops them all (in that or...
int reset()
This is functionally the same as calling reset on the underlying prepared statement object to which t...
int commit()
Commits the active transaction.
The sq3 namespace encapsulates an OO sqlite3 API very similar to the sqlite3x API,...
A specialized dtor to close sqlite3 handles, for use with refcount::rcptr<sqlite3,...
int colcount()
Returns the column count of this prepared statement, or -1 on error.
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.
void operator()(sqlite3_stmt *&t)
Calls sqlite3_finalize(t) and assigns t to 0.
int begin()
Starts the transaction.
int setbusytimeout(int ms)
See sqlite3_busy_timeout().
int64_t insertid()
Returns the rowid of the most recently inserted row on this db.
int reset()
Calls sqlite3_reset() on the underlying statement handle and returns the result.
void operator()(sqlite3_stmt *&t)
Calls sqlite3_reset(t) and assigns t to 0.
int colname(int index, std::string &str)
Sets str to the column name as the given index (0-based).
int colcount()
Returns the column count of the underlying prepared statement.
sqlite3 * take_handle()
Transfers ownership of this->handle() to the caller.
statement(database &db)
Initializes a prepared statement without a query.
sqlite_int64 int64_t
The type used for signed 64-bit integer operations.
~transaction()
Calls this->rollback()
A specialized dtor to finalize sqlite3_stmt handles, for use with refcount::rcptr<sqlite3,...
bool is_open() const
Returns true if this db is opened.
int close(bool force=false)
"Closes" this db.
This type is for stepping through a db query result.
int rollback()
Initiates a rollback and returns the result of executing a rollback command.
std::string name() const
Returns the name of the db file.
int prepare(std::string const &sql)
(Re-)prepares an SQL statement.
A specialized dtor to reset (not close) sq3::statement objects, for use with refcount::rcptr<sqlite3,...