-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpsqlconn.go
96 lines (81 loc) · 1.75 KB
/
psqlconn.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
package psqlconn
import (
"bufio"
"database/sql"
"fmt"
"io"
"os"
"strconv"
"strings"
_ "github.com/lib/pq"
)
func check(e error) {
if e != nil {
panic(e)
}
}
type Config map[string]string
func Trim(s string) string {
s = strings.TrimPrefix(s, `"`)
s = strings.TrimSuffix(s, `"`)
return s
}
func ReadConfig(filename string) (Config, error) {
// init with some bogus data
config := Config{}
if len(filename) == 0 {
return config, nil
}
file, err := os.Open(filename)
check(err)
defer file.Close()
reader := bufio.NewReader(file)
for {
line, err := reader.ReadString('\n')
// check if the line has = sign
// and process the line. Ignore the rest.
if equal := strings.Index(line, "="); equal >= 0 {
if key := strings.TrimSpace(line[:equal]); len(key) > 0 {
value := ""
if len(line) > equal {
value = strings.TrimSpace(line[equal+1:])
}
// assign the config map
config[key] = value
}
}
if err == io.EOF {
break
}
check(err)
}
return config, nil
}
func DBconn(dbfilepath string) {
config, err := ReadConfig(dbfilepath)
check(err)
// Host Trimming
host := config["host"]
host = Trim(host)
//Port Str to Int
port_str := config["port"]
number, err := strconv.ParseUint(port_str, 10, 32)
port := int(number)
//user trimming
user := config["user"]
user = Trim(user)
//Password Trimming
password := config["password"]
password = Trim(password)
//Dbname Trimming
dbname := config["dbname"]
dbname = Trim(dbname)
//SQL Kismi
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+"password=%s dbname=%s sslmode=disable", host, port, user, password, dbname)
db, err := sql.Open("postgres", psqlInfo)
check(err)
defer db.Close()
err = db.Ping()
check(err)
fmt.Println("Successfully connected!")
}