-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon_db.go
77 lines (62 loc) · 1.77 KB
/
common_db.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
package main
import (
"database/sql"
"fmt"
"net/url"
"github.com/BurntSushi/toml"
_ "github.com/sijms/go-ora/v2"
)
var db *sql.DB
type autonomousDB struct {
Service string `toml:"service"`
Username string `toml:"username"`
Server string `toml:"server"`
Port string `toml:"port"`
Password string `toml:"password"`
WalletLocation string `toml:"walletLocation"`
}
func OpenDBConnection() {
var adb autonomousDB
if _, err := toml.DecodeFile("db.toml", &adb); err != nil {
fmt.Println(err)
}
connectionString := "oracle://" + adb.Username + ":" + adb.Password + "@" + adb.Server + ":" + adb.Port + "/" + adb.Service
fmt.Println("Connecting to Oracle autonomous database based on wallet")
fmt.Println("Service:", adb.Service)
if adb.WalletLocation != "" {
connectionString += "?TRACE FILE=trace.log&SSL=enable&SSL Verify=false&WALLET=" + url.QueryEscape(adb.WalletLocation)
}
var err error
db, err = sql.Open("oracle", connectionString)
if err != nil {
panic(fmt.Errorf("error in sql.Open: %w", err))
}
err = db.Ping()
if err != nil {
panic(fmt.Errorf("error pinging db: %w", err))
}
}
func CloseDBConnection() {
fmt.Println("Closing connection to Oracle autonomous database")
err := db.Close()
if err != nil {
fmt.Println("Can't close connection: ", err)
}
}
func DBExecSQL(query string, args ...any) (sql.Result, error) {
return db.Exec(query, args...)
}
func DBExecQuery(query string, args ...any) (*sql.Rows, error) {
rows, err := db.Query(query, args...)
return rows, err
}
func DBExecQueryRow(query string, args ...any) *sql.Row {
row := db.QueryRow(query, args...)
return row
}
func DBPrepare(query string) (*sql.Stmt, error) {
return db.Prepare(query)
}
func DBBeginTx() (*sql.Tx, error) {
return db.Begin()
}