面 向 對 象 查 詢 語 言:NQL
newxy(新座標)技術運用之七
作者:胡立新
net.newxy.dbm.NQL 是 newxy(新座標)的面向對象的查詢類。他以標準的sql語言爲基礎,開發者不需要學習新的語法規則。不需要在查詢語句中鑲入值對象類名。通過add()方法加入新的查詢條件,通過and()、addAnd()方法及or()、addOr()方法設置邏輯關係。利用多態特性,控制查詢範圍。
newxy(新座標)處理多項查詢時採用了NQL技術。
以下是 NQL 類的幾個構造方法
-
public NQL(IFacade ifacade,String _sql)throws Exception{ this.ifacade=ifacade; this._sql=_sql; initial(); }
ifacade是net.newxy.dbm.DBM及其子類的接口。_sql是最初sql語句。 -
public NQL(String dao,String _sql)throws Exception{ this.dao=dao; this._sql=_sql; initial(); }
dao 是src/下newxy.properties文件中設置的DAO類別名。例如
dao.test=common.TestDao
參數dao就可以是"dao.test"。
_sql是最初sql語句。 -
public NQL(String _sql)throws Exception{ this._sql=_sql; initial(); }
這個構造函數調用的initial()方法會用默認DAO類的實例賦給NQL類變量ifacade。_sql是最初sql語句。
應用舉例
下面舉幾個例子。類NQL1、NQL2、NQL3、NQL4之間有遞次繼承關係。NQL1繼承自net.newxy.dbm.NQL類。
NQL1 以"select b.* from industry as a,enterprise as b where{a.code=b.industry_code}"作爲 構造函數參數。查詢得企業表enterprise中所有數據。
NQL2 繼承 NQL1,在NQL1的基礎上加以限制,查詢結果企業的經營範圍包含"批發"或"餐飲"
NQL3 繼承 NQL2,在NQL2的基礎上以加擴張,使查詢結果企業也可以是行業代碼等於"D"。
NQL4 繼承 NQL3,在NQL3的基礎上加限制,使查詢結果在NQL3的基礎上,使企業名稱必需包含"公司"或行業代碼等於"A"。
- 類NQL1,定義及運用
定義package common; import net.newxy.dbm.NQL; public class NQL1 extends NQL{ public NQL1()throws Exception{ super("select b.* from industry as a,enterprise as b where{a.code=b.industry_code}"); } }
注意:作爲參數的查詢語句中應有where{},用的是大括號,而不是小括號,表明這裏是動態生成查詢條件的地方。還有種形式是:select * from enterprise where{}
運用,查詢得企業表enterprise中所有數據NQL nql=new NQL1(); List list=nql.list(); for (int i = 0; i < list.size(); i++) { DynaBean bean = (DynaBean) list.get(i); System.out.println(bean.get("name")+" "+bean.get("dealIn")); }
產生的SQL語句是:select b.* from industry as a,enterprise as b where (a.code=b.industry_code) - 類NQL2,定義及運用
定義package common; public class NQL2 extends NQL1{ public NQL2() throws Exception{ super(); and(); addOr(); add("b.dealIn like '%批發%'"); add("b.dealIn like '%餐飲%'"); setWhere(); } }
運用,在NQL1的基礎上加以限制,查詢結果企業的經營範圍包含"批發"或"餐飲"NQL nql=new NQL2(); List list=nql.list();
產生的SQL語句是:select b.* from industry as a,enterprise as b where ((a.code=b.industry_code) and (b.dealIn like '%批發%' or b.dealIn like '%餐飲%')) - 類NQL3,定義及運用
定義package common; public class NQL3 extends NQL2{ public NQL3() throws Exception{ super(); or(); add("b","industry_code","D"); setWhere(); } }
運用,在NQL2的基礎上加以擴張,使查詢結果企業可以是行業代碼等於"D"NQL nql=new NQL3(); List list=nql.list();
產生的SQL語句是:select b.* from industry as a,enterprise as b where ((a.code=b.industry_code) and ((b.dealIn like '%批發%' or b.dealIn like '%餐飲%') or (b.industry_code='D'))) - 類NQL4,定義及運用
定義public class NQL4 extends NQL3{ public NQL4() throws Exception{ super(); and(); addOr(); add("b.name like '%加工%'"); add("b","industry_code","A"); setWhere(); } }
運用,在NQL3的基礎上加限制,使查詢結果在NQL3的基礎上,使企業名稱必需包含"公司"或行業代碼等於"A"。NQL nql=new NQL4(); List list=nql.list();
產生的SQL語句是:
select b.* from industry as a,enterprise as b where ((a.code=b.industry_code) and (((b.dealIn like '%批發%' or b.dealIn like '%餐飲%') or (b.industry_code='D')) and (b.name like '%公司%' or b.industry_code='A'))) - NQL4產生的sql語句也可由直接得到,方法如下:
NQL nql = new NQL("select b.* from industry as a,enterprise as b where{a.code=b.industry_code}"); nql.and(); nql.addOr(); nql.add("b.dealIn like '%批發%'"); nql.add("b.dealIn like '%餐飲%'"); nql.setWhere(); nql.or(); nql.add("b","industry_code","D"); nql.setWhere(); nql.and(); nql.addOr(); nql.add("b.name like '%公司%'"); nql.add("b","industry_code","A"); nql.setWhere
產生的SQL語句是:
select b.* from industry as a,enterprise as b where ((a.code=b.industry_code) and (((b.dealIn like '%批發%' or b.dealIn like '%餐飲%') or (b.industry_code='D')) and (b.name like '%公司%' or b.industry_code='A')))
注:
1.紅色條件是構造NQL實例時的基本條件,它與後來產生的條件始終是"and "關係。
2.setWhere()方法會將當前附加的條件與先前條件結合構成新的條件。前後條件之間是"and"還是"or"由 and()、or()方法決定。 當前附加各條件之間是"and"還是"or"關係則由addAnd()、addOr()方法決定。可參看NQL4的構造方法及產生的sql語句(注意黑色部分):
public NQL4() throws Exception{ super(); add("b.name like '%公司%'"); add("b","industry_code","A"); setWhere(); } select b.* from industry as a,enterprise as b where (() (((b.dealIn like '%批發%' or b.dealIn like '%餐飲%') or (b.industry_code='D')) (b.name like '%公司%' b.industry_code='A')))3.如果NQL3的構造函數中不包含setWhere();則NQL4產生的sql語句如下:
select b.* from industry as a,enterprise as b where ((a.code=b.industry_code) and ((b.dealIn like '%批發%' or b.dealIn like '%餐飲%') and (b.industry_code='D' or b.name like '%公司%' or b.industry_code='A')))
NQL3構造函數中 add("b","industry_code","D")加入條件會與NQL4構造函數中add("b.name like '%公司%'")、add("b","industry_code","A")加入的條件一同解析,放入同一括號中。結果SQL語名包含的是
and (b.industry_code='D' or b.name like '%公司%' or b.industry_code='A')
所以NQL類的子類構造函數應是如下形式
super(); or();//或者 and(); addAnd();//或者 addOr(); add("b.name like '%公司%'"); add("b","industry_code","D"); setWhere(); 否則就不用繼承,直接用NQL。