CernVM-FS  2.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
sqlite::Sql Class Reference

#include <sql.h>

Inheritance diagram for sqlite::Sql:

Public Member Functions

 Sql (sqlite3 *sqlite_db, const std::string &statement)
 
virtual ~Sql ()
 
bool Execute ()
 
bool FetchRow ()
 
std::string DebugResultTable ()
 
bool Reset ()
 
int GetLastError () const
 
std::string GetLastErrorMsg () const
 
bool BindBlob (const int index, const void *value, const unsigned size)
 
bool BindBlobTransient (const int index, const void *value, const unsigned size)
 
bool BindDouble (const int index, const double value)
 
bool BindInt (const int index, const int value)
 
bool BindInt64 (const int index, const sqlite3_int64 value)
 
bool BindNull (const int index)
 
bool BindTextTransient (const int index, const std::string &value)
 
bool BindTextTransient (const int index, const char *value, const int size)
 
bool BindText (const int index, const std::string &value)
 
bool BindText (const int index, const char *value, const int size, void(*dtor)(void *)=SQLITE_STATIC)
 
template<typename T >
bool Bind (const int index, const T &value)
 
int RetrieveType (const int idx_column) const
 
int RetrieveBytes (const int idx_column) const
 
const void * RetrieveBlob (const int idx_column) const
 
double RetrieveDouble (const int idx_column) const
 
int RetrieveInt (const int idx_column) const
 
sqlite3_int64 RetrieveInt64 (const int idx_column) const
 
const unsigned char * RetrieveText (const int idx_column) const
 
std::string RetrieveString (const int idx_column) const
 
template<typename T >
Retrieve (const int index)
 
template<>
bool Bind (const int index, const int &value)
 
template<>
bool Bind (const int index, const unsigned int &value)
 
template<>
bool Bind (const int index, const uint64_t &value)
 
template<>
bool Bind (const int index, const sqlite3_int64 &value)
 
template<>
bool Bind (const int index, const std::string &value)
 
template<>
bool Bind (const int index, const float &value)
 
template<>
bool Bind (const int index, const double &value)
 
template<>
int Retrieve (const int index)
 
template<>
bool Retrieve (const int index)
 
template<>
sqlite3_int64 Retrieve (const int index)
 
template<>
uint64_t Retrieve (const int index)
 
template<>
std::string Retrieve (const int index)
 
template<>
float Retrieve (const int index)
 
template<>
double Retrieve (const int index)
 

Protected Member Functions

 Sql ()
 
bool IsInitialized () const
 
bool Init (const sqlite3 *database, const std::string &statement)
 
void DeferredInit (const sqlite3 *database, const char *statement)
 
bool Successful () const
 

Private Member Functions

bool Init (const char *statement)
 
void LazyInit ()
 

Private Attributes

sqlite3 * database_
 
sqlite3_stmt * statement_
 
const char * query_string_
 
int last_error_code_
 

Detailed Description

Base class for all SQL statement classes. It wraps a single SQL statement and all neccessary calls of the sqlite3 API to deal with this statement.


NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE * NOTE This base class implements a lazy initialization of the SQLite * NOTE prepared statement. Therefore it is strongly discouraged to use * NOTE any sqlite3_***() functions directly in the subclasses. Instead * NOTE one must wrap them in this base class and implement the lazy * NOTE initialization scheme as seen below. * NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE *


Derived classes can decide if their statement should be prepared immediately or on first use (aka lazily). The derived constructor must call Sql::Init() or Sql::DeferredInit() accordingly.

Sql objects created via the public constructor rather than by the constructor of a derived class are prepared immediately by default.

Definition at line 329 of file sql.h.

Constructor & Destructor Documentation

sqlite::Sql::Sql ( sqlite3 *  sqlite_db,
const std::string &  statement 
)

Basic constructor to use this class for a specific statement.

Parameters
databasethe database to use the query on
statementthe statement to prepare

Definition at line 15 of file sql.cc.

Here is the call graph for this function:

sqlite::Sql::~Sql ( )
virtual

