现在的位置: 首页 > 综合 > 正文

最新面向对象查询语言:NQL

2013年10月31日 ⁄ 综合 ⁄ 共 4360字 ⁄ 字号 评论关闭

    面 向 对 象 查 询 语 言:NQL

newxy(新坐标)技术运用之七

作者:胡立新

      net.newxy.dbm.NQL 是 newxy(新坐标)的面向对象的查询类。他以标准的sql语言为基础,开发者不需要学习新的语法规则。不需要在查询语句中镶入值对象类名。通过add()方法加入新的查询条件,通过and()、addAnd()方法及or()、addOr()方法设置逻辑关系。利用多态特性,控制查询范围。

newxy(新坐标)处理多项查询时采用了NQL技术。

以下是 NQL 类的几个构造方法

  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文件中设置的DAO类别名。例如
    dao.test=common.TestDao
    参数dao就可以是"dao.test"。
    _sql是最初sql语句。

  3.     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"。

  1. 类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)

  2. 类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 '%餐饮%'))

  3. 类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')))

  4. 类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')))

  5. 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();
            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 '%批发%' or b.dealIn like '%餐饮%') or (b.industry_code='D'))
        and (b.name like '%公司%' or 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。
newxy(新坐标)技术网站:http://www.newxy.net
 

抱歉!评论已关闭.