Skip to content

Commit

Permalink
Merge pull request #189 from doug-martin/issue178
Browse files Browse the repository at this point in the history
Fix for issue 178
  • Loading branch information
doug-martin authored Dec 7, 2019
2 parents a0ce863 + 9b920a3 commit 096254c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 23 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* [FIXED] Fix ReturnsColumns() nil pointer panic [#181](https://github.com/doug-martin/goqu/issues/181) - [@yeaha](https://github.com/yeaha)
* [FIXED] SelectDataset From with Error [#183](https://github.com/doug-martin/goqu/issues/183)
* [FIXED] Unable to execute union with order by expression [#185](https://github.com/doug-martin/goqu/issues/185)
* [FIXED] SQlite dialect escapes single quotes wrong, leads to SQL syntax error [#178](https://github.com/doug-martin/goqu/issues/178)

# v9.5.0

Expand Down
8 changes: 1 addition & 7 deletions dialect/sqlite3/sqlite3.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,7 @@ func DialectOptions() *goqu.SQLDialectOptions {
}
opts.UseLiteralIsBools = false
opts.EscapedRunes = map[rune][]byte{
'\'': []byte("\\'"),
'"': []byte("\\\""),
'\\': []byte("\\\\"),
'\n': []byte("\\n"),
'\r': []byte("\\r"),
0: []byte("\\x00"),
0x1a: []byte("\\x1a"),
'\'': []byte("''"),
}
opts.InsertIgnoreClause = []byte("INSERT OR IGNORE")
opts.ConflictFragment = []byte("")
Expand Down
28 changes: 14 additions & 14 deletions dialect/sqlite3/sqlite3_dialect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,31 +63,31 @@ func (sds *sqlite3DialectSuite) TestLiteralString() {

sql, _, err = ds.Where(goqu.C("a").Eq("test'test")).ToSQL()
sds.NoError(err)
sds.Equal("SELECT * FROM `test` WHERE (`a` = 'test\\'test')", sql)
sds.Equal("SELECT * FROM `test` WHERE (`a` = 'test''test')", sql)

sql, _, err = ds.Where(goqu.C("a").Eq(`test"test`)).ToSQL()
sds.NoError(err)
sds.Equal("SELECT * FROM `test` WHERE (`a` = 'test\\\"test')", sql)
sds.Equal("SELECT * FROM `test` WHERE (`a` = 'test\"test')", sql)

sql, _, err = ds.Where(goqu.C("a").Eq(`test\test`)).ToSQL()
sds.NoError(err)
sds.Equal("SELECT * FROM `test` WHERE (`a` = 'test\\\\test')", sql)
sds.Equal("SELECT * FROM `test` WHERE (`a` = 'test\\test')", sql)

sql, _, err = ds.Where(goqu.C("a").Eq("test\ntest")).ToSQL()
sds.NoError(err)
sds.Equal("SELECT * FROM `test` WHERE (`a` = 'test\\ntest')", sql)
sds.Equal("SELECT * FROM `test` WHERE (`a` = 'test\ntest')", sql)

sql, _, err = ds.Where(goqu.C("a").Eq("test\rtest")).ToSQL()
sds.NoError(err)
sds.Equal("SELECT * FROM `test` WHERE (`a` = 'test\\rtest')", sql)
sds.Equal("SELECT * FROM `test` WHERE (`a` = 'test\rtest')", sql)

sql, _, err = ds.Where(goqu.C("a").Eq("test\x00test")).ToSQL()
sds.NoError(err)
sds.Equal("SELECT * FROM `test` WHERE (`a` = 'test\\x00test')", sql)
sds.Equal("SELECT * FROM `test` WHERE (`a` = 'test\x00test')", sql)

sql, _, err = ds.Where(goqu.C("a").Eq("test\x1atest")).ToSQL()
sds.NoError(err)
sds.Equal("SELECT * FROM `test` WHERE (`a` = 'test\\x1atest')", sql)
sds.Equal("SELECT * FROM `test` WHERE (`a` = 'test\x1atest')", sql)
}

func (sds *sqlite3DialectSuite) TestLiteralBytes() {
Expand All @@ -98,31 +98,31 @@ func (sds *sqlite3DialectSuite) TestLiteralBytes() {

sql, _, err = ds.Where(goqu.C("a").Eq([]byte("test'test"))).ToSQL()
sds.NoError(err)
sds.Equal("SELECT * FROM `test` WHERE (`a` = 'test\\'test')", sql)
sds.Equal("SELECT * FROM `test` WHERE (`a` = 'test''test')", sql)

sql, _, err = ds.Where(goqu.C("a").Eq([]byte(`test"test`))).ToSQL()
sds.NoError(err)
sds.Equal("SELECT * FROM `test` WHERE (`a` = 'test\\\"test')", sql)
sds.Equal("SELECT * FROM `test` WHERE (`a` = 'test\"test')", sql)

sql, _, err = ds.Where(goqu.C("a").Eq([]byte(`test\test`))).ToSQL()
sds.NoError(err)
sds.Equal("SELECT * FROM `test` WHERE (`a` = 'test\\\\test')", sql)
sds.Equal("SELECT * FROM `test` WHERE (`a` = 'test\\test')", sql)

sql, _, err = ds.Where(goqu.C("a").Eq([]byte("test\ntest"))).ToSQL()
sds.NoError(err)
sds.Equal("SELECT * FROM `test` WHERE (`a` = 'test\\ntest')", sql)
sds.Equal("SELECT * FROM `test` WHERE (`a` = 'test\ntest')", sql)

sql, _, err = ds.Where(goqu.C("a").Eq([]byte("test\rtest"))).ToSQL()
sds.NoError(err)
sds.Equal("SELECT * FROM `test` WHERE (`a` = 'test\\rtest')", sql)
sds.Equal("SELECT * FROM `test` WHERE (`a` = 'test\rtest')", sql)

sql, _, err = ds.Where(goqu.C("a").Eq([]byte("test\x00test"))).ToSQL()
sds.NoError(err)
sds.Equal("SELECT * FROM `test` WHERE (`a` = 'test\\x00test')", sql)
sds.Equal("SELECT * FROM `test` WHERE (`a` = 'test\x00test')", sql)

sql, _, err = ds.Where(goqu.C("a").Eq([]byte("test\x1atest"))).ToSQL()
sds.NoError(err)
sds.Equal("SELECT * FROM `test` WHERE (`a` = 'test\\x1atest')", sql)
sds.Equal("SELECT * FROM `test` WHERE (`a` = 'test\x1atest')", sql)
}

func (sds *sqlite3DialectSuite) TestBooleanOperations() {
Expand Down
14 changes: 12 additions & 2 deletions dialect/sqlite3/sqlite3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/doug-martin/goqu/v9"
"github.com/doug-martin/goqu/v9/dialect/mysql"
_ "github.com/mattn/go-sqlite3"

"github.com/stretchr/testify/suite"
Expand Down Expand Up @@ -360,25 +361,34 @@ func (st *sqlite3Suite) TestInsert() {
{Int: 12, Float: 1.200000, String: "1.200000", Time: now, Bool: true, Bytes: []byte("1.200000")},
{Int: 13, Float: 1.300000, String: "1.300000", Time: now, Bool: false, Bytes: []byte("1.300000")},
{Int: 14, Float: 1.400000, String: "1.400000", Time: now, Bool: true, Bytes: []byte("1.400000")},
{Int: 14, Float: 1.400000, String: `abc'd"e"f\\gh\n\ri\x00`, Time: now, Bool: true, Bytes: []byte("1.400000")},
}
_, err = ds.Insert().Rows(entries).Executor().Exec()
st.NoError(err)

var newEntries []entry
st.NoError(ds.Where(goqu.C("int").In([]uint32{11, 12, 13, 14})).ScanStructs(&newEntries))
st.Len(newEntries, 4)
for i, e := range newEntries {
st.Equal(entries[i].Int, e.Int)
st.Equal(entries[i].Float, e.Float)
st.Equal(entries[i].String, e.String)
st.Equal(entries[i].Time.UTC().Format(mysql.DialectOptions().TimeFormat), e.Time.Format(mysql.DialectOptions().TimeFormat))
st.Equal(entries[i].Bool, e.Bool)
st.Equal(entries[i].Bytes, e.Bytes)
}

_, err = ds.Insert().Rows(
entry{Int: 15, Float: 1.500000, String: "1.500000", Time: now, Bool: false, Bytes: []byte("1.500000")},
entry{Int: 16, Float: 1.600000, String: "1.600000", Time: now, Bool: true, Bytes: []byte("1.600000")},
entry{Int: 17, Float: 1.700000, String: "1.700000", Time: now, Bool: false, Bytes: []byte("1.700000")},
entry{Int: 18, Float: 1.800000, String: "1.800000", Time: now, Bool: true, Bytes: []byte("1.800000")},
entry{Int: 18, Float: 1.800000, String: `abc'd"e"f\\gh\n\ri\x00`, Time: now, Bool: true, Bytes: []byte("1.800000")},
).Executor().Exec()
st.NoError(err)

newEntries = newEntries[0:0]
st.NoError(ds.Where(goqu.C("int").In([]uint32{15, 16, 17, 18})).ScanStructs(&newEntries))
st.Len(newEntries, 4)
st.Len(newEntries, 5)
}

func (st *sqlite3Suite) TestInsert_returning() {
Expand Down

0 comments on commit 096254c

Please sign in to comment.