CN110555030B - SQL sentence processing method and device - Google Patents

SQL sentence processing method and device Download PDF

Info

Publication number
CN110555030B
CN110555030B CN201810264130.4A CN201810264130A CN110555030B CN 110555030 B CN110555030 B CN 110555030B CN 201810264130 A CN201810264130 A CN 201810264130A CN 110555030 B CN110555030 B CN 110555030B
Authority
CN
China
Prior art keywords
tenant
sql
new
statement
application program
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Active
Application number
CN201810264130.4A
Other languages
Chinese (zh)
Other versions
CN110555030A (en
Inventor
徐开廷
林世洪
孙海波
刘文杰
赵光伟
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Beijing Jingdong Century Trading Co Ltd
Beijing Jingdong Shangke Information Technology Co Ltd
Original Assignee
Beijing Jingdong Century Trading Co Ltd
Beijing Jingdong Shangke Information Technology Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Beijing Jingdong Century Trading Co Ltd, Beijing Jingdong Shangke Information Technology Co Ltd filed Critical Beijing Jingdong Century Trading Co Ltd
Priority to CN201810264130.4A priority Critical patent/CN110555030B/en
Publication of CN110555030A publication Critical patent/CN110555030A/en
Application granted granted Critical
Publication of CN110555030B publication Critical patent/CN110555030B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Landscapes

  • Information Retrieval, Db Structures And Fs Structures Therefor (AREA)
  • Stored Programmes (AREA)

Abstract

The invention discloses a method and a device for processing SQL sentences, and relates to the technical field of computers. One embodiment of the method comprises the following steps: intercepting all SQL sentences in the application program, and analyzing the SQL sentences to obtain the operation type of the SQL sentences; according to the operation type, adding a conditional statement identified by the tenant in the SQL statement to generate a new SQL statement; acquiring the value of the tenant identification and the parameter value of the SQL sentence, and transmitting the value of the tenant identification and the parameter value into a new SQL sentence. According to the embodiment, all SQL sentences in the application program are intercepted, the conditional sentences identified by the tenants are added according to the operation types of the SQL sentences, and the needed parameters of the new SQL sentences are replaced after the new SQL sentences are obtained. On the premise of not changing SQL sentences of the application program, conditional sentences and parameters of tenant identification are automatically added, and the existing application program is upgraded into the SAAS application program at the database level.

Description

SQL sentence processing method and device
Technical Field
The present invention relates to the field of computers, and in particular, to a method and apparatus for processing an SQL statement.
Background
SAAS (Software-as-a-Service) is a mode of providing Software services based on the Internet, vendors uniformly deploy application Software on their own servers, customers can order required application Software services to vendors through the Internet according to their actual demands, pay the vendors for the required services according to the amount and time of the ordered services, and obtain the services provided by the vendors through the Internet. The SAAS application program adopts a multi-tenant mode to carry out development and online deployment, and a traditional software developer hopes to have a mode capable of converting a non-SAAS application program into the SAAS application program.
Because the SAAS application program adopts a multi-tenant mode, in order to realize service isolation, a tenant identification field needs to be newly added in each database table. The application program of the traditional software industry contains a large number of added, deleted and revised SQL (Structured Query Language ) sentences, and when the application program of the traditional software industry is converted into the SAAS application program, a developer is required to manually add the conditional sentences identified by the tenant in all SQL sentences.
In the process of implementing the present invention, the inventor finds that at least the following problems exist in the prior art:
(1) The method involves the change of a large number of SQL sentences, and has huge labor cost and test cost;
(2) The manual modification is difficult to ensure that all SQL sentences are added with conditional sentences identified by tenants, and once the conditional sentences are missing, the data range of business operations such as inquiring, modifying, deleting and the like can be expanded in error after an application program is on line, so that the safety of the data cannot be ensured;
(3) Because all SQL sentences are manually modified, in the life cycle of the application program, if the testing range of the application program is insufficient, the correctness of the functions and the safety of data cannot be ensured after the application program is on line.
Disclosure of Invention
In view of this, the embodiment of the invention provides a method and a device for processing an SQL statement, which are used for intercepting all the SQL statements in an application program, adding a conditional statement identified by a tenant according to the operation type of the SQL statement, so as to replace the required parameters after obtaining a new SQL statement. On the premise of not changing SQL sentences of the application program, conditional sentences and parameters of tenant identification are automatically added, and the existing application program is upgraded into the SAAS application program at the database level.
In order to achieve the above object, according to an aspect of the embodiments of the present invention, there is provided a method for processing an SQL statement.
The method for processing the SQL sentence comprises the following steps: intercepting all SQL sentences in an application program, and analyzing the SQL sentences to obtain the operation type of the SQL sentences; according to the operation type, adding a conditional statement identified by the tenant in the SQL statement to generate a new SQL statement; and acquiring the value of the tenant identification and the parameter value of the SQL sentence, and transmitting the value of the tenant identification and the parameter value into the new SQL sentence.
Optionally, the operation types include query, modification, deletion and addition; and adding the conditional statement identified by the tenant in the SQL statement according to the operation type to generate a new SQL statement, wherein the conditional statement comprises: if the operation type is query, modification or deletion, adding the conditional statement identified by the tenant into a sphere clause of the SQL statement; if the operation type is newly added and no column name corresponding to the tenant identification exists in the newly added column, inserting the column name into the newly added column, and writing parameters corresponding to the tenant identification into values corresponding to the column name.
Optionally, the adding the conditional statement identified by the tenant to the where clause of the SQL statement includes: if the expression of the where clause is empty, adding an expression of a conditional statement comprising the tenant identification; and if the expression of the where clause is not null, connecting the expression with the conditional statement identified by the tenant.
Optionally, the step of transmitting the value of the tenant identifier and the parameter value into the new SQL statement includes: analyzing the new SQL sentence to obtain the types and positions of identifiers of all parameters in the new SQL sentence; and correspondingly transmitting the value of the tenant identification and the parameter value into the new SQL sentence according to the identifier type and the position.
Optionally, after the step of transmitting the value of the tenant identifier and the parameter value into the new SQL statement, the method further includes: predicting the data quantity of a tenant, and when the magnitude of the data quantity is equal to a preset threshold value, establishing a new database table; the new database table has the same structure as the original database table of the tenant, and the table names are different; adding configuration information in the application program; the configuration information comprises a table name of the original database table, a tenant identifier and a table name of the new database table; intercepting all the new SQL sentences in the application program, and analyzing the new SQL sentences to obtain the table names of the original database tables and the tenant identifications; and determining that the table names of the original database tables and the tenant identifications exist in the configuration information, and replacing the table names of the original database tables in the new SQL sentences with the table names of the corresponding new database tables.
Optionally, the configuration information includes a storage location of the new database table; after the step of determining that the table name of the original database table and the tenant identifier exist in the configuration information, the method further includes: and when the storage position of the configuration information is not empty, updating the storage position of the original database table in the application program according to the storage position.
To achieve the above object, according to an aspect of an embodiment of the present invention, there is provided a processing apparatus for an SQL statement.
The device for processing SQL sentences comprises: the interception analysis module is used for intercepting all SQL sentences in the application program and analyzing the SQL sentences to acquire the operation types of the SQL sentences; the addition generating module is used for adding the conditional statement identified by the tenant in the SQL statement according to the operation type so as to generate a new SQL statement; and the acquisition and input module is used for acquiring the value of the tenant identifier and the parameter value of the SQL sentence, and inputting the value of the tenant identifier and the parameter value into the new SQL sentence.
Optionally, the operation types include query, modification, deletion and addition; the addition generating module is further configured to: if the operation type is query, modification or deletion, adding the conditional statement identified by the tenant into a sphere clause of the SQL statement; and if the operation type is newly added and no column name corresponding to the tenant identification exists in the newly added column, inserting the column name into the newly added column, and writing parameters corresponding to the tenant identification into values corresponding to the column name.
Optionally, the adding generation module is further configured to: if the expression of the where clause is empty, adding an expression of a conditional statement comprising the tenant identification; and if the expression of the where clause is not null, connecting the expression with the conditional statement identified by the tenant.
Optionally, the acquiring an incoming module is further configured to: analyzing the new SQL sentence to obtain the types and positions of identifiers of all parameters in the new SQL sentence; and correspondingly transmitting the value of the tenant identification and the parameter value into the new SQL sentence according to the identifier type and the position.
Optionally, the apparatus further comprises: a replacement module is established and used for predicting the data quantity of the tenant, and when the magnitude of the data quantity is equal to a preset threshold value, a new database table is established; the new database table has the same structure as the original database table of the tenant, and the table names are different; adding configuration information in the application program; the configuration information comprises a table name of the original database table, a tenant identifier and a table name of the new database table; intercepting all the new SQL sentences in the application program, and analyzing the new SQL sentences to obtain the table names of the original database tables and the tenant identifications; and determining that the table names of the original database tables and the tenant identifications exist in the configuration information, and replacing the table names of the original database tables in the new SQL sentences with the table names of the corresponding new database tables.
Optionally, the configuration information includes a storage location of the new database table; the apparatus further comprises: and the storage position updating module is used for updating the storage position of the original database table in the application program according to the storage position when the storage position of the configuration information is not empty.
To achieve the above object, according to still another aspect of the embodiments of the present invention, there is provided an electronic device.
An electronic device according to an embodiment of the present invention includes: one or more processors; and the storage device is used for storing one or more programs, and when the one or more programs are executed by the one or more processors, the one or more processors are enabled to realize the SQL sentence processing method.
To achieve the above object, according to still another aspect of the embodiments of the present invention, there is provided a computer-readable medium.
A computer readable medium of an embodiment of the present invention has stored thereon a computer program which, when executed by a processor, implements a method for processing an SQL statement of an embodiment of the present invention.
One embodiment of the above invention has the following advantages or benefits: intercepting all SQL sentences in the application program, adding conditional sentences identified by the tenant according to the operation type of the SQL sentences to replace needed parameters after obtaining new SQL sentences, and automatically adding the conditional sentences and parameters identified by the tenant without changing the SQL sentences of the application program under the condition of transparency of the application program, thereby realizing upgrading the existing application program into the SAAS application program at the database level; respectively processing the query, modification, deletion and new SQL sentences to obtain corresponding new SQL sentences; by presetting configuration information in an application program, under the condition of transparency of the application program, a database table of a tenant with larger data quantity difference and a database table of a tenant with smaller data quantity difference are physically isolated.
Further effects of the above-described non-conventional alternatives are described below in connection with the embodiments.
Drawings
The drawings are included to provide a better understanding of the invention and are not to be construed as unduly limiting the invention. Wherein:
FIG. 1 is a schematic diagram of the main steps of a method of processing SQL statements according to an embodiment of the invention;
FIG. 2 is a main flow diagram of a method for processing SQL statements according to an embodiment of the invention;
FIG. 3 is a schematic diagram of the main modules of a processing apparatus of SQL statements according to an embodiment of the invention;
FIG. 4 is an exemplary system architecture diagram in which embodiments of the present invention may be applied;
Fig. 5 is a schematic structural diagram of a computer device suitable for use in an electronic apparatus to implement an embodiment of the present invention.
Detailed Description
Exemplary embodiments of the present invention will now be described with reference to the accompanying drawings, in which various details of the embodiments of the present invention are included to facilitate understanding, and are to be considered merely exemplary. Accordingly, those of ordinary skill in the art will recognize that various changes and modifications of the embodiments described herein can be made without departing from the scope and spirit of the invention. Also, descriptions of well-known functions and constructions are omitted in the following description for clarity and conciseness.
Fig. 1 is a schematic diagram of main steps of a method for processing an SQL statement according to an embodiment of the invention. As shown in FIG. 1, the SQL sentence processing method in the embodiment of the invention mainly comprises the following steps:
Step S101: intercepting all SQL sentences in an application program, and analyzing the SQL sentences to acquire the operation types of the SQL sentences. In the embodiment, frames such as MyBatis, hibernate can be used for intercepting all SQL sentences in the application program, wherein MyBatis is a Java-based persistent layer frame, and customized SQL, storage processes and advanced mapping are supported; hibernate is an object relational mapping framework of open source code, and can automatically generate SQL sentences and automatically execute the SQL sentences, so that Java programmers can use object programming thinking to manipulate the database at will. The parsed SQL statement includes four operation types, query, modify, delete and add, respectively.
Step S102: and adding the conditional statement identified by the tenant in the SQL statement according to the operation type to generate a new SQL statement. If the operation type is query, modification or deletion, adding the conditional statement identified by the tenant into a sphere clause of the SQL statement; if the operation type is newly added and no column name corresponding to the tenant identification exists in the newly added column, inserting the column name into the newly added column, and writing parameters corresponding to the tenant identification into values corresponding to the column name.
Step S103: and acquiring the value of the tenant identification and the parameter value of the SQL sentence, and transmitting the value of the tenant identification and the parameter value into the new SQL sentence. Acquiring all parameter values from the intercepted SQL statement, acquiring the value of the tenant identification from the context of the access request, and correspondingly transmitting the value of the tenant identification and the parameter value into a new SQL statement according to the type and the position of identifiers of all parameters in the new SQL statement. By the aid of the method, on the premise that SQL sentences of the application program are not changed, conditional sentences and parameters of tenant identifications are automatically added in the SQL sentences, and the existing application program is upgraded to the SAAS application program in a database level.
Fig. 2 is a main flow diagram of a method for processing an SQL statement according to an embodiment of the invention. As shown in FIG. 2, the SQL statement processing method in the embodiment of the invention mainly comprises the following steps:
Step S201: intercepting all SQL sentences in an application program, and analyzing the SQL sentences to acquire the operation types of the SQL sentences. All SQL statements in the application can be intercepted by MyBatis, hibernate, etc. The intercepted SQL statement is divided into four operation types: query (Select), modification (Update), deletion (Delete) and addition (Insert), and obtaining an abstract syntax tree (abstract syntax tree, AST) after parsing, and obtaining the operation type of the SQL statement through the AST.
Step S202: and adding the conditional statement identified by the tenant in the SQL statement according to the operation type to generate a new SQL statement. If the operation type is query, modification or deletion, adding the conditional statement identified by the tenant into a sphere clause of the SQL statement; if the operation type is newly added and the column name corresponding to the tenant identifier does not exist in the column of the data to be newly added, inserting the column name into the column of the data to be newly added, and writing the parameter corresponding to the tenant identifier into the value corresponding to the column name. Wherein, the conditional statement identified by the tenant may be: tablename.tenant id=: tenantId, where: tenantId represents a parameter, which may be an actual value or a wild card dynamic parameter (e.g.,. The specific adding process is as follows:
(1) The grammar format of the Select statement is:
SELECT select_expr[,select_expr…]
FROM table_references
[WHERE where_condition]
[HAVING where_condition]
The processing of the from clause is divided into two cases, wherein the first case is that the from clause is a table, and a conditional statement identified by a tenant is directly added to the where clause, and the conditional statement specifically may be: AND tableName.tenantId=: tenantId, wherein if the table does not define an alias, tableName is the real name of the table; if the table defines an alias, tableName is the alias of the table, the column names of tenantId are only illustrated here, and the configuration parameters may be modified to other names in practical applications. Another case is that the from clause is a sub-query, which needs to be recursively processed, i.e. the sub-query is recursively used as an SQL statement, whether the sub-query has the from clause, the where clause, etc. and the conditional statement of the tenant identity is added.
Join clause: and adding the conditional statement identified by the tenant in the sphere clause.
The same processing logic is executed by the segmentation clause AND the where clause, the type of the clause is judged, AND if the clause is a binary connection symbol (such as AND, OR, =and the like), the left clause AND the right clause of the binary connection symbol need to be processed in a recursion mode; if In connectors, all the expressions In the In list need to be cycled, and then each expression is recursively processed; if the sub-query is a sub-query, the sub-query needs to be processed recursively; if a () connector, then the sub-expressions in brackets need to be recursively processed; in the case of an exist expression, the expression needs to be recursively processed. The recursion processing is to use the above situation as an SQL sentence, recursion from the beginning, see the binary connection symbol left and right clauses, all expressions of in list, sub-query, sub-expression in brackets or exist from clause, where clause, etc. in exist expression, and add the conditional sentence of tenant identification.
The AND tableName.tenantId=: tenantId needs to be added into the expression of the where clause, if the expression of the where clause in the intercepted SQL statement is empty, a where expression of the conditional statement including tenant identification is newly added; and if the expression of the where clause in the intercepted SQL statement is not null, connecting the expression with the conditional statement identified by the tenant. Whether the conditional statement identified by the tenant is placed after the original condition of the where expression or before the original condition can be determined by a configuration mode. For example, the sphere expression of the intercepted SQL statement is not null, assuming sphere a= 'b', then the concatenation is followed by sphere a= 'b' and tableName.tenantId=: tenantId or sphere tableName.tenantId=: TENANTID AND A = 'b'.
The following is illustrative: example one (from clause is a table):
assume that the intercepted SQL statement is: select from a;
The new SQL statement is: SELECT =? .
Embodiment two (from clause is a sub-query):
Assume that the intercepted SQL statement is: SELECT FROM (SELECT FROM tab1 inner joint tab2 white tab1. Id=tab 2. Id) temp1;
Then the new SQL statement is :SELECT*FROM(SELECT*FROM tab1 INNER JOIN tab2 WHERE tab2.tenantId=?AND tab1.tenantId=?AND tab1.id=tab2.id)temp1.
Embodiment three (from clause is a table plus a sub-query):
assume that the intercepted SQL statement is: SELECT ENAME, deptno, sal from emp where deptno = (select deptno from DEPT WHERE loc= 'NEW YORK');
The new SQL statement is :SELECT ename,deptno,sal FROM emp WHERE emp.tenantId=?AND deptno=(SELECT deptno FROM dept WHERE dept.tenantId=?AND loc='NEW YORK'). where the above underlined statement is a conditional statement identified by the added tenant.
(2) The syntax format of the Update statement is:
UPDATA table_reference
SET assignment_list
[WHERE where_condition]
The Update sentence comprises a from clause, a join clause and a where clause, and the processing procedure of the conditional sentence added with the tenant identification is the same as that of the corresponding clause of the Select sentence.
(3) The syntax format of the Delete statement is:
Delete[LOW_PRIORITY][QUICK][IGNORE]FROM tbl_name
[WHERE where_condition]
The Delete sentence comprises a from clause, a join clause and a where clause, and the processing procedure of the conditional sentence added with the tenant identification is the same as the corresponding clause of the Select sentence.
(4) The syntax format of the Insert statement is:
INSERT[LOW_PRIORITY|HIGH_PRIORITY][IGNORE]
[INTO]tbl_name
[PARTITION(partition_name[,partition_name]…)]
[(col_name[,col_name]…)]
SELECT…
[ON DUPLICATE KEY UPDATE assignment_list]
Judging whether the columns of the Insert contain column names corresponding to tenant identifications, and if the columns of the Insert contain column names corresponding to tenant identifications, not performing any processing; if the column name corresponding to the tenant identification is not included, the column name corresponding to the tenant identification is inserted, and meanwhile, parameters of the tenant identification are required to be added in values (values) corresponding to the column name, namely tenantId. For example, the intercepted Insert statement is: insert a (NAME, SEX) values ('xx', '1'), then the new insert sentence is: insert a (tenantId, NAME, SEX) values (: tenantId, 'xx', '1').
Step S203: analyzing the new SQL sentence to obtain the types and positions of identifiers of all parameters in the new SQL sentence. Assuming that the parameters of tenant identification are tenantid expression patterns, and selecting from a window a.tenant Id=: TENANTID AND a.id=? And a.name=? This SQL statement exemplifies the processing procedure of step S203 to step S205. Two types of dynamic parameters are obtained through analysis: is the belt? Parameters of the identifier and the tape, which type of identifier is recorded sequentially from beginning to end, such as the recorded content is::,?,? What is? The actual business meaning is: bit 0 is the parameter of the band-identifier, bit 1 and bit 2 are the bands? Parameters of the identifier.
Since the database cannot directly handle the parameters in AND tableName.tenant Id=: tenantId: tenantId, while parsing, the parameters of the band: identifier need to be replaced with the band? The parameters of the identifier, the substitution result obtained is: select =? and a.id=? And a.name=? .
Step S204: and acquiring all parameter values from the SQL statement, and acquiring the value of the tenant identification from the context of the access request. The client sends an access request to the database, and in an actual application program, the context of the access request is generally put into a thread variable, and the value of the tenant identifier is obtained from the thread variable.
Step S205: and correspondingly transmitting the parameter value and the value of the tenant identifier into the new SQL sentence according to the identifier type and the position so as to execute the new SQL sentence. The types and positions of identifiers of all parameters of the new SQL statement are::,?,? What is? For example, firstly, initializing the access position of a parameter list of the intercepted SQL sentence as 0, wherein the first parameter of the new SQL sentence is a parameter with an identifier, and a tenant is required to be pushed (pushed) into a new parameter list corresponding to the new SQL sentence; the second parameter is the band? The parameter of the identifier is required to read the parameter with the access position of 0 from the original parameter list, then push the parameter into the new parameter list, and then add the access position of the original parameter list to +1; the third parameter is still band? The parameter of the identifier needs to be read from the original parameter list to access the parameter with the position of 1, then the parameter is pushed to the new parameter list, and then the access position of the original parameter list is +1. And so on. The new SQL statement and the new parameter list are then handed back MyBatis, hibernate, etc. frames for continued processing, so that MyBatis, hibernate, etc. frames can continue execution using the new SQL statement and the new parameter list.
The specific procedure of generating the new parameter list of step S205 is exemplified below.
Assuming that the tenant is identified as 1101, the intercepted SQL statement is: SELECT NAME, id, sex from user where user.id=? and user.city=? The parameter list is: 12222, 'SD'; the new SQL statement is: SELECT NAME, id, six from user WHERE TENANTID =? and user.id=? and user.city=? The new parameter list is: 1101, 12222, 'SD'.
In a preferred embodiment, certain SQL statements may be filtered out in a configuration file if they do not require the automatic addition of tenant-identified conditional statements. Such as: the SQL statement to be filtered in the configuration file is: and selecting from a window a= 'b', judging whether the intercepted SQL sentence is identical to the SQL sentence, and if so, filtering the intercepted SQL sentence, so that the purpose of automatically adding the conditional statement of the tenant identification in the SQL sentence can be realized.
In the SAAS application program, the data volume difference of each tenant is large, and the tenant with large data volume and the tenant with small data volume are required to be physically isolated from the dimension of the database table under the condition that the program codes of the developers are unchanged, and the specific implementation process is shown in step S206 to step S209.
Step S206: predicting the data quantity of a tenant, and when the magnitude of the data quantity is equal to a preset threshold value, establishing a new database table; the new database table has the same structure as the original database table of the tenant, and the table names are different. When each tenant accesses the SAAS application program, the data volume of each tenant can be estimated, and if the magnitude of the data volume of a certain tenant reaches a preset threshold value, an operation and maintenance person of the application program can independently establish a set of new database table with the same structure as the original database table for the tenant. The new database table is different from the original database table in table name, and the storage can be isolated independently.
Step S207: adding configuration information in the application program; the configuration information comprises a table name of the original database table, a tenant identifier, a table name of the new database table and a storage position of the new database table. The following configuration information is added in the application program: table a, tenant identity- →database domain name: port number: the system comprises a table, wherein A is the table name of an original database table, A1 is the table name of a new database table, and a database domain name and a port number represent the storage position of the new database table; the specific meaning is as follows: table a, the configuration information corresponding to the tenant identifier is table A1 under a certain port number under the database domain name. Here, the database domain name and port number may be empty, representing that the original database table and the new database table are stored in the same machine; if not empty, the representation of the original database table and the new database table are stored on different machines.
Step S208: intercepting all the new SQL sentences in the application program, and analyzing the new SQL sentences to obtain the table names of the original database tables and the tenant identifications. Analyzing the intercepted new SQL sentence to obtain the table name and tenant identification of the original database table.
Step S209: and determining that the table names of the original database tables and the tenant identifications exist in the configuration information, and replacing the table names of the original database tables in the new SQL sentences with the table names of the corresponding new database tables. And matching the acquired table names and tenant identifications of the original database tables with the configuration information, and if the table names can be matched, replacing the table names in the new SQL sentences with the table names of the new database tables. I.e. table a is replaced by table A1.
In addition, if the new database table and the original database table are stored in different machines, the data source needs to be modified to the database domain name and port number in the configuration information to point to the actual storage machine.
According to the SQL sentence processing method, all SQL sentences in the application program are intercepted, the conditional sentences identified by the tenant are added according to the operation type of the SQL sentences so as to replace the required parameters after the new SQL sentences are obtained, and under the condition that the application program is transparent, the conditional sentences and the parameters identified by the tenant can be automatically added without changing the SQL sentences of the application program, so that the existing application program is upgraded into the SAAS application program at the database level; respectively processing the query, modification, deletion and new SQL sentences to obtain corresponding new SQL sentences; by presetting configuration information in an application program, under the condition of transparency of the application program, a database table of a tenant with larger data quantity difference and a database table of a tenant with smaller data quantity difference are physically isolated.
Fig. 3 is a schematic diagram of main modules of a processing apparatus of an SQL statement according to an embodiment of the invention. As shown in fig. 3, the processing device 300 for SQL statements in the embodiment of the present invention mainly includes:
The interception and analysis module 301 is configured to intercept all the SQL statements in the application program, and analyze the SQL statements to obtain operation types of the SQL statements. All SQL statements in the application program can be intercepted by MyBatis, hibernate and the like, and the parsed SQL statements comprise four operation types, namely inquiry, modification, deletion and new addition.
And the addition generating module 302 is configured to add the conditional statement identified by the tenant in the SQL statement according to the operation type, so as to generate a new SQL statement. If the operation type is query, modification or deletion, adding the conditional statement identified by the tenant into a sphere clause of the SQL statement; if the operation type is newly added and no column name corresponding to the tenant identification exists in the newly added column, inserting the column name into the newly added column, and writing parameters corresponding to the tenant identification into values corresponding to the column name.
And the acquiring and inputting module 303 is configured to acquire the value of the tenant identifier and the parameter value of the SQL statement, and input the value of the tenant identifier and the parameter value into the new SQL statement. Acquiring all parameter values from the intercepted SQL statement, acquiring the value of the tenant identification from the context of the access request, and correspondingly transmitting the value of the tenant identification and the parameter value into a new SQL statement according to the type and the position of identifiers of all parameters in the new SQL statement. The method and the device realize that on the premise of not changing the SQL statement of the application program, the conditional statement and the parameters of the tenant identification are automatically added in the SQL statement, and the existing application program is upgraded into the SAAS application program in the database level.
In addition, the processing device 300 of the SQL statement according to the embodiment of the present invention may further include: a replacement module and a storage location update module (not shown in fig. 3) are established. The establishing and replacing module is used for predicting the data quantity of the tenant, and when the magnitude of the data quantity is equal to a preset threshold value, a new database table is established; the new database table has the same structure as the original database table of the tenant, and the table names are different; adding configuration information in the application program; the configuration information comprises a table name of the original database table, a tenant identifier and a table name of the new database table; intercepting all the new SQL sentences in the application program, and analyzing the new SQL sentences to obtain the table names of the original database tables and the tenant identifications; and determining that the table names of the original database tables and the tenant identifications exist in the configuration information, and replacing the table names of the original database tables in the new SQL sentences with the table names of the corresponding new database tables. And the storage position updating module is used for updating the storage position of the original database table in the application program according to the storage position when the storage position of the configuration information is not empty.
As can be seen from the above description, all the SQL statements in the application program are intercepted, the conditional statements of tenant identifications are added according to the operation type of the SQL statements so as to replace the required parameters after the new SQL statements are obtained, and under the condition that the application program is transparent, the conditional statements and the parameters of the tenant identifications can be automatically added without changing the SQL statements of the application program, thereby realizing upgrading the existing application program into the SAAS application program at the database level; respectively processing the query, modification, deletion and new SQL sentences to obtain corresponding new SQL sentences; by presetting configuration information in an application program, under the condition of transparency of the application program, a database table of a tenant with larger data quantity difference and a database table of a tenant with smaller data quantity difference are physically isolated.
FIG. 4 illustrates an exemplary system architecture 400 of a processing method of SQL statements or a processing device of SQL statements to which embodiments of the invention may be applied.
As shown in fig. 4, the system architecture 400 may include terminal devices 401, 402, 403, a network 404, and a server 405. The network 404 is used as a medium to provide communication links between the terminal devices 401, 402, 403 and the server 405. The network 404 may include various connection types, such as wired, wireless communication links, or fiber optic cables, among others.
A user may interact with the server 405 via the network 404 using the terminal devices 401, 402, 403 to receive or send messages or the like. Various communication client applications, such as shopping class applications, web browser applications, search class applications, instant messaging tools, mailbox clients, social platform software, etc., may be installed on the terminal devices 401, 402, 403.
The terminal devices 401, 402, 403 may be various electronic devices having a display screen and supporting web browsing, including but not limited to smartphones, tablets, laptop and desktop computers, and the like.
The server 405 may be a server providing various services, such as a background management server providing support for click events generated by users using the terminal devices 401, 402, 403. The background management server may analyze the received click data, text content, and other data, and feedback the processing result (e.g., the target push information, the product information—only an example) to the terminal device.
It should be noted that, the processing method of the SQL statement provided in the embodiment of the present application is generally executed by the server 405, and accordingly, the processing device of the SQL statement is generally disposed in the server 405.
It should be understood that the number of terminal devices, networks and servers in fig. 4 is merely illustrative. There may be any number of terminal devices, networks, and servers, as desired for implementation.
According to an embodiment of the invention, the invention further provides an electronic device and a computer readable medium.
The electronic device of the present invention includes: one or more processors; and the storage device is used for storing one or more programs, and when the one or more programs are executed by the one or more processors, the one or more processors are enabled to realize the SQL sentence processing method.
The computer readable medium of the present invention has a computer program stored thereon, which when executed by a processor implements a method for processing an SQL statement of an embodiment of the present invention.
Referring now to FIG. 5, there is illustrated a schematic diagram of a computer system 500 suitable for use in implementing an embodiment of the present invention. The electronic device shown in fig. 5 is only an example and should not be construed as limiting the functionality and scope of use of the embodiments of the present invention.
As shown in fig. 5, the computer system 500 includes a Central Processing Unit (CPU) 501, which can perform various appropriate actions and processes according to a program stored in a Read Only Memory (ROM) 502 or a program loaded from a storage section 508 into a Random Access Memory (RAM) 503. In the RAM 503, various programs and data required for the operation of the computer system 500 are also stored. The CPU 501, ROM 502, and RAM 503 are connected to each other through a bus 504. An input/output (I/O) interface 505 is also connected to bus 504.
The following components are connected to the I/O interface 505: an input section 506 including a keyboard, a mouse, and the like; an output portion 507 including a Cathode Ray Tube (CRT), a Liquid Crystal Display (LCD), and the like, and a speaker, and the like; a storage portion 508 including a hard disk and the like; and a communication section 509 including a network interface card such as a LAN card, a modem, or the like. The communication section 509 performs communication processing via a network such as the internet. The drive 510 is also connected to the I/O interface 505 as needed. A removable medium 511 such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, or the like is mounted on the drive 510 as needed so that a computer program read therefrom is mounted into the storage section 508 as needed.
In particular, the processes described above in the main step diagrams may be implemented as computer software programs according to the disclosed embodiments of the invention. For example, embodiments of the present disclosure include a computer program product comprising a computer program embodied on a computer readable medium, the computer program comprising program code for performing the method shown in the main step diagrams. In such an embodiment, the computer program may be downloaded and installed from a network via the communication portion 509, and/or installed from the removable media 511. The above-described functions defined in the system of the present invention are performed when the computer program is executed by a Central Processing Unit (CPU) 501.
The computer readable medium shown in the present invention may be a computer readable signal medium or a computer readable storage medium, or any combination of the two. The computer readable storage medium can be, for example, but not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or a combination of any of the foregoing. More specific examples of the computer-readable storage medium may include, but are not limited to: an electrical connection having one or more wires, a portable computer diskette, a hard disk, a Random Access Memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disc read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing. In the context of this document, a computer readable storage medium may be any tangible medium that can contain, or store a program for use by or in connection with an instruction execution system, apparatus, or device. In the present invention, however, the computer-readable signal medium may include a data signal propagated in baseband or as part of a carrier wave, with the computer-readable program code embodied therein. Such a propagated data signal may take any of a variety of forms, including, but not limited to, electro-magnetic, optical, or any suitable combination of the foregoing. A computer readable signal medium may also be any computer readable medium that is not a computer readable storage medium and that can communicate, propagate, or transport a program for use by or in connection with an instruction execution system, apparatus, or device. Program code embodied on a computer readable medium may be transmitted using any appropriate medium, including but not limited to: wireless, wire, fiber optic cable, RF, etc., or any suitable combination of the foregoing.
The flowcharts and block diagrams in the figures illustrate the architecture, functionality, and operation of possible implementations of systems, methods and computer program products according to various embodiments of the present invention. In this regard, each block in the flowchart or block diagrams may represent a module, segment, or portion of code, which comprises one or more executable instructions for implementing the specified logical function(s). It should also be noted that, in some alternative implementations, the functions noted in the block may occur out of the order noted in the figures. For example, two blocks shown in succession may, in fact, be executed substantially concurrently, or the blocks may sometimes be executed in the reverse order, depending upon the functionality involved. It will also be noted that each block of the block diagrams or flowchart illustration, and combinations of blocks in the block diagrams or flowchart illustration, can be implemented by special purpose hardware-based systems which perform the specified functions or acts, or combinations of special purpose hardware and computer instructions.
The modules involved in the embodiments of the present invention may be implemented in software or in hardware. The described modules may also be provided in a processor, for example, as: a processor includes an intercept resolution module, an add generation module, and an acquire incoming module. The names of these modules do not limit the module itself in some cases, for example, the interception parsing module may also be described as "a module that intercepts all SQL statements in an application program, parses the SQL statements to obtain an operation type of the SQL statements".
As another aspect, the present invention also provides a computer-readable medium that may be contained in the apparatus described in the above embodiments; or may be present alone without being fitted into the device. The computer readable medium carries one or more programs which, when executed by a device, cause the device to include: intercepting all SQL sentences in an application program, and analyzing the SQL sentences to obtain the operation type of the SQL sentences; according to the operation type, adding a conditional statement identified by the tenant in the SQL statement to generate a new SQL statement; and acquiring the value of the tenant identification and the parameter value of the SQL sentence, and transmitting the value of the tenant identification and the parameter value into the new SQL sentence.
As can be seen from the above description, all the SQL statements in the application program are intercepted, the conditional statements of tenant identifications are added according to the operation type of the SQL statements so as to replace the required parameters after the new SQL statements are obtained, and under the condition that the application program is transparent, the conditional statements and the parameters of the tenant identifications can be automatically added without changing the SQL statements of the application program, thereby realizing upgrading the existing application program into the SAAS application program at the database level; respectively processing the query, modification, deletion and new SQL sentences to obtain corresponding new SQL sentences; by presetting configuration information in an application program, under the condition of transparency of the application program, a database table of a tenant with larger data quantity difference and a database table of a tenant with smaller data quantity difference are physically isolated.
The product can execute the method provided by the embodiment of the invention, and has the corresponding functional modules and beneficial effects of the execution method. Technical details not described in detail in this embodiment may be found in the methods provided in the embodiments of the present invention.
The above embodiments do not limit the scope of the present invention. It will be apparent to those skilled in the art that various modifications, combinations, sub-combinations and alternatives can occur depending upon design requirements and other factors. Any modifications, equivalent substitutions and improvements made within the spirit and principles of the present invention should be included in the scope of the present invention.

Claims (14)

1. A method for processing an SQL statement, comprising:
intercepting all SQL sentences in an application program, and analyzing the SQL sentences to obtain the operation type of the SQL sentences;
According to the operation type, adding a conditional statement identified by the tenant in the SQL statement to generate a new SQL statement;
And acquiring the value of the tenant identification and the parameter value of the SQL sentence, and transmitting the value of the tenant identification and the parameter value into the new SQL sentence.
2. The method of claim 1, wherein the operation types include query, modification, deletion, and addition;
and adding the conditional statement identified by the tenant in the SQL statement according to the operation type to generate a new SQL statement, wherein the conditional statement comprises:
if the operation type is query, modification or deletion, adding the conditional statement identified by the tenant into a sphere clause of the SQL statement;
If the operation type is newly added and no column name corresponding to the tenant identification exists in the newly added column, inserting the column name into the newly added column, and writing parameters corresponding to the tenant identification into values corresponding to the column name.
3. The method of claim 2, wherein adding the tenant-identified conditional statement to the sphere clause of the SQL statement comprises:
If the expression of the where clause is empty, adding an expression of a conditional statement comprising the tenant identification;
and if the expression of the where clause is not null, connecting the expression with the conditional statement identified by the tenant.
4. The method of claim 1, wherein the passing the value of the tenant identity and the parameter value into the new SQL statement comprises:
analyzing the new SQL sentence to obtain the types and positions of identifiers of all parameters in the new SQL sentence;
and correspondingly transmitting the value of the tenant identification and the parameter value into the new SQL sentence according to the identifier type and the position.
5. The method according to any of claims 1 to 4, further comprising, after the step of passing the value of the tenant identity and the parameter value into the new SQL statement:
predicting the data quantity of a tenant, and when the magnitude of the data quantity is equal to a preset threshold value, establishing a new database table; the new database table has the same structure as the original database table of the tenant, and the table names are different;
Adding configuration information in the application program; the configuration information comprises a table name of the original database table, a tenant identifier and a table name of the new database table;
Intercepting all the new SQL sentences in the application program, and analyzing the new SQL sentences to obtain the table names of the original database tables and the tenant identifications;
and determining that the table names of the original database tables and the tenant identifications exist in the configuration information, and replacing the table names of the original database tables in the new SQL sentences with the table names of the corresponding new database tables.
6. The method of claim 5, wherein the configuration information includes a storage location of the new database table;
After the step of determining that the table name of the original database table and the tenant identifier exist in the configuration information, the method further includes:
And when the storage position of the configuration information is not empty, updating the storage position of the original database table in the application program according to the storage position.
7. A processing device for an SQL statement, comprising:
the interception analysis module is used for intercepting all SQL sentences in the application program and analyzing the SQL sentences to acquire the operation types of the SQL sentences;
the addition generating module is used for adding the conditional statement identified by the tenant in the SQL statement according to the operation type so as to generate a new SQL statement;
And the acquisition and input module is used for acquiring the value of the tenant identifier and the parameter value of the SQL sentence, and inputting the value of the tenant identifier and the parameter value into the new SQL sentence.
8. The apparatus of claim 7, wherein the operation types include query, modification, deletion, and addition;
the addition generating module is further configured to:
if the operation type is query, modification or deletion, adding the conditional statement identified by the tenant into a sphere clause of the SQL statement; and
If the operation type is newly added and no column name corresponding to the tenant identification exists in the newly added column, inserting the column name into the newly added column, and writing parameters corresponding to the tenant identification into values corresponding to the column name.
9. The apparatus of claim 8, wherein the addition generation module is further configured to:
if the expression of the where clause is empty, adding an expression of a conditional statement comprising the tenant identification; and
And if the expression of the where clause is not null, connecting the expression with the conditional statement identified by the tenant.
10. The apparatus of claim 7, wherein the acquisition incoming module is further configured to:
analyzing the new SQL sentence to obtain the types and positions of identifiers of all parameters in the new SQL sentence; and
And correspondingly transmitting the value of the tenant identification and the parameter value into the new SQL sentence according to the identifier type and the position.
11. The apparatus according to any one of claims 7 to 10, further comprising: establishing a replacement module for
Predicting the data quantity of a tenant, and when the magnitude of the data quantity is equal to a preset threshold value, establishing a new database table; the new database table has the same structure as the original database table of the tenant, and the table names are different;
Adding configuration information in the application program; the configuration information comprises a table name of the original database table, a tenant identifier and a table name of the new database table;
Intercepting all the new SQL sentences in the application program, and analyzing the new SQL sentences to obtain the table names of the original database tables and the tenant identifications;
and determining that the table names of the original database tables and the tenant identifications exist in the configuration information, and replacing the table names of the original database tables in the new SQL sentences with the table names of the corresponding new database tables.
12. The apparatus of claim 11, wherein the configuration information comprises a storage location of the new database table;
the apparatus further comprises: and the storage position updating module is used for updating the storage position of the original database table in the application program according to the storage position when the storage position of the configuration information is not empty.
13. An electronic device, comprising:
One or more processors;
Storage means for storing one or more programs,
When executed by the one or more processors, causes the one or more processors to implement the method of any of claims 1-6.
14. A computer readable medium, on which a computer program is stored, characterized in that the program, when being executed by a processor, implements the method according to any of claims 1-6.
CN201810264130.4A 2018-03-28 2018-03-28 SQL sentence processing method and device Active CN110555030B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201810264130.4A CN110555030B (en) 2018-03-28 2018-03-28 SQL sentence processing method and device

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201810264130.4A CN110555030B (en) 2018-03-28 2018-03-28 SQL sentence processing method and device

Publications (2)

Publication Number Publication Date
CN110555030A CN110555030A (en) 2019-12-10
CN110555030B true CN110555030B (en) 2024-06-18

Family

ID=68733891

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201810264130.4A Active CN110555030B (en) 2018-03-28 2018-03-28 SQL sentence processing method and device

Country Status (1)

Country Link
CN (1) CN110555030B (en)

Families Citing this family (8)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN111949491B (en) * 2020-08-14 2023-10-27 中国工商银行股份有限公司 SQL extraction method and device of MyBatis application program
CN112597004A (en) * 2020-12-11 2021-04-02 广州品唯软件有限公司 SQL statement performance test method and device, computer equipment and storage medium
CN112597169A (en) * 2020-12-30 2021-04-02 山东恒远智能科技有限公司 Method for realizing industrial software multi-tenant data isolation
CN113111079A (en) * 2021-04-13 2021-07-13 北京沃东天骏信息技术有限公司 Database execution statement generation method and device
CN112988787B (en) * 2021-05-17 2021-07-20 太平金融科技服务(上海)有限公司深圳分公司 Database data processing method and device, computer equipment and storage medium
CN113821531B (en) * 2021-09-28 2024-06-18 山东舜网传媒股份有限公司 Method, system and equipment for isolating fused media multi-tenant data
CN114896273B (en) * 2022-07-12 2022-11-01 苏州青颖飞帆软件科技有限公司 SQL statement configuration method and equipment compatible with storage process and storage medium
CN116756727B (en) * 2023-08-22 2023-12-29 浙江华东工程数字技术有限公司 Data authority control method and device, electronic equipment and storage medium

Citations (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN1881208A (en) * 2005-06-14 2006-12-20 联想(北京)有限公司 Construction method for dynamic structured query language statement

Family Cites Families (12)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20090259683A1 (en) * 2008-04-14 2009-10-15 Fiberlink Communications Corporation System and method for business object modeling
CN101788992A (en) * 2009-05-06 2010-07-28 厦门东南融通***工程有限公司 Method and system for converting query sentence of database
CN102467421B (en) * 2010-11-19 2014-04-16 深圳市金蝶友商电子商务服务有限公司 Tenant-data-based processing method and computer
CN102737020B (en) * 2011-03-31 2014-08-27 国际商业机器公司 Method for initializing multi-tenant database, and apparatus thereof
JP2013025446A (en) * 2011-07-19 2013-02-04 Canon Marketing Japan Inc Information processing apparatus, information processing system, information processing method, program and recording medium
US9305073B1 (en) * 2012-05-24 2016-04-05 Luminix, Inc. Systems and methods for facilitation communications among customer relationship management users
CN103399942B (en) * 2013-08-14 2016-06-29 山大地纬软件股份有限公司 A kind of data engine system supporting the many tenants of SaaS and method of work thereof
CN104462161A (en) * 2013-10-18 2015-03-25 上海宝信软件股份有限公司 Structural data query method based on distributed database
CN107122364B (en) * 2016-02-25 2021-05-18 华为技术有限公司 Data operation method and data management server
CN106055582B (en) * 2016-05-20 2019-09-24 中国农业银行股份有限公司 A kind of method and device of the table name in replacement data library
CN106775770B (en) * 2017-01-16 2020-08-11 兴唐通信科技有限公司 Search method for constructing query statement based on class information
CN107315790B (en) * 2017-06-14 2021-07-06 腾讯科技(深圳)有限公司 Optimization method and device for non-relevant sub-queries

Patent Citations (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN1881208A (en) * 2005-06-14 2006-12-20 联想(北京)有限公司 Construction method for dynamic structured query language statement

Also Published As

Publication number Publication date
CN110555030A (en) 2019-12-10

Similar Documents

Publication Publication Date Title
CN110555030B (en) SQL sentence processing method and device
CN110324169B (en) Interface management method and device
CN111125064B (en) Method and device for generating database schema definition statement
CN111666293A (en) Database access method and device
CN113238740B (en) Code generation method, code generation device, storage medium and electronic device
CN113760948A (en) Data query method and device
CN113641700A (en) Data processing method and device based on Spring boot frame
CN113419789A (en) Method and device for generating data model script
CN108959294B (en) Method and device for accessing search engine
CN112988583A (en) Method and device for testing syntax compatibility of database
CN113760961A (en) Data query method and device
CN116775613A (en) Data migration method, device, electronic equipment and computer readable medium
CN112579151A (en) Method and device for generating model file
CN112100168A (en) Method and device for determining data association relationship
CN113760240B (en) Method and device for generating data model
CN111026629A (en) Method and device for automatically generating test script
CN114625373A (en) Application conversion method and device, electronic equipment and storage medium
CN113760969A (en) Data query method and device based on elastic search
CN113515285A (en) Method and device for generating real-time calculation logic data
CN113111079A (en) Database execution statement generation method and device
CN113779018A (en) Data processing method and device
CN112783615A (en) Method and device for cleaning data processing task
CN110727739B (en) Data storage method and device
CN113495747B (en) Gray scale release method and device
CN113268417B (en) Task execution method and device

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
GR01 Patent grant