-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql_writing.go
63 lines (54 loc) · 1.42 KB
/
sql_writing.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package pgtalk
import (
"bytes"
"io"
"strings"
)
type WriteContext interface {
Write(p []byte) (n int, err error)
WithAlias(tableName, alias string) WriteContext
TableAlias(tableName, defaultAlias string) string
}
type wc struct {
writer io.Writer
aliases map[string]string
}
// NewWriteContext returns a new WriteContext to produce SQL
func NewWriteContext(w io.Writer) WriteContext {
return wc{
writer: w,
aliases: map[string]string{},
}
}
// WithAlias returns a new context that knows about the table alias
func (w wc) WithAlias(tableName, alias string) WriteContext {
cp := wc{
writer: w.writer,
aliases: map[string]string{},
}
cp.aliases[tableName] = alias
return cp
}
// Write is part of io.Writer
func (w wc) Write(p []byte) (n int, err error) {
return w.writer.Write(p)
}
// TableAlias returns the alias for a tableName if present.
func (w wc) TableAlias(tableName, defaultAlias string) string {
a, ok := w.aliases[tableName]
if ok {
return a
}
return defaultAlias
}
// IndentedSQL returns source with tabs and lines trying to have a formatted view.
func IndentedSQL(some SQLWriter) string {
buf := new(bytes.Buffer)
some.SQLOn(NewWriteContext(buf))
return buf.String()
}
// SQL returns source as a oneliner without tabs or line ends.
func SQL(some SQLWriter) string {
src := IndentedSQL(some)
return strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(src, "\t", " "), "\n", " "), " ", " ")
}