跟新 *** 作
let update = users.update(email <- "dream@qq.com", phone <- "88888888")
print(update.asSQL())
try! db.run(update)
更新SQL即更新所有
public struct Update : ExpressionType {
public var template: String
public var bindings: [Binding?]
public init(_ template: String, _ bindings: [Binding?]) {
self.template = template
self.bindings = bindings
}
}
扩展 QueryType
添加update
方法
public func update(_ values: Setter...) -> Update {
return update(values)
}
public func update(_ values: [Setter]) -> Update {
//SQL语句分段存储
//第一个元素:Expression(literal: "UPDATE")->对应字符串->"update"
//第二个元素:tableName()->对应表名称->"users"
//第三个元素:Expression(literal: "SET")->对应->"set"
//第四个元素:", ".join(values.map { " = ".join([$0.column, $0.value]) })->字段
//例如:email = 'dream@qq.com', phone = '88888888'
//第四个元素步骤->首先是拼接"="号,其次在拼接","号
//形成完整SQL语句:update users set email = 'dream@qq.com', phone = '88888888'
//注意:这么多的表达式,最终都会组装为一个表达式
let clauses: [Expressible?] = [
Expression<Void>(literal: "UPDATE"),
tableName(),
Expression<Void>(literal: "SET"),
", ".join(values.map { " = ".join([$0.column, $0.value]) }),
]
//join拼接SQL语句相关
//注意:每一个关键字之间需要" "空格隔开
return Update(" ".join(clauses.compactMap { $0 }).expression)
}
扩展Connection
添加run
方法,执行SQL语句
@discardableResult public func run(_ query: Update) throws -> Int {
//表达式
//SQL->expression
let expression = query.expression
//队列->同步代码块
return try sync {
try self.run(expression.template, expression.bindings)
//返回执行结果
return self.changes
}
}
2、条件更新(where)
更新SQL,添加条件
let filter = users.filter(name == "hello")
try! db.run(filter.update(email <- "dream@qq.com"))
扩展 QueryType
添加条件方法filter
public func filter(_ predicate: Expression<Bool>) -> Self {
return filter(Expression<Bool?>(predicate))
}
//SQL子句(排序->order by,分组->group...)
public func filter(_ predicate: Expression<Bool?>) -> Self {
var query = self
//核心:也就是拼接条件
//条件一:id = 1
//条件二:name = "NSLog"
//where id = 1 and name = "NSLog"
query.clauses.filters = query.clauses.filters.map { && } predicate ?? return predicate
} query
QueryClauses
注意一:Value增加一个Bool类型Bool
过滤条件结构体,增加一个filters
类型var
: filtersExpression <Bool//增加一个Int64类型?>?
数据库里面,Bool类型用Int表示
extension
//注意:Int64->对应数据库类型->INTEGER
Int64 : Number ,Value //数据库类型 {
public
static let = declaredDatatype "INTEGER" public
static func fromDatatypeValue (_: datatypeValueInt64 )Int64 -> return {
} datatypeValue
public
var : datatypeValueInt64 return {
self }
}
extension
//注意:数据库里面->Bool类型->0,1分别表示true、false,最终还是Int类型
Bool : Binding ,Value public {
static var = declaredDatatype Int64 .publicdeclaredDatatype
static func fromDatatypeValue (_: datatypeValueInt64 )Bool -> return {
!= datatypeValue 0 }
public
var : datatypeValueInt64 return {
self ? 1 : 0 }
}
&&
注意二:条件运算符:“public”区分系统->方法重载(运算符重载)->方法名称相同,但是参数列表不同(类型不同)
func && (:lhsExpression <Bool,>: rhsExpression <Bool)>Expression -> <Boolreturn> {
"AND" .infix(,lhs) rhs}
public
func && (:lhsExpression <Bool,>: rhsExpression <Bool)?>Expression -> <Boolreturn?> {
"AND" .infix(,lhs) rhs}
public
func && (:lhsExpression <Bool,?>: rhsExpression <Bool)>Expression -> <Boolreturn?> {
"AND" .infix(,lhs) rhs}
public
func && (:lhsExpression <Bool,?>: rhsExpression <Bool)?>Expression -> <Boolreturn?> {
"AND" .infix(,lhs) rhs}
public
func && (:lhsExpression <Bool,>: rhsBool )Expression -> <Boolreturn> {
"AND" .infix(,lhs) rhs}
public
func && (:lhsExpression <Bool,?>: rhsBool )Expression -> <Boolreturn?> {
"AND" .infix(,lhs) rhs}
public
func && (:lhsBool ,: rhsExpression <Bool)>Expression -> <Boolreturn> {
"AND" .infix(,lhs) rhs}
public
func && (:lhsBool ,: rhsExpression <Bool)?>Expression -> <Boolreturn?> {
"AND" .infix(,lhs) rhs}
==
注意三:条件运算符:“//例如:”
//"="中缀->infix
//name属性
//NSError
//name = 'NSError'
public
func ==< V: Value (>:lhsExpression <V,>: rhsExpression <V)>Expression -> <Boolwhere> V .Datatype: Equatable return {
"=" .infix(,lhs) rhs}
public
func ==< V: Value (>:lhsExpression <V,>: rhsExpression <V)?>Expression -> <Boolwhere?> V .Datatype: Equatable return {
"=" .infix(,lhs) rhs}
public
func ==< V: Value (>:lhsExpression <V,?>: rhsExpression <V)>Expression -> <Boolwhere?> V .Datatype: Equatable return {
"=" .infix(,lhs) rhs}
public
func ==< V: Value (>:lhsExpression <V,?>: rhsExpression <V)?>Expression -> <Boolwhere?> V .Datatype: Equatable return {
"=" .infix(,lhs) rhs}
public
func ==< V: Value (>:lhsExpression <V,>: rhsV )Expression -> <Boolwhere> V .Datatype: Equatable return {
"=" .infix(,lhs) rhs}
public
func ==< V: Value (>:lhsExpression <V,?>: rhsV ?)Expression -> <Boolwhere?> V .Datatype: Equatable guard {
let = rhs else rhs return { "IS" .infix(,lhsExpression <V(?>:valuenil ))} return
"=" .infix(,lhs) rhs}
public
func ==< V: Value (>:lhsV ,: rhsExpression <V)>Expression -> <Boolwhere> V .Datatype: Equatable return {
"=" .infix(,lhs) rhs}
public
func ==< V: Value (>:lhsV ?,: rhsExpression <V)?>Expression -> <Boolwhere?> V .Datatype: Equatable guard {
let = lhs else lhs return { "IS" .infix(Expression<V(?>:valuenil ),) rhs} return
"=" .infix(,lhs) rhs}
replace
多支持几个类型,系统本身就是基本数据类型,让它支持对象类型,相当于给系统扩展功能
3、内容替换(replace)在数据库里面也有替换SQL语法:= ,并且只有String字段才可以replace
let filter . usersfilter(==id "1" )!
try. dbrun(.filterupdate(<name -. namereplace("r",: with"i" )))QueryType
注意一:替换的谁?替换的是表达式:字段,所以说功能在字段身上注意二:替换位置限定:从头开始查找替换
从左边往右边依次查找,本质上和编程开发一样的顺序 删除数据 1、删除所有
- 扩展delete()
增加删除所有数据方法public
func delete ()Delete -> let {
: clauses[ Expressible?]= [ Expression
<Void(>:literal"DELETE FROM" ),tableName
(),]
return
Delete (" ".join(.clauses}compactMap { ) .)}expressionWHERE
whereClause
2、条件删除(where条件)
增加fileprivate条件表达式var
: Expressible whereClause? guardlet {
= . filters else clausesreturnfilters nil {
} return
" "
. join([Expression<
Void(:>"WHERE"literal) ,])
filters
}whereClause
update()
把delete()
放入public和func
update ( _:[ valuesSetter ])Updatelet -> : {
[ clausesExpressible ?]=[ Expression <
Void(:>"UPDATE"literal) ,tableName(
),Expression<
Void(:>"SET"literal) ,", ".
join(." = "values.map { join([.,.]column) })value,] //join拼接SQL语句相关//注意:每一个关键字之间需要" "空格隔开return
whereClause
Update
(
" "
. join(.}).clauses)compactMap { } publicfuncdeleteexpression(
)
Delete let :[Expressible -> ? {
] clauses= [Expression<Void ( :
"DELETE FROM"),>tableNameliteral( ),]
returnDelete(" "
whereClause
.
join (.}).)clauses}compactMap { expression
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)