-
-
Notifications
You must be signed in to change notification settings - Fork 146
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
Comments
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
|
so I solved this problem |
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
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:
The text was updated successfully, but these errors were encountered: