The base class for a field of a class stored in the database, corresponding to a column in a table. More...
#include <db_field.h>
Public Types | |
enum | Type { TEXT, BINARY } |
Public Member Functions | |
DBFieldBase (Type type, DBClass *owner, std::string name, std::string table_name, bool write_permission) | |
virtual bool | fromBinary (const char *binary, size_t length)=0 |
Sets the value of this field from a binary chunk of data. | |
virtual bool | fromString (const std::string &str)=0 |
Sets the value of this field from a text string. | |
std::string | getName () const |
const DBClass * | getOwner () const |
DBClass * | getOwner () |
bool | getReadFromDatabase () const |
std::string | getSequenceName () const |
std::string | getTableName () const |
Type | getType () const |
bool | getWritePermission () const |
bool | getWriteToDatabase () const |
void | setReadFromDatabase (bool sync) |
void | setReadWrite (bool sync) |
void | setSequenceName (std::string seq) |
void | setWriteToDatabase (bool sync) |
virtual bool | toBinary (const char *&binary, size_t &length) const =0 |
Gets a pointer to the value of this field as a binary chunk of data. | |
virtual bool | toString (std::string &str) const =0 |
Gets the value of this field as a text string. | |
Protected Member Functions | |
DBFieldBase (DBClass *owner, const DBFieldBase *other) | |
Copy constructor is protected, it should only be called by derived classes which also copy the data. | |
Protected Attributes | |
std::string | name_ |
The name of the column that this field is stored in in the database. | |
DBClass * | owner_ |
The class that this field is part of. | |
bool | read_from_database_ |
Marks if this field should be read from the database when its owner is read. | |
std::string | sequence_name_ |
Optional: the name of a database sequence that is used as a default value for this field. | |
std::string | table_name_ |
The name of the table that this field is stored in the database. | |
Type | type_ |
The type of this field, either TEXT or BINARY. | |
bool | write_permission_ |
Marks if this field is allowed to be used to modify the database. | |
bool | write_to_database_ |
Marks if this field should be saved to the database when its owner is saved. | |
Private Member Functions | |
const DBFieldBase & | operator= (const DBFieldBase &rhs) |
Makes the class non-assignable, since deep copies are hard because of the owner_ field. |
The base class for a field of a class stored in the database, corresponding to a column in a table.
A class stored in the database (an instance of DBClass) must store all of its database data as instances of a DBField. A DBField (see the DBField class documentation) can store any type of data, as long as it can be converted to/from string or binary, so that it can be retrieved / stored into the database.
For now, all SQL data types except bytea are retreived / stored via text format. That might change in the future. But for now, the requirement for a C++ data type so that it can be used as a DBField is that is can be serialized to / from string. The only exception is std::vector<char> meant to be used in binary form.
This base class provides just the interface for storing / retrieving fields. This is all the database interface needs to use this field.
Definition at line 72 of file db_field.h.
A field marked as TEXT will be serialized to / from string by calling the toString and fromString functions. A field marked as BINARY will be serialized to / from binary by calling the toBinary and fromBinary functions.
Definition at line 83 of file db_field.h.
database_interface::DBFieldBase::DBFieldBase | ( | DBClass * | owner, | |
const DBFieldBase * | other | |||
) | [inline, protected] |
Copy constructor is protected, it should only be called by derived classes which also copy the data.
Definition at line 106 of file db_field.h.
database_interface::DBFieldBase::DBFieldBase | ( | Type | type, | |
DBClass * | owner, | |||
std::string | name, | |||
std::string | table_name, | |||
bool | write_permission | |||
) | [inline] |
Definition at line 113 of file db_field.h.
virtual bool database_interface::DBFieldBase::fromBinary | ( | const char * | binary, | |
size_t | length | |||
) | [pure virtual] |
Sets the value of this field from a binary chunk of data.
Implemented in database_interface::DBFieldData< T >, database_interface::DBField< std::vector< char > >, database_interface::DBFieldData< std::vector< char > >, database_interface::DBFieldData< double >, database_interface::DBFieldData< std::string >, database_interface::DBFieldData< std::vector< std::string > >, database_interface::DBFieldData< int >, and database_interface::DBFieldData< bool >.
virtual bool database_interface::DBFieldBase::fromString | ( | const std::string & | str | ) | [pure virtual] |
Sets the value of this field from a text string.
Implemented in database_interface::DBFieldData< T >, database_interface::DBField< bool >, database_interface::DBField< std::string >, database_interface::DBField< std::vector< std::string > >, database_interface::DBFieldData< std::vector< char > >, database_interface::DBFieldData< double >, database_interface::DBFieldData< std::string >, database_interface::DBFieldData< std::vector< std::string > >, database_interface::DBFieldData< int >, and database_interface::DBFieldData< bool >.
std::string database_interface::DBFieldBase::getName | ( | ) | const [inline] |
Definition at line 149 of file db_field.h.
const DBClass* database_interface::DBFieldBase::getOwner | ( | ) | const [inline] |
Definition at line 133 of file db_field.h.
DBClass* database_interface::DBFieldBase::getOwner | ( | ) | [inline] |
Definition at line 132 of file db_field.h.
bool database_interface::DBFieldBase::getReadFromDatabase | ( | ) | const [inline] |
Definition at line 135 of file db_field.h.
std::string database_interface::DBFieldBase::getSequenceName | ( | ) | const [inline] |
Definition at line 151 of file db_field.h.
std::string database_interface::DBFieldBase::getTableName | ( | ) | const [inline] |
Definition at line 150 of file db_field.h.
Type database_interface::DBFieldBase::getType | ( | ) | const [inline] |
Definition at line 118 of file db_field.h.
bool database_interface::DBFieldBase::getWritePermission | ( | ) | const [inline] |
Definition at line 141 of file db_field.h.
bool database_interface::DBFieldBase::getWriteToDatabase | ( | ) | const [inline] |
Definition at line 138 of file db_field.h.
const DBFieldBase& database_interface::DBFieldBase::operator= | ( | const DBFieldBase & | rhs | ) | [private] |
Makes the class non-assignable, since deep copies are hard because of the owner_ field.
Use the assignment operator of the DBClass instead, which reasons about this.
void database_interface::DBFieldBase::setReadFromDatabase | ( | bool | sync | ) | [inline] |
Definition at line 136 of file db_field.h.
void database_interface::DBFieldBase::setReadWrite | ( | bool | sync | ) | [inline] |
Definition at line 143 of file db_field.h.
void database_interface::DBFieldBase::setSequenceName | ( | std::string | seq | ) | [inline] |
Definition at line 153 of file db_field.h.
void database_interface::DBFieldBase::setWriteToDatabase | ( | bool | sync | ) | [inline] |
Definition at line 139 of file db_field.h.
virtual bool database_interface::DBFieldBase::toBinary | ( | const char *& | binary, | |
size_t & | length | |||
) | const [pure virtual] |
Gets a pointer to the value of this field as a binary chunk of data.
Field MAINTAINS OWNERSHIP of the binary data; caller is not allowed to modify / delete it.
Implemented in database_interface::DBFieldData< T >, database_interface::DBField< std::vector< char > >, database_interface::DBFieldData< std::vector< char > >, database_interface::DBFieldData< double >, database_interface::DBFieldData< std::string >, database_interface::DBFieldData< std::vector< std::string > >, database_interface::DBFieldData< int >, and database_interface::DBFieldData< bool >.
virtual bool database_interface::DBFieldBase::toString | ( | std::string & | str | ) | const [pure virtual] |
Gets the value of this field as a text string.
Implemented in database_interface::DBFieldData< T >, database_interface::DBField< bool >, database_interface::DBField< std::string >, database_interface::DBFieldData< std::vector< char > >, database_interface::DBFieldData< double >, database_interface::DBFieldData< std::string >, database_interface::DBFieldData< std::vector< std::string > >, database_interface::DBFieldData< int >, and database_interface::DBFieldData< bool >.
std::string database_interface::DBFieldBase::name_ [protected] |
The name of the column that this field is stored in in the database.
Definition at line 99 of file db_field.h.
DBClass* database_interface::DBFieldBase::owner_ [protected] |
The class that this field is part of.
Definition at line 88 of file db_field.h.
bool database_interface::DBFieldBase::read_from_database_ [protected] |
Marks if this field should be read from the database when its owner is read.
Definition at line 95 of file db_field.h.
std::string database_interface::DBFieldBase::sequence_name_ [protected] |
Optional: the name of a database sequence that is used as a default value for this field.
Definition at line 103 of file db_field.h.
std::string database_interface::DBFieldBase::table_name_ [protected] |
The name of the table that this field is stored in the database.
Definition at line 101 of file db_field.h.
Type database_interface::DBFieldBase::type_ [protected] |
The type of this field, either TEXT or BINARY.
Definition at line 86 of file db_field.h.
bool database_interface::DBFieldBase::write_permission_ [protected] |
Marks if this field is allowed to be used to modify the database.
If false, supercedes write_to_database_. Can only be set at construction time. Use this for retrieving information from other tables, but information that you are not allowed to modify through an instance of this class.
Definition at line 93 of file db_field.h.
bool database_interface::DBFieldBase::write_to_database_ [protected] |
Marks if this field should be saved to the database when its owner is saved.
Definition at line 97 of file db_field.h.