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

优化生成insert语句

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