LiteSQL
persistent.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_persistent_hpp
8 #define litesql_persistent_hpp
9 
10 #include <functional>
11 #include <map>
12 #include "litesql/relation.hpp"
13 #include "litesql/operations.hpp"
14 
19 namespace litesql {
20 
25 class Persistent {
26 protected:
27  typedef std::map<std::string, std::vector<pair<FieldType, std::string> > > Updates;
29  std::string insert(Record& tables,
30  Records& fieldRecs,
31  Records& values,
32  const std::string& sequence);
33  void update(const Updates& updates);
34  template <class T>
35  void updateField(litesql::Persistent::Updates& updates,
36  const std::string& table,
37  litesql::Field<T> fld) {
38  if (fld.modified()) {
39  updates[table].push_back(make_pair(fld.fieldType(),
40  litesql::convert<const T&, std::string>(fld.value())));
41  fld.setModified(false);
42  }
43  }
44  void prepareUpdate(Updates& updates, const std::string& table);
45  void deleteFromTable(const std::string& table, const std::string& id);
49  const Database *db;
52  bool inDatabase;
56  int oldKey;
57 public:
59  const Database & getDatabase() const {
60  return *db;
61  }
63  bool isInDatabase() const {
64  return inDatabase;
65  }
67  const Persistent & operator=(const Persistent & p);
69  Persistent(const Persistent & p)
70  : db(p.db), inDatabase(p.inDatabase), oldKey(p.oldKey) {}
71  Persistent(const Database & _db, const Record & row)
72  : db(&_db), inDatabase(true), oldKey(0) {
73  if (row.size() > 0)
74  oldKey = atoi(row[0]);
75  }
77  Persistent(const Database & _db)
78  : db(&_db), inDatabase(false), oldKey(0) {}
79  virtual void onDelete() {}
81  virtual ~Persistent() {}
84  static std::vector<std::string> getTablesFromFieldTypes(const std::vector<FieldType> & fdatas);
85 
88 // virtual void getTables(Split & tables) const =0 ;
89 };
90 
91 }
92 #endif
litesql::Persistent::~Persistent
virtual ~Persistent()
empty
Definition: persistent.hpp:81
relation.hpp
litesql::UpdateQuery
a class that helps creating UPDATE-SQL statements.
Definition: updatequery.hpp:18
litesql::Field
holds field value
Definition: field.hpp:75
litesql::Persistent::oldKey
int oldKey
used to keep track of ID changes.
Definition: persistent.hpp:56
litesql::Persistent::getTablesFromFieldTypes
static std::vector< std::string > getTablesFromFieldTypes(const std::vector< FieldType > &fdatas)
selectObjectQuery uses this to list tables used by Persistent
litesql::toString
std::string toString(T a)
returns string representation of passed parameter if it can be written to ostringstream
Definition: string.hpp:20
litesql::RawExpr
used to inject custom expression into WHERE-clause
Definition: expr.hpp:35
persistent.hpp
litesql::Record
SQL data row wrapper.
Definition: types.hpp:20
litesql::Persistent::db
const Database * db
pointer to current Database Persistent is assigned to.
Definition: persistent.hpp:49
litesql::Persistent::isInDatabase
bool isInDatabase() const
returns true if object is in database
Definition: persistent.hpp:63
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
operations.hpp
updatequery.hpp
litesql::Database
A base class of databases.
Definition: database.hpp:33
litesql::atoi
int atoi(const std::string &s)
string version of atoi
litesql::Persistent::inDatabase
bool inDatabase
flag that indicates that object is stored in some database.
Definition: persistent.hpp:52
litesql::Records
std::vector< Record > Records
shortcut
Definition: types.hpp:26
litesql::Persistent::operator=
const Persistent & operator=(const Persistent &p)
copies values and keeps fields unchanged
Definition: persistent.cpp:14
litesql::Persistent::Persistent
Persistent(const Persistent &p)
copies values and registers Field-objects to fields
Definition: persistent.hpp:69
litesql::Persistent::getDatabase
const Database & getDatabase() const
returns reference to Database the object is in
Definition: persistent.hpp:59
litesql::escapeSQL
std::string escapeSQL(const std::string &str)
escapes ' characters so that they do not break SQL statements.
litesql::Persistent
An abstract base class of persistents objects.
Definition: persistent.hpp:25
litesql::Database::query
Records query(const std::string &query) const
executes SQL query
Definition: database.cpp:240
litesql::Persistent::insert
std::string insert(Record &tables, Records &fieldRecs, Records &values, const std::string &sequence)
executed when object is created into database
Definition: persistent.cpp:23
litesql::Persistent::Persistent
Persistent(const Database &_db)
creates empty Persistent
Definition: persistent.hpp:77

SourceForge.net Logo