HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_SqlDatabase Class Reference

#include <UT_SQL.h>

Public Member Functions

 UT_SqlDatabase (const UT_StringRef &filename, int busy_timeout=0, UT_ErrorCode *ec=nullptr)
 
 UT_SqlDatabase ()
 
 ~UT_SqlDatabase ()
 
 UT_SqlDatabase (const UT_SqlDatabase &)=delete
 
UT_SqlDatabaseoperator= (const UT_SqlDatabase &)=delete
 
bool close (UT_ErrorCode *ec=nullptr)
 
bool open (const UT_StringHolder &filename, UT_ErrorCode *ec=nullptr)
 
NO_DISCARD SYS_FORCE_INLINE bool isValid () const
 
NO_DISCARD bool isReadOnly (const char *db="main", UT_ErrorCode *ec=nullptr) const
 
template<typename... Args>
bool run (UT_ErrorCode *ec, const UT_StringRef &sql, Args &&...args)
 Helper function to run an sql statement with provided typed args. More...
 
int exec (const UT_StringRef &sql, UT_ErrorCode *ec=nullptr) const
 Returns the number of rows modified, inserted or deleted. More...
 
NO_DISCARD int dataVersion (UT_ErrorCode *ec=nullptr) const
 
NO_DISCARD int userVersion (UT_ErrorCode *ec=nullptr) const
 
void setUserVersion (int version, UT_ErrorCode *ec=nullptr) const
 
NO_DISCARD bool tableExists (const UT_StringRef &name, UT_ErrorCode *ec=nullptr) const
 Check if the specified table exists. More...
 
NO_DISCARD bool indexExists (const UT_StringRef &name, UT_ErrorCode *ec=nullptr) const
 
NO_DISCARD bool viewExists (const UT_StringRef &name, UT_ErrorCode *ec=nullptr) const
 
NO_DISCARD bool columnExists (const UT_StringRef &table_name, const UT_StringRef &column_name, UT_ErrorCode *ec=nullptr) const
 
NO_DISCARD UT_StringHolder errorMessage () const
 
NO_DISCARD int errorCode () const
 
NO_DISCARD int extendedErrorCode () const
 
NO_DISCARD UT_StringHolder getSchema (UT_ErrorCode *ec=nullptr) const
 
bool setBusyTimeout (int timeout_ms, UT_ErrorCode *ec=nullptr)
 
bool copyTo (UT_SqlDatabase &destination, UT_ErrorCode *ec=nullptr) const
 
UT_IntrusivePtr
< UT_SqlStatementHandle
cachedStatement (const UT_SqlStatementHandleId &id, const UT_StringRef &sql, UT_ErrorCode *ec=nullptr) const
 
UT_IntrusivePtr
< UT_SqlStatementHandle
findCachedStatement (const UT_SqlStatementHandleId &id) const
 
bool startTransaction (UT_ErrorCode *ec=nullptr)
 
bool endTransaction (bool commit, UT_ErrorCode *ec=nullptr)
 

Protected Member Functions

bool schemaItemExists (const UT_StringRef &type, const UT_StringRef &name, UT_ErrorCode *ec) const
 

Friends

class UT_SqlStatementHandle
 

Detailed Description

Definition at line 282 of file UT_SQL.h.

Constructor & Destructor Documentation

UT_SqlDatabase::UT_SqlDatabase ( const UT_StringRef filename,
int  busy_timeout = 0,
UT_ErrorCode ec = nullptr 
)

Open an sqlite object with the provided filename.

  • See setBusyTimeout() for details about busy_timeout.
  • no_raise will avoid raising an exception if an error occurred
UT_SqlDatabase::UT_SqlDatabase ( )
UT_SqlDatabase::~UT_SqlDatabase ( )
UT_SqlDatabase::UT_SqlDatabase ( const UT_SqlDatabase )
delete

Member Function Documentation

UT_IntrusivePtr<UT_SqlStatementHandle> UT_SqlDatabase::cachedStatement ( const UT_SqlStatementHandleId id,
const UT_StringRef sql,
UT_ErrorCode ec = nullptr 
) const

Get a sql statement that is retrieved from the cache. If the statement is not already cached then its compiled and then cached if the compiled statement is valid.

