Search in sources :

Example 1 with AssocCase

use of io.github.ihongs.db.util.AssocCase in project HongsCORE by ihongs.

the class Model method filter.

/**
 * 查询过滤
 *
 * <pre>
 * 作用于getPage,getList上
 *
 * 如需添加过滤条件, 请重写此方法;
 * 注意: 此处需要类似引用参数, 故调用前请务必实例化req和caze;
 * 默认仅关联join类型为LEFT,INNER和link类型为BLS_TO,HAS_ONE的表,
 * 如需指定关联的表请设置FetchCase的option: ASSOCS,
 * 如需指定关联方式请设置FetchCase的option: ASSOC_JOINS, ASSOC_TYEPS
 *
 * 设计目标:
 * 1) 按照rb参数设置查询字段;
 *    限定字段列表: rb=a+b+c或rb=a!+x.b, !表示排除该字段;
 * 2) 按照ob参数设置排序方式,
 *    多个字段排序: ob=a+b+c或ob=a!+b+c, !表示该字段逆序;
 * 3) 按照wd参数设置模糊查询,
 *    多关键词搜索: wd=x+y+z;
 *    指定字段搜索: a.cq=x或a.b.cq=y, 同样也适用上述规则,
 *    a.b为搜索关联表, 注意: a,a.b必须在srchable中有指定;
 * 4) 如果有字段名相同的参数则获取与之对应的记录,
 *    可以在字段名后跟.加上.gt,.lt,.ge,.le,.ne分别表示&gt;,&lt;,&ge;,&le;,&ne;
 * 5) 如果有子表.字段名相同的参数则获取与之对应的记录,
 *    可以在子表.字段名后跟.加上.gt,.lt,.ge,.le,.ne分别表示&gt;,&lt;,&ge;,&le;,&ne;
 * 注: "+" 在URL中表示空格; 以上设计目录均已实现; 以上1/2/3中的参数名可统一设置或单独指定;
 *
 * [2016/9/4] 以上过滤逻辑已移至 AssocCase, 但未指定 listable 时字段过滤使用本类的 field,allow 来处理
 * </pre>
 *
 * @param caze
 * @param rd
 * @throws io.github.ihongs.HongsException
 */
protected void filter(FetchCase caze, Map rd) throws HongsException {
    /**
     * 如果没指定查询的表、字段
     * 默认只关联 BLS_TO , HAS_ONE 的表(仅能关联一个)
     * 默认只连接 LEFT,INNER,RIGHT 的表(常规关联均可)
     */
    if (null == caze.getOption("ASSOCS") && ("getAll".equals(caze.getOption("MODEL_START")) || "getList".equals(caze.getOption("MODEL_START")))) {
        if (null == caze.getOption("ASSOC_TYPES")) {
            Set types = new HashSet();
            types.add("BLS_TO");
            types.add("HAS_ONE");
            caze.setOption("ASSOC_TYPES", types);
        }
        if (null == caze.getOption("ASSOC_JOINS")) {
            Set types = new HashSet();
            types.add("INNER");
            types.add("LEFT");
            types.add("RIGHT");
            types.add("FULL");
            caze.setOption("ASSOC_JOINS", types);
        }
    }
    /**
     * 补全空白关联数据
     * 避免外部解析麻烦
     */
    caze.setOption("ASSOC_FILLS", true);
    if (rd == null || rd.isEmpty()) {
        return;
    }
    /**
     * 如果不是 table.fetchCase( ) 构建查询
     * 在下面组织过滤等时可能导致表别名为空
     */
    caze.from(table.tableName, table.name);
    /**
     * 没有指定 listable
     * 则不使用 AssocCase  来处理查询的字段
     * 此方法并不会将所有字段绑定在顶层用例
     * 而是按照不同表分别绑在对应下级查询上
     * 这可为非 JOIN 关联的用例指定查询字段
     */
    Object rb = null;
    if (!caze.hasField() && !table.getParams().containsKey("listable")) {
        rb = rd.remove(Cnst.RB_KEY);
        Set<String> sb = Synt.toTerms(rb);
        if (sb != null && !sb.isEmpty()) {
            field(caze, sb);
        }
    }
    // 后续调试可正常观察
    try {
        AssocCase uc = new AssocCase(caze);
        uc.allow(this);
        uc.parse(rd);
    } finally {
        if (rb != null) {
            rd.put(Cnst.RB_KEY, rb);
        }
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) AssocCase(io.github.ihongs.db.util.AssocCase) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 2 with AssocCase

use of io.github.ihongs.db.util.AssocCase in project HongsCORE by ihongs.

the class TestDB method testAssocCase.

@Test
public void testAssocCase() throws HongsException {
    FetchCase fc = new FetchCase();
    AssocCase ac = new AssocCase(fc);
    fc.from("a_table", "table1").join("b_table", "table2", "table1.id=table2.a_id");
    ac.allow(AssocCase.LISTABLE, "id", "name", "ctime", "mtime", "state", "t2_id:table2.id", "t2_name:table2.name");
    ac.allow(AssocCase.UNSTORED, "state");
    ac.parse(Synt.mapOf("id", Synt.setOf("1", "2", "3"), "rb", "*,table2.name", "ob", "ctime!,state", "state", Synt.mapOf("gt", 1)));
    assertEquals(fc.toString(), "SELECT `table1`.`id` AS `id`, `table1`.`name` AS `name`, `table1`.`ctime` AS `ctime`, `table1`.`mtime` AS `mtime`, `table1`.`state` AS `state`, table2.id AS `t2_id`, table2.name AS `t2_name` FROM `a_table` AS `table1` INNER JOIN `b_table` AS `table2` ON table1.id=table2.a_id WHERE `table1`.`id` IN ('1','2','3') AND `table1`.`state` > 1 ORDER BY `table1`.`ctime` DESC, `table1`.`state`");
    // System.out.println(fc);
    Map ss = ac.saves(Synt.mapOf("id", "123", "name", "hh", "note", "nn", "state", 6));
    Map zz = new HashMap(Synt.mapOf("id", "123", "name", "hh"));
    assertEquals(ss.toString(), zz.toString());
// System.out.println(ss);
}
Also used : FetchCase(io.github.ihongs.db.util.FetchCase) AssocCase(io.github.ihongs.db.util.AssocCase) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

AssocCase (io.github.ihongs.db.util.AssocCase)2 FetchCase (io.github.ihongs.db.util.FetchCase)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 Test (org.junit.Test)1