java从sql语句中找出字符串中的时间字符,添加to_date格式

  最近用了jeecgboot框架,低代码生成的sql语句不能直接运行,数据库中的时间是date类型,sql语句中的时间是字符类型,需要转换一下。

生成的sql语句(示例):

时间是字符串类型:'2023-05-10 18:05:11'

INSERT INTO sys_permission(id, parent_id, name, url, component, component_name, redirect, menu_type, perms, perms_type, sort_no, always_show, icon, is_route, is_leaf, keep_alive, hidden, hide_tab, description, status, del_flag, rule_flag, create_by, create_time, update_by, update_time, internal_or_external) 
VALUES ('2023051006055810110', NULL, '制度变更', '/com/tbChgOrderList', 'com/TbChgOrderList', NULL, NULL, 0, NULL, '1', 0.00, 0, NULL, 1, 0, 0, 0, 0, NULL, '1', 0, 0, 'admin', '2023-05-10 18:05:11', NULL, NULL, 0);

预期转换的sql语句(示例):

INSERT INTO sys_permission(id, parent_id, name, url, component, component_name, redirect, menu_type, perms, perms_type, sort_no, always_show, icon, is_route, is_leaf, keep_alive, hidden, hide_tab, description, status, del_flag, rule_flag, create_by, create_time, update_by, update_time, internal_or_external)  VALUES ('2023051006055810110', NULL, '制度变更', '/com/tbChgOrderList', 'com/TbChgOrderList', NULL, NULL, 0, NULL, '1', 0.00, 0, NULL, 1, 0, 0, 0, 0, NULL, '1', 0, 0, 'admin', TO_DATE( '2023-05-10 18:05:11', 'SYYYY-MM-DD HH24:MI:SS' ), NULL, NULL, 0);

添加to_date转化成date格式:TO_DATE( '2023-05-10 18:05:11', 'SYYYY-MM-DD HH24:MI:SS' )

目的:sql语句中找出字符串中的时间字符,添加to_date格式

思路:1、读取sql文件,拿出sql语句

2、找出sql语句中的时间字符,替换为to_date的格式

3、打印出转换后的sql

代码如下:

