-
Notifications
You must be signed in to change notification settings - Fork 1
/
simplehstore.go
246 lines (218 loc) · 7.06 KB
/
simplehstore.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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// Package simplehstore offers a simple way to use a PostgreSQL database with HSTORE.
// The database back end is interchangeable with Redis (xyproto/simpleredis), BoltDB (xyproto/simplebolt) and
// MariaDB/MySQL (xyproto/simplemaria) by using the interfaces in the xyproto/pinterface package.
package simplehstore
import (
"database/sql"
"errors"
"fmt"
"log"
"strings"
// Using the PostgreSQL database engine
pq "github.com/lib/pq"
"github.com/xyproto/env/v2"
)
const (
// VersionString is the current version of simplehstore.
VersionString = "1.8.1"
defaultStringType = "TEXT"
defaultPort = 5432
encoding = "UTF8"
)
// Host represents a PostgreSQL database
type Host struct {
db *sql.DB
dbname string
// If set to true, any UTF-8 string will be let through as it is.
// Some UTF-8 strings may be unpalatable for PostgreSQL when performing
// SQL queries. The default is "false".
rawUTF8 bool
}
// Common for each of the db data structures used here
type dbDatastructure struct {
host *Host
table string
}
var defaultConnectionString = func() string {
password := env.Str("POSTGRES_PASSWORD")
s := env.Str("POSTGRES_USER", "postgres")
if password != "" {
s += ":" + password
}
s += "@127.0.0.1/" // for CI testing
return s
}()
var (
// The default "username:password@host:port/database" that the database is running at
defaultDatabaseName = env.Str("POSTGRES_DB", "postgres")
// ErrNoAvailableValues is used as an error if an SQL query returns no values
ErrNoAvailableValues = errors.New("no available values")
// ErrTooFewResults is used as an error if an SQL query returns too few results
ErrTooFewResults = errors.New("too few results")
// Column names
listCol = "a_list"
setCol = "a_set"
ownerCol = "owner"
kvPrefix = "a_kv_"
)
// SetColumnNames can be used to change the column names and prefixes that are used in the PostgreSQL tables.
// The default values are: "a_list", "a_set", "owner" and "a_kv_".
func SetColumnNames(list, set, hashMapOwner, keyValuePrefix string) {
listCol = list
setCol = set
ownerCol = hashMapOwner
kvPrefix = keyValuePrefix
}
// TestConnection checks if the local database server is up and running
func TestConnection() (err error) {
return TestConnectionHost(defaultConnectionString)
}
// TestConnectionHost checks if a given database server is up and running.
// connectionString may be on the form "username:password@host:port/database".
// The database name is ignored.
func TestConnectionHost(connectionString string) error {
newConnectionString, _ := rebuildConnectionString(connectionString, false)
// Connect to the given host:port
db, err := sql.Open("postgres", newConnectionString)
if err != nil {
return err
}
defer db.Close()
err = db.Ping()
if Verbose {
if err != nil {
log.Println("Ping: failed")
} else {
log.Println("Ping: ok")
}
}
return err
}
// TestConnectionHostWithDSN checks if a given database server is up and running.
func TestConnectionHostWithDSN(connectionString string) error {
// Connect to the given host:port
db, err := sql.Open("postgres", connectionString)
if err != nil {
return err
}
defer db.Close()
err = db.Ping()
if Verbose {
if err != nil {
log.Println("Ping: failed")
} else {
log.Println("Ping: ok")
}
}
return err
}
/* --- Host functions --- */
// NewHost sets up a new database connection.
// connectionString may be on the form "username:password@host:port/database".
func NewHost(connectionString string) *Host {
host, err := NewHost2(connectionString)
if err != nil {
log.Fatalln(err)
}
return host
}
// NewHost2 sets up a new database connection.
// connectionString may be on the form "username:password@host:port/database".
// An error may be returned.
func NewHost2(connectionString string) (*Host, error) {
newConnectionString, dbname := rebuildConnectionString(connectionString, true)
db, err := sql.Open("postgres", newConnectionString)
if err != nil {
return nil, fmt.Errorf("could not connect to %s", newConnectionString)
}
host := &Host{db, pq.QuoteIdentifier(dbname), false}
if err := host.Ping(); err != nil {
return nil, fmt.Errorf("database host does not reply to ping: %s", err)
}
if err := host.createDatabase(); err != nil {
return nil, fmt.Errorf("could not create database %s: %s", host.dbname, err)
}
if err := host.useDatabase(); err != nil {
return nil, fmt.Errorf("could not use database %s: %s", host.dbname, err)
}
return host, nil
}
// NewHostWithDSN creates a new database connection with a valid DSN.
func NewHostWithDSN(connectionString string, dbname string) *Host {
host, err := NewHostWithDSN2(connectionString, dbname)
if err != nil {
log.Fatalln(err)
}
return host
}
// NewHostWithDSN2 creates a new database connection with a valid DSN.
// An error may be returned.
func NewHostWithDSN2(connectionString string, dbname string) (*Host, error) {
db, err := sql.Open("postgres", connectionString)
if err != nil {
return nil, fmt.Errorf("could not connect to %s", connectionString)
}
host := &Host{db, pq.QuoteIdentifier(dbname), false}
if err := host.Ping(); err != nil {
return nil, fmt.Errorf("database host does not reply to ping: %s", err)
}
if err := host.createDatabase(); err != nil {
return nil, fmt.Errorf("could not create database %s: %s", host.dbname, err)
}
if err := host.useDatabase(); err != nil {
return nil, fmt.Errorf("could not use database %s: %s", host.dbname, err)
}
return host, nil
}
// New sets up a connection to the default (local) database host
func New() *Host {
connectionString := defaultConnectionString + defaultDatabaseName
if !strings.HasSuffix(defaultConnectionString, "/") {
connectionString = defaultConnectionString + "/" + defaultDatabaseName
}
return NewHost(connectionString)
}
// SetRawUTF8 can be used to select if the UTF-8 data be unprocessed, and not
// hex encoded and compressed. Unprocessed UTF-8 may be slightly faster,
// but malformed UTF-8 strings can potentially cause problems.
// Encoding the strings before sending them to PostgreSQL is the default.
// Choose the setting that best suits your situation.
func (host *Host) SetRawUTF8(enabled bool) {
host.rawUTF8 = enabled
}
// SelectDatabase sets a different database name and creates the database if needed.
func (host *Host) SelectDatabase(dbname string) error {
host.dbname = dbname
if err := host.createDatabase(); err != nil {
return err
}
return host.useDatabase()
}
// Will create the database if it does not already exist
func (host *Host) createDatabase() error {
if _, err := host.db.Exec(fmt.Sprintf("CREATE DATABASE %s WITH ENCODING '%s'", host.dbname, encoding)); err != nil {
if !strings.HasSuffix(err.Error(), "already exists") {
return err
}
}
return nil
}
// Use the host.dbname database
func (host *Host) useDatabase() error {
if Verbose {
log.Println("Using database " + host.dbname)
}
return nil
}
// Database returns the underlying *sql.DB database struct
func (host *Host) Database() *sql.DB {
return host.db
}
// Close the connection
func (host *Host) Close() {
host.db.Close()
}
// Ping the host
func (host *Host) Ping() error {
return host.db.Ping()
}