MLIB
Loading...
Searching...
No Matches
mlib::Database Class Reference

Wrapper for database connection handle. More...

#include <sqlitepp.h>

Public Types

enum class  openflags {
  readonly = SQLITE_OPEN_READONLY , readwrite = SQLITE_OPEN_READWRITE , create = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE , uri = SQLITE_OPEN_URI ,
  nomutex = SQLITE_OPEN_NOMUTEX , fullmutex = SQLITE_OPEN_FULLMUTEX , sharedcache = SQLITE_OPEN_SHAREDCACHE , privatecache = SQLITE_OPEN_PRIVATECACHE ,
  memory = SQLITE_OPEN_MEMORY | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE , nofollow = SQLITE_OPEN_NOFOLLOW , ext_result = SQLITE_OPEN_EXRESCODE
}
 Flags for database opening mode. More...
 

Public Member Functions

 Database ()
 Default constructor.
 
 Database (const std::string &name, openflags flags=openflags::create)
 Open a database specified by name.
 
Databasecopy (Database &src)
 Copy database content.
 
bool connected ()
 Check if database is opened or not.
 
bool is_readonly ()
 Return true if database connection is read-only.
 
 operator sqlite3 * ()
 Return handle of database connection.
 
sqlite3 * handle () const
 Return handle of database connection.
 
sqlite3_int64 last_rowid ()
 Return rowid of last successful insert.
 
sqlite3_int64 changes ()
 Return number of records changed by last query.
 
sqlite3_int64 total_changes ()
 Return total number of changes since database was opened.
 
erc open (const std::string &name, openflags flags=openflags::create)
 Open database connection.
 
erc close ()
 Close database connection.
 
erc exec (const std::string &sql)
 Execute multiple SQL sentences.
 
checked< Querymake_query (const std::string &sql)
 Return a Query object containing a prepared statement with the given SQL text.
 
checked< Querymake_query_multiple (std::string &sql)
 Return a Query object containing the first prepared statement of the given SQL text.
 
std::string filename (const std::string &schema="main") const
 Return filename of a database connection.
 
std::string schema (int n) const
 Return schema name for a database connection.
 
int extended_error ()
 Return extended result code.
 
erc flush ()
 Flush.
 

Static Public Member Functions

static void Errors (mlib::errfac &fac)
 Set error facility for all SQLITEPP errors.
 
static const errfacErrors ()
 Return error facility used by SQLITEPP.
 

Friends

class Query
 

Detailed Description

Wrapper for database connection handle.

The Database class is a wrapper around the sqlite3 handle returned by sqlite_open_v2 function.

Member Enumeration Documentation

◆ openflags

enum class mlib::Database::openflags
strong

Flags for database opening mode.

Enumerator
readonly 

Read-only access.

readwrite 

Read/write access on existing database.

create 

Read/write/create access.

uri 

filename is interpreted as a URI

nomutex 

database connection opens in multi-threaded mode

fullmutex 

database connection opens in the serialized mode

sharedcache 

database connection is eligible to use shared cache mode

privatecache 

database connection does not participate in shared cache mode

memory 

database opened in memory

nofollow 

filename cannot be a symbolic link

ext_result 

enables extended result codes

Constructor & Destructor Documentation

◆ Database() [1/2]

mlib::Database::Database ( )

Default constructor.

The default constructor is used to create an object that is not yet connected to a database.

It has to be connected using the open() function.

◆ Database() [2/2]

mlib::Database::Database ( const std::string &  name,
openflags  flags = openflags::create 
)

Open a database specified by name.

Parameters
namedatabase name
flagsopen flags

Member Function Documentation

◆ copy()

Database & mlib::Database::copy ( Database src)

Copy database content.

Perform a copy operation using the SQLITE Backup API.

Both databases should be opened.

◆ filename()

string mlib::Database::filename ( const std::string &  schema = "main") const

Return filename of a database connection.

Return an empty string if database is not opened or there is no database connection with that name or if the database is a temporary or in-memory database.

Otherwise returns the filename of the database that was attached using an SQL ATTACH statement.

◆ is_readonly()

bool mlib::Database::is_readonly ( )

Return true if database connection is read-only.

Returns FALSE only for connected read-write databases.

If database is not connected or it is read-only, returns TRUE.

◆ make_query()

checked< Query > mlib::Database::make_query ( const std::string &  sql)

Return a Query object containing a prepared statement with the given SQL text.

This function compiles only the first statement in the sql string.

The function returns a checked<Query> object that can be tested.

◆ make_query_multiple()

checked< Query > mlib::Database::make_query_multiple ( std::string &  sql)

Return a Query object containing the first prepared statement of the given SQL text.

This function compiles only the first statement in the sql string.

It updates sql string with what's left after the end of the query.

◆ open()

erc mlib::Database::open ( const std::string &  name,
openflags  flags = openflags::create 
)

Open database connection.

If the object is associated with another connection, the previous connection is closed and a new one is opened.

Parameters
nameDatabase name
flagsCombination of openflags flags

◆ schema()

std::string mlib::Database::schema ( int  n) const

Return schema name for a database connection.

Returns
"main" if n==0 (the schema name for the main database connection)
"temp" if n==1 (schema name for temporary tables)
schema names for other ATTACHED databases if n>1
Note
this is a wrapper for the sqlite3_db_namefunction

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