DatabaseManager
public class DatabaseManager : @unchecked Sendable
本地数据库管理类
备注:查询条件、排序条件、限制条件等语法和SQL语法一致,为空则无条件
-
全局数据库模型版本号,默认1.0
如果模型实现了databaseVersion且不为空,则会忽略全局版本号; 可设置为appVersion+appBuildVersion从而实现App升级时自动更新版本号
Declaration
Swift
public static var version: String { get set }
-
是否打印调试SQL语句,默认true
Declaration
Swift
public static var printSql: Bool { get set }
-
保存模型到本地,主键存在时更新,不存在时新增
Declaration
Swift
@discardableResult public static func save(_ model: DatabaseModel) -> Bool
-
新增模型数组到本地(事务方式),模型数组对象类型要一致
Declaration
Swift
@discardableResult public static func inserts(_ models: [DatabaseModel]) -> Bool
-
新增模型到本地,自动更新主键
Declaration
Swift
@discardableResult public static func insert(_ model: DatabaseModel) -> Bool
-
获取模型类表总条数,支持查询条件
Declaration
Swift
public static func count<T>(_ type: T.Type, where condition: String? = nil) -> Int where T : DatabaseModel
-
查询本地模型对象,支持查询条件、排序条件、限制条件
示例: 对person数据表查询age小于30岁并且根据age自动降序或者升序排序并且限制查询的数量为8偏移为8 [DatabaseManager query:[Person class] where:@“age <= 30” order:@“age desc/asc” limit:@“8 offset 8”];
Declaration
Swift
public static func query<T>(_ type: T.Type, where condition: String? = nil, order: String? = nil, limit: String? = nil) -> [T] where T : DatabaseModel
Parameters
type
模型类
where
查询条件(查询语法和SQL where 查询语法一样,where为空则查询所有)
order
排序条件(排序语法和SQL order 查询语法一样,order为空则不排序)
limit
限制条件(限制语法和SQL limit 查询语法一样,limit为空则不限制查询)
Return Value
查询模型对象数组
-
自定义sql查询
Declaration
Swift
public static func query<T>(_ type: T.Type, sql: String) -> [T] where T : DatabaseModel
-
根据主键查询本地模型对象
Declaration
Swift
public static func query<T>(_ type: T.Type, key: Int) -> T? where T : DatabaseModel
-
利用sqlite 函数进行查询,condition为其他查询条件例如:(where age > 20 order by age desc ….)
Declaration
Swift
public static func query<T>(_ type: T.Type, func function: String, condition: String? = nil) -> Any? where T : DatabaseModel
-
更新本地模型对象,主键必须存在
Declaration
Swift
@discardableResult public static func update(_ model: DatabaseModel) -> Bool
-
更新数据表字段,where为空则更新所有
Declaration
Swift
@discardableResult public static func update<T>(_ type: T.Type, value: T, where condition: String? = nil) -> Bool where T : DatabaseModel
-
更新数据表字段,where为空则更新所有
Declaration
Swift
@discardableResult public static func update<T>(_ type: T.Type, value: String, where condition: String? = nil) -> Bool where T : DatabaseModel
-
清空本地模型对象
Declaration
Swift
@discardableResult public static func clear<T>(_ type: T.Type) -> Bool where T : DatabaseModel
-
根据主键删除本地模型对象,主键必须存在
Declaration
Swift
@discardableResult public static func delete(_ model: DatabaseModel) -> Bool
-
删除本地模型对象
Declaration
Swift
@discardableResult public static func delete<T>(_ type: T.Type, where condition: String? = nil) -> Bool where T : DatabaseModel
-
清空所有本地模型数据库
Declaration
Swift
public static func removeAllModels()
-
清空指定本地模型数据库
Declaration
Swift
public static func removeModel<T>(_ type: T.Type) where T : DatabaseModel
-
返回本地模型数据库路径
Declaration
Swift
public static func localPath<T>(with type: T.Type) -> String? where T : DatabaseModel
-
返回本地模型数据库版本号
Declaration
Swift
public static func version<T>(with type: T.Type) -> String? where T : DatabaseModel