Java Code Examples for org.dom4j.Element.addComment()

The following are Jave code examples for showing how to use addComment() of the org.dom4j.Element class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: pndao   File: DaoGenHelper.java   View Source Code Vote up 6 votes
private Consumer<String> addRemove(DaoGen daoGen, String key, MapperMethod method, Element root) {
    return (prefix) -> {
        Element sql = root.addElement("delete");
        sql.addComment(COMMENT);
        sql.addAttribute("id", key);
        String left = key.replaceFirst(prefix, "");
        List<String> params = split(left, daoGen.separator());
        if (params.isEmpty()) throw new Error("Remove method needs at least  one param");
        StringBuilder select = new StringBuilder(50);
        select.append("delete from ")
                .append(method.getDaoEnv().getTableName());
        int len = params.size();
        if (!params.isEmpty()) select.append(" where ");
        int cur = 0;
        appendParams(params, select, len, cur);
        sql.addText(select.toString());
    };
}
 
Example 2
Project: pndao   File: DaoGenHelper.java   View Source Code Vote up 6 votes
private Consumer<String> addCount(DaoGen daoGen, String key, MapperMethod method, Element root,Consumer<List<String>> validator) {
    return (prefix) -> {
        Element sql = root.addElement("select");
        sql.addComment(COMMENT);
        sql.addAttribute("id", key);
        sql.addAttribute("resultType", "int");
        String left = key.replaceFirst(prefix, "");
        List<String> params = split(left, daoGen.separator());
        validator.accept(params);
        StringBuilder select = new StringBuilder(50);
        select.append("select count(1) from ")
                .append(method.getDaoEnv().getTableName());
        int len = params.size();
        if (!params.isEmpty()) select.append(" where ");
        int cur = 0;
        appendParams(params, select, len, cur);
        select.append(" order by ").append(daoGen.primaryKey());
        sql.addText(select.toString());
    };
}
 
Example 3
Project: pndao   File: DaoGenHelper.java   View Source Code Vote up 5 votes
private Consumer<String> addCountIn(DaoGen daoGen, String key, MapperMethod method, Element root) {
    return (prefix) -> {
        Element sql = root.addElement("select");
        sql.addComment(COMMENT);
        sql.addAttribute("id", key);
        sql.addAttribute("resultType", "int");
        String left = key.replaceFirst(prefix, "");
        List<String> params = split(left, daoGen.separator());
        StringBuilder select = new StringBuilder(50);
        select.append("select count(1) from  ")
                .append(method.getDaoEnv().getTableName());
        int len = params.size();
        if (len != 1)
            throw new Error("count in method only support one param");
        if (!params.isEmpty()) select.append(" where ");
        String param = params.get(0);

        sql.addText(select.toString());
        if (param.endsWith("s")) param = lowerFirst(param.substring(0,param.length()-1));

        Element choose = sql.addElement("choose");
        String collection = param+"s";
        Element when = choose.addElement("when");
        when.addAttribute("test", collection + " !=null and " + collection + ".size() > 0");
        when.addText("`" + param + "` in ");

        Element each = when.addElement("foreach");
        each.addAttribute("item", param);
        each.addAttribute("collection", param+"s");
        each.addAttribute("open", "(");
        each.addAttribute("separator", ",");
        each.addAttribute("close", ")");
        each.addText("#{" + param + "}");

        Element otherwise = choose.addElement("otherwise");
        otherwise.addText(" 1 = 2 ");

        sql.addText(" order by " + daoGen.primaryKey());
    };
}
 
Example 4
Project: pndao   File: DaoGenHelper.java   View Source Code Vote up 5 votes
private Consumer<String> addUpdateFor(DaoGen daoGen, String key, MapperMethod method, Element root) {
    return (prefix) -> {
        Element sql = root.addElement("update");
        sql.addComment(COMMENT);
        sql.addAttribute("id", key);

        StringBuilder updateSql = new StringBuilder(50);
        updateSql.append("update ")
                .append(method.getDaoEnv().getTableName())
                .append(" set \n");

        String left = key.replaceFirst(prefix, "");
        List<String> fields = split(left, daoGen.separator()).stream().map(this::lowerFirst).collect(Collectors.toList());
        fields.add(method.getDaoEnv().getUpdateTime());
        String pk = daoGen.primaryKey();
        updateSql.append(
                Joiner.on(", ").join(
                        fields.stream().filter((field -> !field.equals(pk) &&
                                !method.getDaoEnv().getCreateTime().equals(field)))
                                .map((field -> {
                                    if (method.getDaoEnv().getUpdateTime().equals(field))
                                        return "`" + field + "` = " + "now() ";
                                    else return "`" + field + "` = " + "#{" + field + "} ";
                                }))
                                .iterator()));

        updateSql.append("Where `")
                .append(pk)
                .append("` = ")
                .append("#{")
                .append(pk)
                .append("}");
        sql.addText(updateSql.toString());
    };
}
 
