@@ -22,6 +22,24 @@ public protocol TableDraft: Table {
2222 init ( _ primaryTable: PrimaryTable )
2323}
2424
25+ extension TableDraft {
26+ public static subscript(
27+ dynamicMember keyPath: KeyPath < PrimaryTable . Type , some Statement < PrimaryTable > >
28+ ) -> some Statement < Self > {
29+ SQLQueryExpression ( " \( PrimaryTable . self [ keyPath: keyPath] ) " )
30+ }
31+
32+ public static subscript(
33+ dynamicMember keyPath: KeyPath < PrimaryTable . Type , some SelectStatementOf < PrimaryTable > >
34+ ) -> SelectOf < Self > {
35+ unsafeBitCast ( PrimaryTable . self [ keyPath: keyPath] . asSelect ( ) , to: SelectOf< Self> . self )
36+ }
37+
38+ public static var all : SelectOf < Self > {
39+ unsafeBitCast ( PrimaryTable . all. asSelect ( ) , to: SelectOf< Self> . self )
40+ }
41+ }
42+
2543/// A type representing a database table's columns.
2644///
2745/// Don't conform to this protocol directly. Instead, use the `@Table` and `@Column` macros to
@@ -38,6 +56,14 @@ where QueryValue: PrimaryKeyedTable {
3856 var primaryKey : TableColumn < QueryValue , PrimaryKey > { get }
3957}
4058
59+ extension TableDefinition where QueryValue: TableDraft {
60+ public subscript< Member> (
61+ dynamicMember keyPath: KeyPath < QueryValue . PrimaryTable . TableColumns , Member >
62+ ) -> Member {
63+ QueryValue . PrimaryTable. columns [ keyPath: keyPath]
64+ }
65+ }
66+
4167extension PrimaryKeyedTableDefinition {
4268 /// A query expression representing the number of rows in this table.
4369 ///
@@ -60,6 +86,22 @@ extension PrimaryKeyedTable {
6086 }
6187}
6288
89+ extension TableDraft {
90+ /// A where clause filtered by a primary key.
91+ ///
92+ /// - Parameter primaryKey: A primary key identifying a table row.
93+ /// - Returns: A `WHERE` clause.
94+ public static func find(
95+ _ primaryKey: PrimaryTable . TableColumns . PrimaryKey . QueryOutput
96+ ) -> Where < Self > {
97+ Self . where { _ in
98+ PrimaryTable . columns. primaryKey. eq (
99+ PrimaryTable . TableColumns. PrimaryKey ( queryOutput: primaryKey)
100+ )
101+ }
102+ }
103+ }
104+
63105extension Where where From: PrimaryKeyedTable {
64106 /// Adds a primary key condition to a where clause.
65107 ///
@@ -70,6 +112,40 @@ extension Where where From: PrimaryKeyedTable {
70112 }
71113}
72114
115+ extension Where where From: TableDraft {
116+ /// Adds a primary key condition to a where clause.
117+ ///
118+ /// - Parameter primaryKey: A primary key.
119+ /// - Returns: A where clause with the added primary key.
120+ public func find( _ primaryKey: From . PrimaryTable . TableColumns . PrimaryKey . QueryOutput ) -> Self {
121+ self . where { _ in
122+ From . PrimaryTable. columns. primaryKey. eq (
123+ From . PrimaryTable. TableColumns. PrimaryKey ( queryOutput: primaryKey)
124+ )
125+ }
126+ }
127+ }
128+
129+ extension Select where From: PrimaryKeyedTable {
130+ /// A select statement filtered by a primary key.
131+ ///
132+ /// - Parameter primaryKey: A primary key identifying a table row.
133+ /// - Returns: A select statement filtered by the given key.
134+ public func find( _ primaryKey: From . TableColumns . PrimaryKey . QueryOutput ) -> Self {
135+ self . and ( From . find ( primaryKey) )
136+ }
137+ }
138+
139+ extension Select where From: TableDraft {
140+ /// A select statement filtered by a primary key.
141+ ///
142+ /// - Parameter primaryKey: A primary key identifying a table row.
143+ /// - Returns: A select statement filtered by the given key.
144+ public func find( _ primaryKey: From . PrimaryTable . TableColumns . PrimaryKey . QueryOutput ) -> Self {
145+ self . and ( From . find ( primaryKey) )
146+ }
147+ }
148+
73149extension Update where From: PrimaryKeyedTable {
74150 /// An update statement filtered by a primary key.
75151 ///
@@ -80,6 +156,20 @@ extension Update where From: PrimaryKeyedTable {
80156 }
81157}
82158
159+ extension Update where From: TableDraft {
160+ /// An update statement filtered by a primary key.
161+ ///
162+ /// - Parameter primaryKey: A primary key identifying a table row.
163+ /// - Returns: An update statement filtered by the given key.
164+ public func find( _ primaryKey: From . PrimaryTable . TableColumns . PrimaryKey . QueryOutput ) -> Self {
165+ self . where { _ in
166+ From . PrimaryTable. columns. primaryKey. eq (
167+ From . PrimaryTable. TableColumns. PrimaryKey ( queryOutput: primaryKey)
168+ )
169+ }
170+ }
171+ }
172+
83173extension Delete where From: PrimaryKeyedTable {
84174 /// A delete statement filtered by a primary key.
85175 ///
@@ -90,12 +180,16 @@ extension Delete where From: PrimaryKeyedTable {
90180 }
91181}
92182
93- extension Select where From: PrimaryKeyedTable {
94- /// A select statement filtered by a primary key.
183+ extension Delete where From: TableDraft {
184+ /// A delete statement filtered by a primary key.
95185 ///
96186 /// - Parameter primaryKey: A primary key identifying a table row.
97- /// - Returns: A select statement filtered by the given key.
98- public func find( _ primaryKey: From . TableColumns . PrimaryKey . QueryOutput ) -> Self {
99- self . and ( From . find ( primaryKey) )
187+ /// - Returns: A delete statement filtered by the given key.
188+ public func find( _ primaryKey: From . PrimaryTable . TableColumns . PrimaryKey . QueryOutput ) -> Self {
189+ self . where { _ in
190+ From . PrimaryTable. columns. primaryKey. eq (
191+ From . PrimaryTable. TableColumns. PrimaryKey ( queryOutput: primaryKey)
192+ )
193+ }
100194 }
101195}
0 commit comments