Search in sources :

Example 86 with IntrospectedColumn

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

the class KotlinDataClassGenerator method getKotlinFiles.

@Override
public List<KotlinFile> getKotlinFiles() {
    FullyQualifiedTable table = introspectedTable.getFullyQualifiedTable();
    // $NON-NLS-1$
    progressCallback.startTask(getString("Progress.8", table.toString()));
    CommentGenerator commentGenerator = context.getCommentGenerator();
    FullyQualifiedKotlinType type = new FullyQualifiedKotlinType(introspectedTable.getKotlinRecordType());
    KotlinFile kf = new KotlinFile(type.getShortNameWithoutTypeArguments());
    kf.setPackage(type.getPackageName());
    KotlinType dataClass = KotlinType.newClass(type.getShortNameWithoutTypeArguments()).withModifier(KotlinModifier.DATA).build();
    kf.addNamedItem(dataClass);
    commentGenerator.addFileComment(kf);
    commentGenerator.addModelClassComment(dataClass, introspectedTable);
    List<IntrospectedColumn> introspectedColumns = introspectedTable.getAllColumns();
    for (IntrospectedColumn introspectedColumn : introspectedColumns) {
        FullyQualifiedKotlinType kotlinType = JavaToKotlinTypeConverter.convert(introspectedColumn.getFullyQualifiedJavaType());
        KotlinProperty kp = KotlinProperty.newVar(introspectedColumn.getJavaProperty()).withDataType(// $NON-NLS-1$
        kotlinType.getShortNameWithTypeArguments() + "?").withInitializationString(// $NON-NLS-1$
        "null").build();
        dataClass.addConstructorProperty(kp);
        kf.addImports(kotlinType.getImportList());
    }
    if (context.getPlugins().kotlinDataClassGenerated(kf, dataClass, introspectedTable)) {
        return listOf(kf);
    } else {
        return Collections.emptyList();
    }
}
Also used : IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) CommentGenerator(org.mybatis.generator.api.CommentGenerator) KotlinProperty(org.mybatis.generator.api.dom.kotlin.KotlinProperty) FullyQualifiedTable(org.mybatis.generator.api.FullyQualifiedTable) FullyQualifiedKotlinType(org.mybatis.generator.api.dom.kotlin.FullyQualifiedKotlinType) KotlinType(org.mybatis.generator.api.dom.kotlin.KotlinType) KotlinFile(org.mybatis.generator.api.dom.kotlin.KotlinFile) FullyQualifiedKotlinType(org.mybatis.generator.api.dom.kotlin.FullyQualifiedKotlinType)

Example 87 with IntrospectedColumn

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

the class FragmentGenerator method getAnnotatedConstructorArgs.

public MethodParts getAnnotatedConstructorArgs() {
    MethodParts.Builder builder = new MethodParts.Builder();
    // $NON-NLS-1$
    builder.withImport(new FullyQualifiedJavaType("org.apache.ibatis.type.JdbcType"));
    // $NON-NLS-1$
    builder.withImport(new FullyQualifiedJavaType("org.apache.ibatis.annotations.ConstructorArgs"));
    // $NON-NLS-1$
    builder.withImport(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Arg"));
    // $NON-NLS-1$
    builder.withAnnotation("@ConstructorArgs({");
    StringBuilder sb = new StringBuilder();
    Set<FullyQualifiedJavaType> imports = new HashSet<>();
    Iterator<IntrospectedColumn> iterPk = introspectedTable.getPrimaryKeyColumns().iterator();
    Iterator<IntrospectedColumn> iterNonPk = introspectedTable.getNonPrimaryKeyColumns().iterator();
    while (iterPk.hasNext()) {
        IntrospectedColumn introspectedColumn = iterPk.next();
        sb.setLength(0);
        javaIndent(sb, 1);
        sb.append(getArgAnnotation(imports, introspectedColumn, true));
        if (iterPk.hasNext() || iterNonPk.hasNext()) {
            sb.append(',');
        }
        builder.withAnnotation(sb.toString());
    }
    while (iterNonPk.hasNext()) {
        IntrospectedColumn introspectedColumn = iterNonPk.next();
        sb.setLength(0);
        javaIndent(sb, 1);
        sb.append(getArgAnnotation(imports, introspectedColumn, false));
        if (iterNonPk.hasNext()) {
            sb.append(',');
        }
        builder.withAnnotation(sb.toString());
    }
    // $NON-NLS-1$
    builder.withAnnotation("})").withImports(imports);
    return builder.build();
}
Also used : IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType) HashSet(java.util.HashSet)

