Skip to content

Commit 366138a

Browse files
committed
wip
1 parent 93e4379 commit 366138a

File tree

12 files changed

+51
-44
lines changed

12 files changed

+51
-44
lines changed

Sources/StructuredQueriesCore/Documentation.docc/Articles/CommonTableExpressions.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ or recursive queries of trees and graphs.
66
## Overview
77

88
One can build [common table expressions][] (commonly referred to as CTEs) by using the ``With``
9-
statement, along with the `@Table` macro. CTEs allow you to refactor complex queries into smaller
10-
pieces, and they allow you to execute recursive queries that can traverse tree-like and graph-like
11-
tables.
9+
statement, along with the `@Selection` macro. CTEs allow you to refactor complex queries into
10+
smaller pieces, and they allow you to execute recursive queries that can traverse tree-like and
11+
graph-like tables.
1212

1313
[common table expressions]: https://sqlite.org/lang_with.html
1414

@@ -47,10 +47,10 @@ selecting only lists whose average priority of reminders is greater than 1.5 int
4747
that can then be used in another query.
4848

4949
One can define a new type that represents the data you want to pre-compute as a CTE, and you
50-
annotate the type with the `@Table`:
50+
annotate the type with `@Selection`:
5151

5252
```swift
53-
@Table
53+
@Selection
5454
struct HighPriorityRemindersList {
5555
let id: RemindersList.ID
5656
}
@@ -173,7 +173,7 @@ As a simple example let's construct a query that selects the numbers from 1 to 1
173173
by defining a CTE data type that holds the data we want to compute:
174174

175175
```swift
176-
@Table
176+
@Selection
177177
struct Counts {
178178
let value: Int
179179
}
@@ -271,7 +271,7 @@ Fibonacci number, as well as the previous Fibonacci number:
271271
[recurrence relation]: https://en.wikipedia.org/wiki/Recurrence_relation
272272

273273
```swift
274-
@Table
274+
@Selection
275275
private struct Fibonacci {
276276
let n: Int
277277
let prevFib: Int

Sources/StructuredQueriesCore/Documentation.docc/Articles/GettingStarted.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -766,11 +766,11 @@ struct ReminderResult: QueryRepresentable {
766766
)
767767
```
768768
769-
There is also a way to streamline providing the ``QueryRepresentable`` conformance. You can use the
770-
`@Table` to describe the datatype you want to decode:
769+
There is also a way to streamline providing the ``QueryRepresentable`` conformance. You can use
770+
`@Selection` to describe the datatype you want to decode:
771771
772772
```swift
773-
@Table
773+
@Selection
774774
struct ReminderResult {
775775
let title: String
776776
let isCompleted: Bool
@@ -784,7 +784,4 @@ struct ReminderResult {
784784
)
785785
```
786786
787-
The `@Table` macro can be used to describe any table-like entity. This includes virtual tables,
788-
database views, common table expressions, and even groups of columns that you'd like to decode.
789-
790787
See <doc:SafeSQLStrings> for more information about the `#sql` macro.

Sources/StructuredQueriesCore/Documentation.docc/Articles/QueryCookbook.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -262,20 +262,16 @@ you can use the ``Table/unscoped`` property:
262262
It will often be the case that you want to select very specific data from your database and then
263263
decode that data into a custom Swift data type. For example, if you are displaying a list of
264264
reminders and only need their titles for the list, it would be wasteful to decode an array of all
265-
reminder data. The `@Table` macro allows you to define a custom data type of only the fields you
265+
reminder data. The `@Selection` macro allows you to define a custom data type of only the fields you
266266
want to decode:
267267
268268
```swift
269-
@Table
269+
@Selection
270270
struct ReminderTitle {
271271
let title: String
272272
}
273273
```
274274
275-
> Note: The `@Table` macro can be used to describe not only database tables, but a number of
276-
> table-like things, including virtual tables, database views, common table expressions, and even
277-
> simply groups of columns you'd like to decode together.
278-
279275
Then when selecting the columns for your query you can use this data type:
280276

