Skip to content

Commit

Permalink
added bitwise and comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
pupizoid committed Sep 19, 2019
1 parent 49301af commit b64f8bd
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
14 changes: 14 additions & 0 deletions ormlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ type LessOrEqual float64

type NotEqual float64

type BitwiseAND float64

type BitwiseANDStrict float64

const (
// AND is a glue between multiple statements after `where`
AND = " and "
Expand Down Expand Up @@ -278,6 +282,11 @@ func queryWithOptions(ctx context.Context, db *sql.DB, table string, columns []s
keys = append(keys, fmt.Sprintf("%s <= ?", k))
case NotEqual:
keys = append(keys, fmt.Sprintf("%s != ?", k))
case BitwiseAND:
keys = append(keys, fmt.Sprintf("%s&? > 0", k))
case BitwiseANDStrict:
keys = append(keys, fmt.Sprintf("%s&? = ?", k))
values = append(values, v)
default:
keys = append(keys, fmt.Sprintf("%s = ?", k))
}
Expand Down Expand Up @@ -1035,6 +1044,11 @@ func Count(db *sql.DB, m Model, opts *Options) (count int64, err error) {
query.WriteString(f + " <= ?" + divider)
case NotEqual:
query.WriteString(f + " != ?" + divider)
case BitwiseAND:
query.WriteString(f + "&? > 0" + divider)
case BitwiseANDStrict:
query.WriteString(f + "&? = ?" + divider)
args = append(args, v)
default:
query.WriteString(f + " = ?" + divider)
}
Expand Down
41 changes: 41 additions & 0 deletions ormlite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1125,3 +1125,44 @@ func TestGreaterOrLessOperator(t *testing.T) {
assert.Len(t, mm, 4)
}
}

func TestBitwiseQuerying(t *testing.T) {
db, err := sql.Open("sqlite3", ":memory:?_fk=1")
require.NoError(t, err)

_, err = db.Exec(`
create table test(id integer primary key, number integer);
insert into test(number) values (108);
`)
require.NoError(t, err)

var m testOperatorsModel
if assert.NoError(t, QueryStruct(db, &Options{Where: Where{"number": BitwiseAND(30)}}, &m)) {
assert.EqualValues(t, 1, m.ID)
}

var m1 testOperatorsModel
if assert.NoError(t, QueryStruct(db, &Options{Where: Where{"number": BitwiseANDStrict(30)}}, &m1)) {
assert.EqualValues(t, 0, m1.ID)
}

var m2 testOperatorsModel
if assert.NoError(t, QueryStruct(db, &Options{Where: Where{"number": BitwiseANDStrict(40)}}, &m2)) {
assert.EqualValues(t, 1, m2.ID)
}

count, err := Count(db, &testOperatorsModel{}, &Options{Where: Where{"number": BitwiseAND(30)}})
if assert.NoError(t, err) {
assert.EqualValues(t, 1, count)
}

count, err = Count(db, &testOperatorsModel{}, &Options{Where: Where{"number": BitwiseANDStrict(30)}})
if assert.NoError(t, err) {
assert.EqualValues(t, 0, count)
}

count, err = Count(db, &testOperatorsModel{}, &Options{Where: Where{"number": BitwiseANDStrict(40)}})
if assert.NoError(t, err) {
assert.EqualValues(t, 1, count)
}
}

0 comments on commit b64f8bd

Please sign in to comment.