-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
57 lines (43 loc) · 994 Bytes
/
database.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
package tsbuilder
import (
"bytes"
"fmt"
"strings"
)
var _ tdEngineSqlBuilder = (*databaseBuilder)(nil)
type databaseBuilder struct {
name string
options []string
}
func NewDatabaseBuilder() *databaseBuilder {
return &databaseBuilder{
options: make([]string, 0),
}
}
func (s *databaseBuilder) Name(name string) *databaseBuilder {
s.name = name
return s
}
func (s *databaseBuilder) Options(options ...string) *databaseBuilder {
s.options = append(s.options, options...)
return s
}
func (s *databaseBuilder) Build() (string, error) {
if err := s.validate(); err != nil {
return "", fmt.Errorf("validate error: %w", err)
}
b := bytes.NewBuffer([]byte{})
b.WriteString("CREATE DATABASE IF NOT EXISTS ")
// add name
b.WriteString(s.name + " ")
// add options
b.WriteString(strings.Join(s.options, " "))
b.WriteString(";")
return b.String(), nil
}
func (s databaseBuilder) validate() error {
if s.name == "" {
return ErrEmptyDatabaseName
}
return nil
}