#
Junjie
昨天 536e17a446c170a8b214aaf188b98817ee6258c1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package com.core.generators.domain;
 
import com.core.common.Cools;
import com.core.generators.CoolGenerator;
import com.core.generators.constant.SqlOsType;
import com.core.generators.utils.GeneratorUtils;
 
import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class Column {
 
    private String name;
    private String type;
    private String comment;
    private String humpName;
    private boolean primaryKey;
    private boolean mainKey;
    private boolean notNull;
    private boolean major;
    private boolean image;
    private boolean checkBox;
    private String foreignKey;
    private String foreignKeyMajor;
    private List<Map<String, Object>> enums;
    private Integer length;
 
    public Column(Connection connection, String name, String type, String comment, boolean primaryKey,
                  boolean mainKey, boolean notNull, Integer length, boolean withForeignKey, SqlOsType sqlOsType) {
        this.name = name;
        this.type = type;
        this.comment = "";
        if (!Cools.isEmpty(comment)) {
            Pattern enumTitlePattern = Pattern.compile("(.+?)(?=\\{)");
            Matcher enumTitleMatcher = enumTitlePattern.matcher(comment);
            Pattern foreignTitlePattern = Pattern.compile("(.+?)(?=\\[)");
            Matcher foreignTitleMatcher = foreignTitlePattern.matcher(comment);
            if (enumTitleMatcher.find()) {
                this.comment = enumTitleMatcher.group();
                Pattern enumPattern = Pattern.compile("(?<=\\{)(.+?)(?=})");
                Matcher enumMatcher = enumPattern.matcher(comment);
                if (enumMatcher.find()) {
                    String enumText = enumMatcher.group();
                    if (!Cools.isEmpty(enumText)) {
                        String[] enumArr = enumText.split(",");
                        this.enums = new ArrayList<>();
                        for (String item : enumArr) {
                            Map<String, Object> map = new HashMap<>();
                            String[] pair = item.split(":");
                            if (pair.length >= 2) {
                                map.put(pair[0], pair[1]);
                                this.enums.add(map);
                            }
                        }
                    }
                }
            } else if (foreignTitleMatcher.find()) {
                this.comment = foreignTitleMatcher.group();
                Pattern foreignPattern = Pattern.compile("(?<=\\[)(.+?)(?=])");
                Matcher foreignMatcher = foreignPattern.matcher(comment);
                if (foreignMatcher.find()) {
                    String foreignTable = foreignMatcher.group();
                    if (!Cools.isEmpty(foreignTable)) {
                        this.foreignKey = GeneratorUtils.getNameSpace(foreignTable);
                        List<Column> foreignColumns = new ArrayList<>();
                        if (withForeignKey) {
                            try {
                                switch (sqlOsType) {
                                    case MYSQL:
                                        foreignColumns = CoolGenerator.getMysqlColumns(connection, foreignTable, false, sqlOsType);
                                        break;
                                    case SQL_SERVER:
                                        foreignColumns = CoolGenerator.getSqlServerColumns(connection, foreignTable, false, sqlOsType);
                                        break;
                                    default:
                                        break;
                                }
                            } catch (Exception ex) {
                                ex.printStackTrace();
                            }
                        }
                        if (!Cools.isEmpty(foreignColumns)) {
                            for (Column column : foreignColumns) {
                                if (column.isMajor()) {
                                    this.foreignKeyMajor = GeneratorUtils.firstCharConvert(column.getHumpName(), false);
                                }
                            }
                            if (Cools.isEmpty(this.foreignKeyMajor)) {
                                this.foreignKeyMajor = "Id";
                            }
                        }
                    }
                }
            } else {
                this.comment = comment;
            }
            if (comment.endsWith("(*)")) {
                this.comment = comment.substring(0, comment.length() - 3);
                this.major = true;
            }
            if (comment.endsWith("(img)")) {
                this.comment = comment.substring(0, comment.length() - 5);
                this.image = true;
            }
            if (comment.endsWith("(checkbox)")) {
                this.comment = comment.substring(0, comment.length() - 10);
                this.checkBox = true;
            }
        }
        this.primaryKey = primaryKey;
        this.mainKey = mainKey;
        this.notNull = notNull;
        this.length = length;
        this.humpName = GeneratorUtils._convert(name);
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public String getType() {
        return type;
    }
 
    public void setType(String type) {
        this.type = type;
    }
 
    public String getComment() {
        return comment;
    }
 
    public String getWholeComment() {
        if (!Cools.isEmpty(this.enums)) {
            StringBuilder builder = new StringBuilder("");
            for (Map<String, Object> map : this.enums) {
                for (Map.Entry<String, Object> entry : map.entrySet()) {
                    builder.append(entry.getKey()).append(":").append(entry.getValue()).append("  ");
                }
            }
            return new StringBuilder().append(this.comment).append(builder.toString()).toString();
        }
        return this.comment;
    }
 
    public void setComment(String comment) {
        this.comment = comment;
    }
 
    public String getHumpName() {
        return humpName;
    }
 
    public boolean isPrimaryKey() {
        return primaryKey;
    }
 
    public boolean isOnly() {
        return primaryKey;
    }
 
    public void setPrimaryKey(boolean primaryKey) {
        this.primaryKey = primaryKey;
    }
 
    public boolean isNotNull() {
        return notNull;
    }
 
    public void setNotNull(boolean notNull) {
        this.notNull = notNull;
    }
 
    public String getForeignKey() {
        return foreignKey;
    }
 
    public void setForeignKey(String foreignKey) {
        this.foreignKey = foreignKey;
    }
 
    public String getForeignKeyMajor() {
        return foreignKeyMajor;
    }
 
    public void setForeignKeyMajor(String foreignKeyMajor) {
        this.foreignKeyMajor = foreignKeyMajor;
    }
 
    public List<Map<String, Object>> getEnums() {
        return enums;
    }
 
    public void setEnums(List<Map<String, Object>> enums) {
        this.enums = enums;
    }
 
    public Integer getLength() {
        return length;
    }
 
    public void setLength(Integer length) {
        this.length = length;
    }
 
    public boolean isMajor() {
        return major;
    }
 
    public void setMajor(boolean major) {
        this.major = major;
    }
 
    public boolean isImage() {
        return image;
    }
 
    public void setImage(boolean image) {
        this.image = image;
    }
 
    public boolean isCheckBox() {
        return checkBox;
    }
 
    public void setCheckBox(boolean checkBox) {
        this.checkBox = checkBox;
    }
 
    public boolean isMainKey() {
        return mainKey;
    }
 
    public void setMainKey(boolean mainKey) {
        this.mainKey = mainKey;
    }
 
    @Override
    public String toString() {
        return new StringBuilder()
                .append("Column{name='").append(name).append('\'')
                .append(", type='").append(type).append('\'')
                .append(", comment='").append(comment).append('\'')
                .append(", humpName='").append(humpName).append('\'')
                .append(", primaryKey=").append(primaryKey)
                .append(", notNull=").append(notNull)
                .append(", major=").append(major)
                .append(", image=").append(image)
                .append(", foreignKey='").append(foreignKey).append('\'')
                .append(", foreignKeyMajor='").append(foreignKeyMajor).append('\'')
                .append(", enums=").append(enums)
                .append(", length=").append(length)
                .append('}')
                .toString();
    }
}