Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LIKE Operator lookup #62

Merged
merged 5 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Upcoming changes...

## [0.5.1] - 2024-06-24
### Added
- Added case-insensitive like operator lookup based on DB type

## [0.5.0] - 2024-06-24
### Added
- Added DB query tracing option
Expand Down Expand Up @@ -40,3 +44,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[0.3.0]: https://github.com/scanoss/go-grpc-helper/compare/v0.2.0...v0.3.0
[0.4.0]: https://github.com/scanoss/go-grpc-helper/compare/v0.3.0...v0.4.0
[0.5.0]: https://github.com/scanoss/go-grpc-helper/compare/v0.4.0...v0.5.0
[0.5.1]: https://github.com/scanoss/go-grpc-helper/compare/v0.5.0...v0.5.1
20 changes: 20 additions & 0 deletions pkg/grpc/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import (
zlog "github.com/scanoss/zap-logging-helper/pkg/logger"
)

var defaultLike = "LIKE"

// OpenDBConnection establishes a connection specified database.
func OpenDBConnection(dsn, driver, user, passwd, host, schema, sslMode string) (*sqlx.DB, error) {
if len(dsn) == 0 {
Expand Down Expand Up @@ -84,3 +86,21 @@ func CloseSQLConnection(conn *sqlx.Conn) {
}
}
}

// GetLikeOperator attempts to determine the case-insensitive like operator based on the DB driver.
func GetLikeOperator(db *sqlx.DB) string {
if db != nil {
driverName := db.DriverName()
switch driverName {
case "postgres":
return "ILIKE"
case "sqlite3":
return defaultLike
}
zlog.S.Warnf("DriverName %s is unkown. Defaulting to %s", driverName, defaultLike)
} else {
zlog.S.Warnf("No DB object supplied. Defaulting to %s.", defaultLike)
}
// Return the default like operator
return defaultLike
}
37 changes: 37 additions & 0 deletions pkg/grpc/database/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package database

import (
"fmt"
"testing"

_ "github.com/lib/pq"
Expand Down Expand Up @@ -98,3 +99,39 @@ func TestCloseSQLConnection(t *testing.T) {
CloseSQLConnection(conn) // should pass first time
CloseSQLConnection(conn) // should fail second time
}

func TestGetILikeOperator(t *testing.T) {
err := zlog.NewSugaredDevLogger()
if err != nil {
t.Fatalf("an error '%s' was not expected when opening a sugared logger", err)
}
defer zlog.SyncZap()

likeOperator := GetLikeOperator(nil)
if likeOperator != "LIKE" {
t.Errorf("Expected 'LIKE' but got %s", likeOperator)
}
db, err := OpenDBConnection(":memory:", "sqlite3", "", "", "", "", "")
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
defer CloseDBConnection(db)

likeOperator = GetLikeOperator(db)
if likeOperator != "LIKE" {
t.Errorf("Expected 'LIKE' but got %s", likeOperator)
}
fmt.Printf("SQLite like operator is %s\n", likeOperator)

db, err = OpenDBConnection("", "postgres", "", "", "", "", "")
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
defer CloseDBConnection(db)

likeOperator = GetLikeOperator(db)
if likeOperator != "ILIKE" {
t.Errorf("Expected 'ILIKE' but got %s", likeOperator)
}
fmt.Printf("Postgres like operator is %s\n", likeOperator)
}
Loading