Skip to content

Commit

Permalink
Merge pull request #85 from doug-martin/v6.1.0-rc
Browse files Browse the repository at this point in the history
V6.1.0 rc
  • Loading branch information
doug-martin authored Jun 9, 2019
2 parents 3f720fc + d42afa7 commit be19636
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 55 deletions.
5 changes: 5 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## v6.1.0

* Handle nil *time.Time Literal [#73](https://github.com/doug-martin/goqu/pull/73) and [#52](https://github.com/doug-martin/goqu/pull/52) - [@RoarkeRandall](https://github.com/RoarkeRandall) and [@quetz](https://github.com/quetz)
* Add ability to change column rename function [#66](https://github.com/doug-martin/goqu/pull/66) - [@blainehansen](https://github.com/blainehansen)

## v6.0.0

* Updated go support to `1.10`, `1.11` and `1.12`
Expand Down
123 changes: 72 additions & 51 deletions README.md

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion crud_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ type (
selectResults []Record
)

var defaultColumnRenameFunction = strings.ToLower
var columnRenameFunction = defaultColumnRenameFunction
func SetColumnRenameFunction(new_function func(string) string) {
columnRenameFunction = new_function
}

var struct_map_cache = make(map[interface{}]columnMap)
var struct_map_cache_lock = sync.Mutex{}

Expand Down Expand Up @@ -362,7 +368,7 @@ func createColumnMap(t reflect.Type) columnMap {
} else {
columnName := f.Tag.Get("db")
if columnName == "" {
columnName = strings.ToLower(f.Name)
columnName = columnRenameFunction(f.Name)
}
cm[columnName] = columnData{
ColumnName: columnName,
Expand Down
41 changes: 41 additions & 0 deletions crud_exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package goqu
import (
"context"
"fmt"
"strings"
"sync"
"testing"

Expand Down Expand Up @@ -262,6 +263,46 @@ func (me *crudExecTest) TestScanStructs() {
assert.Equal(t, noTags[1].Name, "Test2")
}

func (me *crudExecTest) TestColumnRename() {
t := me.T()
// different key names are used each time to circumvent the caching that happens
// it seems like a solid assumption that when people use this feature,
// they would simply set a renaming function once at startup,
// and not change between requests like this
lowerAnon := struct {
FirstLower string
LastLower string
}{}
lowerColumnMap, lowerErr := getColumnMap(&lowerAnon)
assert.NoError(t, lowerErr)

var lowerKeys []string
for key := range lowerColumnMap {
lowerKeys = append(lowerKeys, key)
}
assert.Contains(t, lowerKeys, "firstlower")
assert.Contains(t, lowerKeys, "lastlower")

// changing rename function
SetColumnRenameFunction(strings.ToUpper)

upperAnon := struct {
FirstUpper string
LastUpper string
}{}
upperColumnMap, upperErr := getColumnMap(&upperAnon)
assert.NoError(t, upperErr)

var upperKeys []string
for key := range upperColumnMap {
upperKeys = append(upperKeys, key)
}
assert.Contains(t, upperKeys, "FIRSTUPPER")
assert.Contains(t, upperKeys, "LASTUPPER")

SetColumnRenameFunction(defaultColumnRenameFunction)
}

func (me *crudExecTest) TestScanStruct() {
t := me.T()
mDb, mock, err := sqlmock.New()
Expand Down
3 changes: 3 additions & 0 deletions dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,9 @@ func (me *Dataset) Literal(buf *SqlBuilder, val interface{}) error {
} else if v, ok := val.(time.Time); ok {
return me.adapter.LiteralTime(buf, v)
} else if v, ok := val.(*time.Time); ok {
if v == nil {
return me.adapter.LiteralNil(buf)
}
return me.adapter.LiteralTime(buf, *v)
} else if v, ok := val.(driver.Valuer); ok {
dVal, err := v.Value()
Expand Down
11 changes: 11 additions & 0 deletions dataset_insert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ import (
"time"
)

func (me *datasetTest) TestInsertNullTime() {
t := me.T()
ds1 := From("items")
type item struct {
CreatedAt *time.Time `db:"created_at"`
}
sql, _, err := ds1.ToInsertSql(item{CreatedAt: nil})
assert.NoError(t, err)
assert.Equal(t, sql, `INSERT INTO "items" ("created_at") VALUES (NULL)`)
}

func (me *datasetTest) TestInsertSqlNoReturning() {
t := me.T()
mDb, _, _ := sqlmock.New()
Expand Down
11 changes: 9 additions & 2 deletions default_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,9 +454,16 @@ func (me *DefaultAdapter) OnConflictSql(buf *SqlBuilder, o ConflictExpression) e
if u := o.Updates(); u != nil {
target := u.Target
if me.SupportsConflictTarget() && target != "" {
buf.Write([]byte(" ("))
wrapParens := !strings.HasPrefix(strings.ToLower(target), "on constraint")

buf.Write([]byte(" "))
if wrapParens {
buf.Write([]byte("("))
}
buf.Write([]byte(target))
buf.Write([]byte(")"))
if wrapParens {
buf.Write([]byte(")"))
}
}
if err := me.onConflictDoUpdateSql(buf, *u); err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ services:
PG_URI: 'postgres://postgres:@postgres:5432/goqupostgres?sslmode=disable'
depends_on:
- postgres
- mysql
- mysql
27 changes: 27 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"database/sql"
"fmt"
"regexp"
"strings"

"github.com/doug-martin/goqu/v6"

Expand Down Expand Up @@ -1859,3 +1860,29 @@ func ExampleDataset_Prepared() {
// DELETE FROM "items" WHERE ("id" > ?) [10]

}

func ExampleSetColumnRenameFunction() {
mDb, mock, _ := sqlmock.New()

mock.ExpectQuery(`SELECT "ADDRESS", "NAME" FROM "items" LIMIT 1`).
WithArgs().
WillReturnRows(sqlmock.NewRows([]string{"ADDRESS", "NAME"}).FromCSVString("111 Test Addr,Test1"))

db := goqu.New("db-mock", mDb)

goqu.SetColumnRenameFunction(strings.ToUpper)

anonStruct := struct {
Address string
Name string
}{}
found, _ := db.From("items").ScanStruct(&anonStruct)
fmt.Println(found)
fmt.Println(anonStruct.Address)
fmt.Println(anonStruct.Name)

// Output:
// true
// 111 Test Addr
// Test1
}

0 comments on commit be19636

Please sign in to comment.