Skip to content

Commit

Permalink
Merge pull request #15 from TechnotronicOz/literal_bytes
Browse files Browse the repository at this point in the history
Add literal bytes and update to c2fo testify
  • Loading branch information
doug-martin committed Aug 25, 2015
2 parents d16d278 + 27eee7c commit 47a9715
Show file tree
Hide file tree
Showing 18 changed files with 81 additions and 30 deletions.
4 changes: 4 additions & 0 deletions adapters.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ type (
//
//buf: The current SqlBuilder to write the sql to
LiteralString(buf *SqlBuilder, s string) error
//Generates SQL value for a Slice of Bytes
//
//buf: The current SqlBuilder to write the sql to
LiteralBytes(buf *SqlBuilder, bs []byte) error
//Generates SQL value for a Slice
//
//buf: The current SqlBuilder to write the sql to
Expand Down
4 changes: 2 additions & 2 deletions adapters/mysql/dataset_adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"regexp"
"testing"

"github.com/stretchr/testify/suite"
"github.com/technotronicoz/testify/assert"
"github.com/c2fo/testify/assert"
"github.com/c2fo/testify/suite"
"gopkg.in/doug-martin/goqu.v3"
)

Expand Down
4 changes: 2 additions & 2 deletions adapters/mysql/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"testing"
"time"

"github.com/c2fo/testify/assert"
"github.com/c2fo/testify/suite"
_ "github.com/go-sql-driver/mysql"
"github.com/stretchr/testify/suite"
"github.com/technotronicoz/testify/assert"
"gopkg.in/doug-martin/goqu.v3"
)

Expand Down
4 changes: 2 additions & 2 deletions adapters/postgres/dataset_adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package postgres
import (
"testing"

"github.com/stretchr/testify/suite"
"github.com/technotronicoz/testify/assert"
"github.com/c2fo/testify/suite"
"github.com/c2fo/testify/assert"
"gopkg.in/doug-martin/goqu.v3"
)

Expand Down
4 changes: 2 additions & 2 deletions adapters/postgres/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"testing"
"time"

"github.com/c2fo/testify/assert"
"github.com/c2fo/testify/suite"
"github.com/lib/pq"
"github.com/stretchr/testify/suite"
"github.com/technotronicoz/testify/assert"
"gopkg.in/doug-martin/goqu.v3"
)

Expand Down
4 changes: 2 additions & 2 deletions adapters/sqlite3/dataset_adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"regexp"
"testing"

"github.com/stretchr/testify/suite"
"github.com/technotronicoz/testify/assert"
"github.com/c2fo/testify/suite"
"github.com/c2fo/testify/assert"
"gopkg.in/doug-martin/goqu.v3"
)

Expand Down
4 changes: 2 additions & 2 deletions adapters/sqlite3/sqlite3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"testing"
"time"

"github.com/c2fo/testify/assert"
"github.com/c2fo/testify/suite"
_ "github.com/mattn/go-sqlite3"
"github.com/stretchr/testify/suite"
"github.com/technotronicoz/testify/assert"
"gopkg.in/doug-martin/goqu.v3"
)

Expand Down
4 changes: 2 additions & 2 deletions adapters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package goqu
import (
"testing"

"github.com/stretchr/testify/suite"
"github.com/technotronicoz/testify/assert"
"github.com/c2fo/testify/assert"
"github.com/c2fo/testify/suite"
)

type adapterTest struct {
Expand Down
4 changes: 2 additions & 2 deletions crud_exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"testing"

"github.com/DATA-DOG/go-sqlmock"
"github.com/stretchr/testify/suite"
"github.com/technotronicoz/testify/assert"
"github.com/c2fo/testify/assert"
"github.com/c2fo/testify/suite"
)

type testCrudActionItem struct {
Expand Down
4 changes: 2 additions & 2 deletions database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"testing"

"github.com/DATA-DOG/go-sqlmock"
"github.com/stretchr/testify/suite"
"github.com/technotronicoz/testify/assert"
"github.com/c2fo/testify/assert"
"github.com/c2fo/testify/suite"
)

