[Gcblue-commits] gcb_wx/src/sqlite sqlite3x_command.cpp,NONE,1.1 sqlite3x_connection.cpp,NONE,1.1 sq
Status: Alpha
Brought to you by:
ddcforge
|
From: Dewitt C. <ddc...@us...> - 2005-09-16 21:58:43
|
Update of /cvsroot/gcblue/gcb_wx/src/sqlite In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv27580/src/sqlite Added Files: sqlite3x_command.cpp sqlite3x_connection.cpp sqlite3x_exception.cpp sqlite3x_reader.cpp sqlite3x_transaction.cpp Log Message: --- NEW FILE: sqlite3x_exception.cpp --- /* Copyright (C) 2004-2005 Cory Nelson This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. CVS Info : $Author: ddcforge $ $Date: 2005/09/16 21:58:05 $ $Revision: 1.1 $ */ #include <sqlite3.h> #include "sqlite/sqlite3x.hpp" namespace sqlite3x { database_error::database_error(const char *msg) : runtime_error(msg) {} database_error::database_error(sqlite3_connection &con) : runtime_error(sqlite3_errmsg(con.db)) {} } --- NEW FILE: sqlite3x_transaction.cpp --- /* Copyright (C) 2004-2005 Cory Nelson This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. CVS Info : $Author: ddcforge $ $Date: 2005/09/16 21:58:05 $ $Revision: 1.1 $ */ #include <sqlite3.h> #include "sqlite/sqlite3x.hpp" namespace sqlite3x { sqlite3_transaction::sqlite3_transaction(sqlite3_connection &con, bool start) : con(con),intrans(false) { if(start) begin(); } sqlite3_transaction::~sqlite3_transaction() { if(intrans) { try { rollback(); } catch(...) { return; } } } void sqlite3_transaction::begin() { con.executenonquery("begin;"); intrans=true; } void sqlite3_transaction::commit() { con.executenonquery("commit;"); intrans=false; } void sqlite3_transaction::rollback() { con.executenonquery("rollback;"); intrans=false; } } --- NEW FILE: sqlite3x_command.cpp --- /* Copyright (C) 2004-2005 Cory Nelson This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. CVS Info : $Author: ddcforge $ $Date: 2005/09/16 21:58:05 $ $Revision: 1.1 $ */ #include <sqlite3.h> #include "sqlite/sqlite3x.hpp" namespace sqlite3x { sqlite3_command::sqlite3_command(sqlite3_connection &con, const char *sql) : con(con),refs(0) { const char *tail=NULL; if(sqlite3_prepare(con.db, sql, -1, &this->stmt, &tail)!=SQLITE_OK) throw database_error(con); this->argc=sqlite3_column_count(this->stmt); } sqlite3_command::sqlite3_command(sqlite3_connection &con, const wchar_t *sql) : con(con),refs(0) { const wchar_t *tail=NULL; if(sqlite3_prepare16(con.db, sql, -1, &this->stmt, (const void**)&tail)!=SQLITE_OK) throw database_error(con); this->argc=sqlite3_column_count(this->stmt); } sqlite3_command::sqlite3_command(sqlite3_connection &con, const std::string &sql) : con(con),refs(0) { const char *tail=NULL; if(sqlite3_prepare(con.db, sql.data(), (int)sql.length(), &this->stmt, &tail)!=SQLITE_OK) throw database_error(con); this->argc=sqlite3_column_count(this->stmt); } sqlite3_command::sqlite3_command(sqlite3_connection &con, const std::wstring &sql) : con(con),refs(0) { const wchar_t *tail=NULL; if(sqlite3_prepare16(con.db, sql.data(), (int)sql.length()*2, &this->stmt, (const void**)&tail)!=SQLITE_OK) throw database_error(con); this->argc=sqlite3_column_count(this->stmt); } sqlite3_command::~sqlite3_command() { sqlite3_finalize(this->stmt); } void sqlite3_command::bind(int index) { if(sqlite3_bind_null(this->stmt, index)!=SQLITE_OK) throw database_error(this->con); } void sqlite3_command::bind(int index, int data) { if(sqlite3_bind_int(this->stmt, index, data)!=SQLITE_OK) throw database_error(this->con); } void sqlite3_command::bind(int index, long long data) { if(sqlite3_bind_int64(this->stmt, index, data)!=SQLITE_OK) throw database_error(this->con); } void sqlite3_command::bind(int index, double data) { if(sqlite3_bind_double(this->stmt, index, data)!=SQLITE_OK) throw database_error(this->con); } void sqlite3_command::bind(int index, const char *data, int datalen) { if(sqlite3_bind_text(this->stmt, index, data, datalen, SQLITE_TRANSIENT)!=SQLITE_OK) throw database_error(this->con); } void sqlite3_command::bind(int index, const wchar_t *data, int datalen) { if(sqlite3_bind_text16(this->stmt, index, data, datalen, SQLITE_TRANSIENT)!=SQLITE_OK) throw database_error(this->con); } void sqlite3_command::bind(int index, const void *data, int datalen) { if(sqlite3_bind_blob(this->stmt, index, data, datalen, SQLITE_TRANSIENT)!=SQLITE_OK) throw database_error(this->con); } void sqlite3_command::bind(int index, const std::string &data) { if(sqlite3_bind_text(this->stmt, index, data.data(), (int)data.length(), SQLITE_TRANSIENT)!=SQLITE_OK) throw database_error(this->con); } void sqlite3_command::bind(int index, const std::wstring &data) { if(sqlite3_bind_text16(this->stmt, index, data.data(), (int)data.length()*2, SQLITE_TRANSIENT)!=SQLITE_OK) throw database_error(this->con); } sqlite3_reader sqlite3_command::executereader() { return sqlite3_reader(this); } void sqlite3_command::executenonquery() { this->executereader().read(); } int sqlite3_command::executeint() { sqlite3_reader reader=this->executereader(); if(!reader.read()) throw database_error("nothing to read"); return reader.getint(0); } long long sqlite3_command::executeint64() { sqlite3_reader reader=this->executereader(); if(!reader.read()) throw database_error("nothing to read"); return reader.getint64(0); } double sqlite3_command::executedouble() { sqlite3_reader reader=this->executereader(); if(!reader.read()) throw database_error("nothing to read"); return reader.getdouble(0); } std::string sqlite3_command::executestring() { sqlite3_reader reader=this->executereader(); if(!reader.read()) throw database_error("nothing to read"); return reader.getstring(0); } std::wstring sqlite3_command::executestring16() { sqlite3_reader reader=this->executereader(); if(!reader.read()) throw database_error("nothing to read"); return reader.getstring16(0); } std::string sqlite3_command::executeblob() { sqlite3_reader reader=this->executereader(); if(!reader.read()) throw database_error("nothing to read"); return reader.getblob(0); } } --- NEW FILE: sqlite3x_connection.cpp --- /* Copyright (C) 2004-2005 Cory Nelson This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. CVS Info : $Author: ddcforge $ $Date: 2005/09/16 21:58:05 $ $Revision: 1.1 $ */ #include <sqlite3.h> #include "sqlite/sqlite3x.hpp" namespace sqlite3x { sqlite3_connection::sqlite3_connection() : db(NULL) {} sqlite3_connection::sqlite3_connection(const char *db) : db(NULL) { this->open(db); } sqlite3_connection::sqlite3_connection(const wchar_t *db) : db(NULL) { this->open(db); } sqlite3_connection::~sqlite3_connection() { if(this->db) sqlite3_close(this->db); } void sqlite3_connection::open(const char *db) { if(sqlite3_open(db, &this->db)!=SQLITE_OK) throw database_error("unable to open database"); } void sqlite3_connection::open(const wchar_t *db) { if(sqlite3_open16(db, &this->db)!=SQLITE_OK) throw database_error("unable to open database"); } void sqlite3_connection::close() { if(this->db) { if(sqlite3_close(this->db)!=SQLITE_OK) throw database_error(*this); this->db=NULL; } } long long sqlite3_connection::insertid() { if(!this->db) throw database_error("database is not open"); return sqlite3_last_insert_rowid(this->db); } void sqlite3_connection::setbusytimeout(int ms) { if(!this->db) throw database_error("database is not open"); if(sqlite3_busy_timeout(this->db, ms)!=SQLITE_OK) throw database_error(*this); } void sqlite3_connection::executenonquery(const char *sql) { if(!this->db) throw database_error("database is not open"); sqlite3_command(*this, sql).executenonquery(); } void sqlite3_connection::executenonquery(const wchar_t *sql) { if(!this->db) throw database_error("database is not open"); sqlite3_command(*this, sql).executenonquery(); } void sqlite3_connection::executenonquery(const std::string &sql) { if(!this->db) throw database_error("database is not open"); sqlite3_command(*this, sql).executenonquery(); } void sqlite3_connection::executenonquery(const std::wstring &sql) { if(!this->db) throw database_error("database is not open"); sqlite3_command(*this, sql).executenonquery(); } int sqlite3_connection::executeint(const char *sql) { if(!this->db) throw database_error("database is not open"); return sqlite3_command(*this, sql).executeint(); } int sqlite3_connection::executeint(const wchar_t *sql) { if(!this->db) throw database_error("database is not open"); return sqlite3_command(*this, sql).executeint(); } int sqlite3_connection::executeint(const std::string &sql) { if(!this->db) throw database_error("database is not open"); return sqlite3_command(*this, sql).executeint(); } int sqlite3_connection::executeint(const std::wstring &sql) { if(!this->db) throw database_error("database is not open"); return sqlite3_command(*this, sql).executeint(); } long long sqlite3_connection::executeint64(const char *sql) { if(!this->db) throw database_error("database is not open"); return sqlite3_command(*this, sql).executeint64(); } long long sqlite3_connection::executeint64(const wchar_t *sql) { if(!this->db) throw database_error("database is not open"); return sqlite3_command(*this, sql).executeint64(); } long long sqlite3_connection::executeint64(const std::string &sql) { if(!this->db) throw database_error("database is not open"); return sqlite3_command(*this, sql).executeint64(); } long long sqlite3_connection::executeint64(const std::wstring &sql) { if(!this->db) throw database_error("database is not open"); return sqlite3_command(*this, sql).executeint64(); } double sqlite3_connection::executedouble(const char *sql) { if(!this->db) throw database_error("database is not open"); return sqlite3_command(*this, sql).executedouble(); } double sqlite3_connection::executedouble(const wchar_t *sql) { if(!this->db) throw database_error("database is not open"); return sqlite3_command(*this, sql).executedouble(); } double sqlite3_connection::executedouble(const std::string &sql) { if(!this->db) throw database_error("database is not open"); return sqlite3_command(*this, sql).executedouble(); } double sqlite3_connection::executedouble(const std::wstring &sql) { if(!this->db) throw database_error("database is not open"); return sqlite3_command(*this, sql).executedouble(); } std::string sqlite3_connection::executestring(const char *sql) { if(!this->db) throw database_error("database is not open"); return sqlite3_command(*this, sql).executestring(); } std::string sqlite3_connection::executestring(const wchar_t *sql) { if(!this->db) throw database_error("database is not open"); return sqlite3_command(*this, sql).executestring(); } std::string sqlite3_connection::executestring(const std::string &sql) { if(!this->db) throw database_error("database is not open"); return sqlite3_command(*this, sql).executestring(); } std::string sqlite3_connection::executestring(const std::wstring &sql) { if(!this->db) throw database_error("database is not open"); return sqlite3_command(*this, sql).executestring(); } std::wstring sqlite3_connection::executestring16(const char *sql) { if(!this->db) throw database_error("database is not open"); return sqlite3_command(*this, sql).executestring16(); } std::wstring sqlite3_connection::executestring16(const wchar_t *sql) { if(!this->db) throw database_error("database is not open"); return sqlite3_command(*this, sql).executestring16(); } std::wstring sqlite3_connection::executestring16(const std::string &sql) { if(!this->db) throw database_error("database is not open"); return sqlite3_command(*this, sql).executestring16(); } std::wstring sqlite3_connection::executestring16(const std::wstring &sql) { if(!this->db) throw database_error("database is not open"); return sqlite3_command(*this, sql).executestring16(); } std::string sqlite3_connection::executeblob(const char *sql) { if(!this->db) throw database_error("database is not open"); return sqlite3_command(*this, sql).executeblob(); } std::string sqlite3_connection::executeblob(const wchar_t *sql) { if(!this->db) throw database_error("database is not open"); return sqlite3_command(*this, sql).executeblob(); } std::string sqlite3_connection::executeblob(const std::string &sql) { if(!this->db) throw database_error("database is not open"); return sqlite3_command(*this, sql).executeblob(); } std::string sqlite3_connection::executeblob(const std::wstring &sql) { if(!this->db) throw database_error("database is not open"); return sqlite3_command(*this, sql).executeblob(); } } --- NEW FILE: sqlite3x_reader.cpp --- /* Copyright (C) 2004-2005 Cory Nelson This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. CVS Info : $Author: ddcforge $ $Date: 2005/09/16 21:58:05 $ $Revision: 1.1 $ */ #include <sqlite3.h> #include "sqlite/sqlite3x.hpp" namespace sqlite3x { sqlite3_reader::sqlite3_reader() : cmd(NULL) {} sqlite3_reader::sqlite3_reader(const sqlite3_reader ©) : cmd(copy.cmd) { if(this->cmd) ++this->cmd->refs; } sqlite3_reader::sqlite3_reader(sqlite3_command *cmd) : cmd(cmd) { ++cmd->refs; } sqlite3_reader::~sqlite3_reader() { this->close(); } sqlite3_reader& sqlite3_reader::operator=(const sqlite3_reader ©) { this->close(); this->cmd=copy.cmd; if(this->cmd) ++this->cmd->refs; return *this; } bool sqlite3_reader::read() { if(!this->cmd) throw database_error("reader is closed"); switch(sqlite3_step(this->cmd->stmt)) { case SQLITE_ROW: return true; case SQLITE_DONE: return false; default: throw database_error(this->cmd->con); } } void sqlite3_reader::reset() { if(!this->cmd) throw database_error("reader is closed"); if(sqlite3_reset(this->cmd->stmt)!=SQLITE_OK) throw database_error(this->cmd->con); } void sqlite3_reader::close() { if(this->cmd) { if(--this->cmd->refs==0) sqlite3_reset(this->cmd->stmt); this->cmd=NULL; } } int sqlite3_reader::getint(int index) { if(!this->cmd) throw database_error("reader is closed"); if((index)>(this->cmd->argc-1)) throw std::out_of_range("index out of range"); return sqlite3_column_int(this->cmd->stmt, index); } long long sqlite3_reader::getint64(int index) { if(!this->cmd) throw database_error("reader is closed"); if((index)>(this->cmd->argc-1)) throw std::out_of_range("index out of range"); return sqlite3_column_int64(this->cmd->stmt, index); } /** * Modified code, using 2.x atol on string version here */ long sqlite3_reader::getlong(int index) { std::string s = this->getstring(index); return atol(s.c_str()); } double sqlite3_reader::getdouble(int index) { if(!this->cmd) throw database_error("reader is closed"); if((index)>(this->cmd->argc-1)) throw std::out_of_range("index out of range"); return sqlite3_column_double(this->cmd->stmt, index); } std::string sqlite3_reader::getstring(int index) { if(!this->cmd) throw database_error("reader is closed"); if((index)>(this->cmd->argc-1)) throw std::out_of_range("index out of range"); return std::string((const char*)sqlite3_column_text(this->cmd->stmt, index), sqlite3_column_bytes(this->cmd->stmt, index)); } std::wstring sqlite3_reader::getstring16(int index) { if(!this->cmd) throw database_error("reader is closed"); if((index)>(this->cmd->argc-1)) throw std::out_of_range("index out of range"); return std::wstring((const wchar_t*)sqlite3_column_text16(this->cmd->stmt, index), sqlite3_column_bytes16(this->cmd->stmt, index)/2); } std::string sqlite3_reader::getblob(int index) { if(!this->cmd) throw database_error("reader is closed"); if((index)>(this->cmd->argc-1)) throw std::out_of_range("index out of range"); return std::string((const char*)sqlite3_column_blob(this->cmd->stmt, index), sqlite3_column_bytes(this->cmd->stmt, index)); } std::string sqlite3_reader::getcolname(int index) { if(!this->cmd) throw database_error("reader is closed"); if((index)>(this->cmd->argc-1)) throw std::out_of_range("index out of range"); return sqlite3_column_name(this->cmd->stmt, index); } std::wstring sqlite3_reader::getcolname16(int index) { if(!this->cmd) throw database_error("reader is closed"); if((index)>(this->cmd->argc-1)) throw std::out_of_range("index out of range"); return (const wchar_t*)sqlite3_column_name16(this->cmd->stmt, index); } } |