From: Duncan Mac-Vicar P Date: Mon, 3 Jul 2006 15:02:34 +0000 (+0000) Subject: move initializing code to a separate class, it will be easier for testing X-Git-Tag: BASE-SuSE-SLE-10-SP2-Branch~561 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=8c6e934884b75e4aca43093f4f096fa3ca3ca35d;p=platform%2Fupstream%2Flibzypp.git move initializing code to a separate class, it will be easier for testing --- diff --git a/zypp/cache/KnownSourcesCache.cpp b/zypp/cache/KnownSourcesCache.cpp index fb3036b..6abc1ce 100644 --- a/zypp/cache/KnownSourcesCache.cpp +++ b/zypp/cache/KnownSourcesCache.cpp @@ -9,6 +9,7 @@ #include "zypp/base/Logger.h" #include "zypp/cache/KnownSourcesCache.h" +#include "zypp/cache/SourceCacheInitializer.h" #include "zypp/target/store/PersistentStorage.h" #define ZYPP_DB_FILE "/var/lib/zypp/zypp.db" @@ -27,7 +28,9 @@ KnownSourcesCache::KnownSourcesCache( const Pathname &root_r ) : _root(root_r) { try { - _con.reset( new sqlite3_connection(ZYPP_DB_FILE) ); + SourceCacheInitializer init(_root, ZYPP_DB_FILE); + if (init.justInitialized()) + importOldSources(); } catch(exception &ex) { @@ -36,22 +39,14 @@ KnownSourcesCache::KnownSourcesCache( const Pathname &root_r ) : _root(root_r) try { - if( ! tablesCreated() ) - { - try - { - importOldSources(); - } - catch(std::exception &e) - { - ERR << "Exception Occured: " << e.what() << endl; - } - } + _con.reset( new sqlite3_connection(ZYPP_DB_FILE) ); } catch(exception &ex) { ERR << "Exception Occured: " << ex.what() << endl; } + + } KnownSourcesCache::~KnownSourcesCache() @@ -59,17 +54,6 @@ KnownSourcesCache::~KnownSourcesCache() _con->close(); } -bool KnownSourcesCache::tablesCreated() const -{ - unsigned int count = _con->executeint("select count(*) from sqlite_master where type='table' and name='sources';"); - return ( count > 0 ); -} - -void KnownSourcesCache::createTables() -{ - _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);"); -} - void KnownSourcesCache::importOldSources() { // import old sources diff --git a/zypp/cache/KnownSourcesCache.h b/zypp/cache/KnownSourcesCache.h index f48f71f..08dbadb 100644 --- a/zypp/cache/KnownSourcesCache.h +++ b/zypp/cache/KnownSourcesCache.h @@ -45,10 +45,8 @@ namespace zypp ~KnownSourcesCache(); source::SourceInfoList knownSources() const; void storeSource( const source::SourceInfo &info ); + void importOldSources(); protected: - bool tablesCreated() const; - void createTables(); - void importOldSources(); /** Overload to realize stream output. */ virtual std::ostream & dumpOn( std::ostream & str ) const; //typedef std::map MediaMap diff --git a/zypp/cache/Makefile.am b/zypp/cache/Makefile.am index 177a1ce..5a60eed 100644 --- a/zypp/cache/Makefile.am +++ b/zypp/cache/Makefile.am @@ -6,7 +6,7 @@ INCLUDES = -DZYPP_BASE_LOGGER_LOGGROUP=\"cache\" cacheincludedir = $(pkgincludedir)/cache -cacheinclude_HEADERS = SourceCache.h SourceCacher.h KnownSourcesCache.h Utils.h +cacheinclude_HEADERS = SourceCache.h SourceCacheInitializer.h SourceCacher.h KnownSourcesCache.h Utils.h ## ################################################## @@ -14,7 +14,7 @@ noinst_LTLIBRARIES = lib@PACKAGE@_cache.la ## ################################################## -lib@PACKAGE@_cache_la_SOURCES = SourceCache.cpp SourceCacher.cpp KnownSourcesCache.cpp Utils.cpp +lib@PACKAGE@_cache_la_SOURCES = SourceCache.cpp SourceCacheInitializer.cpp SourceCacher.cpp KnownSourcesCache.cpp Utils.cpp lib@PACKAGE@_cache_la_LDFLAGS = @LIBZYPP_VERSION_INFO@ diff --git a/zypp/cache/SourceCache.cpp b/zypp/cache/SourceCache.cpp index 5b88e6c..bf07261 100644 --- a/zypp/cache/SourceCache.cpp +++ b/zypp/cache/SourceCache.cpp @@ -13,6 +13,7 @@ #include "zypp/base/String.h" #include "zypp/cache/SourceCache.h" #include "zypp/target/store/PersistentStorage.h" +#include "zypp/cache/SourceCacheInitializer.h" #include "zypp/cache/Utils.h" #define ZYPP_DB_FILE "/var/lib/zypp/zypp.db" @@ -27,7 +28,7 @@ namespace zypp namespace cache { ///////////////////////////////////////////////////////////////// -#define SOURCES_TABLE_SCHEMA "create table sources ( alias varchar primary key, type varchar, description varchar, url varchar, path varchar, enabled integer, autorefresh integer, timestamp varchar, checksum varchar);" + // alias 0 , type 1, desc 2, url 3, path 4, enabled 5, autorefresh 6, timestamp 7, checksum 8 @@ -35,6 +36,7 @@ SourceCache::SourceCache( const Pathname &root_r, const std::string alias ) { try { + SourceCacheInitializer init( root_r, ZYPP_DB_FILE); _con.reset( new sqlite3_connection(ZYPP_DB_FILE) ); } catch(exception &ex) { diff --git a/zypp/cache/SourceCacheInitializer.cpp b/zypp/cache/SourceCacheInitializer.cpp new file mode 100644 index 0000000..f3ac170 --- /dev/null +++ b/zypp/cache/SourceCacheInitializer.cpp @@ -0,0 +1,95 @@ +/*---------------------------------------------------------------------\ +| ____ _ __ __ ___ | +| |__ / \ / / . \ . \ | +| / / \ V /| _/ _/ | +| / /__ | | | | | | | +| /_____||_| |_| |_| | +| | +\---------------------------------------------------------------------*/ + +#include + +#include "zypp/base/Logger.h" +#include "zypp/base/String.h" +#include "zypp/cache/SourceCacheInitializer.h" +#include "zypp/target/store/PersistentStorage.h" +#include "zypp/cache/Utils.h" + +#define ZYPP_DB_FILE "/var/lib/zypp/zypp.db" + +using namespace sqlite3x; +using namespace std; + +////////////////////////////////////////////////////////////////// +namespace zypp +{ ///////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////// +namespace cache +{ ///////////////////////////////////////////////////////////////// + +#define SOURCES_TABLE_SCHEMA "create table sources ( alias varchar primary key, type varchar, description varchar, url varchar, path varchar, enabled integer, autorefresh integer, timestamp varchar, checksum varchar);" +// alias 0 , type 1, desc 2, url 3, path 4, enabled 5, autorefresh 6, timestamp 7, checksum 8 + +SourceCacheInitializer::SourceCacheInitializer( const Pathname &root_r, const Pathname &db_file ) + : _root(root_r), _just_initialized(false) +{ + try + { + _con.reset( new sqlite3_connection(db_file.asString().c_str()) ); + } + catch(exception &ex) { + ERR << "Exception Occured: " << ex.what() << endl; + } + + try + { + if( ! tablesCreated() ) + { + createTables(); + _just_initialized = true; + _con->close(); + } + } + catch(exception &ex) + { + ERR << "Exception Occured: " << ex.what() << endl; + } + +} + +bool SourceCacheInitializer::justInitialized() const +{ + return _just_initialized; +} + +SourceCacheInitializer::~SourceCacheInitializer() +{ + +} + +bool SourceCacheInitializer::tablesCreated() const +{ + unsigned int count = _con->executeint("select count(*) from sqlite_master where type='table';"); + return ( count == 1 ); +} + +void SourceCacheInitializer::createTables() +{ + sqlite3_transaction trans(*_con); + + { + _con->executenonquery(SOURCES_TABLE_SCHEMA); + + } + + trans.commit(); +} + +std::ostream & SourceCacheInitializer::dumpOn( std::ostream & str ) const +{ + return str; +} + +} +} + diff --git a/zypp/cache/SourceCacheInitializer.h b/zypp/cache/SourceCacheInitializer.h new file mode 100644 index 0000000..432679e --- /dev/null +++ b/zypp/cache/SourceCacheInitializer.h @@ -0,0 +1,76 @@ +/*---------------------------------------------------------------------\ +| ____ _ __ __ ___ | +| |__ / \ / / . \ . \ | +| / / \ V /| _/ _/ | +| / /__ | | | | | | | +| /_____||_| |_| |_| | +| | +\---------------------------------------------------------------------*/ + +#ifndef ZYPP_SourceCacheInitializer_H +#define ZYPP_SourceCacheInitializer_H + +#include +#include + +#include "zypp/base/ReferenceCounted.h" +#include "zypp/base/NonCopyable.h" +#include "zypp/base/PtrTypes.h" +#include "zypp/Pathname.h" +#include "zypp/cache/sqlite3x/sqlite3x.hpp" + +/////////////////////////////////////////////////////////////////// +namespace zypp +{ ///////////////////////////////////////////////////////////////// + /////////////////////////////////////////////////////////////////// + namespace cache + { ///////////////////////////////////////////////////////////////// + + DEFINE_PTR_TYPE(SourceCacheInitializer); + + /////////////////////////////////////////////////////////////////// + // + // CLASS NAME : SourceCacheInitializer + // + class SourceCacheInitializer : public base::ReferenceCounted, private base::NonCopyable + { + friend std::ostream & operator<<( std::ostream & str, const SourceCacheInitializer & obj ); + + public: + /** + * Tries to initialize the source cache if it was not + * \throws When cant initialize + */ + SourceCacheInitializer( const Pathname &root_r, const Pathname &db_file ); + ~SourceCacheInitializer(); + + /** + * only true when cache was not initialized before + * and was just initialized with success + */ + bool justInitialized() const; + protected: + bool tablesCreated() const; + void createTables(); + /** Overload to realize stream output. */ + virtual std::ostream & dumpOn( std::ostream & str ) const; + //typedef std::map MediaMap + shared_ptr _con; + Pathname _root; + bool _just_initialized; + + }; + /////////////////////////////////////////////////////////////////// + + /** \relates SourceCacheInitializer Stream output */ + inline std::ostream & operator<<( std::ostream & str, const SourceCacheInitializer & obj ) + { return obj.dumpOn( str ); } + + + ///////////////////////////////////////////////////////////////// + } // namespace cache + /////////////////////////////////////////////////////////////////// + ///////////////////////////////////////////////////////////////// +} // namespace zypp +/////////////////////////////////////////////////////////////////// +#endif // ZYPP_SOURCE_SourceCacheInitializer_H