Dynamic conversion of SQL statement using design mode

xiaoxiao2021-03-06  19

Since the system is different from the actual situation of the customer, some of the selection of the database is biased towards the ease of use and maintainability of SQL Server, and some are biased towards the roble of robust and stability, and the SQL statement in the existing code A large number of application server-side functions and associations, these function calls, parameters, etc. have great differences in this different databases. If you want a code for each database, the future maintenance cost will be getting higher and higher; if you have a functional upgrade, the modification of the code will be an increase in geometric level.

For this problem, the envision of the start is relatively simple. Before executing the SQL statement, first check the currently connected database, if it is Oracle, if it does not do any interference, if it is connected to SQLServer, simply replace the different keywords and function names in the SQL statement Drop, such as TO_DATE in Oracle, you can execute on SQL Server, you can do this, for some simple SQL statements, but the application of a large number of complex reports in the system, SQL statement may nest There are also multi-storey nested, and if there is only a simple replace, there will be countless if else, and the modifications and commissioning after the error is almost impossible. By comparing the SQL syntax of Oracle and SQL Server, it is considered that the syntax analysis must be used to parse the SQL statement into a syntax tree, and then convert the SQL statement to the SQLServer on SQL Server according to the syntax. To achieve such functions, the models that need to be used are: 1. Interpreter - class behavior mode: given a language, define a representation of its textual law, and define an interpreter, this interpreter is used This representation explains the sentence in the language. By implementing the interpreter mode, the SQL statement to be executed is interpreted as a syntax tree of SQL. For example, a SELECT statement is as follows

As can be seen from this structural diagram, the SQL statement may have a very complex combination, and if you do not use the grammar tree, it is difficult to implement the conversion of different database platforms. In the system, call the SQL statement conversion function is Tadodataset or TadoQuery, which is the client in the mode. Therefore, the class relationship to use this mode is as follows

2.Composite - Object Structure mode: Combine the object into a tree structure to represent a "part-overall" hierarchy. C O m P O S I makes the user consistency for the use of a single object and a combined object.

转载请注明原文地址:https://www.9cbs.com/read-77160.html

New Post(0)