Example 5
Project: pndao   File: DaoGenHelper.java   View Source Code Vote up 5 votes
private Consumer<String> addBatchInsert(DaoGen daoGen, String key, MapperMethod method, Element root) {
    return (prefix) -> {
        Element sql = root.addElement("insert");
        sql.addComment(COMMENT);
        sql.addAttribute("id", key);

        StringBuilder insertSql = new StringBuilder(50);
        insertSql.append("insert into ")
                .append(method.getDaoEnv().getTableName())
                .append("\n");

        String pk = daoGen.primaryKey();
        List<String> fields = getFields(method.getFirstParamType());
        insertSql.append("(")
                .append(Joiner.on(", ").join(getInsertFieldsStream(pk, fields).map(f -> "`"+f + "`").iterator()))
                .append(")\n");

        insertSql.append("values ");
        sql.addText(insertSql.toString());
        Element foreach = sql.addElement("foreach");
        foreach.addAttribute("collection", "list");
        foreach.addAttribute("item", "item");
        foreach.addAttribute("separator", ",");
        StringBuilder eachSql = new StringBuilder(50);
        eachSql.append("(").append(Joiner.on(", ").join(getInsertFieldsStream(pk, fields).map(field -> {
            if (method.getDaoEnv().getCreateTime().contains(field)
                    || method.getDaoEnv().getUpdateTime().contains(field))
                return "now()";
            else return "#{item." + field + "}";
        }).iterator()));
        eachSql.append(")");
        foreach.addText(eachSql.toString());
    };
}
 
Example 6
Project: pndao   File: DaoGenHelper.java   View Source Code Vote up 5 votes
private Consumer<String> addInsert(DaoGen daoGen, String key, MapperMethod method, Element root) {
    return (prefix) -> {
        Element sql = root.addElement("insert");
        sql.addComment(COMMENT);
        sql.addAttribute("id", key);

        StringBuilder insertSql = new StringBuilder(50);
        insertSql.append("insert into ")
                .append(method.getDaoEnv().getTableName())
                .append("\n");

        String pk = daoGen.primaryKey();
        List<String> fields = getFields(method.getFirstParamType());
        insertSql.append("(")
                .append(Joiner.on(", ").join(getInsertFieldsStream(pk, fields).map(f -> "`"+f + "`").iterator()))
                .append(")\n");

        insertSql.append("values (");
        insertSql.append(Joiner.on(", ").join(getInsertFieldsStream(pk, fields).map(field -> {
            if (method.getDaoEnv().getCreateTime().contains(field)
                    || method.getDaoEnv().getUpdateTime().contains(field))
                return "now()";
            else return "#{" + field + "}";
        }).iterator()));
        insertSql.append(")");
        sql.addText(insertSql.toString());

        Element selectKey = sql.addElement("selectKey");
        selectKey.addAttribute("resultType", "int");
        selectKey.addAttribute("keyProperty", pk);
        selectKey.addText("SELECT @@IDENTITY AS " + pk);
    };
}
 
Example 7
Project: pndao   File: DaoGenHelper.java   View Source Code Vote up 4 votes
private Consumer<String> addQueryIn(DaoGen daoGen, String key, MapperMethod method, Element root) {
    return (prefix) -> {
        Element sql = root.addElement("select");
        sql.addComment(COMMENT);
        sql.addAttribute("id", key);
        sql.addAttribute("resultType", method.getReturnType().toString());
        String left = key.replaceFirst(prefix, "");
        List<String> params = split(left, daoGen.separator());
        StringBuilder select = new StringBuilder(50);
        List<String> fields = getFields(method.getReturnType());
        select.append("select ")
                .append(Joiner.on(", ").join(fields.stream().map(f-> "`" + f + "`").iterator()))
                .append(" from ")
                .append(method.getDaoEnv().getTableName());
        int len = params.size();
        if (len != 1)
            throw new Error("query in method only support one param");
        if (!params.isEmpty()) select.append(" where ");
        String param = params.get(0);

        sql.addText(select.toString());
        if (param.endsWith("s")) param = lowerFirst(param.substring(0,param.length()-1));

        Element choose = sql.addElement("choose");
        String collection = param+"s";
        Element when = choose.addElement("when");
        when.addAttribute("test", collection + " !=null and " + collection + ".size() > 0");
        when.addText("`" + param + "` in ");

        Element each = when.addElement("foreach");
        each.addAttribute("item", param);
        each.addAttribute("collection", param+"s");
        each.addAttribute("open", "(");
        each.addAttribute("separator", ",");
        each.addAttribute("close", ")");
        each.addText("#{" + param + "}");

        Element otherwise = choose.addElement("otherwise");
        otherwise.addText(" 1 = 2 ");

        sql.addText(" order by " + daoGen.primaryKey());
    };
}
 
Example 8
Project: pndao   File: DaoGenHelper.java   View Source Code Vote up 4 votes
private Consumer<String> addUpdate(DaoGen daoGen, String key, MapperMethod method, Element root) {
    return (prefix) -> {
        Element sql = root.addElement("update");
        sql.addComment(COMMENT);
        sql.addAttribute("id", key);

        StringBuilder updateSql = new StringBuilder(50);
        updateSql.append("update ")
                .append(method.getDaoEnv().getTableName())
                .append(" set \n");

        List<String> fields = getFields(method.getFirstParamType());
        String pk = daoGen.primaryKey();
        String updateByField;
        String entity;
        if (key.startsWith("updateBy")){
            updateByField = lowerFirst(key.substring(8));
            entity = "entity";
        }else{
            updateByField = pk;
            entity = null;
        }
        updateSql.append(
                Joiner.on(", ").join(
                        fields.stream().filter((field -> !field.equals(pk)
                                && !field.equals(updateByField)
                                && !method.getDaoEnv().getCreateTime().equals(field)))
                                .map((field -> {
                                    if (method.getDaoEnv().getUpdateTime().equals(field))
                                        return "`" + field + "` = " + "now() ";
                                    else
                                        return "`" + field + "` = " + "#{" + (entity != null ?
                                                entity + "." :
                                                "") + field + "} ";
                                }))
                                .iterator()));

        updateSql.append("Where `")
                .append(updateByField)
                .append("` = ")
                .append("#{")
                .append(updateByField)
                .append("}");
        sql.addText(updateSql.toString());
    };
}