数据库模型映射,自动建表, 自动更新表,数据增删改查, FTS全文搜索, 支持自定义fts3,4,5分词器,可拼音分词. sql,fmdb,wcdb,sqlite3,orm,fts,fts3,fts4,fts5. VVSequelize的swift版本
fts5_api_ptr
获取。updateAt
字段受fields
限定。pod 'SQLiteORM', '~> 0.1.8'
使用测试版本:
pod 'SQLiteORM', :git => 'https://github.com/pozi119/SQLiteORM.git'
此处主要列出一些基本用法,详细用法请阅读代码注释.
let db = Database(with: path)
PlainConfig
/ FtsConfig
.Ormable
, fts表适配协议Ftsable
注意: 已移除fts3/4的支持,仅使用fts5
可自定义表名和存放的数据库文件.
注意: SQL关键字不能用于表名
示例如下:
let groupOrm = Orm(ormable: Group.self, db: db, table: "xgroup")
使用ORM模型进行增删改查等操作.
示例如下:
let total = groupOrm.count()
let groups = groupOrm.find().where {
fetch.name.length > 0 ? E.name.like(fetch.name) : ""
}.orderBy {
fetch.collation == .addFirst ? E.createAt.desc : E.updateAt.desc
}.offset {
max(fetch.current - 1, 0) * fetch.size
}.limit {
fetch.size
}.allItems()
let groups = groupOrm.find().where { E.id.in(ids) }.allItems()
let ret = groupOrm.update(with: [E.name: name, E.updateAt: now]) { E.id |== id }
...
infix operator |==: ComparisonPrecedence // ==
infix operator |!=: ComparisonPrecedence // !=
infix operator |>: ComparisonPrecedence // >
infix operator |>=: ComparisonPrecedence // >=
infix operator |<: ComparisonPrecedence // <
infix operator |<=: ComparisonPrecedence // <=
infix operator |!>: ComparisonPrecedence // !>
infix operator |!<: ComparisonPrecedence // !<
infix operator |<>: ComparisonPrecedence // <>
infix operator |&&: LogicalConjunctionPrecedence // &&
infix operator |||: LogicalConjunctionPrecedence // ||
postfix operator ↑
postfix operator ↓
示例:
func testWhereLogic() {
let w1 = "name" |== "zhangsan"
let w2 = "age" |== 22
let w3 = w1 |&& w2
let w4 = w3 ||| "age" |== 21
let w5 = w4 |&& "name" |== "lisi"
let w6 = w5 ||| "age" |== 20
XCTAssertEqual(w1, "\"name\" == \"zhangsan\"")
XCTAssertEqual(w2, "\"age\" == 22")
XCTAssertEqual(w3, "(\"name\" == \"zhangsan\") AND (\"age\" == 22)")
XCTAssertEqual(w4, "((\"name\" == \"zhangsan\") AND (\"age\" == 22)) OR (\"age\" == 21)")
XCTAssertEqual(w5, "(((\"name\" == \"zhangsan\") AND (\"age\" == 22)) OR (\"age\" == 21)) AND (\"name\" == \"lisi\")")
XCTAssertEqual(w6, "((((\"name\" == \"zhangsan\") AND (\"age\" == 22)) OR (\"age\" == 21)) AND (\"name\" == \"lisi\")) OR (\"age\" == 20)")
}
为 String 添加了一些计算属性
比如: asc
desc
为 String 添加了一些函数
比如: like
glob
in
between
示例:
func testWhereExp() {
let v1 = "age".like(22)
let v2 = "age".notLike(22)
let v3 = "age".match(22)
let v4 = "age".glob(22)
let v5 = "age".notGlob(22)
let v6 = "age".is(22)
let v7 = "age".isNot(22)
let v8 = "age".exists(22)
let v9 = "age".notExists(22)
let v10 = "age".isNull()
let v11 = "age".between((20, 30))
let v12 = "age".notBetween((20, 30))
let v13 = "age".in([21, 22, 25, 28])
let v14 = "age".notIn([21, 22, 25, 28])
XCTAssertEqual(v1, "\"age\" LIKE \"%22%\"")
XCTAssertEqual(v2, "\"age\" NOT LIKE \"%22%\"")
XCTAssertEqual(v3, "\"age\" MATCH 22")
XCTAssertEqual(v4, "\"age\" GLOB \"*22*\"")
XCTAssertEqual(v5, "\"age\" NOT GLOB \"*22*\"")
XCTAssertEqual(v6, "\"age\" IS 22")
XCTAssertEqual(v7, "\"age\" IS NOT 22")
XCTAssertEqual(v8, "\"age\" EXISTS 22")
XCTAssertEqual(v9, "\"age\" NOT EXISTS 22")
XCTAssertEqual(v10, "\"age\" IS NULL")
XCTAssertEqual(v11, "\"age\" BETWEEN 20 AND 30")
XCTAssertEqual(v12, "\"age\" NOT BETWEEN 20 AND 30")
XCTAssertEqual(v13, "\"age\" IN (21,22,25,28)")
XCTAssertEqual(v14, "\"age\" NOT IN (21,22,25,28)")
}
db.query("SELECT * FROM 'xgroup';")
db.excute("UPDATE xgroup SET 'name' = '分组1' WHERE 'id' = 1;")
Valo Lee, [email protected]
SQLiteORM is available under the MIT license. See the LICENSE file for more info.