Skip to content

Commit 53c1801

Browse files
authored
Merge pull request #943 from jeffgbutler/refactor-kotlin-where-clauses
Refactor Generated Kotlin Where Clauses
2 parents cf03728 + c1740c8 commit 53c1801

File tree

6 files changed

+48
-46
lines changed

6 files changed

+48
-46
lines changed

core/mybatis-generator-core/src/main/java/org/mybatis/generator/runtime/kotlin/elements/DeleteByPrimaryKeyMethodGenerator.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2022 the original author or authors.
2+
* Copyright 2006-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -46,8 +46,9 @@ public KotlinFunctionAndImports generateMethodAndImports() {
4646

4747
addFunctionComment(functionAndImports);
4848

49-
KotlinFunctionParts functionParts = fragmentGenerator.getPrimaryKeyWhereClauseAndParameters();
49+
KotlinFunctionParts functionParts = fragmentGenerator.getPrimaryKeyWhereClauseAndParameters(false);
5050
acceptParts(functionAndImports, functionParts);
51+
functionAndImports.getFunction().getCodeLines().add("}"); //$NON-NLS-1$
5152

5253
return functionAndImports;
5354
}

core/mybatis-generator-core/src/main/java/org/mybatis/generator/runtime/kotlin/elements/KotlinFragmentGenerator.java

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2022 the original author or authors.
2+
* Copyright 2006-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -47,63 +47,61 @@ private KotlinFragmentGenerator(Builder builder) {
4747
tableFieldName = builder.tableFieldName;
4848
}
4949