Definition at line 26 of file sql.cc.

Here is the call graph for this function:

sqlite::Sql::Sql ( )
inlineprotected

Definition at line 458 of file sql.h.

Member Function Documentation

template<typename T >
bool sqlite::Sql::Bind ( const int  index,
const T &  value 
)
inline

Figures out the type to be bound by template parameter deduction NOTE: For strings this is suboptimal, since it needs to assume that the provided buffer is transient and copy it to be sure.

template<>
bool sqlite::Sql::Bind ( const int  index,
const int &  value 
)
inline

Definition at line 448 of file sql_impl.h.

template<>
bool sqlite::Sql::Bind ( const int  index,
const unsigned int &  value 
)
inline

Definition at line 453 of file sql_impl.h.

template<>
bool sqlite::Sql::Bind ( const int  index,
const uint64_t &  value 
)
inline

Definition at line 458 of file sql_impl.h.

template<>
bool sqlite::Sql::Bind ( const int  index,
const sqlite3_int64 &  value 
)
inline

Definition at line 463 of file sql_impl.h.

template<>
bool sqlite::Sql::Bind ( const int  index,
const std::string &  value 
)
inline

Definition at line 468 of file sql_impl.h.

template<>
bool sqlite::Sql::Bind ( const int  index,
const float &  value 
)
inline

Definition at line 473 of file sql_impl.h.

template<>
bool sqlite::Sql::Bind ( const int  index,
const double &  value 
)
inline

Definition at line 478 of file sql_impl.h.

bool sqlite::Sql::BindBlob ( const int  index,
const void *  value,
const unsigned  size 
)
inline

Definition at line 355 of file sql.h.

Referenced by catalog::SqlCatalog::BindHashBlob().

Here is the call graph for this function:

Here is the caller graph for this function:

bool sqlite::Sql::BindBlobTransient ( const int  index,
const void *  value,
const unsigned  size 
)
inline

Definition at line 362 of file sql.h.

Here is the call graph for this function:

bool sqlite::Sql::BindDouble ( const int  index,
const double  value 
)
inline

Definition at line 371 of file sql.h.

Referenced by swissknife::CommandMigrate::MigrationWorker_217::UpdateCatalogSchema().

Here is the call graph for this function:

Here is the caller graph for this function:

bool sqlite::Sql::BindInt ( const int  index,
const int  value 
)
inline

Definition at line 376 of file sql.h.

Here is the call graph for this function:

bool sqlite::Sql::BindNull ( const int  index)
inline

Definition at line 386 of file sql.h.

Referenced by catalog::SqlCatalog::BindHashBlob().

Here is the call graph for this function:

Here is the caller graph for this function:

bool sqlite::Sql::BindText ( const int  index,
const char *  value,
const int  size,
void(*)(void *)  dtor = SQLITE_STATIC 
)
inline

Definition at line 403 of file sql.h.

Here is the call graph for this function:

bool sqlite::Sql::BindTextTransient ( const int  index,
const std::string &  value 
)
inline
bool sqlite::Sql::BindTextTransient ( const int  index,
const char *  value,
const int  size 
)
inline

Definition at line 395 of file sql.h.

Here is the call graph for this function:

std::string sqlite::Sql::DebugResultTable ( )

Definition at line 69 of file sql.cc.

Here is the call graph for this function:

void sqlite::Sql::DeferredInit ( const sqlite3 *  database,
const char *  statement 
)
protected

Defers the initialization of the prepared statement to the first usage to safe memory and CPU cycles for statements that are defined but never used. Typically this method is used in constructors of derived classes.

Parameters
databasethe sqlite database pointer to be query against
statementthe query string to be prepared for execution

Definition at line 158 of file sql.cc.

