View
*: The query performance of views may be poor, especially if the view queries involve complex joins or aggregate operations. In such cases, ensure the database performance is sufficient to avoid performance issues caused by frequent view queries. Users need to consider these scenarios and use tools like index to improve performance.
Definition of a View¶
IRdbViewInterface¶
Similar to IRdbTableInterface, we abstract out IRdbViewInterface for defining views. The implementation code for IRdbViewInterface is as follows:
It is a CRTP base class. The first template parameter T is the class name. The second parameter enabled is related to type registration and defaults to true. Users inherit from this class to implement their own view types. Let's define a simple view:
This completes the implementation of our simplest view, but it has no fields. Below, we will explain how to add fields.
Macros¶
Similar to Tables, adding fields requires using macro annotations. The macros for Views are defined in IRdbViewPreprocessor.h. Its source code is simple, as shown below:
$ViewField / $Column¶
Here, we alias $Column to $ViewField. Users can use either macro to define fields. Let's define our fields:
$ViewFieldDeclare / $ColumnDeclare¶
$ViewFieldDeclare is an alias for $ColumnDeclare. For its usage, refer to the Table section. We continue to define our fields:
The reason for mixing different styles here is to demonstrate usage. In actual development, it is recommended to use only one method.
$AsView¶
The $AsView macro is used to give the current view an alias. If this macro is not used, the default view name is the class name. For example:
In line 6, we added the macro. Therefore, the current view maps to the database's person_view view, not the PersonView view. If this line is commented out, it maps to the PersonView view.
$CreateViewSql¶
This macro is used to provide the SQL statement for creating the view. The statement should be enclosed in double quotes when used. For example:
This completes the definition of a view.
For more details on creating views, refer to the relevant content of IRdbViewModelInterface.