-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathConvenience.swift
More file actions
107 lines (91 loc) · 2.76 KB
/
Convenience.swift
File metadata and controls
107 lines (91 loc) · 2.76 KB
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
//
// Convenience.swift
// PostgresSTORM
//
// Created by Jonathan Guthrie on 2016-10-04.
//
//
import StORM
import PerfectLogger
/// Convenience methods extending the main class.
extension MySQLStORM {
/// Deletes one row, with an id.
/// Presumes first property in class is the id.
public func delete() throws {
let (idname, idval) = firstAsKey()
do {
try exec(deleteSQL(self.table(), idName: idname), params: [String(describing: idval)])
} catch {
LogFile.error("Error msg: \(error)", logFile: StORMDebug.location)
self.error = StORMError.error("\(error)")
throw error
}
}
/// Deletes one row, with the id as set.
public func delete(_ id: Any) throws {
let (idname, _) = firstAsKey()
do {
try exec(deleteSQL(self.table(), idName: idname), params: [String(describing: id)])
} catch {
LogFile.error("Error msg: \(error)", logFile: StORMDebug.location)
self.error = StORMError.error("\(error)")
throw error
}
}
/// Retrieves a single row with the supplied ID.
public func get(_ id: Any) throws {
let (idname, _) = firstAsKey()
do {
try select(whereclause: "\(idname) = ?", params: [String(describing: id)], orderby: [])
} catch {
LogFile.error("Error msg: \(error)", logFile: StORMDebug.location)
self.error = StORMError.error("\(error)")
throw error
}
}
/// Retrieves a single row with the ID as set.
public func get() throws {
let (idname, idval) = firstAsKey()
do {
try select(whereclause: "\(idname) = ?", params: [String(describing: idval)], orderby: [])
} catch {
LogFile.error("Error msg: \(error)", logFile: StORMDebug.location)
self.error = StORMError.error("\(error)")
throw error
}
}
/// Performs a find on mathing column name/value pairs.
public func find(_ data: [(String, Any)]) throws {
let (idname, _) = firstAsKey()
var paramsString = [String]()
var set = [String]()
for i in data {
paramsString.append(String(describing: i.1))
set.append("\(i.0) = ?")
}
do {
try select(whereclause: set.joined(separator: " AND "), params: paramsString, orderby: [idname])
} catch {
LogFile.error("Error msg: \(error)", logFile: StORMDebug.location)
self.error = StORMError.error("\(error)")
throw error
}
}
/// Performs a find on mathing column name/value pairs.
public func find(_ data: [String: Any]) throws {
let (idname, _) = firstAsKey()
var paramsString = [String]()
var set = [String]()
for (key, value) in data {
paramsString.append(String(describing: value))
set.append("\(key) = ?")
}
do {
try select(whereclause: set.joined(separator: " AND "), params: paramsString, orderby: [idname])
} catch {
LogFile.error("Error msg: \(error)", logFile: StORMDebug.location)
self.error = StORMError.error("\(error)")
throw error
}
}
}