@@ -109,14 +109,25 @@ extension Language {
109109 let type = column. value
110110 fields [ name] = GeneratedField (
111111 name: name,
112- type: . builtin( builtinType ( for: type) , isArray: false ) ,
112+ type: . builtin(
113+ builtinType ( for: type) ,
114+ isArray: false ,
115+ encodedAs: builtinForAliasedType ( for: type)
116+ ) ,
113117 isArray: type. isRow
114118 )
115119 } ,
116120 isTable: true
117121 )
118122 }
119123
124+ /// If the column type was aliased then this will return the `builtin`
125+ /// type for the root type of the alias.
126+ private static func builtinForAliasedType( for type: Type ) -> String ? {
127+ guard case let . alias( root, _) = type else { return nil }
128+ return builtinType ( for: root)
129+ }
130+
120131 private static func inputTypeIfNeeded(
121132 statement: Statement ,
122133 definition: Definition
@@ -126,7 +137,8 @@ extension Language {
126137 guard statement. parameters. count > 1 else {
127138 return . builtin(
128139 builtinType ( for: firstParameter. type) ,
129- isArray: firstParameter. type. isRow
140+ isArray: firstParameter. type. isRow,
141+ encodedAs: builtinForAliasedType ( for: firstParameter. type)
130142 )
131143 }
132144
@@ -137,7 +149,11 @@ extension Language {
137149 fields: statement. parameters. reduce ( into: [ : ] ) { fields, parameter in
138150 fields [ parameter. name] = GeneratedField (
139151 name: parameter. name,
140- type: . builtin( builtinType ( for: parameter. type) , isArray: false ) ,
152+ type: . builtin(
153+ builtinType ( for: parameter. type) ,
154+ isArray: false ,
155+ encodedAs: builtinForAliasedType ( for: parameter. type)
156+ ) ,
141157 isArray: parameter. type. isRow
142158 )
143159 } ,
@@ -168,7 +184,11 @@ extension Language {
168184
169185 // Only one column returned, just use it's type
170186 guard statement. resultColumns. count > 1 else {
171- return . builtin( builtinType ( for: firstColumn) , isArray: firstColumn. isRow)
187+ return . builtin(
188+ builtinType ( for: firstColumn) ,
189+ isArray: firstColumn. isRow,
190+ encodedAs: builtinForAliasedType ( for: firstColumn)
191+ )
172192 }
173193
174194 let outputTypeName = definition. output? . description ?? " \( definition. name. capitalizedFirst) Output "
@@ -189,7 +209,11 @@ extension Language {
189209 let type = column. value
190210 fields [ name] = GeneratedField (
191211 name: name,
192- type: . builtin( builtinType ( for: type) , isArray: false ) ,
212+ type: . builtin(
213+ builtinType ( for: type) ,
214+ isArray: false ,
215+ encodedAs: builtinForAliasedType ( for: type)
216+ ) ,
193217 isArray: type. isRow
194218 )
195219 }
@@ -217,9 +241,21 @@ public struct GeneratedModel {
217241}
218242
219243public struct GeneratedField {
244+ /// The column name
220245 let name : String
246+ /// The type of the field.
247+ /// If it is a `model` that means the user selected
248+ /// all columns from a table `foo.*`
221249 let type : BuiltinOrGenerated
250+ /// Whether or not it is an array. Some fields can take a list
251+ /// as an input for a query like `foo IN :bar`
222252 let isArray : Bool
253+
254+ /// The underlying storage type if it is aliased
255+ var encodedAsType : String ? {
256+ guard case let . builtin( _, _, encodedAs) = type else { return nil }
257+ return encodedAs
258+ }
223259}
224260
225261public struct GeneratedQuery {
@@ -239,12 +275,17 @@ public struct GeneratedResult<Decl> {
239275}
240276
241277public enum BuiltinOrGenerated : CustomStringConvertible {
242- case builtin( String , isArray: Bool )
278+ /// Types can be aliased. So `TEXT AS UUID`. `encodedAs`
279+ /// would be the `TEXT`. It will allow us to tell the
280+ /// `bind` functions to actually encode to the underlying
281+ /// type rather than just having `UUID` always go to `TEXT`
282+ /// when some users may want a `BLOB`.
283+ case builtin( String , isArray: Bool , encodedAs: String ? )
243284 case model( GeneratedModel )
244285
245286 public var description : String {
246287 switch self {
247- case . builtin( let builtin, let isArray) :
288+ case . builtin( let builtin, let isArray, _ ) :
248289 isArray ? " [ \( builtin) ] " : builtin
249290 case . model( let model) :
250291 model. name
0 commit comments