--- /dev/null
+/*---------------------------------------------------------------------\
+| ____ _ __ __ ___ |
+| |__ / \ / / . \ . \ |
+| / / \ V /| _/ _/ |
+| / /__ | | | | | | |
+| /_____||_| |_| |_| |
+| |
+\---------------------------------------------------------------------*/
+
+#include "zypp/base/Logger.h"
+#include "zypp/cache/KnownSourcesCache.h"
+#include "zypp/target/store/PersistentStorage.h"
+
+#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 ) : _root(root_r)
+{
+ try
+ {
+ _con.reset( new sqlite3_connection(ZYPP_DB_FILE) );
+ }
+ catch(exception &ex)
+ {
+ ERR << "Exception Occured: " << ex.what() << endl;
+ }
+
+ try
+ {
+ if( ! tablesCreated() )
+ {
+ try
+ {
+ importOldSources();
+ }
+ catch(std::exception &e)
+ {
+ ERR << "Exception Occured: " << e.what() << endl;
+ }
+ }
+ }
+ 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
+ storage::PersistentStorage store;
+ store.init( _root );
+ source::SourceInfoList old_sources = store.storedSources();
+ for ( source::SourceInfoList::const_iterator it = old_sources.begin(); it != old_sources.end(); it++ )
+ {
+ storeSource( *it );
+ }
+}
+
+source::SourceInfoList KnownSourcesCache::knownSources() const
+{
+ source::SourceInfoList sources;
+ try
+ {
+ sqlite3_command cmd( *_con , "select * from sources;");
+ sqlite3_reader reader = cmd.executereader();
+
+ while(reader.read())
+ {
+ source::SourceInfo info;
+ info.setAlias(reader.getstring(1));
+ info.setUrl(reader.getstring(2));
+ info.setEnabled( (reader.getint(4) == 1 ) ? true : false );
+ info.setAutorefresh( (reader.getint(5) == 1 ) ? true : false );
+ info.setType(reader.getstring(6));
+ info.setCacheDir(reader.getstring(7));
+ info.setPath(reader.getstring(8));
+ sources.push_back(info);
+ }
+ }
+ catch(exception &ex)
+ {
+ ERR << "Exception Occured: " << ex.what() << endl;
+ }
+ return sources;
+}
+
+void KnownSourcesCache::storeSource( const source::SourceInfo &info )
+{
+ try
+ {
+ 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 no description
+ cmd.bind(4, info.enabled() ? 1 : 0 );
+ cmd.bind(5, info.autorefresh() ? 1 : 0 );
+ cmd.bind(6, info.type());
+ cmd.bind(7, info.cacheDir().asString());
+ cmd.bind(8, info.path().asString());
+
+ cmd.executenonquery();
+ }
+ catch(exception &ex)
+ {
+ ERR << "Exception Occured: " << ex.what() << endl;
+ }
+}
+
+std::ostream & KnownSourcesCache::dumpOn( std::ostream & str ) const
+{
+ return str;
+}
+
+}
+}
+
--- /dev/null
+/*---------------------------------------------------------------------\
+| ____ _ __ __ ___ |
+| |__ / \ / / . \ . \ |
+| / / \ V /| _/ _/ |
+| / /__ | | | | | | |
+| /_____||_| |_| |_| |
+| |
+\---------------------------------------------------------------------*/
+/** \file zypp/KnownSourcesCache.h
+ *
+*/
+#ifndef ZYPP_KnownSourcesCache_H
+#define ZYPP_KnownSourcesCache_H
+
+#include <iosfwd>
+#include <string>
+
+#include "zypp/base/ReferenceCounted.h"
+#include "zypp/base/NonCopyable.h"
+#include "zypp/base/PtrTypes.h"
+#include "zypp/source/SourceInfo.h"
+#include "zypp/Pathname.h"
+#include "zypp/cache/sqlite3x/sqlite3x.hpp"
+
+///////////////////////////////////////////////////////////////////
+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:
+ 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
+ shared_ptr<sqlite3x::sqlite3_connection> _con;
+ Pathname _root;
+ };
+ ///////////////////////////////////////////////////////////////////
+
+ /** \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
cacheincludedir = $(pkgincludedir)/cache
-cacheinclude_HEADERS = SourceCache.h SourceCacher.h
+cacheinclude_HEADERS = SourceCache.h SourceCacher.h KnownSourcesCache.h Utils.h
## ##################################################
## ##################################################
-lib@PACKAGE@_cache_la_SOURCES = SourceCache.cpp SourceCacher.cpp
+lib@PACKAGE@_cache_la_SOURCES = SourceCache.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/sqlite3x/sqlite3x.hpp"
+#include "zypp/cache/Utils.h"
#define ZYPP_DB_FILE "/var/lib/zypp/zypp.db"
///////////////////////////////////////////////////////////////////
namespace cache
{ /////////////////////////////////////////////////////////////////
-
-static int tribool_to_int( boost::tribool b )
-{
- if (b)
- return 1;
- else if (!b)
- return 0;
- else
- return 2;
-}
-
-static boost::tribool int_to_tribool( int i )
-{
- if (i==1)
- return true;
- else if (i==0)
- return false;
- else
- return boost::indeterminate;
-}
-
-static std::string checksum_to_string( const CheckSum &checksum )
-{
- return checksum.type() + ":" + checksum.checksum();
-}
-
-static CheckSum string_to_checksum( const std::string &checksum )
-{
- std::vector<std::string> words;
- if ( str::split( checksum, std::back_inserter(words), ":" ) != 2 )
- return CheckSum();
-
- return CheckSum( words[0], words[19]);
-}
#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
-SourceCache::SourceCache( const Pathname &root_r )
+SourceCache::SourceCache( const Pathname &root_r, const std::string alias )
{
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(SOURCES_TABLE_SCHEMA);
-
- 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();
+ _con.reset( new sqlite3_connection(ZYPP_DB_FILE) );
}
catch(exception &ex) {
ERR << "Exception Occured: " << ex.what() << endl;
SourceCache::~SourceCache()
{
+ _con->close();
}
-source::SourceInfoList SourceCache::knownSources() const
+void SourceCache::cachePattern( const data::Pattern pattern )
{
- source::SourceInfoList sources;
-
- try {
- sqlite3_connection con(ZYPP_DB_FILE);
-
- {
- sqlite3_command cmd(con, "select * from sources;");
- sqlite3_reader reader = cmd.executereader();
- while(reader.read())
- {
- // alias 0 , type 1, desc 2, url 3, path 4, enabled 5, autorefresh 6, timestamp 7, checksum 8
- source::SourceInfo info;
- info.setAlias(reader.getstring(0));
- info.setType(reader.getstring(1));
- info.setDescription(reader.getstring(2));
- info.setUrl(reader.getstring(3));
- info.setPath(reader.getstring(4));
- info.setEnabled( int_to_tribool(reader.getint(5)) );
- info.setAutorefresh( int_to_tribool( reader.getint(6) ));
- //info.setTimestamp(Date(reader.getstring(7)));
- info.setChecksum(string_to_checksum(reader.getstring(8)));
- sources.push_back(info);
- }
- }
- con.close();
- }
- catch(exception &ex) {
- ERR << "Exception Occured: " << ex.what() << endl;
- }
- return sources;
}
-void SourceCache::storeSource( const source::SourceInfo &info )
+void SourceCache::cacheResolvable( const data::ResObject )
{
- try
- {
- sqlite3_connection con(ZYPP_DB_FILE);
- sqlite3_transaction trans(con);
-
- {
- // alias 0 , type 1, desc 2, url 3, path 4, enabled 5, autorefresh 6, timestamp 7, checksum 8
-
- sqlite3_command cmd(con, "insert into sources ( alias, type, description, url, path, enabled, autorefresh, timestamp, checksum ) values ( ?, ?, ?, ? , ?, ?, ?, ?, ?);");
- cmd.bind(0, info.alias());
- cmd.bind(1, info.type());
- cmd.bind(2, info.description());
- cmd.bind(3, info.url().asCompleteString());
- cmd.bind(4, info.path().asString());
- cmd.bind(5, tribool_to_int(info.enabled()) );
- cmd.bind(6, tribool_to_int(info.autorefresh()) );
- cmd.bind(7, info.timestamp().asString());
- cmd.bind(8, checksum_to_string(info.checksum()) );
-
- 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 & SourceCache::dumpOn( std::ostream & str ) const
{
#include "zypp/base/NonCopyable.h"
#include "zypp/base/PtrTypes.h"
#include "zypp/source/SourceInfo.h"
+#include "zypp/data/ResolvableData.h"
#include "zypp/Pathname.h"
+#include "zypp/cache/sqlite3x/sqlite3x.hpp"
///////////////////////////////////////////////////////////////////
namespace zypp
friend std::ostream & operator<<( std::ostream & str, const SourceCache & obj );
public:
- /** root path */
- SourceCache( const Pathname &root_r );
+ /** Creates a source cache */
+ SourceCache( const Pathname &root_r, const std::string alias );
~SourceCache();
- source::SourceInfoList knownSources() const;
- void storeSource( const source::SourceInfo &info );
+ void cachePattern( const data::Pattern pattern );
protected:
-
+ void cacheResolvable( const data::ResObject );
/** 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;
};
///////////////////////////////////////////////////////////////////
SourceCacher::SourceCacher( const Pathname &root_r )
{
+
}
SourceCacher::~SourceCacher()
IMPL_PTR_TYPE(Selection);
IMPL_PTR_TYPE(Pattern);
-
+/*
+std::ostream& operator<<(std::ostream& out, const ResObject &data)
+{
+ out << "Script Data: " << endl
+ << " name: " << data.name << endl
+ << " edition: " << data.edition << endl
+ << " provides: " << data.provides << endl
+ << " conflicts: " << data.conflicts << endl
+ << " obsoletes: " << data.obsoletes << endl
+ << " freshens: " << data.freshens << endl
+ << " requires: " << data.requires << endl
+ << " recommends:" << endl << data.recommends << endl
+ << " suggests:" << endl << data.suggests << endl
+ << " supplements:" << endl << data.supplements << endl
+ << " enhances:" << endl << data.enhances << endl
+}
+
std::ostream& operator<<(std::ostream& out, const zypp::shared_ptr<AtomBase> data)
{
out << "Atom data" << endl;
std::ostream& operator<<(std::ostream& out, const Script& data)
{
- out << "Script Data: " << endl
- << " name: " << data.name << endl
- << " epoch: " << data.epoch << endl
- << " version: " << data.ver << endl
- << " release: " << data.rel << endl
- << " provides: " << data.provides << endl
- << " conflicts: " << data.conflicts << endl
- << " obsoletes: " << data.obsoletes << endl
- << " freshens: " << data.freshens << endl
- << " requires: " << data.requires << endl
- << " recommends:" << endl << data.recommends << endl
- << " suggests:" << endl << data.suggests << endl
- << " supplements:" << endl << data.supplements << endl
- << " enhances:" << endl << data.enhances << endl
<< " do script: " << data.do_script << endl
<< " undo script: " << data.undo_script << endl
<< " do script location: " << data.do_location << endl
<< " enhances:" << endl << data.enhances << endl
<< " text: " << data.text << endl;
return out;
-}
+}
+*/
} // namespace cache
} // namespace zypp
};
/* Easy output */
- std::ostream& operator<<(std::ostream &out, const Dependency& data);
- std::ostream& operator<<(std::ostream &out, const ResObject& data);
- std::ostream& operator<<(std::ostream &out, const Product& data);
- std::ostream& operator<<(std::ostream &out, const Pattern& data);
- std::ostream& operator<<(std::ostream &out, const Selection& data);
+// std::ostream& operator<<(std::ostream &out, const Dependency& data);
+// std::ostream& operator<<(std::ostream &out, const ResObject& data);
+// std::ostream& operator<<(std::ostream &out, const Product& data);
+// std::ostream& operator<<(std::ostream &out, const Pattern& data);
+// std::ostream& operator<<(std::ostream &out, const Selection& data);
} // namespace data
} // namespace zypp