-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqlUtils.java
More file actions
253 lines (234 loc) · 7.27 KB
/
SqlUtils.java
File metadata and controls
253 lines (234 loc) · 7.27 KB
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
package ericzz.utils;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
import java.util.Map;
import java.util.Set;
@Slf4j
public class SqlUtils {
/**
* 生成占位符
*
* @param paramLen
* @return
*/
public static String genPlaces(int paramLen) {
StringBuffer result = new StringBuffer();
for (int i = 0; i < paramLen; i++) {
if (i == paramLen - 1) {
result.append("?");
} else {
result.append("?,");
}
}
return result.toString();
}
/**
* 分割表字段
*
* @param columns
* @param speator
* @return
*/
public static String columnsSpeator(List<String> columns, char speator) {
return StringUtils.join(columns, speator);
}
/**
* 分割表字段
*
* @param columns
* @return
*/
public static String columnsSpeator(List<String> columns) {
return StringUtils.join(columns, ',');
}
/**
* 构建查询语句
*
* @param tableName
* @param columns
* @param conditions
* @return
*/
public static String buildSelectSql(String tableName, List<String> columns, Map<String, Object> conditions) {
StringBuffer sql = new StringBuffer();
sql.append("select ").append(columnsSpeator(columns)).append(" from").append(" ").append(tableName).append(" ");
sql.append(conditionSql(conditions));
return sql.toString();
}
public static String buildSelectSql(String tableName, List<String> columns) {
StringBuffer sql = new StringBuffer();
sql.append("select ").append(columnsSpeator(columns)).append(" from").append(" ").append(tableName).append(" ");
return sql.toString();
}
/**
* 拼装条件语句 只支持 =
*
* @param conditions
* @return
*/
public static String conditionSql(Map<String, Object> conditions) {
StringBuffer sql = new StringBuffer();
if (conditions != null && !conditions.isEmpty()) {
sql.append("where ");
int index = 0;
Set<Map.Entry<String, Object>> entries = conditions.entrySet();
for (Map.Entry<String, Object> entry : entries) {
String key = entry.getKey();
Object value = entry.getValue();
sql.append(key).append(" ").append("=").append(" ").append(value);
if (index != conditions.size() - 1) {
sql.append(" and ");
}
index++;
}
}
return sql.toString();
}
/**
* 删除语句
*
* @param tableName
* @param conditions
* @return
*/
public static String buildDeleteSql(String tableName, Map<String, Object> conditions) {
StringBuffer sql = new StringBuffer();
sql.append("delete ").append(" from ").append(tableName).append(" ");
sql.append(conditionSql(conditions));
return sql.toString();
}
/**
* 清空表sql
*
* @param tableName
* @return
*/
public static String truncateTableSql(String tableName) {
return "truncate table " + tableName + "";
}
/**
* 复制表数据
*
* @param tableName
* @param rowdatas
* @param columns
* @return
*/
public static List<Object[]> copyRowsData(String tableName, List<Map<String, Object>> rowdatas,
List<String> columns, Map<String, String> transColumns, Map<String, Object> defauleValues) {
List<Object[]> argsList = Lists.newArrayList();
for (Map<String, Object> item : rowdatas) {
Object[] row = new Object[columns.size()];
for (int i = 0; i < columns.size(); i++) {
String columnName = columns.get(i);
if (StringUtils.equals(columnName, "course_id") && item.containsKey("course_static_id")) {
row[i] = item.get("course_static_id");
} else if (StringUtils.equals(columnName, "id") && item.containsKey(tableName + "_id")) {
row[i] = item.get(tableName + "_id");
} else {
row[i] = item.get(columnName);
}
if (transColumns != null && !transColumns.isEmpty()) {
if (transColumns.containsKey(columnName)) {
row[i] = item.get(transColumns.get(columnName));
}
}
if (defauleValues != null && !defauleValues.isEmpty()) {
if (defauleValues.containsKey(columnName)) {
row[i] = defauleValues.get(columnName);
}
}
}
argsList.add(row);
}
return argsList;
}
/**
* 构建查询语句
*
* @param tableName
* @param columns
* @return
*/
public static String buildInsertSql(String tableName, List<String> columns) {
String insertSql = "insert into " + tableName + " (" + columnsSpeator(columns) + ") " + "values ("
+ genPlaces(columns.size()) + ")";
return insertSql;
}
/**
* 迁移表
*
* @param sourceTableName
* 原表
* @param sourceTableColumns
* 原表字段
* @param targetTableName
* 目标表
* @param targetableColumns
* 目标迁移字段
*/
public static void tableFullCopay(JdbcTemplate sourceJdbc, String sourceTableName, List<String> sourceTableColumns,
JdbcTemplate targetJdbc, String targetTableName, List<String> targetableColumns,
Map<String, String> transColumns, Map<String, Object> defauleValues) {
Long startTime = System.currentTimeMillis();
log.info("开始迁移表【{}】", sourceTableName);
String querySql = SqlUtils.buildSelectSql(sourceTableName, sourceTableColumns, null);
List<Map<String, Object>> migrateList = sourceJdbc.queryForList(querySql);
if (CollectionUtils.isNotEmpty(migrateList)) {
log.info("本次迁移数据为【{}】", migrateList.size());
targetJdbc.update(SqlUtils.truncateTableSql(targetTableName));
List<Object[]> argsList = SqlUtils.copyRowsData(sourceTableName, migrateList, targetableColumns,
transColumns, defauleValues);
String insertSql = SqlUtils.buildInsertSql(targetTableName, targetableColumns);
targetJdbc.batchUpdate(insertSql, argsList);
log.info("本次迁移数据完成【{}】,耗时【{}】", sourceTableName, System.currentTimeMillis() - startTime);
} else {
log.info("表 【{}】无需迁移", sourceTableName);
}
}
/**
*
* @param sourceJdbc
* @param sourceTableName
* @param sourceTableColumns
* @param targetJdbc
* @param targetTableName
* @param targetableColumns
*/
public static void tableFullCopay(JdbcTemplate sourceJdbc, String sourceTableName, List<String> sourceTableColumns,
JdbcTemplate targetJdbc, String targetTableName, List<String> targetableColumns,
Map<String, String> transColumns) {
tableFullCopay(sourceJdbc, sourceTableName, sourceTableColumns, targetJdbc, targetTableName, targetableColumns,
transColumns, null);
}
/**
*
* @param sourceJdbc
* @param sourceTableName
* @param sourceTableColumns
* @param targetJdbc
* @param targetTableName
* @param targetableColumns
*/
public static void tableFullCopay(JdbcTemplate sourceJdbc, String sourceTableName, List<String> sourceTableColumns,
JdbcTemplate targetJdbc, String targetTableName, List<String> targetableColumns) {
tableFullCopay(sourceJdbc, sourceTableName, sourceTableColumns, targetJdbc, targetTableName, targetableColumns,
null, null);
}
/**
* @param sourceJdbc
* @param targetJdbc
* @param tableName
* @param columns
*/
public static void tableFullCopay(JdbcTemplate sourceJdbc, JdbcTemplate targetJdbc, String tableName,
List<String> columns) {
tableFullCopay(sourceJdbc, tableName, columns, targetJdbc, tableName, columns, null, null);
}
public static void main(String[] args) {
}
}