package org.jeecg.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class JeecgUtil {
    static JeecgUtil jeecgUtil = new JeecgUtil();

    //完整文件路径
    static String sqlFilePath = "file/jeecg-boot.sql";

    /**
     * jeecgboot生成的sql语句中时间 字符串类型 转成 date类型
     * eg:'2023-05-10 18:05:45' --> TO_DATE( '2023-05-10 18:05:45', 'SYYYY-MM-DD HH24:MI:SS' )
     *
     * @param sqlFilePath
     * @throws
     * @Description: 从字符串中截取出正确的时间字符串, 并将时间转化成to_date格式的字符串
     * @return:
     */
    public static List chgSqlString(String sqlFilePath) {
        //日期正则表达式
        String regs[] = {"\\d{4}年\\d{2}月\\d{2}日\\s\\d{2}时\\d{2}分\\d{2}秒",
                "\\d{4}年\\d{2}月\\d{2}日\\s\\d{1}时\\d{2}分\\d{2}秒",
                "\\d{4}年\\d{1}月\\d{2}日\\s\\d{1}时\\d{2}分\\d{2}秒",
                "\\d{4}年\\d{1}月\\d{2}日\\s\\d{2}时\\d{2}分\\d{2}秒",
                "\\d{4}年\\d{2}月\\d{2}日\\d{2}时\\d{2}分\\d{2}秒",
                "\\d{4}年\\d{2}月\\d{2}日\\s\\d{2}时\\d{2}分",
                "\\d{4}年\\d{1}月\\d{2}日\\s\\d{2}时\\d{2}分",
                "\\d{4}年\\d{1}月\\d{2}日\\s\\d{1}时\\d{2}分",
                "\\d{4}年\\d{1}月\\d{2}日\\s\\d{2}时\\d{2}分",
                "\\d{4}年\\d{2}月\\d{2}日\\d{2}时\\d{2}分",
                "\\d{4}年\\d{2}月\\d{2}日\\s\\d{2}时",
                "\\d{4}年\\d{2}月\\d{2}日\\s\\d{1}时",
                "\\d{4}年\\d{1}月\\d{2}日\\s\\d{2}时",
                "\\d{4}年\\d{1}月\\d{2}日\\s\\d{1}时",
                "\\d{4}年\\d{2}月\\d{2}日\\d{2}时", "\\d{4}年\\d{2}月\\d{2}日",
                "\\d{4}年\\d{2}月\\d{1}日", "\\d{4}年\\d{1}月\\d{2}日",
                "\\d{4}年\\d{1}月\\d{1}日",
                "\\d{4}年\\d{2}月\\d{2}日\\s\\d{2}:\\d{2}:\\d{2}",
                "\\d{4}年\\d{2}月\\d{2}日\\s\\d{2}:\\d{1}:\\d{2}",
                "\\d{4}年\\d{1}月\\d{2}日\\s\\d{2}:\\d{1}:\\d{2}",
                "\\d{4}年\\d{1}月\\d{2}日\\s\\d{2}:\\d{2}:\\d{2}",
                "\\d{4}年\\d{2}月\\d{2}日\\d{2}:\\d{2}:\\d{2}",
                "\\d{4}年\\d{2}月\\d{2}日\\s\\d{2}:\\d{2}",
                "\\d{4}年\\d{2}月\\d{2}日\\s\\d{1}:\\d{2}",
                "\\d{4}年\\d{1}月\\d{2}日\\s\\d{2}:\\d{2}",
                "\\d{4}年\\d{1}月\\d{2}日\\s\\d{1}:\\d{2}",
                "\\d{4}年\\d{2}月\\d{2}日\\d{2}:\\d{2}",
                "\\d{4}年\\d{2}月\\d{2}日\\s\\d{2}",
                "\\d{4}年\\d{2}月\\d{2}日\\s\\d{1}",
                "\\d{4}年\\d{1}月\\d{2}日\\s\\d{2}",
                "\\d{4}年\\d{1}月\\d{2}日\\s\\d{1}",
                "\\d{4}年\\d{2}月\\d{2}日\\d{2}",
                "\\d{4}-\\d{2}-\\d{2}\\s\\d{2}:\\d{2}:\\d{2}",
                "\\d{4}-\\d{2}-\\d{2}\\s\\d{1}:\\d{2}:\\d{2}",
                "\\d{4}-\\d{1}-\\d{2}\\s\\d{2}:\\d{2}:\\d{2}",
                "\\d{4}-\\d{1}-\\d{2}\\s\\d{1}:\\d{2}:\\d{2}",
                "\\d{4}-\\d{2}-\\d{2}\\s\\d{2}:\\d{2}",
                "\\d{4}-\\d{2}-\\d{2}\\s\\d{1}:\\d{2}",
                "\\d{4}-\\d{2}-\\d{2}\\s\\d{2}", "\\d{4}-\\d{2}-\\d{2}",
                "\\d{4}-\\d{2}-\\d{1}", "\\d{4}-\\d{1}-\\d{2}",
                "\\d{4}-\\d{1}-\\d{1}",
                "\\d{4}-\\d{2}-\\d{2}\\s\\d{2}时\\d{2}分\\d{2}秒",
                "\\d{4}-\\d{2}-\\d{2}\\s\\d{1}时\\d{2}分\\d{2}秒",
                "\\d{4}-\\d{1}-\\d{2}\\s\\d{2}时\\d{2}分\\d{2}秒",
                "\\d{4}-\\d{1}-\\d{2}\\s\\d{1}时\\d{2}分\\d{2}秒",
                "\\d{4}-\\d{1}-\\d{1}\\s\\d{1}时\\d{2}分\\d{2}秒",
                "\\d{4}-\\d{2}-\\d{2}\\s\\d{2}时\\d{2}分",
                "\\d{4}-\\d{2}-\\d{2}\\s\\d{1}时\\d{2}分",
                "\\d{4}-\\d{1}-\\d{2}\\s\\d{2}时\\d{2}分",
                "\\d{4}-\\d{1}-\\d{2}\\s\\d{1}时\\d{2}分",
                "\\d{4}-\\d{2}-\\d{2}\\s\\d{2}时",
                "\\d{4}-\\d{2}-\\d{2}\\s\\d{1}时",
                "\\d{4}-\\d{1}-\\d{2}\\s\\d{2}时",
                "\\d{4}-\\d{1}-\\d{2}\\s\\d{1}时", "\\d{4}.\\d{2}.\\d{2}",
                "\\d{4}.\\d{2}.\\d{1}", "\\d{4}.\\d{1}.\\d{2}",
                "\\d{4}.\\d{1}.\\d{1}",
                "\\d{4}.\\d{2}.\\d{2}\\s\\d{2}:\\d{2}:\\d{2}",
                "\\d{4}.\\d{2}.\\d{2}\\s\\d{1}:\\d{2}:\\d{2}",
                "\\d{4}.\\d{1}.\\d{2}\\s\\d{2}:\\d{2}:\\d{2}",
                "\\d{4}.\\d{1}.\\d{2}\\s\\d{1}:\\d{2}:\\d{2}",
                "\\d{4}.\\d{1}.\\d{1}\\s\\d{1}:\\d{2}:\\d{2}",
                "\\d{4}.\\d{2}.\\d{2}\\s\\d{2}:\\d{2}",
                "\\d{4}.\\d{2}.\\d{2}\\s\\d{1}:\\d{2}",
                "\\d{4}.\\d{1}.\\d{2}\\s\\d{2}:\\d{2}",
                "\\d{4}.\\d{1}.\\d{2}\\s\\d{1}:\\d{2}",
                "\\d{4}.\\d{2}.\\d{2}\\s\\d{2}",
                "\\d{4}.\\d{2}.\\d{2}\\s\\d{1}",
                "\\d{4}.\\d{1}.\\d{2}\\s\\d{2}",
                "\\d{4}.\\d{1}.\\d{2}\\s\\d{1}",
                "\\d{4}/\\d{2}/\\d{2}\\s\\d{2}时\\d{2}分\\d{2}秒",
                "\\d{4}/\\d{2}/\\d{2}\\s\\d{1}时\\d{2}分\\d{2}秒",
                "\\d{4}/\\d{1}/\\d{2}\\s\\d{2}时\\d{2}分\\d{2}秒",
                "\\d{4}/\\d{1}/\\d{2}\\s\\d{1}时\\d{2}分\\d{2}秒",
                "\\d{4}/\\d{2}/\\d{2}\\s\\d{2}时\\d{2}分",
                "\\d{4}/\\d{2}/\\d{2}\\s\\d{1}时\\d{2}分",
                "\\d{4}/\\d{1}/\\d{2}\\s\\d{2}时\\d{2}分",
                "\\d{4}/\\d{1}/\\d{2}\\s\\d{1}时\\d{2}分",
                "\\d{4}/\\d{2}/\\d{2}\\s\\d{2}时",
                "\\d{4}/\\d{2}/\\d{2}\\s\\d{1}时",
                "\\d{4}/\\d{1}/\\d{2}\\s\\d{2}时",
                "\\d{4}/\\d{1}/\\d{2}\\s\\d{1}时",
                "\\d{4}/\\d{2}/\\d{2}",
                "\\d{4}/\\d{2}/\\d{1}",
                "\\d{4}/\\d{1}/\\d{2}",
                "\\d{4}/\\d{1}/\\d{1}",
                "\\d{4}/\\d{2}/\\d{2}\\s\\d{2}:\\d{2}:\\d{2}",
                "\\d{4}/\\d{2}/\\d{2}\\s\\d{1}:\\d{2}:\\d{2}",
                "\\d{4}/\\d{1}/\\d{2}\\s\\d{2}:\\d{2}:\\d{2}",
                "\\d{4}/\\d{1}/\\d{2}\\s\\d{1}:\\d{2}:\\d{2}",
                "\\d{4}/\\d{2}/\\d{2}\\s\\d{2}:\\d{2}",
                "\\d{4}/\\d{2}/\\d{2}\\s\\d{1}:\\d{2}",
                "\\d{4}/\\d{1}/\\d{2}\\s\\d{2}:\\d{2}",
                "\\d{4}/\\d{1}/\\d{2}\\s\\d{1}:\\d{2}",
                "\\d{4}/\\d{2}/\\d{2}\\s\\d{2}",
                "\\d{4}/\\d{2}/\\d{2}\\s\\d{1}",
                "\\d{4}/\\d{1}/\\d{2}\\s\\d{2}",
                "\\d{4}/\\d{1}/\\d{2}\\s\\d{1}",
                "\\d{2}月\\d{2}日\\s\\d{2}时\\d{2}分\\d{2}秒",
                "\\d{2}月\\d{2}日\\s\\d{1}时\\d{2}分\\d{2}秒",
                "\\d{1}月\\d{2}日\\s\\d{1}时\\d{2}分\\d{2}秒",
                "\\d{1}月\\d{2}日\\s\\d{2}时\\d{2}分\\d{2}秒",
                "\\d{2}月\\d{2}日\\d{2}时\\d{2}分\\d{2}秒",
                "\\d{2}月\\d{2}日\\s\\d{2}时\\d{2}分",
                "\\d{1}月\\d{2}日\\s\\d{2}时\\d{2}分",
                "\\d{1}月\\d{2}日\\s\\d{1}时\\d{2}分",
                "\\d{1}月\\d{2}日\\s\\d{2}时\\d{2}分",
                "\\d{2}月\\d{2}日\\d{2}时\\d{2}分",
                "\\d{2}月\\d{2}日\\s\\d{2}时",
                "\\d{2}月\\d{2}日\\s\\d{1}时",
                "\\d{1}月\\d{2}日\\s\\d{2}时",
                "\\d{1}月\\d{2}日\\s\\d{1}时",
                "\\d{2}月\\d{2}日\\d{2}时", "\\d{4}年\\d{2}月\\d{2}日",
                "\\d{2}月\\d{1}日", "\\d{4}年\\d{1}月\\d{2}日",
                "\\d{1}月\\d{1}日",
                "\\d{2}月\\d{2}日\\s\\d{2}:\\d{2}:\\d{2}",
                "\\d{2}月\\d{2}日\\s\\d{2}:\\d{1}:\\d{2}",
                "\\d{1}月\\d{2}日\\s\\d{2}:\\d{1}:\\d{2}",
                "\\d{1}月\\d{2}日\\s\\d{2}:\\d{2}:\\d{2}",
                "\\d{2}月\\d{2}日\\d{2}:\\d{2}:\\d{2}",
                "\\d{2}月\\d{2}日\\s\\d{2}:\\d{2}",
                "\\d{2}月\\d{2}日\\s\\d{1}:\\d{2}",
                "\\d{1}月\\d{2}日\\s\\d{2}:\\d{2}",
                "\\d{1}月\\d{2}日\\s\\d{1}:\\d{2}",
                "\\d{2}月\\d{2}日\\d{2}:\\d{2}",
                "\\d{2}月\\d{2}日\\s\\d{2}",
                "\\d{2}月\\d{2}日\\s\\d{1}",
                "\\d{1}月\\d{2}日\\s\\d{2}",
                "\\d{1}月\\d{2}日\\s\\d{1}",
                "\\d{2}月\\d{2}日\\d{2}",
                "\\d{2}-\\d{2}\\s\\d{2}:\\d{2}:\\d{2}",
                "\\d{2}-\\d{2}\\s\\d{1}:\\d{2}:\\d{2}",
                "\\d{1}-\\d{2}\\s\\d{2}:\\d{2}:\\d{2}",
                "\\d{1}-\\d{2}\\s\\d{1}:\\d{2}:\\d{2}",
                "\\d{2}-\\d{2}\\s\\d{2}:\\d{2}",
                "\\d{2}-\\d{2}\\s\\d{1}:\\d{2}",
                "\\d{2}-\\d{2}\\s\\d{2}", "\\d{4}-\\d{2}-\\d{2}",
                "\\d{2}-\\d{1}", "\\d{4}-\\d{1}-\\d{2}",
                "\\d{1}-\\d{1}",
                "\\d{2}-\\d{2}\\s\\d{2}时\\d{2}分\\d{2}秒",
                "\\d{2}-\\d{2}\\s\\d{1}时\\d{2}分\\d{2}秒",
                "\\d{1}-\\d{2}\\s\\d{2}时\\d{2}分\\d{2}秒",
                "\\d{1}-\\d{2}\\s\\d{1}时\\d{2}分\\d{2}秒",
                "\\d{1}-\\d{1}\\s\\d{1}时\\d{2}分\\d{2}秒",
                "\\d{2}-\\d{2}\\s\\d{2}时\\d{2}分",
                "\\d{2}-\\d{2}\\s\\d{1}时\\d{2}分",
                "\\d{1}-\\d{2}\\s\\d{2}时\\d{2}分",
                "\\d{1}-\\d{2}\\s\\d{1}时\\d{2}分",
                "\\d{2}-\\d{2}\\s\\d{2}时",
                "\\d{2}-\\d{2}\\s\\d{1}时",
                "\\d{1}-\\d{2}\\s\\d{2}时",
                "\\d{1}-\\d{2}\\s\\d{1}时", "\\d{4}.\\d{2}.\\d{2}",
                "\\d{2}.\\d{1}", "\\d{4}.\\d{1}.\\d{2}",
                "\\d{1}.\\d{1}",
                "\\d{2}.\\d{2}\\s\\d{2}:\\d{2}:\\d{2}",
                "\\d{2}.\\d{2}\\s\\d{1}:\\d{2}:\\d{2}",
                "\\d{1}.\\d{2}\\s\\d{2}:\\d{2}:\\d{2}",
                "\\d{1}.\\d{2}\\s\\d{1}:\\d{2}:\\d{2}",
                "\\d{1}.\\d{1}\\s\\d{1}:\\d{2}:\\d{2}",
                "\\d{2}.\\d{2}\\s\\d{2}:\\d{2}",
                "\\d{2}.\\d{2}\\s\\d{1}:\\d{2}",
                "\\d{1}.\\d{2}\\s\\d{2}:\\d{2}",
                "\\d{1}.\\d{2}\\s\\d{1}:\\d{2}",
                "\\d{2}.\\d{2}\\s\\d{2}",
                "\\d{2}.\\d{2}\\s\\d{1}",
                "\\d{1}.\\d{2}\\s\\d{2}",
                "\\d{1}.\\d{2}\\s\\d{1}",
                "\\d{2}/\\d{2}\\s\\d{2}时\\d{2}分\\d{2}秒",
                "\\d{2}/\\d{2}\\s\\d{1}时\\d{2}分\\d{2}秒",
                "\\d{1}/\\d{2}\\s\\d{2}时\\d{2}分\\d{2}秒",
                "\\d{1}/\\d{2}\\s\\d{1}时\\d{2}分\\d{2}秒",
                "\\d{2}/\\d{2}\\s\\d{2}时\\d{2}分",
                "\\d{2}/\\d{2}\\s\\d{1}时\\d{2}分",
                "\\d{1}/\\d{2}\\s\\d{2}时\\d{2}分",
                "\\d{1}/\\d{2}\\s\\d{1}时\\d{2}分",
                "\\d{2}/\\d{2}\\s\\d{2}时",
                "\\d{2}/\\d{2}\\s\\d{1}时",
                "\\d{1}/\\d{2}\\s\\d{2}时",
                "\\d{1}/\\d{2}\\s\\d{1}时",
                "\\d{2}/\\d{2}",
                "\\d{2}/\\d{1}",
                "\\d{1}/\\d{2}",
                "\\d{1}/\\d{1}",
                "\\d{2}/\\d{2}\\s\\d{2}:\\d{2}:\\d{2}",
                "\\d{2}/\\d{2}\\s\\d{1}:\\d{2}:\\d{2}",
                "\\d{1}/\\d{2}\\s\\d{2}:\\d{2}:\\d{2}",
                "\\d{1}/\\d{2}\\s\\d{1}:\\d{2}:\\d{2}",
                "\\d{2}/\\d{2}\\s\\d{2}:\\d{2}",
                "\\d{2}/\\d{2}\\s\\d{1}:\\d{2}",
                "\\d{1}/\\d{2}\\s\\d{2}:\\d{2}",
                "\\d{1}/\\d{2}\\s\\d{1}:\\d{2}",
                "\\d{2}/\\d{2}\\s\\d{2}",
                "\\d{2}/\\d{2}\\s\\d{1}",
                "\\d{1}/\\d{2}\\s\\d{2}",
                "\\d{1}/\\d{2}\\s\\d{1}",
        };
        //读取文件获取sql语句
        String[] sqlStrings = readFile(sqlFilePath);
        System.out.println("=========读取到sql条数:" + sqlStrings.length);
        //转化完的sql语句list
        List sqlStringList = new ArrayList<>();
        for (String sqlString : sqlStrings) {
            String str = "";
            for (String reg : regs) {
                String temp = match(reg, sqlString);
                if (temp.length() > str.length()) {
                    str = temp;
                    if (!"".equals(str)) {
                        //时间字符串 加上 to_date
                        str = "'" + str + "'";
                        sqlString = sqlString.replace(str, "TO_DATE( " + str + ", 'SYYYY-MM-DD HH24:MI:SS' )");
                    }
                }
            }
            //转换完的语句放进list
            sqlStringList.add(sqlString + ";");
            //转换完的语句打印到后台
//            System.out.println(sqlString + ";");
        }

        return sqlStringList;
    }


    /**
     * 读文件获取sql语句
     *
     * @param path
     * @return
     */
    public static String[] readFile(String path) {
        InputStream in = null;
        BufferedReader bufferedReader = null;
        StringBuffer stringBuffer = new StringBuffer();
        try {
//            in = new FileInputStream(path);
            in = jeecgUtil.getClass().getClassLoader()
                    .getResourceAsStream(path);
            bufferedReader = new BufferedReader(new InputStreamReader(in, Charset.forName("utf-8")));
            String line = null;
            while ((line = bufferedReader.readLine()) != null) {
                String s = line;
                //去掉行注释
                if (s.trim().startsWith("--")) {
                    continue;
                }
                stringBuffer.append(" ").append(line);
            }
            String[] sqls = stringBuffer.toString().split(";");
            return sqls;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            stringBuffer = null;
            try {
                if (bufferedReader != null) {
                    bufferedReader.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 根据正则表达式找出sql语句中的时间字符串部分
     *
     * @param reg       正则表达式
     * @param sqlString sql语句
     * @return
     */
    public static String match(String reg, String sqlString) {
        Pattern p = Pattern.compile(reg);
        Matcher m = p.matcher(sqlString);
        String dateString = "";
        if (m.find()) {
            dateString += m.group();
        }
        return dateString;
    }

    public static void main(String[] args) {
        //sql语句处理时间字符串
        List sqls = chgSqlString(sqlFilePath);
        for (String sql : sqls) {
            System.out.println(sql);
        }

    }
}

你可能感兴趣的:(Java,sql,java,数据库,jeecgboot,date)