diff --git a/.travis.yml b/.travis.yml index c8156acd7dda17e3dec9ce3f81319a533b1b9092..1f5e67338ef364031a68f51225566500f5709288 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,10 +1,8 @@ +sudo: false + language: go go: -# - 1.1 // Unsupported, QL fails to compile on go < 1.2. -# - 1.2 // Unsupported on travis because it fails to fetch some resources via HTTPs. -# - 1.2.1 -# - 1.2.2 - 1.3 - 1.3.1 - 1.3.2 @@ -13,11 +11,16 @@ go: - 1.4.1 - 1.4.2 -env: GOARCH=amd64 TEST_HOST=127.0.0.1 UPPERIO_DB_DEBUG=1 +addons: + postgresql: "9.4" + +services: + - mongodb + +env: GOARCH=amd64 TEST_HOST=127.0.0.1 install: - - sudo apt-get install -y bzr make - - mkdir -p $GOPATH/src/upper.io/v2 + - mkdir -p $GOPATH/src/upper.io - mv $PWD $GOPATH/src/upper.io/db - cd $GOPATH/src/upper.io/db - ls -la @@ -28,43 +31,24 @@ install: - go get -v -t -d upper.io/db/postgresql - go get -v -t -d upper.io/db/mongo - go get -v -t -d upper.io/db/ql - - go get -v github.com/pkieltyka/sqlx - - (cd $GOPATH/src/github.com/pkieltyka/sqlx && git pull -a && git checkout ptrs) # temporal fix - - (cp -r $GOPATH/src/github.com/pkieltyka/sqlx/* $GOPATH/src/github.com/jmoiron/sqlx) # temporal fix + - go get -v github.com/jmoiron/sqlx + - (cd $GOPATH/src/github.com/jmoiron/sqlx && git pull -a && git checkout ptrs) # temporal fix - (cd $GOPATH/src/github.com/jmoiron/sqlx && go build -a && go install) - export TRAVIS_BUILD_DIR=$GOPATH/src/upper.io/db -services: - - mongodb - before_script: - mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql - - - rm -rf $HOME/gopath/src/upper.io - - mkdir -p $HOME/gopath/src/upper.io - - mv $HOME/gopath/src/github.com/upper/db $HOME/gopath/src/upper.io - - cd $HOME/gopath/src/upper.io/db - - export TRAVIS_BUILD_DIR=$HOME/gopath/src/upper.io/db - - - cat mysql/_dumps/setup.sql | mysql -uroot - - cat mysql/_dumps/structs.sql | mysql -uupperio -pupperio upperio_tests - - - cat postgresql/_dumps/setup.sql | psql -U postgres - - cat postgresql/_dumps/structs.sql | PGPASSWORD="upperio" psql -U upperio upperio_tests - - - mongo upperio_tests --eval 'db.addUser("upperio", "upperio")' - - (cd mysql/_dumps && make) - (cd postgresql/_dumps && make) - (cd sqlite/_dumps && make) - (cd ql/_dumps && make) - - - cat ql/_dumps/structs.sql | $GOPATH/bin/ql -db ql/_dumps/test.db + - (cd mongo/_dumps && make) script: - cd $GOPATH/src/upper.io/db - - go test upper.io/db/mysql -test.bench=. - - go test upper.io/db/sqlite -test.bench=. - - go test upper.io/db/ql -test.bench=. - - go test upper.io/db/mongo -test.bench=. - - go test -test.v + - go test -v upper.io/db/postgresql + - go test -v upper.io/db/mysql + - go test -v upper.io/db/sqlite + - go test -v upper.io/db/ql + - go test -v upper.io/db/mongo + - go test -v . diff --git a/README.md b/README.md index de32bf6cd68c5c4a8ba1a0c567d89cc003fd18a4..fc13da7f37fd47c4cd59242750e838271278f578 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,9 @@ See the project page, recipes and user documentation at [upper.io/db][1]. ## License -> Copyright (c) 2012-2014 José Carlos Nieto, https://menteslibres.net/xiam +This project is licensed under the terms of the **MIT License**. + +> Copyright (c) 2012-2015 The upper.io/db authors. All rights reserved. > > Permission is hereby granted, free of charge, to any person obtaining > a copy of this software and associated documentation files (the @@ -75,6 +77,11 @@ See the project page, recipes and user documentation at [upper.io/db][1]. > OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION > WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +## Authors + +* [José Carlos Nieto](https://github.com/xiam) +* [Peter Kieltyka](https://github.com/pkieltyka) + [1]: https://upper.io/db [2]: http://golang.org [3]: http://en.wikipedia.org/wiki/Create,_read,_update_and_delete diff --git a/db.go b/db.go index 9807a3aac46e874aab8b215a0dbda660fd8bc557..41dae1df7c3bebfdc2d6599727b104085f70fb63 100644 --- a/db.go +++ b/db.go @@ -1,4 +1,4 @@ -// Copyright (c) 2012-2014 José Carlos Nieto, https://menteslibres.net/xiam +// Copyright (c) 2012-2015 The upper.io/db authors. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -42,7 +42,9 @@ // res = col.Find(db.Cond{"name": "Max"}).Limit(2).Sort("-input") // err = res.All(&people) -package db // import "upper.io/db" +package db + +// import "upper.io/db" // Cond is a map used to define conditions passed to `db.Collection.Find()` and // `db.Result.Where()`. @@ -313,20 +315,6 @@ type ConnectionURL interface { String() string } -// EnvEnableDebug may be used by adapters to determine if the user has enabled -// debugging. -// -// If the user sets the `UPPERIO_DB_DEBUG` environment variable to a -// non-empty value, all generated statements will be printed at runtime to -// the standard logger. -// -// Example: -// -// UPPERIO_DB_DEBUG=1 go test -// -// UPPERIO_DB_DEBUG=1 ./go-program -const EnvEnableDebug = `UPPERIO_DB_DEBUG` - // Marshaler is the interface implemented by structs that can marshal // themselves into data suitable for storage. type Marshaler interface { @@ -360,3 +348,17 @@ type Int64IDSetter interface { type Uint64IDSetter interface { SetID(uint64) error } + +// EnvEnableDebug may be used by adapters to determine if the user has enabled +// debugging. +// +// If the user sets the `UPPERIO_DB_DEBUG` environment variable to a +// non-empty value, all generated statements will be printed at runtime to +// the standard logger. +// +// Example: +// +// UPPERIO_DB_DEBUG=1 go test +// +// UPPERIO_DB_DEBUG=1 ./go-program +const EnvEnableDebug = `UPPERIO_DB_DEBUG` diff --git a/db_test.go b/db_test.go index 9d9023c549ff90dff386ad79b4fb2bfe42571990..557b02d24133ad623ef3b04d522c364cd3abcf28 100644 --- a/db_test.go +++ b/db_test.go @@ -1,4 +1,4 @@ -// Copyright (c) 2012-2014 José Carlos Nieto, https://menteslibres.net/xiam +// Copyright (c) 2012-2015 The upper.io/db authors. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -36,7 +36,6 @@ import ( "gopkg.in/mgo.v2/bson" "upper.io/db" "upper.io/db/mongo" - "upper.io/db/mysql" "upper.io/db/postgresql" "upper.io/db/ql" @@ -84,14 +83,14 @@ func init() { `mongo`: &mongo.ConnectionURL{ Database: `upperio_tests`, Address: db.Host(host), - User: `upperio`, - Password: `upperio`, + User: `upperio_tests`, + Password: `upperio_secret`, }, `mysql`: &mysql.ConnectionURL{ Database: `upperio_tests`, Address: db.Host(host), - User: `upperio`, - Password: `upperio`, + User: `upperio_tests`, + Password: `upperio_secret`, Options: map[string]string{ "parseTime": "true", }, @@ -99,8 +98,8 @@ func init() { `postgresql`: &postgresql.ConnectionURL{ Database: `upperio_tests`, Address: db.Host(host), - User: `upperio`, - Password: `upperio`, + User: `upperio_tests`, + Password: `upperio_secret`, Options: map[string]string{ "timezone": "UTC", }, @@ -847,14 +846,70 @@ func TestFibonacci(t *testing.T) { t.Fatalf(`%s: Unexpected count %d.`, wrapper, total) } + // Find() with no arguments. res = col.Find() - total, err = res.Count() if total != 6 { t.Fatalf(`%s: Unexpected count %d.`, wrapper, total) } + // Find() with empty db.Cond. + res1 := col.Find(db.Cond{}) + total, err = res1.Count() + + if total != 6 { + t.Fatalf(`%s: Unexpected count %d.`, wrapper, total) + } + + // Find() with empty expression + res1b := col.Find(db.Or{db.And{db.Cond{}, db.Cond{}}, db.Or{db.Cond{}}}) + total, err = res1b.Count() + + if total != 6 { + t.Fatalf(`%s: Unexpected count %d.`, wrapper, total) + } + + // Find() with explicit IS NULL + res2 := col.Find(db.Cond{"input IS": nil}) + total, err = res2.Count() + + if total != 0 { + t.Fatalf(`%s: Unexpected count %d.`, wrapper, total) + } + + // Find() with implicit IS NULL + res2a := col.Find(db.Cond{"input": nil}) + total, err = res2a.Count() + + if total != 0 { + t.Fatalf(`%s: Unexpected count %d.`, wrapper, total) + } + + // Find() with explicit = NULL + res2b := col.Find(db.Cond{"input =": nil}) + total, err = res2b.Count() + + if total != 0 { + t.Fatalf(`%s: Unexpected count %d.`, wrapper, total) + } + + // Find() with implicit IN + res3 := col.Find(db.Cond{"input": []int{1, 2, 3, 4}}) + total, err = res3.Count() + + if total != 3 { + t.Fatalf(`%s: Unexpected count %d.`, wrapper, total) + } + + // Find() with implicit NOT IN + res3a := col.Find(db.Cond{"input NOT IN": []int{1, 2, 3, 4}}) + total, err = res3a.Count() + + if total != 3 { + t.Fatalf(`%s: Unexpected count %d.`, wrapper, total) + } + var items []fibonacci err = res.All(&items) @@ -862,6 +917,10 @@ func TestFibonacci(t *testing.T) { t.Fatalf(`%s: %q`, wrapper, err) } + if len(items) != 6 { + t.Fatalf(`Waiting for 6 items.`) + } + for _, item := range items { switch item.Input { case 0: diff --git a/error.go b/error.go index 4110560b965ef194ec1cb14ce6995bac3242134b..52d069705ce004c866b6917073714fa566b13e10 100644 --- a/error.go +++ b/error.go @@ -1,4 +1,4 @@ -// Copyright (c) 2012-2014 José Carlos Nieto, https://menteslibres.net/xiam +// Copyright (c) 2012-2015 The upper.io/db authors. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/mongo/_dumps/Makefile b/mongo/_dumps/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..4089f4910436301990f64d64d86fb48c27f73d1c --- /dev/null +++ b/mongo/_dumps/Makefile @@ -0,0 +1,4 @@ +all: setup + +setup: + mongo upperio_tests --eval 'db.addUser("upperio_tests", "upperio_secret")' diff --git a/mongo/collection.go b/mongo/collection.go index df339a014f64098693ec87c89b2dcc52023cd5a5..a4adadddc9b1030f1d47893123ee34a2c3b47cee 100644 --- a/mongo/collection.go +++ b/mongo/collection.go @@ -1,4 +1,4 @@ -// Copyright (c) 2012-2014 José Carlos Nieto, https://menteslibres.net/xiam +// Copyright (c) 2012-2015 The upper.io/db authors. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/mongo/connection.go b/mongo/connection.go index cb4917a0346170312d78c04f34063d20d321b4ae..c4f284e684411e9edbd742a66cef746288e11a17 100644 --- a/mongo/connection.go +++ b/mongo/connection.go @@ -1,4 +1,4 @@ -// Copyright (c) 2012-2014 José Carlos Nieto, https://menteslibres.net/xiam +// Copyright (c) 2012-2015 The upper.io/db authors. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/mongo/connection_test.go b/mongo/connection_test.go index 3e3f4328d0495e2e74d43c2f5a71ba374948288a..313e40af4b244b87d3162e3e85d54577a2991f3a 100644 --- a/mongo/connection_test.go +++ b/mongo/connection_test.go @@ -1,4 +1,4 @@ -// Copyright (c) 2012-2014 José Carlos Nieto, https://menteslibres.net/xiam +// Copyright (c) 2012-2015 The upper.io/db authors. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/mongo/database.go b/mongo/database.go index 3e473c15db12ff855615f055677743076b4483be..8d2da72453502300a55704c58f0def69f2590874 100644 --- a/mongo/database.go +++ b/mongo/database.go @@ -1,4 +1,4 @@ -// Copyright (c) 2012-2014 José Carlos Nieto, https://menteslibres.net/xiam +// Copyright (c) 2012-2015 The upper.io/db authors. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/mongo/database_test.go b/mongo/database_test.go index 91e316f504f04e043eda80c16d2a18a7d4b4be1c..fe847c767d2ea134b6b78865f180613b82bc3156 100644 --- a/mongo/database_test.go +++ b/mongo/database_test.go @@ -1,4 +1,4 @@ -// Copyright (c) 2012-2014 José Carlos Nieto, https://menteslibres.net/xiam +// Copyright (c) 2012-2015 The upper.io/db authors. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -33,7 +33,6 @@ import ( "gopkg.in/mgo.v2" "gopkg.in/mgo.v2/bson" - "menteslibres.net/gosexy/to" "upper.io/db" ) @@ -133,11 +132,6 @@ func init() { settings.Address = db.ParseAddress(host) } -// Enabling outputting some information to stdout, useful for development. -func TestEnableDebug(t *testing.T) { - os.Setenv(db.EnvEnableDebug, "TRUE") -} - // Trying to open an empty datasource, it must succeed (mongo). /* func TestOpenFailed(t *testing.T) { @@ -543,7 +537,7 @@ func TestResultFetch(t *testing.T) { if row_m["_id"].(bson.ObjectId).Valid() != true { t.Fatalf("Expecting a valid bson.ObjectId.") } - if to.String(row_m["name"]) == "" { + if name, ok := row_m["name"].(string); !ok || name == "" { t.Fatalf("Expecting a name.") } } else { @@ -967,11 +961,6 @@ func TestDataTypes(t *testing.T) { } } -// We are going to benchmark the engine, so this is no longed needed. -func TestDisableDebug(t *testing.T) { - os.Setenv(db.EnvEnableDebug, "") -} - // Benchmarking raw mgo queries. func BenchmarkAppendRaw(b *testing.B) { sess, err := db.Open(Adapter, settings) diff --git a/mongo/id.go b/mongo/id.go index 18076bd4cb09033b793c8c195e8957a6afcdaa09..c8e51f380b61d8ebd481c7c6ee9d781b848c3026 100644 --- a/mongo/id.go +++ b/mongo/id.go @@ -1,4 +1,4 @@ -// Copyright (c) 2012-2014 José Carlos Nieto, https://menteslibres.net/xiam +// Copyright (c) 2012-2015 The upper.io/db authors. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/mongo/result.go b/mongo/result.go index 8e6269b3575a7863f5a29c74a6a5ff0ccef24049..8d85e92464b33995763ce79a2f6cb130fc795498 100644 --- a/mongo/result.go +++ b/mongo/result.go @@ -1,5 +1,5 @@ /* - Copyright (c) 2014 José Carlos Nieto, https://menteslibres.net/xiam + Copyright (c) 2014 The upper.io/db authors. All rights reserved. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the diff --git a/mysql/_dumps/setup.sql b/mysql/_dumps/setup.sql index 33ac9d2912ee2af7a28ca6dde1534521fd34a987..30e41fe67e2aa5057adbb934398d0c4fda0977c6 100644 --- a/mysql/_dumps/setup.sql +++ b/mysql/_dumps/setup.sql @@ -2,5 +2,5 @@ DROP DATABASE IF EXISTS upperio_tests; CREATE DATABASE upperio_tests; -GRANT ALL PRIVILEGES ON upperio_tests.* to upperio@localhost IDENTIFIED BY 'upperio'; -GRANT ALL PRIVILEGES ON upperio_tests.* to upperio@'10.1.2.1' IDENTIFIED BY 'upperio'; +GRANT ALL PRIVILEGES ON upperio_tests.* to upperio_tests@localhost IDENTIFIED BY 'upperio_secret'; +GRANT ALL PRIVILEGES ON upperio_tests.* to upperio_tests@'10.1.2.1' IDENTIFIED BY 'upperio_secret'; diff --git a/mysql/collection.go b/mysql/collection.go index f8aa3acc0e937fac7c9258af8d6aeafa6a71cee6..fb5cccd459f578e2ac4810689fb0b96a98c2e48c 100644 --- a/mysql/collection.go +++ b/mysql/collection.go @@ -1,4 +1,4 @@ -// Copyright (c) 2012-2015 José Carlos Nieto, https://menteslibres.net/xiam +// Copyright (c) 2012-2015 The upper.io/db authors. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/mysql/connection.go b/mysql/connection.go index faa3b4cfa8d925a5fcbb70aa23011c5ecdb67b29..e9264c905946dd2f83780f93034d36ddeed657b8 100644 --- a/mysql/connection.go +++ b/mysql/connection.go @@ -1,4 +1,4 @@ -// Copyright (c) 2012-2014 José Carlos Nieto, https://menteslibres.net/xiam +// Copyright (c) 2012-2015 The upper.io/db authors. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/mysql/connection_test.go b/mysql/connection_test.go index 1e833d1e674eb5f039f405df06e52e49416db8ef..e306aa9a519e3efab197d21fc2a110865fcf7d39 100644 --- a/mysql/connection_test.go +++ b/mysql/connection_test.go @@ -1,4 +1,4 @@ -// Copyright (c) 2012-2014 José Carlos Nieto, https://menteslibres.net/xiam +// Copyright (c) 2012-2015 The upper.io/db authors. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/mysql/database.go b/mysql/database.go index c936286e9a9e3a363a0b9038aae6c698e5792c3e..960bc15c20b369810010bc52a7b9811801daaede 100644 --- a/mysql/database.go +++ b/mysql/database.go @@ -1,4 +1,4 @@ -// Copyright (c) 2012-2015 José Carlos Nieto, https://menteslibres.net/xiam +// Copyright (c) 2012-2015 The upper.io/db authors. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/mysql/database_test.go b/mysql/database_test.go index 77763f3e8bbefb519f39dcbee513b1ddc50eedca..fa5d9adade0a0fd82ac2228b27dd517218dcf5aa 100644 --- a/mysql/database_test.go +++ b/mysql/database_test.go @@ -1,4 +1,4 @@ -// Copyright (c) 2012-2015 José Carlos Nieto, https://menteslibres.net/xiam +// Copyright (c) 2012-2015 The upper.io/db authors. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -25,6 +25,7 @@ import ( "database/sql" "errors" "fmt" + "log" "math/rand" "os" "reflect" @@ -34,7 +35,6 @@ import ( "time" "github.com/jmoiron/sqlx" - "menteslibres.net/gosexy/to" "upper.io/db" "upper.io/db/util/sqlutil" ) @@ -155,12 +155,6 @@ func init() { settings.Address = db.ParseAddress(host) } -// Loggin some information to stdout (like the SQL query and its -// arguments), useful for development. -func TestEnableDebug(t *testing.T) { - os.Setenv(db.EnvEnableDebug, "TRUE") -} - // Attempts to open an empty datasource. func TestOpenFailed(t *testing.T) { var err error @@ -337,7 +331,7 @@ func TestAppend(t *testing.T) { t.Fatal(err) } - if to.Int64(id) == 0 { + if pk, ok := id.(int64); !ok || pk == 0 { t.Fatalf("Expecting an ID.") } @@ -352,7 +346,7 @@ func TestAppend(t *testing.T) { t.Fatal(err) } - if to.Int64(id) == 0 { + if pk, ok := id.(int64); !ok || pk == 0 { t.Fatalf("Expecting an ID.") } @@ -367,7 +361,7 @@ func TestAppend(t *testing.T) { t.Fatal(err) } - if to.Int64(id) == 0 { + if pk, ok := id.(int64); !ok || pk == 0 { t.Fatalf("Expecting an ID.") } @@ -602,10 +596,11 @@ func TestResultFetch(t *testing.T) { } if err == nil { - if to.Int64(rowMap["id"]) == 0 { + log.Println("rowMap[id]:", rowMap["id"], reflect.TypeOf(rowMap["id"])) + if pk, ok := rowMap["id"].(int64); !ok || pk == 0 { t.Fatalf("Expecting a not null ID.") } - if to.String(rowMap["name"]) == "" { + if name, ok := rowMap["name"].([]byte); !ok || string(name) == "" { t.Fatalf("Expecting a name.") } } else { @@ -657,7 +652,7 @@ func TestResultFetch(t *testing.T) { } for _, singleRowMap := range allRowsMap { - if to.Int64(singleRowMap["id"]) == 0 { + if pk, ok := singleRowMap["id"].(int64); !ok || pk == 0 { t.Fatalf("Expecting a not null ID.") } } diff --git a/mysql/template.go b/mysql/template.go index 1b2d21dab87f67e0f0cc87ed872d9ba9f37b1112..20db212c8672c723b968ee549b8c65c9cf5daeba 100644 --- a/mysql/template.go +++ b/mysql/template.go @@ -1,4 +1,4 @@ -// Copyright (c) 2012-2015 José Carlos Nieto, https://menteslibres.net/xiam +// Copyright (c) 2012-2015 The upper.io/db authors. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/net.go b/net.go index e779d48f988e2e48f97e7824aef4e1b6c31afda6..19b12bf81229722e671980bdf3326fd77b4641bc 100644 --- a/net.go +++ b/net.go @@ -1,4 +1,4 @@ -// Copyright (c) 2012-2014 José Carlos Nieto, https://menteslibres.net/xiam +// Copyright (c) 2012-2015 The upper.io/db authors. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/postgresql/_dumps/setup.sql b/postgresql/_dumps/setup.sql index 5b275a3ff74394dc1960ac0a88405e6084033cea..dbfdb87d22a585c54b9664a784b6b6256c4a7268 100644 --- a/postgresql/_dumps/setup.sql +++ b/postgresql/_dumps/setup.sql @@ -1,6 +1,9 @@ -CREATE USER upperio WITH PASSWORD 'upperio'; +DROP DATABASE IF EXISTS upperio_tests; -CREATE DATABASE upperio_tests ENCODING 'UTF-8' LC_COLLATE='en_US.UTF-8' LC_CTYPE='en_US.UTF-8' TEMPLATE template0; +DROP ROLE IF EXISTS upperio_tests; + +CREATE USER upperio_tests WITH PASSWORD 'upperio_secret'; -GRANT ALL PRIVILEGES ON DATABASE upperio_tests TO upperio; +CREATE DATABASE upperio_tests ENCODING 'UTF-8' LC_COLLATE='en_US.UTF-8' LC_CTYPE='en_US.UTF-8' TEMPLATE template0; +GRANT ALL PRIVILEGES ON DATABASE upperio_tests TO upperio_tests; diff --git a/postgresql/collection.go b/postgresql/collection.go index c52e6ef9e24f4bd98ee27923e8211e3e2f7a0811..2d41d9dcc9690802ccb71e5f48ecf3ff2b51ca4d 100644 --- a/postgresql/collection.go +++ b/postgresql/collection.go @@ -1,4 +1,4 @@ -// Copyright (c) 2012-2015 José Carlos Nieto, https://menteslibres.net/xiam +// Copyright (c) 2012-2015 The upper.io/db authors. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/postgresql/connection.go b/postgresql/connection.go index 451383ce96af9a707eb03d9e354405127da4aeca..a314d0e2f9b2dcf2d2b2d3dc345930b21ca19a75 100644 --- a/postgresql/connection.go +++ b/postgresql/connection.go @@ -1,4 +1,4 @@ -// Copyright (c) 2012-2015 José Carlos Nieto, https://menteslibres.net/xiam +// Copyright (c) 2012-2015 The upper.io/db authors. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/postgresql/connection_test.go b/postgresql/connection_test.go index dd6cc0d1c9aae2b22abae7d8c493b886bc9f74c8..f2210c192250b65c5b771ad41096f390de04ea1f 100644 --- a/postgresql/connection_test.go +++ b/postgresql/connection_test.go @@ -1,4 +1,4 @@ -// Copyright (c) 2012-2015 José Carlos Nieto, https://menteslibres.net/xiam +// Copyright (c) 2012-2015 The upper.io/db authors. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/postgresql/database.go b/postgresql/database.go index 5a6edb4cc2fda62265df8ce34fa295125fdc220a..0872939404746dd009cf465b014af5b5114960dc 100644 --- a/postgresql/database.go +++ b/postgresql/database.go @@ -1,4 +1,4 @@ -// Copyright (c) 2012-2015 José Carlos Nieto, https://menteslibres.net/xiam +// Copyright (c) 2012-2015 The upper.io/db authors. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/postgresql/database_test.go b/postgresql/database_test.go index fa197466eb5763aee254d662af8a80639453afb3..95a22c985e009af3d7f92f0fa2202c2181fa880a 100644 --- a/postgresql/database_test.go +++ b/postgresql/database_test.go @@ -1,4 +1,4 @@ -// Copyright (c) 2012-2015 José Carlos Nieto, https://menteslibres.net/xiam +// Copyright (c) 2012-2015 The upper.io/db authors. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -25,6 +25,7 @@ import ( "database/sql" "errors" "fmt" + "math/rand" "os" "reflect" "strconv" @@ -32,8 +33,6 @@ import ( "testing" "time" - "math/rand" - "github.com/jmoiron/sqlx" "upper.io/db" "upper.io/db/util/sqlutil" diff --git a/ql/collection.go b/ql/collection.go index 8fad6cf3066a201589afcc992c3cd06c8657bb4c..b6b5fcf3007a58cb5a9cdc418c7782cf9defef98 100644 --- a/ql/collection.go +++ b/ql/collection.go @@ -1,4 +1,4 @@ -// Copyright (c) 2012-2015 José Carlos Nieto, https://menteslibres.net/xiam +// Copyright (c) 2012-2015 The upper.io/db authors. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/ql/connection.go b/ql/connection.go index 389382c93f33c5dc350659bd35625a64ad5fb3aa..82e2f7242e09587b55ea6a9948ed202ddb5bf14f 100644 --- a/ql/connection.go +++ b/ql/connection.go @@ -1,4 +1,4 @@ -// Copyright (c) 2012-2014 José Carlos Nieto, https://menteslibres.net/xiam +// Copyright (c) 2012-2015 The upper.io/db authors. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/ql/connection_test.go b/ql/connection_test.go index 63e43561d09e9fbc3cb39c973c8f0fb3ba665bcc..bc9bf66743ead044673b56db3b33565f1d04df72 100644 --- a/ql/connection_test.go +++ b/ql/connection_test.go @@ -1,4 +1,4 @@ -// Copyright (c) 2012-2014 José Carlos Nieto, https://menteslibres.net/xiam +// Copyright (c) 2012-2015 The upper.io/db authors. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/ql/database.go b/ql/database.go index e8d5ffd215e8ab510bfa36e10f975c36051f9bdf..949d79386b65c0aa250fd2ae49b90e9b46267140 100644 --- a/ql/database.go +++ b/ql/database.go @@ -1,4 +1,4 @@ -// Copyright (c) 2012-2015 José Carlos Nieto, https://menteslibres.net/xiam +// Copyright (c) 2012-2015 The upper.io/db authors. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/ql/database_test.go b/ql/database_test.go index 432d20b85f2223ff415300bccec1a854c2e2bc16..057a5ba896e5d849bc938feb44dc8062fd812e71 100644 --- a/ql/database_test.go +++ b/ql/database_test.go @@ -1,4 +1,4 @@ -// Copyright (c) 2012-2014 José Carlos Nieto, https://menteslibres.net/xiam +// Copyright (c) 2012-2015 The upper.io/db authors. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -31,7 +31,6 @@ package ql import ( "os" - "menteslibres.net/gosexy/to" //"reflect" //"errors" "math/rand" @@ -131,12 +130,6 @@ func init() { } } -// Loggin some information to stdout (like the SQL query and its -// arguments), useful for development. -func TestEnableDebug(t *testing.T) { - os.Setenv(db.EnvEnableDebug, "TRUE") -} - // Attempts to open an empty datasource. func TestOpenFailed(t *testing.T) { var err error @@ -255,7 +248,7 @@ func TestAppend(t *testing.T) { t.Fatal(err) } - if to.Int64(id) == 0 { + if pk, ok := id.(int64); !ok || pk == 0 { t.Fatalf("Expecting an ID.") } @@ -270,7 +263,7 @@ func TestAppend(t *testing.T) { t.Fatal(err) } - if to.Int64(id) == 0 { + if pk, ok := id.(int64); !ok || pk == 0 { t.Fatalf("Expecting an ID.") } @@ -285,7 +278,7 @@ func TestAppend(t *testing.T) { t.Fatal(err) } - if to.Int64(id) == 0 { + if pk, ok := id.(int64); !ok || pk == 0 { t.Fatalf("Expecting an ID.") } @@ -417,10 +410,10 @@ func TestResultFetch(t *testing.T) { } if err == nil { - if to.Int64(rowMap["id"]) == 0 { + if pk, ok := rowMap["id"].(int64); !ok || pk == 0 { t.Fatalf("Expecting a not null ID.") } - if to.String(rowMap["name"]) == "" { + if name, ok := rowMap["name"].([]byte); !ok || string(name) == "" { t.Fatalf("Expecting a name.") } } else { @@ -472,7 +465,7 @@ func TestResultFetch(t *testing.T) { } for _, singleRowMap := range allRowsMap { - if to.Int64(singleRowMap["id"]) == 0 { + if pk, ok := singleRowMap["id"].(int64); !ok || pk == 0 { t.Fatalf("Expecting a not null ID.") } } diff --git a/ql/template.go b/ql/template.go index 61fc47c044905a107393e34539f62e3f19e1cebc..20e5dd1de91eac3da13b0cbb403948ee059ac4fc 100644 --- a/ql/template.go +++ b/ql/template.go @@ -1,4 +1,4 @@ -// Copyright (c) 2012-2014 José Carlos Nieto, https://menteslibres.net/xiam +// Copyright (c) 2012-2015 The upper.io/db authors. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/settings.go b/settings.go index b44a2590d0500c31fe72c85ba9241cd2e85d9376..1935259e5b5c9c78fb4fb21d8ec061393533d9f8 100644 --- a/settings.go +++ b/settings.go @@ -1,4 +1,4 @@ -// Copyright (c) 2012-2014 José Carlos Nieto, https://menteslibres.net/xiam +// Copyright (c) 2012-2015 The upper.io/db authors. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/sqlite/_dumps/Makefile b/sqlite/_dumps/Makefile index 8102fbfdc684bbf3656a75d75528b5d916bd590c..11c707522b5e6adb4169d7d622296b42f9cbb40c 100644 --- a/sqlite/_dumps/Makefile +++ b/sqlite/_dumps/Makefile @@ -1,4 +1,6 @@ -load: clean +all: reset-db + +reset-db: clean cat structs.sql | sqlite3 gotest.sqlite3.db clean: diff --git a/sqlite/collection.go b/sqlite/collection.go index 676f8fef51dc6e54202d4120217324a218be72cb..59442646ce2584454989604263a1540a8656f01f 100644 --- a/sqlite/collection.go +++ b/sqlite/collection.go @@ -1,4 +1,4 @@ -// Copyright (c) 2012-2015 José Carlos Nieto, https://menteslibres.net/xiam +// Copyright (c) 2012-2015 The upper.io/db authors. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/sqlite/connection.go b/sqlite/connection.go index 2a34c4c1ef3c2802fcbd31325ca4263b5cf2edbe..a81b0d1df667a4deb4ce9a4d629b6fa772421fa3 100644 --- a/sqlite/connection.go +++ b/sqlite/connection.go @@ -1,4 +1,4 @@ -// Copyright (c) 2012-2014 José Carlos Nieto, https://menteslibres.net/xiam +// Copyright (c) 2012-2015 The upper.io/db authors. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/sqlite/connection_test.go b/sqlite/connection_test.go index cd2676b57fe355af4c73d9d9a8b5906777b24790..76b478677545502ff2c40364a1cde447c305f803 100644 --- a/sqlite/connection_test.go +++ b/sqlite/connection_test.go @@ -1,4 +1,4 @@ -// Copyright (c) 2012-2014 José Carlos Nieto, https://menteslibres.net/xiam +// Copyright (c) 2012-2015 The upper.io/db authors. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/sqlite/database.go b/sqlite/database.go index ecfa0d60232044662fc883e699c4cace1e73dce5..c75247d364220f541c16673e62f4ec2306e181e6 100644 --- a/sqlite/database.go +++ b/sqlite/database.go @@ -1,4 +1,4 @@ -// Copyright (c) 2012-2014 José Carlos Nieto, https://menteslibres.net/xiam +// Copyright (c) 2012-2015 The upper.io/db authors. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/sqlite/database_test.go b/sqlite/database_test.go index 4edf13cb78c08c1fbc15d3211ca0020f72d6343c..6d616aef1799e00a24d224294df3e55ea5913f7d 100644 --- a/sqlite/database_test.go +++ b/sqlite/database_test.go @@ -1,4 +1,4 @@ -// Copyright (c) 2012-2014 José Carlos Nieto, https://menteslibres.net/xiam +// Copyright (c) 2012-2015 The upper.io/db authors. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -33,7 +33,6 @@ import ( "errors" "fmt" "math/rand" - "os" "reflect" "strconv" "strings" @@ -41,7 +40,6 @@ import ( "time" "github.com/jmoiron/sqlx" - "menteslibres.net/gosexy/to" "upper.io/db" "upper.io/db/util/sqlutil" ) @@ -144,12 +142,6 @@ func init() { } } -// Loggin some information to stdout (like the SQL query and its -// arguments), useful for development. -func TestEnableDebug(t *testing.T) { - os.Setenv(db.EnvEnableDebug, "TRUE") -} - // Attempts to open an empty datasource. func TestOpenFailed(t *testing.T) { var err error @@ -267,7 +259,7 @@ func TestAppend(t *testing.T) { t.Fatal(err) } - if to.Int64(id) == 0 { + if pk, ok := id.(int64); !ok || pk == 0 { t.Fatalf("Expecting an ID.") } @@ -282,7 +274,7 @@ func TestAppend(t *testing.T) { t.Fatal(err) } - if to.Int64(id) == 0 { + if pk, ok := id.(int64); !ok || pk == 0 { t.Fatalf("Expecting an ID.") } @@ -297,7 +289,7 @@ func TestAppend(t *testing.T) { t.Fatal(err) } - if to.Int64(id) == 0 { + if pk, ok := id.(int64); !ok || pk == 0 { t.Fatalf("Expecting an ID.") } @@ -535,10 +527,10 @@ func TestResultFetch(t *testing.T) { } if err == nil { - if to.Int64(rowMap["id"]) == 0 { + if pk, ok := rowMap["id"].(int64); !ok || pk == 0 { t.Fatalf("Expecting a not null ID.") } - if to.String(rowMap["name"]) == "" { + if name, ok := rowMap["name"].([]byte); !ok || string(name) == "" { t.Fatalf("Expecting a name.") } } else { @@ -590,7 +582,7 @@ func TestResultFetch(t *testing.T) { } for _, singleRowMap := range allRowsMap { - if to.Int64(singleRowMap["id"]) == 0 { + if pk, ok := singleRowMap["id"].(int64); !ok || pk == 0 { t.Fatalf("Expecting a not null ID.") } } diff --git a/sqlite/template.go b/sqlite/template.go index abea16614305df659ce23b2b33a84df02a01da90..45eeeb970596bfaf6dfea21d9dc7961f20515c0c 100644 --- a/sqlite/template.go +++ b/sqlite/template.go @@ -1,4 +1,4 @@ -// Copyright (c) 2012-2014 José Carlos Nieto, https://menteslibres.net/xiam +// Copyright (c) 2012-2015 The upper.io/db authors. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/util/schema/schema.go b/util/schema/schema.go index 99d310b42e9e6394dd72173dd68be97ce9d97a00..762720069653b11cb2650c2590fd647beacdf322 100644 --- a/util/schema/schema.go +++ b/util/schema/schema.go @@ -1,4 +1,4 @@ -// Copyright (c) 2012-2014 José Carlos Nieto, https://menteslibres.net/xiam +// Copyright (c) 2012-2015 The upper.io/db authors. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/util/sqlutil/convert.go b/util/sqlutil/convert.go index d7bc224c8e75bf632986ed8ea87513644dc238e0..260a6e5396e38cf53846b18ec96d5b8f8640490f 100644 --- a/util/sqlutil/convert.go +++ b/util/sqlutil/convert.go @@ -4,13 +4,17 @@ import ( "fmt" "reflect" "strings" + "upper.io/db" "upper.io/db/util/sqlgen" ) var ( - sqlPlaceholder = sqlgen.RawValue(`?`) - sqlNull = sqlgen.RawValue(`NULL`) + sqlPlaceholder = sqlgen.RawValue(`?`) + sqlNull = sqlgen.RawValue(`NULL`) + sqlIsOperator = `IS` + sqlInOperator = `IN` + sqlDefaultOperator = `=` ) type TemplateWithUtils struct { @@ -30,6 +34,9 @@ func (tu *TemplateWithUtils) ToWhereWithArguments(term interface{}) (where sqlge case []interface{}: for i := range t { w, v := tu.ToWhereWithArguments(t[i]) + if len(w.Conditions) == 0 { + continue + } args = append(args, v...) where.Conditions = append(where.Conditions, w.Conditions...) } @@ -37,20 +44,30 @@ func (tu *TemplateWithUtils) ToWhereWithArguments(term interface{}) (where sqlge case db.And: var op sqlgen.And for i := range t { - k, v := tu.ToWhereWithArguments(t[i]) + w, v := tu.ToWhereWithArguments(t[i]) + if len(w.Conditions) == 0 { + continue + } args = append(args, v...) - op.Conditions = append(op.Conditions, k.Conditions...) + op.Conditions = append(op.Conditions, w.Conditions...) + } + if len(op.Conditions) > 0 { + where.Conditions = append(where.Conditions, &op) } - where.Conditions = append(where.Conditions, &op) return case db.Or: var op sqlgen.Or for i := range t { w, v := tu.ToWhereWithArguments(t[i]) + if len(w.Conditions) == 0 { + continue + } args = append(args, v...) op.Conditions = append(op.Conditions, w.Conditions...) } - where.Conditions = append(where.Conditions, &op) + if len(op.Conditions) > 0 { + where.Conditions = append(where.Conditions, &op) + } return case db.Raw: if s, ok := t.Value.(string); ok { @@ -122,8 +139,6 @@ func (tu *TemplateWithUtils) ToColumnValues(cond db.Cond) (ToColumnValues sqlgen if len(chunks) > 1 { columnValue.Operator = chunks[1] - } else { - columnValue.Operator = tu.DefaultOperator } switch value := value.(type) { @@ -143,14 +158,19 @@ func (tu *TemplateWithUtils) ToColumnValues(cond db.Cond) (ToColumnValues sqlgen default: v := tu.ToInterfaceArguments(value) - l := len(v) - if v == nil || l == 0 { + if v == nil { // Nil value given. columnValue.Value = sqlNull + if columnValue.Operator == "" { + columnValue.Operator = sqlIsOperator + } } else { - if l > 1 { + if len(v) > 1 { // Array value given. columnValue.Value = sqlgen.RawValue(fmt.Sprintf(`(?%s)`, strings.Repeat(`, ?`, len(v)-1))) + if columnValue.Operator == "" { + columnValue.Operator = sqlInOperator + } } else { // Single value given. columnValue.Value = sqlPlaceholder @@ -159,6 +179,15 @@ func (tu *TemplateWithUtils) ToColumnValues(cond db.Cond) (ToColumnValues sqlgen } } + // Using guessed operator if no operator was given. + if columnValue.Operator == "" { + if tu.DefaultOperator != "" { + columnValue.Operator = tu.DefaultOperator + } else { + columnValue.Operator = sqlDefaultOperator + } + } + ToColumnValues.ColumnValues = append(ToColumnValues.ColumnValues, &columnValue) } diff --git a/util/sqlutil/debug.go b/util/sqlutil/debug.go index bd96eeb386fcdcab6f03f095db21eae2fce29bb5..3c997891db4a50bf7d59572c60c2bbbffff31615 100644 --- a/util/sqlutil/debug.go +++ b/util/sqlutil/debug.go @@ -1,4 +1,4 @@ -// Copyright (c) 2012-2014 José Carlos Nieto, https://menteslibres.net/xiam +// Copyright (c) 2012-2015 The upper.io/db authors. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/util/sqlutil/fetch.go b/util/sqlutil/fetch.go index 357beba36ef5ab193d98880fbf48ef37305c5671..a37296286da017fb3a8b1fedd2ca8cba81a65dde 100644 --- a/util/sqlutil/fetch.go +++ b/util/sqlutil/fetch.go @@ -1,4 +1,4 @@ -// Copyright (c) 2012-2014 José Carlos Nieto, https://menteslibres.net/xiam +// Copyright (c) 2012-2015 The upper.io/db authors. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/util/sqlutil/result/result.go b/util/sqlutil/result/result.go index 0da722bfcd42f378b39c0b8052be6bf925f2b42d..64c208a5252cbcabc2fed8b4347cd46e4e5c68f9 100644 --- a/util/sqlutil/result/result.go +++ b/util/sqlutil/result/result.go @@ -1,4 +1,4 @@ -// Copyright (c) 2012-2014 José Carlos Nieto, https://menteslibres.net/xiam +// Copyright (c) 2012-2015 The upper.io/db authors. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/util/sqlutil/tx/tx.go b/util/sqlutil/tx/tx.go index 533c54058f9f855dbc3004e8fad03f5f3022e0bb..18ebbf3c937be3a329cc778c26aa99d471539c7d 100644 --- a/util/sqlutil/tx/tx.go +++ b/util/sqlutil/tx/tx.go @@ -1,4 +1,4 @@ -// Copyright (c) 2012-2015 José Carlos Nieto, https://menteslibres.net/xiam +// Copyright (c) 2012-2015 The upper.io/db authors. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/wrapper.go b/wrapper.go index 058cf6904532451ae4392250a7904f594efb8a39..3e0a8658e240ea76e99975022e3827a2e42db57f 100644 --- a/wrapper.go +++ b/wrapper.go @@ -1,4 +1,4 @@ -// Copyright (c) 2012-2014 José Carlos Nieto, https://menteslibres.net/xiam +// Copyright (c) 2012-2015 The upper.io/db authors. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the