This is the 8th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021
Following on from the previous article, “A Study of code automatic generators in The Rule – table Query”, we continue to study the code generation logic in the rule system.
Sql queries that import tables
Click on “Code Generation” in the menu bar and click on the “Import” button in the right pane. In the code Automatic Generator research – Table Query section of The article, we have gone all the way to query the database information_schema.tables to query all the tables in the database.
Our next step is, “Check the my_user table and click OK.” Schematic diagram of operation path and view interface request through F12 debugging, as shown in the figure below.
The requested interface is/tool/gen/importTable, we by the idea of retrieving backend interface, found its ruoyi – generator/com. Ruoyi. The generator/controller/GenController, the diagram below:
The source code of the Controller is as follows :(we supplement the acquisition data of each value through debugging)
/** * import table structure (save) */
@PreAuthorize("@ss.hasPermi('tool:gen:import')")
@log (title = "code generation ", businessType = businessType.IMPORT)
@PostMapping("/importTable")
public AjaxResult importTableSave(String tables)
{
// Through debugging, the format of string tables is: "my_user,table_name_1,table_name_2...."
String[] tableNames = Convert.toStrArray(tables);
// tableNames: ["my_user", "table_name_1", "table_name_2"]
// Query table information
List<GenTable> tableList = genTableService.selectDbTableListByNames(tableNames);
/ / below
genTableService.importGenTable(tableList);
return AjaxResult.success();
}
Copy the code
Let’s look at the SQL that queries the table. By searching in XML, it executes SQL as follows:
SELECT
table_name,
table_comment,
create_time,
update_time
FROM
information_schema. TABLES
WHERE
table_name NOT LIKE 'qrtz_%'
AND table_name NOT LIKE 'gen_%'
AND table_schema = (SELECT DATABASE())
AND table_name IN ('my_user')
Copy the code
Use this query to retrieve the basic information for the my_user table.
So genTableService. ImportGenTable (tableList); What does this sentence do? Let’s keep going.
Import table import
By locating importGenTable, we query its execution method, and we add the parameter values during debugging
/** * import table structure **@paramTableList List of imported tables */
@Override
@Transactional
public void importGenTable(List<GenTable> tableList)
{
TableList: [{my_user information}]
// admin
String operName = SecurityUtils.getUsername();
try
{
for (GenTable table : tableList)
{
String tableName = table.getTableName(); // tableName: my_user
GenUtils.initTable(table, operName);
int row = genTableMapper.insertGenTable(table);
if (row > 0)
{
// Save column information
List<GenTableColumn> genTableColumns = genTableColumnMapper.selectDbTableColumnsByName(tableName);
for(GenTableColumn column : genTableColumns) { GenUtils.initColumnField(column, table); genTableColumnMapper.insertGenTableColumn(column); }}}}catch (Exception e)
{
throw new ServiceException("Import failed:"+ e.getMessage()); }}Copy the code
There is a key data structure: GenTable, which is located in the Domain and corresponds to the table gen_table in the database. The field screenshot is as follows:
Similarly, another table gen_table_column and its corresponding domain class is GenTableColumn, which saves the column information of the generated database table. Its field structure is shown in the following screenshot: The table is associated with a row in the table Gen_table through the field table_id.
What SQL is used to query the columns of a database table?
Upon checking, THE SQL retrieved from the XML is:
SELECT
column_name,
(
CASE
WHEN (
is_nullable = 'no' && column_key ! = 'PRI'
) THEN
'1'
ELSE
NULL
END
) AS is_required,
(
CASE
WHEN column_key = 'PRI' THEN
'1'
ELSE
'0'
END
) AS is_pk,
ordinal_position AS sort,
column_comment,
(
CASE
WHEN extra = 'auto_increment' THEN
'1'
ELSE
'0'
END
) AS is_increment,
column_type
FROM
information_schema. COLUMNS
WHERE
table_schema = (SELECT DATABASE())
AND table_name = 'my_user'
ORDER BY
ordinal_position
Copy the code
Query all COLUMNS of the specified table my_user by querying information_schema. COLUMNS and constrainting table_name and the database.
conclusion
Through the import operation, my_user is converted to a few rows of data in gen_table and gen_table_column for subsequent code generation.