Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### 1.5.33 - 19.11.2026
- Fix: a saved schema (ContextSchemaPath) loads on both .NET and .NET Framework, whichever saved it. Visual Studio, whose editor and compiler run on .NET Framework, failed on a schema saved by dotnet with "Value cannot be null. Parameter name: key"

### 1.5.32 - 14.11.2026
- Fix: PostgreSQL functions are called inside a SELECT. With Npgsql 7 and newer they failed with "procedure ... does not exist", as a stored-procedure command is a CALL there
- PostgreSQL procedures (11 and newer) are under ctx.Procedures and called with CALL, with any Npgsql version; they weren't listed before. INOUT and OUT parameters are the result's properties
Expand Down
39 changes: 36 additions & 3 deletions src/SQLProvider.Common/Utils.fs
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,42 @@ module Utilities =
parseAggregates' fieldNotation fieldNotationAlias tail parsed
parseAggregates' fieldNotat fieldNotationAlias query []

// https://stackoverflow.com/questions/1825147/type-gettypenamespace-a-b-classname-returns-null
let getType typename =
Type.GetType typename
/// The type of an assembly-qualified or plain type name, or null.
/// A saved schema names types as the runtime that saved it had them: "System.String, System.Private.CoreLib,
/// Version=10.0.0.0, ..." from .NET, "System.String, mscorlib, Version=4.0.0.0, ..." from .NET Framework, and a type
/// such as System.Net.IPAddress lives in System.Net.Primitives on one and System on the other. So a name that doesn't
/// load as written is read again part by part, generic arguments included: an assembly is loaded as named, then by
/// its simple name (any version), and a type that isn't in its named assembly is looked for in the core library and
/// then in the assemblies already loaded, public types only. A part that names no assembly is not looked for there.
let getType (typename:string) =
match Type.GetType typename with
| null when not (String.IsNullOrEmpty typename) ->
let coreLibrary = typeof<obj>.Assembly
let load (name:Reflection.AssemblyName) = try Reflection.Assembly.Load name with _ -> null
// Never null: Type.GetType gives up on a null assembly without asking for the type.
let assemblyOf (name:Reflection.AssemblyName) =
match load name with
| null ->
match load (Reflection.AssemblyName name.Name) with
| null -> coreLibrary
| a -> a
| a -> a
let typeOf (assembly:Reflection.Assembly) (name:string) (ignoreCase:bool) =
let publicTypeIn (a:Reflection.Assembly) =
match (try a.GetType(name, false, ignoreCase) with _ -> null) with
| null -> None
| t -> if t.IsVisible then Some t else None
match assembly with
| null -> Type.GetType(name, false, ignoreCase)
| named ->
seq { yield named
yield coreLibrary
for a in AppDomain.CurrentDomain.GetAssemblies() do
if not a.IsDynamic then yield a }
|> Seq.tryPick publicTypeIn
|> Option.toObj
try Type.GetType(typename, Func<_,_> assemblyOf, Func<_,_,_,_> typeOf, false) with _ -> null
| t -> t

/// Some/ValueSome case of the given option or voption type holding the value.
let private makeSomeOf (optionType:Type) (value:obj) =
Expand Down
64 changes: 64 additions & 0 deletions tests/SqlProvider.Tests/SchemaTypeNameTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
module SchemaTypeNameTests

// A saved schema's type names come from the runtime that saved it; both runtimes must read both kinds.

open System
open System.Net
open FSharp.Data.Sql.Common
open NUnit.Framework

let private savedOnNet10 name assembly token = sprintf "%s, %s, Version=10.0.0.0, Culture=neutral, PublicKeyToken=%s" name assembly token
let private savedOnNetFramework name assembly token = sprintf "%s, %s, Version=4.0.0.0, Culture=neutral, PublicKeyToken=%s" name assembly token
let private coreLib = "7cec85d7bea7798e"
let private mscorlib = "b77a5c561934e089"

[<Test; SqlCheck.NoSql>]
let ``a name saved on this runtime resolves`` () =
Assert.AreEqual(typeof<Guid>, Utilities.getType typeof<Guid>.AssemblyQualifiedName)
Assert.AreEqual(typeof<string>, Utilities.getType "System.String")

