Skip to content

Commit c2a681d

Browse files
authored
Simplify TranslateOperator implementation across all drivers (#7)
Replace switch statements with slice-based lookups for better maintainability and consistency. Adds supportedOperations slice to each driver following the same pattern as supportedFeatures.
1 parent 81f89ab commit c2a681d

3 files changed

Lines changed: 71 additions & 68 deletions

File tree

drivers/clickhouse/clickhouse.go

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,27 @@ import (
88
"github.com/pseudomuto/where"
99
)
1010

11-
var supportsFeatures = []string{
12-
"ARRAY",
13-
"FINAL",
14-
"GLOBAL",
15-
"ILIKE",
16-
"JSON",
17-
"PREWHERE",
18-
"SAMPLE",
19-
"TUPLE",
20-
"WITH",
21-
}
11+
var (
12+
supportedFeatures = []string{
13+
"ARRAY",
14+
"FINAL",
15+
"GLOBAL",
16+
"ILIKE",
17+
"JSON",
18+
"PREWHERE",
19+
"SAMPLE",
20+
"TUPLE",
21+
"WITH",
22+
}
23+
24+
supportedOperations = []string{
25+
"=", "!=", "<>", "<", ">", "<=", ">=",
26+
"LIKE", "NOT LIKE", "ILIKE", "NOT ILIKE",
27+
"IN", "NOT IN",
28+
"IS NULL", "IS NOT NULL",
29+
"BETWEEN", "NOT BETWEEN",
30+
}
31+
)
2232

2333
type (
2434
// ClickHouseDriver implements the where.Driver interface for ClickHouse databases.
@@ -87,24 +97,15 @@ func (d *ClickHouseDriver) Keywords() []string {
8797

8898
func (d *ClickHouseDriver) TranslateOperator(op string) (string, bool) {
8999
upperOp := strings.ToUpper(op)
90-
switch upperOp {
91-
case "=", "!=", "<>", "<", ">", "<=", ">=":
92-
return op, true
93-
case "LIKE", "NOT LIKE", "ILIKE", "NOT ILIKE":
100+
if slices.Contains(supportedOperations, upperOp) {
94101
return upperOp, true
95-
case "IN", "NOT IN":
96-
return upperOp, true
97-
case "IS NULL", "IS NOT NULL":
98-
return upperOp, true
99-
case "BETWEEN", "NOT BETWEEN":
100-
return upperOp, true
101-
default:
102-
return "", false
103102
}
103+
104+
return "", false
104105
}
105106

106107
func (d *ClickHouseDriver) SupportsFeature(feature string) bool {
107-
return slices.Contains(supportsFeatures, strings.ToUpper(feature))
108+
return slices.Contains(supportedFeatures, strings.ToUpper(feature))
108109
}
109110

110111
func init() {

drivers/mysql/mysql.go

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,23 @@ import (
88
"github.com/pseudomuto/where"
99
)
1010

11-
var supportedFeatures = []string{
12-
"CTE",
13-
"FULLTEXT",
14-
"JSON",
15-
"PARTITION",
16-
"SPATIAL",
17-
}
11+
var (
12+
supportedFeatures = []string{
13+
"CTE",
14+
"FULLTEXT",
15+
"JSON",
16+
"PARTITION",
17+
"SPATIAL",
18+
}
19+
20+
supportedOperations = []string{
21+
"=", "!=", "<>", "<", ">", "<=", ">=",
22+
"LIKE", "NOT LIKE",
23+
"IN", "NOT IN",
24+
"IS NULL", "IS NOT NULL",
25+
"BETWEEN", "NOT BETWEEN",
26+
}
27+
)
1828

1929
type (
2030
// MySQLDriver implements the where.Driver interface for MySQL and MariaDB databases.
@@ -83,24 +93,15 @@ func (d *MySQLDriver) Keywords() []string {
8393

8494
func (d *MySQLDriver) TranslateOperator(op string) (string, bool) {
8595
upperOp := strings.ToUpper(op)
86-
switch upperOp {
87-
case "=", "!=", "<>", "<", ">", "<=", ">=":
88-
return op, true
89-
case "LIKE", "NOT LIKE":
90-
return upperOp, true
91-
case "ILIKE":
92-
return "LIKE", true
93-
case "NOT ILIKE":
94-
return "NOT LIKE", true
95-
case "IN", "NOT IN":
96+
if slices.Contains(supportedOperations, upperOp) {
9697
return upperOp, true
97-
case "IS NULL", "IS NOT NULL":
98-
return upperOp, true
99-
case "BETWEEN", "NOT BETWEEN":
100-
return upperOp, true
101-
default:
102-
return "", false
10398
}
99+
100+
if upperOp == "ILIKE" || upperOp == "NOT ILIKE" {
101+
return strings.Replace(upperOp, "ILIKE", "LIKE", 1), true
102+
}
103+
104+
return "", false
104105
}
105106

106107
func (d *MySQLDriver) SupportsFeature(feature string) bool {

drivers/postgres/postgres.go

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,25 @@ import (
88
"github.com/pseudomuto/where"
99
)
1010

11-
var supportedFeatures = []string{
12-
"ARRAY",
13-
"CTE",
14-
"ILIKE",
15-
"JSON",
16-
"JSONB",
17-
"RETURNING",
18-
"WINDOW",
19-
}
11+
var (
12+
supportedFeatures = []string{
13+
"ARRAY",
14+
"CTE",
15+
"ILIKE",
16+
"JSON",
17+
"JSONB",
18+
"RETURNING",
19+
"WINDOW",
20+
}
21+
22+
supportedOperations = []string{
23+
"=", "!=", "<>", "<", ">", "<=", ">=",
24+
"LIKE", "NOT LIKE", "ILIKE", "NOT ILIKE",
25+
"IN", "NOT IN",
26+
"IS NULL", "IS NOT NULL",
27+
"BETWEEN", "NOT BETWEEN",
28+
}
29+
)
2030

2131
type (
2232
// PostgreSQLDriver implements the where.Driver interface for PostgreSQL databases.
@@ -85,20 +95,11 @@ func (d *PostgreSQLDriver) Keywords() []string {
8595

8696
func (d *PostgreSQLDriver) TranslateOperator(op string) (string, bool) {
8797
upperOp := strings.ToUpper(op)
88-
switch upperOp {
89-
case "=", "!=", "<>", "<", ">", "<=", ">=":
90-
return op, true
91-
case "LIKE", "NOT LIKE", "ILIKE", "NOT ILIKE":
98+
if slices.Contains(supportedOperations, upperOp) {
9299
return upperOp, true
93-
case "IN", "NOT IN":
94-
return upperOp, true
95-
case "IS NULL", "IS NOT NULL":
96-
return upperOp, true
97-
case "BETWEEN", "NOT BETWEEN":
98-
return upperOp, true
99-
default:
100-
return "", false
101100
}
101+
102+
return "", false
102103
}
103104

104105
func (d *PostgreSQLDriver) SupportsFeature(feature string) bool {

0 commit comments

Comments
 (0)