281277
@Row {
@@ -304,7 +300,7 @@ As another example, consider the query that selects all reminders lists with the
304300
in each list. A data type can be defined like so:
305301

306302
```swift
307-
@Table
303+
@Selection
308304
struct RemindersListWithCount {
309305
let remindersCount: Int
310306
let remindersList: RemindersList

Sources/StructuredQueriesCore/Documentation.docc/Articles/SelectStatements.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,10 @@ To add (or remove) a `DISTINCT` clause from a selection, use ``Select/distinct(_
108108
}
109109

110110
To bundle selected columns up into a custom data type, you can annotate a struct of decoded results
111-
with the `@Table` macro:
111+
with the `@Selection` macro:
112112

113113
```swift
114-
@Table
114+
@Selection
115115
struct ReminderResult {
116116
let title: String
117117
let isCompleted: Bool

Sources/StructuredQueriesCore/Documentation.docc/Extensions/PrimaryKeyedTable.md

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ Typically such columns are also initialized by the database so that when inserti
66
table you do not need to specify the primary key. The library provides extra tools that make it
77
easier to insert, update, and delete records that have a primary key.
88

9-
> Note: Don't conform to this protocol directly. Instead, use the `@Table` and `@Column` macros to
10-
> generate a conformance.
9+
> Note: Don't conform to this protocol directly. Instead, use the `@Table`, `@Column`, and
10+
> `@Columns` macros to generate a conformance.
1111
1212
### Specifying a primary key
1313

@@ -39,7 +39,23 @@ struct Reminder {
3939
}
4040
```
4141

42-
> Note: At most one column can be designated as a primary key.
42+
To define a composite primary key, group them together into a `@Selection` type and annotate the
43+
field with the `@Columns` macro:
44+
45+
```swift
46+
@Table
47+
struct Enrollment {
48+
@Selection
49+
struct ID {
50+
var courseID: CourseID
51+
var studentID: StudentID
52+
}
53+
54+
@Columns // Automatically inferred as '@Columns(primaryKey: True)
55+
let id: ID
56+
// ...
57+
}
58+
```
4359

4460
### Drafts
4561

Sources/StructuredQueriesCore/PrimaryKeyed.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ extension TableDraft {
4444

4545
/// A type representing a database table's columns.
4646
///
47-
/// Don't conform to this protocol directly. Instead, use the `@Table` and `@Column` macros to
48-
/// generate a conformance.
47+
/// Don't conform to this protocol directly. Instead, use the `@Table`, `@Column`, and `@Columns`
48+
/// macros to generate a conformance.
4949
public protocol PrimaryKeyedTableDefinition<PrimaryKey>: TableDefinition
5050
where QueryValue: PrimaryKeyedTable {
5151
/// A type representing this table's primary key.

Sources/StructuredQueriesCore/TableColumn.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ extension WritableTableColumnExpression {
4646

4747
/// A type representing a table column.
4848
///
49-
/// Don't create instances of this value directly. Instead, use the `@Table` and `@Column` macros to
50-
/// generate values of this type.
49+
/// Don't create instances of this value directly. Instead, use the `@Table`, `@Column`, and
50+
/// `@Columns` macros to generate values of this type.
5151
public struct TableColumn<Root: Table, Value: QueryRepresentable & QueryBindable>:
5252
WritableTableColumnExpression
5353
{
@@ -144,8 +144,8 @@ public enum GeneratedColumnStorage {
144144

145145
/// A type representing a generated column.
146146
///
147-
/// Don't create instances of this value directly. Instead, use the `@Table` and `@Column` macros to
148-
/// generate values of this type.
147+
/// Don't create instances of this value directly. Instead, use the `@Table`, `@Column`, and
148+
/// `@Columns` macros to generate values of this type.
149149
public struct GeneratedColumn<Root: Table, Value: QueryRepresentable & QueryBindable>:
150150
TableColumnExpression
151151
{

Sources/StructuredQueriesCore/TableDefinition.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/// A type representing a database table's columns.
22
///
3-
/// Don't conform to this protocol directly. Instead, use the `@Table` and `@Column` macros to
4-
/// generate a conformance.
3+
/// Don't conform to this protocol directly. Instead, use the `@Table`, `@Column`, and `@Columns`
4+
/// macros to generate a conformance.
55
@dynamicMemberLookup
66
public protocol TableDefinition<QueryValue>: QueryExpression where QueryValue: Table {
77
/// An array of this table's columns.

Sources/StructuredQueriesCore/TableExpression.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/// An expression of table columns.
22
///
3-
/// Don't conform to this protocol directly. Instead, use the `@Table` macro to generate a
4-
/// conformance.
3+
/// Don't conform to this protocol directly. Instead, use the `@Table` and `@Selection` macros to
4+
/// generate a conformance.
55
public protocol TableExpression<QueryValue>: QueryExpression where QueryValue: Table {
66
var allColumns: [any QueryExpression] { get }
77
}

Sources/StructuredQueriesSQLiteCore/Documentation.docc/Articles/QueryCookbook.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ this is doing work that SQL actually excels at. In fact, the condition inside th
5858
suspiciously like a join constraint, which should give us a hint that what we are doing is not
5959
quite right.
6060

61-
Another way to do this is to use the `@Table` and `@Column` macros along with a
61+
Another way to do this is to use the `@Selection` and `@Column` macros along with a
6262
`JSONRepresentation`` of the collection of reminders you want to load for each list:
6363

6464
```swift
65-
@Table
65+
@Selection
6666
struct Row {
6767
let remindersList: RemindersList
6868
@Column(as: [Reminder].JSONRepresentation.self)
@@ -117,9 +117,8 @@ And suppose you would like to fetch all `RemindersList`s along with the collecti
117117
and reminders associated with the list:
118118

119119
```struct
120-
@Table
120+
@Selection
121121
struct Row {
122-
@Columns
123122
let remindersList: RemindersList
124123
@Column(as: [Milestone].JSONRepresentation.self)
125124
let milestones: [Milestone]

0 commit comments

Comments
 (0)