LiteSQL
Classes | Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | Friends | List of all members
litesql::Database Class Reference

A base class of databases. More...

#include <database.hpp>

Classes

class  SchemaItem
 intermediate representation of single entity in relational database, table, index or sequence More...
 

Public Member Functions

 Database (const std::string &backendType, const std::string &connInfo)
 opens connection to backend, throw exception if fails or cannot found a Backend class More...
 
 Database (const Database &op)
 opens new connection to same database More...
 
virtual ~Database ()
 closes database
 
void create () const
 creates database based on getSchema
 
void drop () const
 converts CREATE-statements (of getSchema) to DROP statements and executes them
 
bool needsUpgrade () const
 returns true if schema of backend database is different from schema returned by getSchema.
 
void upgrade () const
 tries to upgrade database on disk so that schemas would be compatible
 
Records query (const std::string &query) const
 executes SQL query More...
 
template<class T >
Cursor< T > cursor (const std::string &query) const
 
void insert (const std::string &table, const Record &r, const std::vector< std::string > &fields=std::vector< std::string >()) const
 executes SQL INSERT statement. More...
 
std::string groupInsert (const Record &tables, const Records &fields, const Records &values, const std::string &sequence) const
 executes multiple INSERT-statements and assigns same 'row id' for first field of every record More...
 
void delete_ (const std::string &table, const Expr &e=Expr()) const
 deletes rows from single table, deleted rows are selected by Expr if specified More...
 
void begin () const
 begins SQL transaction
 
void commit () const
 commits SQL transaction
 
void rollback () const
 cancels active SQL transaction
 

Public Attributes

bool verbose
 verbosity, prints queries to cerr if true
 

Protected Member Functions

void storeSchemaItem (const SchemaItem &s) const
 stores SchemaItem to 'schema' - table (deletes old versions)
 
virtual std::vector< SchemaItemgetSchema () const
 returns database schema. More...
 
std::vector< SchemaItemgetCurrentSchema () const
 queries 'schema' - table and converts results to SchemaItem-objects More...
 
void upgradeTable (const std::string &name, const std::string &oldSchema, const std::string &newSchema) const
 adds missing and removes extra fields to/from table (tries to preserve data). More...
 
bool addColumn (const std::string &name, const ColumnDefinition &column_def) const
 

Protected Attributes

std::unique_ptr< Backendbackend
 backend implementation
 

Friends

class Updater
 

Detailed Description

A base class of databases.

Can be used without inheriting own version of Database using query()-method. See Defining Database

Constructor & Destructor Documentation

◆ Database() [1/2]

litesql::Database::Database ( const std::string &  backendType,
const std::string &  connInfo 
)

opens connection to backend, throw exception if fails or cannot found a Backend class

Parameters
backendTypebackend type ("postgresql","mysql" or "sqlite3")
connInfoconnection params, syntax "param=value param=value ..." valid keys: host,user,password,database and port

◆ Database() [2/2]

Database::Database ( const Database op)

opens new connection to same database

Parameters
opopened Database

Member Function Documentation

◆ delete_()

void Database::delete_ ( const std::string &  table,
const Expr e = Expr() 
) const

deletes rows from single table, deleted rows are selected by Expr if specified

Parameters
tabletarget table
eselection expression

References query().

Referenced by storeSchemaItem().

◆ getCurrentSchema()

vector< Database::SchemaItem > Database::getCurrentSchema ( ) const
protected

queries 'schema' - table and converts results to SchemaItem-objects

Returns
SchemaItem-objects

References query().

Referenced by needsUpgrade(), and upgrade().

◆ getSchema()

virtual std::vector<SchemaItem> litesql::Database::getSchema ( ) const
inlineprotectedvirtual

returns database schema.

Returns
SchemaItem-objects

Referenced by create(), drop(), needsUpgrade(), and upgrade().

◆ groupInsert()

string Database::groupInsert ( const Record tables,
const Records fields,
const Records values,
const std::string &  sequence 
) const

executes multiple INSERT-statements and assigns same 'row id' for first field of every record

Parameters
tablesdestination tables for insert operation
fieldsrecord of field names per table
valuesrecord of values per table
sequencesequence where row id-numbers are pulled
Returns
new row id

References backend, litesql::join(), and verbose.

Referenced by litesql::Persistent::insert().

◆ insert()

void Database::insert ( const std::string &  table,
const Record r,
const std::vector< std::string > &  fields = std::vector<std::string>() 
) const

executes SQL INSERT statement.

Parameters
tabledestination table
rvalues
fieldsfield names Values are escaped using escapeSQL - function

References litesql::escapeSQL(), litesql::join(), and query().

Referenced by storeSchemaItem().

◆ query()

Records Database::query ( const std::string &  query) const

executes SQL query

Parameters
queryquery std::string
Returns
result set

References backend, and verbose.

Referenced by litesql::DataSource< T >::count(), create(), delete_(), drop(), getCurrentSchema(), insert(), upgrade(), and upgradeTable().

◆ upgradeTable()

void Database::upgradeTable ( const std::string &  name,
const std::string &  oldSchema,
const std::string &  newSchema 
) const
protected

adds missing and removes extra fields to/from table (tries to preserve data).

copies data to a temporary table and then back to upgraded table because all backends do not support ALTER TABLE ADD ... - SQL statements.

Parameters
nametable name
oldSchemacurrent schema of table
newSchemaupgraded schema of table

References begin(), commit(), litesql::join(), and query().

Referenced by upgrade().


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

SourceForge.net Logo