Compare commits
No commits in common. "029cf6ce01c88a445063d3f253efc26c1d8e4142" and "dd6be6b9b641a736cd4c9a4528ad9c6ddb089fdf" have entirely different histories.
029cf6ce01
...
dd6be6b9b6
10
query.go
10
query.go
@ -44,14 +44,6 @@ func (q *Query) bindInto(into *int, args ...any) *Query {
|
|||||||
}
|
}
|
||||||
for i, arg := range args {
|
for i, arg := range args {
|
||||||
*into++
|
*into++
|
||||||
if arg == nil {
|
|
||||||
q.stmt.BindNull(*into)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
v := reflect.ValueOf(arg)
|
|
||||||
if v.Kind() == reflect.Ptr {
|
|
||||||
arg = v.Elem().Interface()
|
|
||||||
}
|
|
||||||
if asString, ok := arg.(string); ok {
|
if asString, ok := arg.(string); ok {
|
||||||
q.stmt.BindText(*into, asString)
|
q.stmt.BindText(*into, asString)
|
||||||
} else if asInt, ok := arg.(int); ok {
|
} else if asInt, ok := arg.(int); ok {
|
||||||
@ -62,7 +54,7 @@ func (q *Query) bindInto(into *int, args ...any) *Query {
|
|||||||
q.stmt.BindBool(*into, asBool)
|
q.stmt.BindBool(*into, asBool)
|
||||||
} else {
|
} else {
|
||||||
// Check if the argument is a slice or array of any type
|
// Check if the argument is a slice or array of any type
|
||||||
v = reflect.ValueOf(arg)
|
v := reflect.ValueOf(arg)
|
||||||
if v.Kind() == reflect.Slice || v.Kind() == reflect.Array {
|
if v.Kind() == reflect.Slice || v.Kind() == reflect.Array {
|
||||||
*into--
|
*into--
|
||||||
for i := 0; i < v.Len(); i++ {
|
for i := 0; i < v.Len(); i++ {
|
||||||
|
@ -94,50 +94,12 @@ func TestUpdateQuery(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestUpdateQueryWithWrongArguments(t *testing.T) {
|
func TestUpdateQueryWithWrongArguments(t *testing.T) {
|
||||||
type S struct {
|
|
||||||
Field string
|
|
||||||
}
|
|
||||||
db := openTestDb(t)
|
db := openTestDb(t)
|
||||||
abc := S{
|
value := "ipsum"
|
||||||
Field: "ipsum",
|
err := db.Query("insert into mytable(key, value) values ('lorem', ?)").Bind(&value).Exec()
|
||||||
}
|
|
||||||
err := db.Query("insert into mytable(key, value) values ('lorem', ?)").Bind(abc).Exec()
|
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUpdateQueryWithPointerValue(t *testing.T) {
|
|
||||||
db := openTestDb(t)
|
|
||||||
func() {
|
|
||||||
tx := db.MustBegin()
|
|
||||||
defer tx.MustRollback()
|
|
||||||
tx.Query("insert into mytable(key, value) values ('lorem', 'bar')").MustExec()
|
|
||||||
value := "ipsum"
|
|
||||||
key := "lorem"
|
|
||||||
tx.Query("update mytable set value = ? where key = ?").Bind(&value, key).MustExec()
|
|
||||||
tx.MustCommit()
|
|
||||||
}()
|
|
||||||
|
|
||||||
var value string
|
|
||||||
db.Query("select value from mytable where key = 'lorem'").MustScanSingle(&value)
|
|
||||||
require.Equal(t, "ipsum", value)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestUpdateQueryWithNullValue(t *testing.T) {
|
|
||||||
db := openTestDb(t)
|
|
||||||
func() {
|
|
||||||
tx := db.MustBegin()
|
|
||||||
defer tx.MustRollback()
|
|
||||||
tx.Query("insert into mytable(key, value) values ('lorem', 'bar')").MustExec()
|
|
||||||
key := "lorem"
|
|
||||||
tx.Query("update mytable set value = ? where key = ?").Bind(nil, key).MustExec()
|
|
||||||
tx.MustCommit()
|
|
||||||
}()
|
|
||||||
|
|
||||||
var value *string
|
|
||||||
db.Query("select value from mytable where key = 'lorem'").MustScanSingle(&value)
|
|
||||||
require.Nil(t, value)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestQueryWithPointerStringArguments(t *testing.T) {
|
func TestQueryWithPointerStringArguments(t *testing.T) {
|
||||||
db := openTestDb(t)
|
db := openTestDb(t)
|
||||||
var result *string
|
var result *string
|
||||||
|
Loading…
x
Reference in New Issue
Block a user