Search in sources :

Example 76 with IntrospectedColumn

use of org.mybatis.generator.api.IntrospectedColumn in project generator by mybatis.

the class InsertSelectiveMethodGenerator method getMethodShell.

private Method getMethodShell(Set<FullyQualifiedJavaType> importedTypes) {
    Method method = new Method();
    FullyQualifiedJavaType returnType;
    if (introspectedTable.getGeneratedKey() != null) {
        IntrospectedColumn introspectedColumn = introspectedTable.getColumn(introspectedTable.getGeneratedKey().getColumn());
        if (introspectedColumn == null) {
            // the specified column doesn't exist, so don't do the generated
            // key
            // (the warning has already been reported)
            returnType = null;
        } else {
            returnType = introspectedColumn.getFullyQualifiedJavaType();
            importedTypes.add(returnType);
        }
    } else {
        returnType = null;
    }
    method.setReturnType(returnType);
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setName(getDAOMethodNameCalculator().getInsertSelectiveMethodName(introspectedTable));
    FullyQualifiedJavaType parameterType = introspectedTable.getRules().calculateAllFieldsClass();
    importedTypes.add(parameterType);
    //$NON-NLS-1$
    method.addParameter(new Parameter(parameterType, "record"));
    for (FullyQualifiedJavaType fqjt : daoTemplate.getCheckedExceptions()) {
        method.addException(fqjt);
        importedTypes.add(fqjt);
    }
    context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable);
    return method;
}
Also used : IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType) Parameter(org.mybatis.generator.api.dom.java.Parameter) Method(org.mybatis.generator.api.dom.java.Method)

Example 77 with IntrospectedColumn

use of org.mybatis.generator.api.IntrospectedColumn in project generator by mybatis.

the class ExampleGenerator method getGeneratedCriteriaInnerClass.

