From fc206069b1e2a4a63f6851d8486672b37b4e568f Mon Sep 17 00:00:00 2001 From: Doug Martin Date: Fri, 22 May 2015 23:36:58 -0500 Subject: [PATCH] v2.0.0 * When scanning a struct or slice of structs, the struct(s) will be parsed for the column names to select. #9 - @technotronicoz --- HISTORY.md | 2 +- README.md | 35 ++++++++++++++++++++--- adapters/mysql/dataset_adapter_test.go | 3 +- adapters/mysql/mysql_test.go | 3 -- adapters/postgres/dataset_adapter_test.go | 1 - adapters/postgres/postgres_test.go | 2 -- adapters/sqlite3/dataset_adapter_test.go | 1 - adapters/sqlite3/sqlite3_test.go | 2 -- dataset_actions.go | 6 ++-- 9 files changed, 37 insertions(+), 18 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index b384a133..66043a76 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,4 +1,4 @@ -## v1.0.1 +## v2.0.0 * When scanning a struct or slice of structs, the struct(s) will be parsed for the column names to select. [#9](https://github.com/doug-martin/goqu/pull/9) - [@technotronicoz](https://github.com/TechnotronicOz) diff --git a/README.md b/README.md index b7197191..49baf24c 100644 --- a/README.md +++ b/README.md @@ -358,9 +358,26 @@ goqu also has basic query support through the use of either the Database or the ### Dataset * [`ScanStructs`](http://godoc.org/github.com/doug-martin/goqu#Dataset.ScanStructs) - scans rows into a slice of structs + +**NOTE** [`ScanStructs`](http://godoc.org/github.com/doug-martin/goqu#Dataset.ScanStructs) will only select the columns that can be scanned in to the structs unless you have explicitly selected certain columns. + ```go +type User struct{ + FirstName string `db:"first_name"` + LastName string `db:"last_name"` +} + var users []User -if err := db.From("user").ScanStructs(&users){ +//SELECT "first_name", "last_name" FROM "user"; +if err := db.From("user").ScanStructs(&users); err != nil{ + fmt.Println(err.Error()) + return +} +fmt.Printf("\n%+v", users) + +var users []User +//SELECT "first_name" FROM "user"; +if err := db.From("user").Select("first_name").ScanStructs(&users); err != nil{ fmt.Println(err.Error()) return } @@ -368,16 +385,26 @@ fmt.Printf("\n%+v", users) ``` * [`ScanStruct`](http://godoc.org/github.com/doug-martin/goqu#Dataset.ScanStruct) - scans a row into a slice a struct, returns false if a row wasnt found + +**NOTE** [`ScanStruct`](http://godoc.org/github.com/doug-martin/goqu#Dataset.ScanStruct) will only select the columns that can be scanned in to the struct unless you have explicitly selected certain columns. + ```go + +type User struct{ + FirstName string `db:"first_name"` + LastName string `db:"last_name"` +} + var user User +//SELECT "first_name", "last_name" FROM "user" LIMIT 1; found, err := db.From("user").ScanStruct(&user) if err != nil{ fmt.Println(err.Error()) return } -if !found{ +if !found { fmt.Println("No user found") -}else{ +} else { fmt.Printf("\nFound user: %+v", user) } ``` @@ -385,7 +412,7 @@ if !found{ * [`ScanVals`](http://godoc.org/github.com/doug-martin/goqu#Dataset.ScanVals) - scans a rows of 1 column into a slice of primitive values ```go var ids []int64 -if err := db.From("user").Select("id").ScanVals(&ids){ +if err := db.From("user").Select("id").ScanVals(&ids); err != nil{ fmt.Println(err.Error()) return } diff --git a/adapters/mysql/dataset_adapter_test.go b/adapters/mysql/dataset_adapter_test.go index 30116480..c56fd61d 100644 --- a/adapters/mysql/dataset_adapter_test.go +++ b/adapters/mysql/dataset_adapter_test.go @@ -1,4 +1,3 @@ -// +build mysql package mysql import ( @@ -6,8 +5,8 @@ import ( "testing" "github.com/doug-martin/goqu" - "github.com/technotronicoz/testify/assert" "github.com/stretchr/testify/suite" + "github.com/technotronicoz/testify/assert" ) type datasetAdapterTest struct { diff --git a/adapters/mysql/mysql_test.go b/adapters/mysql/mysql_test.go index 66d4aaf0..5e4c3b0b 100644 --- a/adapters/mysql/mysql_test.go +++ b/adapters/mysql/mysql_test.go @@ -1,4 +1,3 @@ -// +build mysql package mysql import ( @@ -73,13 +72,11 @@ func (me logger) Printf(sql string, args ...interface{}) { } func (me *mysqlTest) SetupSuite() { - fmt.Println(db_uri) db, err := sql.Open("mysql", db_uri) if err != nil { panic(err.Error()) } me.db = goqu.New("mysql", db) - // me.db.Logger(logger{}) } func (me *mysqlTest) SetupTest() { diff --git a/adapters/postgres/dataset_adapter_test.go b/adapters/postgres/dataset_adapter_test.go index ab82ad8e..c5849ca5 100644 --- a/adapters/postgres/dataset_adapter_test.go +++ b/adapters/postgres/dataset_adapter_test.go @@ -1,4 +1,3 @@ -// +build postgres package postgres import ( diff --git a/adapters/postgres/postgres_test.go b/adapters/postgres/postgres_test.go index f6759b4b..0b61eaa4 100644 --- a/adapters/postgres/postgres_test.go +++ b/adapters/postgres/postgres_test.go @@ -1,4 +1,3 @@ -// +build postgres package postgres import ( @@ -72,7 +71,6 @@ func (me *postgresTest) SetupSuite() { panic(err) } me.db = goqu.New("postgres", db) - // me.db.Logger(logger{}) } func (me *postgresTest) SetupTest() { diff --git a/adapters/sqlite3/dataset_adapter_test.go b/adapters/sqlite3/dataset_adapter_test.go index 124b06f3..ab8da7cd 100644 --- a/adapters/sqlite3/dataset_adapter_test.go +++ b/adapters/sqlite3/dataset_adapter_test.go @@ -1,4 +1,3 @@ -// +build sqlite3 package sqlite3 import ( diff --git a/adapters/sqlite3/sqlite3_test.go b/adapters/sqlite3/sqlite3_test.go index 371ec55e..4b75fd49 100644 --- a/adapters/sqlite3/sqlite3_test.go +++ b/adapters/sqlite3/sqlite3_test.go @@ -1,4 +1,3 @@ -// +build sqlite3 package sqlite3 import ( @@ -67,7 +66,6 @@ func (me *sqlite3Test) SetupSuite() { panic(err.Error()) } me.db = goqu.New("sqlite3", db) - // me.db.Logger(logger{}) } func (me *sqlite3Test) SetupTest() { diff --git a/dataset_actions.go b/dataset_actions.go index 19ffa1c4..8e431b61 100644 --- a/dataset_actions.go +++ b/dataset_actions.go @@ -1,8 +1,8 @@ package goqu //Generates the SELECT sql for this dataset and uses Exec#ScanStructs to scan the results into a slice of structs. -// When using this method, ScanStructs will only select the columns that can be scanned in to the struct unless you -// have explicitly selected certain columns. See examples. +// +//ScanStructs will only select the columns that can be scanned in to the struct unless you have explicitly selected certain columns. See examples. // //i: A pointer to a slice of structs func (me *Dataset) ScanStructs(i interface{}) error { @@ -16,6 +16,8 @@ func (me *Dataset) ScanStructs(i interface{}) error { //Generates the SELECT sql for this dataset and uses Exec#ScanStruct to scan the result into a slice of structs // +//ScanStruct will only select the columns that can be scanned in to the struct unless you have explicitly selected certain columns. See examples. +// //i: A pointer to a structs func (me *Dataset) ScanStruct(i interface{}) (bool, error) { ds := me.Limit(1)