为提高效率,提问时请提供以下信息,问题描述清晰可优先响应。
【DM版本】: DM8
【操作系统】:
【CPU】:
【问题描述】*:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DMInit {
public static void main(String[] args) {
System.out.println("程序开始!!!");
dmInitFile();
// System.out.println(findTableName("CREATE TABL \"ACT_EVT_LOG\""));
// System.out.println(findKeyName(" \"TYPE_\" VARCHAR(64) NULL,"));
// System.out.println(findAlterPrimaryKeyTableName(
// "ALTER TABLE \"SYS_DICT_TYPE\" ADD CONSTRAINT PRIMARY
// KEY(\"DICT_TYPE\") ;"));
System.out.println("程序结束!!!");
}
public static void dmInitFile() {
// 读文件变量
String filePath;
FileInputStream fis = null;
InputStreamReader isr = null;
BufferedReader br = null;
// 写文件变量
File wfilePath;
FileOutputStream fos = null;
OutputStreamWriter osw = null;
BufferedWriter bw = null;
String str;
String newStr;
int count = 0;
Set<String> tableKeySet = new HashSet<String>();
System.out.println("开始读取,时间:" + new Date());
try {
String tableName = null;
String tempStr;
boolean autoFlag = false;
String keyStr = null;
filePath = "C:/Users/Administrator/Desktop/dm初始化脚本.sql";
fis = new FileInputStream(filePath);
isr = new InputStreamReader(fis, "UTF-8");
br = new BufferedReader(isr);
wfilePath = new File("C:/Users/Administrator/Desktop/new_dm初始化脚本.sql");
fos = new FileOutputStream(wfilePath);
osw = new OutputStreamWriter(fos, "UTF-8");
bw = new BufferedWriter(osw);
while ((str = br.readLine()) != null) {
count++;
System.out.println(str);
if (str.contains("SET IDENTITY_INSERT")) {
continue;
}
if (str.contains("PRIMARY KEY")) {
tempStr = findAlterPrimaryKeyTableName(str);
if (tableKeySet.contains(tempStr)) {
continue;
}
}
if ((tempStr = findTableName(str)) != null) {
tableName = tempStr;
}
newStr = str;
if (str.contains("IDENTITY(1,1)")) {
keyStr = findKeyName(str);
autoFlag = true;
tableKeySet.add(tableName);
newStr = str.replace("IDENTITY(1,1)", "AUTO_INCREMENT");
}
if (str.contains(");")) {
if (autoFlag) {
newStr = ",PRIMARY KEY(\"" + keyStr + "\")" + newStr;
}
autoFlag = false;
}
bw.write(newStr);
bw.newLine();
}
bw.flush();
System.out.println("行数:" + count);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (isr != null) {
try {
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (osw != null) {
try {
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("读取结束,时间:" + new Date());
}
}
public static String findTableName(String str) {
String tableName;
tableName = getRgString("CREATE TABLE \"(\\w+)\"", str);
return tableName;
}
public static String findKeyName(String str) {
String keyName;
keyName = getRgString("\"(\\w+)\".*IDENTITY\\(1,1\\).*", str);
return keyName;
}
public static String findAlterPrimaryKeyTableName(String str) {
String findStr;
findStr = getRgString("ALTER TABLE \"(\\w+)\".*PRIMARY KEY", str);
return findStr;
}
/**
* 查找出匹配的字符串
*
* @param pattern
* @param value
* @return
*/
public static String getRgString(String pattern, String value) {
Pattern prn = Pattern.compile(pattern);
Matcher matcher = prn.matcher(value);
if (matcher.find()) {
return matcher.group(1);
}
return null;
}
}
identity是列自增属性,级联使用cascade