-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnector.go
More file actions
125 lines (101 loc) · 3.2 KB
/
Copy pathconnector.go
File metadata and controls
125 lines (101 loc) · 3.2 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
// Copyright 2018 Eryx <evorui аt gmail dοt cοm>, All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package pgsqlgo
import (
"database/sql"
"errors"
"fmt"
"strings"
_ "github.com/lib/pq"
"github.com/lynkdb/lynkapi/go/lynktable"
)
// NewConnector opens a PostgreSQL connection pool.
//
// Supported option keys:
// - "host", "port", "user", "pass", "dbname" for a TCP connection
// - "socket", "user", "pass", "dbname" for a unix domain socket connection
func NewConnector(opts map[string]string) (lynktable.Connector, error) {
var dsn string
if v := opts["host"]; v != "" {
dsn = fmt.Sprintf(
"host=%s port=%s user=%s password=%s dbname=%s sslmode=disable",
v, opts["port"], opts["user"], opts["pass"], opts["dbname"],
)
} else if v := opts["socket"]; v != "" {
dsn = fmt.Sprintf(
"host=%s user=%s password=%s dbname=%s sslmode=disable",
v, opts["user"], opts["pass"], opts["dbname"],
)
} else {
return nil, errors.New("Incorrect configuration")
}
db, err := sql.Open("postgres", dsn)
if err != nil {
return nil, err
}
base, err := lynktable.NewBase(db)
if err != nil {
return nil, err
}
base.BindVar = dialectStmtBindVar
base.QuoteStr = dialectQuoteStr
for k, v := range dialectStmts {
base.StmtSet(k, v)
}
return &Dialect{
Base: base,
dbName: opts["dbname"],
}, nil
}
// Upsert inserts a row, or on conflict of the given keys updates the
// remaining columns from the EXCLUDED row (INSERT ... ON CONFLICT DO UPDATE).
func (dc *Dialect) Upsert(tableName string, item map[string]any, keys map[string]any) lynktable.Result {
if len(item) == 0 || len(keys) == 0 {
return lynktable.NewResult(nil, errors.New("invalid args"))
}
var (
cols, vars, vals, sets, pkeys = []string{}, []string{}, []any{}, []string{}, []string{}
)
for key, val := range item {
cols = append(cols, dialectQuoteStr(key))
vars = append(vars, "?")
vals = append(vals, val)
sets = append(sets, fmt.Sprintf("%s = EXCLUDED.%s", dialectQuoteStr(key), dialectQuoteStr(key)))
}
for key, val := range keys {
cols = append(cols, dialectQuoteStr(key))
vars = append(vars, "?")
vals = append(vals, val)
pkeys = append(pkeys, dialectQuoteStr(key))
}
q := fmt.Sprintf("INSERT INTO %s (%s) VALUES (%s) ON CONFLICT (%s) DO UPDATE SET %s",
dialectQuoteStr(tableName),
strings.Join(cols, ","),
strings.Join(vars, ","),
strings.Join(pkeys, ","),
strings.Join(sets, ","))
if dc.BindVar != nil {
q, vals = dc.BindVar(q, vals)
}
stmt, err := dc.DB().Prepare(q)
if err != nil {
return lynktable.NewResult(nil, err)
}
defer stmt.Close()
res, err := stmt.Exec(vals...)
if err != nil {
return lynktable.NewResult(nil, err)
}
return lynktable.NewResult(res, nil)
}