Referenced by SqlContainsReference::SqlContainsReference(), SqlCountReferences::SqlCountReferences(), history::SqlCountTags::SqlCountTags(), history::SqlGetHashes::SqlGetHashes(), SqlGetTimestamp::SqlGetTimestamp(), history::SqlInsertBranch::SqlInsertBranch(), history::SqlListBranches::SqlListBranches(), SqlListReferences::SqlListReferences(), history::SqlRecycleBinFlush::SqlRecycleBinFlush(), history::SqlRecycleBinList::SqlRecycleBinList(), SqlRemoveReference::SqlRemoveReference(), and history::SqlRemoveTag::SqlRemoveTag().

Here is the call graph for this function:

Here is the caller graph for this function:

bool sqlite::Sql::Execute ( )

Executes the prepared statement. (this method should be used for modifying statements like DELETE or INSERT)

Returns
true on success otherwise false

Definition at line 42 of file sql.cc.

Referenced by catalog::WritableCatalog::AddFileChunk(), swissknife::CommandMigrate::MigrationWorker_20x::AnalyzeFileLinkcounts(), swissknife::CommandMigrate::ChownMigrationWorker::ApplyPersonaMappings(), swissknife::CommandMigrate::MigrationWorker_20x::AttachOldCatalogDatabase(), swissknife::CommandMigrate::HardlinkRemovalMigrationWorker::BreakUpHardlinks(), catalog::CatalogDatabase::CompactDatabase(), sqlite::Database< DerivedT >::Configure(), catalog::WritableCatalog::CopyToParent(), history::HistoryDatabase::CreateBranchesTable(), ReflogDatabase::CreateEmptyDatabase(), swissknife::FileStatsDatabase::CreateEmptyDatabase(), history::HistoryDatabase::CreateEmptyDatabase(), catalog::CatalogDatabase::CreateEmptyDatabase(), StatisticsDatabase::CreateEmptyDatabase(), sqlite::Database< DerivedT >::CreatePropertiesTable(), history::HistoryDatabase::CreateRecycleBinTable(), history::HistoryDatabase::CreateTagsTable(), swissknife::CommandMigrate::MigrationWorker_20x::DetachOldCatalogDatabase(), swissknife::CommandMigrate::MigrationWorker_20x::FixNestedCatalogTransitionPoints(), catalog::WritableCatalog::IncLinkcount(), catalog::WritableCatalog::InitPreparedStatements(), catalog::WritableCatalog::InsertBindMountpoint(), catalog::CatalogDatabase::InsertInitialValues(), catalog::TreeCountersBase< FieldT >::InsertIntoDatabase(), catalog::WritableCatalog::InsertNestedCatalog(), history::HistoryDatabase::LiveSchemaUpgradeIfNecessary(), StatisticsDatabase::LiveSchemaUpgradeIfNecessary(), catalog::CatalogDatabase::LiveSchemaUpgradeIfNecessary(), swissknife::CommandMigrate::MigrationWorker_20x::MigrateFileMetadata(), swissknife::CommandMigrate::MigrationWorker_20x::MigrateNestedCatalogMountPoints(), swissknife::CommandFileStats::ProcessCatalog(), StatisticsDatabase::Prune(), history::SqliteHistory::PruneBranches(), catalog::WritableCatalog::RemoveBindMountpoint(), swissknife::CommandMigrate::MigrationWorker_20x::RemoveDanglingNestedMountpoints(), catalog::WritableCatalog::RemoveEntry(), catalog::WritableCatalog::RemoveFileChunks(), catalog::WritableCatalog::RemoveNestedCatalog(), swissknife::CommandMigrate::BulkhashRemovalMigrationWorker::RemoveRedundantBulkHashes(), StatisticsDatabase::StoreEntry(), catalog::WritableCatalog::TouchEntry(), swissknife::CommandMigrate::MigrationWorker_217::UpdateCatalogSchema(), catalog::WritableCatalog::UpdateEntry(), catalog::WritableCatalog::UpdateNestedCatalog(), swissknife::CommandMigrate::AbstractMigrationWorker< DerivedT >::UpdateNestedCatalogReferences(), history::HistoryDatabase::UpgradeSchemaRevision_10_1(), history::HistoryDatabase::UpgradeSchemaRevision_10_3(), sqlite::Database< DerivedT >::Vacuum(), and catalog::TreeCountersBase< FieldT >::WriteToDatabase().

