diff --git a/README.md b/README.md index d1fe014..16bf8bb 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,9 @@ # s2m > [!WARNING] -> Work in progress, from version [0.5.0](https://github.com/Filip7/s2m/releases/tag/v0.5.0) it works with taking user input +> Work in progress, currently it can read from stdin and files, also it can save the output to a file +> BUT, I have not tested it on larger sql files, so there might be performance issues +> Will work on them Single to multi line SQL @@ -42,6 +44,20 @@ Use it like this: ./s2m -f export.sql ``` +To save the output to file, pass `-o` command line flag with the name of the file. + +```sh +./s2m -f export.sql -o output.sql +``` + +Also works with standard in + +```sh +./s2m -o output.sql "SELECT * from mail; \ +INSERT into mail(id) values (1); \ +INSERT into mail(id) values (2)" +``` + ## Do I need this? Actually no, if you use Intellij/DataGrip or any other Jetbrains IDE, the IDE can do that for you. diff --git a/convert.go b/convert.go index fde1bf1..9373b08 100644 --- a/convert.go +++ b/convert.go @@ -49,21 +49,21 @@ func addCorrectEnding(i int, lines []string, isFirst int, fromFile bool) { } } -func ConvertSingleLineToMultilineSQL(input string) string { +func ConvertSingleLineToMultilineSQL(input string) []string { lines := strings.Split(input, ";") lines = removeEmptyStrings(lines) convert(lines, false) - return strings.Join(lines, "") + return lines } -func ConvertSingleLineToMultilineSQLFromFile(input []string) string { +func ConvertSingleLineToMultilineSQLFromFile(input []string) []string { input = removeEmptyStrings(input) convert(input, true) - return strings.Join(input, "") + return input } func convert(lines []string, fromFile bool) { diff --git a/convert_test.go b/convert_test.go index 210419e..909b2e0 100644 --- a/convert_test.go +++ b/convert_test.go @@ -1,6 +1,7 @@ package main import ( + "strings" "testing" ) @@ -72,7 +73,8 @@ INSERT INTO films (code, title, did, date_prod, kind) VALUES ('B6717', 'Tampopo' } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got := ConvertSingleLineToMultilineSQL(tt.input) + converted := ConvertSingleLineToMultilineSQL(tt.input) + got := strings.Join(converted, "") if got != tt.want { t.Errorf("Expected\n\"%s\"\ngot\n\"%s\"", tt.want, got) } @@ -85,7 +87,8 @@ func TestConvertSingleLineToMultilineSQLFromFile(t *testing.T) { INSERT INTO films (code, title, did, date_prod, kind) VALUES ('B6717', 'Tampopo', 110, '1985-02-10', 'Comedy'), ('HG120', 'The Dinner Game', 140, DEFAULT, 'Comedy'); SELECT * FROM films2;` - out, err := readFile(".test.sql") + read, err := readFile(".test.sql") + out := strings.Join(ConvertSingleLineToMultilineSQLFromFile(read), "") if err != nil { t.Errorf("Error happened %s", err.Error()) } diff --git a/fileOperations.go b/fileOperations.go new file mode 100644 index 0000000..b26e173 --- /dev/null +++ b/fileOperations.go @@ -0,0 +1,64 @@ +package main + +import ( + "bufio" + "fmt" + "log" + "os" +) + +func readFile(filePath string) ([]string, error) { + var s []string + file, err := os.Open(filePath) + if err != nil { + log.Fatal(err) + } + defer file.Close() + + reader := bufio.NewReader(file) + for { + line, err := reader.ReadString('\n') + if err != nil { + if err.Error() == "EOF" { + break + } + fmt.Println("Error reading file: ", err) + return nil, err + } + + s = append(s, line) + } + + return s, nil +} + +func saveToFile(filePath string, data []string) { + file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) + if err != nil { + log.Println("error opening file:", err) + return + } + // Ensure the file is closed after the function completes + defer file.Close() + + // Create a buffered writer + writer := bufio.NewWriter(file) + + // Write a string to the buffer + for _, line := range data { + _, err = writer.WriteString(line) + if err != nil { + fmt.Println("error writing to buffer:", err) + return + } + } + + // Flush the buffer to ensure all data is written to the file + err = writer.Flush() + if err != nil { + fmt.Println("error flushing buffer:", err) + return + } + + fmt.Println("data written successfully.") +} diff --git a/main.go b/main.go index 5a4b954..204541c 100644 --- a/main.go +++ b/main.go @@ -4,10 +4,12 @@ import ( "flag" "fmt" "os" + "strings" ) func parseFlags() { flag.StringVar(&cmdLineArgs.fileName, "f", "", "Define file to read") + flag.StringVar(&cmdLineArgs.outputFileName, "o", "", "Define file to save the output to") flag.Parse() } @@ -21,13 +23,23 @@ func main() { os.Exit(1) } - fmt.Println(out) + converted := ConvertSingleLineToMultilineSQLFromFile(out) + if cmdLineArgs.outputFileName != "" { + saveToFile(cmdLineArgs.outputFileName, converted) + } else { + fmt.Println(strings.Join(converted, "")) + } os.Exit(0) } args := flag.Args() sqlInput := args[len(args)-1] - output := ConvertSingleLineToMultilineSQL(sqlInput) + converted := ConvertSingleLineToMultilineSQL(sqlInput) - fmt.Println(output) + if cmdLineArgs.outputFileName != "" { + saveToFile(cmdLineArgs.outputFileName, converted) + } else { + output := strings.Join(converted, "") + fmt.Println(output) + } } diff --git a/readfile.go b/readfile.go deleted file mode 100644 index 304f0c5..0000000 --- a/readfile.go +++ /dev/null @@ -1,34 +0,0 @@ -package main - -import ( - "bufio" - "fmt" - "log" - "os" -) - -func readFile(filePath string) (string, error) { - var s []string - file, err := os.Open(filePath) - if err != nil { - log.Fatal(err) - } - defer file.Close() - - reader := bufio.NewReader(file) - for { - line, err := reader.ReadString('\n') - if err != nil { - if err.Error() == "EOF" { - break - } - fmt.Println("Error reading file: ", err) - return "", err - } - - s = append(s, line) - } - - out := ConvertSingleLineToMultilineSQLFromFile(s) - return out, nil -} diff --git a/structs.go b/structs.go index f486857..77190fd 100644 --- a/structs.go +++ b/structs.go @@ -1,7 +1,8 @@ package main type CommandLineArgs struct { - fileName string + fileName string + outputFileName string } var cmdLineArgs CommandLineArgs