Skip to content

Commit 23cc67c

Browse files
authored
Parse #sql macro comments (#187)
* Suppress `#sql` diagnostics in SQL comments * another test * unrelated docs fix
1 parent 278b5e2 commit 23cc67c

File tree

3 files changed

+75
-3
lines changed

3 files changed

+75
-3
lines changed

Sources/StructuredQueriesMacros/SQLMacro.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public enum SQLMacro: ExpressionMacro {
2424
var unexpectedBind: (segment: StringSegmentSyntax, offset: Int)?
2525
var unexpectedClose: (delimiter: UInt8, segment: StringSegmentSyntax, offset: Int)?
2626
var invalidBind = false
27+
var isInComment = false
2728
if let string = argument.as(StringLiteralExprSyntax.self) {
2829
for segment in string.segments {
2930
guard let segment = segment.as(StringSegmentSyntax.self)
@@ -75,6 +76,12 @@ public enum SQLMacro: ExpressionMacro {
7576
while offset < segment.content.syntaxTextBytes.endIndex {
7677
defer { offset += 1 }
7778
let byte = segment.content.syntaxTextBytes[offset]
79+
if isInComment {
80+
if byte == UInt8(ascii: "\n") {
81+
isInComment = false
82+
}
83+
continue
84+
}
7885
if let delimiter = currentDelimiter ?? parenStack.last {
7986
if byte == delimiters[delimiter.delimiter] {
8087
if currentDelimiter == nil {
@@ -104,6 +111,12 @@ public enum SQLMacro: ExpressionMacro {
104111
unexpectedClose = (byte, segment, offset)
105112
} else if binds.contains(byte) {
106113
unexpectedBind = (segment, offset)
114+
} else if byte == UInt8(ascii: "-"),
115+
segment.content.syntaxTextBytes.indices.contains(offset + 1),
116+
segment.content.syntaxTextBytes[offset + 1] == byte
117+
{
118+
offset += 1
119+
isInComment = true
107120
}
108121
}
109122
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ one does with common table expressions, and it allows one to represent a type th
2828
purposes seems like a regular SQLite table, but it's not actually persisted in the database.
2929

3030
With that type defined we can use the
31-
``StructuredQueriesCore/Table/createTemporaryView(ifNotExists:as:)`` to create a SQL query that
32-
creates a temporary view. You provide a select statement that selects all the data needed for the
33-
view:
31+
``StructuredQueriesCore/Table/createTemporaryView(ifNotExists:as:)`` function to create a SQL query
32+
that creates a temporary view. You provide a select statement that selects all the data needed for
33+
the view:
3434

3535
```swift
3636
ReminderWithList.createTemporaryView(

Tests/StructuredQueriesMacrosTests/SQLMacroTests.swift

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,5 +374,64 @@ extension SnapshotTests {
374374
"""
375375
}
376376
}
377+
378+
@Test func sqlComments() {
379+
assertMacro {
380+
"""
381+
#sql("SELECT 1 -- TODO: Implement logic")
382+
"""
383+
} expansion: {
384+
"""
385+
StructuredQueriesCore.SQLQueryExpression("SELECT 1 -- TODO: Implement logic")
386+
"""
387+
}
388+
assertMacro {
389+
"""
390+
#sql("SELECT '1 -- TODO: Implement logic'")
391+
"""
392+
} expansion: {
393+
"""
394+
StructuredQueriesCore.SQLQueryExpression("SELECT '1 -- TODO: Implement logic'")
395+
"""
396+
}
397+
assertMacro {
398+
#"""
399+
#sql(
400+
"""
401+
SELECT * FROM reminders -- TODO: We should write columns out by hand
402+
WHERE isCompleted -- TODO: Double-check this logic
403+
"""
404+
)
405+
"""#
406+
} expansion: {
407+
#"""
408+
StructuredQueriesCore.SQLQueryExpression(
409+
"""
410+
SELECT * FROM reminders -- TODO: We should write columns out by hand
411+
WHERE isCompleted -- TODO: Double-check this logic
412+
""")
413+
"""#
414+
}
415+
assertMacro {
416+
#"""
417+
#sql(
418+
"""
419+
SELECT ( -- TODO: ;-)
420+
1 = 1
421+
)
422+
"""
423+
)
424+
"""#
425+
} expansion: {
426+
#"""
427+
StructuredQueriesCore.SQLQueryExpression(
428+
"""
429+
SELECT ( -- TODO: ;-)
430+
1 = 1
431+
)
432+
""")
433+
"""#
434+
}
435+
}
377436
}
378437
}

0 commit comments

Comments
 (0)