1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.generator.runtime.kotlin.elements;
17
18 import org.mybatis.generator.api.IntrospectedColumn;
19 import org.mybatis.generator.api.IntrospectedTable;
20 import org.mybatis.generator.api.dom.kotlin.KotlinArg;
21 import org.mybatis.generator.api.dom.kotlin.KotlinFile;
22 import org.mybatis.generator.api.dom.kotlin.KotlinFunction;
23 import org.mybatis.generator.config.Context;
24
25 public abstract class AbstractKotlinFunctionGenerator {
26 protected final Context context;
27 protected final IntrospectedTable introspectedTable;
28 protected final String tableFieldName;
29
30 protected AbstractKotlinFunctionGenerator(BaseBuilder<?> builder) {
31 context = builder.context;
32 introspectedTable = builder.introspectedTable;
33 tableFieldName = builder.tableFieldName;
34 }
35
36 protected void acceptParts(KotlinFile kotlinFile, KotlinFunction kotlinFunction,
37 KotlinFunctionParts functionParts) {
38 for (KotlinArg argument : functionParts.getArguments()) {
39 kotlinFunction.addArgument(argument);
40 }
41
42 for (String annotation : functionParts.getAnnotations()) {
43 kotlinFunction.addAnnotation(annotation);
44 }
45
46 kotlinFunction.addCodeLines(functionParts.getCodeLines());
47 kotlinFile.addImports(functionParts.getImports());
48 }
49
50 protected void acceptParts(KotlinFunctionAndImports functionAndImports, KotlinFunctionParts functionParts) {
51 for (KotlinArg argument : functionParts.getArguments()) {
52 functionAndImports.getFunction().addArgument(argument);
53 }
54
55 for (String annotation : functionParts.getAnnotations()) {
56 functionAndImports.getFunction().addAnnotation(annotation);
57 }
58
59 functionAndImports.getFunction().addCodeLines(functionParts.getCodeLines());
60 functionAndImports.getImports().addAll(functionParts.getImports());
61 }
62
63 protected void addFunctionComment(KotlinFunctionAndImports functionAndImports) {
64 context.getCommentGenerator().addGeneralFunctionComment(functionAndImports.getFunction(), introspectedTable,
65 functionAndImports.getImports());
66 }
67
68 public static FieldNameAndImport calculateFieldNameAndImport(String tableFieldName, String supportObjectImport,
69 IntrospectedColumn column) {
70 FieldNameAndImport answer = new FieldNameAndImport();
71 answer.fieldName = column.getJavaProperty();
72 if (answer.fieldName.equals(tableFieldName)) {
73
74 answer.fieldName = tableFieldName + "." + answer.fieldName;
75 answer.importString = supportObjectImport + "." + tableFieldName;
76 } else {
77 answer.importString = supportObjectImport + "." + answer.fieldName;
78 }
79 return answer;
80 }
81
82 public static class FieldNameAndImport {
83 private String fieldName;
84 private String importString;
85
86 public String fieldName() {
87 return fieldName;
88 }
89
90 public String importString() {
91 return importString;
92 }
93 }
94
95 public abstract KotlinFunctionAndImports generateMethodAndImports();
96
97 public abstract boolean callPlugins(KotlinFunction method, KotlinFile kotlinFile);
98
99 public abstract static class BaseBuilder<T extends BaseBuilder<T>> {
100 private Context context;
101 private IntrospectedTable introspectedTable;
102 private String tableFieldName;
103
104 public T withContext(Context context) {
105 this.context = context;
106 return getThis();
107 }
108
109 public T withTableFieldName(String tableFieldName) {
110 this.tableFieldName = tableFieldName;
111 return getThis();
112 }
113
114 public T withIntrospectedTable(IntrospectedTable introspectedTable) {
115 this.introspectedTable = introspectedTable;
116 return getThis();
117 }
118
119 public abstract T getThis();
120 }
121 }