Here is the call graph for this function:

Here is the caller graph for this function:

bool sqlite::Sql::FetchRow ( )

Execute the prepared statement or fetch its next row. This method is intended to step through the result set. If it returns false this does not neccessarily mean, that the actual statement execution failed, but that no row was fetched.

Returns
true if a new row was fetched otherwise false

Definition at line 62 of file sql.cc.

Referenced by swissknife::CommandMigrate::MigrationWorker_20x::AnalyzeFileLinkcounts(), swissknife::CommandMigrate::AbstractMigrationWorker< DerivedT >::CollectAndAggregateStatistics(), DebugResultTable(), catalog::Catalog::FindNested(), swissknife::CommandMigrate::MigrationWorker_20x::FixNestedCatalogTransitionPoints(), swissknife::CommandMigrate::MigrationWorker_20x::GenerateCatalogStatistics(), swissknife::CommandMigrate::MigrationWorker_217::GenerateNewStatisticsCounters(), sqlite::Database< DerivedT >::GetFreePageRatio(), catalog::WritableCatalog::GetMaxLinkId(), catalog::Catalog::GetNumEntries(), catalog::Catalog::GetReferencedObjects(), catalog::CatalogDatabase::GetRowIdWasteRatio(), catalog::Catalog::ListingMd5Path(), catalog::Catalog::ListingMd5PathStat(), catalog::Catalog::ListMd5PathChunks(), catalog::Catalog::ListNestedCatalogs(), catalog::Catalog::ListOwnNestedCatalogs(), catalog::Catalog::LookupEntry(), catalog::Catalog::LookupXattrsMd5Path(), catalog::Catalog::OpenDatabase(), swissknife::CommandFileStats::ProcessCatalog(), history::SqliteHistory::PruneBranches(), catalog::TreeCountersBase< FieldT >::ReadFromDatabase(), swissknife::CommandMigrate::MigrationWorker_20x::RemoveDanglingNestedMountpoints(), and swissknife::CommandMigrate::StatsMigrationWorker::RepairStatisticsCounters().

Here is the call graph for this function:

Here is the caller graph for this function:

int sqlite::Sql::GetLastError ( ) const
inline

Definition at line 343 of file sql.h.

Referenced by swissknife::Error(), Execute(), Init(), swissknife::CommandMigrate::MigrationWorker_20x::MigrateFileMetadata(), and catalog::Catalog::OpenDatabase().

Here is the caller graph for this function:

std::string sqlite::Sql::GetLastErrorMsg ( ) const

Returns the english language error description of the last error happened in the context of the sqlite3 database object this statement is registered to. Note: In a multithreaded context it might be unpredictable which the actual last error is.

Returns
english language error description of last error

Definition at line 164 of file sql.cc.

Referenced by swissknife::Error(), and Execute().

Here is the caller graph for this function:

bool sqlite::Sql::Init ( const sqlite3 *  database,
const std::string &  statement 
)
protected

Initializes the prepared statement immediately.

Parameters
databasethe sqlite database pointer to be query against
statementthe query string to be prepared for execution
Returns
true on successful statement preparation

Definition at line 132 of file sql.cc.

Referenced by LazyInit(), Sql(), and catalog::SqlCatalog::SqlCatalog().

Here is the caller graph for this function:

bool sqlite::Sql::Init ( const char *  statement)
private

Definition at line 137 of file sql.cc.

Here is the call graph for this function:

bool sqlite::Sql::IsInitialized ( ) const
inlineprotected

Definition at line 464 of file sql.h.

Referenced by LazyInit().

Here is the caller graph for this function:

void sqlite::Sql::LazyInit ( )
inlineprivate

Definition at line 497 of file sql.h.

Referenced by BindBlob(), BindBlobTransient(), BindDouble(), BindInt(), BindInt64(), BindNull(), BindText(), Execute(), and FetchRow().

Here is the call graph for this function:

Here is the caller graph for this function:

