Regular expressions that parse all table names and their aliases for SQL statements

Source: Internet
Author: User

Recently, in order to write a distributed data component to conceive a lot of scenarios, a simple and easy solution has finally formed in the head. Thinking about 1 o'clock in the morning last night, I found that the solution was simple, but all the database tools could not be used. Unless you write your own Query Analyzer to execute the programmer's own maintenance statements.

Say do it, things go well, incredibly half a day to do a basic version came out:)

So just think about whether you can add smart hint fields. It seems difficult to analyze the syntax of programmer input. Of course, it is simple to remove the table alias. Home Air-conditioning no money to change, in order to save 100 yuan, but also a few days later to be used. So I'm going to sleep late tonight and get this regular expression out of the way, and I'll be able to continue developing the Query Analyzer tomorrow.

Things are not as complicated as they are imagined, and in less than half an hour, regular expressions are sorted out. The following two regular rules should be used to parse the table names and their aliases in the syntax.

\s+from\s+ (\w+) \s+ (\w+) \s+ (Where|left|join|inner) \s+join\s+ (\w+) \s+ (\w+) \s+on

For testing convenience I used the Combox to save the sorted expression, so the code for all tables and aliases is like this

DataTable table =NewDataTable (); Table. Columns.Add ("TableName"); Table. Columns.Add ("aliasname"); foreach(stringStrinch  This. Combobox1.items) {Regex reg=NewRegex (str); MatchCollection mces= Reg. Matches ( This. richTextBox1.Text); foreach(Match MCinchmces) {DataRow row=table.                    NewRow (); row["TableName"] = MC. groups[1].                    Value; row["aliasname"] = MC. groups[2].                    Value; Table.                Rows.Add (row); }            }             This. Datagridview1.datasource = table. DefaultView;

Here's the SQL I used to test

Select *  fromoutvisit L Left JoinPatient P onL.patid=P.patientidJoinPatstatic C onL.patid=C.patidInner JoinPatphone ph onL.patid=Ph.patidwhereL.name='Kevin'  andExsits (Select 1  fromPharmacywestpas PwhereP.outvisitid=l.outvisitid) Unit AllSelect *  fromInvisit Vwhere

And finally the little program I tested.

Regular expressions that parse all table names and their aliases for SQL statements

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.