Search in sources :

Example 1 with FullyQualifiedKotlinType

use of org.mybatis.generator.api.dom.kotlin.FullyQualifiedKotlinType in project generator by mybatis.

the class KotlinDynamicSqlSupportClassGenerator method handleColumn.

private void handleColumn(KotlinFile kotlinFile, KotlinType outerObject, KotlinType innerClass, String tableFieldName, IntrospectedColumn column) {
    FullyQualifiedKotlinType kt = JavaToKotlinTypeConverter.convert(column.getFullyQualifiedJavaType());
    kotlinFile.addImports(kt.getImportList());
    String fieldName = column.getJavaProperty();
    // outer object
    if (fieldName.equals(tableFieldName)) {
        // name collision, skip the shortcut field
        warnings.add(// $NON-NLS-1$
        Messages.getString(// $NON-NLS-1$
        "Warning.29", fieldName, getSupportObjectImport()));
    } else {
        KotlinProperty prop = KotlinProperty.newVal(fieldName).withInitializationString(tableFieldName + "." + fieldName).build();
        outerObject.addNamedItem(prop);
    }
    // inner class
    KotlinProperty property = KotlinProperty.newVal(fieldName).withInitializationString(calculateInnerInitializationString(column, kt)).build();
    innerClass.addNamedItem(property);
}
Also used : KotlinProperty(org.mybatis.generator.api.dom.kotlin.KotlinProperty) FullyQualifiedKotlinType(org.mybatis.generator.api.dom.kotlin.FullyQualifiedKotlinType)

Example 2 with FullyQualifiedKotlinType

use of org.mybatis.generator.api.dom.kotlin.FullyQualifiedKotlinType in project generator by mybatis.

the class KotlinFragmentGenerator method getGeneratedKeyAnnotation.

public KotlinFunctionParts getGeneratedKeyAnnotation(GeneratedKey gk) {
    KotlinFunctionParts.Builder builder = new KotlinFunctionParts.Builder();
    StringBuilder sb = new StringBuilder();
    introspectedTable.getColumn(gk.getColumn()).ifPresent(introspectedColumn -> {
        if (gk.isJdbcStandard()) {
            // $NON-NLS-1$
            builder.withImport("org.apache.ibatis.annotations.Options");
            // $NON-NLS-1$
            sb.append("@Options(useGeneratedKeys=true,keyProperty=\"row.");
            sb.append(introspectedColumn.getJavaProperty());
            // $NON-NLS-1$
            sb.append("\")");
            builder.withAnnotation(sb.toString());
        } else {
            // $NON-NLS-1$
            builder.withImport("org.apache.ibatis.annotations.SelectKey");
            FullyQualifiedKotlinType kt = JavaToKotlinTypeConverter.convert(introspectedColumn.getFullyQualifiedJavaType());
            // $NON-NLS-1$
            sb.append("@SelectKey(statement=[\"");
            sb.append(gk.getRuntimeSqlStatement());
            // $NON-NLS-1$
            sb.append("\"], keyProperty=\"row.");
            sb.append(introspectedColumn.getJavaProperty());
            // $NON-NLS-1$
            sb.append("\", before=");
            // $NON-NLS-1$ //$NON-NLS-2$
            sb.append(gk.isIdentity() ? "false" : "true");
            // $NON-NLS-1$
            sb.append(", resultType=");
            sb.append(kt.getShortNameWithoutTypeArguments());
            // $NON-NLS-1$
            sb.append("::class)");
            builder.withAnnotation(sb.toString());
        }
    });
    return builder.build();
}
Also used : FullyQualifiedKotlinType(org.mybatis.generator.api.dom.kotlin.FullyQualifiedKotlinType)

Example 3 with FullyQualifiedKotlinType

use of org.mybatis.generator.api.dom.kotlin.FullyQualifiedKotlinType in project generator by mybatis.

the class KotlinFragmentGenerator method getPrimaryKeyWhereClauseAndParameters.

