-
Notifications
You must be signed in to change notification settings - Fork 2
/
SQLUpdateOperation.swift
110 lines (90 loc) · 4.02 KB
/
SQLUpdateOperation.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//
// Created by Jeffrey Roberts on 7/18/15.
// Copyright (c) 2015 NimbleNoggin.io. All rights reserved.
//
import Foundation
import SwiftyBeaver
/**
**SQLUpdateOperation** represents a SQL INSERT, UPDATE or DELETE operation that either inserts, updates
or deletes in a single database table.
*/
@objc
public class SQLUpdateOperation: SQLiteOperation {
private let logger = SwiftyBeaver.self
/// An optional map of values that will be inserted/updated where the key is the column name and
/// the value is the column value
public var contentValues:[String: AnyObject]?
public override init(database: SQLiteDatabase, statementBuilder: SQLStatementBuilder) {
super.init(database: database, statementBuilder: statementBuilder)
}
/// executeDelete(): Deletes zero or more rows from a database table
/// - Returns: The count of rows deleted after execution of the statement
public func executeDelete() throws -> Int {
guard let table = self.tableName else {
throw SQLError.MissingTableName
}
let hasNamedParameters = self.namedSelectionArgs != nil
let statement = self.statementBuilder.buildDeleteStatement(table, selection: self.selection)
if hasNamedParameters {
logger.debug("Executing \(statement) with named parameters: \(self.namedSelectionArgs)")
return try self.database.executeUpdate(statement, parameters:self.namedSelectionArgs)
} else {
logger.debug("Executing \(statement) with parameters: \(self.selectionArgs)")
return try self.database.executeUpdate(statement, parameters:self.selectionArgs)
}
}
/// executeInsert(): Inserts a row into a database table
/// - Returns: The count of rows inserted after execution of the statement, should be 1
public func executeInsert() throws -> Int {
guard let values = self.contentValues else {
throw SQLError.MissingContentValues
}
guard let table = self.tableName else {
throw SQLError.MissingTableName
}
let statement = self.statementBuilder.buildInsertStatement(table, columnNames: Array(values.keys), useNamedParameters: true)
logger.debug("Executing \(statement) with values: \(values)")
return try self.database.executeUpdate(statement, parameters: values)
}
/// executeUpdate(): Updates zero or more rows in a database table
/// - Returns: The count of rows updated after execution of the statement
public func executeUpdate() throws -> Int {
guard let values = self.contentValues else {
throw SQLError.MissingContentValues
}
guard let table = self.tableName else {
throw SQLError.MissingTableName
}
let hasNamedParameters = self.namedSelectionArgs != nil
let statement = self.statementBuilder.buildUpdateStatement(
table,
updatingColumnNames: Array(values.keys),
selection: self.selection,
useNamedParameters: hasNamedParameters
)
if hasNamedParameters {
let mergedValues = self.merge(values, second:self.namedSelectionArgs)
logger.debug("Executing \(statement) with named parameters: \(mergedValues)")
return try self.database.executeUpdate(statement, parameters:mergedValues)
} else {
logger.debug("Executing \(statement) with parameters: \(self.selectionArgs)")
return try self.database.executeUpdate(statement, parameters:self.selectionArgs)
}
}
internal func merge(first:[String:AnyObject]?, second:[String:AnyObject]?) -> [String:AnyObject]? {
if first == nil && second != nil {
return second
}
if second == nil && first != nil {
return first
}
var combined = [String:AnyObject]()
for (key, value) in first! {
combined[key] = value
}
for (key, value) in second! {
combined[key] = value
}
return combined
}
}