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