Skip to content

Commit

Permalink
v2.0.0
Browse files Browse the repository at this point in the history
* When scanning a struct or slice of structs, the struct(s) will be parsed for the column names to select. #9 - @TechnotronicOz
  • Loading branch information
doug-martin committed May 23, 2015
1 parent 4e8a0b7 commit fc20606
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 18 deletions.
2 changes: 1 addition & 1 deletion HISTORY.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
35 changes: 31 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -358,34 +358,61 @@ 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
}
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)
}
```

* [`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
}
Expand Down
3 changes: 1 addition & 2 deletions adapters/mysql/dataset_adapter_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
// +build mysql
package mysql

import (
"regexp"
"testing"

"github.com/doug-martin/goqu"
"github.com/technotronicoz/testify/assert"
"github.com/stretchr/testify/suite"
"github.com/technotronicoz/testify/assert"
)

type datasetAdapterTest struct {
Expand Down
3 changes: 0 additions & 3 deletions adapters/mysql/mysql_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// +build mysql
package mysql

import (
Expand Down Expand Up @@ -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() {
Expand Down
1 change: 0 additions & 1 deletion adapters/postgres/dataset_adapter_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// +build postgres
package postgres

import (
Expand Down
2 changes: 0 additions & 2 deletions adapters/postgres/postgres_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// +build postgres
package postgres

import (
Expand Down Expand Up @@ -72,7 +71,6 @@ func (me *postgresTest) SetupSuite() {
panic(err)
}
me.db = goqu.New("postgres", db)
// me.db.Logger(logger{})
}

func (me *postgresTest) SetupTest() {
Expand Down
1 change: 0 additions & 1 deletion adapters/sqlite3/dataset_adapter_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// +build sqlite3
package sqlite3

import (
Expand Down
2 changes: 0 additions & 2 deletions adapters/sqlite3/sqlite3_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// +build sqlite3
package sqlite3

import (
Expand Down Expand Up @@ -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() {
Expand Down
6 changes: 4 additions & 2 deletions dataset_actions.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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)
Expand Down

0 comments on commit fc20606

Please sign in to comment.