private InnerClass getGeneratedCriteriaInnerClass(TopLevelClass topLevelClass) {
    Field field;
    Method method;
    InnerClass answer = new InnerClass(FullyQualifiedJavaType.getGeneratedCriteriaInstance());
    answer.setVisibility(JavaVisibility.PROTECTED);
    answer.setStatic(true);
    answer.setAbstract(true);
    context.getCommentGenerator().addClassComment(answer, introspectedTable);
    method = new Method();
    method.setVisibility(JavaVisibility.PROTECTED);
    //$NON-NLS-1$
    method.setName("GeneratedCriteria");
    method.setConstructor(true);
    //$NON-NLS-1$
    method.addBodyLine("super();");
    if (generateForJava5) {
        method.addBodyLine(//$NON-NLS-1$
        "criteriaWithoutValue = new ArrayList<String>();");
        method.addBodyLine(//$NON-NLS-1$
        "criteriaWithSingleValue = new ArrayList<Map<String, Object>>();");
        method.addBodyLine(//$NON-NLS-1$
        "criteriaWithListValue = new ArrayList<Map<String, Object>>();");
        method.addBodyLine(//$NON-NLS-1$
        "criteriaWithBetweenValue = new ArrayList<Map<String, Object>>();");
    } else {
        //$NON-NLS-1$
        method.addBodyLine("criteriaWithoutValue = new ArrayList();");
        //$NON-NLS-1$
        method.addBodyLine("criteriaWithSingleValue = new ArrayList();");
        //$NON-NLS-1$
        method.addBodyLine("criteriaWithListValue = new ArrayList();");
        //$NON-NLS-1$
        method.addBodyLine("criteriaWithBetweenValue = new ArrayList();");
    }
    answer.addMethod(method);
    List<String> criteriaLists = new ArrayList<String>();
    //$NON-NLS-1$
    criteriaLists.add("criteriaWithoutValue");
    //$NON-NLS-1$
    criteriaLists.add("criteriaWithSingleValue");
    //$NON-NLS-1$
    criteriaLists.add("criteriaWithListValue");
    //$NON-NLS-1$
    criteriaLists.add("criteriaWithBetweenValue");
    for (IntrospectedColumn introspectedColumn : introspectedTable.getNonBLOBColumns()) {
        if (stringHasValue(introspectedColumn.getTypeHandler())) {
            criteriaLists.addAll(addtypeHandledObjectsAndMethods(introspectedColumn, method, answer));
        }
    }
    // now generate the isValid method
    method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    //$NON-NLS-1$
    method.setName("isValid");
    method.setReturnType(FullyQualifiedJavaType.getBooleanPrimitiveInstance());
    StringBuilder sb = new StringBuilder();
    Iterator<String> strIter = criteriaLists.iterator();
    //$NON-NLS-1$
    sb.append("return ");
    sb.append(strIter.next());
    //$NON-NLS-1$
    sb.append(".size() > 0");
    method.addBodyLine(sb.toString());
    while (strIter.hasNext()) {
        sb.setLength(0);
        OutputUtilities.javaIndent(sb, 1);
        //$NON-NLS-1$
        sb.append("|| ");
        sb.append(strIter.next());
        //$NON-NLS-1$
        sb.append(".size() > 0");
        if (!strIter.hasNext()) {
            sb.append(';');
        }
        method.addBodyLine(sb.toString());
    }
    answer.addMethod(method);
    // now we need to generate the methods that will be used in the SqlMap
    // to generate the dynamic where clause
    topLevelClass.addImportedType(FullyQualifiedJavaType.getNewMapInstance());
    topLevelClass.addImportedType(FullyQualifiedJavaType.getNewListInstance());
    topLevelClass.addImportedType(FullyQualifiedJavaType.getNewHashMapInstance());
    topLevelClass.addImportedType(FullyQualifiedJavaType.getNewArrayListInstance());
    field = new Field();
    field.setVisibility(JavaVisibility.PROTECTED);
    FullyQualifiedJavaType listOfStrings;
    if (generateForJava5) {
        listOfStrings = new FullyQualifiedJavaType(//$NON-NLS-1$
        "java.util.List<java.lang.String>");
    } else {
        //$NON-NLS-1$
        listOfStrings = new FullyQualifiedJavaType("java.util.List");
    }
    field.setType(listOfStrings);
    //$NON-NLS-1$
    field.setName("criteriaWithoutValue");
    answer.addField(field);
    method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setReturnType(field.getType());
    method.setName(getGetterMethodName(field.getName(), field.getType()));
    //$NON-NLS-1$
    method.addBodyLine("return criteriaWithoutValue;");
    answer.addMethod(method);
    FullyQualifiedJavaType listOfMaps;
    if (generateForJava5) {
        listOfMaps = new FullyQualifiedJavaType(//$NON-NLS-1$
        "java.util.List<java.util.Map<java.lang.String, java.lang.Object>>");
    } else {
        //$NON-NLS-1$
        listOfMaps = new FullyQualifiedJavaType("java.util.List");
    }
    field = new Field();
    field.setVisibility(JavaVisibility.PROTECTED);
    field.setType(listOfMaps);
    //$NON-NLS-1$
    field.setName("criteriaWithSingleValue");
    answer.addField(field);
    method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setReturnType(field.getType());
    method.setName(getGetterMethodName(field.getName(), field.getType()));
    //$NON-NLS-1$
    method.addBodyLine("return criteriaWithSingleValue;");
    answer.addMethod(method);
    field = new Field();
    field.setVisibility(JavaVisibility.PROTECTED);
    field.setType(listOfMaps);
    //$NON-NLS-1$
    field.setName("criteriaWithListValue");
    answer.addField(field);
    method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setReturnType(field.getType());
    method.setName(getGetterMethodName(field.getName(), field.getType()));
    //$NON-NLS-1$
    method.addBodyLine("return criteriaWithListValue;");
    answer.addMethod(method);
    field = new Field();
    field.setVisibility(JavaVisibility.PROTECTED);
    field.setType(listOfMaps);
    //$NON-NLS-1$
    field.setName("criteriaWithBetweenValue");
    answer.addField(field);
    method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setReturnType(field.getType());
    method.setName(getGetterMethodName(field.getName(), field.getType()));
    //$NON-NLS-1$
    method.addBodyLine("return criteriaWithBetweenValue;");
    answer.addMethod(method);
    // now add the methods for simplifying the individual field set methods
    method = new Method();
    method.setVisibility(JavaVisibility.PROTECTED);
    //$NON-NLS-1$
    method.setName("addCriterion");
    method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
    "condition"));
    //$NON-NLS-1$
    method.addBodyLine("if (condition == null) {");
    method.addBodyLine(//$NON-NLS-1$
    "throw new RuntimeException(\"Value for condition cannot be null\");");
    //$NON-NLS-1$
    method.addBodyLine("}");
    //$NON-NLS-1$
    method.addBodyLine("criteriaWithoutValue.add(condition);");
    answer.addMethod(method);
    method = new Method();
    method.setVisibility(JavaVisibility.PROTECTED);
    //$NON-NLS-1$
    method.setName("addCriterion");
    method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
    "condition"));
    method.addParameter(new Parameter(FullyQualifiedJavaType.getObjectInstance(), //$NON-NLS-1$
    "value"));
    method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
    "property"));
    //$NON-NLS-1$
    method.addBodyLine("if (value == null) {");
    method.addBodyLine(//$NON-NLS-1$
    "throw new RuntimeException(\"Value for \" + property + \" cannot be null\");");
    //$NON-NLS-1$
    method.addBodyLine("}");
    if (generateForJava5) {
        method.addBodyLine(//$NON-NLS-1$
        "Map<String, Object> map = new HashMap<String, Object>();");
    } else {
        //$NON-NLS-1$
        method.addBodyLine("Map map = new HashMap();");
    }
    //$NON-NLS-1$
    method.addBodyLine("map.put(\"condition\", condition);");
    //$NON-NLS-1$
    method.addBodyLine("map.put(\"value\", value);");
    //$NON-NLS-1$
    method.addBodyLine("criteriaWithSingleValue.add(map);");
    answer.addMethod(method);
    FullyQualifiedJavaType listOfObjects;
    if (generateForJava5) {
        listOfObjects = new FullyQualifiedJavaType(//$NON-NLS-1$
        "java.util.List<? extends java.lang.Object>");
    } else {
        //$NON-NLS-1$
        listOfObjects = new FullyQualifiedJavaType("java.util.List");
    }
    method = new Method();
    method.setVisibility(JavaVisibility.PROTECTED);
    //$NON-NLS-1$
    method.setName("addCriterion");
    method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
    "condition"));
    //$NON-NLS-1$
    method.addParameter(new Parameter(listOfObjects, "values"));
    method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
    "property"));
    //$NON-NLS-1$
    method.addBodyLine("if (values == null || values.size() == 0) {");
    method.addBodyLine(//$NON-NLS-1$
    "throw new RuntimeException(\"Value list for \" + property + \" cannot be null or empty\");");
    //$NON-NLS-1$
    method.addBodyLine("}");
    if (generateForJava5) {
        method.addBodyLine(//$NON-NLS-1$
        "Map<String, Object> map = new HashMap<String, Object>();");
    } else {
        //$NON-NLS-1$
        method.addBodyLine("Map map = new HashMap();");
    }
    //$NON-NLS-1$
    method.addBodyLine("map.put(\"condition\", condition);");
    //$NON-NLS-1$
    method.addBodyLine("map.put(\"values\", values);");
    //$NON-NLS-1$
    method.addBodyLine("criteriaWithListValue.add(map);");
    answer.addMethod(method);
    method = new Method();
    method.setVisibility(JavaVisibility.PROTECTED);
    //$NON-NLS-1$
    method.setName("addCriterion");
    method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
    "condition"));
    method.addParameter(new Parameter(FullyQualifiedJavaType.getObjectInstance(), //$NON-NLS-1$
    "value1"));
    method.addParameter(new Parameter(FullyQualifiedJavaType.getObjectInstance(), //$NON-NLS-1$
    "value2"));
    method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
    "property"));
    //$NON-NLS-1$
    method.addBodyLine("if (value1 == null || value2 == null) {");
    method.addBodyLine(//$NON-NLS-1$
    "throw new RuntimeException(\"Between values for \" + property + \" cannot be null\");");
    //$NON-NLS-1$
    method.addBodyLine("}");
    if (generateForJava5) {
        //$NON-NLS-1$
        method.addBodyLine("List<Object> list = new ArrayList<Object>();");
    } else {
        //$NON-NLS-1$
        method.addBodyLine("List list = new ArrayList();");
    }
    //$NON-NLS-1$
    method.addBodyLine("list.add(value1);");
    //$NON-NLS-1$
    method.addBodyLine("list.add(value2);");
    if (generateForJava5) {
        method.addBodyLine(//$NON-NLS-1$
        "Map<String, Object> map = new HashMap<String, Object>();");
    } else {
        //$NON-NLS-1$
        method.addBodyLine("Map map = new HashMap();");
    }
    //$NON-NLS-1$
    method.addBodyLine("map.put(\"condition\", condition);");
    //$NON-NLS-1$
    method.addBodyLine("map.put(\"values\", list);");
    //$NON-NLS-1$
    method.addBodyLine("criteriaWithBetweenValue.add(map);");
    answer.addMethod(method);
    FullyQualifiedJavaType listOfDates;
    if (generateForJava5) {
        listOfDates = new FullyQualifiedJavaType(//$NON-NLS-1$
        "java.util.List<java.util.Date>");
    } else {
        //$NON-NLS-1$
        listOfDates = new FullyQualifiedJavaType("java.util.List");
    }
    if (introspectedTable.hasJDBCDateColumns()) {
        topLevelClass.addImportedType(FullyQualifiedJavaType.getDateInstance());
        topLevelClass.addImportedType(FullyQualifiedJavaType.getNewIteratorInstance());
        method = new Method();
        method.setVisibility(JavaVisibility.PROTECTED);
        //$NON-NLS-1$
        method.setName("addCriterionForJDBCDate");
        method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
        "condition"));
        method.addParameter(new Parameter(FullyQualifiedJavaType.getDateInstance(), //$NON-NLS-1$
        "value"));
        method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
        "property"));
        //$NON-NLS-1$
        method.addBodyLine("if (value == null) {");
        method.addBodyLine(//$NON-NLS-1$
        "throw new RuntimeException(\"Value for \" + property + \" cannot be null\");");
        //$NON-NLS-1$
        method.addBodyLine("}");
        method.addBodyLine(//$NON-NLS-1$
        "addCriterion(condition, new java.sql.Date(value.getTime()), property);");
        answer.addMethod(method);
        method = new Method();
        method.setVisibility(JavaVisibility.PROTECTED);
        //$NON-NLS-1$
        method.setName("addCriterionForJDBCDate");
        method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
        "condition"));
        //$NON-NLS-1$
        method.addParameter(new Parameter(listOfDates, "values"));
        method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
        "property"));
        //$NON-NLS-1$
        method.addBodyLine("if (values == null || values.size() == 0) {");
        method.addBodyLine(//$NON-NLS-1$
        "throw new RuntimeException(\"Value list for \" + property + \" cannot be null or empty\");");
        //$NON-NLS-1$
        method.addBodyLine("}");
        if (generateForJava5) {
            method.addBodyLine(//$NON-NLS-1$
            "List<java.sql.Date> dateList = new ArrayList<java.sql.Date>();");
            //$NON-NLS-1$
            method.addBodyLine("Iterator<Date> iter = values.iterator();");
            //$NON-NLS-1$
            method.addBodyLine("while (iter.hasNext()) {");
            method.addBodyLine(//$NON-NLS-1$
            "dateList.add(new java.sql.Date(iter.next().getTime()));");
            //$NON-NLS-1$
            method.addBodyLine("}");
        } else {
            //$NON-NLS-1$
            method.addBodyLine("List dateList = new ArrayList();");
            //$NON-NLS-1$
            method.addBodyLine("Iterator iter = values.iterator();");
            //$NON-NLS-1$
            method.addBodyLine("while (iter.hasNext()) {");
            method.addBodyLine(//$NON-NLS-1$
            "dateList.add(new java.sql.Date(((Date)iter.next()).getTime()));");
            //$NON-NLS-1$
            method.addBodyLine("}");
        }
        //$NON-NLS-1$
        method.addBodyLine("addCriterion(condition, dateList, property);");
        answer.addMethod(method);
        method = new Method();
        method.setVisibility(JavaVisibility.PROTECTED);
        //$NON-NLS-1$
        method.setName("addCriterionForJDBCDate");
        method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
        "condition"));
        method.addParameter(new Parameter(FullyQualifiedJavaType.getDateInstance(), //$NON-NLS-1$
        "value1"));
        method.addParameter(new Parameter(FullyQualifiedJavaType.getDateInstance(), //$NON-NLS-1$
        "value2"));
        method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
        "property"));
        //$NON-NLS-1$
        method.addBodyLine("if (value1 == null || value2 == null) {");
        method.addBodyLine(//$NON-NLS-1$
        "throw new RuntimeException(\"Between values for \" + property + \" cannot be null\");");
        //$NON-NLS-1$
        method.addBodyLine("}");
        method.addBodyLine(//$NON-NLS-1$
        "addCriterion(condition, new java.sql.Date(value1.getTime()), new java.sql.Date(value2.getTime()), property);");
        answer.addMethod(method);
    }
    if (introspectedTable.hasJDBCTimeColumns()) {
        topLevelClass.addImportedType(FullyQualifiedJavaType.getDateInstance());
        topLevelClass.addImportedType(FullyQualifiedJavaType.getNewIteratorInstance());
        method = new Method();
        method.setVisibility(JavaVisibility.PROTECTED);
        //$NON-NLS-1$
        method.setName("addCriterionForJDBCTime");
        method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
        "condition"));
        method.addParameter(new Parameter(FullyQualifiedJavaType.getDateInstance(), //$NON-NLS-1$
        "value"));
        method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
        "property"));
        //$NON-NLS-1$
        method.addBodyLine("if (value == null) {");
        method.addBodyLine(//$NON-NLS-1$
        "throw new RuntimeException(\"Value for \" + property + \" cannot be null\");");
        //$NON-NLS-1$
        method.addBodyLine("}");
        method.addBodyLine(//$NON-NLS-1$
        "addCriterion(condition, new java.sql.Time(value.getTime()), property);");
        answer.addMethod(method);
        method = new Method();
        method.setVisibility(JavaVisibility.PROTECTED);
        //$NON-NLS-1$
        method.setName("addCriterionForJDBCTime");
        method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
        "condition"));
        //$NON-NLS-1$
        method.addParameter(new Parameter(listOfDates, "values"));
        method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
        "property"));
        //$NON-NLS-1$
        method.addBodyLine("if (values == null || values.size() == 0) {");
        method.addBodyLine(//$NON-NLS-1$
        "throw new RuntimeException(\"Value list for \" + property + \" cannot be null or empty\");");
        //$NON-NLS-1$
        method.addBodyLine("}");
        if (generateForJava5) {
            method.addBodyLine(//$NON-NLS-1$
            "List<java.sql.Time> timeList = new ArrayList<java.sql.Time>();");
            //$NON-NLS-1$
            method.addBodyLine("Iterator<Date> iter = values.iterator();");
            //$NON-NLS-1$
            method.addBodyLine("while (iter.hasNext()) {");
            method.addBodyLine(//$NON-NLS-1$
            "timeList.add(new java.sql.Time(iter.next().getTime()));");
            //$NON-NLS-1$
            method.addBodyLine("}");
        } else {
            //$NON-NLS-1$
            method.addBodyLine("List timeList = new ArrayList();");
            //$NON-NLS-1$
            method.addBodyLine("Iterator iter = values.iterator();");
            //$NON-NLS-1$
            method.addBodyLine("while (iter.hasNext()) {");
            method.addBodyLine(//$NON-NLS-1$
            "timeList.add(new java.sql.Time(((Date)iter.next()).getTime()));");
            //$NON-NLS-1$
            method.addBodyLine("}");
        }
        //$NON-NLS-1$
        method.addBodyLine("addCriterion(condition, timeList, property);");
        answer.addMethod(method);
        method = new Method();
        method.setVisibility(JavaVisibility.PROTECTED);
        //$NON-NLS-1$
        method.setName("addCriterionForJDBCTime");
        method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
        "condition"));
        method.addParameter(new Parameter(FullyQualifiedJavaType.getDateInstance(), //$NON-NLS-1$
        "value1"));
        method.addParameter(new Parameter(FullyQualifiedJavaType.getDateInstance(), //$NON-NLS-1$
        "value2"));
        method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
        "property"));
        //$NON-NLS-1$
        method.addBodyLine("if (value1 == null || value2 == null) {");
        method.addBodyLine(//$NON-NLS-1$
        "throw new RuntimeException(\"Between values for \" + property + \" cannot be null\");");
        //$NON-NLS-1$
        method.addBodyLine("}");
        method.addBodyLine(//$NON-NLS-1$
        "addCriterion(condition, new java.sql.Time(value1.getTime()), new java.sql.Time(value2.getTime()), property);");
        answer.addMethod(method);
    }
    for (IntrospectedColumn introspectedColumn : introspectedTable.getNonBLOBColumns()) {
        topLevelClass.addImportedType(introspectedColumn.getFullyQualifiedJavaType());
        // here we need to add the individual methods for setting the
        // conditions for a field
        answer.addMethod(getSetNullMethod(introspectedColumn));
        answer.addMethod(getSetNotNullMethod(introspectedColumn));
        answer.addMethod(getSetEqualMethod(introspectedColumn));
        answer.addMethod(getSetNotEqualMethod(introspectedColumn));
        answer.addMethod(getSetGreaterThanMethod(introspectedColumn));
        answer.addMethod(getSetGreaterThenOrEqualMethod(introspectedColumn));
        answer.addMethod(getSetLessThanMethod(introspectedColumn));
        answer.addMethod(getSetLessThanOrEqualMethod(introspectedColumn));
        if (introspectedColumn.isJdbcCharacterColumn()) {
            answer.addMethod(getSetLikeMethod(introspectedColumn));
            answer.addMethod(getSetNotLikeMethod(introspectedColumn));
        }
        answer.addMethod(getSetInOrNotInMethod(introspectedColumn, true));
        answer.addMethod(getSetInOrNotInMethod(introspectedColumn, false));
        answer.addMethod(getSetBetweenOrNotBetweenMethod(introspectedColumn, true));
        answer.addMethod(getSetBetweenOrNotBetweenMethod(introspectedColumn, false));
    }
    return answer;
}
Also used : Field(org.mybatis.generator.api.dom.java.Field) IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType) InnerClass(org.mybatis.generator.api.dom.java.InnerClass) ArrayList(java.util.ArrayList) Parameter(org.mybatis.generator.api.dom.java.Parameter) Method(org.mybatis.generator.api.dom.java.Method) Messages.getString(org.mybatis.generator.internal.util.messages.Messages.getString)

