1
1
/// Represents a PostgreSQL column.
2
- public struct PostgreSQLColumn : Hashable {
3
- /// See `Hashable.hashValue`
4
- public var hashValue : Int {
5
- return ( ( table ?? " _ " ) + " . " + name) . hashValue
6
- }
7
-
2
+ public struct PostgreSQLColumn : Hashable , Equatable {
8
3
/// The table this column belongs to.
9
- public var table : String ?
4
+ public var tableOID : Int32
10
5
11
6
/// The column's name.
12
7
public var name : String
13
8
}
14
9
15
10
extension PostgreSQLColumn : CustomStringConvertible {
16
11
public var description : String {
17
- if let table = table {
18
- return " \( table) ( \( name) ) "
19
- } else {
20
- return " \( name) "
21
- }
12
+ return " < \( tableOID) >.( \( name) ) "
22
13
}
23
14
}
24
15
@@ -35,50 +26,7 @@ extension Dictionary where Key == PostgreSQLColumn {
35
26
36
27
/// Access a `Value` from this dictionary keyed by `PostgreSQLColumn`s
37
28
/// using a field (column) name and entity (table) name.
38
- public func value( forTable table: String , atColumn column: String ) -> Value ? {
39
- return self [ PostgreSQLColumn ( table: table, name: column) ]
40
- }
41
- }
42
-
43
- import Async
44
-
45
- final class PostgreSQLTableNameCache {
46
- var connection : Future < PostgreSQLConnection >
47
- var cache : [ Int32 : String ? ]
48
- var working : Bool
49
-
50
- init ( connection: Future < PostgreSQLConnection > ) {
51
- self . connection = connection
52
- self . cache = [ : ]
53
- self . working = false
54
- }
55
-
56
- func tableName( oid: Int32 ) throws -> String ? {
57
- if oid == 0 {
58
- return nil
59
- }
60
-
61
- if working {
62
- cache [ oid] = " pg_class "
63
- return " pg_class "
64
- }
65
- working = true
66
- defer { working = false }
67
- if let existing = cache [ oid] {
68
- return existing
69
- } else {
70
- let res = try connection
71
- . wait ( )
72
- . simpleQuery ( " select relname from pg_class where oid = \( oid) " )
73
- . wait ( )
74
- let new : String ?
75
- if res. count > 0 {
76
- new = try res [ 0 ] . firstValue ( forColumn: " relname " ) !. decode ( String . self)
77
- } else {
78
- new = nil
79
- }
80
- cache [ oid] = new
81
- return new
82
- }
29
+ public func value( forTableOID tableOID: Int32 , atColumn column: String ) -> Value ? {
30
+ return self [ PostgreSQLColumn ( tableOID: tableOID, name: column) ]
83
31
}
84
32
}
0 commit comments