|
1 | 1 | /*
|
2 |
| - * Copyright 2006-2022 the original author or authors. |
| 2 | + * Copyright 2006-2023 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
@@ -47,63 +47,61 @@ private KotlinFragmentGenerator(Builder builder) {
|
47 | 47 | tableFieldName = builder.tableFieldName;
|
48 | 48 | }
|
49 | 49 |
|
50 |
| - public KotlinFunctionParts getPrimaryKeyWhereClauseAndParameters() { |
| 50 | + public KotlinFunctionParts getPrimaryKeyWhereClauseAndParameters(boolean forUpdate) { |
51 | 51 | KotlinFunctionParts.Builder builder = new KotlinFunctionParts.Builder();
|
52 | 52 |
|
| 53 | + int columnCount = introspectedTable.getPrimaryKeyColumns().size(); |
53 | 54 | boolean first = true;
|
54 | 55 | 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 | + } |
56 | 67 |
|
57 | 68 | AbstractKotlinFunctionGenerator.FieldNameAndImport fieldNameAndImport =
|
58 | 69 | AbstractKotlinFunctionGenerator.calculateFieldNameAndImport(tableFieldName,
|
59 | 70 | supportObjectImport, column);
|
60 |
| - String argName = column.getJavaProperty() + "_"; //$NON-NLS-1$ |
61 | 71 |
|
62 | 72 | 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)); |
71 | 79 | first = false;
|
72 | 80 | } 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)); |
76 | 82 | }
|
77 | 83 | }
|
78 |
| - builder.withCodeLine("}"); //$NON-NLS-1$ |
| 84 | + if (columnCount > 1) { |
| 85 | + builder.withCodeLine(" }"); //$NON-NLS-1$ |
| 86 | + } |
79 | 87 |
|
80 | 88 | return builder.build();
|
81 | 89 | }
|
82 | 90 |
|
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 | + } |
85 | 94 |
|
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 | + } |
91 | 98 |
|
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 | + } |
105 | 102 |
|
106 |
| - return builder.build(); |
| 103 | + private String composeIsEqualTo(String columName, String property) { |
| 104 | + return columName + " isEqualTo " + property; //$NON-NLS-1$ |
107 | 105 | }
|
108 | 106 |
|
109 | 107 | public KotlinFunctionParts getAnnotatedResults() {
|
|
0 commit comments