Rolling back gorp to earlier version (#3056)

Этот коммит содержится в:
Christopher Speller
2016-05-19 12:55:35 -04:00
коммит произвёл enahum
родитель 98fa24f216
Коммит 04175d36eb
9 изменённых файлов: 160 добавлений и 422 удалений

2
glide.lock сгенерированный
Просмотреть файл

@@ -17,7 +17,7 @@ imports:
- redis - redis
- internal - internal
- name: github.com/go-gorp/gorp - name: github.com/go-gorp/gorp
version: 6a3c8a87d0457cf700e57046c41e19b7cf3c44fa version: 0c9bc0918534d133cedb439a24adc7cbe66e4a9d
- name: github.com/go-ldap/ldap - name: github.com/go-ldap/ldap
version: 0e7db8eb77695b5a952f0e5d78df9ab160050c73 version: 0e7db8eb77695b5a952f0e5d78df9ab160050c73
- name: github.com/go-sql-driver/mysql - name: github.com/go-sql-driver/mysql

Просмотреть файл

@@ -7,6 +7,7 @@ import:
- package: github.com/dgryski/dgoogauth - package: github.com/dgryski/dgoogauth
- package: github.com/disintegration/imaging - package: github.com/disintegration/imaging
- package: github.com/go-gorp/gorp - package: github.com/go-gorp/gorp
version: 0c9bc0918534d133cedb439a24adc7cbe66e4a9d
- package: github.com/go-ldap/ldap - package: github.com/go-ldap/ldap
- package: github.com/go-sql-driver/mysql - package: github.com/go-sql-driver/mysql
- package: github.com/goamz/goamz - package: github.com/goamz/goamz

1
vendor/github.com/go-gorp/gorp/.gitignore сгенерированный поставляемый
Просмотреть файл

@@ -1,5 +1,4 @@
_test _test
*.test
_testmain.go _testmain.go
_obj _obj
*~ *~

1
vendor/github.com/go-gorp/gorp/.travis.yml сгенерированный поставляемый
Просмотреть файл

@@ -24,6 +24,5 @@ before_script:
- go get github.com/go-sql-driver/mysql - go get github.com/go-sql-driver/mysql
- go get golang.org/x/tools/cmd/cover - go get golang.org/x/tools/cmd/cover
- go get github.com/mattn/goveralls - go get github.com/mattn/goveralls
- go get github.com/onsi/ginkgo/ginkgo
script: ./test_all.sh script: ./test_all.sh

204
vendor/github.com/go-gorp/gorp/dialect_mysql_test.go сгенерированный поставляемый
Просмотреть файл

@@ -1,204 +0,0 @@
// Copyright 2012 James Cooper. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
// Package gorp provides a simple way to marshal Go structs to and from
// SQL databases. It uses the database/sql package, and should work with any
// compliant database/sql driver.
//
// Source code and project home:
// https://github.com/go-gorp/gorp
package gorp_test
import (
"database/sql"
"reflect"
"time"
// ginkgo/gomega functions read better as dot-imports.
. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/gomega"
"github.com/go-gorp/gorp"
)
var _ = Describe("MySQLDialect", func() {
var (
engine, encoding string
dialect gorp.MySQLDialect
)
JustBeforeEach(func() {
dialect = gorp.MySQLDialect{
Engine: engine,
Encoding: encoding,
}
})
DescribeTable("ToSqlType",
func(value interface{}, maxsize int, autoIncr bool, expected string) {
typ := reflect.TypeOf(value)
sqlType := dialect.ToSqlType(typ, maxsize, autoIncr)
Expect(sqlType).To(Equal(expected))
},
Entry("bool", true, 0, false, "boolean"),
Entry("int8", int8(1), 0, false, "tinyint"),
Entry("uint8", uint8(1), 0, false, "tinyint unsigned"),
Entry("int16", int16(1), 0, false, "smallint"),
Entry("uint16", uint16(1), 0, false, "smallint unsigned"),
Entry("int32", int32(1), 0, false, "int"),
Entry("int (treated as int32)", int(1), 0, false, "int"),
Entry("uint32", uint32(1), 0, false, "int unsigned"),
Entry("uint (treated as uint32)", uint(1), 0, false, "int unsigned"),
Entry("int64", int64(1), 0, false, "bigint"),
Entry("uint64", uint64(1), 0, false, "bigint unsigned"),
Entry("float32", float32(1), 0, false, "double"),
Entry("float64", float64(1), 0, false, "double"),
Entry("[]uint8", []uint8{1}, 0, false, "mediumblob"),
Entry("NullInt64", sql.NullInt64{}, 0, false, "bigint"),
Entry("NullFloat64", sql.NullFloat64{}, 0, false, "double"),
Entry("NullBool", sql.NullBool{}, 0, false, "tinyint"),
Entry("Time", time.Time{}, 0, false, "datetime"),
Entry("default-size string", "", 0, false, "varchar(255)"),
Entry("sized string", "", 50, false, "varchar(50)"),
Entry("large string", "", 1024, false, "text"),
)
Describe("AutoIncrStr", func() {
It("returns the auto increment string", func() {
Expect(dialect.AutoIncrStr()).To(Equal("auto_increment"))
})
})
Describe("AutoIncrBindValue", func() {
It("returns the value used to bind the auto-increment value", func() {
Expect(dialect.AutoIncrBindValue()).To(Equal("null"))
})
})
Describe("AutoIncrInsertSuffix", func() {
It("returns the suffix needed for auto-incrementing", func() {
Expect(dialect.AutoIncrInsertSuffix(nil)).To(BeEmpty())
})
})
Describe("CreateTableSuffix", func() {
Context("with an empty engine", func() {
BeforeEach(func() {
engine = ""
encoding = "foo"
})
It("panics", func() {
Expect(func() {
dialect.CreateTableSuffix()
}).To(Panic())
})
})
Context("with an empty encoding", func() {
BeforeEach(func() {
engine = "foo"
encoding = ""
})
It("panics", func() {
Expect(func() {
dialect.CreateTableSuffix()
}).To(Panic())
})
})
Context("with an engine and an encoding", func() {
BeforeEach(func() {
engine = "foo"
encoding = "bar"
})
It("returns a valid suffix", func() {
Expect(dialect.CreateTableSuffix()).To(Equal(" engine=foo charset=bar"))
})
})
})
Describe("CreateIndexSuffix", func() {
It("returns the suffix for creating indexes", func() {
Expect(dialect.CreateIndexSuffix()).To(Equal("using"))
})
})
Describe("DropIndexSuffix", func() {
It("returns the suffix for deleting indexes", func() {
Expect(dialect.DropIndexSuffix()).To(Equal("on"))
})
})
Describe("TruncateClause", func() {
It("returns the clause for truncating a table", func() {
Expect(dialect.TruncateClause()).To(Equal("truncate"))
})
})
Describe("BindVar", func() {
It("returns the variable binding sequence", func() {
Expect(dialect.BindVar(0)).To(Equal("?"))
})
})
PDescribe("InsertAutoIncr", func() {})
Describe("QuoteField", func() {
It("returns the argument quoted as a field", func() {
Expect(dialect.QuoteField("foo")).To(Equal("`foo`"))
})
})
Describe("QuotedTableForQuery", func() {
var (
schema, table string
quotedTable string
)
JustBeforeEach(func() {
quotedTable = dialect.QuotedTableForQuery(schema, table)
})
Context("using the default schema", func() {
BeforeEach(func() {
schema = ""
table = "foo"
})
It("returns just the table", func() {
Expect(quotedTable).To(Equal("`foo`"))
})
})
Context("with a supplied schema", func() {
BeforeEach(func() {
schema = "foo"
table = "bar"
})
It("returns the schema and table", func() {
Expect(quotedTable).To(Equal("foo.`bar`"))
})
})
})
Describe("IfSchemaNotExists", func() {
It("appends 'if not exists' to the command", func() {
Expect(dialect.IfSchemaNotExists("foo", "bar")).To(Equal("foo if not exists"))
})
})
Describe("IfTableExists", func() {
It("appends 'if exists' to the command", func() {
Expect(dialect.IfTableExists("foo", "bar", "baz")).To(Equal("foo if exists"))
})
})
Describe("IfTableNotExists", func() {
It("appends 'if not exists' to the command", func() {
Expect(dialect.IfTableNotExists("foo", "bar", "baz")).To(Equal("foo if not exists"))
})
})
})

4
vendor/github.com/go-gorp/gorp/dialect_postgres.go сгенерированный поставляемый
Просмотреть файл

@@ -78,7 +78,7 @@ func (d PostgresDialect) AutoIncrBindValue() string {
} }
func (d PostgresDialect) AutoIncrInsertSuffix(col *ColumnMap) string { func (d PostgresDialect) AutoIncrInsertSuffix(col *ColumnMap) string {
return " returning " + d.QuoteField(col.ColumnName) return " returning " + col.ColumnName
} }
// Returns suffix // Returns suffix
@@ -123,7 +123,7 @@ func (d PostgresDialect) InsertAutoIncrToTarget(exec SqlExecutor, insertSql stri
} }
func (d PostgresDialect) QuoteField(f string) string { func (d PostgresDialect) QuoteField(f string) string {
return `"` + f + `"` return `"` + strings.ToLower(f) + `"`
} }
func (d PostgresDialect) QuotedTableForQuery(schema string, table string) string { func (d PostgresDialect) QuotedTableForQuery(schema string, table string) string {

13
vendor/github.com/go-gorp/gorp/gorp_suite_test.go сгенерированный поставляемый
Просмотреть файл

@@ -1,13 +0,0 @@
package gorp_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"testing"
)
func TestGorp(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Gorp Suite")
}

353
vendor/github.com/go-gorp/gorp/gorp_test.go сгенерированный поставляемый
Просмотреть файл

@@ -9,7 +9,7 @@
// Source code and project home: // Source code and project home:
// https://github.com/go-gorp/gorp // https://github.com/go-gorp/gorp
package gorp_test package gorp
import ( import (
"bytes" "bytes"
@@ -28,8 +28,6 @@ import (
"testing" "testing"
"time" "time"
"github.com/go-gorp/gorp"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq" _ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
@@ -38,12 +36,12 @@ import (
var ( var (
// verify interface compliance // verify interface compliance
_ = []gorp.Dialect{ _ = []Dialect{
gorp.SqliteDialect{}, SqliteDialect{},
gorp.PostgresDialect{}, PostgresDialect{},
gorp.MySQLDialect{}, MySQLDialect{},
gorp.SqlServerDialect{}, SqlServerDialect{},
gorp.OracleDialect{}, OracleDialect{},
} }
debug bool debug bool
@@ -126,36 +124,26 @@ type Person struct {
Version int64 Version int64
} }
// PersonValuerScanner is used as a field in test types to ensure that we
// make use of "database/sql/driver".Valuer for choosing column types when
// creating tables and that we don't get in the way of the underlying
// database libraries when they make use of either Valuer or
// "database/sql".Scanner.
type PersonValuerScanner struct { type PersonValuerScanner struct {
Person Person
} }
// Value implements "database/sql/driver".Valuer. It will be automatically
// run by the "database/sql" package when inserting/updating data.
func (p PersonValuerScanner) Value() (driver.Value, error) { func (p PersonValuerScanner) Value() (driver.Value, error) {
return p.Id, nil return p.Id, nil
} }
// Scan implements "database/sql".Scanner. It will be automatically run // FIXME: this Scan is never actually used in the tests?
// by the "database/sql" package when reading column data into a field // Also: if the comments below on the mysql driver are true, then that should be fixed by the dialect when scanning values into structs.
// of type PersonValuerScanner.
func (p *PersonValuerScanner) Scan(value interface{}) (err error) { func (p *PersonValuerScanner) Scan(value interface{}) (err error) {
switch src := value.(type) { switch src := value.(type) {
case []byte: case []byte:
// TODO: this case is here for mysql only. For some reason, // The mysql driver seems to return a []byte, even though the
// one (both?) of the mysql libraries opt to pass us a []byte // type in the database is bigint. Note that this case is
// instead of an int64 for the bigint column. We should add // *only* used by the mysql driver.
// table tests around valuers/scanners and try to solve these
// types of odd discrepencies to make it easier for users of
// gorp to migrate to other database engines.
p.Id, err = strconv.ParseInt(string(src), 10, 64) p.Id, err = strconv.ParseInt(string(src), 10, 64)
case int64: case int64:
// Most libraries pass in the type we'd expect. // postgres, gomysql, and sqlite drivers all return an int64,
// as you'd expect.
p.Id = src p.Id = src
default: default:
typ := reflect.TypeOf(value) typ := reflect.TypeOf(value)
@@ -291,7 +279,7 @@ type WithCustomDate struct {
type WithNullTime struct { type WithNullTime struct {
Id int64 Id int64
Time gorp.NullTime Time NullTime
} }
type testTypeConverter struct{} type testTypeConverter struct{}
@@ -314,7 +302,7 @@ func (me testTypeConverter) ToDb(val interface{}) (interface{}, error) {
return val, nil return val, nil
} }
func (me testTypeConverter) FromDb(target interface{}) (gorp.CustomScanner, bool) { func (me testTypeConverter) FromDb(target interface{}) (CustomScanner, bool) {
switch target.(type) { switch target.(type) {
case *Person: case *Person:
binder := func(holder, target interface{}) error { binder := func(holder, target interface{}) error {
@@ -325,7 +313,7 @@ func (me testTypeConverter) FromDb(target interface{}) (gorp.CustomScanner, bool
b := []byte(*s) b := []byte(*s)
return json.Unmarshal(b, target) return json.Unmarshal(b, target)
} }
return gorp.CustomScanner{new(string), target, binder}, true return CustomScanner{new(string), target, binder}, true
case *CustomStringType: case *CustomStringType:
binder := func(holder, target interface{}) error { binder := func(holder, target interface{}) error {
s, ok := holder.(*string) s, ok := holder.(*string)
@@ -339,7 +327,7 @@ func (me testTypeConverter) FromDb(target interface{}) (gorp.CustomScanner, bool
*st = CustomStringType(*s) *st = CustomStringType(*s)
return nil return nil
} }
return gorp.CustomScanner{new(string), target, binder}, true return CustomScanner{new(string), target, binder}, true
case *CustomDate: case *CustomDate:
binder := func(holder, target interface{}) error { binder := func(holder, target interface{}) error {
t, ok := holder.(*time.Time) t, ok := holder.(*time.Time)
@@ -353,13 +341,13 @@ func (me testTypeConverter) FromDb(target interface{}) (gorp.CustomScanner, bool
dateTarget.Time = *t dateTarget.Time = *t
return nil return nil
} }
return gorp.CustomScanner{new(time.Time), target, binder}, true return CustomScanner{new(time.Time), target, binder}, true
} }
return gorp.CustomScanner{}, false return CustomScanner{}, false
} }
func (p *Person) PreInsert(s gorp.SqlExecutor) error { func (p *Person) PreInsert(s SqlExecutor) error {
p.Created = time.Now().UnixNano() p.Created = time.Now().UnixNano()
p.Updated = p.Created p.Updated = p.Created
if p.FName == "badname" { if p.FName == "badname" {
@@ -368,32 +356,32 @@ func (p *Person) PreInsert(s gorp.SqlExecutor) error {
return nil return nil
} }
func (p *Person) PostInsert(s gorp.SqlExecutor) error { func (p *Person) PostInsert(s SqlExecutor) error {
p.LName = "postinsert" p.LName = "postinsert"
return nil return nil
} }
func (p *Person) PreUpdate(s gorp.SqlExecutor) error { func (p *Person) PreUpdate(s SqlExecutor) error {
p.FName = "preupdate" p.FName = "preupdate"
return nil return nil
} }
func (p *Person) PostUpdate(s gorp.SqlExecutor) error { func (p *Person) PostUpdate(s SqlExecutor) error {
p.LName = "postupdate" p.LName = "postupdate"
return nil return nil
} }
func (p *Person) PreDelete(s gorp.SqlExecutor) error { func (p *Person) PreDelete(s SqlExecutor) error {
p.FName = "predelete" p.FName = "predelete"
return nil return nil
} }
func (p *Person) PostDelete(s gorp.SqlExecutor) error { func (p *Person) PostDelete(s SqlExecutor) error {
p.LName = "postdelete" p.LName = "postdelete"
return nil return nil
} }
func (p *Person) PostGet(s gorp.SqlExecutor) error { func (p *Person) PostGet(s SqlExecutor) error {
p.LName = "postget" p.LName = "postget"
return nil return nil
} }
@@ -581,7 +569,7 @@ func TestPersistentUser(t *testing.T) {
t.Errorf("%v!=%v", pu, pu2) t.Errorf("%v!=%v", pu, pu2)
} }
arr, err := dbmap.Select(pu, "select * from "+tableName(dbmap, PersistentUser{})) arr, err := dbmap.Select(pu, "select * from PersistentUser")
if err != nil { if err != nil {
panic(err) panic(err)
} }
@@ -591,7 +579,7 @@ func TestPersistentUser(t *testing.T) {
// prove we can get the results back in a slice // prove we can get the results back in a slice
var puArr []*PersistentUser var puArr []*PersistentUser
_, err = dbmap.Select(&puArr, "select * from "+tableName(dbmap, PersistentUser{})) _, err = dbmap.Select(&puArr, "select * from PersistentUser")
if err != nil { if err != nil {
panic(err) panic(err)
} }
@@ -604,7 +592,7 @@ func TestPersistentUser(t *testing.T) {
// prove we can get the results back in a non-pointer slice // prove we can get the results back in a non-pointer slice
var puValues []PersistentUser var puValues []PersistentUser
_, err = dbmap.Select(&puValues, "select * from "+tableName(dbmap, PersistentUser{})) _, err = dbmap.Select(&puValues, "select * from PersistentUser")
if err != nil { if err != nil {
panic(err) panic(err)
} }
@@ -617,7 +605,7 @@ func TestPersistentUser(t *testing.T) {
// prove we can get the results back in a string slice // prove we can get the results back in a string slice
var idArr []*string var idArr []*string
_, err = dbmap.Select(&idArr, "select "+columnName(dbmap, PersistentUser{}, "Id")+" from "+tableName(dbmap, PersistentUser{})) _, err = dbmap.Select(&idArr, "select Id from PersistentUser")
if err != nil { if err != nil {
panic(err) panic(err)
} }
@@ -630,7 +618,7 @@ func TestPersistentUser(t *testing.T) {
// prove we can get the results back in an int slice // prove we can get the results back in an int slice
var keyArr []*int32 var keyArr []*int32
_, err = dbmap.Select(&keyArr, "select mykey from "+tableName(dbmap, PersistentUser{})) _, err = dbmap.Select(&keyArr, "select mykey from PersistentUser")
if err != nil { if err != nil {
panic(err) panic(err)
} }
@@ -643,7 +631,7 @@ func TestPersistentUser(t *testing.T) {
// prove we can get the results back in a bool slice // prove we can get the results back in a bool slice
var passedArr []*bool var passedArr []*bool
_, err = dbmap.Select(&passedArr, "select "+columnName(dbmap, PersistentUser{}, "PassedTraining")+" from "+tableName(dbmap, PersistentUser{})) _, err = dbmap.Select(&passedArr, "select PassedTraining from PersistentUser")
if err != nil { if err != nil {
panic(err) panic(err)
} }
@@ -656,7 +644,7 @@ func TestPersistentUser(t *testing.T) {
// prove we can get the results back in a non-pointer slice // prove we can get the results back in a non-pointer slice
var stringArr []string var stringArr []string
_, err = dbmap.Select(&stringArr, "select "+columnName(dbmap, PersistentUser{}, "Id")+" from "+tableName(dbmap, PersistentUser{})) _, err = dbmap.Select(&stringArr, "select Id from PersistentUser")
if err != nil { if err != nil {
panic(err) panic(err)
} }
@@ -687,7 +675,7 @@ func TestNamedQueryMap(t *testing.T) {
// Test simple case // Test simple case
var puArr []*PersistentUser var puArr []*PersistentUser
_, err = dbmap.Select(&puArr, "select * from "+tableName(dbmap, PersistentUser{})+" where mykey = :Key", map[string]interface{}{ _, err = dbmap.Select(&puArr, "select * from PersistentUser where mykey = :Key", map[string]interface{}{
"Key": 43, "Key": 43,
}) })
if err != nil { if err != nil {
@@ -703,7 +691,7 @@ func TestNamedQueryMap(t *testing.T) {
// Test more specific map value type is ok // Test more specific map value type is ok
puArr = nil puArr = nil
_, err = dbmap.Select(&puArr, "select * from "+tableName(dbmap, PersistentUser{})+" where mykey = :Key", map[string]int{ _, err = dbmap.Select(&puArr, "select * from PersistentUser where mykey = :Key", map[string]int{
"Key": 43, "Key": 43,
}) })
if err != nil { if err != nil {
@@ -717,10 +705,10 @@ func TestNamedQueryMap(t *testing.T) {
// Test multiple parameters set. // Test multiple parameters set.
puArr = nil puArr = nil
_, err = dbmap.Select(&puArr, ` _, err = dbmap.Select(&puArr, `
select * from `+tableName(dbmap, PersistentUser{})+` select * from PersistentUser
where mykey = :Key where mykey = :Key
and `+columnName(dbmap, PersistentUser{}, "PassedTraining")+` = :PassedTraining and PassedTraining = :PassedTraining
and `+columnName(dbmap, PersistentUser{}, "Id")+` = :Id`, map[string]interface{}{ and Id = :Id`, map[string]interface{}{
"Key": 43, "Key": 43,
"PassedTraining": false, "PassedTraining": false,
"Id": "33r", "Id": "33r",
@@ -737,9 +725,9 @@ select * from `+tableName(dbmap, PersistentUser{})+`
// Test having extra, unused properties in the map. // Test having extra, unused properties in the map.
puArr = nil puArr = nil
_, err = dbmap.Select(&puArr, ` _, err = dbmap.Select(&puArr, `
select * from `+tableName(dbmap, PersistentUser{})+` select * from PersistentUser
where mykey = :Key where mykey = :Key
and `+columnName(dbmap, PersistentUser{}, "Id")+` != 'abc:def'`, map[string]interface{}{ and Id != 'abc:def'`, map[string]interface{}{
"Key": 43, "Key": 43,
"PassedTraining": false, "PassedTraining": false,
}) })
@@ -752,7 +740,7 @@ select * from `+tableName(dbmap, PersistentUser{})+`
} }
// Test to delete with Exec and named params. // Test to delete with Exec and named params.
result, err := dbmap.Exec("delete from "+tableName(dbmap, PersistentUser{})+" where mykey = :Key", map[string]interface{}{ result, err := dbmap.Exec("delete from PersistentUser where mykey = :Key", map[string]interface{}{
"Key": 43, "Key": 43,
}) })
count, err := result.RowsAffected() count, err := result.RowsAffected()
@@ -785,10 +773,10 @@ func TestNamedQueryStruct(t *testing.T) {
// Test select self // Test select self
var puArr []*PersistentUser var puArr []*PersistentUser
_, err = dbmap.Select(&puArr, ` _, err = dbmap.Select(&puArr, `
select * from `+tableName(dbmap, PersistentUser{})+` select * from PersistentUser
where mykey = :Key where mykey = :Key
and `+columnName(dbmap, PersistentUser{}, "PassedTraining")+` = :PassedTraining and PassedTraining = :PassedTraining
and `+columnName(dbmap, PersistentUser{}, "Id")+` = :Id`, pu) and Id = :Id`, pu)
if err != nil { if err != nil {
t.Errorf("Failed to select: %s", err) t.Errorf("Failed to select: %s", err)
t.FailNow() t.FailNow()
@@ -802,10 +790,10 @@ select * from `+tableName(dbmap, PersistentUser{})+`
// Test delete self. // Test delete self.
result, err := dbmap.Exec(` result, err := dbmap.Exec(`
delete from `+tableName(dbmap, PersistentUser{})+` delete from PersistentUser
where mykey = :Key where mykey = :Key
and `+columnName(dbmap, PersistentUser{}, "PassedTraining")+` = :PassedTraining and PassedTraining = :PassedTraining
and `+columnName(dbmap, PersistentUser{}, "Id")+` = :Id`, pu) and Id = :Id`, pu)
count, err := result.RowsAffected() count, err := result.RowsAffected()
if err != nil { if err != nil {
t.Errorf("Failed to exec: %s", err) t.Errorf("Failed to exec: %s", err)
@@ -820,14 +808,14 @@ delete from `+tableName(dbmap, PersistentUser{})+`
func TestReturnsNonNilSlice(t *testing.T) { func TestReturnsNonNilSlice(t *testing.T) {
dbmap := initDbMap() dbmap := initDbMap()
defer dropAndClose(dbmap) defer dropAndClose(dbmap)
noResultsSQL := "select * from invoice_test where " + columnName(dbmap, Invoice{}, "Id") + "=99999" noResultsSQL := "select * from invoice_test where id=99999"
var r1 []*Invoice var r1 []*Invoice
rawSelect(dbmap, &r1, noResultsSQL) _rawselect(dbmap, &r1, noResultsSQL)
if r1 == nil { if r1 == nil {
t.Errorf("r1==nil") t.Errorf("r1==nil")
} }
r2 := rawSelect(dbmap, Invoice{}, noResultsSQL) r2 := _rawselect(dbmap, Invoice{}, noResultsSQL)
if r2 == nil { if r2 == nil {
t.Errorf("r2==nil") t.Errorf("r2==nil")
} }
@@ -881,16 +869,16 @@ func TestOptimisticLocking(t *testing.T) {
p1.LName = "Howard" p1.LName = "Howard"
count, err := dbmap.Update(p1) count, err := dbmap.Update(p1)
if _, ok := err.(gorp.OptimisticLockError); !ok { if _, ok := err.(OptimisticLockError); !ok {
t.Errorf("update - Expected gorp.OptimisticLockError, got: %v", err) t.Errorf("update - Expected OptimisticLockError, got: %v", err)
} }
if count != -1 { if count != -1 {
t.Errorf("update - Expected -1 count, got: %d", count) t.Errorf("update - Expected -1 count, got: %d", count)
} }
count, err = dbmap.Delete(p1) count, err = dbmap.Delete(p1)
if _, ok := err.(gorp.OptimisticLockError); !ok { if _, ok := err.(OptimisticLockError); !ok {
t.Errorf("delete - Expected gorp.OptimisticLockError, got: %v", err) t.Errorf("delete - Expected OptimisticLockError, got: %v", err)
} }
if count != -1 { if count != -1 {
t.Errorf("delete - Expected -1 count, got: %d", count) t.Errorf("delete - Expected -1 count, got: %d", count)
@@ -913,7 +901,7 @@ func TestNullValues(t *testing.T) {
defer dropAndClose(dbmap) defer dropAndClose(dbmap)
// insert a row directly // insert a row directly
rawExec(dbmap, "insert into "+tableName(dbmap, TableWithNull{})+" values (10, null, "+ _rawexec(dbmap, "insert into TableWithNull values (10, null, "+
"null, null, null, null)") "null, null, null, null)")
// try to load it // try to load it
@@ -1037,10 +1025,10 @@ func TestRawSelect(t *testing.T) {
expected := &InvoicePersonView{inv1.Id, p1.Id, inv1.Memo, p1.FName, 0} expected := &InvoicePersonView{inv1.Id, p1.Id, inv1.Memo, p1.FName, 0}
query := "select i." + columnName(dbmap, Invoice{}, "Id") + " InvoiceId, p." + columnName(dbmap, Person{}, "Id") + " PersonId, i." + columnName(dbmap, Invoice{}, "Memo") + ", p." + columnName(dbmap, Person{}, "FName") + " " + query := "select i.Id InvoiceId, p.Id PersonId, i.Memo, p.FName " +
"from invoice_test i, person_test p " + "from invoice_test i, person_test p " +
"where i." + columnName(dbmap, Invoice{}, "PersonId") + " = p." + columnName(dbmap, Person{}, "Id") "where i.PersonId = p.Id"
list := rawSelect(dbmap, InvoicePersonView{}, query) list := _rawselect(dbmap, InvoicePersonView{}, query)
if len(list) != 1 { if len(list) != 1 {
t.Errorf("len(list) != 1: %d", len(list)) t.Errorf("len(list) != 1: %d", len(list))
} else if !reflect.DeepEqual(expected, list[0]) { } else if !reflect.DeepEqual(expected, list[0]) {
@@ -1075,7 +1063,7 @@ func TestHooks(t *testing.T) {
var persons []*Person var persons []*Person
bindVar := dbmap.Dialect.BindVar(0) bindVar := dbmap.Dialect.BindVar(0)
rawSelect(dbmap, &persons, "select * from person_test where "+columnName(dbmap, Person{}, "Id")+" = "+bindVar, p1.Id) _rawselect(dbmap, &persons, "select * from person_test where id = "+bindVar, p1.Id)
if persons[0].LName != "postget" { if persons[0].LName != "postget" {
t.Errorf("p1.PostGet() didn't run after select: %v", p1) t.Errorf("p1.PostGet() didn't run after select: %v", p1)
} }
@@ -1141,7 +1129,7 @@ func TestSavepoint(t *testing.T) {
trans.Insert(inv1) trans.Insert(inv1)
var checkMemo = func(want string) { var checkMemo = func(want string) {
memo, err := trans.SelectStr("select " + columnName(dbmap, Invoice{}, "Memo") + " from invoice_test") memo, err := trans.SelectStr("select memo from invoice_test")
if err != nil { if err != nil {
panic(err) panic(err)
} }
@@ -1208,8 +1196,8 @@ func TestCrud(t *testing.T) {
testCrudInternal(t, dbmap, foo) testCrudInternal(t, dbmap, foo)
} }
func testCrudInternal(t *testing.T, dbmap *gorp.DbMap, val testable) { func testCrudInternal(t *testing.T, dbmap *DbMap, val testable) {
table, err := dbmap.TableFor(reflect.TypeOf(val).Elem(), false) table, _, err := dbmap.tableForPointer(val, false)
if err != nil { if err != nil {
t.Errorf("couldn't call TableFor: val=%v err=%v", val, err) t.Errorf("couldn't call TableFor: val=%v err=%v", val, err)
} }
@@ -1244,11 +1232,11 @@ func testCrudInternal(t *testing.T, dbmap *gorp.DbMap, val testable) {
} }
// Select * // Select *
rows, err := dbmap.Select(val, "select * from "+dbmap.Dialect.QuoteField(table.TableName)) rows, err := dbmap.Select(val, "select * from "+table.TableName)
if err != nil { if err != nil {
t.Errorf("couldn't select * from %s err=%v", dbmap.Dialect.QuoteField(table.TableName), err) t.Errorf("couldn't select * from %s err=%v", table.TableName, err)
} else if len(rows) != 1 { } else if len(rows) != 1 {
t.Errorf("unexpected row count in %s: %d", dbmap.Dialect.QuoteField(table.TableName), len(rows)) t.Errorf("unexpected row count in %s: %d", table.TableName, len(rows))
} else if !reflect.DeepEqual(val, rows[0]) { } else if !reflect.DeepEqual(val, rows[0]) {
t.Errorf("select * result: %v != %v", val, rows[0]) t.Errorf("select * result: %v != %v", val, rows[0])
} }
@@ -1297,7 +1285,7 @@ func TestColumnFilter(t *testing.T) {
inv1.Memo = "c" inv1.Memo = "c"
inv1.IsPaid = true inv1.IsPaid = true
_updateColumns(dbmap, func(col *gorp.ColumnMap) bool { _updateColumns(dbmap, func(col *ColumnMap) bool {
return col.ColumnName == "Memo" return col.ColumnName == "Memo"
}, inv1) }, inv1)
@@ -1361,7 +1349,7 @@ func TestWithEmbeddedStruct(t *testing.T) {
t.Errorf("%v != %v", expected, es2) t.Errorf("%v != %v", expected, es2)
} }
ess := rawSelect(dbmap, WithEmbeddedStruct{}, "select * from embedded_struct_test") ess := _rawselect(dbmap, WithEmbeddedStruct{}, "select * from embedded_struct_test")
if !reflect.DeepEqual(es2, ess[0]) { if !reflect.DeepEqual(es2, ess[0]) {
t.Errorf("%v != %v", es2, ess[0]) t.Errorf("%v != %v", es2, ess[0])
} }
@@ -1388,7 +1376,7 @@ func TestWithEmbeddedStructConflictingEmbeddedMemberNames(t *testing.T) {
t.Errorf("%v != %v", expected, es2) t.Errorf("%v != %v", expected, es2)
} }
ess := rawSelect(dbmap, WithEmbeddedStructConflictingEmbeddedMemberNames{}, "select * from embedded_struct_conflict_name_test") ess := _rawselect(dbmap, WithEmbeddedStructConflictingEmbeddedMemberNames{}, "select * from embedded_struct_conflict_name_test")
if !reflect.DeepEqual(es2, ess[0]) { if !reflect.DeepEqual(es2, ess[0]) {
t.Errorf("%v != %v", es2, ess[0]) t.Errorf("%v != %v", es2, ess[0])
} }
@@ -1414,7 +1402,7 @@ func TestWithEmbeddedStructSameMemberName(t *testing.T) {
t.Errorf("%v != %v", expected, es2) t.Errorf("%v != %v", expected, es2)
} }
ess := rawSelect(dbmap, WithEmbeddedStructSameMemberName{}, "select * from embedded_struct_same_member_name_test") ess := _rawselect(dbmap, WithEmbeddedStructSameMemberName{}, "select * from embedded_struct_same_member_name_test")
if !reflect.DeepEqual(es2, ess[0]) { if !reflect.DeepEqual(es2, ess[0]) {
t.Errorf("%v != %v", es2, ess[0]) t.Errorf("%v != %v", es2, ess[0])
} }
@@ -1462,81 +1450,81 @@ func TestSelectVal(t *testing.T) {
_insert(dbmap, &t1) _insert(dbmap, &t1)
// SelectInt // SelectInt
i64 := selectInt(dbmap, "select "+columnName(dbmap, TableWithNull{}, "Int64")+" from "+tableName(dbmap, TableWithNull{})+" where "+columnName(dbmap, TableWithNull{}, "Str")+"='abc'") i64 := selectInt(dbmap, "select Int64 from TableWithNull where Str='abc'")
if i64 != 78 { if i64 != 78 {
t.Errorf("int64 %d != 78", i64) t.Errorf("int64 %d != 78", i64)
} }
i64 = selectInt(dbmap, "select count(*) from "+tableName(dbmap, TableWithNull{})) i64 = selectInt(dbmap, "select count(*) from TableWithNull")
if i64 != 1 { if i64 != 1 {
t.Errorf("int64 count %d != 1", i64) t.Errorf("int64 count %d != 1", i64)
} }
i64 = selectInt(dbmap, "select count(*) from "+tableName(dbmap, TableWithNull{})+" where "+columnName(dbmap, TableWithNull{}, "Str")+"="+bindVar, "asdfasdf") i64 = selectInt(dbmap, "select count(*) from TableWithNull where Str="+bindVar, "asdfasdf")
if i64 != 0 { if i64 != 0 {
t.Errorf("int64 no rows %d != 0", i64) t.Errorf("int64 no rows %d != 0", i64)
} }
// SelectNullInt // SelectNullInt
n := selectNullInt(dbmap, "select "+columnName(dbmap, TableWithNull{}, "Int64")+" from "+tableName(dbmap, TableWithNull{})+" where "+columnName(dbmap, TableWithNull{}, "Str")+"='notfound'") n := selectNullInt(dbmap, "select Int64 from TableWithNull where Str='notfound'")
if !reflect.DeepEqual(n, sql.NullInt64{0, false}) { if !reflect.DeepEqual(n, sql.NullInt64{0, false}) {
t.Errorf("nullint %v != 0,false", n) t.Errorf("nullint %v != 0,false", n)
} }
n = selectNullInt(dbmap, "select "+columnName(dbmap, TableWithNull{}, "Int64")+" from "+tableName(dbmap, TableWithNull{})+" where "+columnName(dbmap, TableWithNull{}, "Str")+"='abc'") n = selectNullInt(dbmap, "select Int64 from TableWithNull where Str='abc'")
if !reflect.DeepEqual(n, sql.NullInt64{78, true}) { if !reflect.DeepEqual(n, sql.NullInt64{78, true}) {
t.Errorf("nullint %v != 78, true", n) t.Errorf("nullint %v != 78, true", n)
} }
// SelectFloat // SelectFloat
f64 := selectFloat(dbmap, "select "+columnName(dbmap, TableWithNull{}, "Float64")+" from "+tableName(dbmap, TableWithNull{})+" where "+columnName(dbmap, TableWithNull{}, "Str")+"='abc'") f64 := selectFloat(dbmap, "select Float64 from TableWithNull where Str='abc'")
if f64 != 32.2 { if f64 != 32.2 {
t.Errorf("float64 %d != 32.2", f64) t.Errorf("float64 %d != 32.2", f64)
} }
f64 = selectFloat(dbmap, "select min("+columnName(dbmap, TableWithNull{}, "Float64")+") from "+tableName(dbmap, TableWithNull{})) f64 = selectFloat(dbmap, "select min(Float64) from TableWithNull")
if f64 != 32.2 { if f64 != 32.2 {
t.Errorf("float64 min %d != 32.2", f64) t.Errorf("float64 min %d != 32.2", f64)
} }
f64 = selectFloat(dbmap, "select count(*) from "+tableName(dbmap, TableWithNull{})+" where "+columnName(dbmap, TableWithNull{}, "Str")+"="+bindVar, "asdfasdf") f64 = selectFloat(dbmap, "select count(*) from TableWithNull where Str="+bindVar, "asdfasdf")
if f64 != 0 { if f64 != 0 {
t.Errorf("float64 no rows %d != 0", f64) t.Errorf("float64 no rows %d != 0", f64)
} }
// SelectNullFloat // SelectNullFloat
nf := selectNullFloat(dbmap, "select "+columnName(dbmap, TableWithNull{}, "Float64")+" from "+tableName(dbmap, TableWithNull{})+" where "+columnName(dbmap, TableWithNull{}, "Str")+"='notfound'") nf := selectNullFloat(dbmap, "select Float64 from TableWithNull where Str='notfound'")
if !reflect.DeepEqual(nf, sql.NullFloat64{0, false}) { if !reflect.DeepEqual(nf, sql.NullFloat64{0, false}) {
t.Errorf("nullfloat %v != 0,false", nf) t.Errorf("nullfloat %v != 0,false", nf)
} }
nf = selectNullFloat(dbmap, "select "+columnName(dbmap, TableWithNull{}, "Float64")+" from "+tableName(dbmap, TableWithNull{})+" where "+columnName(dbmap, TableWithNull{}, "Str")+"='abc'") nf = selectNullFloat(dbmap, "select Float64 from TableWithNull where Str='abc'")
if !reflect.DeepEqual(nf, sql.NullFloat64{32.2, true}) { if !reflect.DeepEqual(nf, sql.NullFloat64{32.2, true}) {
t.Errorf("nullfloat %v != 32.2, true", nf) t.Errorf("nullfloat %v != 32.2, true", nf)
} }
// SelectStr // SelectStr
s := selectStr(dbmap, "select "+columnName(dbmap, TableWithNull{}, "Str")+" from "+tableName(dbmap, TableWithNull{})+" where "+columnName(dbmap, TableWithNull{}, "Int64")+"="+bindVar, 78) s := selectStr(dbmap, "select Str from TableWithNull where Int64="+bindVar, 78)
if s != "abc" { if s != "abc" {
t.Errorf("s %s != abc", s) t.Errorf("s %s != abc", s)
} }
s = selectStr(dbmap, "select "+columnName(dbmap, TableWithNull{}, "Str")+" from "+tableName(dbmap, TableWithNull{})+" where "+columnName(dbmap, TableWithNull{}, "Str")+"='asdfasdf'") s = selectStr(dbmap, "select Str from TableWithNull where Str='asdfasdf'")
if s != "" { if s != "" {
t.Errorf("s no rows %s != ''", s) t.Errorf("s no rows %s != ''", s)
} }
// SelectNullStr // SelectNullStr
ns := selectNullStr(dbmap, "select "+columnName(dbmap, TableWithNull{}, "Str")+" from "+tableName(dbmap, TableWithNull{})+" where "+columnName(dbmap, TableWithNull{}, "Int64")+"="+bindVar, 78) ns := selectNullStr(dbmap, "select Str from TableWithNull where Int64="+bindVar, 78)
if !reflect.DeepEqual(ns, sql.NullString{"abc", true}) { if !reflect.DeepEqual(ns, sql.NullString{"abc", true}) {
t.Errorf("nullstr %v != abc,true", ns) t.Errorf("nullstr %v != abc,true", ns)
} }
ns = selectNullStr(dbmap, "select "+columnName(dbmap, TableWithNull{}, "Str")+" from "+tableName(dbmap, TableWithNull{})+" where "+columnName(dbmap, TableWithNull{}, "Str")+"='asdfasdf'") ns = selectNullStr(dbmap, "select Str from TableWithNull where Str='asdfasdf'")
if !reflect.DeepEqual(ns, sql.NullString{"", false}) { if !reflect.DeepEqual(ns, sql.NullString{"", false}) {
t.Errorf("nullstr no rows %v != '',false", ns) t.Errorf("nullstr no rows %v != '',false", ns)
} }
// SelectInt/Str with named parameters // SelectInt/Str with named parameters
i64 = selectInt(dbmap, "select "+columnName(dbmap, TableWithNull{}, "Int64")+" from "+tableName(dbmap, TableWithNull{})+" where "+columnName(dbmap, TableWithNull{}, "Str")+"=:abc", map[string]string{"abc": "abc"}) i64 = selectInt(dbmap, "select Int64 from TableWithNull where Str=:abc", map[string]string{"abc": "abc"})
if i64 != 78 { if i64 != 78 {
t.Errorf("int64 %d != 78", i64) t.Errorf("int64 %d != 78", i64)
} }
ns = selectNullStr(dbmap, "select "+columnName(dbmap, TableWithNull{}, "Str")+" from "+tableName(dbmap, TableWithNull{})+" where "+columnName(dbmap, TableWithNull{}, "Int64")+"=:num", map[string]int{"num": 78}) ns = selectNullStr(dbmap, "select Str from TableWithNull where Int64=:num", map[string]int{"num": 78})
if !reflect.DeepEqual(ns, sql.NullString{"abc", true}) { if !reflect.DeepEqual(ns, sql.NullString{"abc", true}) {
t.Errorf("nullstr %v != abc,true", ns) t.Errorf("nullstr %v != abc,true", ns)
} }
@@ -1577,12 +1565,12 @@ func TestWithStringPk(t *testing.T) {
} }
} }
// TestSqlExecutorInterfaceSelects ensures that all gorp.DbMap methods starting with Select... // TestSqlExecutorInterfaceSelects ensures that all DbMap methods starting with Select...
// are also exposed in the gorp.SqlExecutor interface. Select... functions can always // are also exposed in the SqlExecutor interface. Select... functions can always
// run on Pre/Post hooks. // run on Pre/Post hooks.
func TestSqlExecutorInterfaceSelects(t *testing.T) { func TestSqlExecutorInterfaceSelects(t *testing.T) {
dbMapType := reflect.TypeOf(&gorp.DbMap{}) dbMapType := reflect.TypeOf(&DbMap{})
sqlExecutorType := reflect.TypeOf((*gorp.SqlExecutor)(nil)).Elem() sqlExecutorType := reflect.TypeOf((*SqlExecutor)(nil)).Elem()
numDbMapMethods := dbMapType.NumMethod() numDbMapMethods := dbMapType.NumMethod()
for i := 0; i < numDbMapMethods; i += 1 { for i := 0; i < numDbMapMethods; i += 1 {
dbMapMethod := dbMapType.Method(i) dbMapMethod := dbMapType.Method(i)
@@ -1590,7 +1578,7 @@ func TestSqlExecutorInterfaceSelects(t *testing.T) {
continue continue
} }
if _, found := sqlExecutorType.MethodByName(dbMapMethod.Name); !found { if _, found := sqlExecutorType.MethodByName(dbMapMethod.Name); !found {
t.Errorf("Method %s is defined on gorp.DbMap but not implemented in gorp.SqlExecutor", t.Errorf("Method %s is defined on DbMap but not implemented in SqlExecutor",
dbMapMethod.Name) dbMapMethod.Name)
} }
} }
@@ -1603,28 +1591,28 @@ func TestNullTime(t *testing.T) {
// if time is null // if time is null
ent := &WithNullTime{ ent := &WithNullTime{
Id: 0, Id: 0,
Time: gorp.NullTime{ Time: NullTime{
Valid: false, Valid: false,
}} }}
err := dbmap.Insert(ent) err := dbmap.Insert(ent)
if err != nil { if err != nil {
t.Error("failed insert on %s", err.Error()) t.Error("failed insert on %s", err.Error())
} }
err = dbmap.SelectOne(ent, `select * from nulltime_test where `+columnName(dbmap, WithNullTime{}, "Id")+`=:Id`, map[string]interface{}{ err = dbmap.SelectOne(ent, `select * from nulltime_test where Id=:Id`, map[string]interface{}{
"Id": ent.Id, "Id": ent.Id,
}) })
if err != nil { if err != nil {
t.Error("failed select on %s", err.Error()) t.Error("failed select on %s", err.Error())
} }
if ent.Time.Valid { if ent.Time.Valid {
t.Error("gorp.NullTime returns valid but expected null.") t.Error("NullTime returns valid but expected null.")
} }
// if time is not null // if time is not null
ts, err := time.Parse(time.Stamp, "Jan 2 15:04:05") ts, err := time.Parse(time.Stamp, "Jan 2 15:04:05")
ent = &WithNullTime{ ent = &WithNullTime{
Id: 1, Id: 1,
Time: gorp.NullTime{ Time: NullTime{
Valid: true, Valid: true,
Time: ts, Time: ts,
}} }}
@@ -1632,14 +1620,14 @@ func TestNullTime(t *testing.T) {
if err != nil { if err != nil {
t.Error("failed insert on %s", err.Error()) t.Error("failed insert on %s", err.Error())
} }
err = dbmap.SelectOne(ent, `select * from nulltime_test where `+columnName(dbmap, WithNullTime{}, "Id")+`=:Id`, map[string]interface{}{ err = dbmap.SelectOne(ent, `select * from nulltime_test where Id=:Id`, map[string]interface{}{
"Id": ent.Id, "Id": ent.Id,
}) })
if err != nil { if err != nil {
t.Error("failed select on %s", err.Error()) t.Error("failed select on %s", err.Error())
} }
if !ent.Time.Valid { if !ent.Time.Valid {
t.Error("gorp.NullTime returns invalid but expected valid.") t.Error("NullTime returns invalid but expected valid.")
} }
if ent.Time.Time.UTC() != ts.UTC() { if ent.Time.Time.UTC() != ts.UTC() {
t.Errorf("expect %v but got %v.", ts, ent.Time.Time) t.Errorf("expect %v but got %v.", ts, ent.Time.Time)
@@ -1724,7 +1712,7 @@ func TestWithTimeSelect(t *testing.T) {
_insert(dbmap, &w1, &w2) _insert(dbmap, &w1, &w2)
var caseIds []int64 var caseIds []int64
_, err := dbmap.Select(&caseIds, "SELECT "+columnName(dbmap, WithTime{}, "Id")+" FROM time_test WHERE "+columnName(dbmap, WithTime{}, "Time")+" < "+dbmap.Dialect.BindVar(0), halfhourago) _, err := dbmap.Select(&caseIds, "SELECT id FROM time_test WHERE Time < "+dbmap.Dialect.BindVar(0), halfhourago)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
@@ -1750,9 +1738,9 @@ func TestInvoicePersonView(t *testing.T) {
dbmap.Insert(inv1) dbmap.Insert(inv1)
// Run your query // Run your query
query := "select i." + columnName(dbmap, Invoice{}, "Id") + " InvoiceId, p." + columnName(dbmap, Person{}, "Id") + " PersonId, i." + columnName(dbmap, Invoice{}, "Memo") + ", p." + columnName(dbmap, Person{}, "FName") + " " + query := "select i.Id InvoiceId, p.Id PersonId, i.Memo, p.FName " +
"from invoice_test i, person_test p " + "from invoice_test i, person_test p " +
"where i." + columnName(dbmap, Invoice{}, "PersonId") + " = p." + columnName(dbmap, Person{}, "Id") "where i.PersonId = p.Id"
// pass a slice of pointers to Select() // pass a slice of pointers to Select()
// this avoids the need to type assert after the query is run // this avoids the need to type assert after the query is run
@@ -1817,9 +1805,9 @@ func TestSelectTooManyCols(t *testing.T) {
} }
var p3 FNameOnly var p3 FNameOnly
err := dbmap.SelectOne(&p3, "select * from person_test where "+columnName(dbmap, Person{}, "Id")+"=:Id", params) err := dbmap.SelectOne(&p3, "select * from person_test where Id=:Id", params)
if err != nil { if err != nil {
if !gorp.NonFatalError(err) { if !NonFatalError(err) {
t.Error(err) t.Error(err)
} }
} else { } else {
@@ -1831,9 +1819,9 @@ func TestSelectTooManyCols(t *testing.T) {
} }
var pSlice []FNameOnly var pSlice []FNameOnly
_, err = dbmap.Select(&pSlice, "select * from person_test order by "+columnName(dbmap, Person{}, "FName")+" asc") _, err = dbmap.Select(&pSlice, "select * from person_test order by fname asc")
if err != nil { if err != nil {
if !gorp.NonFatalError(err) { if !NonFatalError(err) {
t.Error(err) t.Error(err)
} }
} else { } else {
@@ -1863,7 +1851,7 @@ func TestSelectSingleVal(t *testing.T) {
} }
var p2 Person var p2 Person
err := dbmap.SelectOne(&p2, "select * from person_test where "+columnName(dbmap, Person{}, "Id")+"=:Id", params) err := dbmap.SelectOne(&p2, "select * from person_test where Id=:Id", params)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
@@ -1874,7 +1862,7 @@ func TestSelectSingleVal(t *testing.T) {
// verify SelectOne allows non-struct holders // verify SelectOne allows non-struct holders
var s string var s string
err = dbmap.SelectOne(&s, "select "+columnName(dbmap, Person{}, "FName")+" from person_test where "+columnName(dbmap, Person{}, "Id")+"=:Id", params) err = dbmap.SelectOne(&s, "select FName from person_test where Id=:Id", params)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
@@ -1883,14 +1871,14 @@ func TestSelectSingleVal(t *testing.T) {
} }
// verify SelectOne requires pointer receiver // verify SelectOne requires pointer receiver
err = dbmap.SelectOne(s, "select "+columnName(dbmap, Person{}, "FName")+" from person_test where "+columnName(dbmap, Person{}, "Id")+"=:Id", params) err = dbmap.SelectOne(s, "select FName from person_test where Id=:Id", params)
if err == nil { if err == nil {
t.Error("SelectOne should have returned error for non-pointer holder") t.Error("SelectOne should have returned error for non-pointer holder")
} }
// verify SelectOne works with uninitialized pointers // verify SelectOne works with uninitialized pointers
var p3 *Person var p3 *Person
err = dbmap.SelectOne(&p3, "select * from person_test where "+columnName(dbmap, Person{}, "Id")+"=:Id", params) err = dbmap.SelectOne(&p3, "select * from person_test where Id=:Id", params)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
@@ -1901,13 +1889,13 @@ func TestSelectSingleVal(t *testing.T) {
// verify that the receiver is still nil if nothing was found // verify that the receiver is still nil if nothing was found
var p4 *Person var p4 *Person
dbmap.SelectOne(&p3, "select * from person_test where 2<1 AND "+columnName(dbmap, Person{}, "Id")+"=:Id", params) dbmap.SelectOne(&p3, "select * from person_test where 2<1 AND Id=:Id", params)
if p4 != nil { if p4 != nil {
t.Error("SelectOne should not have changed a nil receiver when no rows were found") t.Error("SelectOne should not have changed a nil receiver when no rows were found")
} }
// verify that the error is set to sql.ErrNoRows if not found // verify that the error is set to sql.ErrNoRows if not found
err = dbmap.SelectOne(&p2, "select * from person_test where "+columnName(dbmap, Person{}, "Id")+"=:Id", map[string]interface{}{ err = dbmap.SelectOne(&p2, "select * from person_test where Id=:Id", map[string]interface{}{
"Id": -2222, "Id": -2222,
}) })
if err == nil || err != sql.ErrNoRows { if err == nil || err != sql.ErrNoRows {
@@ -1915,7 +1903,7 @@ func TestSelectSingleVal(t *testing.T) {
} }
_insert(dbmap, &Person{0, 0, 0, "bob", "smith", 0}) _insert(dbmap, &Person{0, 0, 0, "bob", "smith", 0})
err = dbmap.SelectOne(&p2, "select * from person_test where "+columnName(dbmap, Person{}, "FName")+"='bob'") err = dbmap.SelectOne(&p2, "select * from person_test where Fname='bob'")
if err == nil { if err == nil {
t.Error("Expected error when two rows found") t.Error("Expected error when two rows found")
} }
@@ -1927,7 +1915,7 @@ func TestSelectSingleVal(t *testing.T) {
var tFloat float64 var tFloat float64
primVals := []interface{}{tInt, tStr, tBool, tFloat} primVals := []interface{}{tInt, tStr, tBool, tFloat}
for _, prim := range primVals { for _, prim := range primVals {
err = dbmap.SelectOne(&prim, "select * from person_test where "+columnName(dbmap, Person{}, "Id")+"=-123") err = dbmap.SelectOne(&prim, "select * from person_test where Id=-123")
if err == nil || err != sql.ErrNoRows { if err == nil || err != sql.ErrNoRows {
t.Error("primVals: SelectOne should have returned sql.ErrNoRows") t.Error("primVals: SelectOne should have returned sql.ErrNoRows")
} }
@@ -1946,7 +1934,7 @@ func TestSelectAlias(t *testing.T) {
// Select into IdCreatedExternal type, which includes some fields not present // Select into IdCreatedExternal type, which includes some fields not present
// in id_created_test // in id_created_test
var p2 IdCreatedExternal var p2 IdCreatedExternal
err := dbmap.SelectOne(&p2, "select * from id_created_test where "+columnName(dbmap, IdCreatedExternal{}, "Id")+"=1") err := dbmap.SelectOne(&p2, "select * from id_created_test where Id=1")
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
@@ -1985,13 +1973,13 @@ func TestMysqlPanicIfDialectNotInitialized(t *testing.T) {
defer func() { defer func() {
r := recover() r := recover()
if r == nil { if r == nil {
t.Error("db.CreateTables() should panic if db is initialized with an incorrect gorp.MySQLDialect") t.Error("db.CreateTables() should panic if db is initialized with an incorrect MySQLDialect")
} }
}() }()
// invalid MySQLDialect : does not contain Engine or Encoding specification // invalid MySQLDialect : does not contain Engine or Encoding specification
dialect := gorp.MySQLDialect{} dialect := MySQLDialect{}
db := &gorp.DbMap{Db: connect(driver), Dialect: dialect} db := &DbMap{Db: connect(driver), Dialect: dialect}
db.AddTableWithName(Invoice{}, "invoice") db.AddTableWithName(Invoice{}, "invoice")
// the following call should panic : // the following call should panic :
db.CreateTables() db.CreateTables()
@@ -2038,7 +2026,7 @@ func TestPrepare(t *testing.T) {
bindVar0 := dbmap.Dialect.BindVar(0) bindVar0 := dbmap.Dialect.BindVar(0)
bindVar1 := dbmap.Dialect.BindVar(1) bindVar1 := dbmap.Dialect.BindVar(1)
stmt, err := dbmap.Prepare(fmt.Sprintf("UPDATE invoice_test SET "+columnName(dbmap, Invoice{}, "Memo")+"=%s WHERE "+columnName(dbmap, Invoice{}, "Id")+"=%s", bindVar0, bindVar1)) stmt, err := dbmap.Prepare(fmt.Sprintf("UPDATE invoice_test SET Memo=%s WHERE Id=%s", bindVar0, bindVar1))
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
@@ -2047,7 +2035,7 @@ func TestPrepare(t *testing.T) {
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
err = dbmap.SelectOne(inv1, "SELECT * from invoice_test WHERE "+columnName(dbmap, Invoice{}, "Memo")+"='prepare-baz'") err = dbmap.SelectOne(inv1, "SELECT * from invoice_test WHERE Memo='prepare-baz'")
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
@@ -2056,7 +2044,7 @@ func TestPrepare(t *testing.T) {
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
transStmt, err := trans.Prepare(fmt.Sprintf("UPDATE invoice_test SET "+columnName(dbmap, Invoice{}, "IsPaid")+"=%s WHERE "+columnName(dbmap, Invoice{}, "Id")+"=%s", bindVar0, bindVar1)) transStmt, err := trans.Prepare(fmt.Sprintf("UPDATE invoice_test SET IsPaid=%s WHERE Id=%s", bindVar0, bindVar1))
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
@@ -2065,11 +2053,11 @@ func TestPrepare(t *testing.T) {
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
err = dbmap.SelectOne(inv2, fmt.Sprintf("SELECT * from invoice_test WHERE "+columnName(dbmap, Invoice{}, "IsPaid")+"=%s", bindVar0), true) err = dbmap.SelectOne(inv2, fmt.Sprintf("SELECT * from invoice_test WHERE IsPaid=%s", bindVar0), true)
if err == nil || err != sql.ErrNoRows { if err == nil || err != sql.ErrNoRows {
t.Error("SelectOne should have returned an sql.ErrNoRows") t.Error("SelectOne should have returned an sql.ErrNoRows")
} }
err = trans.SelectOne(inv2, fmt.Sprintf("SELECT * from invoice_test WHERE "+columnName(dbmap, Invoice{}, "IsPaid")+"=%s", bindVar0), true) err = trans.SelectOne(inv2, fmt.Sprintf("SELECT * from invoice_test WHERE IsPaid=%s", bindVar0), true)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
@@ -2077,7 +2065,7 @@ func TestPrepare(t *testing.T) {
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
err = dbmap.SelectOne(inv2, fmt.Sprintf("SELECT * from invoice_test WHERE "+columnName(dbmap, Invoice{}, "IsPaid")+"=%s", bindVar0), true) err = dbmap.SelectOne(inv2, fmt.Sprintf("SELECT * from invoice_test WHERE IsPaid=%s", bindVar0), true)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
@@ -2087,25 +2075,12 @@ func BenchmarkNativeCrud(b *testing.B) {
b.StopTimer() b.StopTimer()
dbmap := initDbMapBench() dbmap := initDbMapBench()
defer dropAndClose(dbmap) defer dropAndClose(dbmap)
columnId := columnName(dbmap, Invoice{}, "Id")
columnCreated := columnName(dbmap, Invoice{}, "Created")
columnUpdated := columnName(dbmap, Invoice{}, "Updated")
columnMemo := columnName(dbmap, Invoice{}, "Memo")
columnPersonId := columnName(dbmap, Invoice{}, "PersonId")
b.StartTimer() b.StartTimer()
var insert, sel, update, delete string insert := "insert into invoice_test (Created, Updated, Memo, PersonId) values (?, ?, ?, ?)"
if os.Getenv("GORP_TEST_DIALECT") != "postgres" { sel := "select Id, Created, Updated, Memo, PersonId from invoice_test where Id=?"
insert = "insert into invoice_test (" + columnCreated + ", " + columnUpdated + ", " + columnMemo + ", " + columnPersonId + ") values (?, ?, ?, ?)" update := "update invoice_test set Created=?, Updated=?, Memo=?, PersonId=? where Id=?"
sel = "select " + columnId + ", " + columnCreated + ", " + columnUpdated + ", " + columnMemo + ", " + columnPersonId + " from invoice_test where " + columnId + "=?" delete := "delete from invoice_test where Id=?"
update = "update invoice_test set " + columnCreated + "=?, " + columnUpdated + "=?, " + columnMemo + "=?, " + columnPersonId + "=? where " + columnId + "=?"
delete = "delete from invoice_test where " + columnId + "=?"
} else {
insert = "insert into invoice_test (" + columnCreated + ", " + columnUpdated + ", " + columnMemo + ", " + columnPersonId + ") values ($1, $2, $3, $4)"
sel = "select " + columnId + ", " + columnCreated + ", " + columnUpdated + ", " + columnMemo + ", " + columnPersonId + " from invoice_test where " + columnId + "=$1"
update = "update invoice_test set " + columnCreated + "=$1, " + columnUpdated + "=$2, " + columnMemo + "=$3, " + columnPersonId + "=$4 where " + columnId + "=$5"
delete = "delete from invoice_test where " + columnId + "=$1"
}
inv := &Invoice{0, 100, 200, "my memo", 0, false} inv := &Invoice{0, 100, 200, "my memo", 0, false}
@@ -2188,7 +2163,7 @@ func BenchmarkGorpCrud(b *testing.B) {
} }
} }
func initDbMapBench() *gorp.DbMap { func initDbMapBench() *DbMap {
dbmap := newDbMap() dbmap := newDbMap()
dbmap.Db.Exec("drop table if exists invoice_test") dbmap.Db.Exec("drop table if exists invoice_test")
dbmap.AddTableWithName(Invoice{}, "invoice_test").SetKeys(true, "Id") dbmap.AddTableWithName(Invoice{}, "invoice_test").SetKeys(true, "Id")
@@ -2199,7 +2174,7 @@ func initDbMapBench() *gorp.DbMap {
return dbmap return dbmap
} }
func initDbMap() *gorp.DbMap { func initDbMap() *DbMap {
dbmap := newDbMap() dbmap := newDbMap()
dbmap.AddTableWithName(Invoice{}, "invoice_test").SetKeys(true, "Id") dbmap.AddTableWithName(Invoice{}, "invoice_test").SetKeys(true, "Id")
dbmap.AddTableWithName(InvoiceTag{}, "invoice_tag_test") //key is set via primarykey attribute dbmap.AddTableWithName(InvoiceTag{}, "invoice_tag_test") //key is set via primarykey attribute
@@ -2233,7 +2208,7 @@ func initDbMap() *gorp.DbMap {
return dbmap return dbmap
} }
func initDbMapNulls() *gorp.DbMap { func initDbMapNulls() *DbMap {
dbmap := newDbMap() dbmap := newDbMap()
dbmap.AddTable(TableWithNull{}).SetKeys(false, "Id") dbmap.AddTable(TableWithNull{}).SetKeys(false, "Id")
err := dbmap.CreateTables() err := dbmap.CreateTables()
@@ -2243,16 +2218,16 @@ func initDbMapNulls() *gorp.DbMap {
return dbmap return dbmap
} }
func newDbMap() *gorp.DbMap { func newDbMap() *DbMap {
dialect, driver := dialectAndDriver() dialect, driver := dialectAndDriver()
dbmap := &gorp.DbMap{Db: connect(driver), Dialect: dialect} dbmap := &DbMap{Db: connect(driver), Dialect: dialect}
if debug { if debug {
dbmap.TraceOn("", log.New(os.Stdout, "gorptest: ", log.Lmicroseconds)) dbmap.TraceOn("", log.New(os.Stdout, "gorptest: ", log.Lmicroseconds))
} }
return dbmap return dbmap
} }
func dropAndClose(dbmap *gorp.DbMap) { func dropAndClose(dbmap *DbMap) {
dbmap.DropTablesIfExists() dbmap.DropTablesIfExists()
dbmap.Db.Close() dbmap.Db.Close()
} }
@@ -2270,28 +2245,28 @@ func connect(driver string) *sql.DB {
return db return db
} }
func dialectAndDriver() (gorp.Dialect, string) { func dialectAndDriver() (Dialect, string) {
switch os.Getenv("GORP_TEST_DIALECT") { switch os.Getenv("GORP_TEST_DIALECT") {
case "mysql": case "mysql":
return gorp.MySQLDialect{"InnoDB", "UTF8"}, "mymysql" return MySQLDialect{"InnoDB", "UTF8"}, "mymysql"
case "gomysql": case "gomysql":
return gorp.MySQLDialect{"InnoDB", "UTF8"}, "mysql" return MySQLDialect{"InnoDB", "UTF8"}, "mysql"
case "postgres": case "postgres":
return gorp.PostgresDialect{}, "postgres" return PostgresDialect{}, "postgres"
case "sqlite": case "sqlite":
return gorp.SqliteDialect{}, "sqlite3" return SqliteDialect{}, "sqlite3"
} }
panic("GORP_TEST_DIALECT env variable is not set or is invalid. Please see README.md") panic("GORP_TEST_DIALECT env variable is not set or is invalid. Please see README.md")
} }
func _insert(dbmap *gorp.DbMap, list ...interface{}) { func _insert(dbmap *DbMap, list ...interface{}) {
err := dbmap.Insert(list...) err := dbmap.Insert(list...)
if err != nil { if err != nil {
panic(err) panic(err)
} }
} }
func _update(dbmap *gorp.DbMap, list ...interface{}) int64 { func _update(dbmap *DbMap, list ...interface{}) int64 {
count, err := dbmap.Update(list...) count, err := dbmap.Update(list...)
if err != nil { if err != nil {
panic(err) panic(err)
@@ -2299,7 +2274,7 @@ func _update(dbmap *gorp.DbMap, list ...interface{}) int64 {
return count return count
} }
func _updateColumns(dbmap *gorp.DbMap, filter gorp.ColumnFilter, list ...interface{}) int64 { func _updateColumns(dbmap *DbMap, filter ColumnFilter, list ...interface{}) int64 {
count, err := dbmap.UpdateColumns(filter, list...) count, err := dbmap.UpdateColumns(filter, list...)
if err != nil { if err != nil {
panic(err) panic(err)
@@ -2307,7 +2282,7 @@ func _updateColumns(dbmap *gorp.DbMap, filter gorp.ColumnFilter, list ...interfa
return count return count
} }
func _del(dbmap *gorp.DbMap, list ...interface{}) int64 { func _del(dbmap *DbMap, list ...interface{}) int64 {
count, err := dbmap.Delete(list...) count, err := dbmap.Delete(list...)
if err != nil { if err != nil {
panic(err) panic(err)
@@ -2316,7 +2291,7 @@ func _del(dbmap *gorp.DbMap, list ...interface{}) int64 {
return count return count
} }
func _get(dbmap *gorp.DbMap, i interface{}, keys ...interface{}) interface{} { func _get(dbmap *DbMap, i interface{}, keys ...interface{}) interface{} {
obj, err := dbmap.Get(i, keys...) obj, err := dbmap.Get(i, keys...)
if err != nil { if err != nil {
panic(err) panic(err)
@@ -2325,8 +2300,8 @@ func _get(dbmap *gorp.DbMap, i interface{}, keys ...interface{}) interface{} {
return obj return obj
} }
func selectInt(dbmap *gorp.DbMap, query string, args ...interface{}) int64 { func selectInt(dbmap *DbMap, query string, args ...interface{}) int64 {
i64, err := gorp.SelectInt(dbmap, query, args...) i64, err := SelectInt(dbmap, query, args...)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@@ -2334,8 +2309,8 @@ func selectInt(dbmap *gorp.DbMap, query string, args ...interface{}) int64 {
return i64 return i64
} }
func selectNullInt(dbmap *gorp.DbMap, query string, args ...interface{}) sql.NullInt64 { func selectNullInt(dbmap *DbMap, query string, args ...interface{}) sql.NullInt64 {
i64, err := gorp.SelectNullInt(dbmap, query, args...) i64, err := SelectNullInt(dbmap, query, args...)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@@ -2343,8 +2318,8 @@ func selectNullInt(dbmap *gorp.DbMap, query string, args ...interface{}) sql.Nul
return i64 return i64
} }
func selectFloat(dbmap *gorp.DbMap, query string, args ...interface{}) float64 { func selectFloat(dbmap *DbMap, query string, args ...interface{}) float64 {
f64, err := gorp.SelectFloat(dbmap, query, args...) f64, err := SelectFloat(dbmap, query, args...)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@@ -2352,8 +2327,8 @@ func selectFloat(dbmap *gorp.DbMap, query string, args ...interface{}) float64 {
return f64 return f64
} }
func selectNullFloat(dbmap *gorp.DbMap, query string, args ...interface{}) sql.NullFloat64 { func selectNullFloat(dbmap *DbMap, query string, args ...interface{}) sql.NullFloat64 {
f64, err := gorp.SelectNullFloat(dbmap, query, args...) f64, err := SelectNullFloat(dbmap, query, args...)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@@ -2361,8 +2336,8 @@ func selectNullFloat(dbmap *gorp.DbMap, query string, args ...interface{}) sql.N
return f64 return f64
} }
func selectStr(dbmap *gorp.DbMap, query string, args ...interface{}) string { func selectStr(dbmap *DbMap, query string, args ...interface{}) string {
s, err := gorp.SelectStr(dbmap, query, args...) s, err := SelectStr(dbmap, query, args...)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@@ -2370,8 +2345,8 @@ func selectStr(dbmap *gorp.DbMap, query string, args ...interface{}) string {
return s return s
} }
func selectNullStr(dbmap *gorp.DbMap, query string, args ...interface{}) sql.NullString { func selectNullStr(dbmap *DbMap, query string, args ...interface{}) sql.NullString {
s, err := gorp.SelectNullStr(dbmap, query, args...) s, err := SelectNullStr(dbmap, query, args...)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@@ -2379,7 +2354,7 @@ func selectNullStr(dbmap *gorp.DbMap, query string, args ...interface{}) sql.Nul
return s return s
} }
func rawExec(dbmap *gorp.DbMap, query string, args ...interface{}) sql.Result { func _rawexec(dbmap *DbMap, query string, args ...interface{}) sql.Result {
res, err := dbmap.Exec(query, args...) res, err := dbmap.Exec(query, args...)
if err != nil { if err != nil {
panic(err) panic(err)
@@ -2387,26 +2362,10 @@ func rawExec(dbmap *gorp.DbMap, query string, args ...interface{}) sql.Result {
return res return res
} }
func rawSelect(dbmap *gorp.DbMap, i interface{}, query string, args ...interface{}) []interface{} { func _rawselect(dbmap *DbMap, i interface{}, query string, args ...interface{}) []interface{} {
list, err := dbmap.Select(i, query, args...) list, err := dbmap.Select(i, query, args...)
if err != nil { if err != nil {
panic(err) panic(err)
} }
return list return list
} }
func tableName(dbmap *gorp.DbMap, i interface{}) string {
t := reflect.TypeOf(i)
if table, err := dbmap.TableFor(t, false); table != nil && err == nil {
return dbmap.Dialect.QuoteField(table.TableName)
}
return t.Name()
}
func columnName(dbmap *gorp.DbMap, i interface{}, fieldName string) string {
t := reflect.TypeOf(i)
if table, err := dbmap.TableFor(t, false); table != nil && err == nil {
return dbmap.Dialect.QuoteField(table.ColMap(fieldName).ColumnName)
}
return fieldName
}

3
vendor/github.com/go-gorp/gorp/test_all.sh сгенерированный поставляемый
Просмотреть файл

@@ -5,9 +5,6 @@
coveralls_testflags="-v -covermode=count -coverprofile=coverage.out" coveralls_testflags="-v -covermode=count -coverprofile=coverage.out"
echo "Running unit tests"
ginkgo -r -race -randomizeAllSpecs -keepGoing -- -test.run TestGorp
echo "Testing against mysql" echo "Testing against mysql"
export GORP_TEST_DSN=gorptest/gorptest/gorptest export GORP_TEST_DSN=gorptest/gorptest/gorptest
export GORP_TEST_DIALECT=mysql export GORP_TEST_DIALECT=mysql