|
LiteSQL
|
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< SchemaItem > | getSchema () const |
| returns database schema. More... | |
| std::vector< SchemaItem > | getCurrentSchema () 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< Backend > | backend |
| backend implementation | |
Friends | |
| class | Updater |
A base class of databases.
Can be used without inheriting own version of Database using query()-method. See Defining Database
| 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
| backendType | backend type ("postgresql","mysql" or "sqlite3") |
| connInfo | connection params, syntax "param=value param=value ..." valid keys: host,user,password,database and port |
| Database::Database | ( | const Database & | op | ) |
opens new connection to same database
| op | opened Database |
deletes rows from single table, deleted rows are selected by Expr if specified
| table | target table |
| e | selection expression |
References query().
Referenced by storeSchemaItem().
|
protected |
queries 'schema' - table and converts results to SchemaItem-objects
References query().
Referenced by needsUpgrade(), and upgrade().
|
inlineprotectedvirtual |
returns database schema.
Referenced by create(), drop(), needsUpgrade(), and upgrade().
| 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
| tables | destination tables for insert operation |
| fields | record of field names per table |
| values | record of values per table |
| sequence | sequence where row id-numbers are pulled |
References backend, litesql::join(), and verbose.
Referenced by litesql::Persistent::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.
| table | destination table |
| r | values |
| fields | field names Values are escaped using escapeSQL - function |
References litesql::escapeSQL(), litesql::join(), and query().
Referenced by storeSchemaItem().
| Records Database::query | ( | const std::string & | query | ) | const |
executes SQL query
| query | query std::string |
References backend, and verbose.
Referenced by litesql::DataSource< T >::count(), create(), delete_(), drop(), getCurrentSchema(), insert(), upgrade(), and upgradeTable().
|
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.
| name | table name |
| oldSchema | current schema of table |
| newSchema | upgraded schema of table |
References begin(), commit(), litesql::join(), and query().
Referenced by upgrade().