type testActionItem struct {
Expand Down
4 changes: 2 additions & 2 deletions dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,10 @@ func (me *Dataset) Literal(buf *SqlBuilder, val interface{}) error {
return me.adapter.LiteralFloat(buf, v)
} else if v, ok := val.(string); ok {
return me.adapter.LiteralString(buf, v)
} else if v, ok := val.([]byte); ok {
return me.adapter.LiteralBytes(buf, v)
} else if v, ok := val.(bool); ok {
return me.adapter.LiteralBool(buf, v)
} else if v, ok := val.([]byte); ok {
return me.adapter.LiteralString(buf, string(v))
} else if v, ok := val.(time.Time); ok {
return me.adapter.LiteralTime(buf, v)
} else if v, ok := val.(*time.Time); ok {
Expand Down
2 changes: 1 addition & 1 deletion dataset_actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package goqu

import (
"github.com/DATA-DOG/go-sqlmock"
"github.com/technotronicoz/testify/assert"
"github.com/c2fo/testify/assert"
)

type dsTestActionItem struct {
Expand Down
2 changes: 1 addition & 1 deletion dataset_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package goqu

import (
"github.com/DATA-DOG/go-sqlmock"
"github.com/technotronicoz/testify/assert"
"github.com/c2fo/testify/assert"
)

func (me *datasetTest) TestDeleteSqlNoReturning() {
Expand Down
2 changes: 1 addition & 1 deletion dataset_insert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package goqu

import (
"github.com/DATA-DOG/go-sqlmock"
"github.com/technotronicoz/testify/assert"
"github.com/c2fo/testify/assert"

"time"
)
Expand Down
2 changes: 1 addition & 1 deletion dataset_select_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package goqu

import (
"github.com/technotronicoz/testify/assert"
"github.com/c2fo/testify/assert"
)

func (me *datasetTest) TestSelect() {
Expand Down
30 changes: 27 additions & 3 deletions dataset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"testing"
"time"

"github.com/stretchr/testify/suite"
"github.com/technotronicoz/testify/assert"
"github.com/c2fo/testify/assert"
"github.com/c2fo/testify/suite"
)

type datasetTest struct {
Expand Down Expand Up @@ -190,6 +190,30 @@ func (me *datasetTest) TestLiteralStringTypes() {
assert.Equal(t, buf.String(), "?")
}

func (me *datasetTest) TestLiteralBytesTypes() {
t := me.T()
ds := From("test")
var b string
buf := NewSqlBuilder(false)
assert.NoError(t, ds.Literal(me.Truncate(buf), []byte("Hello")))
assert.Equal(t, buf.Bytes(), []byte("'Hello'"))
//should escape single quotes
assert.NoError(t, ds.Literal(me.Truncate(buf), []byte("hello'")))
assert.Equal(t, buf.Bytes(), []byte("'hello'''"))
assert.NoError(t, ds.Literal(me.Truncate(buf), (&b)))
assert.Equal(t, buf.Bytes(), []byte("''"))
buf = NewSqlBuilder(true)
assert.NoError(t, ds.Literal(me.Truncate(buf), []byte("Hello")))
assert.Equal(t, buf.args, []interface{}{[]byte("Hello")})
assert.Equal(t, buf.Bytes(), []byte("?"))
assert.NoError(t, ds.Literal(me.Truncate(buf), []byte("hello'")))
assert.Equal(t, buf.args, []interface{}{[]byte("hello'")})
assert.Equal(t, buf.Bytes(), []byte("?"))
assert.NoError(t, ds.Literal(me.Truncate(buf), []byte(*(&b))))
assert.Equal(t, buf.args, []interface{}{[]byte(b)})
assert.Equal(t, buf.Bytes(), []byte("?"))
}

func (me *datasetTest) TestLiteralBoolTypes() {
t := me.T()
var b bool
Expand Down Expand Up @@ -262,7 +286,7 @@ func (me *datasetTest) TestLiteralValuer() {

buf = NewSqlBuilder(true)
assert.NoError(t, ds.Literal(me.Truncate(buf), datasetValuerType(10)))
assert.Equal(t, buf.args, []interface{}{"Hello World 10"})
assert.Equal(t, buf.args, []interface{}{[]byte("Hello World 10")})
assert.Equal(t, buf.String(), "?")

}
Expand Down
6 changes: 3 additions & 3 deletions dataset_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"time"

"github.com/DATA-DOG/go-sqlmock"
"github.com/technotronicoz/testify/assert"
"github.com/c2fo/testify/assert"
)

func (me *datasetTest) TestUpdateSqlWithNoSources() {
Expand Down Expand Up @@ -244,7 +244,7 @@ func (me *datasetTest) TestPreparedUpdateSqlWithByteSlice() {
}
sql, args, err := ds1.Returning(I("items").All()).Prepared(true).ToUpdateSql(item{Name: "Test", Data: []byte(`{"someJson":"data"}`)})
assert.NoError(t, err)
assert.Equal(t, args, []interface{}{"Test", `{"someJson":"data"}`})
assert.Equal(t, args, []interface{}{"Test", []byte(`{"someJson":"data"}`)})
assert.Equal(t, sql, `UPDATE "items" SET "name"=?,"data"=? RETURNING "items".*`)
}

Expand All @@ -257,7 +257,7 @@ func (me *datasetTest) TestPreparedUpdateSqlWithValuer() {
}
sql, args, err := ds1.Returning(I("items").All()).Prepared(true).ToUpdateSql(item{Name: "Test", Data: []byte(`Hello`)})
assert.NoError(t, err)
assert.Equal(t, args, []interface{}{"Test", "Hello World"})
assert.Equal(t, args, []interface{}{"Test", []byte("Hello World")})
assert.Equal(t, sql, `UPDATE "items" SET "name"=?,"data"=? RETURNING "items".*`)
}

Expand Down
23 changes: 23 additions & 0 deletions default_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strconv"
"strings"
"time"
"unicode/utf8"
)

var (
Expand Down Expand Up @@ -637,6 +638,28 @@ func (me *DefaultAdapter) LiteralString(buf *SqlBuilder, s string) error {
return nil
}

// Generates SQL for a slice of bytes
func (me *DefaultAdapter) LiteralBytes(buf *SqlBuilder, bs []byte) error {
if buf.IsPrepared {
return me.PlaceHolderSql(buf, bs)
}
buf.WriteRune(me.StringQuote)
i := 0
for len(bs) > 0 {
char, l := utf8.DecodeRune(bs)
if char == me.StringQuote { // single quote: ' -> \'
buf.WriteRune(me.StringQuote)
buf.WriteRune(me.StringQuote)
} else {
buf.WriteRune(char)
}
i++
bs = bs[l:]
}
buf.WriteRune(me.StringQuote)
return nil
}

//Generates SQL for a slice of values (e.g. []int64{1,2,3,4} -> (1,2,3,4)
func (me *DefaultAdapter) SliceValueSql(buf *SqlBuilder, slice reflect.Value) error {
buf.WriteRune(left_paren_rune)
Expand Down

0 comments on commit 47a9715

Please sign in to comment.