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
A base class of databases.
Definition: database.hpp:33
holds field value
Definition: field.hpp:75
An abstract base class of persistents objects.
Definition: persistent.hpp:25
const Persistent & operator=(const Persistent &p)
copies values and keeps fields unchanged
Definition: persistent.cpp:14
bool inDatabase
flag that indicates that object is stored in some database.
Definition: persistent.hpp:52
virtual ~Persistent()
empty
Definition: persistent.hpp:81
bool isInDatabase() const
returns true if object is in database
Definition: persistent.hpp:63
static std::vector< std::string > getTablesFromFieldTypes(const std::vector< FieldType > &fdatas)
selectObjectQuery uses this to list tables used by Persistent
const Database * db
pointer to current Database Persistent is assigned to.
Definition: persistent.hpp:49
Persistent(const Database &_db)
creates empty Persistent
Definition: persistent.hpp:77
int oldKey
used to keep track of ID changes.
Definition: persistent.hpp:56
std::string insert(Record &tables, Records &fieldRecs, Records &values, const std::string &sequence)
executed when object is created into database
Definition: persistent.cpp:23
Persistent(const Persistent &p)
copies values and registers Field-objects to fields
Definition: persistent.hpp:69
const Database & getDatabase() const
returns reference to Database the object is in
Definition: persistent.hpp:59
SQL data row wrapper.
Definition: types.hpp:20
select<T>, intersect<T>, union_<T>, except<T>
See Using Relations.
int atoi(const std::string &s)
string version of atoi
std::vector< Record > Records
shortcut
Definition: types.hpp:26

SourceForge.net Logo