The base class for all C++ classes that can be stored in the database. More...
#include <db_class.h>
Public Member Functions | |
DBClass () | |
DBFieldBase * | getField (size_t i) |
const DBFieldBase * | getField (size_t i) const |
DBFieldBase * | getField (std::string name) |
const DBFieldBase * | getField (std::string name) const |
bool | getForeignKey (std::string table, const DBFieldBase *&key) const |
Returns the name of the foreign key column in a given table that references our primary key. | |
bool | getForeignKey (std::string table, DBFieldBase *&key) |
size_t | getNumFields () const |
DBFieldBase * | getPrimaryKeyField () |
const DBFieldBase * | getPrimaryKeyField () const |
void | setAllFieldsReadFromDatabase (bool sync) |
void | setAllFieldsWriteToDatabase (bool sync) |
Protected Attributes | |
std::vector< DBFieldBase * > | fields_ |
The addresses of all the other fields. | |
std::map< std::string, DBFieldBase * > | foreign_keys_ |
List of foreign keys in OTHER tables that reference our PRIMARY KEY. | |
DBFieldBase * | primary_key_field_ |
The address of the field that acts as a primary key. | |
Private Member Functions | |
DBClass & | operator= (const DBClass &rhs) |
Makes the class non-assignable. |
The base class for all C++ classes that can be stored in the database.
A DBClass is a collection of fields.
One of the fields is marked as a primary key, which is the only unique identifier for this class. The primary key field is expected to reference the primary key column in the main table where this class is stored in the database.
All the other fields are expected to be stored either in the same table as the primary key, or in other tables that reference our primary key in a foreign key column.
To use this class, any data members that you want to save / load from database must be declared as DBFields. Then, IN THE CONSTRUCTOR, you must tell this interface which of them is the primary key, and store the rest in the fields_ vector. After this, all database functionality will be automagically done for you.
Also in the contructor you can set which fields are read from / written to the database by default, and which should be only when you specifically ask for them
For an example, see the DatabaseOriginalModel implementation.
Definition at line 64 of file db_class.h.
database_interface::DBClass::DBClass | ( | ) | [inline] |
Definition at line 86 of file db_class.h.
DBFieldBase* database_interface::DBClass::getField | ( | size_t | i | ) | [inline] |
Definition at line 90 of file db_class.h.
const DBFieldBase* database_interface::DBClass::getField | ( | size_t | i | ) | const [inline] |
Definition at line 91 of file db_class.h.
DBFieldBase* database_interface::DBClass::getField | ( | std::string | name | ) | [inline] |
Definition at line 93 of file db_class.h.
const DBFieldBase* database_interface::DBClass::getField | ( | std::string | name | ) | const [inline] |
Definition at line 102 of file db_class.h.
bool database_interface::DBClass::getForeignKey | ( | std::string | table, |
const DBFieldBase *& | key | ||
) | const [inline] |
Returns the name of the foreign key column in a given table that references our primary key.
The list of foreign keys must be inserted here IN THE CONSTRUCTOR, if you have fields that live in other tables than the primary key.
Definition at line 111 of file db_class.h.
bool database_interface::DBClass::getForeignKey | ( | std::string | table, |
DBFieldBase *& | key | ||
) | [inline] |
Definition at line 120 of file db_class.h.
size_t database_interface::DBClass::getNumFields | ( | ) | const [inline] |
Definition at line 88 of file db_class.h.
DBFieldBase* database_interface::DBClass::getPrimaryKeyField | ( | ) | [inline] |
Definition at line 104 of file db_class.h.
const DBFieldBase* database_interface::DBClass::getPrimaryKeyField | ( | ) | const [inline] |
Definition at line 105 of file db_class.h.
Makes the class non-assignable.
Prevents the use of instances of DBClass in std::vectors since deep copies are extremely hard because of the vector of pointers to DBFieldBase, which should have pointers to its own members. Use boost::ptr_vector instead for passing around containers of DBClass.
void database_interface::DBClass::setAllFieldsReadFromDatabase | ( | bool | sync | ) | [inline] |
Definition at line 125 of file db_class.h.
void database_interface::DBClass::setAllFieldsWriteToDatabase | ( | bool | sync | ) | [inline] |
Definition at line 134 of file db_class.h.
std::vector<DBFieldBase*> database_interface::DBClass::fields_ [protected] |
The addresses of all the other fields.
Definition at line 79 of file db_class.h.
std::map<std::string, DBFieldBase*> database_interface::DBClass::foreign_keys_ [protected] |
List of foreign keys in OTHER tables that reference our PRIMARY KEY.
Foreign keys are expected to have the *same name* in both tables they join
Definition at line 83 of file db_class.h.
The address of the field that acts as a primary key.
Definition at line 76 of file db_class.h.