Example 88 with IntrospectedColumn

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

the class FragmentGenerator method getSetLines.

private List<String> getSetLines(List<IntrospectedColumn> columnList, String firstLinePrefix, String subsequentLinePrefix, boolean terminate, String fragment) {
    List<String> lines = new ArrayList<>();
    List<IntrospectedColumn> columns = ListUtilities.removeIdentityAndGeneratedAlwaysColumns(columnList);
    Iterator<IntrospectedColumn> iter = columns.iterator();
    boolean first = true;
    while (iter.hasNext()) {
        IntrospectedColumn column = iter.next();
        String fieldName = AbstractMethodGenerator.calculateFieldName(tableFieldName, column);
        String methodName = JavaBeansUtil.getGetterMethodName(column.getJavaProperty(), column.getFullyQualifiedJavaType());
        String start;
        if (first) {
            start = firstLinePrefix;
            first = false;
        } else {
            start = subsequentLinePrefix;
        }
        String line = start + // $NON-NLS-1$
        ".set(" + fieldName + // $NON-NLS-1$
        ")." + fragment + // $NON-NLS-1$
        "(row::" + methodName + // $NON-NLS-1$
        ")";
        if (terminate && !iter.hasNext()) {
            // $NON-NLS-1$
            line += ";";
        }
        lines.add(line);
    }
    return lines;
}
Also used : IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) ArrayList(java.util.ArrayList)

Example 89 with IntrospectedColumn

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

the class InsertMultipleMethodGenerator method generateMethodAndImports.

@Override
public MethodAndImports generateMethodAndImports() {
    if (!Utils.generateMultipleRowInsert(introspectedTable)) {
        return null;
    }
    Set<FullyQualifiedJavaType> imports = new HashSet<>();
    // $NON-NLS-1$
    imports.add(new FullyQualifiedJavaType("org.mybatis.dynamic.sql.util.mybatis3.MyBatis3Utils"));
    imports.add(recordType);
    // $NON-NLS-1$
    Method method = new Method("insertMultiple");
    method.setDefault(true);
    context.getCommentGenerator().addGeneralMethodAnnotation(method, introspectedTable, imports);
    method.setReturnType(FullyQualifiedJavaType.getIntInstance());
    // $NON-NLS-1$
    FullyQualifiedJavaType parameterType = new FullyQualifiedJavaType("java.util.Collection");
    parameterType.addTypeArgument(recordType);
    imports.add(parameterType);
    // $NON-NLS-1$
    method.addParameter(new Parameter(parameterType, "records"));
    String methodName;
    if (Utils.canRetrieveMultiRowGeneratedKeys(introspectedTable)) {
        methodName = "MyBatis3Utils.insertMultipleWithGeneratedKeys";
    } else {
        methodName = "MyBatis3Utils.insertMultiple";
    }
    method.addBodyLine(// $NON-NLS-1$ //$NON-NLS-2$
    "return " + methodName + "(this::insertMultiple, records, " + // $NON-NLS-1$
    tableFieldName + // $NON-NLS-1$
    ", c ->");
    List<IntrospectedColumn> columns = ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns());
    boolean first = true;
    for (IntrospectedColumn column : columns) {
        String fieldName = calculateFieldName(column);
        if (first) {
            method.addBodyLine(// $NON-NLS-1$
            "    c.map(" + fieldName + ").toProperty(\"" + // $NON-NLS-1$
            column.getJavaProperty() + // $NON-NLS-1$
            "\")");
            first = false;
        } else {
            method.addBodyLine(// $NON-NLS-1$
            "    .map(" + fieldName + ").toProperty(\"" + // $NON-NLS-1$
            column.getJavaProperty() + // $NON-NLS-1$
            "\")");
        }
    }
    // $NON-NLS-1$
    method.addBodyLine(");");
    return MethodAndImports.withMethod(method).withImports(imports).build();
}
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) HashSet(java.util.HashSet)

