forked from nytimes/gizmo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oracle.go
52 lines (46 loc) · 1.14 KB
/
oracle.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
package oracle
import (
"database/sql"
"fmt"
"github.com/NYTimes/gizmo/config"
)
// Config holds everything you need to
// connect and interact with an Oracle DB.
type Config struct {
User string `envconfig:"ORACLE_USER"`
Pw string `envconfig:"ORACLE_PW"`
Host string `envconfig:"ORACLE_HOST_NAME"`
Port int `envconfig:"ORACLE_PORT"`
DBName string `envconfig:"ORACLE_DB_NAME"`
ConnectString string `envconfig:"ORACLE_CONNECT_STRING"`
}
// DB will attempt to open a sql connection.
// Users must import an oci8 driver in their
// main to use this.
func (o *Config) DB() (*sql.DB, error) {
return sql.Open("oci8", o.String())
}
// String will return the Oracle connection string.
func (o *Config) String() string {
if o.ConnectString != "" {
return fmt.Sprintf("%s/%s@%s",
o.User,
o.Pw,
o.ConnectString,
)
}
return fmt.Sprintf("%s/%s@%s:%d/%v",
o.User,
o.Pw,
o.Host,
o.Port,
o.DBName,
)
}
// LoadConfigFromEnv will attempt to load an OracleCreds object
// from environment variables.
func LoadConfigFromEnv() Config {
var ora Config
config.LoadEnvConfig(&ora)
return ora
}