[英]Correct variable binding and avoiding SQL injection with SQLite.swift queries
[英]Sqlite.swift create dynamic complex queries
我有1個具有多列的表。 在應用程序上,我們期待添加4個動態濾鏡,例如(貓,大小,顏色,形狀)。
我們知道我們可以像這樣為sqllite創建一個過濾器:
user = user.select(name)
.filter((color == "Blue") && (size = "Big") && (cat="a") && (shape="round"))
.order(name.asc, name) // ORDER BY "email" DESC, "name"
.limit(5, offset: 0)
但是,如果使用濾鏡,那會發生什么呢?假設對於顏色,我們要搜索所有顏色。 然后,
.filter((color == "?????") && (size = "Big") && (cat="a") && (shape="round"))
關於如何為這種情況創建動態過濾器的任何想法?
filter()
方法使用Expression<Bool>
參數,並且可以使用邏輯運算符&&
, ||
動態創建復合表達式。 等
簡單的例子:
// Start with "true" expression (matches all records):
var myFilter = Expression<Bool>(value: true)
// Dynamically add boolean expressions:
if shouldFilterColor {
myFilter = myFilter && (color == "Blue")
}
if shouldFilterSize {
myFilter = myFilter && (size == "Big")
}
// ... etc ...
// Use compound filter:
query = user.select(name)
.filter(myFilter)
.order(name.asc, name)
.limit(5, offset: 0)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.