提交 07e2f555 authored 作者: zhangshuang's avatar zhangshuang

zs

上级 0bc27b40
......@@ -2,7 +2,6 @@ package com.zjty.adaptationmaster;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@SpringBootApplication
public class AdaptationMasterApplication {
......
package com.zjty.adaptationmaster.adaptor.entity;
import org.springframework.context.ApplicationContext;
public class SystemVariable {
public static ApplicationContext context;
}
......@@ -20,8 +20,11 @@ public class DBManage {
private String databaseType;//数据库类型
private String address;//连接地址
private String port;//数据库端口号
private String databaseName;//数据库名字
@Transient
private String databaseName;//可连接数据库名字
@Transient
private String connectionType;//数据库连接时的sql
@Transient
private String driver;//数据库驱动
private Integer status;//逻辑删除 0-删除 1-未删除
}
......@@ -6,7 +6,6 @@ import com.zjty.adaptationmaster.adaptor.repository.DBManageDao;
import com.zjty.adaptationmaster.adaptor.service.DBManageService;
import com.zjty.adaptationmaster.base.response.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
......@@ -15,12 +14,6 @@ public class DBManageServiceImpl implements DBManageService {
@Autowired
private DBManageDao databaseDao;
@Value("${highgo.driver}")
private String highDriver;
@Value("${highgo.connectionType}")
private String highConnectionType;
@Value("${highgo.dbName}")
private String highDBName;
/**
* 新增数据库配置文件
......@@ -31,11 +24,6 @@ public class DBManageServiceImpl implements DBManageService {
@Transactional
public ServerResponse saveDatabase(DBManage databaseManagement) {
databaseManagement.setStatus(1);//1-未删除
if(databaseManagement.getDatabaseType().equals("highgo")){
databaseManagement.setDatabaseName(highDBName);
databaseManagement.setDriver(highDriver);
databaseManagement.setConnectionType(highConnectionType);
}
return ServerResponse.success(databaseDao.save(databaseManagement));
}
......
......@@ -12,6 +12,7 @@ import com.zjty.adaptationmaster.utils.Regular;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.BufferedReader;
......@@ -34,6 +35,13 @@ public class DBMigrateServiceImpl implements DBMigrateService {
@Autowired
private DBRecordDao dbRecordDao;
@Value("${highgo.driver}")
private String highgoDriver;
@Value("${highgo.connectionType}")
private String highgoConnnectionType;
@Value("${highgo.dbName}")
private String highgoDBName;
/**
* 创建新数据库和数据库中的表
*/
......@@ -62,6 +70,9 @@ public class DBMigrateServiceImpl implements DBMigrateService {
if(databaseResponse.getCreateType().equals("highgo") && databaseResponse.getSourceType().equals("mysql")){
//生成数据库的名称
//连接系统数据库,建立新数据库
databaseManagement.setDatabaseName(highgoDBName);
databaseManagement.setDriver(highgoDriver);
databaseManagement.setConnectionType(highgoConnnectionType);
Connection connection = Regular.databaseConnection(databaseManagement);
if(connection != null){
PreparedStatement preparedStatement = null;
......@@ -112,7 +123,7 @@ public class DBMigrateServiceImpl implements DBMigrateService {
continue;
}
}
log.info(databaseResponse.getSqlPath() + "sql执行完成");
log.info(databaseResponse.getSqlPath() + "执行完成");
} catch (Exception e) {
e.printStackTrace();
} finally {
......@@ -126,6 +137,8 @@ public class DBMigrateServiceImpl implements DBMigrateService {
}
}
}
}else {
return ServerResponse.error("创建数据库时连接数据库失败");
}
}
//保存迁移记录到数据库
......@@ -165,6 +178,9 @@ public class DBMigrateServiceImpl implements DBMigrateService {
DBManage databaseManagement = dbManageDao.findDBManageByIdAndStatus(id, 1);
//highgo查询所有数据库
if(databaseManagement.getDatabaseType().equals("highgo")){
databaseManagement.setDatabaseName(highgoDBName);
databaseManagement.setDriver(highgoDriver);
databaseManagement.setConnectionType(highgoConnnectionType);
Connection connection = Regular.databaseConnection(databaseManagement);
if (connection != null) {
dbNameReturn.setStatus(1);
......
package com.zjty.adaptationmaster.utils;
import com.zjty.adaptationmaster.adaptor.entity.SystemVariable;
import com.zjty.adaptationmaster.adaptor.entity.db.DBManage;
import org.springframework.core.env.Environment;
import java.io.*;
import java.sql.Connection;
......@@ -18,7 +20,11 @@ public class Regular {
*/
public static String mySqlRegular(String path,String sqlName){
File file = new File(path);
String savePath = file.getParent() + "/" + sqlName + ".sql";//导出的sql地址
File fileParent = new File(file.getParent() + "/transformSql");
if(!fileParent.exists()){
fileParent.mkdirs();
}
String savePath = fileParent.getAbsolutePath() + "/" + sqlName + ".sql";//导出的sql地址
File saveFile = new File(savePath);
FileReader fileReader = null;
BufferedReader bufferedReader = null;
......@@ -33,7 +39,8 @@ public class Regular {
boolean isFirstDrop = false;//去除.sql中使用数据库的sql语句
String tableName = "";//数据库名称
String incrementField = "";//自增长字段
List<String> foreignList = new ArrayList<>();
List<String> foreignList = new ArrayList<>();//外键集合
List<String> indexList = new ArrayList<>();//索引集合
String previous = "";//上一个读取的行
String createTableSql = "";
boolean createLock = true;
......@@ -41,6 +48,17 @@ public class Regular {
while ((s = bufferedReader.readLine()) != null){
s = s.replaceAll("`","\"");
s += "\r\n";
if(lowerAndUpper(s,"INSERT") && lowerAndUpper(s,"INTO")){
//将\0与\1替换为true或false
s = s.replace("'\\0'","'false'");
s = s.replace("'\\1'","'true'");
bufferedWriter.write(s);
continue;
}
//记录自增字段
if(lowerAndUpper(s,"PRIMARY") && lowerAndUpper(s,"KEY")){
incrementField = s.substring(s.indexOf("\"") + 1,s.lastIndexOf("\""));
}
//记录创建表时表的名字
if(lowerAndUpper(s,"CREATE") && lowerAndUpper(s,"TABLE")){
tableName = s.substring(s.indexOf("\"") + 1,s.lastIndexOf("\"")).trim();
......@@ -67,30 +85,30 @@ public class Regular {
}
}
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");
s = StringType(s,"double","real");
//插入值时boolean类型转换
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,"ENGINE") && (lowerAndUpper(s,"CHARSET") || lowerAndUpper(s,"CHARACTER "))){
//String[] array = s.split(" ");
if(lowerAndUpper(s,"AUTO_INCREMENT")){
for(String a:array){
if(lowerAndUpper(a.trim(),"AUTO_INCREMENT")){
String num = a.substring(a.indexOf("=") + 1);
int begin = stringIndex(s,"AUTO_INCREMENT");
for(int i = begin;i < s.length();i++){
String ca = s.charAt(i) + "";
if(ca.matches("\\d")){
s = s.substring(i);
String num = s.substring(0,s.indexOf(" "));
s = ");\r\nSELECT setval('\"" + tableName + "_" + incrementField + "_seq\"'," + (Integer.parseInt(num) - 1) + ", true);\r\n";
break;
}
}
}else {
......@@ -99,7 +117,8 @@ public class Regular {
}
//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";
// ON DELETE NO ACTION ON UPDATE NO ACTION
s = "ALTER TABLE \"public\".\"" + tableName + "\" ADD " + s.replace("\r\n","") + ";\r\n";
foreignList.add(s);
s = "";
}
......@@ -112,30 +131,37 @@ public class Regular {
if(createLock){
if(createTableSql != null && createTableSql != "" && createTableSql.contains(");") && lowerAndUpper(createTableSql,"CREATE") && lowerAndUpper(createTableSql,"TABLE")){
//mysql的外键key值去除
String[] array = createTableSql.split(",");
String[] array = createTableSql.split("\r\n");
createTableSql = "";
for(String a:array){
for(int i = 0;i < array.length;i++){
String startChar = "";
if(a.contains("\"")){
startChar = a.substring(0,a.indexOf("\""));
if(array[i].contains("\"")){
startChar = array[i].substring(0,array[i].indexOf("\""));
}
//不是索引
if(!(lowerAndUpper(startChar,"KEY") && !(lowerAndUpper(startChar,"FOREIGN") || lowerAndUpper(startChar,"PRIMARY")))){
createTableSql += a;
if(!a.contains(");")){
createTableSql += ",";
}
//不是索引,只有KEY
if(!((lowerAndUpper(startChar,"KEY") || lowerAndUpper(startChar,"INDEX")) && !(lowerAndUpper(startChar,"FOREIGN") || lowerAndUpper(startChar,"PRIMARY")))){
if(i != (array.length - 1) && array[i + 1].contains(");") && array[i].charAt(array[i].length() - 1) == ','){
array[i] = array[i].substring(0,array[i].lastIndexOf(","));
}
createTableSql += array[i] + "\r\n";
}else {
//索引
if(lowerAndUpper(a,"USING") && lowerAndUpper(a,"BTREE")) {
}
}
String elseName = "";
int firstYinhaoIndex = array[i].indexOf("\"") + 1;
for(int j = firstYinhaoIndex;j < array[i].length();j++){
if(array[i].charAt(j) == '\"'){
break;
}
elseName += array[i].charAt(j);
}
String indexName = array[i].substring(array[i].indexOf("(") + 1,array[i].lastIndexOf(")"));
String indexSql = "CREATE INDEX " + elseName + " ON " + tableName + "(" + indexName + ");\r\n";
indexList.add(indexSql);
}
}
//逗号去除
String removeCommas = "";
if(createTableSql.contains(",") && createTableSql.contains(");")){
removeCommas = createTableSql.substring(createTableSql.lastIndexOf(",") + 1,createTableSql.lastIndexOf(")"));
removeCommas = createTableSql.substring(createTableSql.lastIndexOf(",") + 1,createTableSql.lastIndexOf(");"));
}
if(removeCommas.equals("\r\n")){
String startChar = createTableSql.substring(0,createTableSql.lastIndexOf(","));
......@@ -154,6 +180,10 @@ public class Regular {
for(String list:foreignList){
bufferedWriter.write(list);
}
//写入索引的sql语句
for(String list:indexList){
bufferedWriter.write(list);
}
bufferedWriter.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
......@@ -183,6 +213,14 @@ public class Regular {
}
}
public static int stringIndex(String s,String upper){
int index = s.indexOf(upper);
if(index == -1){
index = s.indexOf(upper.toLowerCase());
}
return index;
}
/**
* 大小写都替换
*/
......@@ -233,12 +271,14 @@ public class Regular {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e){
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
mySqlRegular("C:\\Users\\admin\\Desktop\\database\\hrm.sql","debug");
mySqlRegular("C:\\Users\\admin\\Desktop\\database\\hrm.sql","hrm");
}
}
......@@ -38,4 +38,4 @@ spring.resources.static-locations=classpath:/uploads/
#highgo
highgo.driver=org.postgresql.Driver
highgo.connectionType=postgresql
highgo.dbName=template0
\ No newline at end of file
highgo.dbName=template1
\ No newline at end of file
......@@ -2,8 +2,13 @@ package com.zjty.adaptationmaster;
public class Test4 {
public static void main(String[] args) {
String a = "\r\n KEY \"FK14pq56p21er9aref4rplla3ef\" (\"routine_routine_id\")\n" +
");";
System.out.println(a.substring(0,a.indexOf("\"")));
String a = "sdaf = df ccc = 18 adfasdf = utf8";
int begin= a.indexOf("ccc");
for(int i = begin;i < a.length();i++){
String m = a.charAt(i) + "";
if(m.matches("\\d")){
System.out.println(m);
}
}
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论