#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"
{
try
{
- _con.reset( new sqlite3_connection(ZYPP_DB_FILE) );
+ SourceCacheInitializer init(_root, ZYPP_DB_FILE);
+ if (init.justInitialized())
+ importOldSources();
}
catch(exception &ex)
{
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()
_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
~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<media::MediaNr, media::MediaAccessId> MediaMap
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
## ##################################################
## ##################################################
-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@
#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"
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
{
try
{
+ SourceCacheInitializer init( root_r, ZYPP_DB_FILE);
_con.reset( new sqlite3_connection(ZYPP_DB_FILE) );
}
catch(exception &ex) {
--- /dev/null
+/*---------------------------------------------------------------------\
+| ____ _ __ __ ___ |
+| |__ / \ / / . \ . \ |
+| / / \ V /| _/ _/ |
+| / /__ | | | | | | |
+| /_____||_| |_| |_| |
+| |
+\---------------------------------------------------------------------*/
+
+#include <vector>
+
+#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;
+}
+
+}
+}
+
--- /dev/null
+/*---------------------------------------------------------------------\
+| ____ _ __ __ ___ |
+| |__ / \ / / . \ . \ |
+| / / \ V /| _/ _/ |
+| / /__ | | | | | | |
+| /_____||_| |_| |_| |
+| |
+\---------------------------------------------------------------------*/
+
+#ifndef ZYPP_SourceCacheInitializer_H
+#define ZYPP_SourceCacheInitializer_H
+
+#include <iosfwd>
+#include <string>
+
+#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<media::MediaNr, media::MediaAccessId> MediaMap
+ shared_ptr<sqlite3x::sqlite3_connection> _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