Skip to content

Commit

Permalink
v9.0.1
Browse files Browse the repository at this point in the history
* [FIXED] Issue where `NULL`, `TRUE` and `FALSE` are interpolated when using an `IS` clause. #165
  • Loading branch information
doug-martin committed Sep 19, 2019
1 parent 25020dc commit 3e93ae2
Show file tree
Hide file tree
Showing 15 changed files with 398 additions and 68 deletions.
4 changes: 4 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# v9.0.1

* [FIXED] Issue where `NULL`, `TRUE` and `FALSE` are interpolated when using an `IS` clause. [#165](https://github.com/doug-martin/goqu/issues/165)

# v9.0.0

* Changed `NULL`, `TRUE`, `FALSE` to not be interpolated when creating prepared statements. [#132](https://github.com/doug-martin/goqu/pull/132), [#158](https://github.com/doug-martin/goqu/pull/158) - [@marshallmcmullen](https://github.com/marshallmcmullen)
Expand Down
10 changes: 5 additions & 5 deletions delete_dataset_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,11 @@ func ExampleDeleteDataset_Where_prepared() {
).ToSQL()
fmt.Println(sql, args)
// Output:
// DELETE FROM "test" WHERE (("a" > ?) AND ("b" < ?) AND ("c" IS ?) AND ("d" IN (?, ?, ?))) [10 10 <nil> a b c]
// DELETE FROM "test" WHERE (("a" > ?) OR ("b" < ?) OR ("c" IS ?) OR ("d" IN (?, ?, ?))) [10 10 <nil> a b c]
// DELETE FROM "test" WHERE ((("a" > ?) AND ("b" < ?)) OR (("c" IS ?) AND ("d" IN (?, ?, ?)))) [10 10 <nil> a b c]
// DELETE FROM "test" WHERE (("a" > ?) AND ("b" < ?) AND ("c" IS ?) AND ("d" IN (?, ?, ?))) [10 10 <nil> a b c]
// DELETE FROM "test" WHERE (("a" > ?) OR (("b" < ?) AND ("c" IS ?))) [10 10 <nil>]
// DELETE FROM "test" WHERE (("a" > ?) AND ("b" < ?) AND ("c" IS NULL) AND ("d" IN (?, ?, ?))) [10 10 a b c]
// DELETE FROM "test" WHERE (("a" > ?) OR ("b" < ?) OR ("c" IS NULL) OR ("d" IN (?, ?, ?))) [10 10 a b c]
// DELETE FROM "test" WHERE ((("a" > ?) AND ("b" < ?)) OR (("c" IS NULL) AND ("d" IN (?, ?, ?)))) [10 10 a b c]
// DELETE FROM "test" WHERE (("a" > ?) AND ("b" < ?) AND ("c" IS NULL) AND ("d" IN (?, ?, ?))) [10 10 a b c]
// DELETE FROM "test" WHERE (("a" > ?) OR (("b" < ?) AND ("c" IS NULL))) [10 10]
}

func ExampleDeleteDataset_ClearWhere() {
Expand Down
100 changes: 100 additions & 0 deletions dialect/mysql/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,106 @@ func (mt *mysqlTest) TestQuery() {
mt.Len(entries, 0)
}

func (mt *mysqlTest) TestQuery_Prepared() {
var entries []entry
ds := mt.db.From("entry").Prepared(true)
mt.NoError(ds.Order(goqu.C("id").Asc()).ScanStructs(&entries))
mt.Len(entries, 10)
floatVal := float64(0)
baseDate, err := time.Parse(
"2006-01-02 15:04:05",
"2015-02-22 18:19:55",
)
mt.NoError(err)
for i, entry := range entries {
f := fmt.Sprintf("%f", floatVal)
mt.Equal(uint32(i+1), entry.ID)
mt.Equal(i, entry.Int)
mt.Equal(f, fmt.Sprintf("%f", entry.Float))
mt.Equal(f, entry.String)
mt.Equal([]byte(f), entry.Bytes)
mt.Equal(i%2 == 0, entry.Bool)
mt.Equal(baseDate.Add(time.Duration(i)*time.Hour), entry.Time)
floatVal += float64(0.1)
}
entries = entries[0:0]
mt.NoError(ds.Where(goqu.C("bool").IsTrue()).Order(goqu.C("id").Asc()).ScanStructs(&entries))
mt.Len(entries, 5)
mt.NoError(err)
for _, entry := range entries {
mt.True(entry.Bool)
}

entries = entries[0:0]
mt.NoError(ds.Where(goqu.C("int").Gt(4)).Order(goqu.C("id").Asc()).ScanStructs(&entries))
mt.Len(entries, 5)
mt.NoError(err)
for _, entry := range entries {
mt.True(entry.Int > 4)
}

entries = entries[0:0]
mt.NoError(ds.Where(goqu.C("int").Gte(5)).Order(goqu.C("id").Asc()).ScanStructs(&entries))
mt.Len(entries, 5)
mt.NoError(err)
for _, entry := range entries {
mt.True(entry.Int >= 5)
}

entries = entries[0:0]
mt.NoError(ds.Where(goqu.C("int").Lt(5)).Order(goqu.C("id").Asc()).ScanStructs(&entries))
mt.Len(entries, 5)
mt.NoError(err)
for _, entry := range entries {
mt.True(entry.Int < 5)
}

entries = entries[0:0]
mt.NoError(ds.Where(goqu.C("int").Lte(4)).Order(goqu.C("id").Asc()).ScanStructs(&entries))
mt.Len(entries, 5)
mt.NoError(err)
for _, entry := range entries {
mt.True(entry.Int <= 4)
}

entries = entries[0:0]
mt.NoError(ds.Where(goqu.C("int").Between(goqu.Range(3, 6))).Order(goqu.C("id").Asc()).ScanStructs(&entries))
mt.Len(entries, 4)
mt.NoError(err)
for _, entry := range entries {
mt.True(entry.Int >= 3)
mt.True(entry.Int <= 6)
}

entries = entries[0:0]
mt.NoError(ds.Where(goqu.C("string").Eq("0.100000")).Order(goqu.C("id").Asc()).ScanStructs(&entries))
mt.Len(entries, 1)
mt.NoError(err)
for _, entry := range entries {
mt.Equal("0.100000", entry.String)
}

entries = entries[0:0]
mt.NoError(ds.Where(goqu.C("string").Like("0.1%")).Order(goqu.C("id").Asc()).ScanStructs(&entries))
mt.Len(entries, 1)
mt.NoError(err)
for _, entry := range entries {
mt.Equal("0.100000", entry.String)
}

entries = entries[0:0]
mt.NoError(ds.Where(goqu.C("string").NotLike("0.1%")).Order(goqu.C("id").Asc()).ScanStructs(&entries))
mt.Len(entries, 9)
mt.NoError(err)
for _, entry := range entries {
mt.NotEqual("0.100000", entry.String)
}

entries = entries[0:0]
mt.NoError(ds.Where(goqu.C("string").IsNull()).Order(goqu.C("id").Asc()).ScanStructs(&entries))
mt.Len(entries, 0)
}

func (mt *mysqlTest) TestQuery_ValueExpressions() {
type wrappedEntry struct {
entry
Expand Down
106 changes: 106 additions & 0 deletions dialect/postgres/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,112 @@ func (pt *postgresTest) TestQuery() {
pt.Len(entries, 0)
}

func (pt *postgresTest) TestQuery_Prepared() {
var entries []entry
ds := pt.db.From("entry").Prepared(true)
pt.NoError(ds.Order(goqu.C("id").Asc()).ScanStructs(&entries))
pt.Len(entries, 10)
floatVal := float64(0)
baseDate, err := time.Parse(time.RFC3339Nano, "2015-02-22T18:19:55.000000000-00:00")
pt.NoError(err)
baseDate = baseDate.UTC()
for i, entry := range entries {
f := fmt.Sprintf("%f", floatVal)
pt.Equal(uint32(i+1), entry.ID)
pt.Equal(i, entry.Int)
pt.Equal(f, fmt.Sprintf("%f", entry.Float))
pt.Equal(f, entry.String)
pt.Equal([]byte(f), entry.Bytes)
pt.Equal(i%2 == 0, entry.Bool)
pt.Equal(baseDate.Add(time.Duration(i)*time.Hour).Unix(), entry.Time.Unix())
floatVal += float64(0.1)
}
entries = entries[0:0]
pt.NoError(ds.Where(goqu.C("bool").IsTrue()).Order(goqu.C("id").Asc()).ScanStructs(&entries))
pt.Len(entries, 5)
pt.NoError(err)
for _, entry := range entries {
pt.True(entry.Bool)
}

entries = entries[0:0]
pt.NoError(ds.Where(goqu.C("bool").IsFalse()).Order(goqu.C("id").Asc()).ScanStructs(&entries))
pt.Len(entries, 5)
pt.NoError(err)
for _, entry := range entries {
pt.False(entry.Bool)
}

entries = entries[0:0]
pt.NoError(ds.Where(goqu.C("int").Gt(4)).Order(goqu.C("id").Asc()).ScanStructs(&entries))
pt.Len(entries, 5)
pt.NoError(err)
for _, entry := range entries {
pt.True(entry.Int > 4)
}

entries = entries[0:0]
pt.NoError(ds.Where(goqu.C("int").Gte(5)).Order(goqu.C("id").Asc()).ScanStructs(&entries))
pt.Len(entries, 5)
pt.NoError(err)
for _, entry := range entries {
pt.True(entry.Int >= 5)
}

entries = entries[0:0]
pt.NoError(ds.Where(goqu.C("int").Lt(5)).Order(goqu.C("id").Asc()).ScanStructs(&entries))
pt.Len(entries, 5)
pt.NoError(err)
for _, entry := range entries {
pt.True(entry.Int < 5)
}

entries = entries[0:0]
pt.NoError(ds.Where(goqu.C("int").Lte(4)).Order(goqu.C("id").Asc()).ScanStructs(&entries))
pt.Len(entries, 5)
pt.NoError(err)
for _, entry := range entries {
pt.True(entry.Int <= 4)
}

entries = entries[0:0]
pt.NoError(ds.Where(goqu.C("int").Between(goqu.Range(3, 6))).Order(goqu.C("id").Asc()).ScanStructs(&entries))
pt.Len(entries, 4)
pt.NoError(err)
for _, entry := range entries {
pt.True(entry.Int >= 3)
pt.True(entry.Int <= 6)
}

entries = entries[0:0]
pt.NoError(ds.Where(goqu.C("string").Eq("0.100000")).Order(goqu.C("id").Asc()).ScanStructs(&entries))
pt.Len(entries, 1)
pt.NoError(err)
for _, entry := range entries {
pt.Equal(entry.String, "0.100000")
}

entries = entries[0:0]
pt.NoError(ds.Where(goqu.C("string").Like("0.1%")).Order(goqu.C("id").Asc()).ScanStructs(&entries))
pt.Len(entries, 1)
pt.NoError(err)
for _, entry := range entries {
pt.Equal("0.100000", entry.String)
}

entries = entries[0:0]
pt.NoError(ds.Where(goqu.C("string").NotLike("0.1%")).Order(goqu.C("id").Asc()).ScanStructs(&entries))
pt.Len(entries, 9)
pt.NoError(err)
for _, entry := range entries {
pt.NotEqual("0.100000", entry.String)
}

entries = entries[0:0]
pt.NoError(ds.Where(goqu.C("string").IsNull()).Order(goqu.C("id").Asc()).ScanStructs(&entries))
pt.Len(entries, 0)
}

func (pt *postgresTest) TestQuery_ValueExpressions() {
type wrappedEntry struct {
entry
Expand Down
97 changes: 97 additions & 0 deletions dialect/sqlite3/sqlite3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,103 @@ func (st *sqlite3Suite) TestQuery() {
st.Empty(entries)
}

func (st *sqlite3Suite) TestQuery_Prepared() {
var entries []entry
ds := st.db.From("entry").Prepared(true)
st.NoError(ds.Order(goqu.C("id").Asc()).ScanStructs(&entries))
st.Len(entries, 10)
floatVal := float64(0)
baseDate, err := time.Parse(DialectOptions().TimeFormat, "2015-02-22 18:19:55")
st.NoError(err)
for i, entry := range entries {
f := fmt.Sprintf("%f", floatVal)
st.Equal(uint32(i+1), entry.ID)
st.Equal(i, entry.Int)
st.Equal(f, fmt.Sprintf("%f", entry.Float))
st.Equal(f, entry.String)
st.Equal([]byte(f), entry.Bytes)
st.Equal(i%2 == 0, entry.Bool)
st.Equal(baseDate.Add(time.Duration(i)*time.Hour), entry.Time)
floatVal += float64(0.1)
}
entries = entries[0:0]
st.NoError(ds.Where(goqu.C("bool").IsTrue()).Order(goqu.C("id").Asc()).ScanStructs(&entries))
st.Len(entries, 5)
st.NoError(err)
for _, entry := range entries {
st.True(entry.Bool)
}

entries = entries[0:0]
st.NoError(ds.Where(goqu.C("int").Gt(4)).Order(goqu.C("id").Asc()).ScanStructs(&entries))
st.Len(entries, 5)
st.NoError(err)
for _, entry := range entries {
st.True(entry.Int > 4)
}

entries = entries[0:0]
st.NoError(ds.Where(goqu.C("int").Gte(5)).Order(goqu.C("id").Asc()).ScanStructs(&entries))
st.Len(entries, 5)
st.NoError(err)
for _, entry := range entries {
st.True(entry.Int >= 5)
}

entries = entries[0:0]
st.NoError(ds.Where(goqu.C("int").Lt(5)).Order(goqu.C("id").Asc()).ScanStructs(&entries))
st.Len(entries, 5)
st.NoError(err)
for _, entry := range entries {
st.True(entry.Int < 5)
}

entries = entries[0:0]
st.NoError(ds.Where(goqu.C("int").Lte(4)).Order(goqu.C("id").Asc()).ScanStructs(&entries))
st.Len(entries, 5)
st.NoError(err)
for _, entry := range entries {
st.True(entry.Int <= 4)
}

entries = entries[0:0]
st.NoError(ds.Where(goqu.C("int").Between(goqu.Range(3, 6))).Order(goqu.C("id").Asc()).ScanStructs(&entries))
st.Len(entries, 4)
st.NoError(err)
for _, entry := range entries {
st.True(entry.Int >= 3)
st.True(entry.Int <= 6)
}

entries = entries[0:0]
st.NoError(ds.Where(goqu.C("string").Eq("0.100000")).Order(goqu.C("id").Asc()).ScanStructs(&entries))
st.Len(entries, 1)
st.NoError(err)
for _, entry := range entries {
st.Equal(entry.String, "0.100000")
}

entries = entries[0:0]
st.NoError(ds.Where(goqu.C("string").Like("0.1%")).Order(goqu.C("id").Asc()).ScanStructs(&entries))
st.Len(entries, 1)
st.NoError(err)
for _, entry := range entries {
st.Equal("0.100000", entry.String)
}

entries = entries[0:0]
st.NoError(ds.Where(goqu.C("string").NotLike("0.1%")).Order(goqu.C("id").Asc()).ScanStructs(&entries))
st.Len(entries, 9)
st.NoError(err)
for _, entry := range entries {
st.NotEqual("0.100000", entry.String)
}

entries = entries[0:0]
st.NoError(ds.Where(goqu.C("string").IsNull()).Order(goqu.C("id").Asc()).ScanStructs(&entries))
st.Empty(entries)
}

func (st *sqlite3Suite) TestQuery_ValueExpressions() {
type wrappedEntry struct {
entry
Expand Down
2 changes: 1 addition & 1 deletion docs/deleting.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ fmt.Println(sql)

Output:
```
DELETE FROM "test" WHERE (("a" > ?) AND ("b" < ?) AND ("c" IS ?) AND ("d" IN (?, ?, ?))) [10 10 <nil> a b c]
DELETE FROM "test" WHERE (("a" > ?) AND ("b" < ?) AND ("c" IS NULL) AND ("d" IN (?, ?, ?))) [10 10 a b c]
```

<a name="where"></a>
Expand Down
8 changes: 4 additions & 4 deletions docs/expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ fmt.Println(sql, args)
Output:
```sql
SELECT * FROM "test" WHERE ((("col1" = 1) AND ("col2" IS TRUE)) OR (("col3" IS NULL) AND ("col4" = 'foo'))) []
SELECT * FROM "test" WHERE ((("col1" = ?) AND ("col2" IS ?)) OR (("col3" IS ?) AND ("col4" = ?))) [1 true <nil> foo]
SELECT * FROM "test" WHERE ((("col1" = ?) AND ("col2" IS TRUE)) OR (("col3" IS NULL) AND ("col4" = ?))) [1 foo]
```

<a name="complex"></a>
Expand Down Expand Up @@ -437,11 +437,11 @@ SELECT COUNT(*)
FROM "test"
INNER JOIN "test2" ON ("test"."fkey" = "test2"."id")
LEFT JOIN "test3" ON ("test2"."fkey" = "test3"."id")
WHERE ((("test"."name" ~ ?) AND ("test2"."amount" IS NOT ?)) AND
(("test3"."id" IS ?) OR ("test3"."status" IN (?, ?, ?))))
WHERE ((("test"."name" ~ ?) AND ("test2"."amount" IS NOT NULL)) AND
(("test3"."id" IS NULL) OR ("test3"."status" IN (?, ?, ?))))
GROUP BY "test"."user_id"
HAVING (AVG("test3"."age") > ?)
ORDER BY "test"."created" DESC NULLS LAST [^(a|b) <nil> <nil> passed active registered 10]
ORDER BY "test"."created" DESC NULLS LAST [^(a|b) passed active registered 10]
```


Loading

0 comments on commit 3e93ae2

Please sign in to comment.