-
Notifications
You must be signed in to change notification settings - Fork 0
/
templatesql.go
54 lines (47 loc) · 1.23 KB
/
templatesql.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
package main
import "bytes"
const (
templateSQL = "sql"
contentTypeSQL = "text/sql"
)
type TemplateSQL struct {
schema Schema
linePrefix []byte
}
func (template TemplateSQL) Generate(chunk *Chunk) ([]byte, error) {
var buffer bytes.Buffer
buffer.Write(template.getLinePrefix(chunk))
for _, column := range template.schema.columns {
val, err := generateValue(chunk, template.schema.emptyValue, column)
if err != nil {
return nil, err
}
buffer.WriteByte('\'')
buffer.WriteString(val)
buffer.WriteByte('\'')
buffer.WriteByte(',')
}
buffer.Truncate(buffer.Len() - 1)
buffer.WriteString(");")
buffer.WriteByte('\n')
return buffer.Bytes(), nil
}
func (template TemplateSQL) ContentType() string {
return contentTypeSQL
}
func (template TemplateSQL) getLinePrefix(chunk *Chunk) []byte {
if template.linePrefix == nil {
var buffer bytes.Buffer
buffer.WriteString("INSERT INTO ")
buffer.WriteString(template.schema.document)
buffer.WriteString(" (")
for _, column := range template.schema.columns {
buffer.WriteString(column.Column().name)
buffer.WriteByte(',')
}
buffer.Truncate(buffer.Len() - 1)
buffer.WriteString(") VALUES (")
template.linePrefix = buffer.Bytes()
}
return template.linePrefix
}