Example 78 with IntrospectedColumn

use of org.mybatis.generator.api.IntrospectedColumn in project generator by mybatis.

the class PrimaryKeyGenerator method getCompilationUnits.

@Override
public List<CompilationUnit> getCompilationUnits() {
    FullyQualifiedTable table = introspectedTable.getFullyQualifiedTable();
    progressCallback.startTask(getString("Progress.7", //$NON-NLS-1$
    table.toString()));
    Plugin plugins = context.getPlugins();
    CommentGenerator commentGenerator = context.getCommentGenerator();
    TopLevelClass topLevelClass = new TopLevelClass(introspectedTable.getPrimaryKeyType());
    topLevelClass.setVisibility(JavaVisibility.PUBLIC);
    commentGenerator.addJavaFileComment(topLevelClass);
    String rootClass = getRootClass();
    if (rootClass != null) {
        topLevelClass.setSuperClass(new FullyQualifiedJavaType(rootClass));
        topLevelClass.addImportedType(topLevelClass.getSuperClass());
    }
    for (IntrospectedColumn introspectedColumn : introspectedTable.getPrimaryKeyColumns()) {
        if (RootClassInfo.getInstance(rootClass, warnings).containsProperty(introspectedColumn)) {
            continue;
        }
        Field field = getJavaBeansField(introspectedColumn, context, introspectedTable);
        if (plugins.modelFieldGenerated(field, topLevelClass, introspectedColumn, introspectedTable, Plugin.ModelClassType.PRIMARY_KEY)) {
            topLevelClass.addField(field);
            topLevelClass.addImportedType(field.getType());
        }
        Method method = getJavaBeansGetter(introspectedColumn, context, introspectedTable);
        if (plugins.modelGetterMethodGenerated(method, topLevelClass, introspectedColumn, introspectedTable, Plugin.ModelClassType.PRIMARY_KEY)) {
            topLevelClass.addMethod(method);
        }
        method = getJavaBeansSetter(introspectedColumn, context, introspectedTable);
        if (plugins.modelSetterMethodGenerated(method, topLevelClass, introspectedColumn, introspectedTable, Plugin.ModelClassType.PRIMARY_KEY)) {
            topLevelClass.addMethod(method);
        }
    }
    List<CompilationUnit> answer = new ArrayList<CompilationUnit>();
    if (context.getPlugins().modelPrimaryKeyClassGenerated(topLevelClass, introspectedTable)) {
        answer.add(topLevelClass);
    }
    return answer;
}
Also used : CompilationUnit(org.mybatis.generator.api.dom.java.CompilationUnit) Field(org.mybatis.generator.api.dom.java.Field) JavaBeansUtil.getJavaBeansField(org.mybatis.generator.internal.util.JavaBeansUtil.getJavaBeansField) IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) CommentGenerator(org.mybatis.generator.api.CommentGenerator) FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType) FullyQualifiedTable(org.mybatis.generator.api.FullyQualifiedTable) TopLevelClass(org.mybatis.generator.api.dom.java.TopLevelClass) ArrayList(java.util.ArrayList) Messages.getString(org.mybatis.generator.internal.util.messages.Messages.getString) Method(org.mybatis.generator.api.dom.java.Method) Plugin(org.mybatis.generator.api.Plugin)

