Commit 68533ec0 authored by 黎博's avatar 黎博

优化生成insert语句

parent 7dff7e82
......@@ -32,7 +32,7 @@ public class DatabaseSyncServiceImpl implements DatabaseSyncService {
@Override
public boolean getDbInfoFromSource(String ip, String port, String username, String password, String dbName) {
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://" + ip + ":" + port + "/" + dbName + "?zeroDateTimeBehavior=convertToNull";
String url = "jdbc:mysql://" + ip + ":" + port + "/" + dbName;
String createTableKey = dbSyncPrefix + dbName + ":create" ;
String insertTableKey = dbSyncPrefix + dbName + ":insert";
String dbRedisKey = dbSyncPrefix + dbName;
......@@ -77,6 +77,7 @@ public class DatabaseSyncServiceImpl implements DatabaseSyncService {
createTableRedisValue.add(pResultSet.getString(2));
}
if (!redisUtils.hasKey(insertTableKey)) {
ResultSet columnResultSet = databaseMetaData.getColumns(null, "%", tableName, "%");
while (columnResultSet.next()) {
......@@ -105,9 +106,7 @@ public class DatabaseSyncServiceImpl implements DatabaseSyncService {
ResultSet selectResultSet = preparedStatement.executeQuery();
while (selectResultSet.next()) {
String rowValues = getRowValues(selectResultSet, columnNameList.size(), columnTypeList);
String insertSql = String.format("insert into %s (%s) values(%s);", tableName, columnArrayStr,
rowValues);
log.info(insertSql);
String insertSql = String.format("insert into %s (%s) values(%s);", tableName, columnArrayStr, rowValues);
insertSql = insertSql.replaceAll("\n", "<br/>");
insertSql = insertSql + "\n";
insertRedisValue += insertSql;
......@@ -180,8 +179,11 @@ public class DatabaseSyncServiceImpl implements DatabaseSyncService {
// 从redis中同步表数据
String insertTableRedisValue = redisUtils.get(insertTableKey).toString();
preparedStatement = newConnection.prepareStatement(insertTableRedisValue);
preparedStatement.execute();
for (String insertSql: insertTableRedisValue.split("\n")) {
log.info(insertSql);
preparedStatement = newConnection.prepareStatement(insertSql);
preparedStatement.execute();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
......@@ -271,6 +273,13 @@ public class DatabaseSyncServiceImpl implements DatabaseSyncService {
columnValue = getValue(rs, i, columnTypeList.get(i - 1));
// 如果是空值不添加单引号
if (null != columnValue) {
// if (columnValue.contains("'")) {
// columnValue = "\"" + columnValue + "\"";
// } else {
// columnValue = "'" + columnValue + "'";
// }
columnValue = columnValue.replace("'", "\\'");
columnValue = columnValue.replace("\"", "\\\"");
columnValue = "'" + columnValue + "'";
}
// 拼接字段值
......@@ -305,13 +314,18 @@ public class DatabaseSyncServiceImpl implements DatabaseSyncService {
}
return intValue + "";
} else if ("bigint".equals(columnType) || "BIGINT".equals(columnType)) {
// 长整形
Object value = resultSet.getObject(index);
if (null == value) {
return null;
}
return value + "";
} else if ( "bigint unsigned".equals(columnType) || "BIGINT UNSIGNED".equals(columnType)) {
Object value = resultSet.getObject(index);
if (null == value) {
return null;
}
return value + "";
} else if ("smallint".equals(columnType) || "SMALLINT".equals(columnType)) {
// 整数
Object value = resultSet.getObject(index);
......@@ -407,12 +421,16 @@ public class DatabaseSyncServiceImpl implements DatabaseSyncService {
// 时间类型:范围 1970-01-01 00:00:00/2037 年某时 格式 YYYYMMDD HHMMSS 混合日期和时间值,时间戳
return resultSet.getString(index);
} else {
return null;
Object value = resultSet.getObject(index);
if (null == value) {
return null;
}
return value + "";
}
} catch (Exception e) {
e.printStackTrace();
System.err.println("获取数据库类型值异常");
return null;
return "0000-00-00 00:00:00";
}
}
......
......@@ -82,54 +82,55 @@ public class SqlExecutionUtils {
// }
// 获取行
ResultSet columnResultSet = databaseMetaData.getColumns(null, "%", tableName, "%");
while (columnResultSet.next()) {
String columnName = columnResultSet.getString("COLUMN_NAME");
// 数据类型
String columnType = columnResultSet.getString("TYPE_NAME");
// 字段长度
int datasize = columnResultSet.getInt("COLUMN_SIZE");
// 小数部分位数
int digits = columnResultSet.getInt("DECIMAL_DIGITS");
// 是否可为空 1代表可空 0代表不可为空
int nullable = columnResultSet.getInt("NULLABLE");
// 描述
String remarks = columnResultSet.getString("REMARKS");
columnNameList.add(columnName);
columnTypeList.add(columnType);
commentList.add(remarks);
// System.out.println(columnName + " " + columnType + " " + datasize + " " + digits + " " + nullable + " " + remarks);
}
System.out.println(columnNameList);
System.out.println(columnTypeList);
System.out.println(commentList);
String columnArrayStr = null;
for (String column : columnNameList) {
if (null == columnArrayStr) {
columnArrayStr = "`" + column + "`";
} else {
columnArrayStr = columnArrayStr + "," + "`" + column + "`";
}
}
String selectSQL = String.format("select %s from %s", columnArrayStr, tableName);
System.out.println(selectSQL);
PreparedStatement selectPstmt = connection.prepareStatement(selectSQL);
ResultSet selResultSet = selectPstmt.executeQuery();
while (selResultSet.next()) {
String rowValues = getRowValues(selResultSet, columnNameList.size(), columnTypeList);
String insertSql = String.format("insert into %s (%s) values(%s);", tableName, columnArrayStr,
rowValues);
// System.out.println(insertSql);
insertSql = insertSql.replaceAll("\n", "<br/>");
insertSql = insertSql + "\n";
dbBackupInfo += insertSql;
}
columnNameList.clear();
columnTypeList.clear();
commentList.clear();
selectPstmt.close();
// ResultSet columnResultSet = databaseMetaData.getColumns(null, "%", tableName, "%");
// while (columnResultSet.next()) {
// String columnName = columnResultSet.getString("COLUMN_NAME");
// // 数据类型
// String columnType = columnResultSet.getString("TYPE_NAME");
// // 字段长度
// int datasize = columnResultSet.getInt("COLUMN_SIZE");
// // 小数部分位数
// int digits = columnResultSet.getInt("DECIMAL_DIGITS");
// // 是否可为空 1代表可空 0代表不可为空
// int nullable = columnResultSet.getInt("NULLABLE");
// // 描述
// String remarks = columnResultSet.getString("REMARKS");
// columnNameList.add(columnName);
// columnTypeList.add(columnType);
// commentList.add(remarks);
//// System.out.println(columnName + " " + columnType + " " + datasize + " " + digits + " " + nullable + " " + remarks);
// }
// System.out.println(columnNameList);
// System.out.println(columnTypeList);
// System.out.println(commentList);
//
// String columnArrayStr = null;
// for (String column : columnNameList) {
// if (null == columnArrayStr) {
// columnArrayStr = "`" + column + "`";
// } else {
// columnArrayStr = columnArrayStr + "," + "`" + column + "`";
// }
// }
//
// String selectSQL = String.format("select %s from %s", columnArrayStr, tableName);
// System.out.println(selectSQL);
// PreparedStatement selectPstmt = connection.prepareStatement(selectSQL);
// ResultSet selResultSet = selectPstmt.executeQuery();
// while (selResultSet.next()) {
// String rowValues = getRowValues(selResultSet, columnNameList.size(), columnTypeList);
// String insertSql = String.format("insert into %s (%s) values(%s);", tableName, columnArrayStr,
// rowValues);
//// System.out.println(insertSql);
// insertSql = insertSql.replaceAll("\n", "<br/>");
// insertSql = insertSql + "\n";
// dbBackupInfo += insertSql;
// }
// columnNameList.clear();
// columnTypeList.clear();
// commentList.clear();
// selectPstmt.close();
// }
}
System.out.println(dbBackupInfo);
connection.commit();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment