sql文件的拆分

一:需求是很多公司业务烦乱,用户操作不规范,现有程序笨拙等原因导致相关操作后导致程序异常,只能修改数据库来修正程序,在这时手动执行sql语句任务繁重,需要简化工具。上代码:

   

/**
         * 获取SQL,文本中sql以 --:n 进行分割,--:代表分隔符,n代表sql预计影响条数
         * @param filePath
         * @return
         */
        public String readToString(String filePath,String db_code) {  
            if(StringUtils.isEmpty(db_code)) {
                db_code="UTF-8";
            }
            StringBuffer result = new StringBuffer();
                try{
                    FileInputStream fr = new FileInputStream(filePath);
                    //防止中文符号 --:  乱码
                    InputStreamReader is = new InputStreamReader(fr,db_code);
                    BufferedReader br = new BufferedReader(is);//构造一个BufferedReader类来读取文件
                    String lineStr = null;
                    int count =0;//0正常 1带注释
                    String immedateStr ="";//即时字符串  截取注释用
                    while((lineStr = br.readLine())!=null){
                        //System.lineSeparator() 获取系统 行分隔符
                        //多行注释   区别 sql语句中 嵌套 注释的情况
                        if(lineStr.contains("/*") || lineStr.contains("*/") || count == 1) {
                            immedateStr += lineStr.contains("/*")?lineStr.substring(lineStr.indexOf("/*"),lineStr.length())+" ":lineStr;
                            immedateStr += lineStr.contains("*/")?lineStr.substring(0,lineStr.indexOf("*/"))+" ":lineStr;
                            count =1;
                        }
                        if(immedateStr.contains("/*") && immedateStr.contains("*/")) {
                            immedateStr ="";
                            count =0;
                            continue;
                        }
                        //单行注释  区别sql语句后待--的注释
                        if(lineStr.contains("--") && lineStr.replace(" ", "").indexOf("--:") != 0 && lineStr.replace(" ", "").indexOf("--:") != 0) {
                            immedateStr ="";
                            count = 0;
                            result.append(lineStr.substring(0, lineStr.indexOf("--"))+" ");
                            continue;
                        }
                        if(count==0) {
                            //等于0 --sql " "防止换行时字符连在一起
                            result.append(lineStr+" ");
                        }
                    }
                    br.close();    
                }catch(Exception e){
                    e.printStackTrace();
                }
                return result.toString();   
        }

二:以上可以用下面sql进行验证,任何段落注释和单行注释均可实现

       

/*注释啊
select * from tttsssss;
select * from tablesssss;*/

/*select *
  from table es
where es.busstype = 'POS'
   and es.subtype = '1'; */
--:1
update test1 
    set id=3 --id
   where name='小明';
/*注释啊
select * from tttsssss;
select * from tablesssss;*/

/*select *
  from table es
where es.busstype = 'POS'
   and es.subtype = '1'; */
--:1
update test1 
    set id=3 --id
   where name='小明';

三:输出的结果,可以按字符截取,字符规范可以自己定义

  --:1 update test1  set id=3     where name='小明';  --:1 update test1  set id=3     where name='小明';   

四:方式还有很多种,也可以尝试将update变成select 语句,喜欢的猿猿们可以试试,有更好的处理方式欢迎留言讨论,不喜勿喷!

以上。。。

posted @ 2019-10-23 17:22    阅读(1072)  评论(0)    收藏  举报