-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathConvenience.swift
More file actions
110 lines (93 loc) · 3.19 KB
/
Convenience.swift
File metadata and controls
110 lines (93 loc) · 3.19 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
108
109
110
//
// Convenience.swift
// PostgresStORM
//
// Created by Jonathan Guthrie on 2016-10-04.
//
//
import StORM
import PerfectLogger
/// Convenience methods extending the main class.
extension PostgresStORM {
/// 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.lowercased()), params: [String(describing: idval)])
} catch {
LogFile.error("Error: \(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.lowercased()), params: [String(describing: id)])
} catch {
LogFile.error("Error: \(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.lowercased())\" = $1", params: [id], orderby: [])
} catch {
LogFile.error("Error: \(error)", logFile: StORMDebug.location)
throw error
}
}
/// Retrieves a single row with the ID as set.
public func get() throws {
let (idname, idval) = firstAsKey()
do {
try select(whereclause: "\"\(idname.lowercased())\" = $1", params: ["\(idval)"], orderby: [])
} catch {
LogFile.error("Error: \(error)", logFile: StORMDebug.location)
throw error
}
}
/// Performs a find on mathing column name/value pairs.
/// An optional `cursor:StORMCursor` object can be supplied to determine pagination through a larger result set.
/// For example, `try find([("username","joe")])` will find all rows that have a username equal to "joe"
public func find(_ data: [(String, Any)], cursor: StORMCursor = StORMCursor()) throws {
let (idname, _) = firstAsKey()
var paramsString = [String]()
var set = [String]()
for i in 0..<data.count {
paramsString.append("\(data[i].1)")
set.append("\(data[i].0.lowercased()) = $\(i+1)")
}
do {
try select(whereclause: set.joined(separator: " AND "), params: paramsString, orderby: [idname], cursor: cursor)
} catch {
LogFile.error("Error: \(error)", logFile: StORMDebug.location)
throw error
}
}
/// Performs a find on mathing column name/value pairs.
/// An optional `cursor:StORMCursor` object can be supplied to determine pagination through a larger result set.
/// For example, `try find(["username": "joe"])` will find all rows that have a username equal to "joe"
public func find(_ data: [String: Any], cursor: StORMCursor = StORMCursor()) throws {
let (idname, _) = firstAsKey()
var paramsString = [String]()
var set = [String]()
var counter = 0
for i in data.keys {
paramsString.append(data[i] as! String)
set.append("\(i.lowercased()) = $\(counter+1)")
counter += 1
}
do {
try select(whereclause: set.joined(separator: " AND "), params: paramsString, orderby: [idname], cursor: cursor)
} catch {
LogFile.error("Error: \(error)", logFile: StORMDebug.location)
throw error
}
}
}