The regular expression extracts the @ Parameter Name of the SQL statement and fixes a bug in subsonic.

Source: Internet
Author: User

Subsonic inlinequery is very easy to use. It is used to execute SQL statements of parameters to prevent injection. Its Running Mechanism is similar to the borrow statement I wrote earlier. net Framework string. fromat (...), to implement a method for executing parameterized SQL statements, the command is first created based on the parameter name in the SQL statement, and then dynamically constructed based on the parameter name and input value.

Paste an official example of using inlinequery:

Northwind. productcollection Products =
New Inlinequery ()
. Executeascollection < Northwind. productcollection >
( " Select productid from products where productid = @ productid " , 1 );

 

 

When I use inlinequery to execute an SQL statement with parameters, I found a bug in subsonic's analysis of SQL parameters and tracked it into subsonic'sSource codeIt is found that the parameters for parsing SQL statements are parsed as follows:

  private   static  List  string  parseparameters ( string  SQL) {list  string  result =  New  List  string  (); regEx paramreg =  New  RegEx (@ " @ \ W * "); matchcollection matches = paramreg. matches (string. concat (SQL, " ");  foreach  (Match m  in  matches) result. add (M. value);  return  result ;}
See the third line, RegEx paramreg =NewRegEx (@"@ \ W *"), Its other mechanism is to use regular expressions for grouping and matching, for example, the following SQL statement:
 
Select * from students where fname = @ name; select @ rowcount
 
Variables such as @ rowcount are extracted during extraction,

Obviously this is not what we want. The result we want is to extract only the @ name parameter name, which obviously cannot meet the requirements, actually, the expression @ "@ \ W *" is too hasty to handle. Fortunately, the source code of open-source products is well modified, and it would take 10 minutes (it's really embarrassing, I haven't used this for a long time.) Write a regular expression: ([^ @] (? <P> @ \ W +) | (? <P> ^ @ \ W +)

Last ModifiedCodeAs follows:

 Private   Static List < String > Parseparameters ( String SQL) {list < String > Result =New List < String > (); // RegEx paramreg = new RegEx (@ "@ \ W *");  // When the regular expression matches a parameter, the SQL statement contains a variable such as @ rowcount, which should not be counted as a parameter. RegEx paramreg = New RegEx (@" [^ @] (? <P> @ \ W +) "); Matchcollection matches = paramreg. Matches (string. Concat (SQL ,"   ")); Foreach (Match m In Matches) result. Add (M. Groups [" P "]. Value ); Return Result ;}
 
 
 
If you still have a simple method, please leave a message to inform me. Thank you.

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.