Skip to content

Commit

Permalink
Adds recognition of ORACLE_SID environment variable for oracle databases
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenneth Shaw committed Oct 25, 2017
1 parent 090bce0 commit 9f0e81f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
2 changes: 1 addition & 1 deletion drivers/oracle/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func init() {
if e, ok := err.(interface {
Code() int
}); ok {
return e.Code() == 1017
return e.Code() == 1017 || e.Code() == 1005
}
return false
},
Expand Down
18 changes: 9 additions & 9 deletions env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
"github.com/xo/usql/text"
)

// getenv tries retrieving successive keys from os environment variables.
func getenv(keys ...string) string {
// Getenv tries retrieving successive keys from os environment variables.
func Getenv(keys ...string) string {
for _, key := range keys {
if s := os.Getenv(key); s != "" {
return s
Expand Down Expand Up @@ -106,7 +106,7 @@ func OpenFile(u *user.User, path string, relative bool) (string, *os.File, error
func EditFile(u *user.User, path, line, s string) ([]rune, error) {
var err error

ed := getenv(text.CommandUpper()+"_EDITOR", "EDITOR", "VISUAL")
ed := Getenv(text.CommandUpper()+"_EDITOR", "EDITOR", "VISUAL")
if ed == "" {
return nil, text.ErrNoEditorDefined
}
Expand Down Expand Up @@ -135,7 +135,7 @@ func EditFile(u *user.User, path, line, s string) ([]rune, error) {
// setup args
args := []string{path}
if line != "" {
prefix := getenv(text.CommandUpper() + "_EDITOR_LINENUMBER_ARG")
prefix := Getenv(text.CommandUpper() + "_EDITOR_LINENUMBER_ARG")
if prefix == "" {
prefix = "+"
}
Expand Down Expand Up @@ -170,7 +170,7 @@ func EditFile(u *user.User, path, line, s string) ([]rune, error) {
func HistoryFile(u *user.User) string {
n := text.CommandUpper() + "_HISTORY"
path := "~/." + strings.ToLower(n)
if s := getenv(n); s != "" {
if s := Getenv(n); s != "" {
path = s
}

Expand All @@ -184,7 +184,7 @@ func HistoryFile(u *user.User) string {
func RCFile(u *user.User) string {
n := text.CommandUpper() + "RC"
path := "~/." + strings.ToLower(n)
if s := getenv(n); s != "" {
if s := Getenv(n); s != "" {
path = s
}

Expand All @@ -198,7 +198,7 @@ func RCFile(u *user.User) string {
func PassFile(u *user.User) string {
n := text.CommandUpper() + "PASS"
path := "~/." + strings.ToLower(n)
if s := getenv(n); s != "" {
if s := Getenv(n); s != "" {
path = s
}

Expand Down Expand Up @@ -327,9 +327,9 @@ func Chdir(u *user.User, path string) error {
func getshell() (string, string) {
var shell, param string

shell, param = getenv("SHELL"), "-c"
shell, param = Getenv("SHELL"), "-c"
if shell == "" && runtime.GOOS == "windows" {
shell, param = getenv("COMSPEC", "ComSpec"), "/c"
shell, param = Getenv("COMSPEC", "ComSpec"), "/c"
}

// look up path for "cmd.exe" if no other SHELL
Expand Down
11 changes: 11 additions & 0 deletions handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,17 @@ func (h *Handler) forceParams(u *dburl.URL) {
u.RawQuery = v.Encode()
}

// if oracle database, and the service name is not specified, use the
// environment variable if present.
if u.Driver == "ora" && strings.TrimPrefix(u.Path, "/") == "" {
if n := env.Getenv("ORACLE_SID", "ORASID"); n != "" {
u.Path = "/" + n
if u.Host == "" {
u.Host = "localhost"
}
}
}

// see if password entry is present
user, err := env.PassFileEntry(h.user, u)
if err != nil {
Expand Down

0 comments on commit 9f0e81f

Please sign in to comment.