Skip to content

Commit

Permalink
LIKE Operator lookup (#62)
Browse files Browse the repository at this point in the history
* added like operator lookup
  • Loading branch information
eeisegn authored Jun 24, 2024
1 parent bc7cc4c commit ba88bd8
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
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)
}

0 comments on commit ba88bd8

Please sign in to comment.