Skip to content

Commit

Permalink
Introduce operators for bitwise expressions and bitflag checks (#1242)…
Browse files Browse the repository at this point in the history
… / Oracle doesn't support bitwise eq, fixed in test
  • Loading branch information
Tapac committed Aug 8, 2021
1 parent 15be18c commit 8358de0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import org.jetbrains.exposed.dao.IntEntity
import org.jetbrains.exposed.dao.IntEntityClass
import org.jetbrains.exposed.dao.id.EntityID
import org.jetbrains.exposed.dao.id.IntIdTable
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.Column
import org.jetbrains.exposed.sql.ResultRow
import org.jetbrains.exposed.sql.Table
import org.jetbrains.exposed.sql.Transaction
import org.jetbrains.exposed.sql.insert
import org.jetbrains.exposed.sql.tests.DatabaseTestsBase
import org.jetbrains.exposed.sql.tests.TestDB
import java.util.*
Expand Down Expand Up @@ -36,7 +40,11 @@ object DMLTestsData {
}
}

fun DatabaseTestsBase.withCitiesAndUsers(exclude: List<TestDB> = emptyList(), statement: Transaction.(cities: DMLTestsData.Cities, users: DMLTestsData.Users, userData: DMLTestsData.UserData) -> Unit) {
@Suppress("LongMethod")
fun DatabaseTestsBase.withCitiesAndUsers(
exclude: List<TestDB> = emptyList(),
statement: Transaction.(cities: DMLTestsData.Cities, users: DMLTestsData.Users, userData: DMLTestsData.UserData) -> Unit
) {
val Users = DMLTestsData.Users
val UserFlags = DMLTestsData.Users.Flags
val Cities = DMLTestsData.Cities
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import org.jetbrains.exposed.sql.tests.shared.assertEqualCollections
import org.jetbrains.exposed.sql.tests.shared.assertEquals
import org.jetbrains.exposed.sql.tests.shared.dml.DMLTestsData
import org.jetbrains.exposed.sql.tests.shared.dml.withCitiesAndUsers
import org.jetbrains.exposed.sql.vendors.OracleDialect
import org.jetbrains.exposed.sql.vendors.SQLServerDialect
import org.jetbrains.exposed.sql.vendors.currentDialect
import org.junit.Test
import kotlin.test.assertNotNull

Expand Down Expand Up @@ -64,20 +66,20 @@ class FunctionsTests : DatabaseTestsBase() {
@Test
fun testBitwiseAnd1() {
withCitiesAndUsers { _, users, _ ->
// SQLServer doesn't support = on bit values
val isSQLServer = currentDialectTest is SQLServerDialect
// SQLServer and Oracle don't support = on bit values
val doesntSupportBitwiseEQ = currentDialectTest is SQLServerDialect || currentDialectTest is OracleDialect
val adminFlag = DMLTestsData.Users.Flags.IS_ADMIN
val adminAndFlagsExpr = Expression.build { (users.flags bitwiseAnd adminFlag) }
val adminEq = Expression.build { adminAndFlagsExpr eq adminFlag }
val toSlice = listOfNotNull(adminAndFlagsExpr, adminEq.takeIf { !isSQLServer })
val toSlice = listOfNotNull(adminAndFlagsExpr, adminEq.takeIf { !doesntSupportBitwiseEQ })
val r = users.slice(toSlice).selectAll().orderBy(users.id).toList()
assertEquals(5, r.size)
assertEquals(0, r[0][adminAndFlagsExpr])
assertEquals(1, r[1][adminAndFlagsExpr])
assertEquals(0, r[2][adminAndFlagsExpr])
assertEquals(1, r[3][adminAndFlagsExpr])
assertEquals(0, r[4][adminAndFlagsExpr])
if (!isSQLServer) {
if (!doesntSupportBitwiseEQ) {
assertEquals(false, r[0][adminEq])
assertEquals(true, r[1][adminEq])
assertEquals(false, r[2][adminEq])
Expand All @@ -90,20 +92,20 @@ class FunctionsTests : DatabaseTestsBase() {
@Test
fun testBitwiseAnd2() {
withCitiesAndUsers { _, users, _ ->
// SQLServer doesn't support = on bit values
val isSQLServer = currentDialectTest is SQLServerDialect
// SQLServer and Oracle don't support = on bit values
val doesntSupportBitwiseEQ = currentDialectTest is SQLServerDialect || currentDialectTest is OracleDialect
val adminFlag = DMLTestsData.Users.Flags.IS_ADMIN
val adminAndFlagsExpr = Expression.build { (users.flags bitwiseAnd intLiteral(adminFlag)) }
val adminEq = Expression.build { adminAndFlagsExpr eq adminFlag }
val toSlice = listOfNotNull(adminAndFlagsExpr, adminEq.takeIf { !isSQLServer })
val toSlice = listOfNotNull(adminAndFlagsExpr, adminEq.takeIf { !doesntSupportBitwiseEQ })
val r = users.slice(toSlice).selectAll().orderBy(users.id).toList()
assertEquals(5, r.size)
assertEquals(0, r[0][adminAndFlagsExpr])
assertEquals(1, r[1][adminAndFlagsExpr])
assertEquals(0, r[2][adminAndFlagsExpr])
assertEquals(1, r[3][adminAndFlagsExpr])
assertEquals(0, r[4][adminAndFlagsExpr])
if (!isSQLServer) {
if (!doesntSupportBitwiseEQ) {
assertEquals(false, r[0][adminEq])
assertEquals(true, r[1][adminEq])
assertEquals(false, r[2][adminEq])
Expand Down

0 comments on commit 8358de0

Please sign in to comment.