Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/main/scala/usql/dao/Rep.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ trait Rep[T] {
}

def &&(using T =:= Boolean)(rep: Rep[Boolean]): Rep[Boolean] = {
SqlRep(sql"${toInterpolationParameter} AND ${rep.toInterpolationParameter}")
SqlRep(sql"(${toInterpolationParameter}) AND (${rep.toInterpolationParameter})")
}

def ||(using T =:= Boolean)(rep: Rep[Boolean]): Rep[Boolean] = {
SqlRep(sql"${toInterpolationParameter} OR ${rep.toInterpolationParameter}")
SqlRep(sql"(${toInterpolationParameter}) OR (${rep.toInterpolationParameter})")
}

def isNull(using @unused optCheck: T => Option[?]): Rep[Boolean] = {
Expand All @@ -49,6 +49,10 @@ trait Rep[T] {
def isNotNull(using @unused optCheck: T => Option[?]): Rep[Boolean] = {
SqlRep(sql"${toInterpolationParameter} IS NOT NULL")
}

def unary_!(using T =:= Boolean): Rep[Boolean] = {
SqlRep(sql"NOT (${toInterpolationParameter})")
}
}

object Rep {
Expand Down
23 changes: 23 additions & 0 deletions src/test/scala/usql/dao/QueryBuilderTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,29 @@ class QueryBuilderTest extends TestBaseWithH2 {
withoutAgeQuery.count() shouldBe 2
}

it should "correctly order operators" in new EnvWithSamples {
val query = Person.query
.filter(x => x.age.isNotNull && (x.name === "Alice" || x.name === "Bob"))
.map(_.name)

println(s"Query = ${query.sql}")

val names = query.all()

// This fails, when the query SELECT name AS name FROM person WHERE age IS NOT NULL AND name = ? OR name = ? is used

names shouldBe Seq("Alice")
}

it should "also support negation operations" in new EnvWithSamples {
val query = Person.query
.filter(x => !x.age.isNull)
.map(_.name)

val names = query.all()
names should contain theSameElementsAs Seq("Alice")
}

it should "work with a single join" in new EnvWithSamples {
val foo: QueryBuilder[(Person, Int)] = Person.query
.join(PersonPermission.query)(_.id === _.personId)
Expand Down