Skip to content

Commit

Permalink
v0.1.1
Browse files Browse the repository at this point in the history
* Added SQLite3 adapter #1 - @mattn
  • Loading branch information
doug-martin committed Mar 9, 2015
1 parent 23bb379 commit 6755f41
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 36 deletions.
4 changes: 4 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## v0.1.1

* Added SQLite3 adapter [#1](https://github.com/doug-martin/goqu/pull/1) - [@mattn](https://github.com/mattn)

## v0.1.0

* Added:
Expand Down
32 changes: 16 additions & 16 deletions adapters/sqlite3/dataset_adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,49 +111,49 @@ func (me *datasetAdapterTest) TestBooleanOperations() {
ds := me.GetDs("test")
sql, err := ds.Where(goqu.I("a").Eq(true)).Sql()
assert.NoError(t, err)
assert.Equal(t, sql, "SELECT * FROM `test` WHERE (`a` IS TRUE)")
assert.Equal(t, sql, "SELECT * FROM `test` WHERE (`a` IS 1)")
sql, err = ds.Where(goqu.I("a").Eq(false)).Sql()
assert.NoError(t, err)
assert.Equal(t, sql, "SELECT * FROM `test` WHERE (`a` IS FALSE)")
assert.Equal(t, sql, "SELECT * FROM `test` WHERE (`a` IS 0)")
sql, err = ds.Where(goqu.I("a").Is(true)).Sql()
assert.NoError(t, err)
assert.Equal(t, sql, "SELECT * FROM `test` WHERE (`a` IS TRUE)")
assert.Equal(t, sql, "SELECT * FROM `test` WHERE (`a` IS 1)")
sql, err = ds.Where(goqu.I("a").Is(false)).Sql()
assert.NoError(t, err)
assert.Equal(t, sql, "SELECT * FROM `test` WHERE (`a` IS FALSE)")
assert.Equal(t, sql, "SELECT * FROM `test` WHERE (`a` IS 0)")
sql, err = ds.Where(goqu.I("a").IsTrue()).Sql()
assert.NoError(t, err)
assert.Equal(t, sql, "SELECT * FROM `test` WHERE (`a` IS TRUE)")
assert.Equal(t, sql, "SELECT * FROM `test` WHERE (`a` IS 1)")
sql, err = ds.Where(goqu.I("a").IsFalse()).Sql()
assert.NoError(t, err)
assert.Equal(t, sql, "SELECT * FROM `test` WHERE (`a` IS FALSE)")
assert.Equal(t, sql, "SELECT * FROM `test` WHERE (`a` IS 0)")

sql, err = ds.Where(goqu.I("a").Neq(true)).Sql()
assert.NoError(t, err)
assert.Equal(t, sql, "SELECT * FROM `test` WHERE (`a` IS NOT TRUE)")
assert.Equal(t, sql, "SELECT * FROM `test` WHERE (`a` IS NOT 1)")
sql, err = ds.Where(goqu.I("a").Neq(false)).Sql()
assert.NoError(t, err)
assert.Equal(t, sql, "SELECT * FROM `test` WHERE (`a` IS NOT FALSE)")
assert.Equal(t, sql, "SELECT * FROM `test` WHERE (`a` IS NOT 0)")
sql, err = ds.Where(goqu.I("a").IsNot(true)).Sql()
assert.NoError(t, err)
assert.Equal(t, sql, "SELECT * FROM `test` WHERE (`a` IS NOT TRUE)")
assert.Equal(t, sql, "SELECT * FROM `test` WHERE (`a` IS NOT 1)")
sql, err = ds.Where(goqu.I("a").IsNot(false)).Sql()
assert.NoError(t, err)
assert.Equal(t, sql, "SELECT * FROM `test` WHERE (`a` IS NOT FALSE)")
assert.Equal(t, sql, "SELECT * FROM `test` WHERE (`a` IS NOT 0)")
sql, err = ds.Where(goqu.I("a").IsNotTrue()).Sql()
assert.NoError(t, err)
assert.Equal(t, sql, "SELECT * FROM `test` WHERE (`a` IS NOT TRUE)")
assert.Equal(t, sql, "SELECT * FROM `test` WHERE (`a` IS NOT 1)")
sql, err = ds.Where(goqu.I("a").IsNotFalse()).Sql()
assert.NoError(t, err)
assert.Equal(t, sql, "SELECT * FROM `test` WHERE (`a` IS NOT FALSE)")
assert.Equal(t, sql, "SELECT * FROM `test` WHERE (`a` IS NOT 0)")

sql, err = ds.Where(goqu.I("a").Like("a%")).Sql()
assert.NoError(t, err)
assert.Equal(t, sql, "SELECT * FROM `test` WHERE (`a` LIKE BINARY 'a%')")
assert.Equal(t, sql, "SELECT * FROM `test` WHERE (`a` LIKE 'a%')")

sql, err = ds.Where(goqu.I("a").NotLike("a%")).Sql()
assert.NoError(t, err)
assert.Equal(t, sql, "SELECT * FROM `test` WHERE (`a` NOT LIKE BINARY 'a%')")
assert.Equal(t, sql, "SELECT * FROM `test` WHERE (`a` NOT LIKE 'a%')")

sql, err = ds.Where(goqu.I("a").ILike("a%")).Sql()
assert.NoError(t, err)
Expand All @@ -164,10 +164,10 @@ func (me *datasetAdapterTest) TestBooleanOperations() {

sql, err = ds.Where(goqu.I("a").Like(regexp.MustCompile("(a|b)"))).Sql()
assert.NoError(t, err)
assert.Equal(t, sql, "SELECT * FROM `test` WHERE (`a` REGEXP BINARY '(a|b)')")
assert.Equal(t, sql, "SELECT * FROM `test` WHERE (`a` REGEXP '(a|b)')")
sql, err = ds.Where(goqu.I("a").NotLike(regexp.MustCompile("(a|b)"))).Sql()
assert.NoError(t, err)
assert.Equal(t, sql, "SELECT * FROM `test` WHERE (`a` NOT REGEXP BINARY '(a|b)')")
assert.Equal(t, sql, "SELECT * FROM `test` WHERE (`a` NOT REGEXP '(a|b)')")
sql, err = ds.Where(goqu.I("a").ILike(regexp.MustCompile("(a|b)"))).Sql()
assert.NoError(t, err)
assert.Equal(t, sql, "SELECT * FROM `test` WHERE (`a` REGEXP '(a|b)')")
Expand Down
9 changes: 5 additions & 4 deletions adapters/sqlite3/sqlite3.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ var (
goqu.NOT_IN_OP: []byte("NOT IN"),
goqu.IS_OP: []byte("IS"),
goqu.IS_NOT_OP: []byte("IS NOT"),
goqu.LIKE_OP: []byte("LIKE BINARY"),
goqu.NOT_LIKE_OP: []byte("NOT LIKE BINARY"),
goqu.LIKE_OP: []byte("LIKE"),
goqu.NOT_LIKE_OP: []byte("NOT LIKE"),
goqu.I_LIKE_OP: []byte("LIKE"),
goqu.NOT_I_LIKE_OP: []byte("NOT LIKE"),
goqu.REGEXP_LIKE_OP: []byte("REGEXP BINARY"),
goqu.REGEXP_NOT_LIKE_OP: []byte("NOT REGEXP BINARY"),
goqu.REGEXP_LIKE_OP: []byte("REGEXP"),
goqu.REGEXP_NOT_LIKE_OP: []byte("NOT REGEXP"),
goqu.REGEXP_I_LIKE_OP: []byte("REGEXP"),
goqu.REGEXP_NOT_I_LIKE_OP: []byte("NOT REGEXP"),
}
Expand Down Expand Up @@ -96,6 +96,7 @@ func newDatasetAdapter(ds *goqu.Dataset) goqu.Adapter {
def.False = sqlite3_false
def.TimeFormat = time_format
def.BooleanOperatorLookup = operator_lookup
def.UseLiteralIsBools = false
return &DatasetAdapter{def}
}

Expand Down
30 changes: 15 additions & 15 deletions adapters/sqlite3/sqlite3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,25 @@ import (
const (
drop_table = "DROP TABLE IF EXISTS `entry`;"
create_table = "CREATE TABLE `entry` (" +
"`id` INT NOT NULL AUTO_INCREMENT ," +
"`id` INTEGER PRIMARY KEY," +
"`int` INT NOT NULL ," +
"`float` FLOAT NOT NULL ," +
"`string` VARCHAR(255) NOT NULL ," +
"`time` DATETIME NOT NULL ," +
"`bool` TINYINT NOT NULL ," +
"`bytes` BLOB NOT NULL ," +
"PRIMARY KEY (`id`) );"
"`bytes` BLOB NOT NULL" +
");"
insert_default_reords = "INSERT INTO `entry` (`int`, `float`, `string`, `time`, `bool`, `bytes`) VALUES" +
"(0, 0.000000, '0.000000', '2015-02-22 18:19:55', TRUE, '0.000000')," +
"(1, 0.100000, '0.100000', '2015-02-22 19:19:55', FALSE, '0.100000')," +
"(2, 0.200000, '0.200000', '2015-02-22 20:19:55', TRUE, '0.200000')," +
"(3, 0.300000, '0.300000', '2015-02-22 21:19:55', FALSE, '0.300000')," +
"(4, 0.400000, '0.400000', '2015-02-22 22:19:55', TRUE, '0.400000')," +
"(5, 0.500000, '0.500000', '2015-02-22 23:19:55', FALSE, '0.500000')," +
"(6, 0.600000, '0.600000', '2015-02-23 00:19:55', TRUE, '0.600000')," +
"(7, 0.700000, '0.700000', '2015-02-23 01:19:55', FALSE, '0.700000')," +
"(8, 0.800000, '0.800000', '2015-02-23 02:19:55', TRUE, '0.800000')," +
"(9, 0.900000, '0.900000', '2015-02-23 03:19:55', FALSE, '0.900000');"
"(0, 0.000000, '0.000000', '2015-02-22 18:19:55', 1, '0.000000')," +
"(1, 0.100000, '0.100000', '2015-02-22 19:19:55', 0, '0.100000')," +
"(2, 0.200000, '0.200000', '2015-02-22 20:19:55', 1, '0.200000')," +
"(3, 0.300000, '0.300000', '2015-02-22 21:19:55', 0, '0.300000')," +
"(4, 0.400000, '0.400000', '2015-02-22 22:19:55', 1, '0.400000')," +
"(5, 0.500000, '0.500000', '2015-02-22 23:19:55', 0, '0.500000')," +
"(6, 0.600000, '0.600000', '2015-02-23 00:19:55', 1, '0.600000')," +
"(7, 0.700000, '0.700000', '2015-02-23 01:19:55', 0, '0.700000')," +
"(8, 0.800000, '0.800000', '2015-02-23 02:19:55', 1, '0.800000')," +
"(9, 0.900000, '0.900000', '2015-02-23 03:19:55', 0, '0.900000');"
)

var db_uri = ":memory:"
Expand Down Expand Up @@ -65,7 +65,7 @@ func (me *sqlite3Test) SetupSuite() {
panic(err.Error())
}
me.db = goqu.New("sqlite3", db)
// me.db.Logger(logger{})
// me.db.Logger(logger{})
}

func (me *sqlite3Test) SetupTest() {
Expand Down Expand Up @@ -311,6 +311,6 @@ func (me *sqlite3Test) TestDelete() {
assert.Equal(t, err.Error(), "goqu: Adapter does not support RETURNING clause")
}

func Testsqlite3Suite(t *testing.T) {
func TestSqlite3Suite(t *testing.T) {
suite.Run(t, new(sqlite3Test))
}
5 changes: 4 additions & 1 deletion default_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ type (
BooleanOperatorLookup map[BooleanOperation][]byte
//A map used to look up JoinTypes and their SQL equivalents
JoinTypeLookup map[JoinType][]byte
//Whether or not to use literal TRUE or FALSE for IS statements (e.g. IS TRUE or IS 0)
UseLiteralIsBools bool
}
)

Expand Down Expand Up @@ -227,6 +229,7 @@ func NewDefaultAdapter(ds *Dataset) Adapter {
BooleanOperatorLookup: default_operator_lookup,
JoinTypeLookup: default_join_lookup,
TimeFormat: time.RFC3339Nano,
UseLiteralIsBools: true,
}
}

Expand Down Expand Up @@ -681,7 +684,7 @@ func (me *DefaultAdapter) BooleanExpressionSql(buf *SqlBuilder, operator Boolean
return NewGoquError("Boolean operator %+v not supported", operatorOp)
}
rhs := operator.Rhs()
if operatorOp == IS_OP || operatorOp == IS_NOT_OP {
if (operatorOp == IS_OP || operatorOp == IS_NOT_OP) && me.UseLiteralIsBools {
if rhs == true {
rhs = L("TRUE")
} else if rhs == false {
Expand Down

1 comment on commit 6755f41

@mattn
Copy link
Contributor

@mattn mattn commented on 6755f41 Mar 9, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, it seems i should look more carefully. Sorry

Please sign in to comment.