Example 79 with IntrospectedColumn

use of org.mybatis.generator.api.IntrospectedColumn in project generator by mybatis.

the class UpdateByPrimaryKeyWithBLOBsElementGenerator method addElements.

@Override
public void addElements(XmlElement parentElement) {
    //$NON-NLS-1$
    XmlElement answer = new XmlElement("update");
    answer.addAttribute(new Attribute("id", //$NON-NLS-1$
    introspectedTable.getUpdateByPrimaryKeyWithBLOBsStatementId()));
    String parameterType;
    if (introspectedTable.getRules().generateRecordWithBLOBsClass()) {
        parameterType = introspectedTable.getRecordWithBLOBsType();
    } else {
        parameterType = introspectedTable.getBaseRecordType();
    }
    answer.addAttribute(new //$NON-NLS-1$
    Attribute(//$NON-NLS-1$
    "parameterClass", parameterType));
    context.getCommentGenerator().addComment(answer);
    StringBuilder sb = new StringBuilder();
    //$NON-NLS-1$
    sb.append("update ");
    sb.append(introspectedTable.getFullyQualifiedTableNameAtRuntime());
    answer.addElement(new TextElement(sb.toString()));
    // set up for first column
    sb.setLength(0);
    //$NON-NLS-1$
    sb.append("set ");
    Iterator<IntrospectedColumn> iter = introspectedTable.getNonPrimaryKeyColumns().iterator();
    while (iter.hasNext()) {
        IntrospectedColumn introspectedColumn = iter.next();
        sb.append(Ibatis2FormattingUtilities.getEscapedColumnName(introspectedColumn));
        //$NON-NLS-1$
        sb.append(" = ");
        sb.append(Ibatis2FormattingUtilities.getParameterClause(introspectedColumn));
        if (iter.hasNext()) {
            sb.append(',');
        }
        answer.addElement(new TextElement(sb.toString()));
        // set up for the next column
        if (iter.hasNext()) {
            sb.setLength(0);
            OutputUtilities.xmlIndent(sb, 1);
        }
    }
    boolean and = false;
    for (IntrospectedColumn introspectedColumn : introspectedTable.getPrimaryKeyColumns()) {
        sb.setLength(0);
        if (and) {
            //$NON-NLS-1$
            sb.append("  and ");
        } else {
            //$NON-NLS-1$
            sb.append("where ");
            and = true;
        }
        sb.append(Ibatis2FormattingUtilities.getEscapedColumnName(introspectedColumn));
        //$NON-NLS-1$
        sb.append(" = ");
        sb.append(Ibatis2FormattingUtilities.getParameterClause(introspectedColumn));
        answer.addElement(new TextElement(sb.toString()));
    }
    if (context.getPlugins().sqlMapUpdateByPrimaryKeyWithBLOBsElementGenerated(answer, introspectedTable)) {
        parentElement.addElement(answer);
    }
}
Also used : IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) TextElement(org.mybatis.generator.api.dom.xml.TextElement) Attribute(org.mybatis.generator.api.dom.xml.Attribute) XmlElement(org.mybatis.generator.api.dom.xml.XmlElement)

