libcommonc++  0.7
SQLDatabase Class Reference

A class representing a session with an embedded SQL database. More...

#include <SQLDatabase.h++>

Public Member Functions

 SQLDatabase ()
 Construct a new SQLDatabase object. More...
 
virtual ~SQLDatabase ()
 Destructor. More...
 
String getVersion ()
 Get the version of the underlying SQLite database software. More...
 
void open (const String &path, bool create=true, bool readOnly=false)
 Open the database. More...
 
void close ()
 Close the database. More...
 
uint_t executeSQL (const String &sql)
 Execute an SQL statement. More...
 
void interrupt ()
 Interrupt any currently-executing SQL statement. More...
 
bool isAutoCommit ()
 Test if the database is currently in auto-commit mode. More...
 
SQLQueryprepareQuery (const String &sql)
 Create a query for the given SQL statement. More...
 
int64_t getGeneratedKey ()
 Get the generated key created by the last SQL "INSERT" statement. More...
 
uint_t getModifiedRowCount ()
 Get the number of rows modified by the last SQL statement. More...
 
void begin ()
 Begin a transaction. More...
 
void commit ()
 Commit a pending transaction. More...
 
void rollback ()
 Roll back a pending transaction. More...
 

Protected Member Functions

virtual bool onProgress ()
 A callback that's invoked periodically while a SQL statement is being executed. More...
 
virtual void onCommit ()
 A callback that's invoked each time a transaction is committed. More...
 
virtual void onRollback ()
 A callback that's invoked each time a transaction is rolled back. More...
 

Detailed Description

A class representing a session with an embedded SQL database.

The class can be subclassed to provide custom implementations for the three callback methods.

Author
Mark Lindner

Constructor & Destructor Documentation

◆ SQLDatabase()

Construct a new SQLDatabase object.

◆ ~SQLDatabase()

~SQLDatabase ( )
virtual

Destructor.

Closes the database, if it is open.

Member Function Documentation

◆ begin()

void begin ( )

Begin a transaction.

Transactions cannot be nested.

Exceptions
SQLExceptionIf a database error occurs.

◆ close()

void close ( )

Close the database.

Any pending uncommitted transactions will be rolled back. It is a programming error to close the database while SQLQuery objects that were created by that database still exist.

◆ commit()

void commit ( )

Commit a pending transaction.

Exceptions
SQLExceptionIf a database error occurs.
TimeoutExceptionIf a timeout occurs while waiting for database locks. The call should be retried in this case.

◆ executeSQL()

uint_t executeSQL ( const String sql)

Execute an SQL statement.

Parameters
sqlThe statement to execute.
Returns
The number of rows that were affected by the statement.
Exceptions
SQLExceptionIf a database error occurs.
InterruptedExceptionIf the statement was interrupted via a call to interrupt().

◆ getGeneratedKey()

int64_t getGeneratedKey ( )

Get the generated key created by the last SQL "INSERT" statement.

Returns
The generated key.
Exceptions
SQLExceptionIf a database error occurs.

◆ getModifiedRowCount()

uint_t getModifiedRowCount ( )

Get the number of rows modified by the last SQL statement.

◆ getVersion()

String getVersion ( )

Get the version of the underlying SQLite database software.

◆ interrupt()

void interrupt ( )

Interrupt any currently-executing SQL statement.

The corresponding method will throw an InterruptedException.

Exceptions
SQLExceptionIf a database error occurs.

◆ isAutoCommit()

bool isAutoCommit ( )

Test if the database is currently in auto-commit mode.

Auto-commit mode is on by default if a transaction is not currently in progress.

Exceptions
SQLExceptionIf a database error occurs.

◆ onCommit()

void onCommit ( )
protectedvirtual

A callback that's invoked each time a transaction is committed.

The default implementation does nothing.

◆ onProgress()

bool onProgress ( )
protectedvirtual

A callback that's invoked periodically while a SQL statement is being executed.

The default implementation does nothing and returns true.

Returns
true if the in-progress operation should continue, or false if it should be interrupted.

◆ onRollback()

void onRollback ( )
protectedvirtual

A callback that's invoked each time a transaction is rolled back.

The default implementation does nothing.

◆ open()

void open ( const String path,
bool  create = true,
bool  readOnly = false 
)

Open the database.

Parameters
pathThe pathname of the database file to open.
createA flag indicating whether the database should be created if it does not exist.
readOnlyA flag indicating whether the database should be opened read-only.
Exceptions
IOExceptionIf the database file could not be opened.

◆ prepareQuery()

SQLQuery * prepareQuery ( const String sql)

Create a query for the given SQL statement.

Parameters
sqlThe statement.
Returns
The new SQLQuery. This object must be deleted before closing the database.
Exceptions
SQLExceptionIf a database error occurs.

◆ rollback()

void rollback ( )

Roll back a pending transaction.

Exceptions
SQLExceptionIf a database error occurs.
TimeoutExceptionIf a timeout occurs while waiting for database locks. The call should be retried in this case.

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