最近為了 寫一個分布式的數據組件構想了很多的方案,最近一個簡單易行的方案終於在腦袋里成型。昨晚想到凌晨1點多,發現方案雖簡單,但所有的數據庫工具就不能使用了 。除非自己寫一下查詢分析器來執行程序員自己的維護語句。
說做就做,事情也出乎順利,居然半天時間做了一個基本的版本出來了:)
於是就想想能否加上智能提示字段。似乎難在分析程序員錄入的語法。當然說白了也簡單就是取出表的別名。家里的空調沒錢換,為了省100大元,還要晚幾天才有得用。於是今晚繼續晚點睡,把這個正則表達式弄出來,明天上班就能繼續開發查詢分析器了。
事情也沒有想象的復雜,不到半小時,正則表達式整理出來了。利用下面的兩個正則應就能分析出語法中的表名和其別名
\s+from\s+(\w+)\s+(\w+)\s+(where|left|join|inner)
\s+join\s+(\w+)\s+(\w+)\s+on
為了測試方便我使用了Combox來保存整理出來的表達式,於是取所有表和別名的代碼是這樣的
DataTable table = new DataTable(); table.Columns.Add("tableName"); table.Columns.Add("aliasName"); foreach (string str in this.comboBox1.Items) { Regex reg = new Regex(str); MatchCollection mces = reg.Matches(this.richTextBox1.Text); foreach (Match mc in mces) { 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;
以下是我用於測試的sql
select * from Outvisit l left join patient p on l.patid=p.patientid join patstatic c on l.patid=c.patid inner join patphone ph on l.patid=ph.patid where l.name='kevin' and exsits(select 1 from pharmacywestpas p where p.outvisitid=l.outvisitid) unit all select * from invisit v where
最后是我測試的小程序的截圖