Source

sbbdep / include / sbbdep / cachedb.hpp

Full commit
/*
--------------Copyright (c) 2010-2013 H a r a l d  A c h i t z---------------
-----------< h a r a l d dot a c h i t z at g m a i l dot c o m >------------
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/


#ifndef SBBDEP_CACHEDB_HPP_
#define SBBDEP_CACHEDB_HPP_



#include <a4sqlt3/database.hpp>




namespace sbbdep {


class CacheDB : public a4sqlt3::Database 
{
public:

  class Transaction{
    a4sqlt3::Database& m_db;
    bool m_commited;

  public:
    Transaction(a4sqlt3::Database& db)
    : m_db(db), m_commited(false)
    {
      m_db.Execute("BEGIN TRANSACTION");
    }
    ~Transaction()
    {
      if(!m_commited) m_db.Execute("ROLLBACK TRANSACTION");
    }

    Transaction(const Transaction&) = delete;
    Transaction(Transaction&&) = delete; //could do this, other commit to true that nothing is executed
    Transaction& operator=(const Transaction&) = delete;
    Transaction& operator=(Transaction&&) = delete;


    void commit()
    {
      m_db.Execute("COMMIT TRANSACTION"); m_commited = true ;
    }
  };



  // ~/sbbdep.cache bzw noch anpassen  
  CacheDB();
  
  CacheDB( const std::string& name );
  
  ~CacheDB() = default;
  
  // don't forget , SQLITE_OPEN_READWRITE no mtx
  bool Open() ;
  
  bool Create();

  // TODO , put the db version / schema check here ...
  
  bool isNew() { return m_isNew; }

  //us major minor combination to see if schema has changed in an way that it needs to be re-created
  void checkVersion( int major, int minor, int patchlevel );


  using StringVec = std::vector<std::string>;

  void updateData(const StringVec& toremove, const StringVec& toinsert);




  int64_t getLatestPkgTimeStamp();
private:
    bool m_isNew;

    void updateLdDirs(const StringVec& lddirs, const StringVec& ldlinkdirs);

    void updateLdDirOnly(const StringVec& lddirs);

    void persistLdSoTime() ;


};

}

#endif /* SBBDEP_CACHEDB_HPP_ */