Example 80 with IntrospectedColumn

use of org.mybatis.generator.api.IntrospectedColumn in project generator by mybatis.

the class InsertElementGenerator method addElements.

@Override
public void addElements(XmlElement parentElement) {
    //$NON-NLS-1$
    XmlElement answer = new XmlElement("insert");
    answer.addAttribute(new Attribute("id", //$NON-NLS-1$
    introspectedTable.getInsertStatementId()));
    FullyQualifiedJavaType parameterType = introspectedTable.getRules().calculateAllFieldsClass();
    answer.addAttribute(new //$NON-NLS-1$
    Attribute(//$NON-NLS-1$
    "parameterClass", parameterType.getFullyQualifiedName()));
    context.getCommentGenerator().addComment(answer);
    GeneratedKey gk = introspectedTable.getGeneratedKey();
    if (gk != null && gk.isPlacedBeforeInsertInIbatis2()) {
        IntrospectedColumn introspectedColumn = introspectedTable.getColumn(gk.getColumn());
        // warning has already been reported
        if (introspectedColumn != null) {
            // pre-generated key
            answer.addElement(getSelectKey(introspectedColumn, gk));
        }
    }
    StringBuilder insertClause = new StringBuilder();
    StringBuilder valuesClause = new StringBuilder();
    //$NON-NLS-1$
    insertClause.append("insert into ");
    insertClause.append(introspectedTable.getFullyQualifiedTableNameAtRuntime());
    //$NON-NLS-1$
    insertClause.append(" (");
    //$NON-NLS-1$
    valuesClause.append("values (");
    List<String> valuesClauses = new ArrayList<String>();
    Iterator<IntrospectedColumn> iter = introspectedTable.getAllColumns().iterator();
    while (iter.hasNext()) {
        IntrospectedColumn introspectedColumn = iter.next();
        if (introspectedColumn.isIdentity()) {
            // cannot set values on identity fields
            continue;
        }
        insertClause.append(Ibatis2FormattingUtilities.getEscapedColumnName(introspectedColumn));
        valuesClause.append(Ibatis2FormattingUtilities.getParameterClause(introspectedColumn));
        if (iter.hasNext()) {
            //$NON-NLS-1$
            insertClause.append(", ");
            //$NON-NLS-1$
            valuesClause.append(", ");
        }
        if (valuesClause.length() > 80) {
            answer.addElement(new TextElement(insertClause.toString()));
            insertClause.setLength(0);
            OutputUtilities.xmlIndent(insertClause, 1);
            valuesClauses.add(valuesClause.toString());
            valuesClause.setLength(0);
            OutputUtilities.xmlIndent(valuesClause, 1);
        }
    }
    insertClause.append(')');
    answer.addElement(new TextElement(insertClause.toString()));
    valuesClause.append(')');
    valuesClauses.add(valuesClause.toString());
    for (String clause : valuesClauses) {
        answer.addElement(new TextElement(clause));
    }
    if (gk != null && !gk.isPlacedBeforeInsertInIbatis2()) {
        IntrospectedColumn introspectedColumn = introspectedTable.getColumn(gk.getColumn());
        // warning has already been reported
        if (introspectedColumn != null) {
            // pre-generated key
            answer.addElement(getSelectKey(introspectedColumn, gk));
        }
    }
    if (context.getPlugins().sqlMapInsertElementGenerated(answer, introspectedTable)) {
        parentElement.addElement(answer);
    }
}
Also used : GeneratedKey(org.mybatis.generator.config.GeneratedKey) IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) TextElement(org.mybatis.generator.api.dom.xml.TextElement) Attribute(org.mybatis.generator.api.dom.xml.Attribute) FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType) ArrayList(java.util.ArrayList) XmlElement(org.mybatis.generator.api.dom.xml.XmlElement)

