Xsqlfilterresult -- dynamically generate the SQL statement class to filter NULL query conditions.

Source: Internet
Author: User

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
  1. String XSQL = "select * from user where 1 = 1
  2. /~ And username = {username }~ /
  3. /~ And Password = {password }~ /
  4. /~ And age = [age] ~ /
  5. /~ And sex = [sex] ~ /"
  6.  
  7. Map filters = new hashmap ();
  8. Filters. Put ("username", "badqiu ");
  9. Filters. Put ("Age", "12 ");
  10. Filters. Put ("sex ","");
  11.  
  12. 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
  1. 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
  1. Map acceptedfilters = result. getacceptedfilters ();
  2. Will be equal:
  3. {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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.