-
Notifications
You must be signed in to change notification settings - Fork 1
/
sql.go
46 lines (36 loc) · 740 Bytes
/
sql.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package utils
import (
"io"
"os"
"os/exec"
"strings"
)
type SqlResultColumn struct {
Name string
Type string
}
func AnalyzeSQL(sql string, url string) ([]SqlResultColumn, error) {
cmd := exec.Command("psql", url, "-A", "-F,", "-t")
stdin, err := cmd.StdinPipe()
if err != nil {
return nil, err
}
go func() {
defer stdin.Close()
io.WriteString(stdin, sql+"\\gdesc")
}()
cmd.Stderr = os.Stderr
out, err := cmd.Output()
if err != nil {
return nil, err
}
lines := strings.Split(string(out), "\n")
var result []SqlResultColumn
for _, line := range lines {
xs := strings.Split(line, ",")
if len(xs) == 2 {
result = append(result, SqlResultColumn{Name: xs[0], Type: xs[1]})
}
}
return result, nil
}