@@ -17,6 +17,11 @@ open ProviderImplementation.ProvidedTypes
1717
1818open FSharp.Data .SqlClient
1919
20+ type SqlEnumKind =
21+ | Default = 0
22+ | CLI = 1
23+ | UnitsOfMeasure = 2
24+
2025[<TypeProvider>]
2126type public SqlEnumProvider ( config : TypeProviderConfig ) as this =
2227 inherit TypeProviderForNamespaces()
@@ -49,7 +54,7 @@ type public SqlEnumProvider(config : TypeProviderConfig) as this =
4954 ProvidedStaticParameter( " ConnectionStringOrName" , typeof< string>)
5055 ProvidedStaticParameter( " Provider" , typeof< string>, " System.Data.SqlClient" )
5156 ProvidedStaticParameter( " ConfigFile" , typeof< string>, " " )
52- ProvidedStaticParameter( " CLIEnum " , typeof< bool >, false )
57+ ProvidedStaticParameter( " Kind " , typeof< SqlEnumKind >, SqlEnumKind.Default )
5358 ],
5459 instantiationFunction = ( fun typeName args ->
5560 cache.GetOrAdd( typeName, lazy this.CreateRootType( typeName, unbox args.[ 0 ], unbox args.[ 1 ], unbox args.[ 2 ], unbox args.[ 3 ], unbox args.[ 4 ]))
@@ -62,12 +67,12 @@ type public SqlEnumProvider(config : TypeProviderConfig) as this =
6267<param name='ConnectionString'>String used to open a data connection.</param>
6368<param name='Provider'>Invariant name of a ADO.NET provider. Default is "System.Data.SqlClient".</param>
6469<param name='ConfigFile'>The name of the configuration file that’s used for connection strings at DESIGN-TIME. The default value is app.config or web.config.</param>
65- <param name='CLIEnum'>Generate standard CLI Enum. Default is false. </param>
70+ <param name='Kind'> </param>
6671"""
6772
6873 this.AddNamespace( nameSpace, [ providerType ])
6974
70- member internal this.CreateRootType ( typeName , query , connectionStringOrName , provider , configFile , cliEnum ) =
75+ member internal this.CreateRootType ( typeName , query , connectionStringOrName , provider , configFile , kind : SqlEnumKind ) =
7176 let tempAssembly = ProvidedAssembly( Path.ChangeExtension( Path.GetTempFileName(), " .dll" ))
7277
7378 let providedEnumType = ProvidedTypeDefinition( assembly, nameSpace, typeName, baseType = Some typeof< obj>, HideObjectMethods = true , IsErased = false )
@@ -147,8 +152,8 @@ type public SqlEnumProvider(config : TypeProviderConfig) as this =
147152 |> Seq.groupBy id
148153 |> Seq.iter ( fun ( key , xs ) -> if Seq.length xs > 1 then failwithf " Non-unique label %s ." key)
149154
150- if cliEnum
151- then
155+ match kind with
156+ | SqlEnumKind.CLI ->
152157
153158 if not ( allowedTypesForEnum.Contains( valueType))
154159 then failwithf " Enumerated types can only have one of the following underlying types: %A ." [| for t in allowedTypesForEnum -> t.Name |]
@@ -160,7 +165,19 @@ type public SqlEnumProvider(config : TypeProviderConfig) as this =
160165 ||> List.map2 ( fun name value -> ProvidedLiteralField( name, providedEnumType, fst value))
161166 |> providedEnumType.AddMembers
162167
163- else
168+ | SqlEnumKind.UnitsOfMeasure ->
169+
170+ for name in names do
171+ let units = ProvidedTypeDefinition( name, None, IsErased = false )
172+ units.AddCustomAttribute {
173+ new CustomAttributeData() with
174+ member __.Constructor = typeof< MeasureAttribute>. GetConstructor [||]
175+ member __.ConstructorArguments = upcast Array.empty
176+ member __.NamedArguments = upcast Array.empty
177+ }
178+ providedEnumType.AddMember units
179+
180+ | _ ->
164181 let valueFields , setFieldValues =
165182 ( names, values) ||> List.map2 ( fun name value ->
166183 if allowedTypesForLiteral.Contains valueType
0 commit comments