-
Notifications
You must be signed in to change notification settings - Fork 0
Datatypes
This library implements the basic SQL datatypes as datatype classes that can be used to specify the datatype of a defined column in a model. Some of these basic SQL datatypes include:
- Integer (and all its derivatives)
- Decimal (and all its derivatives)
- Chars (and all its derivatives)
- Binary or Byte Arrays
- Boolean
- Date and Time or DateTime
- Foreign keys.
The above are all implemented as C++ classes with the following structure:
-
Each class representing a datatype has common attributes, namely:
- primary key (
pk) - Specifies if a column should be part of the primary key set or not. - unique constraint(
unique) - Specifies if the values of a column should be unique or not. - not null constraint(
not_null) - Specifies whether a column is nullable of not. - datatype string.(
datatype) - Specifies the datatype of the column.
- primary key (
-
Each class then implements more tailored attributes according to what need to have to fully represent the raw SQL datatypes. This will be explained as we go in-depth into each class.
Note
If a datatype class constructor doesn't have one of the common attributes as one of its arguments, then it means that the datatype doesn't support that attribute to describe it.
IntegerField(std::string datatype, bool pk = false, bool not_null = false, bool unique = false, int check_constraint = 0, std::string check_condition = "");-
datatype: takes the following:smallintintegerbigint
-
check_constraint: specifies the value that the contents of the column in the database are checked against. -
check_condition: specifies the condition to check the values of the column against thecheck_constraint.
e.g. If for a column age, the check_constraint = 18 and check_condition = ">=", then all values for the column
will be checked agains age >= 18.
Note
Some of the attributes are default-initialized meaning if satisfied with their values, one can neglect to explicitly define their values when constructing the columns.
DecimalField(std::string datatype, int max_length, int decimal_places, bool pk = false);-
datatype: can be any of the following:decimalnumericrealdouble precision
-
max_length: specifies the length of the digits to the left of the decimal point e.g. ifmax_length = 4, then 1234.43 is valid. -
decimal_places: specifies the length of the number of digits after the decimal point. ifdecimal_places = 2, then 1234.43 is valid.
Note
For the datatypes real and double precision, max_length and decimal_places are not used even if specified.
An exception will be thrown if either max_length and/or decimal_places are 0 for decimal and numeric.
CharField(std::string datatype, int length = 0, bool not_null = false, bool unique = false, bool pk = false);-
datatype: can be any of the following:charvarchartext
-
length: specifies the max length of the values of the column.
The length attribute does not apply to the text datatype but an exception will be thrown if length is not specified for char and varchar.
BinaryField(bool not_null, bool unique = false, bool pk = false);Here, the datatype field is already specified(bytea).
The only thing to specify here is the not_null attribute.
BoolField(bool not_null = false, bool enable_default = false, bool default_value = false);Datatype is already specified(boolean).
-
enable_default: specifies whether you want the column to have a default value for each inserted row.(defaulted to false). -
default_value: specifies the default value to use for new rows ifenable_defaultis set to true.(defaulted to false).
DateTimeField(std::string datatype, bool enable_default = false, std::string default_value = "", bool pk = false);-
datatype: can be any of the following:datetimetime_wtztimestamptimestamp_wtzinterval
The _wtz suffix means with time zone.
-
enable_default: specifies whether to have default values for the new column values. -
default_value: specifies the default value to be used ifenable_defaultis set to true.
ForeignKey(std::string column_name, std::string referenced_model_name, std::string referenced_column_name,
std::optional<FieldAttr> ref_col_obj=std::nullopt, std::string on_delete="cascade", std::string on_update="cascade");Note
Each model has an added column, that is the model_id, which is an auto-incrementing integer column. This acts as the primary key whether or not you defined
your own keys. This is prefixes with the model name, then suffixed with "_id". This is what is specified for the referenced_column_name if
you want to reference the default primary key or haven't defined any other primary key in the referenced_model.
-
column_name: This specifies the column name that you are actively defining. -
referenced_model_name: This specifies the name of the model whose column is being referenced by the foreign key. -
referenced_column_name: This specifies the name of the column you are referencing. -
ref_col_obj: If referencing the defaultmodel_idcolumn, specify this asstd::nulloptelse, provide an object to the referenced column using the same constructor you used for the creation of that column. -
on_delete: Specifies the action to perform in case the referenced column is deleted e.g. cascade, set null. The default iscascade. -
on_update: Specifies the action to perform in case the referenced column is updated e.g. cascade, set null. The default iscascade.