Created
March 18, 2015 13:42
-
-
Save auycro/74fd6c095aedb9f101b9 to your computer and use it in GitHub Desktop.
reformat sql interbase to DB2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"fmt" | |
"log" | |
"os" | |
"strings" | |
//"time" | |
) | |
// readLines reads a whole file into memory | |
// and returns a slice of its lines. | |
func readLines(path string) ([]string, error) { | |
file, err := os.Open(path) | |
if err != nil { | |
return nil, err | |
} | |
defer file.Close() | |
var lines []string | |
scanner := bufio.NewScanner(file) | |
for scanner.Scan() { | |
lines = append(lines, scanner.Text()) | |
} | |
return lines, scanner.Err() | |
} | |
// writeLines writes the lines to the given file. | |
func writeLines(lines []string, path string) error { | |
file, err := os.Create(path) | |
if err != nil { | |
return err | |
} | |
defer file.Close() | |
w := bufio.NewWriter(file) | |
for _, line := range lines { | |
fmt.Fprintln(w, line) | |
} | |
return w.Flush() | |
} | |
func ConvertToDB2Date(str string) string{ | |
strMonth := string(str[2])+string(str[3]) // '01/01/1900 00:00:00' | |
strDate := string(str[5])+string(str[6]) | |
strYear := string(str[8])+string(str[9])+string(str[10])+string(str[11]) | |
strTime := "00:00:00" | |
strResult := []string{strYear,"-",strMonth,"-",strDate," ",strTime} | |
return strings.Join(strResult,"") | |
} | |
func FindTableName(str string) string{ | |
SQLCommand := strings.ToUpper(str) | |
Command := (strings.Split(SQLCommand, " ")) | |
TableName := "" | |
for i:=0;i<len(Command);i++{ | |
if Command[i] == "INTO" { | |
TableName = Command[i+1] | |
} | |
if Command[i] == "FROM" { | |
TableName = Command[i+1] | |
} | |
if strings.Contains(TableName,";") { | |
TableName = TableName[0:len(TableName)-1] | |
} | |
} | |
return TableName | |
} | |
func main() { | |
fileInputName := "INFOinputold.sql" | |
if len(os.Args) == 2 { | |
fileInputName = os.Args[1] | |
} | |
lines, err := readLines(fileInputName) | |
if err != nil { | |
log.Fatalf("readLines: %s", err) | |
} | |
var a []string | |
var tableName string | |
for i, line := range lines { | |
_=line | |
if tableName == ""{ | |
tableName = FindTableName(lines[i]) | |
} | |
//lines[i] = strings.Replace(line, " INFOTAB ", " SCHEMA1.INFOTAB ", -1) | |
//lines[i] = strings.Replace(lines[i], " INFOTAB;", " SCHEMA1.INFOTAB;", -1) | |
if !strings.Contains(lines[i],"SCHEMA1") { | |
lines[i] = strings.Replace(lines[i], " "+tableName+" ", " SCHEMA1."+tableName+" ", -1) | |
lines[i] = strings.Replace(lines[i], " "+tableName+";", " SCHEMA1."+tableName+";", -1) | |
} | |
a = (strings.Split(lines[i], ",")) | |
for j:=0;j<len(a);j++{ | |
if len(a[j]) == 22 { | |
if a[j][4] == a[j][7]{ | |
date := a[j] | |
a[j] = "'"+ConvertToDB2Date(date)+"'" | |
} | |
} | |
} | |
lines[i] = strings.Join(a,",") | |
} | |
if err := writeLines(lines, "OutputSQL.sql"); err != nil { | |
log.Fatalf("writeLines: %s", err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment