Skip to content

Commit 9867e41

Browse files
committed
Readme
1 parent f74854c commit 9867e41

4 files changed

Lines changed: 19 additions & 7 deletions

File tree

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,20 @@ func main() async throws {
108108
> As of now it is not recommended for larger projects. There are quite a few limitations
109109
that won't scale well beyond a fairly simple schema and a handfull of queries.
110110

111+
#### Anatomy of a @Query
112+
```swift
113+
@Query(
114+
"SELECT * FROM foo WHERE id IN ?", // 1.
115+
inputName: "CustomInputName", // 2.
116+
outputName: "CustomOutputName" // 3.
117+
)
118+
var variableName: any MyQuery // 4.
119+
```
120+
1. The raw SQL to execute
121+
2. Optionally supply a custom type name for the generated input type.
122+
3. Optionally supply a custom type name for the generated output type.
123+
4. The `variableName` can be anything and does not affect any of the generated code.
124+
111125
#### Current Limitations
112126
* Since macros operate purely on the syntax, all queries must be within the `@Database` itself so it has access to the schema.
113127
* All generated types will be nested under the `@Database` struct.

Sources/Compiler/Utils/String+Extensions.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
//
77

88
extension StringProtocol {
9-
var capitalizedFirst: String {
9+
public var capitalizedFirst: String {
1010
guard !isEmpty else { return self.description }
1111
let first = self[startIndex]
1212
let rest = self[index(after: startIndex)..<endIndex]
1313
return "\(first.uppercased())\(rest)"
1414
}
1515

16-
var lowercaseFirst: String {
16+
public var lowercaseFirst: String {
1717
guard !isEmpty else { return self.description }
1818
let first = self[startIndex]
1919
let rest = self[index(after: startIndex)..<endIndex]

Sources/OtterMacros/DatabaseMacro.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ extension DatabaseMacro: MemberMacro {
4646

4747
for variable in variables.values {
4848
guard let queryMacro = variable.queryMacroInputsIfIsQuery(in: context),
49-
let typeName = variable.typeName?.removingQuerySuffix() else { continue }
49+
let typeName = variable.typeName?.removingQuerySuffix().lowercaseFirst else { continue }
5050

5151
let (statement, diagnostics) = compiler.compile(
5252
query: queryMacro.source,

Sources/OtterMacros/QueryMacro.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,13 @@ public struct QueryMacro: AccessorMacro {
1919
in context: some MacroExpansionContext
2020
) throws -> [AccessorDeclSyntax] {
2121
guard let property = declaration.as(VariableDeclSyntax.self),
22-
let binding = property.bindings.first,
23-
let identifier = binding.pattern.as(IdentifierPatternSyntax.self)?.identifier,
24-
binding.accessorBlock == nil
22+
let typeName = property.typeName?.removingQuerySuffix().lowercaseFirst
2523
else {
2624
return []
2725
}
2826

2927
return [
30-
"get { return _\(raw: identifier.text.removingQuerySuffix()) }",
28+
"get { return _\(raw: typeName) }",
3129
]
3230
}
3331
}

0 commit comments

Comments
 (0)