template<typename T >
T sqlite::Sql::Retrieve ( const int  index)
inline
template<>
int sqlite::Sql::Retrieve ( const int  index)
inline

Definition at line 484 of file sql_impl.h.

template<>
bool sqlite::Sql::Retrieve ( const int  index)
inline

Definition at line 489 of file sql_impl.h.

template<>
sqlite3_int64 sqlite::Sql::Retrieve ( const int  index)
inline

Definition at line 494 of file sql_impl.h.

template<>
uint64_t sqlite::Sql::Retrieve ( const int  index)
inline

Definition at line 499 of file sql_impl.h.

template<>
std::string sqlite::Sql::Retrieve ( const int  index)
inline

Definition at line 504 of file sql_impl.h.

template<>
float sqlite::Sql::Retrieve ( const int  index)
inline

Definition at line 509 of file sql_impl.h.

template<>
double sqlite::Sql::Retrieve ( const int  index)
inline

Definition at line 514 of file sql_impl.h.

const void* sqlite::Sql::RetrieveBlob ( const int  idx_column) const
inline

Definition at line 436 of file sql.h.

Referenced by swissknife::CommandFileStats::ProcessCatalog(), and catalog::SqlCatalog::RetrieveHashBlob().

Here is the caller graph for this function:

int sqlite::Sql::RetrieveBytes ( const int  idx_column) const
inline

Determines the number of bytes necessary to store the column's data as a string. This might involve type conversions and depends on which other RetrieveXXX methods were called on the same column index before!

See SQLite documentation for sqlite_column_bytes() for details: https://www.sqlite.org/c3ref/column_blob.html

Definition at line 433 of file sql.h.

Referenced by swissknife::CommandFileStats::ProcessCatalog(), and catalog::SqlCatalog::RetrieveHashBlob().

Here is the caller graph for this function:

double sqlite::Sql::RetrieveDouble ( const int  idx_column) const
inline

Definition at line 439 of file sql.h.

Referenced by DebugResultTable(), and catalog::CatalogDatabase::GetRowIdWasteRatio().

Here is the caller graph for this function:

int sqlite::Sql::RetrieveInt ( const int  idx_column) const
inline

Definition at line 442 of file sql.h.

Referenced by swissknife::CommandFileStats::ProcessCatalog().

Here is the caller graph for this function:

std::string sqlite::Sql::RetrieveString ( const int  idx_column) const
inline

Definition at line 451 of file sql.h.

Referenced by history::SqlListBranches::RetrieveBranch(), SqlListReferences::RetrieveHash(), history::SqlGetHashes::RetrieveHash(), and history::SqlRecycleBinList::RetrieveHash().

Here is the call graph for this function:

Here is the caller graph for this function:

const unsigned char* sqlite::Sql::RetrieveText ( const int  idx_column) const
inline

Definition at line 448 of file sql.h.

Referenced by DebugResultTable(), catalog::SqlCatalog::RetrieveHashHex(), and RetrieveString().

Here is the caller graph for this function:

int sqlite::Sql::RetrieveType ( const int  idx_column) const
inline

Definition at line 421 of file sql.h.

Referenced by history::SqlListBranches::RetrieveBranch().

Here is the caller graph for this function:

bool sqlite::Sql::Successful ( ) const
inlineprotected

Checks the last action for success

Returns
true if last action succeeded otherwise false

Definition at line 489 of file sql.h.

Referenced by BindBlob(), BindBlobTransient(), BindDouble(), BindInt(), BindInt64(), BindNull(), BindText(), Execute(), Init(), Reset(), and ~Sql().

Here is the caller graph for this function:

Member Data Documentation

sqlite3* sqlite::Sql::database_
private

Definition at line 506 of file sql.h.

Referenced by DeferredInit(), GetLastErrorMsg(), Init(), and LazyInit().

int sqlite::Sql::last_error_code_
private
const char* sqlite::Sql::query_string_
private

Definition at line 508 of file sql.h.

Referenced by DeferredInit(), and LazyInit().


The documentation for this class was generated from the following files: