-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsert.go
More file actions
177 lines (144 loc) · 3.34 KB
/
insert.go
File metadata and controls
177 lines (144 loc) · 3.34 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package tsbuilder
import (
"bytes"
"errors"
"fmt"
"strings"
)
var _ TdEngineSQLBuilder = (*InsertBuilder)(nil)
type InsertTableData struct {
tableName string
using string
columns []string
values [][]any
tags map[string]any
}
type InsertBuilder struct {
tablesData []*InsertTableData
}
func NewInsertBuilder() *InsertBuilder {
return &InsertBuilder{
tablesData: make([]*InsertTableData, 0),
}
}
func (s *InsertBuilder) AddTable(tableName string) *InsertTableData {
for idx, table := range s.tablesData {
if table.tableName == tableName {
s.tablesData = append(s.tablesData[:idx], s.tablesData[idx+1:]...)
}
}
tData := &InsertTableData{
tableName: tableName,
tags: make(map[string]any),
columns: make([]string, 0),
values: make([][]any, 0),
}
s.tablesData = append(s.tablesData, tData)
return tData
}
func (s *InsertTableData) Using(v string) *InsertTableData {
s.using = v
return s
}
func (s *InsertTableData) Columns(columns ...string) *InsertTableData {
s.columns = columns
return s
}
func (s *InsertTableData) Values(values ...any) *InsertTableData {
s.values = append(s.values, values)
return s
}
func (s *InsertTableData) Tags(tags map[string]any) *InsertTableData {
if tags == nil {
return s
}
s.tags = tags
return s
}
func (s *InsertBuilder) Build() (string, error) {
b := bytes.NewBuffer([]byte{})
b.WriteString("INSERT INTO ")
for tIndex, table := range s.tablesData {
if err := table.validate(); err != nil {
return "", fmt.Errorf("validate error: %w", err)
}
// add table name
b.WriteString(table.tableName + " ")
// add using
if table.using != "" {
b.WriteString("USING " + table.using + " ")
}
// add tags
if len(table.tags) > 0 {
tagsKeys := make([]string, 0, len(table.tags))
tagsValues := make([]any, 0, len(table.tags))
for key, value := range table.tags {
tagsKeys = append(tagsKeys, key)
tagsValues = append(tagsValues, value)
}
b.WriteString("(")
for index, value := range tagsKeys {
b.WriteString(value)
if index != len(tagsKeys)-1 {
b.WriteString(", ")
}
}
b.WriteString(") ")
b.WriteString("TAGS (")
for index, value := range tagsValues {
v, err := castType(value)
if err != nil {
return "", err
}
b.WriteString(v)
if index != len(tagsValues)-1 {
b.WriteString(", ")
}
}
b.WriteString(") ")
}
// add columns
if len(table.columns) > 0 {
b.WriteString("(" + strings.Join(table.columns, ", ") + ") ")
}
// add values
b.WriteString("VALUES ")
for index, valuesGroup := range table.values {
if len(table.columns) != len(valuesGroup) {
return "", errors.New("columns should be equal values")
}
b.WriteString("(")
for vIndex, value := range valuesGroup {
v, err := castType(value)
if err != nil {
return "", err
}
b.WriteString(v)
if vIndex != len(valuesGroup)-1 {
b.WriteString(", ")
}
}
b.WriteString(")")
if index != len(table.values)-1 {
b.WriteString(" ")
}
}
if tIndex != len(s.tablesData)-1 {
b.WriteString(", ")
}
}
b.WriteString(";")
return b.String(), nil
}
func (s *InsertTableData) validate() error {
if s.tableName == "" {
return ErrEmptyTableName
}
if len(s.columns) == 0 {
return ErrEmptyColumns
}
if len(s.values) == 0 {
return ErrEmptyValues
}
return nil
}