Extensible multi-type database structure document generation method
Through the combination of factory mode and template engine, dynamically adapting to multiple database types is solved, and the ease of use and scalability of database structure document generation in the existing technology is realized, real-time update of database structure document and multi-type adaptation of database structure document is realized. , improves development efficiency.
Patent Information
- Application Number
- CN202411453824.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-17
- Publication Date
- 2025-01-07
AI Technical Summary
When facing multiple types of databases, existing database structure document generation methods require manual writing of multiple sets of metadata acquisition code, resulting in low ease of use and scalability, and the inability to achieve real-time updates and effectiveness, resulting in large workloads for developers. And the document is not real-time.
The factory model is used to dynamically create metadata acquisition classes, and the template engine and Java reflection mechanism are used to automatically adapt and generate database structure documents according to the data source type, reducing the need for manual encoding, and real-time generation of multi-type database structure documents.
It greatly reduces the workload of developers, ensures the real-time and effectiveness of database structure documents, improves work efficiency, and does not need to adjust code when the data source type increases, automatically adapts to different types of databases, and supports generation of multiple document formats.
Smart Images

Figure CN119271673A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of database information technology, and specifically to a method for generating scalable multi-type database structure documents. The method implements a method for generating different types of data source structure documents through configurable data source types, targeting multi-type data source adaptation and database table structure parsing technology. Background Art
[0002] A database schema document is a formal document that details the database structure, relationships, and other related design details. The primary purpose of this document is to provide the development team with clear guidance on how the database should be constructed and used, ensuring that all stakeholders have a shared understanding of the database's design and implementation.
[0003] When developing large-scale projects, different types of databases are usually involved, such as Oracle, MySQL, TiDB, DM, etc. There are thousands of databases and database tables involved, and there are many people involved in the development. Each person may define and generate tables in each type of database. This may lead to developers not understanding the relevant information of the database tables built by other members, and the subsequent database table content changes, and the database structure document cannot be modified in time. There are certain discrepancies between the database table and the document, and other people involved in the development cannot update the database table structure in time, which may cause problems caused by the change in the database table structure. Summary of the Invention
[0004] The existing database structure document generation method can only manually add related database metadata acquisition methods based on the database type. For example, when MySQL and Oracle databases are used simultaneously in a project, two sets of data source metadata acquisition and parsing methods will be written, and then the method will be selected based on the data source type. As the number of data source types increases, the relevant data source metadata acquisition code needs to be manually added, which makes it very difficult to use and scalable.
[0005] In order to solve the above problems, the present invention provides an extensible method for generating multi-type database structure documents. This method can dynamically create metadata acquisition classes based on the data source type using a factory pattern, thereby realizing an extensible method for generating database structure documents, thereby achieving the real-time and effectiveness of various types of database structure documents, which can greatly reduce the workload of developers, ensure the real-time and effectiveness of documents, and thus improve work efficiency.
[0006] 1. Obtaining data source connection information and instances First, obtain the database connection Url, username, password and other information, and use the obtained URL to determine the data source type, which includes DAMO, MySQL, TIDB, and KOSPI. Adapt the corresponding data source driver according to the data source type, then initialize the database connection pool configuration HikariConfig, and then generate the DataSource database connection through the SpringBoot default connection pool HikariDataSource; construct the generated file information, including the file output path file type, file name, and template storage path, where file types include hypertext html, text word, and online html preview text; then build a custom EngineConfig instance; generate the Configuration document configuration, set the EngineConfig instance, custom version number, description, and database connection information parameters; 2. Dynamically obtain data source metadata based on data source information Customize the JdbcUtils tool class. This tool class includes the close function to close the data source connection and the getDbType function. It determines the current data source type based on the incoming URL, and then dynamically creates and returns the xxxDataBaseQueryImpl implementation class based on the corresponding data source type using the factory mode. First, obtain the data source information from the Configuration instance and the URL of the data source connection. Then, use the getDbType function in the custom JdbcUtils tool class to obtain the current data source type and the class of its corresponding data source type xxxDataBaseQueryImpl implementation class. If the currently integrated DataBaseQueryImpl does not contain an implementation class for the current data source type, the corresponding DataBaseQueryImpl implementation class is automatically created dynamically through the factory pattern. Then, obtain the newly created class constructor and use Java reflection to create a new DatabaseQuery object instance. Create a custom entity DataModel object. The entity contains database data source information, tables data table collection, title title, version version, description description and other information. Create a custom DatabaseQueryFactory factory class, which contains a private DataSource object. The return value is the getDataSource operation of DataSource. The private DataSource object is returned directly. The return value is the newInstance operation of DatabaseQuery. First, according to the getDataSource operation, the DataSource object will be reached, and then the URL of the database connection metadata will be obtained. Then, according to the URL, the getDbType operation in JdbcUtils is used to obtain the class of DataBaseQuery. Then, a new instance object is created through the Constructor object, and the current DataSource is passed as an actual parameter to the constructor of the new object. Finally, the newly created DatabaseQuery object is returned, which is a subclass of DatabaseQuery, and has parameterized and non-parameterized construction operations. A DatabaseQuery interface object is obtained according to the newInstance operation of the custom DatabaseQueryFactory. At this time, the implementation class of DatabaseQuery is created as xxxDataBaseQueryImpl of the current data source type through the factory design pattern. Each operation in xxxDataBaseQueryImpl will obtain and uniformly encapsulate the database table metadata according to different data source types, and then call the getDataBase operation in the custom interface DatabaseQuery to obtain the database object, the getTables operation to obtain the table collection, the getTableColumns operation to obtain the column collection, and the getPrimaryKeys operation to obtain the primaryKey collection. Then, through recursive encapsulation, the table collection, column collection, and primaryKey collection are encapsulated into a tree structure. Finally, the database object and the encapsulated table collection are placed together in the created DataModel object. 3. Generate Documentation Get the template engine by creating and configuring a Configuration object, setting the configuration version, setting the description information of the Configuration object, specifying the data source, specifying a specific table name to generate the configuration, and then using this configuration object to perform the document generation task. Create an instance of the DocumentationExecute class, pass in the previously constructed Configuration object as a parameter of the constructor, and then call the execute operation to perform the document generation task. When calling the execute operation, a DataModelProcess object is created. This object receives the Configuration as a parameter of its constructor. Then, the process operation of this object is called, which processes the data model according to the configuration and returns a DataModel object. Create an EngineFactory object, pass in the Configuration's engineConfig as a parameter, and obtain a Class object through the reflection mechanism of engineConfig.getProduceType(). This Class object represents a specific TemplateEngine subclass. Get the type information of the template engine implementation from the current engineConfig configuration, and then find and obtain a constructor from the Class object obtained in the previous step through the Class object's getConstructor(EngineConfig.class). This constructor receives a parameter of type EngineConfig. The purpose of this step is The purpose is to find a constructor that can accept EngineConfig as a parameter so that TemplateEngine can be instantiated later. The previously obtained constructor is called to create a new TemplateEngine instance. Then, type casting (TemplateEngine) is used to convert the created object to the TemplateEngine type and return it. A new TemplateEngine instance is generated. TemplateEngine is used to process template files. The produce operation of the TemplateEngine object is called, and two parameters are passed in: dataModel and docName. Here, dataModel is the data model processed in the previous step, and docName is the name of the output document. The produce operation uses the provided data model and document name to generate the final document output, write it to File, and write it to the specified path.
[0007] The present invention may be applicable to big data-related scenarios in the future. Big data application services often involve a large number of databases, and data is transmitted between different types of databases. During the transmission process, the upstream database structure may be adjusted according to demand, but the upstream data source type is uncertain. Therefore, the present invention can automatically respond and adapt when the data source type is unclear, without adjusting the code, and output the database structure document to the downstream database service provider, so that adjustments can be made quickly.
[0008] Compared with the existing technology, the advantage of the present invention is that there is no need to write multiple sets of codes for obtaining metadata of different data sources. The method can automatically create the acquisition class and method of the relevant data source metadata. Under normal circumstances, it is necessary to manually write the database query implementation class corresponding to the data source type according to the data source type, and when the data source types increase, as the data source types increase, multiple sets of corresponding codes for obtaining data source information need to be manually written. After each data source is added, the service needs to be republished, which will result in very poor code scalability. This method can dynamically create codes of different data source types according to the data source type, and there is no need to manually add codes according to the increase in data source types.
[0009] To summarize, users only need to add data source connection information and test the connection successfully. When viewing or generating database structure documents, they only need to provide data source related information and the file type to be generated, HTML or Word. This method can also configure the document template to be exported according to their own database tables and automatically generate documents to avoid the incompleteness and errors of handwritten documents. After the database table information is modified, there is no need to manually maintain the database structure document. The document can be updated in real time to ensure the validity and real-time nature of the document, thereby improving work efficiency. BRIEF DESCRIPTION OF THE DRAWINGS
[0010] Figure 1 To generate a flowchart for database structure documentation; Figure 2 This is a schematic diagram of a specific implementation case for the generated HTML version of the document; Figure 3 This is a schematic diagram of a specific implementation case for the generated Word version document. DETAILED DESCRIPTION
[0011] The present invention aims to provide an extensible method for generating multi-type database structure documents. The method is developed based on a template engine and a Java reflection mechanism to obtain data source information, and then dynamically creates an implementation class of DataBaseQuery through a factory design pattern to obtain metadata information of the currently connected data source, thereby achieving data source information acquisition and template data binding processing, and outputting it to a specific specified type of file type.
[0012] Specifically, the method of the present invention includes the following key steps: Get data source connection information and instance This method first obtains the database connection Url, username, password and other information, and uses the obtained URL to determine the data source type, such as DAMO, MySQL, tidb, kingbase, etc., and adapts the corresponding data source driver according to the data source type. Then initialize the database connection pool configuration HikariConfig, and then generate the DataSource database connection through the SpringBoot default connection pool HikariDataSource; build the generated file information, including the file output path file type (html, word, online preview html type), file name, template storage path, etc., and then build a custom EngineConfig instance through the builder mode; generate the Configuration document configuration, set the EngineConfig instance, custom version number, description, database connection information and other parameters.
[0013] Dynamically obtain data source metadata based on data source information Customize the JdbcUtils tool class. This tool class includes the close function to close the data source connection and the getDbType function. It determines the current data source type based on the incoming URL, and then dynamically creates and returns the xxxDataBaseQueryImpl implementation class based on the corresponding data source type using the factory mode.
[0014] First, obtain the data source information in the Configuration instance and the URL information of the data source connection. Then, obtain the current data source type and its corresponding data source type xxxDataBaseQueryImpl according to the getDbType operation in the custom JdbcUtils tool class. xxxDataBaseQueryImpl inherits the custom interface DataBaseQuery. The operations in the interface are the class of the getDataBase, getTables, getTableColumns, and getPrimaryKeys implementation classes. If the currently integrated DataBaseQueryImpl does not contain the implementation class of the current data source type, the corresponding DataBaseQueryImpl implementation class is automatically created dynamically through the factory mode, and then the newly created class constructor is obtained to create a new DatabaseQuery object instance using the Java reflection mechanism.
[0015] Create a custom entity DataModel object. The entity contains database data source information, tables data table collection, title title, version version, description description and other information. Create a custom DatabaseQueryFactory factory class, which contains a private DataSource object. The return value is the getDataSource operation of DataSource. The private DataSource object is returned directly. The return value is the newInstance operation of DatabaseQuery. First, according to the getDataSource operation, the DataSource object will be reached, and then the URL of the database connection metadata will be obtained. Then, according to the URL, the getDbType operation in JdbcUtils is used to obtain the DataBaseQuery class). Then, a new instance object is created through the Constructor object, and the current DataSource is passed as an actual parameter to the constructor of the new object. Finally, the newly created DatabaseQuery object is returned), which is a subclass of DatabaseQuery, as well as parameterized and non-parameterized construction operations. A DatabaseQuery interface object is obtained according to the newInstance operation of the custom DatabaseQueryFactory. At this time, the implementation class of DatabaseQuery is created as xxxDataBaseQueryImpl of the current data source type through the factory design pattern. The various operations in xxxDataBaseQueryImpl will obtain and uniformly encapsulate the database table metadata according to different data source types, and then call the getDataBase operation in the custom interface DatabaseQuery to obtain the database object, the getTables operation to obtain the table collection, the getTableColumns operation to obtain the column collection, and the getPrimaryKeys operation to obtain the primaryKey collection. Then, through recursive encapsulation, the table collection, column collection, and primaryKey collection are encapsulated into a tree structure. Finally, the database object and the encapsulated table collection are placed together in the created DataModel object.
[0016] Generate Documentation Obtain the template engine. Create and configure a Configuration object, set the configuration version, set the Configuration object's description, specify the data source, and specify a specific table name to generate the configuration. Then, use this configuration object to perform the document generation task. Create an instance of the DocumentationExecute class, passing the previously constructed Configuration object as a constructor parameter. Then, call the execute operation to execute the document generation task. This call creates a DataModelProcess object, which receives the Configuration as a constructor parameter. Then the process operation of this object is called, which will process the data model according to the configuration, return a DataModel object, create an EngineFactory object, pass in the Configuration's engineConfig as a parameter, and obtain a Class object of engineConfig.getProduceType() through the reflection mechanism. This Class object represents a specific TemplateEngine subclass, obtains the type information of the template engine implementation from the current engineConfig configuration, and then searches and obtains a constructor (Constructor) from the Class object obtained in the previous step through the Class object's getConstructor(EngineConfig.class). This constructor receives a parameter of type EngineConfig. The purpose of this step is to find a constructor that can accept EngineConfig as a parameter so that the TemplateEngine can be instantiated later and the previously obtained constructor can be called to create a new TemplateEngine instance. Then use forced type conversion (TemplateEngine) to convert the created object to the TemplateEngine type and return it, and generate a new TemplateEngine instance. TemplateEngine is used to process template files. Call the produce operation of the TemplateEngine object and pass in two parameters: dataModel and docName. Here, dataModel is the data model processed in the previous step, and docName is the name of the output document. The produce operation uses the provided data model and document name to generate the final document output, write it to File, and write it to the specified path. This method is integrated and used in the Hebei Ecological Environment Big Data Center project. On the data source configuration page, you can select the data source for online preview of the database structure document and export HTML and word type documents.like. Figure 2 、 Figure 3 The following example is a specific implementation case of this method. The technical solution includes the following steps: a. Write related tool classes; the data source JDBC tool class JdbcUtils includes operations for obtaining the DataBaseQueryImpl implementation class and closing the data source connection. Generally, tool classes automatically match existing data source drivers based on the current data source type. If the corresponding data source driver is not configured, this type of data source cannot be adapted for use. The tool class JdbcUtils in this method can obtain the data source type and then automatically create the query class xxxDataBaseQueryImpl of the corresponding database type through the factory mode. It then obtains and encapsulates metadata such as data source information, table information, field information, and primary key information.
[0017] b. Add data source connection information and test the connection successfully.
[0018] c. You can customize the HTML and Word document export templates as needed. If you do not need to customize the template content, you can use the default template.
[0019] d. Create the getDbDesignDoc interface, pass in the data source ID added in step b and the fileType of the document to be generated, automatically adapt the exported document template, fill in the template content, and use the template engine to generate a preview HTML page or export it to an HTML or Word file.
[0020] Description of the specific technical effects of the present invention: 1. Dynamic creation of database query implementation class Generally, it is necessary to manually write the database query implementation class corresponding to the data source type according to the data source type. As the number of data source types increases, multiple sets of corresponding codes for obtaining data source information need to be manually written. After each data source is added, the service needs to be republished, which will lead to very poor code scalability. This method can dynamically create codes for different data source types according to the data source type, and there is no need to manually add codes according to the increase in data source types.
[0021] 2. Automatically adapt to one-click export Through the data source information registered in the system, the data source type, URL, account password and other information are obtained, and the data source driver type is automatically adapted. There is no need to specify or select the data source type and driver. The implementation class that adapts to the current data source is dynamically created. A customized document template is selected according to the selected document type to realize the one-click export function of various types of data source documents.
[0022] 3. Customize database structure document content Customized HTML and Word document export templates can adjust the format, style, and layout content of the export template according to the requirements of the document demander to achieve customized database structure documents.
[0023] Glossary Database structure: A database structure document is a detailed description of the various elements in a database and the relationships between them. This document typically includes the database's tables, fields, data types, indexes, relationships, and any other information related to database design.
[0024] JAR, short for Java Archive, is a Java compressed file format used to package multiple Java class files, associated metadata, and resource files (such as text files and images) into a single file. A JAR file is essentially a ZIP file, but with a specific purpose and format for deploying and distributing Java applications.
Claims
1. A method for generating scalable multi-type database structure documents, characterized in that The steps include:
1. Get data source connection information and examples First, obtain the database connection Url, username, password and other information, and use the obtained URL to determine the data source type, which includes DAMO, MySQL, TIDB, and KOSPI. Adapt the corresponding data source driver according to the data source type, and then initialize the database connection pool configuration HikariConfig, and then generate the DataSource database connection through the SpringBoot default connection pool HikariDataSource; build the generated file information, including the output path file type, file name, and template storage path of the file, where the file types include hypertext html, text word, and online html preview text; then build a custom EngineConfig instance; generate the Configuration document configuration, set the EngineConfig instance, custom version number, description, and database connection information parameters; Dynamically obtain data source metadata based on data source information Customize the JdbcUtils tool class, which includes the close function to close the data source connection and the getDbType function. It determines the current data source type based on the incoming URL, and then dynamically creates and returns the xxxDataBaseQueryImpl implementation class based on the corresponding data source type using the factory mode. First, get the data source information in the Configuration instance, get the url information of the data source connection, and get the current data source type and its corresponding data source type xxxDataBaseQueryImpl implementation class class according to the getDbType function in the custom JdbcUtils tool class. If the currently integrated DataBaseQueryImpl does not contain the implementation class of the current data source type, the corresponding DataBaseQueryImpl implementation class is automatically created dynamically through the factory mode, and then the newly created class constructor is obtained, and a new DatabaseQuery object instance is created using the Java reflection mechanism; Create a custom entity DataModel object. The entity contains information such as database data source information, tables data table collection, title title, version version, description description, etc. Create a custom DatabaseQueryFactory factory class, which contains a private DataSource object. The return value is the getDataSource operation of DataSource. The private DataSource object is directly returned. The return value is the newInstance operation of DatabaseQuery. First, the DataSource object is obtained according to the getDataSource operation, and then the URL of the database connection metadata is obtained. Then, the class of DataBaseQuery is obtained according to the URL using the getDbType operation in JdbcUtils. Then, a new instance object is created through the Constructor object, and the current DataSource is passed as an actual parameter to the constructor of the new object. Finally, the newly created DatabaseQuery object is returned, which is a subclass of DatabaseQuery, as well as parameterized and non-parameterized construction operations. A DatabaseQuery interface object is obtained according to the newInstance operation of the custom DatabaseQueryFactory. At this time, the implementation class of DatabaseQuery is created as xxxDataBaseQueryImpl of the current data source type through the factory design pattern. Each operation in xxxDataBaseQueryImpl will obtain and uniformly encapsulate the database table metadata according to different data source types, and then call the getDataBase operation in the custom interface DatabaseQuery to obtain the database object, the getTables operation to obtain the table collection, the getTableColumns operation to obtain the column collection, and the getPrimaryKeys operation to obtain the primaryKey collection. Then, the table collection, column collection and primaryKey collection are encapsulated into the tree structure through recursive encapsulation. Finally, the database object and the encapsulated table collection are placed in the created DataModel object; Generate Documentation Get the template engine, create and configure a Configuration object, set the configuration version, set the description information of the Configuration object, specify the data source, specify the specific table name to generate the configuration, and then use this configuration object to perform the document generation task. Create an instance of the DocumentationExecute class, pass in the previously constructed Configuration object as a parameter of the constructor, and then call the execute operation to perform the document generation task; when calling the execute operation, a DataModelProcess object is created, which receives the Configuration as a parameter of its constructor; Then the process operation of this object is called, which processes the data model according to the configuration and returns a DataModel object. An EngineFactory object is created, and the engineConfig of Configuration is passed in as a parameter. A Class object is obtained through the reflection mechanism, and engineConfig.getProduceType(). This Class object represents a specific TemplateEngine subclass. The type information of the template engine implementation is obtained from the current engineConfig configuration. Then, a constructor Constructor is found and obtained from the Class object obtained in the previous step through the getConstructor(EngineConfig.class) of the Class object. This constructor receives a parameter of the EngineConfig type. This step The purpose is to find the constructor that accepts EngineConfig as a parameter so that TemplateEngine can be instantiated later. The previously obtained constructor is called to create a new TemplateEngine instance. Then, type conversion (TemplateEngine) is used to convert the created object to the TemplateEngine type and return it. A new TemplateEngine instance is generated. TemplateEngine is used to process template files. The produce operation of the TemplateEngine object is called, and two parameters are passed in: dataModel and docName. Here, dataModel is the data model processed in the previous step, and docName is the name of the output document. The produce operation uses the provided data model and document name to generate the final document output, which is written to File and written to the specified path.