Example 90 with IntrospectedColumn

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

the class DatabaseIntrospector method calculateIntrospectedTables.

private List<IntrospectedTable> calculateIntrospectedTables(TableConfiguration tc, Map<ActualTableName, List<IntrospectedColumn>> columns) {
    boolean delimitIdentifiers = tc.isDelimitIdentifiers() || stringContainsSpace(tc.getCatalog()) || stringContainsSpace(tc.getSchema()) || stringContainsSpace(tc.getTableName());
    List<IntrospectedTable> answer = new ArrayList<>();
    for (Map.Entry<ActualTableName, List<IntrospectedColumn>> entry : columns.entrySet()) {
        ActualTableName atn = entry.getKey();
        // we only use the returned catalog and schema if something was
        // actually
        // specified on the table configuration. If something was returned
        // from the DB for these fields, but nothing was specified on the
        // table
        // configuration, then some sort of DB default is being returned
        // and we don't want that in our SQL
        FullyQualifiedTable table = new FullyQualifiedTable(stringHasValue(tc.getCatalog()) ? atn.getCatalog() : null, stringHasValue(tc.getSchema()) ? atn.getSchema() : null, atn.getTableName(), tc.getDomainObjectName(), tc.getAlias(), isTrue(tc.getProperty(PropertyRegistry.TABLE_IGNORE_QUALIFIERS_AT_RUNTIME)), tc.getProperty(PropertyRegistry.TABLE_RUNTIME_CATALOG), tc.getProperty(PropertyRegistry.TABLE_RUNTIME_SCHEMA), tc.getProperty(PropertyRegistry.TABLE_RUNTIME_TABLE_NAME), delimitIdentifiers, tc.getDomainObjectRenamingRule(), context);
        IntrospectedTable introspectedTable = ObjectFactory.createIntrospectedTable(tc, table, context);
        for (IntrospectedColumn introspectedColumn : entry.getValue()) {
            introspectedTable.addColumn(introspectedColumn);
        }
        calculatePrimaryKey(table, introspectedTable);
        enhanceIntrospectedTable(introspectedTable);
        answer.add(introspectedTable);
    }
    return answer;
}
Also used : IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) FullyQualifiedTable(org.mybatis.generator.api.FullyQualifiedTable) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) IntrospectedTable(org.mybatis.generator.api.IntrospectedTable) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Aggregations

IntrospectedColumn (org.mybatis.generator.api.IntrospectedColumn)107 FullyQualifiedJavaType (org.mybatis.generator.api.dom.java.FullyQualifiedJavaType)36 Method (org.mybatis.generator.api.dom.java.Method)33 XmlElement (org.mybatis.generator.api.dom.xml.XmlElement)32 Attribute (org.mybatis.generator.api.dom.xml.Attribute)28 ArrayList (java.util.ArrayList)26 Parameter (org.mybatis.generator.api.dom.java.Parameter)24 TextElement (org.mybatis.generator.api.dom.xml.TextElement)24 Messages.getString (org.mybatis.generator.internal.util.messages.Messages.getString)14 Field (org.mybatis.generator.api.dom.java.Field)11 FullyQualifiedTable (org.mybatis.generator.api.FullyQualifiedTable)10 CommentGenerator (org.mybatis.generator.api.CommentGenerator)9 TopLevelClass (org.mybatis.generator.api.dom.java.TopLevelClass)9 Plugin (org.mybatis.generator.api.Plugin)8 CompilationUnit (org.mybatis.generator.api.dom.java.CompilationUnit)8 JavaBeansUtil.getJavaBeansField (org.mybatis.generator.internal.util.JavaBeansUtil.getJavaBeansField)7 HashSet (java.util.HashSet)6 List (java.util.List)6 HashMap (java.util.HashMap)5 KotlinFunction (org.mybatis.generator.api.dom.kotlin.KotlinFunction)5