Aggregations

IntrospectedColumn (org.mybatis.generator.api.IntrospectedColumn)82 XmlElement (org.mybatis.generator.api.dom.xml.XmlElement)34 FullyQualifiedJavaType (org.mybatis.generator.api.dom.java.FullyQualifiedJavaType)32 Attribute (org.mybatis.generator.api.dom.xml.Attribute)30 Method (org.mybatis.generator.api.dom.java.Method)29 TextElement (org.mybatis.generator.api.dom.xml.TextElement)27 ArrayList (java.util.ArrayList)17 Parameter (org.mybatis.generator.api.dom.java.Parameter)17 Messages.getString (org.mybatis.generator.internal.util.messages.Messages.getString)12 TreeSet (java.util.TreeSet)9 Field (org.mybatis.generator.api.dom.java.Field)9 FullyQualifiedTable (org.mybatis.generator.api.FullyQualifiedTable)8 CommentGenerator (org.mybatis.generator.api.CommentGenerator)7 Plugin (org.mybatis.generator.api.Plugin)7 CompilationUnit (org.mybatis.generator.api.dom.java.CompilationUnit)7 TopLevelClass (org.mybatis.generator.api.dom.java.TopLevelClass)7 JavaBeansUtil.getJavaBeansField (org.mybatis.generator.internal.util.JavaBeansUtil.getJavaBeansField)7 GeneratedKey (org.mybatis.generator.config.GeneratedKey)6 HashMap (java.util.HashMap)5 List (java.util.List)5