Skip to content

Commit

Permalink
Merge pull request #17 from future-architect/feature/comment-support
Browse files Browse the repository at this point in the history
Add sql comment support
  • Loading branch information
ma91n authored Jun 3, 2022
2 parents 946b063 + 325178d commit b2fb5f2
Show file tree
Hide file tree
Showing 7 changed files with 827 additions and 34 deletions.
15 changes: 14 additions & 1 deletion e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,20 @@ func TestSelect(t *testing.T) {
},
}

err := tw.Select(ctx, &people, `SELECT first_name, last_name, email FROM persons WHERE employee_no < /*maxEmpNo*/1000 /* IF deptNo */ AND dept_no < /*deptNo*/1 /* END */`, &params)
sql := `-- comment
SELECT
first_name
, last_name
, email
FROM
persons
WHERE
employee_no < /*maxEmpNo*/1000 -- comment
/* IF deptNo */
AND dept_no < /*deptNo*/1
/* END */
-- comment`
err := tw.Select(ctx, &people, sql, &params)
if err != nil {
t.Errorf("select: failed: %v", err)
}
Expand Down
11 changes: 1 addition & 10 deletions eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ import (
func Eval(inputQuery string, inputParams interface{}) (string, []interface{}, error) {
mapParams := map[string]interface{}{}

query := formatQuery(inputQuery)

if inputParams != nil {
if err := encode(mapParams, inputParams); err != nil {
return "", nil, err
Expand All @@ -26,7 +24,7 @@ func Eval(inputQuery string, inputParams interface{}) (string, []interface{}, er
mapParams = nil
}

tokens, err := tokenize(query)
tokens, err := tokenize(inputQuery)
if err != nil {
return "", nil, err
}
Expand Down Expand Up @@ -127,13 +125,6 @@ func bindTable(str string, rowNumber, columnNumber int) string {
return fmt.Sprint(row.String(), str)
}

func formatQuery(query string) string {
query = strings.ReplaceAll(query, "\n", " ")
query = strings.ReplaceAll(query, "\t", " ")
query = strings.TrimSpace(query)
return query
}

// 空白が二つ以上続いていたら一つにする。=1 -> = 1のような変換はできない
// 単純な空白を想定。 -> issue: よりロバストな実装
func arrangeWhiteSpace(str string) string {
Expand Down
212 changes: 190 additions & 22 deletions eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package twowaysql

import (
"testing"

"gotest.tools/v3/assert"
"gotest.tools/v3/assert/cmp"
)

type Info struct {
Expand Down Expand Up @@ -235,8 +238,18 @@ func TestEval(t *testing.T) {
/* END */
`,
inputParams: Info{},
wantQuery: `SELECT * FROM person WHERE employee_no < 1000 AND dept_no = 1`,
wantParams: []interface{}{},
wantQuery: `
SELECT
*
FROM
person
WHERE
employee_no < 1000
AND dept_no = 1
`,
wantParams: []interface{}{},
},
{
name: "multiline if false elif false else",
Expand Down Expand Up @@ -270,7 +283,17 @@ func TestEval(t *testing.T) {
GenderList: []string{"M", "F"},
IntList: []int{1, 2, 3},
},
wantQuery: `SELECT * FROM person WHERE employee_no < 1000 AND id = ?/*maxEmpNo*/`,
wantQuery: `
SELECT
*
FROM
person
WHERE
employee_no < 1000
AND id = ?/*maxEmpNo*/
`,
wantParams: []interface{}{3},
},
{
Expand Down Expand Up @@ -311,7 +334,19 @@ func TestEval(t *testing.T) {
GenderList: []string{"M", "F"},
IntList: []int{1, 2, 3},
},
wantQuery: `SELECT * FROM person WHERE employee_no < 1000 AND id = 3`,
wantQuery: `
SELECT
*
FROM
person
WHERE
employee_no < 1000
AND id = 3
`,
wantParams: []interface{}{},
},
{
Expand All @@ -338,7 +373,17 @@ func TestEval(t *testing.T) {
GenderList: []string{"M", "F"},
IntList: []int{1, 2, 3},
},
wantQuery: `SELECT * FROM person WHERE employee_no = ?/*maxEmpNo*/ AND person.gender in (?, ?, ?)/*int_list*/`,
wantQuery: `
SELECT
*
FROM
person
WHERE
employee_no = ?/*maxEmpNo*/
AND person.gender in (?, ?, ?)/*int_list*/
`,
wantParams: []interface{}{
3,
1,
Expand Down Expand Up @@ -385,7 +430,34 @@ func TestEval(t *testing.T) {
Email: "email",
GenderList: []string{"f", "m", "o"},
},
wantQuery: `SELECT * FROM person WHERE 1=1 AND employee_no < ?/*EmpNo*/ AND id = ?/*maxEmpNo*/ AND dept_no = ?/*deptNo*/ AND first_name = ?/*firstName*/ AND last_name = ?/*lastName*/ AND email = ?/*email*/ AND gender_list IN (?, ?, ?)/*gender_list*/`,
wantQuery: `
SELECT
*
FROM
person
WHERE 1=1
AND employee_no < ?/*EmpNo*/
AND id = ?/*maxEmpNo*/
AND dept_no = ?/*deptNo*/
AND first_name = ?/*firstName*/
AND last_name = ?/*lastName*/
AND email = ?/*email*/
AND gender_list IN (?, ?, ?)/*gender_list*/
`,
wantParams: []interface{}{1000, 10, 1, "first", "last", "email", "f", "m", "o"},
},
{
Expand All @@ -407,7 +479,17 @@ func TestEval(t *testing.T) {
EmpNo: 1000,
MaxEmpNo: 10,
},
wantQuery: `SELECT * FROM person WHERE 1=1 AND id = ?/*maxEmpNo*/`,
wantQuery: `
SELECT
*
FROM
person
WHERE 1=1
AND id = ?/*maxEmpNo*/
`,
wantParams: []interface{}{10},
},
{
Expand Down Expand Up @@ -440,7 +522,21 @@ func TestEval(t *testing.T) {
EmpNo: 1000,
MaxEmpNo: 10,
},
wantQuery: `SELECT * FROM person WHERE 1=1 AND employee_no < ?/*EmpNo*/ AND id = ?/*maxEmpNo*/`,
wantQuery: `
SELECT
*
FROM
person
WHERE 1=1
AND employee_no < ?/*EmpNo*/
AND id = ?/*maxEmpNo*/
`,
wantParams: []interface{}{1000, 10},
},
{
Expand Down Expand Up @@ -473,7 +569,21 @@ func TestEval(t *testing.T) {
EmpNo: 1000,
MaxEmpNo: 10,
},
wantQuery: `SELECT * FROM person WHERE 1=1 AND employee_no < ?/*EmpNo*/ AND id = ?/*maxEmpNo*/`,
wantQuery: `
SELECT
*
FROM
person
WHERE 1=1
AND employee_no < ?/*EmpNo*/
AND id = ?/*maxEmpNo*/
`,
wantParams: []interface{}{1000, 10},
},
{
Expand Down Expand Up @@ -506,7 +616,21 @@ func TestEval(t *testing.T) {
EmpNo: 1000,
MaxEmpNo: 10,
},
wantQuery: `SELECT * FROM person WHERE 1=1 AND employee_no < ?/*EmpNo*/ AND id = ?/*maxEmpNo*/`,
wantQuery: `
SELECT
*
FROM
person
WHERE 1=1
AND employee_no < ?/*EmpNo*/
AND id = ?/*maxEmpNo*/
`,
wantParams: []interface{}{1000, 10},
},
{
Expand Down Expand Up @@ -543,24 +667,68 @@ func TestEval(t *testing.T) {
EmpNo: 1000,
MaxEmpNo: 10,
},
wantQuery: `SELECT * FROM person WHERE 1=1 AND employee_no < 222 AND id = ?/*maxEmpNo*/`,
wantQuery: `
SELECT
*
FROM
person
WHERE 1=1
AND employee_no < 222
AND id = ?/*maxEmpNo*/
`,
wantParams: []interface{}{10},
},
{
name: "comment",
input: `
-- header comment
SELECT -- inner comment
* -- inner comment
FROM -- inner comment
person -- inner comment
WHERE -- inner comment
employee_no < 1000 -- inner comment
/* IF true */ -- inner comment
AND dept_no = 1 -- inner comment
/* END */ -- inner comment
-- footer comment
`,
inputParams: Info{},
wantQuery: `
-- header comment
SELECT -- inner comment
* -- inner comment
FROM -- inner comment
person -- inner comment
WHERE -- inner comment
employee_no < 1000 -- inner comment
-- inner comment
AND dept_no = 1 -- inner comment
-- inner comment
-- footer comment
`,
wantParams: []interface{}{},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if query, params, err := Eval(tt.input, &tt.inputParams); err != nil || query != tt.wantQuery || !interfaceSliceEqual(params, tt.wantParams) {
if err != nil {
t.Error(err)
}
if query != tt.wantQuery {
t.Errorf("Doesn't Match\nexpected: \n%s\n but got: \n%s\n", tt.wantQuery, query)
}
if !interfaceSliceEqual(params, tt.wantParams) {
t.Errorf("Doesn't Match\nexpected: \n%v\n but got: \n%v\n", tt.wantParams, params)
}
}
query, params, err := Eval(tt.input, &tt.inputParams)
assert.NilError(t, err)
assert.Check(t, cmp.DeepEqual(tt.wantParams, params))
assert.Check(t, cmp.DeepEqual(tt.wantQuery, query))
})
}
}
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ module github.com/future-architect/go-twowaysql
go 1.16

require (
github.com/google/go-cmp v0.5.5
github.com/jmoiron/sqlx v1.3.1
github.com/lib/pq v1.9.0
github.com/robertkrimen/otto v0.0.0-20200922221731-ef014fd054ac
gitlab.com/osaki-lab/tagscanner v0.1.2
gopkg.in/sourcemap.v1 v1.0.5 // indirect
gotest.tools/v3 v3.2.0
)
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
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/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/jmoiron/sqlx v1.3.1 h1:aLN7YINNZ7cYOPK3QC83dbM6KT0NMqVMw961TqrejlE=
github.com/jmoiron/sqlx v1.3.1/go.mod h1:2BljVx/86SuTyjE+aPYlHCTNvZrnJXghYGpNiXLBMCQ=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
Expand All @@ -13,6 +15,7 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/robertkrimen/otto v0.0.0-20200922221731-ef014fd054ac h1:kYPjbEN6YPYWWHI6ky1J813KzIq/8+Wg4TO4xU7A/KU=
github.com/robertkrimen/otto v0.0.0-20200922221731-ef014fd054ac/go.mod h1:xvqspoSXJTIpemEonrMDFq6XzwHYYgToXWj5eRX1OtY=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand All @@ -39,9 +42,13 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/sourcemap.v1 v1.0.5 h1:inv58fC9f9J3TK2Y2R1NPntXEn3/wjWHkonhIUODNTI=
gopkg.in/sourcemap.v1 v1.0.5/go.mod h1:2RlvNNSMglmRrcvhfuzp4hQHwOtjxlbjX7UPY/GXb78=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools/v3 v3.2.0 h1:I0DwBVMGAx26dttAj1BtJLAkVGncrkkUXfJLC4Flt/I=
gotest.tools/v3 v3.2.0/go.mod h1:Mcr9QNxkg0uMvy/YElmo4SpXgJKWgQvYrT7Kw5RzJ1A=
Loading

0 comments on commit b2fb5f2

Please sign in to comment.