fix(xugu): require database before connecting
This commit is contained in:
1 parent
95c17abfd1
commit
4a5dca63fb
16 files changed
+206
-3
No files matched your search
@@ -1384,6 +1384,9 @@ func (s *server) validateConnection() error {
|
||||
}
|
||||
|
||||
func openDB(params connectParams) (*sql.DB, error) {
|
||||
if configuredDatabaseName(params) == "" {
|
||||
return nil, errors.New("XuguDB requires an existing database name; set Database or include DB in the connection string")
|
||||
}
|
||||
dsn := buildDSN(params)
|
||||
db, err := sql.Open("xugu", dsn)
|
||||
if err != nil {
|
||||
@@ -1920,13 +1923,47 @@ func isXuguMissingOnNullColumnError(err error) bool {
|
||||
}
|
||||
|
||||
func xuguDSNValue(dsn string, key string) string {
|
||||
for _, part := range strings.Split(dsn, ";") {
|
||||
var result string
|
||||
for _, part := range splitXuguDSNSegments(dsn) {
|
||||
name, value, ok := strings.Cut(part, "=")
|
||||
if ok && strings.EqualFold(strings.TrimSpace(name), key) {
|
||||
return strings.TrimSpace(value)
|
||||
value = strings.TrimSpace(value)
|
||||
if len(value) >= 2 && value[0] == '\'' && value[len(value)-1] == '\'' {
|
||||
value = strings.ReplaceAll(value[1:len(value)-1], "''", "'")
|
||||
}
|
||||
result = strings.TrimSpace(value)
|
||||
}
|
||||
}
|
||||
return ""
|
||||
return result
|
||||
}
|
||||
|
||||
func splitXuguDSNSegments(dsn string) []string {
|
||||
var segments []string
|
||||
var current strings.Builder
|
||||
inQuotes := false
|
||||
|
||||
for index := 0; index < len(dsn); index++ {
|
||||
character := dsn[index]
|
||||
if character == '\'' {
|
||||
current.WriteByte(character)
|
||||
if inQuotes && index+1 < len(dsn) && dsn[index+1] == '\'' {
|
||||
current.WriteByte(dsn[index+1])
|
||||
index++
|
||||
} else {
|
||||
inQuotes = !inQuotes
|
||||
}
|
||||
continue
|
||||
}
|
||||
if character == ';' && !inQuotes {
|
||||
segments = append(segments, current.String())
|
||||
current.Reset()
|
||||
continue
|
||||
}
|
||||
current.WriteByte(character)
|
||||
}
|
||||
|
||||
segments = append(segments, current.String())
|
||||
return segments
|
||||
}
|
||||
|
||||
func (s *server) listSchemas() ([]string, error) {
|
||||
|
||||
@@ -452,6 +452,34 @@ func TestBuildDSNUsesConnectionFields(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpenDBRejectsMissingDatabaseBeforeOpeningDriver(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
params connectParams
|
||||
}{
|
||||
{name: "empty structured database", params: connectParams{Host: "db.example.com", Username: "app"}},
|
||||
{name: "whitespace structured database", params: connectParams{Host: "db.example.com", Database: " \t ", Username: "app"}},
|
||||
{name: "empty native DSN database", params: connectParams{ConnectionString: "IP=db.example.com;DB=;User=app"}},
|
||||
{name: "quoted whitespace native DSN database", params: connectParams{ConnectionString: "IP=db.example.com;DB=' ';User=app"}},
|
||||
{name: "URL without database", params: connectParams{ConnectionString: "xugu://app:secret@db.example.com:5138/"}},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
db, err := openDB(test.params)
|
||||
if err == nil {
|
||||
if db != nil {
|
||||
_ = db.Close()
|
||||
}
|
||||
t.Fatal("expected missing database to be rejected before opening the driver")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "XuguDB requires an existing database name") {
|
||||
t.Fatalf("unexpected missing-database error: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildDSNUsesDefaultPort(t *testing.T) {
|
||||
dsn := buildDSN(connectParams{
|
||||
Host: "db.example.com",
|
||||
@@ -707,6 +735,9 @@ func TestConfiguredDatabaseName(t *testing.T) {
|
||||
{params: connectParams{ConnectionString: "xugu://user:secret@db.example.com:5138/demo"}, want: "demo"},
|
||||
{params: connectParams{ConnectionString: "jdbc:xugu://db.example.com:5138/reporting"}, want: "reporting"},
|
||||
{params: connectParams{ConnectionString: "IP=db.example.com;DB=SYSTEM;User=SYSDBA;PWD=secret"}, want: "SYSTEM"},
|
||||
{params: connectParams{ConnectionString: "IP=db.example.com;DB='shop;east';User=APP"}, want: "shop;east"},
|
||||
{params: connectParams{ConnectionString: "IP=db.example.com;DB='sales''east';User=APP"}, want: "sales'east"},
|
||||
{params: connectParams{ConnectionString: "IP=db.example.com;DB=' ';User=APP"}, want: ""},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
|
||||
Reference in new issue
Block a user