Skip to content

Commit

Permalink
#121 Add dbhelper.ParseDatabaseURL
Browse files Browse the repository at this point in the history
  • Loading branch information
docktermj committed Nov 13, 2024
1 parent d8bf851 commit 2b3bd23
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 16 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning].

-

## [0.5.4] - 2024-11-04
## [0.5.4] - 2024-11-13

### Added in 0.5.4

Expand Down
14 changes: 3 additions & 11 deletions connector/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/senzing-garage/go-databasing/connectororacle"
"github.com/senzing-garage/go-databasing/connectorpostgresql"
"github.com/senzing-garage/go-databasing/connectorsqlite"
"github.com/senzing-garage/go-databasing/dbhelper"
)

// ----------------------------------------------------------------------------
Expand All @@ -33,18 +34,9 @@ Input
func NewConnector(ctx context.Context, databaseURL string) (driver.Connector, error) {
var result driver.Connector

parsedURL, err := url.Parse(databaseURL)
parsedURL, err := dbhelper.ParseDatabaseURL(databaseURL)
if err != nil {

if strings.HasPrefix(databaseURL, "postgresql") {
index := strings.LastIndex(databaseURL, ":")
newDatabaseURL := databaseURL[:index] + "/" + databaseURL[index+1:]
parsedURL, err = url.Parse(newDatabaseURL)
}

if err != nil {
return result, err
}
return result, err
}

// Parse URL: https://pkg.go.dev/net/url#URL
Expand Down
25 changes: 24 additions & 1 deletion dbhelper/dbhelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func ExtractSqliteDatabaseFilename(databaseURL string) (string, error) {
return result, fmt.Errorf("sqlite3 URL schema needed")
}

parsedURL, err := url.Parse(databaseURL)
parsedURL, err := ParseDatabaseURL(databaseURL)
if err != nil {
return result, err
}
Expand Down Expand Up @@ -68,3 +68,26 @@ func GetMessenger(componentID int, idMessages map[int]string, callerSkip int, op
}
return result
}

/*
Function ParseDatabaseURL is fixes database URLs prior to parsing them.
Input
- databaseURL: The database URL to be parsed.
Output
- [url.URL]
[url.URL]: https://pkg.go.dev/net/url#URL
*/
func ParseDatabaseURL(databaseURL string) (*url.URL, error) {
result, err := url.Parse(databaseURL)
if err != nil {
if strings.HasPrefix(databaseURL, "postgresql") {
index := strings.LastIndex(databaseURL, ":")
newDatabaseURL := databaseURL[:index] + "/" + databaseURL[index+1:]
result, err = url.Parse(newDatabaseURL)
}
}
return result, err
}
21 changes: 21 additions & 0 deletions dbhelper/dbhelper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
type testCaseMetadata struct {
databaseFilename string
databaseURL string
fixedDatabaseURL string
name string
succeeds bool
}
Expand Down Expand Up @@ -55,6 +56,12 @@ var testCasesForMultiPlatform = []testCaseMetadata{
databaseURL: "postgresql://username:password@hostname:5432/G2/?schema=schemaname",
succeeds: false,
},
{
name: "postgresql-003",
databaseURL: "postgresql://username:password@hostname:5432:G2/?schema=schemaname",
fixedDatabaseURL: "postgresql://username:password@hostname:5432/G2/?schema=schemaname",
succeeds: false,
},
{
name: "sqlite-001",
databaseURL: "sqlite3://na:na@/tmp/sqlite/G2C.db",
Expand Down Expand Up @@ -89,3 +96,17 @@ func TestGetMessenger(test *testing.T) {
options := []interface{}{}
_ = GetMessenger(1, map[int]string{}, 0, options...)
}

func TestParseDatabaseURL(test *testing.T) {
for _, testCase := range testCases {
test.Run(testCase.name, func(test *testing.T) {
result, err := ParseDatabaseURL(testCase.databaseURL)
require.NoError(test, err)
if len(testCase.fixedDatabaseURL) > 0 {
assert.Equal(test, testCase.fixedDatabaseURL, result.String())
} else {
assert.Equal(test, testCase.databaseURL, result.String())
}
})
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ require (
golang.org/x/net v0.31.0 // indirect
golang.org/x/sys v0.27.0 // indirect
golang.org/x/text v0.20.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20241104194629-dd2ea8efbc28 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20241113154021-e0fbfb71d213 // indirect
google.golang.org/grpc v1.68.0 // indirect
google.golang.org/protobuf v1.35.1 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ golang.org/x/term v0.26.0 h1:WEQa6V3Gja/BhNxg540hBip/kkaYtRg3cxg4oXSw4AU=
golang.org/x/term v0.26.0/go.mod h1:Si5m1o57C5nBNQo5z1iq+XDijt21BDBDp2bK0QI8e3E=
golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug=
golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4=
google.golang.org/genproto/googleapis/rpc v0.0.0-20241104194629-dd2ea8efbc28 h1:XVhgTWWV3kGQlwJHR3upFWZeTsei6Oks1apkZSeonIE=
google.golang.org/genproto/googleapis/rpc v0.0.0-20241104194629-dd2ea8efbc28/go.mod h1:GX3210XPVPUjJbTUbvwI8f2IpZDMZuPJWDzDuebbviI=
google.golang.org/genproto/googleapis/rpc v0.0.0-20241113154021-e0fbfb71d213 h1:L+WcQXqkyf5MX6g7AudgYEuJjmYbqSRkTmJqGfAPw+Y=
google.golang.org/genproto/googleapis/rpc v0.0.0-20241113154021-e0fbfb71d213/go.mod h1:GX3210XPVPUjJbTUbvwI8f2IpZDMZuPJWDzDuebbviI=
google.golang.org/grpc v1.68.0 h1:aHQeeJbo8zAkAa3pRzrVjZlbz6uSfeOXlJNQM0RAbz0=
google.golang.org/grpc v1.68.0/go.mod h1:fmSPC5AsjSBCK54MyHRx48kpOti1/jRfOlwEWywNjWA=
google.golang.org/protobuf v1.35.1 h1:m3LfL6/Ca+fqnjnlqQXNpFPABW1UD7mjh8KO2mKFytA=
Expand Down

0 comments on commit 2b3bd23

Please sign in to comment.