package dbtools.sqlupdater;

import dbtools.common.file.FileReader;
import dbtools.common.file.FileWriter;
import dbtools.common.utils.StrUtils;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by user on 2017/9/30.
 */
public class SqlUpdater {
    public static String Sql_File_Ext = ".sql";
    public static String Sql_File_name = "_updated.sql";
    public static String Sql_Folder_name = "_updated";

    private static String[] Sql_Update_Header_Array = {
            "set @companyId = 1000;",
            "-- Original 51 is replaced:",
            "-- ,51, => ,@companyId,",
            "-- ,51) => ,@companyId)",
            ""
    };

    private static Map<String, String> Sql_Replace_Map = new HashMap<String, String>() {{
        put(",51,", ",@companyId,");
        put(",51)", ",@companyId)");
    }};


    public static void processFile(File inputFile, String outputFileName) {
        if (inputFile == null || !inputFile.exists() || !inputFile.canRead() || StrUtils.isEmpty(outputFileName)) {
            return;
        }

        // Input
        String filePath = inputFile.getPath();
        FileReader reader = new FileReader(filePath);
        if (!reader.open()) {
            System.out.printf("Fail to open file: %s\n", filePath);
            return;
        }

        // Output
        FileWriter writer = new FileWriter(outputFileName);
        if (!writer.open()) {
            System.out.printf("Fail to create output file: %s\n", outputFileName);

            // Close
            reader.close();
            return;
        }

        writeHeaders(writer);

        // read and update, then write
        String str;
        while ((str = reader.readLine()) != null) {
            if (!StrUtils.isEmpty(str)) {
                // Update the str;
                str = updateSql(str);
            }

            // Write
            writer.writeLine(str);
        }

        // close
        writer.close();
        reader.close();
    }

    private static String updateSql(String sql) {
        if (StrUtils.isEmpty(sql)) {
            return sql;
        }

        for (Map.Entry<String, String> replace : Sql_Replace_Map.entrySet()) {
            String str = replace.getKey();
            if (StrUtils.isEmpty(str)) {
                continue;
            }

            sql = sql.replace(str, replace.getValue());
        }
        return sql;
    }

    private static void writeHeaders(FileWriter writer) {
        if (writer == null || !writer.isOpen()) {
            return;
        }

        for (String str : Sql_Update_Header_Array) {
            writer.writeLine(str);
        }
    }
}

@@ -437,16 +437,75 @@
 ated%22;%0A%0A
+    private static long Sql_Update_Header_Line_Index = 17;%0A
     priv
@@ -552,16 +552,16 @@
 ray = %7B%0A
-
         
@@ -1662,28 +1662,103 @@
     
+// Mark the header 
 writ
-eHeaders(writer)
+ten or not%0A        long index = 0;%0A        boolean headerWritten = false
 ;%0A%0A 
@@ -2013,24 +2013,234 @@
     // Write
+ header%0A            if (!headerWritten && Sql_Update_Header_Line_Index %3C= index++) %7B%0A                headerWritten = true;%0A                index += writeHeaders(writer);%0A            %7D%0A%0A            // Write line
 %0A           
@@ -2784,20 +2784,20 @@
  static 
-void
+long
  writeHe
@@ -2883,32 +2883,34 @@
           return
+ 0
 ;%0A        %7D%0A%0A   
@@ -2986,32 +2986,32 @@
 writeLine(str);%0A
-
         %7D%0A    %7D%0A
@@ -3000,20 +3000,67 @@
 ;%0A        %7D%0A
+        return Sql_Update_Header_Array.length;%0A
     %7D%0A%7D%0A

Write header to special line index