Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to implement bitwise operations? #572

Open
KunMinX opened this issue Aug 3, 2024 · 3 comments
Open

How to implement bitwise operations? #572

KunMinX opened this issue Aug 3, 2024 · 3 comments

Comments

@KunMinX
Copy link

KunMinX commented Aug 3, 2024

Currently, there are no operators like bitAnd or bitOr.
The following is my usage in the Android Room framework, for example, feature & 0x0001 equals 0x0001.
I want to use bitwise operators in where or order by clauses:

@Query("select * from note where isDeleted = 0 order by (feature & 0x0001) = 0x0001 desc, create_time desc")
fun notes(): List<Note>
@KunMinX
Copy link
Author

KunMinX commented Aug 3, 2024

I tried custom ColumnDeclaring, but the BinaryExpression first parament only accpet BinaryExpressionType, which is enum and do not has BinaryExpressionType.BitAnd, so it seems impossible to support bitAnd, unless official update the ktorm lib

infix fun ColumnDeclaring<Int>.bitAnd(value :Int):BinaryExpression<Int>{
  return BinaryExpression(
    BinaryExpressionType.AND,
    asExpression(),
    wrapArgument(value),
    IntSqlType
  )
}

@KunMinX
Copy link
Author

KunMinX commented Aug 3, 2024

so I solved this problem
#573

@vincentlauvlwj
Copy link
Member

enum class BitOperationType(val op: String) {
    AND("&"),
    OR("|"),
    XOR("^"),
    LEFT_SHIFT("<<"),
    RIGHT_SHIFT(">>");

    override fun toString(): String {
        return op;
    }
}

data class BitOperationExpression(
    val type: BitOperationType,
    val left: ScalarExpression<Long>,
    val right: ScalarExpression<Long>,
    override val sqlType: SqlType<Long>,
    override val isLeafNode: Boolean = false,
    override val extraProperties: Map<String, Any> = emptyMap()
) : ScalarExpression<Long>()

infix fun ColumnDeclaring<Long>.and(expr: ColumnDeclaring<Long>): BitOperationExpression {
    return BitOperationExpression(BitOperationType.AND, asExpression(), expr.asExpression(), LongSqlType)
}

infix fun ColumnDeclaring<Long>.or(expr: ColumnDeclaring<Long>): BitOperationExpression {
    return BitOperationExpression(BitOperationType.OR, asExpression(), expr.asExpression(), LongSqlType)
}

infix fun ColumnDeclaring<Long>.xor(expr: ColumnDeclaring<Long>): BitOperationExpression {
    return BitOperationExpression(BitOperationType.XOR, asExpression(), expr.asExpression(), LongSqlType)
}

infix fun ColumnDeclaring<Long>.shl(bitCount: Int): BitOperationExpression {
    return BitOperationExpression(BitOperationType.LEFT_SHIFT, asExpression(), ArgumentExpression(bitCount.toLong(), LongSqlType), LongSqlType)
}

infix fun ColumnDeclaring<Long>.shr(bitCount: Int): BitOperationExpression {
    return BitOperationExpression(BitOperationType.RIGHT_SHIFT, asExpression(), ArgumentExpression(bitCount.toLong(), LongSqlType), LongSqlType)
}

fun ColumnDeclaring<Long>.inv(): BitOperationExpression {
    return this xor ArgumentExpression(0xFFFFFFFFFFFFFFFFUL.toLong(), LongSqlType)
}

class CustomSqlFormatter (
    database: Database, beautifySql: Boolean, indentSize: Int
) : MySqlFormatter(database, beautifySql, indentSize) {

    override fun visitUnknown(expr: SqlExpression): SqlExpression {
        if (expr is BitOperationExpression) {
            if (expr.left.removeBrackets) {
                visit(expr.left)
            } else {
                write("(")
                visit(expr.left)
                removeLastBlank()
                write(") ")
            }

            writeKeyword("${expr.type} ")

            if (expr.right.removeBrackets) {
                visit(expr.right)
            } else {
                write("(")
                visit(expr.right)
                removeLastBlank()
                write(") ")
            }

            return expr
        }

        return super.visitUnknown(expr)
    }
}

fun main() {
    val database = Database.connect(
        url = "jdbc:mysql://localhost:3306/ktorm-example",
        user = "root",
        dialect = object : MySqlDialect() {
            override fun createSqlFormatter(database: Database, beautifySql: Boolean, indentSize: Int): SqlFormatter {
                return CustomSqlFormatter(database, beautifySql, indentSize)
            }
        }
    )

    // Use the database instance to perform your operations...
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants