From 39815aaeb317c656eac4c67555ffb1148fb081b3 Mon Sep 17 00:00:00 2001 From: liulin <lin.liu@aliyun.com> Date: 星期五, 02 八月 2024 17:00:24 +0800 Subject: [PATCH] mysql迁移数据库到gauss --- src/test/java/com/lunhan/xxx/host/GenCodeGauss.java | 264 +++++++++++++++++++++++++++++++++++++++++++++++----- 1 files changed, 236 insertions(+), 28 deletions(-) diff --git a/src/test/java/com/lunhan/xxx/host/GenCodeGauss.java b/src/test/java/com/lunhan/xxx/host/GenCodeGauss.java index de0f69b..86923dd 100644 --- a/src/test/java/com/lunhan/xxx/host/GenCodeGauss.java +++ b/src/test/java/com/lunhan/xxx/host/GenCodeGauss.java @@ -1,10 +1,7 @@ package com.lunhan.xxx.host; import com.lunhan.xxx.common.ConstantFactory; -import com.lunhan.xxx.common.util.ListUtil; -import com.lunhan.xxx.common.util.LocalDateTimeUtil; -import com.lunhan.xxx.common.util.NumericUtil; -import com.lunhan.xxx.common.util.StringUtil; +import com.lunhan.xxx.common.util.*; import org.apache.commons.lang3.BooleanUtils; import java.io.File; @@ -12,6 +9,9 @@ import java.io.IOException; import java.io.PrintWriter; import java.sql.*; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.util.Date; import java.util.*; public class GenCodeGauss { @@ -19,7 +19,7 @@ private static final String PORT = "57654"; private static final String USER = "lunhan"; private static final String PASSWORD = "lunhan.20240330"; - private static final String DB_NAME = "test_db1"; + private static final String DB_NAME = "water_basic"; private static final String AUTHOR = "lin.liu"; private static Connection CONN = null; @@ -1368,7 +1368,7 @@ //#endregion - private static Map<String, String> getTables(String dbSchema) { + public static Map<String, String> getTables(String dbSchema) { // dbSchema="public"; Map<String, String> result = new LinkedHashMap<>(); String sql = "SELECT tb.table_name, d.description AS TABLE_COMMENT" + @@ -1391,27 +1391,20 @@ return result; } - private static List<Map<String, Object>> getColumns(String tableName, String dbSchema) { + public static List<Map<String, Object>> getColumns(String tableName, String dbSchema) { List<Map<String, Object>> result = new ArrayList<>(); - String sql = "select " + - "col.table_name AS objName, " + - "col.column_name AS name, " + - "col.data_type AS type, " + - "col.character_maximum_length AS LENGTH, " + - "des.description, " + - "col.numeric_precision, " + - "col.numeric_scale," + - "CASE is_identity WHEN 'YES' THEN 1 ELSE 0 END AS isIdentity, " + - "CASE col.IS_NULLABLE WHEN 'YES' THEN 1 ELSE 0 END AS isNullable, " + - "col.column_default AS default " + - "from " + - "information_schema.columns col left join pg_description des on " + - "col.table_name::regclass = des.objoid " + - "and col.ordinal_position = des.objsubid " + - "where " + - "table_name = ? " + - "and table_schema = 'public' " + - "order by " + + String sql = "SELECT col.table_name AS objName, col.column_name AS name, col.udt_name AS type, col.character_maximum_length AS LENGTH, des.description,\n" + + "col.numeric_precision, \n" + + "col.numeric_scale,\n" + + "col.datetime_precision,\n" + + "CASE is_identity WHEN 'YES' THEN 1 ELSE 0 END AS isIdentity,\n" + + "CASE col.IS_NULLABLE WHEN 'YES' THEN 1 ELSE 0 END AS isNullable,\n" + + "col.column_default AS default\n" + + "FROM information_schema.columns col \n" + + "LEFT JOIN pg_description des ON col.table_name::regclass = des.objoid AND col.ordinal_position = des.objsubid\n" + + "WHERE table_name = ? " + + "AND table_schema = 'public' " + + "ORDER BY " + "ordinal_position;"; try (PreparedStatement pStmt = CONN.prepareStatement(sql)) { pStmt.setString(1, tableName); @@ -1437,9 +1430,11 @@ return result; } - private static String getJavaType(String typeName) { + public static String getJavaType(String typeName) { String result = "String"; switch (typeName) { + case "int2": + case "int4": case "integer": case "tinyint": result = "Integer"; @@ -1462,6 +1457,7 @@ case "decimal": result = "BigDecimal"; break; + case "int8": case "bigint": result = "Long"; break; @@ -1475,7 +1471,7 @@ return result; } - private static String underline2Camel(String line, boolean... firstIsUpperCase) { + public static String underline2Camel(String line, boolean... firstIsUpperCase) { String str = ""; if (StringUtil.isNullOrEmpty(line)) { return str; @@ -1535,4 +1531,216 @@ e.printStackTrace(); } } + + public static String toDBValue(Object value) { + String result; + if (value instanceof Number || value instanceof Boolean) { + result = value.toString(); + } else if (value instanceof Timestamp) { + result = String.valueOf(((Timestamp) value).getTime()); + } else if (value instanceof LocalDateTime) { + result = "'" + LocalDateTimeUtil.toFormatFullString((LocalDateTime) value) + "'"; + } else if (value instanceof LocalDate) { + result = "'" + LocalDateTimeUtil.toFormatString((LocalDate) value) + "'"; + } else if (value instanceof Date) { + result = "'" + LocalDateTimeUtil.toFormatFullString(((Date) value).getTime()) + "'"; + } else if (value instanceof Calendar) { + result = "'" + CalendarUtil.toDateTimeMSStr((Calendar) value) + "'"; + } else { + result = "'" + value.toString() + "'"; + } + return result; + } + + public static boolean genTable(String tableName, String tableDesc, List<Map<String, Object>> listColumn, Map<String, String> mapPK, Map<String, String> mapIndex) { + String sql = "DROP TABLE IF EXISTS \"public\".\"" + tableName + "\";\n" + + "CREATE TABLE \"public\".\"" + tableName + "\" (\n"; + + Map<String, String> mapColumnDesc = new HashMap<>(); + int idx = 0; + // 遍历列,构建sql + for (Map<String, Object> column : listColumn) { + idx++; + String name = column.get("name").toString(); + String type = column.get("type").toString(); + String desc = column.get("description").toString(); + + mapColumnDesc.put(name,desc); + + sql += "\t\""+ name +"\" " + type; + switch (type) { + case "char": + case "varchar": + case "nvarchar": + case "text": + String length = column.get("length").toString(); + sql += "(" + length+ ") NOT NULL DEFAULT NULL::character varying"; + break; + case "numeric": + case "decimal": + String numeric_precision = column.get("numeric_precision").toString(); + String numeric_scale = column.get("numeric_scale").toString(); + sql += "(" + numeric_precision+ ","+ numeric_scale +") NOT NULL DEFAULT 0"; + break; + case "datetime": + case "timestamp": + String datetime_precision = column.get("datetime_precision").toString(); + sql += "(" + datetime_precision +") NOT NULL DEFAULT PG_SYSTIMESTAMP()"; + break; + case "int": + case "tinyint": + case "bit": + case "int2": + case "int4": + case "int8": + sql += " NOT NULL DEFAULT 0"; + break; + + default: + sql += " NULL DEFAULT NULL"; + break; + } + if (idx < listColumn.size()) { + sql += ","; + } + sql += "\n"; + } + sql += ")\n" + + ";\n" + + "COMMENT ON TABLE \"public\".\"" + tableName + "\" IS '" + tableDesc + "';\n"; + // 构建字段描述sql + for (Map.Entry<String, String> desc : mapColumnDesc.entrySet()) { + sql += "COMMENT ON COLUMN \"public\".\"" + tableName + "\".\"" + desc.getKey() +"\" IS '" + desc.getValue().replace("\r\n", " ").replace("\n", " ") + "';\n"; + } + // 设置主键 + if (!mapPK.isEmpty()) { + for (Map.Entry<String, String> key : mapPK.entrySet()) { + sql += "ALTER TABLE \"public\".\"" + tableName + "\" ADD CONSTRAINT \"PK_" + tableName + "\" PRIMARY KEY (\"" + key.getValue() +"\");\n"; + } + } + sql += "ALTER TABLE \"public\".\"" + tableName + "\" ADD CONSTRAINT \"PK_" + tableName + "\" PRIMARY KEY (\"id\");\n"; + // 触发器:自动赋值最后更新时间 + sql += "CREATE TRIGGER \"TG_" + tableName + "\" BEFORE UPDATE OF \"update_time\" ON \"public\".\"" + tableName + "\"\n" + + "FOR EACH ROW\n" + + "EXECUTE PROCEDURE \"public\".\"update_timestamp_column\"();"; + // 遍历索引,构建sql + for (Map.Entry<String, String> index : mapIndex.entrySet()) { + if (!mapPK.containsKey(index.getKey())) { + sql += "CREATE INDEX \"" + index.getKey() + "\" ON \"public\".\"test_info\"(\"" + index.getValue() + "\");\n"; + } + } + try (PreparedStatement pStmt = CONN.prepareCall(sql)) { + int[] result = pStmt.executeBatch(); + return result != null; + } catch (Exception e) { + System.out.println(e); + return Boolean.FALSE; + } + } + + public static Map<String, String> getPK(String tableName) { + Map<String, String> mapPK = new HashMap<>(); + // 查询主键 + String pkSq = "select pg_attribute.attname as col_name,pg_constraint.conname as pk_name\n" + + "from pg_constraint\n" + + "inner join pg_class on pg_constraint.conrelid=pg_class.oid\n" + + "inner join pg_attribute on pg_attribute.attrelid=pg_class.oid and pg_attribute.attnum=pg_constraint.conkey[1]\n" + + "where pg_class.relname='" + tableName + "'\n" + + "and pg_constraint.contype='p'"; + try (PreparedStatement pStmt = CONN.prepareStatement(pkSq)) { + try (ResultSet rs = pStmt.executeQuery()) { + //获取键名 + ResultSetMetaData md = rs.getMetaData(); + //获取列的数量 + int columnCount = md.getColumnCount(); + while (rs.next()) { + String pk_name = rs.getString("pk_name"); + String col_name = rs.getString("col_name"); + mapPK.put(pk_name, col_name); + } + } + } catch (Exception e) { + System.out.println(e); + } + return mapPK; + } + + public static Map<String, String> getIndex(String tableName) { + Map<String, String> mapPK = new HashMap<>(); + // 查询主键 + String pkSq = "SELECT * FROM pg_indexes WHERE tablename = '" + tableName +"';"; + try (PreparedStatement pStmt = CONN.prepareStatement(pkSq)) { + try (ResultSet rs = pStmt.executeQuery()) { + //获取键名 + ResultSetMetaData md = rs.getMetaData(); + while (rs.next()) { + String indexname = rs.getString("indexname"); + String indexdef = rs.getString("indexdef"); + mapPK.put(indexname, indexdef); + } + } + } catch (Exception e) { + System.out.println(e); + } + return mapPK; + } + + public static List<Map<String, Object>> getListAll(String tableName, String dbSchema) { + List<Map<String, Object>> result = new ArrayList<>(); + String sql = "SELECT * FROM " + dbSchema + "." + tableName; + try (PreparedStatement pStmt = CONN.prepareStatement(sql)) { + try (ResultSet rs = pStmt.executeQuery()) { + //获取键名 + ResultSetMetaData md = rs.getMetaData(); + //获取列的数量 + int columnCount = md.getColumnCount(); + while (rs.next()) { + Map<String, Object> column = new HashMap<>(); + for (int i = 1; i <= columnCount; i++) { + String keyName = md.getColumnLabel(i); + Object value = rs.getObject(i); + column.put(keyName, value); + } + result.add(column); + } + } + } catch (SQLException e) { + e.printStackTrace(); + } + return result; + } + + public static List<Boolean> insertAll(List<Map<String, Object>> list, String tableName, Integer onceCount) { + List<Boolean> result = new ArrayList<>(); + String insertSql = "INSERT INTO public." + tableName + "("; + List<String> listColumn = new ArrayList<>(); + for (String column : list.get(0).keySet()) { + listColumn.add(column); + } + insertSql += StringUtil.join(listColumn) + ") VALUES"; + + String sql = ""; + for (int i = 0; i < list.size(); i++) { + if (StringUtil.isNullOrEmpty(sql)) { + sql = insertSql; + } + Map<String, Object> row = list.get(i); + List<String> listValue = new ArrayList<>(); + for (Map.Entry<String, Object> item : row.entrySet()) { + listValue.add(GenCodeGauss.toDBValue(item.getValue())); + } + sql += "(" + StringUtil.join(listValue) + ")"; + if (i > 0 && i % onceCount == 0) { + try (PreparedStatement pStmt = CONN.prepareStatement(sql)) { + boolean insertList = pStmt.execute(); + result.add(insertList); + System.out.println("插入数据" + Math.max(i - onceCount, 0) + " - " + i + ",结果:" + insertList); + } catch (SQLException e) { + e.printStackTrace(); + } + sql = ""; + } + } + return result; + } } -- Gitblit v1.9.3