Java将MySQL建表语句转换为SQLite的建表语句
Java将MySQL建表语句转换为SQLite的建表语句
源代码:
package com.fxsen.platform.core.util;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* mysql转SQLite
*@Author: <a href="mailto:fxsen@foxmail.com">Fxsen</a>
*@CreateTime: 2023年08月22日 15:46
*/
public class MysqlToSqLiteUtil {
public static final Map<String, String> FIELD_TYPE_MAP = new HashMap<>();
static {
FIELD_TYPE_MAP.put("int", "INTEGER");
FIELD_TYPE_MAP.put("bigint", "INTEGER");
FIELD_TYPE_MAP.put("tinyint", "INTEGER");
FIELD_TYPE_MAP.put("smallint", "INTEGER");
FIELD_TYPE_MAP.put("mediumint", "INTEGER");
FIELD_TYPE_MAP.put("float", "REAL");
FIELD_TYPE_MAP.put("double", "REAL");
FIELD_TYPE_MAP.put("decimal", "NUMERIC");
FIELD_TYPE_MAP.put("varchar", "TEXT");
FIELD_TYPE_MAP.put("char", "TEXT");
FIELD_TYPE_MAP.put("text", "TEXT");
FIELD_TYPE_MAP.put("enum", "TEXT");
FIELD_TYPE_MAP.put("datetime", "TEXT");
FIELD_TYPE_MAP.put("timestamp", "TEXT");
FIELD_TYPE_MAP.put("date", "TEXT");
FIELD_TYPE_MAP.put("time", "TEXT");
FIELD_TYPE_MAP.put("blob", "BLOB");
FIELD_TYPE_MAP.put("bit", "TEXT");
FIELD_TYPE_MAP.put("boolean", "INTEGER");
FIELD_TYPE_MAP.put("set", "TEXT");
FIELD_TYPE_MAP.put("json", "TEXT");
FIELD_TYPE_MAP.put("geometry", "BLOB");
FIELD_TYPE_MAP.put("point", "BLOB");
FIELD_TYPE_MAP.put("linestring", "BLOB");
FIELD_TYPE_MAP.put("polygon", "BLOB");
FIELD_TYPE_MAP.put("multipoint", "BLOB");
FIELD_TYPE_MAP.put("multilinestring", "BLOB");
FIELD_TYPE_MAP.put("multipolygon", "BLOB");
FIELD_TYPE_MAP.put("geometrycollection", "BLOB");
}
public static void main(String[] args) {
String createTableStatement = "CREATE TABLE `t_enterprise_info` (\n" +
" `id` varchar(32) NOT NULL COMMENT '主键ID',\n" +
" `category_id` text COMMENT '企业类别ID',\n" +
" `name` varchar(255) DEFAULT NULL COMMENT '企业名称',\n" +
" `social_credit_code` varchar(255) DEFAULT NULL COMMENT '社会信用代码',\n" +
" `org_code` varchar(255) DEFAULT NULL COMMENT '组织机构代码',\n" +
" `address` varchar(255) DEFAULT NULL COMMENT '街(村)、门牌号(实际经营地)',\n" +
" `register_address` varchar(255) DEFAULT NULL COMMENT '单位注册地',\n" +
" `legal_person` varchar(255) DEFAULT NULL COMMENT '单位法人',\n" +
" `open_date` varchar(50) DEFAULT NULL COMMENT '开业时间',\n" +
" PRIMARY KEY (`id`) USING BTREE\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='企业基本信息表';";
System.out.println(convertMysqlToSQLite(createTableStatement));
}
public static String convertMysqlToSQLite(String mysqlStatement) {
// 删除COMMENT
String reg = "COMMENT\\s*'.*?'";
Pattern pattern = Pattern.compile(reg);
Matcher matcher = pattern.matcher(mysqlStatement);
String result = matcher.replaceAll("")
.replaceAll("\\).*?;", ");")
.replaceAll("USING BTREE","")
.replaceAll("`","\"");
// 替换 MySQL 建表语句中的数据类型和关键字为 SQLite 的等价语句
for (String key : FIELD_TYPE_MAP.keySet()) {
// 生成正则表达式,匹配字段类型
String regex = "\\b" + key + "\\b";
Pattern pattern2 = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
Matcher matcher2 = pattern2.matcher(result);
// 执行替换
result = matcher2.replaceAll(FIELD_TYPE_MAP.get(key));
}
// 返回替换后的 SQLite 建表语句
return result;
}
转换前:
CREATE TABLE `t_enterprise_info` (
`id` varchar(32) NOT NULL COMMENT '主键ID',
`category_id` text COMMENT '企业类别ID',
`name` varchar(255) DEFAULT NULL COMMENT '企业名称',
`social_credit_code` varchar(255) DEFAULT NULL COMMENT '社会信用代码',
`org_code` varchar(255) DEFAULT NULL COMMENT '组织机构代码',
`address` varchar(255) DEFAULT NULL COMMENT '街(村)、门牌号(实际经营地)',
`register_address` varchar(255) DEFAULT NULL COMMENT '单位注册地',
`legal_person` varchar(255) DEFAULT NULL COMMENT '单位法人',
`open_date` varchar(50) DEFAULT NULL COMMENT '开业时间',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='企业基本信息表';
转换后:
CREATE TABLE "t_enterprise_info" (
"id" TEXT(32) NOT NULL ,
"category_id" TEXT ,
"name" TEXT(255) DEFAULT NULL ,
"social_credit_code" TEXT(255) DEFAULT NULL ,
"org_code" TEXT(255) DEFAULT NULL ,
"address" TEXT(255) DEFAULT NULL ,
"register_address" TEXT(255) DEFAULT NULL ,
"legal_person" TEXT(255) DEFAULT NULL ,
"open_date" TEXT(50) DEFAULT NULL ,
PRIMARY KEY ("id")
);
热门相关:有个人爱你很久 买妻种田:山野夫君,强势宠! 本法官萌萌哒 本法官萌萌哒 大妆