forked from vapor/postgres-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostgreSQLColumn.swift
38 lines (34 loc) · 1.16 KB
/
PostgreSQLColumn.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
/// Represents a PostgreSQL column.
public struct PostgreSQLColumn: Hashable, Equatable {
/// The table this column belongs to.
public var tableOID: UInt32
/// The column's name.
public var name: String
/// Creates a new `PostgreSQLColumn`.
public init(tableOID: UInt32 = 0, name: String) {
self.tableOID = tableOID
self.name = name
}
}
extension PostgreSQLColumn: CustomStringConvertible {
/// See `CustomStringConvertible`.
public var description: String {
switch tableOID {
case 0: return name
default: return tableOID.description + "." + name
}
}
}
extension Dictionary where Key == PostgreSQLColumn {
/// Accesses the _first_ value from this dictionary with a matching field name.
///
/// - Note: This performs a linear search over the dictionary and thus is fairly slow.
public func firstValue(tableOID: UInt32 = 0, name: String) -> Value? {
for (column, data) in self {
if (tableOID == 0 || column.tableOID == 0 || column.tableOID == tableOID) && column.name == name {
return data
}
}
return nil
}
}