Skip to content

Commit 6229fb7

Browse files
authored
Add more docs for #bind (#46)
* Add more docs for `#bind` * fix
1 parent 0b77ca6 commit 6229fb7

File tree

4 files changed

+37
-16
lines changed

4 files changed

+37
-16
lines changed

Package.resolved

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@
7878
"kind" : "remoteSourceControl",
7979
"location" : "https://github.com/pointfreeco/swift-snapshot-testing",
8080
"state" : {
81-
"revision" : "1be8144023c367c5de701a6313ed29a3a10bf59b",
82-
"version" : "1.18.3"
81+
"revision" : "37230a37e83f1b7023be08e1b1a2603fcb1567fb",
82+
"version" : "1.18.4"
8383
}
8484
},
8585
{

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

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,19 @@ generated when writing queries will correctly use `"is_completed"`.
143143
144144
### Custom data types
145145
146-
There are many data types that do not have native support in SQLite. For these data types you must
147-
define a conformance to ``QueryBindable`` in order to translate values to a format that SQLite does
148-
understand. The library comes with conformances to aid in representing dates, UUIDs, and JSON, and
149-
you can define your own conformances for your own custom data types.
146+
StructuredQueries provides support for many basic Swift data types out of the box, like strings,
147+
integers, doubles, bytes, and booleans, but you may want to represent custom, domain specific types
148+
with your table's columns, instead. For these data types you must either define a conformance to
149+
``QueryBindable`` to translate values to a format that the library does understand, or provide a
150+
``QueryRepresentable`` type that wraps your domain type.
151+
152+
The library comes with several `QueryRepresentable` conformances to aid in representing dates,
153+
UUIDs, and JSON, and you can define your own conformances for your own custom data types.
150154
151155
#### Dates
152156
153-
SQLite does not have a native date type, and instead has 3 different ways to represent dates:
157+
While some relational databases, like MySQL and Postgres, have native support for dates, SQLite
158+
does _not_. Instead, it has 3 different ways to represent dates:
154159
155160
* Text column interpreted as ISO-8601-formatted string.
156161
* Int column interpreted as number of seconds since Unix epoch.
@@ -203,6 +208,14 @@ And StructuredQueries will take care of formatting the value for the database:
203208
}
204209
}
205210
211+
When querying against a date column with a Swift date, you will need to explicitly bundle up the
212+
Swift date into the appropriate representation to use various query helpers. This can be done using
213+
the `#bind` macro:
214+
215+
```swift
216+
Reminder.where { $0.created > #bind(startDate) }
217+
```
218+
206219
#### UUID
207220
208221
SQLite also does not have native support for UUIDs. If you try to use a UUID in your tables you
@@ -238,6 +251,14 @@ translate the UUID to text:
238251
}
239252
```
240253
254+
When querying against a UUID column with a Swift UUID, you will need to explicitly bundle up the
255+
Swift UUID into the appropriate representation to use various query helpers. This can be done using
256+
the `#bind` macro:
257+
258+
```swift
259+
Reminder.where { $0.id != #bind(reminder.id) }
260+
```
261+
241262
#### RawRepresentable
242263
243264
Simple data types, in particular ones conforming to `RawRepresentable` whose `RawValue` is a string

Tests/StructuredQueriesMacrosTests/Support/SnapshotTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ import Testing
88
@Suite(
99
.serialized,
1010
.macros(
11-
record: .failed,
12-
macros: [
11+
[
1312
"_Draft": TableMacro.self,
1413
"bind": BindMacro.self,
1514
"Column": ColumnMacro.self,
1615
"Ephemeral": EphemeralMacro.self,
17-
"Table": TableMacro.self,
1816
"Selection": SelectionMacro.self,
1917
"sql": SQLMacro.self,
20-
]
18+
"Table": TableMacro.self,
19+
],
20+
record: .failed
2121
)
2222
) struct SnapshotTests {}
2323

Tests/StructuredQueriesMacrosTests/TableMacroTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -229,18 +229,18 @@ extension SnapshotTests {
229229
var baz: Int
230230
}
231231
232-
extension Bar: StructuredQueries.Table {
233-
public struct TableColumns: StructuredQueries.TableDefinition {
232+
extension Bar: StructuredQueriesCore.Table {
233+
public struct TableColumns: StructuredQueriesCore.TableDefinition {
234234
public typealias QueryValue = Bar
235-
public let baz = StructuredQueries.TableColumn<QueryValue, Int>("baz", keyPath: \QueryValue.baz)
236-
public static var allColumns: [any StructuredQueries.TableColumnExpression] {
235+
public let baz = StructuredQueriesCore.TableColumn<QueryValue, Int>("baz", keyPath: \QueryValue.baz)
236+
public static var allColumns: [any StructuredQueriesCore.TableColumnExpression] {
237237
[QueryValue.columns.baz]
238238
}
239239
}
240240
public static let columns = TableColumns()
241241
public static let tableName = "bar"
242242
public static let schemaName: Swift.String? = "foo"
243-
public init(decoder: inout some StructuredQueries.QueryDecoder) throws {
243+
public init(decoder: inout some StructuredQueriesCore.QueryDecoder) throws {
244244
let baz = try decoder.decode(Int.self)
245245
guard let baz else {
246246
throw QueryDecodingError.missingRequiredColumn

0 commit comments

Comments
 (0)