From 27eee7ceb12f059a552a1383cebb594530dbf822 Mon Sep 17 00:00:00 2001 From: Matt Carter Date: Mon, 24 Aug 2015 15:10:00 -0500 Subject: [PATCH] Add literal bytes and update to c2fo testify --- adapters.go | 4 +++ adapters/mysql/dataset_adapter_test.go | 4 +-- adapters/mysql/mysql_test.go | 4 +-- adapters/postgres/dataset_adapter_test.go | 4 +-- adapters/postgres/postgres_test.go | 4 +-- adapters/sqlite3/dataset_adapter_test.go | 4 +-- adapters/sqlite3/sqlite3_test.go | 4 +-- adapters_test.go | 4 +-- crud_exec_test.go | 4 +-- database_test.go | 4 +-- dataset.go | 4 +-- dataset_actions_test.go | 2 +- dataset_delete_test.go | 2 +- dataset_insert_test.go | 2 +- dataset_select_test.go | 2 +- dataset_test.go | 30 ++++++++++++++++++++--- dataset_update_test.go | 6 ++--- default_adapter.go | 23 +++++++++++++++++ 18 files changed, 81 insertions(+), 30 deletions(-) diff --git a/adapters.go b/adapters.go index d2be20be..96016cbd 100644 --- a/adapters.go +++ b/adapters.go @@ -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 diff --git a/adapters/mysql/dataset_adapter_test.go b/adapters/mysql/dataset_adapter_test.go index 86ab95da..7fa3852d 100644 --- a/adapters/mysql/dataset_adapter_test.go +++ b/adapters/mysql/dataset_adapter_test.go @@ -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" ) diff --git a/adapters/mysql/mysql_test.go b/adapters/mysql/mysql_test.go index a33a0a4d..6ed8ee42 100644 --- a/adapters/mysql/mysql_test.go +++ b/adapters/mysql/mysql_test.go @@ -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" ) diff --git a/adapters/postgres/dataset_adapter_test.go b/adapters/postgres/dataset_adapter_test.go index cbc345a8..7c9fc6a0 100644 --- a/adapters/postgres/dataset_adapter_test.go +++ b/adapters/postgres/dataset_adapter_test.go @@ -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" ) diff --git a/adapters/postgres/postgres_test.go b/adapters/postgres/postgres_test.go index 4daffbf5..31e7d52e 100644 --- a/adapters/postgres/postgres_test.go +++ b/adapters/postgres/postgres_test.go @@ -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" ) diff --git a/adapters/sqlite3/dataset_adapter_test.go b/adapters/sqlite3/dataset_adapter_test.go index 5cc3543e..671e72cd 100644 --- a/adapters/sqlite3/dataset_adapter_test.go +++ b/adapters/sqlite3/dataset_adapter_test.go @@ -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" ) diff --git a/adapters/sqlite3/sqlite3_test.go b/adapters/sqlite3/sqlite3_test.go index 3c6e95d4..5afdd4e3 100644 --- a/adapters/sqlite3/sqlite3_test.go +++ b/adapters/sqlite3/sqlite3_test.go @@ -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" ) diff --git a/adapters_test.go b/adapters_test.go index 87b717ba..f83ad830 100644 --- a/adapters_test.go +++ b/adapters_test.go @@ -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 { diff --git a/crud_exec_test.go b/crud_exec_test.go index d8918cfc..1eba025b 100644 --- a/crud_exec_test.go +++ b/crud_exec_test.go @@ -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 { diff --git a/database_test.go b/database_test.go index ff492ae2..9c5876a2 100644 --- a/database_test.go +++ b/database_test.go @@ -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 { diff --git a/dataset.go b/dataset.go index 33968b6c..4c144a06 100644 --- a/dataset.go +++ b/dataset.go @@ -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 { diff --git a/dataset_actions_test.go b/dataset_actions_test.go index 87966842..20a95c63 100644 --- a/dataset_actions_test.go +++ b/dataset_actions_test.go @@ -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 { diff --git a/dataset_delete_test.go b/dataset_delete_test.go index 68de31f6..3f4105a9 100644 --- a/dataset_delete_test.go +++ b/dataset_delete_test.go @@ -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() { diff --git a/dataset_insert_test.go b/dataset_insert_test.go index a6c65078..84700183 100644 --- a/dataset_insert_test.go +++ b/dataset_insert_test.go @@ -2,7 +2,7 @@ package goqu import ( "github.com/DATA-DOG/go-sqlmock" - "github.com/technotronicoz/testify/assert" + "github.com/c2fo/testify/assert" "time" ) diff --git a/dataset_select_test.go b/dataset_select_test.go index fbf6e802..e208f530 100644 --- a/dataset_select_test.go +++ b/dataset_select_test.go @@ -1,7 +1,7 @@ package goqu import ( - "github.com/technotronicoz/testify/assert" + "github.com/c2fo/testify/assert" ) func (me *datasetTest) TestSelect() { diff --git a/dataset_test.go b/dataset_test.go index 5286be90..68c28ce9 100644 --- a/dataset_test.go +++ b/dataset_test.go @@ -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 { @@ -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 @@ -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(), "?") } diff --git a/dataset_update_test.go b/dataset_update_test.go index 8a7bab6f..569bf18b 100644 --- a/dataset_update_test.go +++ b/dataset_update_test.go @@ -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() { @@ -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".*`) } @@ -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".*`) } diff --git a/default_adapter.go b/default_adapter.go index 9fdb7924..70938532 100644 --- a/default_adapter.go +++ b/default_adapter.go @@ -6,6 +6,7 @@ import ( "strconv" "strings" "time" + "unicode/utf8" ) var ( @@ -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)