Search in sources :

Example 36 with Pojo

use of org.nutz.dao.sql.Pojo in project nutz by nutzam.

the class NutDao method _count.

private int _count(Entity<?> en, String tableName, Condition cnd) {
    // 如果有条件的话
    if (null != cnd) {
        Pojo pojo = pojoMaker.makeFunc(tableName, "COUNT", "*");
        pojo.setEntity(en);
        // 高级条件接口,直接得到 WHERE 子句
        if (cnd instanceof Criteria) {
            pojo.append(((Criteria) cnd).where());
            // MySQL/PgSQL/SqlServer 与 Oracle/H2的结果会不一样,奇葩啊
            GroupBy gb = ((Criteria) cnd).getGroupBy();
            if (gb != null)
                pojo.append(gb);
        } else // 否则暴力获取 WHERE 子句
        {
            String str = Pojos.formatCondition(en, cnd);
            if (!Strings.isBlank(str)) {
                String[] ss = str.toUpperCase().split("ORDER BY");
                pojo.append(Pojos.Items.wrap(str.substring(0, ss[0].length())));
            }
        }
        // 设置回调,并执行 SQL
        pojo.setAfter(_pojo_fetchInt);
        _exec(pojo);
        return pojo.getInt();
    }
    // 没有条件,直接生成表达式
    return func(tableName, "COUNT", "*");
}
Also used : Pojo(org.nutz.dao.sql.Pojo) GroupBy(org.nutz.dao.sql.GroupBy) Criteria(org.nutz.dao.sql.Criteria)

Example 37 with Pojo

use of org.nutz.dao.sql.Pojo in project nutz by nutzam.

the class Sqlserver2005JdbcExpert method fetchPojoId.

public Pojo fetchPojoId(Entity<?> en, MappingField idField) {
    String autoSql = "SELECT @@@@IDENTITY as $field";
    Pojo autoInfo = new SqlFieldMacro(idField, autoSql);
    autoInfo.setEntity(en);
    return autoInfo;
}
Also used : Pojo(org.nutz.dao.sql.Pojo) SqlFieldMacro(org.nutz.dao.impl.entity.macro.SqlFieldMacro)

Example 38 with Pojo

use of org.nutz.dao.sql.Pojo in project nutz by nutzam.

the class DoClearRelationByLinkedFieldLinkVisitor method visit.

public void visit(Object obj, LinkField lnk) {
    if (lnk instanceof ManyManyLinkField) {
        final ManyManyLinkField mm = (ManyManyLinkField) lnk;
        Object value = mm.getValue(obj);
        if (Lang.length(value) == 0)
            return;
        final Pojo pojo = opt.maker().makeDelete(mm.getRelationName());
        pojo.append(Pojos.Items.cndColumn(mm.getToColumnName(), mm.getLinkedField(), null));
        Lang.each(value, new Each<Object>() {

            public void invoke(int i, Object ele, int length) throws ExitLoop, LoopException {
                pojo.addParamsBy(mm.getLinkedField().getValue(ele));
            }
        });
        opt.add(pojo);
    }
}
Also used : Pojo(org.nutz.dao.sql.Pojo) ExitLoop(org.nutz.lang.ExitLoop) LoopException(org.nutz.lang.LoopException) ManyManyLinkField(org.nutz.dao.impl.entity.field.ManyManyLinkField)

Example 39 with Pojo

use of org.nutz.dao.sql.Pojo in project nutz by nutzam.

the class DoDeleteLinkVisitor method visit.

public void visit(Object obj, LinkField lnk) {
    Object value = lnk.getValue(obj);
    if (value == null || Lang.length(value) == 0) {
        log.infof("Value of LinkField(@%s-->%s.%s) is null or isEmtry, ingore", lnk.getLinkType(), lnk.getEntity().getType().getSimpleName(), lnk.getHostField().getName());
        return;
    }
    final Pojo pojo = opt.maker().makeDelete(lnk.getLinkedEntity());
    pojo.setOperatingObject(value);
    pojo.append(Pojos.Items.cndAuto(lnk.getLinkedEntity(), null));
    Lang.each(value, new Each<Object>() {

        public void invoke(int i, Object ele, int length) throws ExitLoop, LoopException {
            pojo.addParamsBy(ele);
        }
    });
    opt.add(pojo);
}
Also used : Pojo(org.nutz.dao.sql.Pojo) ExitLoop(org.nutz.lang.ExitLoop) LoopException(org.nutz.lang.LoopException)

Example 40 with Pojo

use of org.nutz.dao.sql.Pojo in project nutz by nutzam.

the class AbstractJdbcExpert method fetchPojoId.

public Pojo fetchPojoId(Entity<?> en, MappingField idField) {
    String autoSql = "SELECT MAX($field) AS $field FROM $view";
    Pojo autoInfo = new SqlFieldMacro(idField, autoSql);
    autoInfo.setEntity(en);
    return autoInfo;
}
Also used : Pojo(org.nutz.dao.sql.Pojo) SqlFieldMacro(org.nutz.dao.impl.entity.macro.SqlFieldMacro)

Aggregations

Pojo (org.nutz.dao.sql.Pojo)49 SqlFieldMacro (org.nutz.dao.impl.entity.macro.SqlFieldMacro)6 ExitLoop (org.nutz.lang.ExitLoop)5 LoopException (org.nutz.lang.LoopException)5 ManyManyLinkField (org.nutz.dao.impl.entity.field.ManyManyLinkField)4 ArrayList (java.util.ArrayList)3 Connection (java.sql.Connection)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 Statement (java.sql.Statement)2 Map (java.util.Map)2 DaoException (org.nutz.dao.DaoException)2 LinkField (org.nutz.dao.entity.LinkField)2 LinkVisitor (org.nutz.dao.entity.LinkVisitor)2 DoClearLinkVisitor (org.nutz.dao.impl.link.DoClearLinkVisitor)2 DoClearRelationByHostFieldLinkVisitor (org.nutz.dao.impl.link.DoClearRelationByHostFieldLinkVisitor)2 DoClearRelationByLinkedFieldLinkVisitor (org.nutz.dao.impl.link.DoClearRelationByLinkedFieldLinkVisitor)2 DoDeleteLinkVisitor (org.nutz.dao.impl.link.DoDeleteLinkVisitor)2 DoInsertLinkVisitor (org.nutz.dao.impl.link.DoInsertLinkVisitor)2 DoInsertRelationLinkVisitor (org.nutz.dao.impl.link.DoInsertRelationLinkVisitor)2