Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 5 additions & 2 deletions convert_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"strings"
"testing"
)

Expand Down Expand Up @@ -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)
}
Expand All @@ -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())
}
Expand Down
64 changes: 64 additions & 0 deletions fileOperations.go
Original file line number Diff line number Diff line change
@@ -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.")
}
18 changes: 15 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand All @@ -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)
}
}
34 changes: 0 additions & 34 deletions readfile.go

This file was deleted.

3 changes: 2 additions & 1 deletion structs.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package main

type CommandLineArgs struct {
fileName string
fileName string
outputFileName string
}

var cmdLineArgs CommandLineArgs
Loading