[<Test; SqlCheck.NoSql>]
let ``core library types saved on .NET resolve`` () =
Assert.AreEqual(typeof<string>, Utilities.getType (savedOnNet10 "System.String" "System.Private.CoreLib" coreLib))
Assert.AreEqual(typeof<DateTime>, Utilities.getType (savedOnNet10 "System.DateTime" "System.Private.CoreLib" coreLib))
Assert.AreEqual(typeof<byte[]>, Utilities.getType (savedOnNet10 "System.Byte[]" "System.Private.CoreLib" coreLib))

[<Test; SqlCheck.NoSql>]
let ``core library types saved on .NET Framework resolve`` () =
Assert.AreEqual(typeof<decimal>, Utilities.getType (savedOnNetFramework "System.Decimal" "mscorlib" mscorlib))
Assert.AreEqual(typeof<int[]>, Utilities.getType (savedOnNetFramework "System.Int32[]" "mscorlib" mscorlib))

[<Test; SqlCheck.NoSql>]
let ``a later version of the saving runtime still resolves`` () =
Assert.AreEqual(typeof<string>, Utilities.getType "System.String, System.Private.CoreLib, Version=99.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e")

[<Test; SqlCheck.NoSql>]
let ``a type in another assembly on the other runtime resolves`` () =
let ipAddress = typeof<IPAddress>
Assert.AreEqual(ipAddress, Utilities.getType (savedOnNet10 "System.Net.IPAddress" "System.Net.Primitives" "b03f5f7f11d50a3a"))
Assert.AreEqual(ipAddress, Utilities.getType (savedOnNetFramework "System.Net.IPAddress" "System" mscorlib))

[<Test; SqlCheck.NoSql>]
let ``the arguments of a generic type resolve as well`` () =
let stringOnNet10 = savedOnNet10 "System.String" "System.Private.CoreLib" coreLib
let hstore = sprintf "System.Collections.Generic.IDictionary`2[[%s],[%s]]" stringOnNet10 stringOnNet10
Assert.AreEqual(typeof<Collections.Generic.IDictionary<string,string>>, Utilities.getType (savedOnNet10 hstore "System.Private.CoreLib" coreLib))
let stringOnNetFramework = savedOnNetFramework "System.String" "mscorlib" mscorlib
let hstore = sprintf "System.Collections.Generic.IDictionary`2[[%s],[%s]]" stringOnNetFramework stringOnNetFramework
Assert.AreEqual(typeof<Collections.Generic.IDictionary<string,string>>, Utilities.getType (savedOnNetFramework hstore "mscorlib" mscorlib))
let nullableInts = sprintf "System.Nullable`1[[%s]][]" (savedOnNet10 "System.Int32" "System.Private.CoreLib" coreLib)
Assert.AreEqual(typeof<Nullable<int>[]>, Utilities.getType (savedOnNet10 nullableInts "System.Private.CoreLib" coreLib))
let ipAddress = savedOnNet10 "System.Net.IPAddress" "System.Net.Primitives" "b03f5f7f11d50a3a"
Assert.AreEqual(typeof<Lazy<IPAddress>>, Utilities.getType (savedOnNet10 (sprintf "System.Lazy`1[[%s]]" ipAddress) "System.Private.CoreLib" coreLib))

[<Test; SqlCheck.NoSql>]
let ``a name without an assembly is not searched in the loaded assemblies`` () =
Assert.AreEqual(typeof<Assert>, Utilities.getType "NUnit.Framework.Assert, No.Such.Assembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")
Assert.IsNull(Utilities.getType "NUnit.Framework.Assert")

[<Test; SqlCheck.NoSql>]
let ``an unknown type is null`` () =
Assert.IsNull(Utilities.getType "No.Such.Type")
Assert.IsNull(Utilities.getType "No.Such.Type, NoSuchAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")
Assert.IsNull(Utilities.getType (savedOnNet10 "System.NoSuchType" "System.Private.CoreLib" coreLib))
Assert.IsNull(Utilities.getType "")
1 change: 1 addition & 0 deletions tests/SqlProvider.Tests/SqlProvider.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
<Compile Include="SqlServerTests.fs" Condition="'$(TargetFramework)' == 'net48'" />
<Compile Include="MsDataSqliteTransactions.fs" />
<Compile Include="PostgreSqlSchemaTests.fs" />
<Compile Include="SchemaTypeNameTests.fs" />
<Content Include="app.config" />
<None Include="x86\SQLite.Interop.dll" Condition="'$(TargetFramework)' == 'net48'">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
Expand Down
Loading