50-
public KotlinFunctionParts getPrimaryKeyWhereClauseAndParameters() {
50+
public KotlinFunctionParts getPrimaryKeyWhereClauseAndParameters(boolean forUpdate) {
5151
KotlinFunctionParts.Builder builder = new KotlinFunctionParts.Builder();
5252

53+
int columnCount = introspectedTable.getPrimaryKeyColumns().size();
5354
boolean first = true;
5455
for (IntrospectedColumn column : introspectedTable.getPrimaryKeyColumns()) {
55-
FullyQualifiedKotlinType kt = JavaToKotlinTypeConverter.convert(column.getFullyQualifiedJavaType());
56+
String argName;
57+
if (forUpdate) {
58+
argName = "row." + column.getJavaProperty() + "!!"; //$NON-NLS-1$ //$NON-NLS-2$
59+
} else {
60+
argName = column.getJavaProperty() + "_"; //$NON-NLS-1$
61+
FullyQualifiedKotlinType kt = JavaToKotlinTypeConverter.convert(column.getFullyQualifiedJavaType());
62+
builder.withImports(kt.getImportList());
63+
builder.withArgument(KotlinArg.newArg(argName)
64+
.withDataType(kt.getShortNameWithTypeArguments())
65+
.build());
66+
}
5667

5768
AbstractKotlinFunctionGenerator.FieldNameAndImport fieldNameAndImport =
5869
AbstractKotlinFunctionGenerator.calculateFieldNameAndImport(tableFieldName,
5970
supportObjectImport, column);
60-
String argName = column.getJavaProperty() + "_"; //$NON-NLS-1$
6171

6272
builder.withImport(fieldNameAndImport.importString());
63-
builder.withImports(kt.getImportList());
64-
builder.withArgument(KotlinArg.newArg(argName)
65-
.withDataType(kt.getShortNameWithTypeArguments())
66-
.build());
67-
if (first) {
68-
builder.withCodeLine(" where { " + fieldNameAndImport.fieldName() //$NON-NLS-1$
69-
+ " isEqualTo " + argName //$NON-NLS-1$
70-
+ " }"); //$NON-NLS-1$
73+
if(columnCount == 1) {
74+
builder.withCodeLine(singleColumnWhere(fieldNameAndImport.fieldName(), argName));
75+
first = false;
76+
} else if (first) {
77+
builder.withCodeLine(" where {"); //$NON-NLS-1$
78+
builder.withCodeLine(multiColumnWhere(fieldNameAndImport.fieldName(), argName));
7179
first = false;
7280
} else {
73-
builder.withCodeLine(" and { " + fieldNameAndImport.fieldName() //$NON-NLS-1$
74-
+ " isEqualTo " + argName //$NON-NLS-1$
75-
+ " }"); //$NON-NLS-1$
81+
builder.withCodeLine(multiColumnAnd(fieldNameAndImport.fieldName(), argName));
7682
}
7783
}
78-
builder.withCodeLine("}"); //$NON-NLS-1$
84+
if (columnCount > 1) {
85+
builder.withCodeLine(" }"); //$NON-NLS-1$
86+
}
7987

8088
return builder.build();
8189
}
8290

83-
public KotlinFunctionParts getPrimaryKeyWhereClauseForUpdate() {
84-
KotlinFunctionParts.Builder builder = new KotlinFunctionParts.Builder();
91+
private String singleColumnWhere(String columName, String property) {
92+
return " where { " + composeIsEqualTo(columName, property) + " }"; //$NON-NLS-1$ //$NON-NLS-2$
93+
}
8594

86-
boolean first = true;
87-
for (IntrospectedColumn column : introspectedTable.getPrimaryKeyColumns()) {
88-
AbstractKotlinFunctionGenerator.FieldNameAndImport fieldNameAndImport =
89-
AbstractKotlinFunctionGenerator.calculateFieldNameAndImport(tableFieldName,
90-
supportObjectImport, column);
95+
private String multiColumnWhere(String columName, String property) {
96+
return " " + composeIsEqualTo(columName, property); //$NON-NLS-1$
97+
}
9198

92-
builder.withImport(fieldNameAndImport.importString());
93-
if (first) {
94-
builder.withCodeLine(" where { " + fieldNameAndImport.fieldName() //$NON-NLS-1$
95-
+ " isEqualTo row." + column.getJavaProperty() //$NON-NLS-1$
96-
+ "!! }"); //$NON-NLS-1$
97-
first = false;
98-
} else {
99-
builder.withCodeLine(" and {" + fieldNameAndImport.fieldName() //$NON-NLS-1$
100-
+ " isEqualTo row." + column.getJavaProperty() //$NON-NLS-1$
101-
+ "!! }"); //$NON-NLS-1$
102-
}
103-
}
104-
builder.withCodeLine("}"); //$NON-NLS-1$
99+
private String multiColumnAnd(String columName, String property) {
100+
return " and { " + composeIsEqualTo(columName, property) + " }"; //$NON-NLS-1$ //$NON-NLS-2$
101+
}
105102

106-
return builder.build();
103+
private String composeIsEqualTo(String columName, String property) {
104+
return columName + " isEqualTo " + property; //$NON-NLS-1$
107105
}
108106

109107
public KotlinFunctionParts getAnnotatedResults() {

core/mybatis-generator-core/src/main/java/org/mybatis/generator/runtime/kotlin/elements/SelectByPrimaryKeyMethodGenerator.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2022 the original author or authors.
2+
* Copyright 2006-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -43,8 +43,9 @@ public KotlinFunctionAndImports generateMethodAndImports() {
4343

4444
addFunctionComment(functionAndImports);
4545

46-
KotlinFunctionParts functionParts = fragmentGenerator.getPrimaryKeyWhereClauseAndParameters();
46+
KotlinFunctionParts functionParts = fragmentGenerator.getPrimaryKeyWhereClauseAndParameters(false);
4747
acceptParts(functionAndImports, functionParts);
48+
functionAndImports.getFunction().getCodeLines().add("}"); //$NON-NLS-1$
4849

4950
return functionAndImports;
5051
}

core/mybatis-generator-core/src/main/java/org/mybatis/generator/runtime/kotlin/elements/UpdateByPrimaryKeyMethodGenerator.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2022 the original author or authors.
2+
* Copyright 2006-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -58,8 +58,9 @@ public KotlinFunctionAndImports generateMethodAndImports() {
5858
KotlinFunctionParts functionParts = fragmentGenerator.getSetEqualLines(columns);
5959
acceptParts(functionAndImports, functionParts);
6060

61-
functionParts = fragmentGenerator.getPrimaryKeyWhereClauseForUpdate();
61+
functionParts = fragmentGenerator.getPrimaryKeyWhereClauseAndParameters(true);
6262
acceptParts(functionAndImports, functionParts);
63+
functionAndImports.getFunction().getCodeLines().add("}"); //$NON-NLS-1$
6364

6465
return functionAndImports;
6566
}

core/mybatis-generator-core/src/main/java/org/mybatis/generator/runtime/kotlin/elements/UpdateByPrimaryKeySelectiveMethodGenerator.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2022 the original author or authors.
2+
* Copyright 2006-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -58,8 +58,9 @@ public KotlinFunctionAndImports generateMethodAndImports() {
5858
KotlinFunctionParts functionParts = fragmentGenerator.getSetEqualWhenPresentLines(columns);
5959
acceptParts(functionAndImports, functionParts);
6060

61-
functionParts = fragmentGenerator.getPrimaryKeyWhereClauseForUpdate();
61+
functionParts = fragmentGenerator.getPrimaryKeyWhereClauseAndParameters(true);
6262
acceptParts(functionAndImports, functionParts);
63+
functionAndImports.getFunction().getCodeLines().add("}"); //$NON-NLS-1$
6364

6465
return functionAndImports;
6566
}

core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
33
4-
Copyright 2006-2022 the original author or authors.
4+
Copyright 2006-2023 the original author or authors.
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.

0 commit comments

Comments
 (0)