Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
H
holmes
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
QA
holmes
Commits
68533ec0
Commit
68533ec0
authored
Jun 19, 2021
by
黎博
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
优化生成insert语句
parent
7dff7e82
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
76 additions
and
57 deletions
+76
-57
DatabaseSyncServiceImpl.java
...g/holmes/service/effect/impl/DatabaseSyncServiceImpl.java
+27
-9
SqlExecutionUtils.java
src/main/java/cn/qg/holmes/utils/SqlExecutionUtils.java
+49
-48
No files found.
src/main/java/cn/qg/holmes/service/effect/impl/DatabaseSyncServiceImpl.java
View file @
68533ec0
...
...
@@ -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"
;
}
}
...
...
src/main/java/cn/qg/holmes/utils/SqlExecutionUtils.java
View file @
68533ec0
...
...
@@ -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
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment