-
Notifications
You must be signed in to change notification settings - Fork 7
/
connection_postgres.go
149 lines (126 loc) · 4.78 KB
/
connection_postgres.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
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
package materialize
import (
"fmt"
"strings"
"github.com/jmoiron/sqlx"
)
type ConnectionPostgresBuilder struct {
Connection
connectionType string
postgresDatabase string
postgresHost string
postgresPort int
postgresUser ValueSecretStruct
postgresPassword IdentifierSchemaStruct
postgresSSHTunnel IdentifierSchemaStruct
postgresSSLCa ValueSecretStruct
postgresSSLCert ValueSecretStruct
postgresSSLKey IdentifierSchemaStruct
postgresSSLMode string
postgresAWSPrivateLink IdentifierSchemaStruct
validate bool
}
func NewConnectionPostgresBuilder(conn *sqlx.DB, obj MaterializeObject) *ConnectionPostgresBuilder {
b := Builder{conn, BaseConnection}
return &ConnectionPostgresBuilder{
Connection: Connection{b, obj.Name, obj.SchemaName, obj.DatabaseName},
}
}
func (b *ConnectionPostgresBuilder) ConnectionType(connectionType string) *ConnectionPostgresBuilder {
b.connectionType = connectionType
return b
}
func (b *ConnectionPostgresBuilder) PostgresDatabase(postgresDatabase string) *ConnectionPostgresBuilder {
b.postgresDatabase = postgresDatabase
return b
}
func (b *ConnectionPostgresBuilder) PostgresHost(postgresHost string) *ConnectionPostgresBuilder {
b.postgresHost = postgresHost
return b
}
func (b *ConnectionPostgresBuilder) PostgresPort(postgresPort int) *ConnectionPostgresBuilder {
b.postgresPort = postgresPort
return b
}
func (b *ConnectionPostgresBuilder) PostgresUser(postgresUser ValueSecretStruct) *ConnectionPostgresBuilder {
b.postgresUser = postgresUser
return b
}
func (b *ConnectionPostgresBuilder) PostgresPassword(postgresPassword IdentifierSchemaStruct) *ConnectionPostgresBuilder {
b.postgresPassword = postgresPassword
return b
}
func (b *ConnectionPostgresBuilder) PostgresSSHTunnel(postgresSSHTunnel IdentifierSchemaStruct) *ConnectionPostgresBuilder {
b.postgresSSHTunnel = postgresSSHTunnel
return b
}
func (b *ConnectionPostgresBuilder) PostgresSSLCa(postgresSSLCa ValueSecretStruct) *ConnectionPostgresBuilder {
b.postgresSSLCa = postgresSSLCa
return b
}
func (b *ConnectionPostgresBuilder) PostgresSSLCert(postgresSSLCert ValueSecretStruct) *ConnectionPostgresBuilder {
b.postgresSSLCert = postgresSSLCert
return b
}
func (b *ConnectionPostgresBuilder) PostgresSSLKey(postgresSSLKey IdentifierSchemaStruct) *ConnectionPostgresBuilder {
b.postgresSSLKey = postgresSSLKey
return b
}
func (b *ConnectionPostgresBuilder) PostgresSSLMode(postgresSSLMode string) *ConnectionPostgresBuilder {
b.postgresSSLMode = postgresSSLMode
return b
}
func (b *ConnectionPostgresBuilder) PostgresAWSPrivateLink(postgresAWSPrivateLink IdentifierSchemaStruct) *ConnectionPostgresBuilder {
b.postgresAWSPrivateLink = postgresAWSPrivateLink
return b
}
func (b *ConnectionPostgresBuilder) Validate(validate bool) *ConnectionPostgresBuilder {
b.validate = validate
return b
}
func (b *ConnectionPostgresBuilder) Create() error {
q := strings.Builder{}
q.WriteString(fmt.Sprintf(`CREATE CONNECTION %s TO POSTGRES (`, b.QualifiedName()))
q.WriteString(fmt.Sprintf(`HOST %s`, QuoteString(b.postgresHost)))
q.WriteString(fmt.Sprintf(`, PORT %d`, b.postgresPort))
if b.postgresUser.Text != "" {
q.WriteString(fmt.Sprintf(`, USER %s`, QuoteString(b.postgresUser.Text)))
}
if b.postgresUser.Secret.Name != "" {
q.WriteString(fmt.Sprintf(`, USER SECRET %s`, b.postgresUser.Secret.QualifiedName()))
}
if b.postgresPassword.Name != "" {
q.WriteString(fmt.Sprintf(`, PASSWORD SECRET %s`, b.postgresPassword.QualifiedName()))
}
if b.postgresSSLMode != "" {
q.WriteString(fmt.Sprintf(`, SSL MODE %s`, QuoteString(b.postgresSSLMode)))
}
if b.postgresSSHTunnel.Name != "" {
q.WriteString(fmt.Sprintf(`, SSH TUNNEL %s`, b.postgresSSHTunnel.QualifiedName()))
}
if b.postgresSSLCa.Text != "" {
q.WriteString(fmt.Sprintf(`, SSL CERTIFICATE AUTHORITY %s`, QuoteString(b.postgresSSLCa.Text)))
}
if b.postgresSSLCa.Secret.Name != "" {
q.WriteString(fmt.Sprintf(`, SSL CERTIFICATE AUTHORITY SECRET %s`, b.postgresSSLCa.Secret.QualifiedName()))
}
if b.postgresSSLCert.Text != "" {
q.WriteString(fmt.Sprintf(`, SSL CERTIFICATE %s`, QuoteString(b.postgresSSLCert.Text)))
}
if b.postgresSSLCert.Secret.Name != "" {
q.WriteString(fmt.Sprintf(`, SSL CERTIFICATE SECRET %s`, b.postgresSSLCert.Secret.QualifiedName()))
}
if b.postgresSSLKey.Name != "" {
q.WriteString(fmt.Sprintf(`, SSL KEY SECRET %s`, b.postgresSSLKey.QualifiedName()))
}
if b.postgresAWSPrivateLink.Name != "" {
q.WriteString(fmt.Sprintf(`, AWS PRIVATELINK %s`, b.postgresAWSPrivateLink.QualifiedName()))
}
q.WriteString(fmt.Sprintf(`, DATABASE %s`, QuoteString(b.postgresDatabase)))
q.WriteString(`)`)
if !b.validate {
q.WriteString(` WITH (VALIDATE = false)`)
}
q.WriteString(`;`)
return b.ddl.exec(q.String())
}