skyouc
2025-02-17 708a39865b7b0d7a0aae3fad98c282aaa8b93b13
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package com.vincent.rsf.framework.generators.domain;
 
import com.vincent.rsf.framework.common.Cools;
import com.vincent.rsf.framework.generators.CoolGenerator;
import com.vincent.rsf.framework.generators.constant.SqlOsType;
import com.vincent.rsf.framework.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;
 
/**
 * Created by vincent on 2019-06-18
 */
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; // 外健实例名(大驼峰,如 sys_user ==> User)
    private String foreignKeyMajor; // 外键显示字段(大驼峰,如 name ==> Name)
    private List<Map<String, Object>> enums; // 枚举值
    private Integer length; // 字段长度
 
    public Column(Connection conn, String name, String type, String comment, boolean primaryKey, boolean mainKey, boolean notNull, Integer length, boolean init, SqlOsType sqlOsType) {
        this.name = name;
        this.type = type;
        this.comment = "";
        if (!Cools.isEmpty(comment)){
            // 枚举
            Pattern pattern1 = Pattern.compile("(.+?)(?=\\{)");
            Matcher matcher1 = pattern1.matcher(comment);
            // 外键
            Pattern pattern11 = Pattern.compile("(.+?)(?=\\[)");
            Matcher matcher11 = pattern11.matcher(comment);
            // 枚举
            if (matcher1.find()) {
                this.comment = matcher1.group();
                Pattern pattern2 = Pattern.compile("(?<=\\{)(.+?)(?=})");
                Matcher matcher2 = pattern2.matcher(comment);
                if (matcher2.find()) {
                    String group = matcher2.group();
                    if (!Cools.isEmpty(group)) {
                        String[] values = group.split(",");
                        this.enums = new ArrayList<>();
                        for (String val : values) {
                            Map<String, Object> map = new HashMap<>();
                            String[] split = val.split(":");
                            map.put(split[0], split[1]);
                            enums.add(map);
                        }
                    }
                }
            //  外键
            } else if (matcher11.find()){
                this.comment = matcher11.group();
                Pattern pattern22 = Pattern.compile("(?<=\\[)(.+?)(?=])");
                Matcher matcher22 = pattern22.matcher(comment);
                if (matcher22.find()) {
                    String group = matcher22.group();
                    if (!Cools.isEmpty(group)) {
                        this.foreignKey = GeneratorUtils.getNameSpace(group);
                        List<Column> foreignColumns = new ArrayList<>();
                        if (init) {
                            try {
                                switch (sqlOsType) {
                                    case MYSQL:
                                        foreignColumns = CoolGenerator.getMysqlColumns(conn, group, false, sqlOsType);
                                        break;
                                    case SQL_SERVER:
                                        foreignColumns = CoolGenerator.getSqlServerColumns(conn, group, false, sqlOsType);
                                        break;
                                    default:
                                        break;
                                }
 
                            } catch (Exception e){
                                e.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;
            }
        }
//        if (primaryKey || mainKey){
//            this.primaryKey = true;
//        } else {
//            this.primaryKey = false;
//        }
        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(final String name) {
        this.name = name;
    }
 
    public String getType() {
        return type;
    }
 
    public void setType(final String type) {
        this.type = type;
    }
 
    public String getComment() {
        return comment;
    }
 
    public String getWholeComment() {
        if (!Cools.isEmpty(this.enums)){
            StringBuilder sb = new StringBuilder(" ");
            for (Map<String, Object> val : enums){
                for (Map.Entry<String, Object> entry : val.entrySet()){
                    sb.append(entry.getKey())
                            .append(": ")
                            .append(entry.getValue())
                            .append("  ");
                }
            }
            return comment + sb.toString();
        }
        return comment;
    }
 
    public void setComment(final String comment) {
        this.comment = comment;
    }
 
    public String getHumpName() {
        return humpName;
    }
 
//    public boolean isPrimaryKey() {
//        return primaryKey || mainKey;
//    }
 
    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(final boolean notNull) {
        this.notNull = notNull;
    }
 
    public String getForeignKey() {
        return foreignKey;
    }
 
    public void setForeignKey(final String foreignKey) {
        this.foreignKey = foreignKey;
    }
 
    public String getForeignKeyMajor() {
        return foreignKeyMajor;
    }
 
    public void setForeignKeyMajor(final 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(final Integer length) {
        this.length = length;
    }
 
    public boolean isMajor() {
        return major;
    }
 
    public void setMajor(final boolean major) {
        this.major = major;
    }
 
    public boolean isImage() {
        return image;
    }
 
    public void setImage(final 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 "Column{" +
                "name='" + name + '\'' +
                ", type='" + type + '\'' +
                ", comment='" + comment + '\'' +
                ", humpName='" + humpName + '\'' +
                ", primaryKey=" + primaryKey +
                ", notNull=" + notNull +
                ", major=" + major +
                ", image=" + image +
                ", foreignKey='" + foreignKey + '\'' +
                ", foreignKeyMajor='" + foreignKeyMajor + '\'' +
                ", enums=" + enums +
                ", length=" + length +
                '}';
    }
 
}