隨筆-39  評論-33  文章-0  trackbacks-0

          ??? 面 向 對 象 查 詢 語 言:NQL

          newxy(新坐標(biāo)) 技術(shù)運(yùn)用之七

          作者:胡立新

          ??????net.newxy.dbm.NQL 是 newxy(新坐標(biāo))的面向?qū)ο蟮牟樵冾悺K詷?biāo)準(zhǔn)的sql語言為基礎(chǔ),開發(fā)者不需要學(xué)習(xí)新的語法規(guī)則。不需要在查詢語句中鑲?cè)胫祵ο箢惷Mㄟ^add()方法加入新的查詢條件,通過and()、addAnd()方法及or()、addOr()方法設(shè)置邏輯關(guān)系。利用多態(tài)特性,控制查詢范圍。

          newxy(新坐標(biāo))處理多項(xiàng)查詢時(shí)采用了NQL技術(shù)。

          以下是 NQL 類的幾個(gè)構(gòu)造方法

          1.     public NQL(IFacade ifacade,String _sql)throws Exception{
                    this.ifacade=ifacade;
                    this._sql=_sql;
                    initial();
                }
                
            ifacade是net.newxy.dbm.DBM及其子類的接口。_sql是最初sql語句。
          2.     public NQL(String dao,String _sql)throws Exception{
                    this.dao=dao;
                    this._sql=_sql;
                    initial();
                }
                

            dao 是src/下newxy.properties文件中設(shè)置的DAO類別名。例如
            dao.test=common.TestDao
            參數(shù)dao就可以是"dao.test"。
            _sql是最初sql語句。

          3.     public NQL(String _sql)throws Exception{
                    this._sql=_sql;
                    initial();
                }
             這個(gè)構(gòu)造函數(shù)調(diào)用的initial()方法會用默認(rèn)DAO類的實(shí)例賦給NQL類變量ifacade。_sql是最初sql語句。 

          應(yīng)用舉例

          下面舉幾個(gè)例子。類NQL1、NQL2、NQL3、NQL4之間有遞次繼承關(guān)系。NQL1繼承自net.newxy.dbm.NQL類。

          NQL1 以"select b.* from industry as a,enterprise as b where{a.code=b.industry_code}"作為 構(gòu)造函數(shù)參數(shù)。查詢得企業(yè)表enterprise中所有數(shù)據(jù)。
          NQL2 繼承 NQL1,在NQL1的基礎(chǔ)上加以限制,查詢結(jié)果企業(yè)的經(jīng)營范圍包含"批發(fā)"或"餐飲"
          NQL3 繼承 NQL2,在NQL2的基礎(chǔ)上以加擴(kuò)張,使查詢結(jié)果企業(yè)也可以是行業(yè)代碼等于"D"。
          NQL4 繼承 NQL3,在NQL3的基礎(chǔ)上加限制,使查詢結(jié)果在NQL3的基礎(chǔ)上,使企業(yè)名稱必需包含"公司"或行業(yè)代碼等于"A"。

          1. 類NQL1,定義及運(yùn)用
            定義
            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}");
                }
            }
                

            注意:作為參數(shù)的查詢語句中應(yīng)有where{},用的是大括號,而不是小括號,表明這里是動態(tài)生成查詢條件的地方。還有種形式是:select * from enterprise where{}

            運(yùn)用,查詢得企業(yè)表enterprise中所有數(shù)據(jù)
                  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"));
                  }
              
            產(chǎn)生的SQL語句是:select b.* from industry as a,enterprise as b where (a.code=b.industry_code)
          2. 類NQL2,定義及運(yùn)用
            定義
            package common;
            public class NQL2 extends NQL1{
                public NQL2() throws Exception{
                    super();
                    and();
                    addOr();
                    add("b.dealIn like '%批發(fā)%'");
                    add("b.dealIn like '%餐飲%'");
                    setWhere();
                }
            }
              
            運(yùn)用,在NQL1的基礎(chǔ)上加以限制,查詢結(jié)果企業(yè)的經(jīng)營范圍包含"批發(fā)"或"餐飲"
                  NQL nql=new NQL2();
                  List list=nql.list();
              
            產(chǎn)生的SQL語句是:select b.* from industry as a,enterprise as b where ((a.code=b.industry_code) and (b.dealIn like '%批發(fā)%' or b.dealIn like '%餐飲%'))
          3. 類NQL3,定義及運(yùn)用
            定義
            package common;
            
            public class NQL3 extends NQL2{
                public NQL3() throws Exception{
                    super();
                    or();
                    add("b","industry_code","D");
                    setWhere();
                }
            }
              
            運(yùn)用,在NQL2的基礎(chǔ)上加以擴(kuò)張,使查詢結(jié)果企業(yè)可以是行業(yè)代碼等于"D"
                  NQL nql=new NQL3();
                  List list=nql.list();
              
            產(chǎn)生的SQL語句是:select b.* from industry as a,enterprise as b where ((a.code=b.industry_code) and ((b.dealIn like '%批發(fā)%' or b.dealIn like '%餐飲%') or (b.industry_code='D')))
          4. 類NQL4,定義及運(yùn)用
            定義
            public class NQL4 extends NQL3{
                public NQL4() throws Exception{
                    super();
                    and();
                    addOr();
                    add("b.name like '%加工%'");
                    add("b","industry_code","A");
                    setWhere();
                }
            }
              
            運(yùn)用,在NQL3的基礎(chǔ)上加限制,使查詢結(jié)果在NQL3的基礎(chǔ)上,使企業(yè)名稱必需包含"公司"或行業(yè)代碼等于"A"。
                  NQL nql=new NQL4();
                  List list=nql.list();
              
            產(chǎn)生的SQL語句是:
            select b.* from industry as a,enterprise as b where ((a.code=b.industry_code) and (((b.dealIn like '%批發(fā)%' or b.dealIn like '%餐飲%') or (b.industry_code='D')) and (b.name like '%公司%' or b.industry_code='A')))
          5. NQL4產(chǎn)生的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 '%批發(fā)%'");
                        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
              
            產(chǎn)生的SQL語句是:
            select b.* from industry as a,enterprise as b where ((a.code=b.industry_code) and (((b.dealIn like '%批發(fā)%' or b.dealIn like '%餐飲%') or (b.industry_code='D')) and (b.name like '%公司%' or b.industry_code='A')))

          注:
          1.紅色條件是構(gòu)造NQL實(shí)例時(shí)的基本條件,它與后來產(chǎn)生的條件始終是"and "關(guān)系。
          2.setWhere()方法會將當(dāng)前附加的條件與先前條件結(jié)合構(gòu)成新的條件。前后條件之間是"and"還是"or"由 and()、or()方法決定。 當(dāng)前附加各條件之間是"and"還是"or"關(guān)系則由addAnd()、addOr()方法決定。可參看NQL4的構(gòu)造方法及產(chǎn)生的sql語句(注意黑色部分):

                  public NQL4() throws Exception{
                      super();
                      and();addOr();
                      add("b.name like '%公司%'");
                      add("b","industry_code","A");
                      setWhere();
                  }
                  select b.* from industry as a,enterprise as b where ((a.code=b.industry_code) and
                  (((b.dealIn like '%批發(fā)%' or b.dealIn like '%餐飲%') or (b.industry_code='D'))
                  and (b.name like '%公司%' or b.industry_code='A')))
             
          3.如果NQL3的構(gòu)造函數(shù)中不包含setWhere();則NQL4產(chǎn)生的sql語句如下:
          ????select b.* from industry as a,enterprise as b where ((a.code=b.industry_code) and ((b.dealIn like '%批發(fā)%' or b.dealIn like '%餐飲%') and (b.industry_code='D' or b.name like '%公司%' or b.industry_code='A')))
          ????NQL3構(gòu)造函數(shù)中 add("b","industry_code","D")加入條件會與NQL4構(gòu)造函數(shù)中add("b.name like '%公司%'")、add("b","industry_code","A")加入的條件一同解析,放入同一括號中。結(jié)果SQL語名包含的是
          ???? and (b.industry_code='D' or b.name like '%公司%' or b.industry_code='A')
          所以NQL類的子類構(gòu)造函數(shù)應(yīng)是如下形式
          super();or();//或者 and();addAnd();//或者 addOr();
                  add("b.name like '%公司%'");
                  add("b","industry_code","D");
                  setWhere();
          
                  否則就不用繼承,直接用NQL。
          newxy(新坐標(biāo))技術(shù)網(wǎng)站:http://www.newxy.net
          posted on 2006-07-25 02:27 newxy新坐標(biāo) 閱讀(206) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          <2006年7月>
          2526272829301
          2345678
          9101112131415
          16171819202122
          23242526272829
          303112345

          常用鏈接

          留言簿(4)

          隨筆檔案(39)

          文章分類

          GIS技術(shù)及運(yùn)用

          struts

          實(shí)用代碼

          報(bào)表

          軟件工程

          搜索

          •  

          積分與排名

          • 積分 - 44075
          • 排名 - 1090

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 大悟县| 汝州市| 舟山市| 紫云| 礼泉县| 武宣县| 霍林郭勒市| 五大连池市| 遂宁市| 应用必备| 新沂市| 句容市| 襄城县| 沙雅县| 扶余县| 勃利县| 尚志市| 鄂尔多斯市| 卢湾区| 唐山市| 兰溪市| 渑池县| 奉贤区| 交口县| 松江区| 琼结县| 施秉县| 布拖县| 汶上县| 天台县| 那曲县| 沙洋县| 墨竹工卡县| 吴川市| 孟连| 肇庆市| 台江县| 芒康县| 洛南县| 志丹县| 马龙县|