Closed 0x1306a94 closed 2 months ago
Winq does not support raw string expression
@Qiuwen-chen Can subsequent versions consider supporting NSCompoundPredicate?
@Qiuwen-chen Can subsequent versions consider supporting NSCompoundPredicate?
NO
@Qiuwen-chen I mean support for NSCompoundPredicate-like syntax. For example, you can convert a list into a set of AND or OR
func test(orderId: String, ticketCode: String?) {
var condition = Ticket.Properties.orderId == orderId
var dynamicConditions: [ExpressionConvertible] = []
if let ticketCode, !ticketCode.isEmpty {
dynamicConditions.append(Ticket.Properties.ticketCode == ticketCode)
dynamicConditions.append(Ticket.Properties.telephone == "dddd")
}
let finalCondition = condition && dynamicConditions.and()
// sql: (orderId = 'dd' AND (ticketCode = 'xx' AND telephone = 'ddd'))
// let finalCondition = condition && dynamicConditions.or()
// sql: (orderId = 'dd' AND (ticketCode = 'xx' OR telephone = 'ddd'))
print("sql: \(condition.description)")
}
func testPlaceholder(orderId: String?, ticketCode: String?) {
var condition = WCDBSwift.Condition.Placeholder // sql:
if let orderId, let ticketCode {
condition = condition && Ticket.Properties.orderId == orderId &&
Ticket.Properties.ticketCode == ticketCode
// sql: (orderId = 'xx' AND ticketCode = 'xx')
}
}
@Qiuwen-chen I mean support for NSCompoundPredicate-like syntax. For example, you can convert a list into a set of AND or OR
func test(orderId: String, ticketCode: String?) { var condition = Ticket.Properties.orderId == orderId var dynamicConditions: [ExpressionConvertible] = [] if let ticketCode, !ticketCode.isEmpty { dynamicConditions.append(Ticket.Properties.ticketCode == ticketCode) dynamicConditions.append(Ticket.Properties.telephone == "dddd") } let finalCondition = condition && dynamicConditions.and() // sql: (orderId = 'dd' AND (ticketCode = 'xx' AND telephone = 'ddd')) // let finalCondition = condition && dynamicConditions.or() // sql: (orderId = 'dd' AND (ticketCode = 'xx' OR telephone = 'ddd')) print("sql: \(condition.description)") } func testPlaceholder(orderId: String?, ticketCode: String?) { var condition = WCDBSwift.Condition.Placeholder // sql: if let orderId, let ticketCode { condition = condition && Ticket.Properties.orderId == orderId && Ticket.Properties.ticketCode == ticketCode // sql: (orderId = 'xx' AND ticketCode = 'xx') } }
It seems that you can do it outside of wcdb
@Qiuwen-chen
It just doesn't work?
That's why it needs support in wcdb!
Although this can be achieved through the following extension, Expression(nilLiteral: ())
will output NULL
and Expression(stringLiteral: "")
will output ''
extension Collection where Element: ExpressionConvertible {
func and() -> ExpressionConvertible {
if self.isEmpty {
return Expression(nilLiteral: ())
}
let finalExpression = self.reduce(Expression(nilLiteral: ())) { $0 && $ 1 }
return finalExpression
}
func or() -> ExpressionConvertible {
if self.isEmpty {
return Expression(nilLiteral: ())
}
let finalExpression = self.reduce(Expression(nilLiteral: ())) { $0 || $ 1 }
return finalExpression
}
}
You need a empty expression, may be you can try Expression(booleanLiteral: true)
@Qiuwen-chen
In addition, I noticed that the latest version 2.1.7 already supports the OHOS platform. Does this mean that the 2.1.7 version C++ API can be fully used on the OHOS platform?
extension Collection where Element: ExpressionConvertible {
func and() -> ExpressionConvertible? {
guard let first = self.first else { return nil }
guard count > 1 else { return first }
let remainder = dropFirst()
let finalExpression = remainder.reduce(first.asExpression()) { $0 && $1 }
return finalExpression
}
func or() -> ExpressionConvertible? {
guard let first = self.first else { return nil }
guard count > 1 else { return first }
let remainder = dropFirst()
let finalExpression = remainder.reduce(first.asExpression()) { $0 || $1 }
return finalExpression
}
}
Yes, WCDB C++ can be fully used on the OHOS platform. NSCompoundPredicate-like syntax is not a normal sql expression, I don't want to support this officially.
The language of WCDB
The version of WCDB
The platform of WCDB
The installation of WCDB
What's the issue?
Is it possible to support writing similar to NSCompoundPredicate? I used the following extension, and the generated sql should be illegal. I checked the test code of the Swift part in the repository, but couldn't find how to write this.
For example, some dynamic conditions are very cumbersome to write using the following method