[ref] move sql context to adaper pkg

Signed-off-by: Anton Nesterov <anton@demiurg.io>
This commit is contained in:
Anton Nesterov 2024-08-09 16:56:38 +02:00
parent fde44ce343
commit b72e67badc
No known key found for this signature in database
GPG key ID: 59121E8AE2851FB5
13 changed files with 56 additions and 27 deletions

View file

@ -1,4 +1,4 @@
package filters
package adapter
import (
"strconv"

11
pkg/adapter/go.mod Normal file
View file

@ -0,0 +1,11 @@
module l12.xyz/dal/adapter
go 1.22.6
replace l12.xyz/dal/utils v0.0.0 => ../utils
replace l12.xyz/dal/filters v0.0.0 => ../filters
require l12.xyz/dal/utils v0.0.0
require github.com/pkg/errors v0.9.1 // indirect

2
pkg/adapter/go.sum Normal file
View file

@ -0,0 +1,2 @@
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=

10
pkg/adapter/types.go Normal file
View file

@ -0,0 +1,10 @@
package adapter
type CtxOpts map[string]string
type Context interface {
New(opts CtxOpts) Context
GetTableName() string
GetFieldName() string
NormalizeValue(interface{}) interface{}
}

View file

@ -2,12 +2,10 @@ package dal
import (
"testing"
filters "l12.xyz/dal/filters"
)
func TestConvertFieldsBool(t *testing.T) {
ctx := filters.SQLiteContext{
ctx := SQLiteContext{
TableAlias: "t",
FieldName: "test",
}
@ -24,7 +22,7 @@ func TestConvertFieldsBool(t *testing.T) {
}
func TestConvertFieldsInt(t *testing.T) {
ctx := filters.SQLiteContext{
ctx := SQLiteContext{
TableAlias: "t",
FieldName: "test",
}
@ -41,7 +39,7 @@ func TestConvertFieldsInt(t *testing.T) {
}
func TestConvertFieldsStr(t *testing.T) {
ctx := filters.SQLiteContext{
ctx := SQLiteContext{
TableAlias: "t",
FieldName: "test",
}

View file

@ -2,8 +2,6 @@ package dal
import (
"testing"
f "l12.xyz/dal/filters"
)
func TestConvertFind(t *testing.T) {
@ -13,7 +11,7 @@ func TestConvertFind(t *testing.T) {
"$gt": 1,
},
}
ctx := f.SQLiteContext{
ctx := SQLiteContext{
TableAlias: "t",
}
result := CovertFind(ctx, find)
@ -37,7 +35,7 @@ func TestConvertFindAnd(t *testing.T) {
},
},
}
ctx := f.SQLiteContext{
ctx := SQLiteContext{
TableAlias: "t",
}
result := CovertFind(ctx, find)
@ -61,7 +59,7 @@ func TestConvertFindOr(t *testing.T) {
},
},
}
ctx := f.SQLiteContext{
ctx := SQLiteContext{
TableAlias: "t",
}
result := CovertFind(ctx, find)

View file

@ -3,12 +3,10 @@ package dal
import (
"fmt"
"testing"
filters "l12.xyz/dal/filters"
)
func TestConvertInsert(t *testing.T) {
ctx := filters.SQLiteContext{
ctx := SQLiteContext{
TableName: "test",
TableAlias: "t",
}

View file

@ -3,9 +3,11 @@ package dal
import (
"testing"
f "l12.xyz/dal/filters"
adapter "l12.xyz/dal/adapter"
)
type SQLiteContext = adapter.SQLiteContext
func TestJoin(t *testing.T) {
j := Join{
For: "artist a",
@ -14,7 +16,7 @@ func TestJoin(t *testing.T) {
},
As: "LEFT",
}
ctx := f.SQLiteContext{
ctx := SQLiteContext{
TableAlias: "t",
}
result := j.Convert(ctx)
@ -34,7 +36,7 @@ func TestConvertJoin(t *testing.T) {
},
},
}
ctx := f.SQLiteContext{
ctx := SQLiteContext{
TableAlias: "t",
}
result := ConvertJoin(ctx, joins...)
@ -51,7 +53,7 @@ func TestConvertMap(t *testing.T) {
joins := []interface{}{
Map{"$for": "artist a", "$do": Map{"a.impl": "t.impl"}, "$as": "LEFT"},
}
ctx := f.SQLiteContext{
ctx := SQLiteContext{
TableAlias: "t",
}
result := ConvertJoin(ctx, joins...)

View file

@ -11,3 +11,7 @@ require l12.xyz/dal/filters v0.0.0
require github.com/pkg/errors v0.9.1 // indirect
replace l12.xyz/dal/filters v0.0.0 => ../filters
require l12.xyz/dal/adapter v0.0.0
replace l12.xyz/dal/adapter v0.0.0 => ../adapter

View file

@ -1,6 +1,7 @@
package dal
import (
adapter "l12.xyz/dal/adapter"
filters "l12.xyz/dal/filters"
)
@ -10,5 +11,5 @@ type Find = filters.Find
type Query = filters.Find
type Filter = filters.Filter
type Is = filters.Filter
type Context = filters.Context
type CtxOpts = filters.CtxOpts
type Context = adapter.Context
type CtxOpts = adapter.CtxOpts

View file

@ -7,3 +7,7 @@ require github.com/pkg/errors v0.9.1 // indirect
require l12.xyz/dal/utils v0.0.0
replace l12.xyz/dal/utils v0.0.0 => ../utils
require l12.xyz/dal/adapter v0.0.0
replace l12.xyz/dal/adapter v0.0.0 => ../adapter

View file

@ -1,12 +1,9 @@
package filters
type CtxOpts map[string]string
type Context interface {
New(opts CtxOpts) Context
GetTableName() string
GetFieldName() string
NormalizeValue(interface{}) interface{}
}
import "l12.xyz/dal/adapter"
type CtxOpts = adapter.CtxOpts
type Context = adapter.Context
type IFilter interface {
ToSQLPart(ctx Context) string

View file

@ -2,8 +2,12 @@ package filters
import (
"testing"
adapter "l12.xyz/dal/adapter"
)
type SQLiteContext = adapter.SQLiteContext
func TestEq(t *testing.T) {
ctx := SQLiteContext{
TableAlias: "t",