@@ -82,6 +82,37 @@ public struct SwiftLanguage: Language {
8282 return file. formatted ( ) . description
8383 }
8484
85+ public static func macro(
86+ databaseName: String ,
87+ tables: [ GeneratedModel ] ,
88+ queries: [ GeneratedQuery ] ,
89+ options: GenerationOptions ,
90+ addConnection: Bool
91+ ) throws -> [ DeclSyntax ] {
92+ var decls : [ DeclSyntax ] = [ ]
93+
94+ if addConnection {
95+ decls. append ( " let connection: any Feather.Connection " )
96+ }
97+
98+ for table in tables {
99+ try decls. append ( declaration ( for: table, isOutput: true , options: options) )
100+ }
101+
102+ // Always do this at the top level since it will automatically namespaced under the
103+ // struct that the macro is attached too.
104+ for query in queries {
105+ try decls. append ( contentsOf: modelsFor ( query: query, options: options) )
106+ try decls. append ( declaration ( for: query, underscoreName: true , databaseName: databaseName, options: options) )
107+ try decls. append ( DeclSyntax ( dbTypealiasFor ( query: query, databaseName: databaseName, options: options) ) )
108+ try decls. append ( DeclSyntax ( typealiasFor ( query: query, databaseName: databaseName, options: options) ) )
109+ }
110+
111+ // TODO: Generate extensions if this can be done.
112+
113+ return decls
114+ }
115+
85116 private static func modelsFor(
86117 query: GeneratedQuery ,
87118 options: GenerationOptions
@@ -98,22 +129,7 @@ public struct SwiftLanguage: Language {
98129
99130 return decls
100131 }
101-
102- public static func queryType(
103- for cardinality: Cardinality ? ,
104- input: BuiltinOrGenerated ? ,
105- output: BuiltinOrGenerated ?
106- ) -> String {
107- let input = input? . description ?? " () "
108- let output = output? . description ?? " () "
109-
110- return switch cardinality {
111- case . single: " FetchSingleQuery< \( input) , \( output) > "
112- case . many: " FetchManyQuery< \( input) , \( output) > "
113- default : " VoidQuery< \( input) > "
114- }
115- }
116-
132+
117133 private static func declaration(
118134 for migrations: [ String ] ,
119135 options: GenerationOptions
@@ -132,14 +148,16 @@ public struct SwiftLanguage: Language {
132148
133149 private static func declaration(
134150 for query: GeneratedQuery ,
151+ underscoreName: Bool = false ,
135152 databaseName: String ,
136153 options: GenerationOptions
137154 ) throws -> DeclSyntax {
138155 let inputTypeName = inputTypeName ( for: query, databaseName: databaseName)
139156 let outputTypeName = outputTypeName ( for: query, databaseName: databaseName)
140157 let queryTypeName = " AnyDatabaseQuery< \( inputTypeName) , \( outputTypeName) > "
158+ let variableName = underscoreName ? " _ \( query. name) " : query. name
141159
142- let query = try VariableDeclSyntax ( " var \( raw: query . name ) : \( raw: queryTypeName) " ) {
160+ let query = try VariableDeclSyntax ( " var \( raw: variableName ) : \( raw: queryTypeName) " ) {
143161 FunctionCallExprSyntax (
144162 calledExpression: DeclReferenceExprSyntax (
145163 baseName: . identifier( " AnyDatabaseQuery< \( inputTypeName) , \( outputTypeName) > " )
@@ -215,6 +233,20 @@ public struct SwiftLanguage: Language {
215233 )
216234 }
217235
236+ private static func dbTypealiasFor(
237+ query: GeneratedQuery ,
238+ databaseName: String ,
239+ options: GenerationOptions
240+ ) throws -> TypeAliasDeclSyntax {
241+ let namespace = options. contains ( . namespaceGeneratedModels)
242+ let inputTypeName = inputTypeName ( for: query, namespaced: namespace, databaseName: databaseName)
243+ let outputTypeName = outputTypeName ( for: query, namespaced: namespace, databaseName: databaseName)
244+ let name = query. name. capitalizedFirst. replacingOccurrences ( of: " Query " , with: " DatabaseQuery " )
245+ return try TypeAliasDeclSyntax (
246+ " typealias \( raw: name) = AnyDatabaseQuery< \( raw: inputTypeName) , \( raw: outputTypeName) > "
247+ )
248+ }
249+
218250 private static func inputExtension(
219251 for query: GeneratedQuery ,
220252 input: GeneratedModel ,
0 commit comments