LiteSQL
database.hpp
Go to the documentation of this file.
1 /* LiteSQL
2  *
3  * The list of contributors at http://litesql.sf.net/
4  *
5  * See LICENSE for copyright information. */
6 
7 #ifndef litesql_database_hpp
8 #define litesql_database_hpp
9 
13 #include "litesql/expr.hpp"
14 #include "litesql/cursor.hpp"
15 
16 namespace litesql {
17 
19 public:
20  std::string name;
21  std::string type;
22 
23  class EqualName {
24  std::string m_name;
25  public:
26  EqualName(const std::string& name) : m_name(name) {};
27  bool operator()(ColumnDefinition d) { return d.name==m_name;};
28  };
29 };
30 
33 class Database {
34  friend class Updater;
35 private:
37  std::string backendType;
39  std::string connInfo;
41  Database &operator=(const Database &op)=delete;
43  void openDatabase() noexcept(false);
44 protected:
46  std::unique_ptr<Backend> backend;
49  class SchemaItem {
50  public:
51  std::string name, type, sql;
52  SchemaItem(const std::string& n, const std::string& t, const std::string& s)
53  : name(n), type(t), sql(s) {}
54  };
56  void storeSchemaItem(const SchemaItem& s) const;
59  virtual std::vector<SchemaItem> getSchema() const {
60  return std::vector<SchemaItem>();
61  }
64  std::vector<SchemaItem> getCurrentSchema() const;
73  void upgradeTable(const std::string& name, const std::string& oldSchema, const std::string& newSchema) const;
74 
75  bool addColumn(const std::string & name,const ColumnDefinition & column_def) const;
76 
77 public:
79  bool verbose;
86  Database(const std::string& backendType, const std::string& connInfo);
89  Database(const Database &op);
91  virtual ~Database();
93  void create() const;
96  void drop() const;
99  bool needsUpgrade() const;
101  void upgrade() const;
105  Records query(const std::string &query) const;
106  template <class T>
107  Cursor<T> cursor(const std::string& query) const {
108  if (verbose)
109  std::cerr << query << std::endl;
110  return Cursor<T>(*this, backend->cursor(query));
111  }
112 
119  void insert(const std::string &table, const Record &r,
120  const std::vector<std::string>& fields=std::vector<std::string>()) const;
128  std::string groupInsert(const Record& tables,
129  const Records& fields,
130  const Records& values,
131  const std::string& sequence) const;
136  void delete_(const std::string &table, const Expr& e= Expr()) const;
138  void begin() const { backend->begin(); }
140  void commit() const { backend->commit(); }
142  void rollback() const { backend->rollback(); }
143 };
144 
145 }
146 
147 #endif
litesql::Database::delete_
void delete_(const std::string &table, const Expr &e=Expr()) const
deletes rows from single table, deleted rows are selected by Expr if specified
Definition: database.cpp:277
litesql::Database::~Database
virtual ~Database()
closes database
Definition: database.cpp:176
litesql::Database::needsUpgrade
bool needsUpgrade() const
returns true if schema of backend database is different from schema returned by getSchema.
Definition: database.cpp:205
litesql::replace
std::string replace(const std::string &s, const std::string &what, const std::string &with)
returns a copy string 's' where 'what' is replaced with 'with'
litesql::Except
base class for exceptions
Definition: except.hpp:18
litesql::Database::upgradeTable
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).
Definition: database.cpp:94
litesql::Database::begin
void begin() const
begins SQL transaction
Definition: database.hpp:138
cursor.hpp
litesql::SelectQuery
a class that helps creating SELECT-SQL statements.
Definition: selectquery.hpp:20
except.hpp
database.hpp
litesql::Database::storeSchemaItem
void storeSchemaItem(const SchemaItem &s) const
stores SchemaItem to 'schema' - table (deletes old versions)
Definition: database.cpp:35
litesql::RawExpr
used to inject custom expression into WHERE-clause
Definition: expr.hpp:35
litesql::Database::insert
void insert(const std::string &table, const Record &r, const std::vector< std::string > &fields=std::vector< std::string >()) const
executes SQL INSERT statement.
Definition: database.cpp:247
litesql::Database::verbose
bool verbose
verbosity, prints queries to cerr if true
Definition: database.hpp:79
litesql::Database::commit
void commit() const
commits SQL transaction
Definition: database.hpp:140
litesql::Database::rollback
void rollback() const
cancels active SQL transaction
Definition: database.hpp:142
litesql::Record
SQL data row wrapper.
Definition: types.hpp:20
litesql::Database::SchemaItem
intermediate representation of single entity in relational database, table, index or sequence
Definition: database.hpp:49
litesql::split
std::vector< std::string > split(const std::string &s, const std::string &delim=" ")
from string.
litesql::Database::groupInsert
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
Definition: database.cpp:261
selectquery.hpp
litesql::join
std::string join(const std::vector< std::string > &strings, const std::string &delim=" ")
returns strings joined with delimiter
expr.hpp
litesql::Database::getSchema
virtual std::vector< SchemaItem > getSchema() const
returns database schema.
Definition: database.hpp:59
litesql::Database
A base class of databases.
Definition: database.hpp:33
litesql::Database::getCurrentSchema
std::vector< SchemaItem > getCurrentSchema() const
queries 'schema' - table and converts results to SchemaItem-objects
Definition: database.cpp:47
litesql::Records
std::vector< Record > Records
shortcut
Definition: types.hpp:26
litesql::Database::upgrade
void upgrade() const
tries to upgrade database on disk so that schemas would be compatible
Definition: database.cpp:219
litesql::Backend::getBackend
static std::unique_ptr< Backend > getBackend(const std::string &type, const std::string &connInfo) noexcept(false)
returns a backend according to Backendtype in type, parameters are specific to backend and are separa...
Definition: backend.cpp:201
litesql::ColumnDefinition::EqualName
Definition: database.hpp:23
litesql::Cursor
used to iterate results of SQL statement, creates objects of type T from retrieved records.
Definition: cursor.hpp:21
litesql::Database::backend
std::unique_ptr< Backend > backend
backend implementation
Definition: database.hpp:46
litesql::Expr
A base class for expression in WHERE - clause.
Definition: expr.hpp:18
litesql::escapeSQL
std::string escapeSQL(const std::string &str)
escapes ' characters so that they do not break SQL statements.
litesql::Database::query
Records query(const std::string &query) const
executes SQL query
Definition: database.cpp:240
litesql::ColumnDefinition
Definition: database.hpp:18
litesql::Database::drop
void drop() const
converts CREATE-statements (of getSchema) to DROP statements and executes them
Definition: database.cpp:188
litesql::Database::create
void create() const
creates database based on getSchema
Definition: database.cpp:179
litesql::DatabaseError
exception thrown when database cannot be accessed
Definition: except.hpp:38
litesql::Database::Database
Database(const std::string &backendType, const std::string &connInfo)
opens connection to backend, throw exception if fails or cannot found a Backend class

SourceForge.net Logo