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

Positional Parameter Support #30

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
26 changes: 19 additions & 7 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ const (

// Default values for configuration.
const (
DefaultTagName = "rql"
DefaultOpPrefix = "$"
DefaultFieldSep = "_"
DefaultLimit = 25
DefaultMaxLimit = 100
Offset = "offset"
Limit = "limit"
DefaultTagName = "rql"
DefaultOpPrefix = "$"
DefaultFieldSep = "_"
DefaultLimit = 25
DefaultMaxLimit = 100
DefaultParamOffset = 1
DefaultParamSymbol = "?"
Offset = "offset"
Limit = "limit"
)

var (
Expand Down Expand Up @@ -136,6 +138,14 @@ type Config struct {
// DefaultSort is the default value for the 'Sort' field that returns when no sort expression is supplied by the caller.
// It defaults to an empty string slice.
DefaultSort []string
// ParamSymbol is the placehold for parameters in the Filter expression the default is '?', postgres for example uses '$'
ParamSymbol string
// PositionalParams if true will append a numerical suffix to the ParamSymbol, i.e. ?1, ?2, etc.
PositionalParams bool
// ParamOffset is the zero-based parameter offset added to positional parameters
// This allows the parameters to begin at another offeset and useful when the FilterExp falls after other arguments
// manually numbered in the SQL statement, the default is 1
ParamOffset int
}

// defaults sets the default configuration of Config.
Expand All @@ -152,11 +162,13 @@ func (c *Config) defaults() error {
if c.ColumnFn == nil {
c.ColumnFn = Column
}
defaultString(&c.ParamSymbol, DefaultParamSymbol)
defaultString(&c.TagName, DefaultTagName)
defaultString(&c.OpPrefix, DefaultOpPrefix)
defaultString(&c.FieldSep, DefaultFieldSep)
defaultInt(&c.DefaultLimit, DefaultLimit)
defaultInt(&c.LimitMaxValue, DefaultMaxLimit)
defaultInt(&c.ParamOffset, DefaultParamOffset)
return nil
}

Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module github.com/a8m/rql

require github.com/mailru/easyjson v0.0.0-20180823135443-60711f1a8329
require (
github.com/mailru/easyjson v0.0.0-20180823135443-60711f1a8329
)

go 1.13
37 changes: 37 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,39 @@
github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc=
github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y=
github.com/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd h1:83Wprp6ROGeiHFAP8WJdI2RoxALQYgdllERc3N5N2DM=
github.com/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU=
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 h1:Yzb9+7DPaBjB8zlTR87/ElzFsnQfuHnVUVqpZZIcV5Y=
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5/go.mod h1:a2zkGnVExMxdzMo3M0Hi/3sEU+cWnZpSni0O6/Yb/P0=
github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe h1:lXe2qZdvpiX5WZkZR4hgp4KJVfY3nMkvmwbVkpv1rVY=
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0=
github.com/jinzhu/gorm v1.9.14 h1:Kg3ShyTPcM6nzVo148fRrcMO6MNKuqtOUwnzqMgVniM=
github.com/jinzhu/gorm v1.9.14/go.mod h1:G3LB3wezTOWM2ITLzPxEXgSkOXAntiLHS7UdBefADcs=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.0.1 h1:HjfetcXq097iXP0uoPCdnM4Efp5/9MsM0/M+XOTeR3M=
github.com/jinzhu/now v1.0.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs=
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/lib/pq v1.1.1 h1:sJZmqHoEaY7f+NPP8pgLB/WxulyR3fewgCM2qaSlBb4=
github.com/lib/pq v1.1.1/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/mailru/easyjson v0.0.0-20180823135443-60711f1a8329 h1:2gxZ0XQIU/5z3Z3bUBu+FXuk2pFbkN6tcwi/pjyaDic=
github.com/mailru/easyjson v0.0.0-20180823135443-60711f1a8329/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mattn/go-sqlite3 v1.14.0 h1:mLyGNKR8+Vv9CAU7PphKa2hkEqxxhn8i32J6FPj1/QA=
github.com/mattn/go-sqlite3 v1.14.0/go.mod h1:JIl7NbARA7phWnGvh0LKTyg7S9BA+6gx71ShQilpsus=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191205180655-e7c4368fe9dd h1:GGJVjV8waZKRHrgwvtH66z9ZGVurTD1MT0n1Bb+q4aM=
golang.org/x/crypto v0.0.0-20191205180655-e7c4368fe9dd/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
18 changes: 16 additions & 2 deletions rql.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ type Params struct {
// Args: "a8m", 22
FilterExp string
FilterArgs []interface{}
// PositionalParams if true will append a numerical suffix to the ParamSymbol, i.e. ?1, ?2, etc.
PositionalParams bool
// ParamSymbol is the placehold for parameters in the Filter expression the default is '?', postgres for example uses '$'
ParamSymbol string
}

// ParseError is type of error returned when there is a parsing problem.
Expand Down Expand Up @@ -189,6 +193,8 @@ func (p *Parser) ParseQuery(q *Query) (pr *Params, err error) {
pr.FilterExp = ps.String()
pr.FilterArgs = ps.values
pr.Sort = p.sort(q.Sort)
pr.PositionalParams = p.PositionalParams
pr.ParamSymbol = p.ParamSymbol
if len(pr.Sort) == 0 && len(p.DefaultSort) > 0 {
pr.Sort = p.sort(p.DefaultSort)
}
Expand Down Expand Up @@ -349,6 +355,7 @@ type parseState struct {
*Parser // reference of the parser config
*bytes.Buffer // query builder
values []interface{} // query values
argN int // current arg counter
}

var parseStatePool sync.Pool
Expand All @@ -367,6 +374,7 @@ func (p *Parser) newParseState() (ps *parseState) {
}
ps.values = make([]interface{}, 0, 8)
ps.Parser = p
ps.argN = 0
return
}

Expand Down Expand Up @@ -467,9 +475,15 @@ func (p *parseState) field(f *field, v interface{}) {

// fmtOp create a string for the operation with a placeholder.
// for example: "name = ?", or "age >= ?".
func (p *Parser) fmtOp(field string, op Op) string {
func (p *parseState) fmtOp(field string, op Op) string {
param := p.ParamSymbol
if p.PositionalParams {
param = fmt.Sprintf("%s%d", p.ParamSymbol, p.argN+p.ParamOffset)
}
p.argN++

colName := p.colName(field)
return colName + " " + op.SQL() + " ?"
return colName + " " + op.SQL() + " " + param
}

// colName formats the query field to database column name in cases the user configured a custom
Expand Down
125 changes: 119 additions & 6 deletions rql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"
"testing"
"time"
"unicode"
)

func TestInit(t *testing.T) {
Expand Down Expand Up @@ -929,6 +930,102 @@ func TestParse(t *testing.T) {
}
}

func TestExtensions(t *testing.T) {
tests := []struct {
name string
conf Config
input []byte
wantErr bool
wantOut *Params
}{
{
name: "alternate parameter symbol test",
conf: Config{
Model: new(struct {
Age int `rql:"filter"`
Name string `rql:"filter"`
Address string `rql:"filter"`
Other string
}),
ParamSymbol: "@",
DefaultLimit: 25,
},
input: []byte(`{
"filter": {
"name": "foo",
"age": 12,
"$or": [
{ "address": "DC" },
{ "address": "Marvel" }
],
"$and": [
{ "age": { "$neq": 10} },
{ "age": { "$neq": 20} },
{ "$or": [{ "age": 11 }, {"age": 10}] }
]
}
}`),
wantOut: &Params{
Limit: 25,
FilterExp: "name = @ AND age = @ AND (address = @ OR address = @) AND (age <> @ AND age <> @ AND (age = @ OR age = @))",
FilterArgs: []interface{}{"foo", 12, "DC", "Marvel", 10, 20, 11, 10},
ParamSymbol: "@",
},
},
{
name: "positional parameter symbol test",
conf: Config{
Model: new(struct {
Age int `rql:"filter"`
Name string `rql:"filter"`
Address string `rql:"filter"`
Other string
}),
ParamSymbol: "$",
PositionalParams: true,
DefaultLimit: 25,
},
input: []byte(`{
"filter": {
"name": "foo",
"age": 12,
"$or": [
{ "address": "DC" },
{ "address": "Marvel" }
],
"$and": [
{ "age": { "$neq": 10} },
{ "age": { "$neq": 20} },
{ "$or": [{ "age": 11 }, {"age": 10}] }
]
}
}`),
wantOut: &Params{
Limit: 25,
FilterExp: "name = $1 AND age = $2 AND (address = $3 OR address = $4) AND (age <> $5 AND age <> $6 AND (age = $7 OR age = $8))",
FilterArgs: []interface{}{"foo", 12, "DC", "Marvel", 10, 20, 11, 10},
ParamSymbol: "$",
PositionalParams: true,
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.conf.Log = t.Logf
p, err := NewParser(tt.conf)
if err != nil {
t.Fatalf("failed to build parser: %v", err)
}
out, err := p.Parse(tt.input)
if tt.wantErr != (err != nil) {
t.Fatalf("want: %v\ngot:%v\nerr: %v", tt.wantErr, err != nil, err)
}
assertParams(t, out, tt.wantOut)
})
}
}

// AssertQueryEqual tests if two query input are equal.
// TODO: improve this in the future.
func assertParams(t *testing.T, got *Params, want *Params) {
Expand All @@ -947,9 +1044,11 @@ func assertParams(t *testing.T, got *Params, want *Params) {
if got.Select != want.Select {
t.Fatalf("select: got: %q want %q", got.Select, want.Select)
}
if !equalExp(got.FilterExp, want.FilterExp) || !equalExp(want.FilterExp, got.FilterExp) {

if !equalExp(got.FilterExp, want.FilterExp, got.ParamSymbol, got.PositionalParams) || !equalExp(want.FilterExp, got.FilterExp, want.ParamSymbol, want.PositionalParams) {
t.Fatalf("filter expr:\n\tgot: %q\n\twant %q", got.FilterExp, want.FilterExp)
}

if !equalArgs(got.FilterArgs, got.FilterArgs) || !equalArgs(want.FilterArgs, got.FilterArgs) {
t.Fatalf("filter args:\n\tgot: %v\n\twant %v", got.FilterArgs, want.FilterArgs)
}
Expand Down Expand Up @@ -977,14 +1076,17 @@ func equalArgs(a, b []interface{}) bool {
return true
}

func equalExp(e1, e2 string) bool {
s1, s2 := split(e1), split(e2)
func equalExp(e1, e2 string, pexp string, pos bool) bool {
if pexp == "" {
pexp = "?"
}
s1, s2 := split(e1, pexp, pos), split(e2, pexp, pos)
for i := range s1 {
var found bool
for j := range s2 {
// if it is a start of conjunction.
if s1[i][0] == '(' && s2[j][0] == '(' {
found = equalExp(s1[i][1:len(s1[i])-1], s2[j][1:len(s2[j])-1])
found = equalExp(s1[i][1:len(s1[i])-1], s2[j][1:len(s2[j])-1], pexp, pos)
} else {
found = s1[i] == s2[j]
}
Expand All @@ -999,15 +1101,26 @@ func equalExp(e1, e2 string) bool {
return true
}

func split(e string) []string {
func split(e string, pexp string, pos bool) []string {
var s []string
for len(e) > 0 {
if e[0] == '(' {
end := strings.LastIndexByte(e, ')') + 1
s = append(s, e[:end])
e = e[end:]
} else {
end := strings.IndexByte(e, '?') + 1
end := strings.IndexByte(e, pexp[0]) + 1
if pos {
for {
if end >= len(e) {
break
} else if unicode.IsDigit(rune(e[end])) {
end++
} else {
break
}
}
}
s = append(s, e[:end])
e = e[end:]
}
Expand Down