From 0883443ca4337ddcfd972060fa028f6e999da55e Mon Sep 17 00:00:00 2001 From: Duncan Mac-Vicar P Date: Tue, 27 Jun 2006 16:19:00 +0000 Subject: [PATCH] can initialize the KnownSourcesCache, and import the old sources. next steps: implement knownSources, port SourceManager --- configure.ac | 4 +- devel/devel.dmacvicar/testbed.cc | 27 +--- zypp/Makefile.am | 3 +- zypp/cache/KnownSourcesCache.cpp | 110 ++++++++++++++ zypp/cache/KnownSourcesCache.h | 66 +++++++++ zypp/cache/Makefile.am | 25 ++++ zypp/cache/sqlite3x/Makefile.am | 20 +++ zypp/cache/sqlite3x/sqlite3x.hpp | 183 +++++++++++++++++++++++ zypp/cache/sqlite3x/sqlite3x_command.cpp | 156 ++++++++++++++++++++ zypp/cache/sqlite3x/sqlite3x_connection.cpp | 209 +++++++++++++++++++++++++++ zypp/cache/sqlite3x/sqlite3x_exception.cpp | 34 +++++ zypp/cache/sqlite3x/sqlite3x_reader.cpp | 129 +++++++++++++++++ zypp/cache/sqlite3x/sqlite3x_transaction.cpp | 61 ++++++++ 13 files changed, 1005 insertions(+), 22 deletions(-) create mode 100644 zypp/cache/KnownSourcesCache.cpp create mode 100644 zypp/cache/KnownSourcesCache.h create mode 100644 zypp/cache/Makefile.am create mode 100644 zypp/cache/sqlite3x/Makefile.am create mode 100644 zypp/cache/sqlite3x/sqlite3x.hpp create mode 100644 zypp/cache/sqlite3x/sqlite3x_command.cpp create mode 100644 zypp/cache/sqlite3x/sqlite3x_connection.cpp create mode 100644 zypp/cache/sqlite3x/sqlite3x_exception.cpp create mode 100644 zypp/cache/sqlite3x/sqlite3x_reader.cpp create mode 100644 zypp/cache/sqlite3x/sqlite3x_transaction.cpp diff --git a/configure.ac b/configure.ac index 1519c9a..be7ecb2 100644 --- a/configure.ac +++ b/configure.ac @@ -250,7 +250,9 @@ AC_OUTPUT( po/Makefile.in\ zypp/pool/Makefile \ zypp/solver/Makefile \ zypp/solver/detail/Makefile \ - zypp/source/Makefile \ + zypp/cache/Makefile \ + zypp/cache/sqlite3x/Makefile + zypp/source/Makefile \ zypp/source/plaindir/Makefile \ zypp/source/susetags/Makefile \ zypp/source/yum/Makefile \ diff --git a/devel/devel.dmacvicar/testbed.cc b/devel/devel.dmacvicar/testbed.cc index 0a8a880..4ff4862 100644 --- a/devel/devel.dmacvicar/testbed.cc +++ b/devel/devel.dmacvicar/testbed.cc @@ -15,41 +15,28 @@ #include /////////////////////////////////////////////////////////////////// -#include #include -#include -#include -#include -#include -#include -#include + #include #include -#include -#include -#include +#include "zypp/CapFactory.h" +#include "zypp/KeyRing.h" +#include "zypp/PublicKey.h" + +#include "zypp/cache/KnownSourcesCache.h" using namespace zypp::detail; using namespace std; using namespace zypp; -using namespace zypp::parser::yum; -using namespace zypp::source::yum; - - //using namespace DbXml; int main() { - zypp::devel::PublicKey k("/suse/dmacvicar/duncan.txt"); - - cout << k.id() << std::endl; - cout << k.fingerprint() << std::endl; - cout << k.name() << std::endl; - + cache::KnownSourcesCache("/"); } diff --git a/zypp/Makefile.am b/zypp/Makefile.am index 8167dd0..22b8950 100644 --- a/zypp/Makefile.am +++ b/zypp/Makefile.am @@ -2,7 +2,7 @@ ## ################################################## SUBDIRS = base thread url media capability detail pool parser \ - source target solver zypp_detail ui + source cache target solver zypp_detail ui AM_CXXFLAGS = -DZYPP_BASE_LOGGER_LOGGROUP=\"zypp\" @@ -173,6 +173,7 @@ lib@PACKAGE@_la_LIBADD = thread/lib@PACKAGE@_thread.la \ pool/lib@PACKAGE@_pool.la \ parser/lib@PACKAGE@_parser.la \ source/lib@PACKAGE@_source.la \ + cache/lib@PACKAGE@_cache.la \ media/lib@PACKAGE@_media.la \ url/lib@PACKAGE@_url.la \ target/lib@PACKAGE@_target.la \ diff --git a/zypp/cache/KnownSourcesCache.cpp b/zypp/cache/KnownSourcesCache.cpp new file mode 100644 index 0000000..bf0c88a --- /dev/null +++ b/zypp/cache/KnownSourcesCache.cpp @@ -0,0 +1,110 @@ +/*---------------------------------------------------------------------\ +| ____ _ __ __ ___ | +| |__ / \ / / . \ . \ | +| / / \ V /| _/ _/ | +| / /__ | | | | | | | +| /_____||_| |_| |_| | +| | +\---------------------------------------------------------------------*/ + +#include "zypp/base/Logger.h" +#include "zypp/cache/KnownSourcesCache.h" +#include "zypp/target/store/PersistentStorage.h" +#include "zypp/cache/sqlite3x/sqlite3x.hpp" + +#define ZYPP_DB_FILE "/var/lib/zypp/zypp.db" + +using namespace sqlite3x; +using namespace std; + +////////////////////////////////////////////////////////////////// +namespace zypp +{ ///////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////// +namespace cache +{ ///////////////////////////////////////////////////////////////// + +KnownSourcesCache::KnownSourcesCache( const Pathname &root_r ) +{ + try + { + sqlite3_connection con(ZYPP_DB_FILE); + + { + int count = con.executeint("select count(*) from sqlite_master where type='table' and name='sources';"); + if( count==0 ) + { + con.executenonquery("create table sources ( id integer primary key autoincrement, alias varchar, url varchar, description varchar, enabled integer, autorefresh integer, type varchar, cachedir varchar, path varchar);"); + + try + { + // import old sources + storage::PersistentStorage store; + store.init( root_r ); + source::SourceInfoList old_sources = store.storedSources(); + for ( source::SourceInfoList::const_iterator it = old_sources.begin(); it != old_sources.end(); it++ ) + { + storeSource( *it ); + } + } + catch(std::exception &e) + { + ERR << "Exception Occured: " << e.what() << endl; + } + } + } + + con.close(); + } + catch(exception &ex) { + ERR << "Exception Occured: " << ex.what() << endl; + } +} + +KnownSourcesCache::~KnownSourcesCache() +{ +} + +source::SourceInfoList KnownSourcesCache::knownSources() const +{ + return source::SourceInfoList(); +} + +void KnownSourcesCache::storeSource( const source::SourceInfo &info ) +{ + try + { + sqlite3_connection con(ZYPP_DB_FILE); + sqlite3_transaction trans(con); + + { + sqlite3_command cmd(con, "insert into sources ( alias, url, description, enabled, autorefresh, type, cachedir, path) values ( ?, ?, ?, ? , ?, ?, ?, ?);"); + cmd.bind(1, info.alias()); + cmd.bind(2, info.url().asCompleteString()); + // FIXME + cmd.bind(4, info.enabled() ? 1 : 0 ); + cmd.bind(5, info.autorefresh() ? 1 : 0 ); + cmd.bind(6, info.type()); + cmd.bind(6, info.cacheDir().asString()); + cmd.bind(7, info.path().asString()); + + cmd.executenonquery(); + } + + trans.commit(); // note: if trans goes out of scope (due to an exception or anything else) before calling commit(), it will automatically rollback() + + con.close(); + } + catch(exception &ex) { + ERR << "Exception Occured: " << ex.what() << endl; + } +} + +std::ostream & KnownSourcesCache::dumpOn( std::ostream & str ) const +{ + return str; +} + +} +} + diff --git a/zypp/cache/KnownSourcesCache.h b/zypp/cache/KnownSourcesCache.h new file mode 100644 index 0000000..567d6d8 --- /dev/null +++ b/zypp/cache/KnownSourcesCache.h @@ -0,0 +1,66 @@ +/*---------------------------------------------------------------------\ +| ____ _ __ __ ___ | +| |__ / \ / / . \ . \ | +| / / \ V /| _/ _/ | +| / /__ | | | | | | | +| /_____||_| |_| |_| | +| | +\---------------------------------------------------------------------*/ +/** \file zypp/KnownSourcesCache.h + * +*/ +#ifndef ZYPP_KnownSourcesCache_H +#define ZYPP_KnownSourcesCache_H + +#include +#include + +#include "zypp/base/ReferenceCounted.h" +#include "zypp/base/NonCopyable.h" +#include "zypp/base/PtrTypes.h" +#include "zypp/source/SourceInfo.h" +#include "zypp/Pathname.h" + +/////////////////////////////////////////////////////////////////// +namespace zypp +{ ///////////////////////////////////////////////////////////////// + /////////////////////////////////////////////////////////////////// + namespace cache + { ///////////////////////////////////////////////////////////////// + + DEFINE_PTR_TYPE(KnownSourcesCache); + + /////////////////////////////////////////////////////////////////// + // + // CLASS NAME : KnownSourcesCache + // + class KnownSourcesCache : public base::ReferenceCounted, private base::NonCopyable + { + friend std::ostream & operator<<( std::ostream & str, const KnownSourcesCache & obj ); + + public: + /** root path */ + KnownSourcesCache( const Pathname &root_r ); + ~KnownSourcesCache(); + source::SourceInfoList knownSources() const; + void storeSource( const source::SourceInfo &info ); + protected: + + /** Overload to realize stream output. */ + virtual std::ostream & dumpOn( std::ostream & str ) const; + //typedef std::map MediaMap + }; + /////////////////////////////////////////////////////////////////// + + /** \relates KnownSourcesCache Stream output */ + inline std::ostream & operator<<( std::ostream & str, const KnownSourcesCache & obj ) + { return obj.dumpOn( str ); } + + + ///////////////////////////////////////////////////////////////// + } // namespace cache + /////////////////////////////////////////////////////////////////// + ///////////////////////////////////////////////////////////////// +} // namespace zypp +/////////////////////////////////////////////////////////////////// +#endif // ZYPP_SOURCE_KnownSourcesCache_H diff --git a/zypp/cache/Makefile.am b/zypp/cache/Makefile.am new file mode 100644 index 0000000..6627338 --- /dev/null +++ b/zypp/cache/Makefile.am @@ -0,0 +1,25 @@ +SUBDIRS = sqlite3x + +INCLUDES = -DZYPP_BASE_LOGGER_LOGGROUP=\"cache\" + +## ################################################## + +cacheincludedir = $(pkgincludedir)/cache + +cacheinclude_HEADERS = KnownSourcesCache.h + +## ################################################## + +noinst_LTLIBRARIES = lib@PACKAGE@_cache.la + +## ################################################## + +lib@PACKAGE@_cache_la_SOURCES = KnownSourcesCache.cpp + +lib@PACKAGE@_cache_la_LDFLAGS = @LIBZYPP_VERSION_INFO@ + +lib@PACKAGE@_cache_la_LIBADD = sqlite3x/lib@PACKAGE@_sqlite3x.la +## hal/lib@PACKAGE@_cache_hal.la \ +## modalias/lib@PACKAGE@_cache_modalias.la \ +## store/lib@PACKAGE@_cache_store.la + diff --git a/zypp/cache/sqlite3x/Makefile.am b/zypp/cache/sqlite3x/Makefile.am new file mode 100644 index 0000000..5543f97 --- /dev/null +++ b/zypp/cache/sqlite3x/Makefile.am @@ -0,0 +1,20 @@ +SUBDIRS = + +INCLUDES = -DZYPP_BASE_LOGGER_LOGGROUP=\"sqlite3x\" + +## ################################################## + +sqlite3xincludedir = $(pkgincludedir)/sqlite3x + +sqlite3xinclude_HEADERS = sqlite3x.hpp + +## ################################################## + +noinst_LTLIBRARIES = lib@PACKAGE@_sqlite3x.la + +## ################################################## + +lib@PACKAGE@_sqlite3x_la_SOURCES = sqlite3x_command.cpp sqlite3x_connection.cpp \ + sqlite3x_exception.cpp sqlite3x_reader.cpp sqlite3x_transaction.cpp + +lib@PACKAGE@_sqlite3x_la_LDFLAGS = -lsqlite3 @LIBZYPP_VERSION_INFO@ diff --git a/zypp/cache/sqlite3x/sqlite3x.hpp b/zypp/cache/sqlite3x/sqlite3x.hpp new file mode 100644 index 0000000..d0e517e --- /dev/null +++ b/zypp/cache/sqlite3x/sqlite3x.hpp @@ -0,0 +1,183 @@ +/* + 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: phrostbyte $ + $Date: 2005/06/16 20:46:40 $ + $Revision: 1.1 $ +*/ + +#ifndef __SQLITE3X_HPP__ +#define __SQLITE3X_HPP__ + +#include +#include +#include +#include + +namespace sqlite3x +{ + class sqlite3_connection : boost::noncopyable + { + private: + friend class sqlite3_command; + friend class database_error; + + struct sqlite3 *db; + + public: + sqlite3_connection(); + sqlite3_connection(const char *db); + sqlite3_connection(const wchar_t *db); + ~sqlite3_connection(); + + void open(const char *db); + void open(const wchar_t *db); + void close(); + + long long insertid(); + void setbusytimeout(int ms); + + void executenonquery(const char *sql); + void executenonquery(const wchar_t *sql); + void executenonquery(const std::string &sql); + void executenonquery(const std::wstring &sql); + + int executeint(const char *sql); + int executeint(const wchar_t *sql); + int executeint(const std::string &sql); + int executeint(const std::wstring &sql); + + long long executeint64(const char *sql); + long long executeint64(const wchar_t *sql); + long long executeint64(const std::string &sql); + long long executeint64(const std::wstring &sql); + + double executedouble(const char *sql); + double executedouble(const wchar_t *sql); + double executedouble(const std::string &sql); + double executedouble(const std::wstring &sql); + + std::string executestring(const char *sql); + std::string executestring(const wchar_t *sql); + std::string executestring(const std::string &sql); + std::string executestring(const std::wstring &sql); + + std::wstring executestring16(const char *sql); + std::wstring executestring16(const wchar_t *sql); + std::wstring executestring16(const std::string &sql); + std::wstring executestring16(const std::wstring &sql); + + std::string executeblob(const char *sql); + std::string executeblob(const wchar_t *sql); + std::string executeblob(const std::string &sql); + std::string executeblob(const std::wstring &sql); + }; + + class sqlite3_command; + + class database_error : public std::runtime_error { + public: + database_error(const char *msg); + database_error(sqlite3_connection &con); + }; + + class sqlite3_transaction : boost::noncopyable { + private: + sqlite3_connection &con; + bool intrans; + + public: + sqlite3_transaction(sqlite3_connection &con, bool start=true); + ~sqlite3_transaction(); + + void begin(); + void commit(); + void rollback(); + }; + + class sqlite3_reader { + private: + friend class sqlite3_command; + + sqlite3_command *cmd; + + sqlite3_reader(sqlite3_command *cmd); + + public: + sqlite3_reader(); + sqlite3_reader(const sqlite3_reader ©); + ~sqlite3_reader(); + + sqlite3_reader& operator=(const sqlite3_reader ©); + + bool read(); + void reset(); + void close(); + + int getint(int index); + long long getint64(int index); + double getdouble(int index); + std::string getstring(int index); + std::wstring getstring16(int index); + std::string getblob(int index); + + std::string getcolname(int index); + std::wstring getcolname16(int index); + }; + + class sqlite3_command : boost::noncopyable { + private: + friend class sqlite3_reader; + + sqlite3_connection &con; + struct sqlite3_stmt *stmt; + unsigned int refs; + int argc; + + public: + sqlite3_command(sqlite3_connection &con, const char *sql); + sqlite3_command(sqlite3_connection &con, const wchar_t *sql); + sqlite3_command(sqlite3_connection &con, const std::string &sql); + sqlite3_command(sqlite3_connection &con, const std::wstring &sql); + ~sqlite3_command(); + + void bind(int index); + void bind(int index, int data); + void bind(int index, long long data); + void bind(int index, double data); + void bind(int index, const char *data, int datalen); + void bind(int index, const wchar_t *data, int datalen); + void bind(int index, const void *data, int datalen); + void bind(int index, const std::string &data); + void bind(int index, const std::wstring &data); + + sqlite3_reader executereader(); + void executenonquery(); + int executeint(); + long long executeint64(); + double executedouble(); + std::string executestring(); + std::wstring executestring16(); + std::string executeblob(); + }; + +} + +#endif diff --git a/zypp/cache/sqlite3x/sqlite3x_command.cpp b/zypp/cache/sqlite3x/sqlite3x_command.cpp new file mode 100644 index 0000000..2d3748d --- /dev/null +++ b/zypp/cache/sqlite3x/sqlite3x_command.cpp @@ -0,0 +1,156 @@ +/* + 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: phrostbyte $ + $Date: 2005/06/16 20:46:40 $ + $Revision: 1.1 $ +*/ + +#include +#include "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); +} + +} diff --git a/zypp/cache/sqlite3x/sqlite3x_connection.cpp b/zypp/cache/sqlite3x/sqlite3x_connection.cpp new file mode 100644 index 0000000..9143387 --- /dev/null +++ b/zypp/cache/sqlite3x/sqlite3x_connection.cpp @@ -0,0 +1,209 @@ +/* + 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: phrostbyte $ + $Date: 2005/06/16 20:46:40 $ + $Revision: 1.1 $ +*/ + +#include +#include "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(); +} + +} diff --git a/zypp/cache/sqlite3x/sqlite3x_exception.cpp b/zypp/cache/sqlite3x/sqlite3x_exception.cpp new file mode 100644 index 0000000..d56f4aa --- /dev/null +++ b/zypp/cache/sqlite3x/sqlite3x_exception.cpp @@ -0,0 +1,34 @@ +/* + 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: phrostbyte $ + $Date: 2005/06/16 20:46:40 $ + $Revision: 1.1 $ +*/ + +#include +#include "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)) {} + +} diff --git a/zypp/cache/sqlite3x/sqlite3x_reader.cpp b/zypp/cache/sqlite3x/sqlite3x_reader.cpp new file mode 100644 index 0000000..2b3d5f3 --- /dev/null +++ b/zypp/cache/sqlite3x/sqlite3x_reader.cpp @@ -0,0 +1,129 @@ +/* + 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: phrostbyte $ + $Date: 2005/06/16 20:46:40 $ + $Revision: 1.1 $ +*/ + +#include +#include "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); +} + +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); +} + +} diff --git a/zypp/cache/sqlite3x/sqlite3x_transaction.cpp b/zypp/cache/sqlite3x/sqlite3x_transaction.cpp new file mode 100644 index 0000000..f515b96 --- /dev/null +++ b/zypp/cache/sqlite3x/sqlite3x_transaction.cpp @@ -0,0 +1,61 @@ +/* + 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: phrostbyte $ + $Date: 2005/06/16 20:46:40 $ + $Revision: 1.1 $ +*/ + +#include +#include "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; +} + +} -- 2.7.4