bool UT_SqlDatabase::close ( UT_ErrorCode ec = nullptr)
NO_DISCARD bool UT_SqlDatabase::columnExists ( const UT_StringRef table_name,
const UT_StringRef column_name,
UT_ErrorCode ec = nullptr 
) const
bool UT_SqlDatabase::copyTo ( UT_SqlDatabase destination,
UT_ErrorCode ec = nullptr 
) const

Copy the contents of this database into the provided destination database. Return true on success and false otherwise. On failure, the error code is set in the destination database.

NO_DISCARD int UT_SqlDatabase::dataVersion ( UT_ErrorCode ec = nullptr) const

Check the database's "data_version" (see https://www.sqlite.org/pragma.html#pragma_data_version)

bool UT_SqlDatabase::endTransaction ( bool  commit,
UT_ErrorCode ec = nullptr 
)
NO_DISCARD int UT_SqlDatabase::errorCode ( ) const
NO_DISCARD UT_StringHolder UT_SqlDatabase::errorMessage ( ) const
int UT_SqlDatabase::exec ( const UT_StringRef sql,
UT_ErrorCode ec = nullptr 
) const

Returns the number of rows modified, inserted or deleted.

NO_DISCARD int UT_SqlDatabase::extendedErrorCode ( ) const
UT_IntrusivePtr<UT_SqlStatementHandle> UT_SqlDatabase::findCachedStatement ( const UT_SqlStatementHandleId id) const

Find an sql handle based on its id. The sql statement must have already been added from cachedStatement(). This method is typically used when a statement has already been compiled and added to the cache but needs to be dynamically looked up some time later.

NO_DISCARD UT_StringHolder UT_SqlDatabase::getSchema ( UT_ErrorCode ec = nullptr) const
NO_DISCARD bool UT_SqlDatabase::indexExists ( const UT_StringRef name,
UT_ErrorCode ec = nullptr 
) const
NO_DISCARD bool UT_SqlDatabase::isReadOnly ( const char *  db = "main",
UT_ErrorCode ec = nullptr 
) const
NO_DISCARD SYS_FORCE_INLINE bool UT_SqlDatabase::isValid ( ) const
inline

Definition at line 301 of file UT_SQL.h.

bool UT_SqlDatabase::open ( const UT_StringHolder filename,
UT_ErrorCode ec = nullptr 
)
UT_SqlDatabase& UT_SqlDatabase::operator= ( const UT_SqlDatabase )
delete
template<typename... Args>
bool UT_SqlDatabase::run ( UT_ErrorCode ec,
const UT_StringRef sql,
Args &&...  args 
)
inline

Helper function to run an sql statement with provided typed args.

Definition at line 309 of file UT_SQL.h.

bool UT_SqlDatabase::schemaItemExists ( const UT_StringRef type,
const UT_StringRef name,
UT_ErrorCode ec 
) const
protected
bool UT_SqlDatabase::setBusyTimeout ( int  timeout_ms,
UT_ErrorCode ec = nullptr 
)

This sets a busy handler that sleeps for a specified amount of time when a table is locked. The handler will sleep multiple times until at least milliseconds of sleeping have accumulated. After the timeout the handler returns 0 which causes the step() to return kSQLITE_BUSY.

void UT_SqlDatabase::setUserVersion ( int  version,
UT_ErrorCode ec = nullptr 
) const
bool UT_SqlDatabase::startTransaction ( UT_ErrorCode ec = nullptr)

These are primarily used by UT_SqlTransaction (but can be used by any client code) and provide an abstraction that resembles nested transations. This is inspired by (and follows the same behaviour as): https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase#beginTransaction()

NO_DISCARD bool UT_SqlDatabase::tableExists ( const UT_StringRef name,
UT_ErrorCode ec = nullptr 
) const

Check if the specified table exists.

NO_DISCARD int UT_SqlDatabase::userVersion ( UT_ErrorCode ec = nullptr) const

Get/set the database's "user_version" (see https://www.sqlite.org/pragma.html#pragma_user_version)

NO_DISCARD bool UT_SqlDatabase::viewExists ( const UT_StringRef name,
UT_ErrorCode ec = nullptr 
) const

Friends And Related Function Documentation

Definition at line 391 of file UT_SQL.h.


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