TreeFrogFramework  2.8
tsqlobject.h
Go to the documentation of this file.
1 #pragma once
2 #include <QDateTime>
3 #include <QObject>
4 #include <QSqlError>
5 #include <QSqlRecord>
6 #include <QStringList>
7 #include <QVariantMap>
8 #include <QSqlDatabase>
9 #include <TGlobal>
10 #include <TModelObject>
11 
12 
13 class T_CORE_EXPORT TSqlObject : public TModelObject, public QSqlRecord {
14  Q_OBJECT
15 public:
16  TSqlObject();
17  TSqlObject(const TSqlObject &other);
18  TSqlObject &operator=(const TSqlObject &other);
19  virtual ~TSqlObject() { }
20 
21  virtual QString tableName() const;
22  virtual int primaryKeyIndex() const { return -1; }
23  virtual int autoValueIndex() const { return -1; }
24  virtual int databaseId() const { return 0; }
25  void setRecord(const QSqlRecord &record, const QSqlError &error);
26  bool create() override;
27  bool update() override;
28  bool save() override;
29  bool remove() override;
30  bool reload();
31  bool isNull() const override { return QSqlRecord::isEmpty(); }
32  bool isNew() const { return QSqlRecord::isEmpty(); }
33  bool isModified() const;
34  void clear() override { QSqlRecord::clear(); }
35  QSqlError error() const { return sqlError; }
36 
37 protected:
38  void syncToSqlRecord();
39  void syncToObject();
40  QSqlDatabase &getDatabase();
41 
42  QSqlError sqlError;
43 
44 private:
45  QSqlDatabase _database;
46 };
The TModelObject class provides an abstract base for model objects.
Definition: tmodelobject.h:9
virtual bool save()=0
virtual bool remove()=0
virtual bool create()=0
virtual bool update()=0
The TSqlObject class is the base class of ORM objects.
Definition: tsqlobject.h:13
virtual int autoValueIndex() const
Returns the position of the auto-generated value field on the table.
Definition: tsqlobject.h:23
bool isNull() const override
Returns true if there is no database record associated with the object; otherwise returns false.
Definition: tsqlobject.h:31
virtual int databaseId() const
Returns the database ID.
Definition: tsqlobject.h:24
QSqlError sqlError
Definition: tsqlobject.h:42
bool isNew() const
Returns true if it is a new object, otherwise returns false.
Definition: tsqlobject.h:32
void clear() override
Clears the contents of the object.
Definition: tsqlobject.h:34
QSqlError error() const
Returns a QSqlError object which contains information about the last error that occurred on the datab...
Definition: tsqlobject.h:35
virtual int primaryKeyIndex() const
Returns the position of the primary key field on the table.
Definition: tsqlobject.h:22
virtual ~TSqlObject()
Definition: tsqlobject.h:19
#define T_CORE_EXPORT
Definition: tdeclexport.h:28