-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
76 lines (66 loc) · 1.66 KB
/
main.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
package main
import (
"database/sql"
"fmt"
_ "github.com/sijms/go-ora/v2"
"github.com/spf13/viper"
"log"
"net/url"
"time"
)
func connectToOracle() *sql.DB {
username := viper.GetString("database.username")
password := viper.GetString("database.password")
server := viper.GetString("database.server")
port := viper.GetString("database.port")
service := viper.GetString("database.service")
walletLocation := viper.GetString("database.walletLocation")
connectionString := "oracle://" + username + ":" + password + "@" + server + ":" + port + "/" + service
if walletLocation != "" {
connectionString += "?TRACE FILE=trace.log&SSL=enable&SSL Verify=false&WALLET=" + url.QueryEscape(walletLocation)
}
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))
}
return db
}
func main() {
viper.SetConfigName("oraconfig")
viper.SetConfigType("ini")
viper.AddConfigPath("/etc/oratest")
viper.AddConfigPath("$HOME/.oratest")
viper.AddConfigPath(".")
err := viper.ReadInConfig()
if err != nil {
panic(fmt.Errorf("Fatal error config file: %w \n", err))
}
var (
sysTimeStamp time.Time
)
db := connectToOracle()
rows, err := db.Query("SELECT SYSTIMESTAMP FROM DUAL")
if err != nil {
log.Panicln("error in query: ", err)
return
}
defer rows.Close()
for rows.Next() {
rows.Scan(&sysTimeStamp)
fmt.Printf("DB SERVER TIME=%s", sysTimeStamp)
}
err = rows.Err()
if err != nil {
log.Fatal(err)
}
defer func() {
err = db.Close()
if err != nil {
fmt.Println("Can't close connection: ", err)
}
}()
}