/
database.go
100 lines (81 loc) · 2.04 KB
/
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
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
package postgres
import (
"context"
"crypto/tls"
"errors"
"fmt"
"log"
"os"
"sync"
"github.com/go-pg/pg/v10"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/spf13/viper"
)
var once sync.Once
type DB struct {
pgx *pgxpool.Pool
}
// variabel Global
var db *DB
func GetInstanceDB() *DB {
once.Do(func() {
db = &DB{}
pgx, err := pgxpool.Connect(context.Background(), viper.GetViper().GetString("lily"))
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to connection to database: %v\n", err)
db.pgx = nil
return
}
log.Println("db -> connected")
db.pgx = pgx
})
return db
}
func (db *DB) Close() {
if db != nil {
db.pgx.Close()
}
}
// old stuff
type Database struct {
Db *pg.DB
}
// New makes a new database using the connection string and
// returns it, otherwise returns the error
func (t *Database) New(host string, port int, user string, password string, dbName string, searchPath string) error {
// fmt.Print(opt.TLSConfig)
t.Db = pg.Connect(&pg.Options{
Addr: fmt.Sprintf("%s:%d", host, port),
User: user,
Password: password,
Database: dbName,
TLSConfig: &tls.Config{InsecureSkipVerify: true},
OnConnect: func(ctx context.Context, conn *pg.Conn) error {
_, err := conn.Exec("set search_path=?", searchPath)
if err != nil {
return err
}
return nil
},
})
//db, err := sql.Open("postgres", connString)
if t.Db == nil {
return errors.New("no database connection")
}
// Check that our connection is good
if err := t.Db.Ping(context.Background()); err != nil {
return err
}
return nil
}
// ConnString returns a connection string based on the parameters it's given
// This would normally also contain the password, however we're not using one
func (t *Database) ConnString(host string, port int, user string, password string, dbName string, searchPath string) string {
return fmt.Sprintf(
"host=%s port=%d user=%s password=%s dbname=%s sslmode=require search_path=%s",
host, port, user, password, dbName, searchPath,
)
}
func (t *Database) Close() error {
return t.Db.Close()
}