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
Definition: database.hpp:23
Definition: database.hpp:18
used to iterate results of SQL statement, creates objects of type T from retrieved records.
Definition: cursor.hpp:21
intermediate representation of single entity in relational database, table, index or sequence
Definition: database.hpp:49
A base class of databases.
Definition: database.hpp:33
bool needsUpgrade() const
returns true if schema of backend database is different from schema returned by getSchema.
Definition: database.cpp:205
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
void rollback() const
cancels active SQL transaction
Definition: database.hpp:142
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
std::vector< SchemaItem > getCurrentSchema() const
queries 'schema' - table and converts results to SchemaItem-objects
Definition: database.cpp:47
void create() const
creates database based on getSchema
Definition: database.cpp:179
void upgrade() const
tries to upgrade database on disk so that schemas would be compatible
Definition: database.cpp:219
void commit() const
commits SQL transaction
Definition: database.hpp:140
virtual ~Database()
closes database
Definition: database.cpp:176
Database(const std::string &backendType, const std::string &connInfo)
opens connection to backend, throw exception if fails or cannot found a Backend class
void drop() const
converts CREATE-statements (of getSchema) to DROP statements and executes them
Definition: database.cpp:188
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
virtual std::vector< SchemaItem > getSchema() const
returns database schema.
Definition: database.hpp:59
void storeSchemaItem(const SchemaItem &s) const
stores SchemaItem to 'schema' - table (deletes old versions)
Definition: database.cpp:35
std::unique_ptr< Backend > backend
backend implementation
Definition: database.hpp:46
void begin() const
begins SQL transaction
Definition: database.hpp:138
Records query(const std::string &query) const
executes SQL query
Definition: database.cpp:240
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
bool verbose
verbosity, prints queries to cerr if true
Definition: database.hpp:79
A base class for expression in WHERE - clause.
Definition: expr.hpp:18
SQL data row wrapper.
Definition: types.hpp:20
Contains class Cursor.
Contains Expr-class hierarchy and operator overloadings for them.
std::vector< Record > Records
shortcut
Definition: types.hpp:26

SourceForge.net Logo