You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is it a specification that *time.Time is not supported as a parameter?
I think that Gorp should be able to work with only one *time.Time as well as with more than one parameter.
Here is a reproduction test code to get error.
package main
import (
"database/sql""log""time""github.com/go-gorp/gorp/v3"
_ "github.com/mattn/go-sqlite3"
)
funcmain() {
// initialize the DbMapdbmap:=initDb()
deferdbmap.Db.Close()
// delete any existing rowserr:=dbmap.TruncateTables()
checkErr(err, "TruncateTables failed")
// create two postsp1:=newPost("Go 1.1 released!", "Lorem ipsum lorem ipsum")
p2:=newPost("Go 1.2 released!", "Lorem ipsum lorem ipsum")
// insert rows - auto increment PKs will be set properly after the inserterr=dbmap.Insert(&p1, &p2)
checkErr(err, "Insert failed")
from:=time.Now().Add(-10*time.Second)
to:=time.Now().Add(10*time.Second)
// fetch all rowsvarposts []Post// pass two parameters (*time.Time and *time.Time) -> ok_, err=dbmap.Select(&posts, "select * from posts where created_at >= ? and created_at < ?", &from, &to)
checkErr(err, "Select failed")
log.Println("Rows filtered by two *time.Time parameters:", len(posts))
// pass only one parameter that is not pointer (time.Time) -> okposts=nil_, err=dbmap.Select(&posts, "select * from posts where created_at > ?", from)
checkErr(err, "select failed")
log.Println("Rows filtered by only one time.Time parameter:", len(posts))
// pass only one parameter (*time.Time) -> failedposts=nil_, err=dbmap.Select(&posts, "select * from posts where created_at > ?", &from)
checkErr(err, "select failed")
log.Println("Rows filtered by only one *time.Time parameter:", len(posts))
log.Println("Done!")
}
typePoststruct {
// db tag lets you specify the column name if it differs from the struct fieldIdint64`db:"post_id"`CreatedAt time.Time`db:"created_at"`Titlestring`db:",size:50"`// Column size set to 50Bodystring`db:"article_body,size:1024"`// Set both column name and size
}
funcnewPost(title, bodystring) Post {
returnPost{
CreatedAt: time.Now(),
Title: title,
Body: body,
}
}
funcinitDb() *gorp.DbMap {
// connect to db using standard Go database/sql API// use whatever database/sql driver you wishdb, err:=sql.Open("sqlite3", "/tmp/post_db.bin")
checkErr(err, "sql.Open failed")
// construct a gorp DbMapdbmap:=&gorp.DbMap{Db: db, Dialect: gorp.SqliteDialect{}}
// add a table, setting the table name to 'posts' and// specifying that the Id property is an auto incrementing PKdbmap.AddTableWithName(Post{}, "posts").SetKeys(true, "Id")
// create the table. in a production system you'd generally// use a migration tool, or create the tables via scriptserr=dbmap.CreateTablesIfNotExists()
checkErr(err, "Create tables failed")
returndbmap
}
funccheckErr(errerror, msgstring) {
iferr!=nil {
log.Fatalln(msg, err)
}
}
The text was updated successfully, but these errors were encountered:
When only one
*time.Time
is passed toDbMap.Select
, it returns an error.not enough args to execute query: want 1 got 0
If the number of parameters is 2 or more, passing
*time.Time
works fine.And only one
time.Time
(not pointer) works.It may be caused by
maybeExpandNamedQuery
does not support pointer of time.Time.gorp/gorp.go
Line 207 in 80a2f4b
Is it a specification that
*time.Time
is not supported as a parameter?I think that Gorp should be able to work with only one
*time.Time
as well as with more than one parameter.Here is a reproduction test code to get error.
The text was updated successfully, but these errors were encountered: