Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
A
adaptation-master
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
912协同工作系统
项目监控管理工具
adaptation-master
Commits
370cb070
提交
370cb070
authored
12月 12, 2019
作者:
zhangshuang
浏览文件
操作
浏览文件
下载
差异文件
合并分支 'zs' 到 'master'
zs 查看合并请求 ty_wyl/adaptation-master1!10
上级
68bb64f1
4650fdec
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
95 行增加
和
82 行删除
+95
-82
DBManage.java
...com/zjty/adaptationmaster/adaptor/entity/db/DBManage.java
+2
-2
Regular.java
src/main/java/com/zjty/adaptationmaster/utils/Regular.java
+90
-69
Test4.java
src/test/java/com/zjty/adaptationmaster/Test4.java
+3
-11
没有找到文件。
src/main/java/com/zjty/adaptationmaster/adaptor/entity/db/DBManage.java
浏览文件 @
370cb070
...
...
@@ -21,9 +21,9 @@ public class DBManage {
private
String
userName
;
//用户名
private
String
password
;
//密码
private
String
databaseType
;
//数据库类型
private
String
driver
;
//数据库驱动
//
private String driver;//数据库驱动
private
String
address
;
//连接地址
private
String
port
;
//数据库端口号
private
String
connectionType
;
//数据库连接时的sql
//
private String connectionType;//数据库连接时的sql
private
Integer
status
;
//逻辑删除 0-删除 1-未删除
}
src/main/java/com/zjty/adaptationmaster/utils/Regular.java
浏览文件 @
370cb070
...
...
@@ -18,7 +18,7 @@ public class Regular {
*/
public
static
String
mySqlRegular
(
String
path
,
String
sqlName
){
File
file
=
new
File
(
path
);
String
savePath
=
file
.
getParent
()
+
"/"
+
sqlName
+
".sql"
;
String
savePath
=
file
.
getParent
()
+
"/"
+
sqlName
+
".sql"
;
//导出的sql地址
File
saveFile
=
new
File
(
savePath
);
FileReader
fileReader
=
null
;
BufferedReader
bufferedReader
=
null
;
...
...
@@ -30,112 +30,126 @@ public class Regular {
fileWriter
=
new
FileWriter
(
saveFile
);
bufferedWriter
=
new
BufferedWriter
(
fileWriter
);
String
s
=
""
;
boolean
database
=
tru
e
;
//去除.sql中使用数据库的sql语句
String
tableName
=
null
;
String
incrementField
=
null
;
boolean
isFirstDrop
=
fals
e
;
//去除.sql中使用数据库的sql语句
String
tableName
=
""
;
//数据库名称
String
incrementField
=
""
;
//自增长字段
List
<
String
>
foreignList
=
new
ArrayList
<>();
String
previous
=
null
;
String
previous
=
""
;
//上一个读取的行
String
createTableSql
=
""
;
boolean
createLock
=
true
;
boolean
executeOne
=
true
;
while
((
s
=
bufferedReader
.
readLine
())
!=
null
){
s
=
s
.
replaceAll
(
"`"
,
"\""
);
s
+=
"\r\n"
;
//记录创建表时表的名字
if
(
lowerAndUpper
(
s
,
"CREATE"
)
&&
lowerAndUpper
(
s
,
"TABLE"
)){
tableName
=
s
.
substring
(
s
.
indexOf
(
"\""
)
+
1
,
s
.
lastIndexOf
(
"\""
)).
trim
();
createLock
=
false
;
}
//转换mysql的int和bigint类型
if
(
lowerAndUpper
(
s
,
"INT("
)){
String
dataType
=
s
.
substring
(
s
.
lastIndexOf
(
"\""
)
+
1
,
s
.
indexOf
(
"("
)).
trim
();
String
startChar
=
s
.
substring
(
0
,
s
.
indexOf
(
"("
));
String
endChar
=
s
.
substring
(
s
.
indexOf
(
")"
)
+
1
);
if
(
dataType
.
length
()
<=
4
){
//int类型
String
startChar
=
s
.
substring
(
0
,
s
.
indexOf
(
"("
));
String
endChar
=
s
.
substring
(
s
.
indexOf
(
")"
)
+
1
);
s
=
startChar
+
"4"
+
endChar
;
if
(
lowerAndUpper
(
s
,
"AUTO_INCREMENT"
)){
s
=
replaceLowerAndUpper
(
s
,
"AUTO_INCREMENT"
,
""
);
s
=
replaceLowerAndUpper
(
s
,
"INT4"
,
"serial"
);
incrementField
=
s
.
substring
(
s
.
indexOf
(
"\""
)
+
1
,
s
.
lastIndexOf
(
"\""
));
}
}
else
{
s
=
startChar
+
endChar
;
//bigint类型
if
(
lowerAndUpper
(
s
,
"BIGINT"
)){
String
startChar
=
s
.
substring
(
0
,
s
.
indexOf
(
"("
));
String
endChar
=
s
.
substring
(
s
.
indexOf
(
")"
)
+
1
);
s
=
startChar
+
endChar
;
if
(
lowerAndUpper
(
s
,
"AUTO_INCREMENT"
)){
s
=
replaceLowerAndUpper
(
s
,
"AUTO_INCREMENT"
,
""
);
s
=
replaceLowerAndUpper
(
s
,
"BIGINT"
,
"bigserial"
);
incrementField
=
s
.
substring
(
s
.
indexOf
(
"\""
)
+
1
,
s
.
lastIndexOf
(
"\""
));
}
}
else
if
(
lowerAndUpper
(
s
,
"SMALLINT"
)){
String
startChar
=
s
.
substring
(
0
,
s
.
indexOf
(
"("
));
String
endChar
=
s
.
substring
(
s
.
indexOf
(
")"
)
+
1
);
s
=
startChar
+
endChar
;
if
(
lowerAndUpper
(
s
,
"AUTO_INCREMENT"
)){
s
=
replaceLowerAndUpper
(
s
,
"AUTO_INCREMENT"
,
""
);
s
=
replaceLowerAndUpper
(
s
,
"SMALLINT"
,
"smallserial"
);
incrementField
=
s
.
substring
(
s
.
indexOf
(
"\""
)
+
1
,
s
.
lastIndexOf
(
"\""
));
}
if
(
lowerAndUpper
(
s
,
"BIGINT"
)
&&
lowerAndUpper
(
s
,
"AUTO_INCREMENT"
)){
s
=
replaceLowerAndUpper
(
s
,
"BIGINT"
,
"bigserial"
);
}
else
if
(
lowerAndUpper
(
s
,
"SMALLINT"
)
&&
lowerAndUpper
(
s
,
"AUTO_INCREMENT"
)){
s
=
replaceLowerAndUpper
(
s
,
"SMALLINT"
,
"smallserial"
);
}
}
s
=
replaceLowerAndUpper
(
s
,
"AUTO_INCREMENT"
,
""
);
incrementField
=
s
.
substring
(
s
.
indexOf
(
"\""
)
+
1
,
s
.
lastIndexOf
(
"\""
));
}
//转换mysql的时间datetime类型
s
=
StringType
(
s
,
"datetime"
,
"timestamp"
);
//转换mysql的double类型
s
=
StringType
(
s
,
"double"
,
"float"
);
//插入值时boolean类型转换
if
(
s
.
contains
(
"bit(1)"
)){
s
=
replaceLowerAndUpper
(
s
,
"bit(1)"
,
"boolean"
);
}
if
(
s
.
contains
(
"'\\0'"
)){
s
=
s
.
replace
(
"'\\0'"
,
"'false'"
);
}
if
(
s
.
contains
(
"'\\1'"
)){
s
=
s
.
replace
(
"'\\1'"
,
"true"
);
}
//记录创建表时表的名字
if
(
lowerAndUpper
(
s
,
"CREATE"
)
&&
lowerAndUpper
(
s
,
"TABLE"
)){
tableName
=
s
.
substring
(
s
.
indexOf
(
"\""
)
+
1
,
s
.
lastIndexOf
(
"\""
)).
trim
();
s
=
replaceLowerAndUpper
(
s
,
"BIT(1)"
,
"boolean"
);
//将\0与\1替换为true或false
s
=
s
.
replace
(
"'\\0'"
,
"'false'"
);
s
=
s
.
replace
(
"'\\1'"
,
"'true'"
);
//从第一个创建表的可执行命令开始
if
(
lowerAndUpper
(
s
,
"DROP"
)
&&
lowerAndUpper
(
s
,
"TABLE"
)
&&
executeOne
){
isFirstDrop
=
true
;
executeOne
=
false
;
}
//去除表类型的定义
if
(
lowerAndUpper
(
s
,
"ENGINE"
)
&&
lowerAndUpper
(
s
,
"CHARSET"
)){
String
[]
array
=
s
.
split
(
" "
);
if
(
lowerAndUpper
(
s
,
"AUTO_INCREMENT"
)){
String
[]
array
=
s
.
split
(
" "
);
for
(
String
a:
array
){
if
(
lowerAndUpper
(
a
.
trim
(),
"AUTO_INCREMENT"
)){
String
num
=
a
.
substring
(
a
.
indexOf
(
"="
)
+
1
);
String
alterIncrementInital
=
"SELECT setval('\""
+
tableName
+
"_"
+
incrementField
+
"_seq\"',"
+
(
Integer
.
parseInt
(
num
)
-
1
)
+
", true);\r\n"
;
s
=
");\r\n"
;
s
=
s
+
alterIncrementInital
;
s
=
");\r\nSELECT setval('\""
+
tableName
+
"_"
+
incrementField
+
"_seq\"',"
+
(
Integer
.
parseInt
(
num
)
-
1
)
+
", true);\r\n"
;
}
}
}
else
{
s
=
");\r\n"
;
}
}
//去除.sql中使用数据库的sql语句
if
(
s
.
contains
(
"USE"
)
&&
s
.
contains
(
";"
)
&&
s
.
contains
(
"\""
)
&&
database
){
database
=
false
;
}
else
{
//逗号去除
//mysql的外键key值去除
if
(
previous
!=
null
){
if
(
lowerAndUpper
(
s
,
"KEY"
)
&&
!
lowerAndUpper
(
s
,
"FOREIGN"
)
&&
!
lowerAndUpper
(
s
,
"PRIMARY"
))
{
String
startChar
=
s
.
substring
(
0
,
s
.
indexOf
(
"\""
));
if
(
lowerAndUpper
(
startChar
,
"KEY"
)){
previous
=
previous
.
substring
(
0
,
previous
.
lastIndexOf
(
","
))
+
"\r\n"
;
s
=
""
;
//msql的外键设置移动位置
if
(
lowerAndUpper
(
s
,
"FOREIGN"
)
&&
lowerAndUpper
(
s
,
"KEY"
)
&&
lowerAndUpper
(
s
,
"CONSTRAINT"
)){
s
=
"ALTER TABLE \"public\".\""
+
tableName
+
"\" ADD "
+
s
+
" ON DELETE NO ACTION ON UPDATE NO ACTION;\r\n"
;
foreignList
.
add
(
s
);
s
=
""
;
}
if
(
previous
!=
null
&&
isFirstDrop
){
//对创建表再做一次检查
if
(
lowerAndUpper
(
s
,
");"
)){
createLock
=
true
;
}
createTableSql
+=
s
;
if
(
createLock
){
if
(
createTableSql
!=
null
&&
createTableSql
!=
""
&&
createTableSql
.
contains
(
");"
)
&&
lowerAndUpper
(
createTableSql
,
"CREATE"
)
&&
lowerAndUpper
(
createTableSql
,
"TABLE"
)){
//mysql的外键key值去除
String
[]
array
=
createTableSql
.
split
(
","
);
createTableSql
=
""
;
for
(
String
a:
array
){
String
startChar
=
""
;
if
(
a
.
contains
(
"\""
)){
startChar
=
a
.
substring
(
0
,
a
.
indexOf
(
"\""
));
}
//不是索引
if
(!(
lowerAndUpper
(
startChar
,
"KEY"
)
&&
!(
lowerAndUpper
(
startChar
,
"FOREIGN"
)
||
lowerAndUpper
(
startChar
,
"PRIMARY"
)))){
createTableSql
+=
a
;
if
(!
a
.
contains
(
");"
)){
createTableSql
+=
","
;
}
}
else
{
//索引
if
(
lowerAndUpper
(
a
,
"USING"
)
&&
lowerAndUpper
(
a
,
"BTREE"
))
{
}
}
}
//逗号去除
String
removeCommas
=
""
;
if
(
createTableSql
.
contains
(
","
)
&&
createTableSql
.
contains
(
");"
)){
removeCommas
=
createTableSql
.
substring
(
createTableSql
.
lastIndexOf
(
","
)
+
1
,
createTableSql
.
lastIndexOf
(
")"
));
}
if
(
removeCommas
.
equals
(
"\r\n"
)){
String
startChar
=
createTableSql
.
substring
(
0
,
createTableSql
.
lastIndexOf
(
","
));
String
endChar
=
createTableSql
.
substring
(
createTableSql
.
lastIndexOf
(
","
)
+
1
);
createTableSql
=
startChar
+
endChar
;
}
}
//msql的外键设置移动位置
if
(
lowerAndUpper
(
s
,
"FOREIGN"
)
&&
lowerAndUpper
(
s
,
"KEY"
)
&&
lowerAndUpper
(
s
,
"CONSTRAINT"
)){
s
=
"ALTER TABLE \"public\".\""
+
tableName
+
"\" ADD "
+
s
+
" ON DELETE NO ACTION ON UPDATE NO ACTION;\r\n"
;
foreignList
.
add
(
s
);
s
=
""
;
}
else
{
bufferedWriter
.
write
(
previous
);
}
previous
=
createTableSql
;
createTableSql
=
""
;
bufferedWriter
.
write
(
previous
);
}
previous
=
s
;
}
previous
=
s
;
}
//写入最后一句
bufferedWriter
.
write
(
previous
);
//写入创建外键的sql语句
for
(
String
list:
foreignList
){
bufferedWriter
.
write
(
list
);
...
...
@@ -210,9 +224,16 @@ public class Regular {
*/
public
static
Connection
databaseConnection
(
DBManage
databaseManagement
){
Connection
connection
=
null
;
String
driver
=
null
;
String
connectionType
=
null
;
//highgo
if
(
databaseManagement
.
getDatabaseType
().
equals
(
"highgo"
)){
driver
=
"org.postgresql.Driver"
;
connectionType
=
"postgresql"
;
}
try
{
Class
.
forName
(
d
atabaseManagement
.
getDriver
()
);
String
connectionUrl
=
"jdbc:"
+
databaseManagement
.
getConnectionType
()
+
":"
+
"//"
+
databaseManagement
.
getAddress
()
+
":"
+
databaseManagement
.
getPort
()
+
"/"
+
databaseManagement
.
getDatabaseName
();
Class
.
forName
(
d
river
);
String
connectionUrl
=
"jdbc:"
+
connectionType
+
":"
+
"//"
+
databaseManagement
.
getAddress
()
+
":"
+
databaseManagement
.
getPort
()
+
"/"
+
databaseManagement
.
getDatabaseName
();
connection
=
DriverManager
.
getConnection
(
connectionUrl
,
databaseManagement
.
getUserName
(),
databaseManagement
.
getPassword
());
return
connection
;
}
catch
(
ClassNotFoundException
e
)
{
...
...
@@ -225,6 +246,6 @@ public class Regular {
public
static
void
main
(
String
[]
args
)
{
mySqlRegular
(
"C:\\Users\\admin\\Desktop\\database\\hrm.sql"
,
"
else
"
);
mySqlRegular
(
"C:\\Users\\admin\\Desktop\\database\\hrm.sql"
,
"
debug
"
);
}
}
src/test/java/com/zjty/adaptationmaster/Test4.java
浏览文件 @
370cb070
...
...
@@ -2,16 +2,8 @@ package com.zjty.adaptationmaster;
public
class
Test4
{
public
static
void
main
(
String
[]
args
)
{
String
a
=
"CREATE TABLE \"routine_human_list\" (\n"
+
" \"routine_routine_id\" varchar(255) NOT NULL,\n"
+
" \"human_list\" varchar(255) DEFAULT NULL,\n"
+
")"
;
String
endChar
=
a
.
substring
(
a
.
lastIndexOf
(
"\""
)
+
1
);
if
(
endChar
.
contains
(
","
)){
String
start
=
a
.
substring
(
0
,
a
.
lastIndexOf
(
","
));
String
end
=
a
.
substring
(
a
.
lastIndexOf
(
","
)
+
1
);
a
=
start
+
end
;
System
.
out
.
println
(
a
);
}
String
a
=
"\r\n KEY \"FK14pq56p21er9aref4rplla3ef\" (\"routine_routine_id\")\n"
+
");"
;
System
.
out
.
println
(
a
.
substring
(
0
,
a
.
indexOf
(
"\""
)));
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论