Xsqlbuilder can be used to dynamically construct SQL statements to avoid excessive if judgment when constructing SQL statements. It is integrated with safesqlprocesser to prevent SQL injection attacks and integrate with datamodifier to convert data types.
Dynamic SQL Construction Example:
Java code
- String XSQL = "select * from user where 1 = 1
- /~ And username = {username }~ /
- /~ And Password = {password }~ /
- /~ And age = [age] ~ /
- /~ And sex = [sex] ~ /"
-
- Map filters = new hashmap ();
- Filters. Put ("username", "badqiu ");
- Filters. Put ("Age", "12 ");
- Filters. Put ("sex ","");
-
- Xsqlfilterresult result = xsqlbuilder. applyfilters (XSQL, filters );
String xsql = "select * from user where 1=1 /~ and username = {username} ~/ /~ and password = {password} ~/ /~ and age = [age] ~/ /~ and sex = [sex] ~/" Map filters = new HashMap(); filters.put("username", "badqiu"); filters.put("age", "12"); filters.put("sex", ""); XsqlFilterResult result = xsqlBuilder.applyFilters(xsql,filters);
Construct the generated result. getxsql () will be equal
SQL code
- Select * fromuserwhere 1 = 1 and username = {username} andage = 12
select * from user where 1=1 and username={username} and age=12
Filtered and deleted segments:/~ And Password = {password }~ /This section is not constructed because the password does not exist in filters /~ And sex = [sex] ~ /Because the sex value is empty, it is not constructed.
Last result. acceptedfilters Value
Java code
- Map acceptedfilters = result. getacceptedfilters ();
- Will be equal:
- {Username = badqiu}
Map acceptedfilters = result. getacceptedfilters (); will be equal to: {username = badqiu}
Introduction to related symbols:
/~ Segment ...~ /Is the Key marked by the condition code block {key} filter, which can be replaced with SQL ?, Or the username mark [Key] of hql will be replaced with the key value directly.
Data type conversion example:
Select * from user where and 1 = 1 /~ Age = {age? Int }~ /Converts the value of key = age in map filters to the int type.
Project address: http://code.google.com/p/rapid-xsqlbuilder/
: Http://rapid-xsqlbuilder.googlecode.com/files/xsqlbuilder-1.0.zip