public KotlinFunctionParts getPrimaryKeyWhereClauseAndParameters() {
    KotlinFunctionParts.Builder builder = new KotlinFunctionParts.Builder();
    boolean first = true;
    for (IntrospectedColumn column : introspectedTable.getPrimaryKeyColumns()) {
        FullyQualifiedKotlinType kt = JavaToKotlinTypeConverter.convert(column.getFullyQualifiedJavaType());
        AbstractKotlinFunctionGenerator.FieldNameAndImport fieldNameAndImport = AbstractKotlinFunctionGenerator.calculateFieldNameAndImport(tableFieldName, supportObjectImport, column);
        // $NON-NLS-1$
        String argName = column.getJavaProperty() + "_";
        builder.withImport(fieldNameAndImport.importString());
        builder.withImports(kt.getImportList());
        builder.withArgument(KotlinArg.newArg(argName).withDataType(kt.getShortNameWithTypeArguments()).build());
        if (first) {
            builder.withCodeLine(// $NON-NLS-1$
            "    where { " + fieldNameAndImport.fieldName() + " isEqualTo " + // $NON-NLS-1$
            argName + // $NON-NLS-1$
            " }");
            first = false;
        } else {
            builder.withCodeLine(// $NON-NLS-1$
            "    and { " + fieldNameAndImport.fieldName() + " isEqualTo " + // $NON-NLS-1$
            argName + // $NON-NLS-1$
            " }");
        }
    }
    // $NON-NLS-1$
    builder.withCodeLine("}");
    return builder.build();
}
Also used : IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) FullyQualifiedKotlinType(org.mybatis.generator.api.dom.kotlin.FullyQualifiedKotlinType)

Example 4 with FullyQualifiedKotlinType

use of org.mybatis.generator.api.dom.kotlin.FullyQualifiedKotlinType in project generator by mybatis.

the class KotlinFragmentGenerator method getResultAnnotation.

private String getResultAnnotation(Set<String> imports, IntrospectedColumn introspectedColumn, boolean idColumn) {
    StringBuilder sb = new StringBuilder();
    // $NON-NLS-1$
    sb.append("Result(column=\"");
    sb.append(escapeStringForKotlin(introspectedColumn.getActualColumnName()));
    // $NON-NLS-1$
    sb.append("\", property=\"");
    sb.append(introspectedColumn.getJavaProperty());
    sb.append('\"');
    if (stringHasValue(introspectedColumn.getTypeHandler())) {
        FullyQualifiedKotlinType fqjt = new FullyQualifiedKotlinType(introspectedColumn.getTypeHandler());
        imports.add(introspectedColumn.getTypeHandler());
        // $NON-NLS-1$
        sb.append(", typeHandler=");
        sb.append(fqjt.getShortNameWithoutTypeArguments());
        // $NON-NLS-1$
        sb.append("::class");
    }
    // $NON-NLS-1$
    sb.append(", jdbcType=JdbcType.");
    sb.append(introspectedColumn.getJdbcTypeName());
    if (idColumn) {
        // $NON-NLS-1$
        sb.append(", id=true");
    }
    sb.append(')');
    return sb.toString();
}
Also used : FullyQualifiedKotlinType(org.mybatis.generator.api.dom.kotlin.FullyQualifiedKotlinType)

Example 5 with FullyQualifiedKotlinType

use of org.mybatis.generator.api.dom.kotlin.FullyQualifiedKotlinType 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)

Aggregations

FullyQualifiedKotlinType (org.mybatis.generator.api.dom.kotlin.FullyQualifiedKotlinType)8 IntrospectedColumn (org.mybatis.generator.api.IntrospectedColumn)2 KotlinFile (org.mybatis.generator.api.dom.kotlin.KotlinFile)2 KotlinProperty (org.mybatis.generator.api.dom.kotlin.KotlinProperty)2 KotlinType (org.mybatis.generator.api.dom.kotlin.KotlinType)2 CommentGenerator (org.mybatis.generator.api.CommentGenerator)1 FullyQualifiedTable (org.mybatis.generator.api.FullyQualifiedTable)1