Skip to content

Commit

Permalink
Remove QueryArgs
Browse files Browse the repository at this point in the history
  • Loading branch information
pinzolo committed May 29, 2018
1 parent d8e7b63 commit 70c35b8
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 172 deletions.
26 changes: 0 additions & 26 deletions sqlt.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,32 +52,6 @@ func (st SQLTemplate) ExecNamed(text string, m map[string]interface{}) (string,
return s, c.namedArgs, nil
}

// QueryArgs builds arguments for sql.
func QueryArgs(s string, vals []interface{}) []interface{} {
if len(vals) == 0 {
return []interface{}{s}
}
args := make([]interface{}, len(vals)+1)
args[0] = s
for i, v := range vals {
args[i+1] = v
}
return args
}

// QueryArgsNamed builds arguments for sql with named arguments.
func QueryArgsNamed(s string, vals []sql.NamedArg) []interface{} {
if len(vals) == 0 {
return []interface{}{s}
}
args := make([]interface{}, len(vals)+1)
args[0] = s
for i, v := range vals {
args[i+1] = v
}
return args
}

func (st SQLTemplate) exec(c *context, text string) (string, error) {
t, err := template.New("").Funcs(c.funcMap()).Delims(LeftDelim, RightDelim).Parse(dropSample(text))
if err != nil {
Expand Down
146 changes: 0 additions & 146 deletions sqlt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,152 +293,6 @@ func TestWithInvalidParamNameOnInFunc(t *testing.T) {
}
}

func TestQueryArgs(t *testing.T) {
s := `SELECT *
FROM users
WHERE id IN /*% in "ids" %*/(1, 2)
AND name = /*% p "name" %*/'John Doe'
/*%- if .onlyMale %*/
AND sex = 'MALE'
/*%- end%*/
ORDER BY /*% .order %*/id`
sql, vals, err := New(Postgres).Exec(s, map[string]interface{}{
"ids": []int{1, 2, 3},
"order": "name DESC",
"onlyMale": true,
"name": "Alex",
})
if err != nil {
t.Error(err)
}

args := QueryArgs(sql, vals)
if len(args) != 5 {
t.Errorf("QueryArgs failed: arguments should have 5 length, but got %d", len(args))
}
if arg, ok := args[0].(string); ok {
if arg != sql {
t.Errorf("1st argument should be sql, but got %s", arg)
}
} else {
t.Errorf("1st argument should be string.")
}
if isInvalidInt(args[1], 1) {
t.Errorf("QueryArgs failed: arguments should have 1, but got %v", args)
}
if isInvalidInt(args[2], 2) {
t.Errorf("QueryArgs failed: arguments should have 2, but got %v", args)
}
if isInvalidInt(args[3], 3) {
t.Errorf("QueryArgs failed: arguments should have 3, but got %v", args)
}
if isInvalidString(args[4], "Alex") {
t.Errorf("QueryArgs failed: arguments should have 'Alex', but got %v", args)
}
}

func TestQueryArgsNamed(t *testing.T) {
s := `SELECT *
FROM users
WHERE id IN /*% in "ids" %*/(1, 2)
AND name = /*% p "name" %*/'John Doe'
/*%- if .onlyMale %*/
AND sex = 'MALE'
/*%- end%*/
ORDER BY /*% .order %*/id`
query, vals, err := New(Postgres).ExecNamed(s, map[string]interface{}{
"ids": []int{1, 2, 3},
"order": "name DESC",
"onlyMale": false,
"name": "Alex",
})
if err != nil {
t.Error(err)
}

args := QueryArgsNamed(query, vals)
if len(args) != 5 {
t.Errorf("QueryArgsNamed failed: arguments should have 5 length, but got %d", len(args))
}
if arg, ok := args[0].(string); ok {
if arg != query {
t.Errorf("1st argument should be sql, but got %s", arg)
}
} else {
t.Errorf("1st argument should be string.")
}
if arg, ok := args[1].(sql.NamedArg); ok {
if isInvalidIntArg(arg, "ids1", 1) {
t.Errorf("QueryArgsNamed failed: arguments should have ids1 = 1, but got %v", arg)
}
} else {
t.Error("QueryArgsNamed failed: 2nd argument should be named args")
}
if arg, ok := args[2].(sql.NamedArg); ok {
if isInvalidIntArg(arg, "ids2", 2) {
t.Errorf("QueryArgsNamed failed: arguments should have ids2 = 2, but got %v", arg)
}
} else {
t.Error("QueryArgsNamed failed: 3rd argument should be named args")
}
if arg, ok := args[3].(sql.NamedArg); ok {
if isInvalidIntArg(arg, "ids3", 3) {
t.Errorf("QueryArgsNamed failed: arguments should have ids3 = 3, but got %v", arg)
}
} else {
t.Error("QueryArgsNamed failed: 4th argument should be named args")
}
if arg, ok := args[4].(sql.NamedArg); ok {
if isInvalidStringArg(arg, "name", "Alex") {
t.Errorf("QueryArgsNamed failed: arguments should have name = 'Alex', but got %v", arg)
}
} else {
t.Error("QueryArgsNamed failed: 5th argument should be named args")
}
}

func TestQueryArgsWithoutValues(t *testing.T) {
s := "SELECT * FROM users"
params := [][]interface{}{
nil,
[]interface{}{},
}
for _, p := range params {
args := QueryArgs(s, p)
if len(args) != 1 {
t.Error("QueryArgs without values should returns only sql.")
}
if arg, ok := args[0].(string); ok {
if arg != s {
t.Errorf("1st argument should be sql, but got %s", arg)
}
} else {
t.Errorf("1st argument should be string.")
}
}
}

func TestQueryArgsNamedWithoutValues(t *testing.T) {
s := "SELECT * FROM users"
params := [][]sql.NamedArg{
nil,
[]sql.NamedArg{},
}
for _, p := range params {
args := QueryArgsNamed(s, p)
if len(args) != 1 {
t.Error("QueryArgsNamed without values should returns only sql.")
}
if arg, ok := args[0].(string); ok {
if arg != s {
t.Errorf("1st argument should be sql, but got %s", arg)
}
} else {
t.Errorf("1st argument should be string.")
}
}
}

func singleMap(k string, v interface{}) map[string]interface{} {
return map[string]interface{}{
k: v,
Expand Down

0 comments on commit 70c35b8

Please sign in to comment.