Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
erstam committed Sep 10, 2021
0 parents commit 6aa1a99
Show file tree
Hide file tree
Showing 8 changed files with 424 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

# Jetbrains IDE
.idea/
*.iml
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2021 Eric St-Amand <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Go-pgsanity

A Golang port of [markdrago/pgsanity](https://github.com/markdrago/pgsanity)
to run sanity checks on SQL scripts with focus on PostgreSQL syntax.

Under the hood, pgsanity uses
[ecpg](https://www.postgresql.org/docs/current/app-ecpg.html).

> ecpg is the embedded SQL preprocessor for C programs.
> It converts C programs with embedded SQL statements to
> normal C code by replacing the SQL invocations with
> special function calls. The output files can then be
> processed with any C compiler tool chain.
## License

See [LICENSE](./LICENSE) for details.

## Installation

```
go get github.com/erstam/go-pgsanity/pgsanity
```

You can control where the executable gets installed
using the %GOBIN% env var.

**Note:** Make sure you have Postgres installed and the `ecgp` tool
can be found in %PATH%.

## Usage

At the cmd prompt / linux shell:

```
> pgsanity <path_to_file_or_folder>
```

- If a file path is provided, it must end with `.sql`.
- If a folder path is provided, the folder is scanned for all `.sql` files,
and those are processed, others are skipped.

**Note:** tested on Windows only!

## Contribute

PRs are welcome! :)
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/erstam/go-pgsanity

go 1.16
21 changes: 21 additions & 0 deletions internal/args/args.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* Copyright (c) 2021 Eric St-Amand
See LICENSE for details. */

package args

import (
"flag"
"log"
)

func Parse() string {
flag.Parse()
args := flag.Args()
if len(args) == 0 {
log.Fatal("pgsanity: missing valid file or directory argument")
}
if len(args) > 1 {
log.Fatal("pgsanity: too many arguments, only 1 argument supported")
}
return args[0]
}
242 changes: 242 additions & 0 deletions internal/ecpg/ecpg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
/* Copyright (c) 2021 Eric St-Amand
See LICENSE for details. */

package ecpg

import (
"bytes"
"fmt"
"io"
"log"
"os"
"os/exec"
)

const NoneInt = -1

func FromRawSQLFilePath(f string) []byte {
var err error
var fh *os.File
var buf []byte
if fh, err = os.Open(f); err != nil {
log.Fatalf("pgsanity: cannot open file %s: %v", f, err)
}
defer func(fh *os.File) {
err := fh.Close()
if err != nil {
log.Fatalf("pgsanity: cannot close file %s: %v", f, err)
}
}(fh)
if buf, err = io.ReadAll(fh); err != nil {
log.Fatalf("pgsanity: cannot read file %s: %v", f, err)
}

return prepareSql(buf)
}

type reader interface {
ReadString(delim byte) (line string, err error)
}

func read(r reader, delim []byte) (line []byte, err error) {
for {
s := ""
s, err = r.ReadString(delim[len(delim)-1])
if err != nil {
line = nil
return
}

line = append(line, []byte(s)...)
if bytes.HasSuffix(line, delim) {
return line[:len(line)-len(delim)], nil
}
}
}

func prepareSql(buf []byte) []byte {

var result = bytes.NewBuffer(make([]byte, 0, 2*len(buf)))

inStatement := false
inLineComment := false
inBlockComment := false

for _, segment := range parseSegments(buf) {

start := segment.Start
end := segment.End
contents := segment.Content

precontents := ""
startString := start

if !inStatement && !inLineComment && !inBlockComment {
// Currently not in any block
if start != "--" && start != "/*" && len(bytes.TrimSpace(contents)) > 0 {
inStatement = true
precontents = "EXEC SQL "
}
}

if start == "/*" {
inBlockComment = true
} else if start == "--" && !inBlockComment {
inLineComment = true
if !inStatement {
startString = "//"
}
}

result.Write([]byte(startString))
result.Write([]byte(precontents))
result.Write(contents)

if !inLineComment && !inBlockComment && inStatement && end == ";" {
inStatement = false
}

if inBlockComment && end == "*/" {
inBlockComment = false
}

if inLineComment && end == "\n" {
inLineComment = false
}
}

return result.Bytes()
}

type Segment struct {
Start string
End string
Content []byte
}

func (s Segment) String() string {
return fmt.Sprintf("Segment: Start bookend: %s, End bookend: %s, content: %s", s.Start, s.End, s.Content)
}

func parseSegments(buf []byte) []Segment {
segments := make([]Segment, 0, 10)

bookends := []string{"\n", ";", "--", "/*", "*/"}
lastBookendFound := ""
start := 0

for {
end, bookend := getNextOccurence(buf[start:], bookends)
if end == NoneInt {
// This is probably the last Segment
segment := Segment{
Start: lastBookendFound,
End: "",
Content: buf[start:],
}
segments = append(segments, segment)
start++
} else {
end = start + end
segment := Segment{
Start: lastBookendFound,
End: bookend,
Content: buf[start:end],
}
segments = append(segments, segment)
start = end + len(bookend)
lastBookendFound = bookend
}

if start > len(buf) {
break
}
}

return segments
}

func getNextOccurence(buf []byte, bookends []string) (end int, bookend string) {

end = len(buf)
var line []byte
var err error
var chosenBookend = ""

for _, bookend := range bookends {
if line, err = read(bytes.NewBuffer(buf), []byte(bookend)); err != nil {
if err != io.EOF {
log.Fatalf("pgsanity: Internal error while searching for next Segment: %v", err)
}
}
if line != nil {
if len(line) < end {
end = len(line)
chosenBookend = bookend
}
}
}

if end == len(buf) {
// No new occurence
return NoneInt, chosenBookend
} else {
return end, chosenBookend
}
}

func CheckSyntax(content []byte) (err error) {

//fmt.Println(string(content))

ecpgCmd := exec.Command("ecpg", "-o", "-", "-")

var cmdIn io.WriteCloser
var cmdOut io.ReadCloser
var cmdErr io.ReadCloser
var outBytes []byte

if cmdIn, err = ecpgCmd.StdinPipe(); err != nil {
return
}
if cmdOut, err = ecpgCmd.StdoutPipe(); err != nil {
return
}
if cmdErr, err = ecpgCmd.StderrPipe(); err != nil {
return
}
if err = ecpgCmd.Start(); err != nil {
return
}

// Run the write on stdin in a goroutine to allow the main thread to start reading output pipes.
// Otherwise, on large SQL files this can cause a deadlock in the io with the subprocess.
go func() {
if _, err = cmdIn.Write(content); err != nil {
return
}
if err = cmdIn.Close(); err != nil {
return
}
}()

if outBytes, err = io.ReadAll(cmdOut); err != nil {
return
}
//if len(outBytes) > 0 {
// log.Println(string(outBytes))
//}

if outBytes, err = io.ReadAll(cmdErr); err != nil {
return
}
if len(outBytes) > 0 {
log.Println(string(outBytes))
}

if err = ecpgCmd.Wait(); err != nil {
return
}

return nil
}
Loading

0 comments on commit 6aa1a99

Please sign in to comment.