Q
Quinlan Morake
Good day,
I have the following code and am uncertain as to how it should best done / thought of. I'm creating a "data access layer", and am aware that there arec++ libraries for this, but am just doing it for the practice. I would like to have a dbField class, that holds the various column info, that is the name, type and data. A dbClass would then hold a list of dbFields, mapping to the columns available in the table. The dbField will thus have differentdata depending on that of the database column, i.e. integer / varchar / bit etc.
My implementation thinking is as follows
enum eDbFieldType
{
Varchar, Integer, Boolean
};
template <class T>
class dbField
{
private:
// Name of column in the database
QString fieldName;
// Column datatype
eDbFieldType dtColumn;
// Data the field contains
T data;
public:
void setData(T);
T getData();
};
class DbClass
{
private:
map<QString, dbField<> > mDbFields;
....
After loading the data from the database into the dbField, I may have to doother things to it depending on its type, for example to base64 decode/encode, etc when getData() is called, and then return the data.
I figure I could use a void* for data, is that what is generally done in this type of situation? I've read that templates are preferable to void* but is that the case here and can they be used? Or should I be thinking differently and do something different? I'm ideally trying to avoid casting.
I have the following code and am uncertain as to how it should best done / thought of. I'm creating a "data access layer", and am aware that there arec++ libraries for this, but am just doing it for the practice. I would like to have a dbField class, that holds the various column info, that is the name, type and data. A dbClass would then hold a list of dbFields, mapping to the columns available in the table. The dbField will thus have differentdata depending on that of the database column, i.e. integer / varchar / bit etc.
My implementation thinking is as follows
enum eDbFieldType
{
Varchar, Integer, Boolean
};
template <class T>
class dbField
{
private:
// Name of column in the database
QString fieldName;
// Column datatype
eDbFieldType dtColumn;
// Data the field contains
T data;
public:
void setData(T);
T getData();
};
class DbClass
{
private:
map<QString, dbField<> > mDbFields;
....
After loading the data from the database into the dbField, I may have to doother things to it depending on its type, for example to base64 decode/encode, etc when getData() is called, and then return the data.
I figure I could use a void* for data, is that what is generally done in this type of situation? I've read that templates are preferable to void* but is that the case here and can they be used? Or should I be thinking differently and do something different? I'm ideally trying to avoid casting.