Table Entity¶
This document describes the creation of the Table entity.
ITableInterface¶
Code Implementation¶
When defining a table entity, we inherit the IRdbTableInterface class. The code implementation of IRdbTableInterface is as follows:
IBeanInterface Introduction¶
For details on IBeanInterface, you can refer to the content of Bean. Here, we briefly explain this concept.
The simplified code is as follows:
When a class inherits IBeanInterface, we need to define the fields used for reflection. Fields are defined using two methods: $BeanFieldDeclare and $BeanField.
$BeanFieldDeclare is used to declare a field, followed by its definition. $BeanField combines the declaration and definition. For example, to define a name field, the following two methods are equivalent and valid.
If you want to initialize a field during definition, you can do so as follows. Both methods generate the same code.
IRdbTableInfo¶
In IRdbTableInterface, the code static const IRdbTableInfo& staticEntityInfo(); converts the entity information into a static member function call to return to the user. Its implementation is as follows:
It inherits from IRdbEntityInfo as follows:
From the above code, we can see that IRdbTableInfo retains a series of original information, which can be used to operate on database tables.
Column Annotations¶
$Column and $ColumnDeclare Macro Annotations¶
These macro annotations are used as follows:
After using the above methods to define fields, a static member constant $field_xxx is automatically defined. For example, the implementation of ColumnDeclare is as follows:
As shown above, the following code is automatically added:
In subsequent queries, we can directly use $field_id to represent the id column, reducing the likelihood of errors.
$SqlType Macro Annotation¶
This annotation is used when generating the table. When generating the table creation statement, the field type is mapped by default. For example, in MySql, std::string is mapped to VARCHAR(100). Users can define custom mappings using this annotation, as shown below:
This maps the name field to TEXT in SQL.
In SQLite, it is translated as follows:
$NotNull¶
This annotation marks the column as non-null in SQL, meaning it cannot have a null value. It is used when generating the table creation statement, typically adding a NOT NULL constraint. The usage is as follows:
In SQLite, it is translated as follows:
$Unique¶
This annotation indicates that the column cannot have duplicate values. It is commonly used for primary keys but can also be used for other non-duplicate columns. The usage is as follows:
In SQLite, it is translated as follows:
$Constraint¶
This is a general-purpose annotation. When creating a database table, the content of $Constraint is appended to the column definition. For example:
In MySQL, it is translated as follows:
Primary Key Annotations¶
In the Table entity, there must be exactly one primary key. The primary key is annotated using $PrimaryKey. If a table does not have a primary key, an error is thrown during runtime.
Currently, the supported primary key data types are strings and numeric types. Other types will result in an error.
$PrimaryKey¶
This macro annotation is used to designate a field as the primary key. The usage is as follows:
The generated SQL is as follows:
$AutoIncrement¶
This macro annotation is used for auto-increment fields. It is applied to numeric fields. If the field is not numeric, an error is thrown. The usage is as follows:
In MySQL, the generated SQL is as follows:
Different databases may generate different SQL, but the code remains valid.
$GenerateValue¶
This macro annotation is used for custom primary key generation. While $AutoIncrement is for numeric fields, $GenerateValue allows users to define their own generation method. For example:
The primary key generation method for the database table is set to uuid. For more details, refer to IRdbValueGeneratorInterface.