SQLProvider

Version control instructions

Git information is in a separate document.

The environment

Databases that you should need for development:

Even though our test run will run modifications to the test databases, don't check in these *.mdb and *.db files with your commit, to avoid bad merge-cases.

Solution structure

We use Fake and Paket. Before opening the solutions, you have to run build.cmd on Windows (or sh ./build.sh on Mac/Linux).

The main source solution is SQLProvider.sln. The unit tests are located in another one, SQLProvider.Tests.sln, and when you open the solution, it will lock the bin\net48\FSharp.Data.SqlProvider.dll, and after that you can't build the main solution.

Workarounds for "file in use" (issue #172)

Referenced Files

Test cases

There are database-specific test files as scripts in the test solution, /tests/SqlProvider.Tests/scripts/, but also one generic /tests/SqlProvider.Tests/QueryTests.fs which is running all the SQLite tests in the build script.

High-level description of the provider

Context and design time

You have a source code like:

// type sql = SqlDataProvider<...params...>

let dc = sql.GetDataContext()

What will first happen in the design-time, is that this will call createTypes of SqlDesignTime.fs and create (as lazily as possible) the database schema types (the shape of the database). These methods are added to the sql.datacontext and are stored to concurrent dictionaries. Visual Studio will do a lot of background processing so thread-safety is important here.

GetDataContext() will return a dynamic class called dataContext which will on design-time call class SqlDataContext in file SqlRuntime.DataContext.fs through interface ISqlDataContext. SqlDataContext uses ProviderBuilder to create database specific providers, fairly well documented ISqlProvider in file SqlRuntime.Common.fs.

Querying

The entity-items themselves are rows in the database data, and they are modelled as dynamic sub-classes of SqlEntity, base-class in file SqlRuntime.Common.fs which can be thought of as a wrapper for Dictionary<string,obj> (a column name, and the value). SqlEntity is actually used for all kinds of result data, so the data columns may not correspond to the actual data values. Mostly the results of the data are shaped as SqlQueryable<SqlEntity> or SqlQueryable<'T>, which is SQLProvider's class for IQueryable<'T> items.

let qry = 
 query {
    for cust in dc.Main.Customers do
    where ("ALFKI" = cust.CustomerId)
    select cust
 } |> Seq.toArray

This query is translated to a LINQ-expression-tree through Microsoft.FSharp.Linq.QueryFSharpBuilder. That will call IQueryable<'T>'s member Provider to execute two things for the LINQ-expression-tree: first CreateQuery and later Execute.

Parsing the LINQ-expression-tree

CreateQuery will hit our SqlQueryable<...>'s Provider (IQueryProvider) property. LINQ-expression-trees can be recursive type structures, so we will call CreateQuery for each linq-method. We get the expression-tree as a parameter and parse that with (multi-layer-) active patterns.

Our example of the LINQ-expression tree is:

.Call System.Linq.Queryable.Where(
    .Constant<System.Linq.IQueryable`1[SqlEntity]>(SqlQueryable`1[SqlEntity]),
    '(.Lambda #Lambda1<System.Func`2[SqlEntity,Boolean]>))
.Lambda #Lambda1<System.Func`2[SqlEntity,Boolean]>(SqlEntity $cust) {
    .Call $cust.GetColumn("CustomerID") == "ALFKI"
}

so it would hit this in SqlRuntime.Linq.fs:

| MethodCall(None, (MethodWithName "Where" as meth), [ SourceWithQueryData source; OptionalQuote qual ]) ->

because the LINQ-expression-tree has ExpressionType.Call named "Where" with source of IWithSqlService (which is the SqlQueryable). What happens then is the parsing of the Where-query. Where-queries are nested structures having known conditions (modelled with pattern Condition). If the conditions have SqlColumnGets, a pattern that says that it's SqlEntity with method GetColumn, we know that it has to be part of SQL-clause.

We collect all the known patterns in IWithSqlServices field SqlExpression, being a type SqlExp, our non-complete known recursive model-tree of SQL clauses.

Operations that can be done on .NET side vs. Operations translated to SQL

Execution of the query

Eventually, there also comes the call executeQuery (or executeQueryScalar for SQL-queries that will return a single value like count), either by enumeration of our IQueryable or at the end of LINQ-expression-tree. That will call QueryExpressionTransformer.convertExpression. What happens there (in SqlRuntime.Linq.fs):

In our example, the whole cust object was selected. For security reasons, we don't do SELECT *, but we actually list the columns at compile time.

The TupleIndex of IWithSqlService is a way to collect joined tables to match the sql-aliasses, here the [cust].

SELECT [cust].[Address] as 'Address', 
       [cust].[City] as 'City',
       [cust].[CompanyName] as 'CompanyName',
     [cust].[ContactName] as 'ContactName',
     [cust].[ContactTitle] as 'ContactTitle',
     [cust].[Country] as 'Country',
     [cust].[CustomerID] as 'CustomerID',
     [cust].[Fax] as 'Fax',
     [cust].[Phone] as 'Phone',
     [cust].[PostalCode] as 'PostalCode',
     [cust].[Region] as 'Region' 
FROM main.Customers as [cust] 
WHERE (([cust].[CustomerID]= @param1))

-- params @param1 - "ALFKI";

Projection-lambda

Now, if the select-clause had been complex:

let qry2 = 
 query {
    for emp in dc.Main.Employees do
    select (emp.BirthDate.DayOfYear + 3)
 } |> Seq.toArray

We don't know the function of DayOfYear for each different SQL-providers (Oracle/MSSQL/Odbc/...), but we still want this code to work. The LINQ-expression-tree for this query is:

.Call System.Linq.Queryable.Select(
    .Constant<System.Linq.IQueryable`1[SqlEntity]>(SqlQueryable`1[SqlEntity]),
    '(.Lambda #Lambda1<System.Func`2[SqlEntity,Int32]>))
.Lambda #Lambda1<System.Func`2[SqlEntity,System.Int32]>(SqlEntity $emp) {
    (.Call $emp.GetColumn("BirthDate")).DayOfYear + 3
}

What happens now is that in SqlRuntime.QueryExpression.fs, we parse the whole LINQ-expression-tree, and find the parts that we do know to belong to SQL: the SqlEntity's emp.GetColumn("BirthDate"), and create a lambda-expression where this is replaced with a parameter:

fun empBirthDate -> empBirthDate.DayOfYear + 3

Now when we get the empBirthDate from the SQL result, we can execute this lambda for the parameter in the .NET-side, not SQL, and then we get the correct result. This is done with for e in results -> projector.DynamicInvoke(e) in SqlRuntime.Linq.fs.

How to debug SQLProvider in your own solution, not the test-project

The runtime debugging can be done just like any other:

  1. Build your own project
  2. Then just build SQLProvider.sln and go to bin-folder, select your framework, and copy FSharp.Data.SqlProvider.dll and FSharp.Data.SqlProvider.pdb
  3. Replace your own project's bin-folder copies of these two files, run your software without rebuilding.

Debugging the design-time VS2022 is doable but a bit more complex. This will mess up your SQLProvider Nuget cache, so after done, delete the SQLProvider cache-folder and restore the package again.

  1. Open SQLProvider.sln (with Visual Studio 2022) and build it (in debug mode). Keep this open for now.
  2. Open Explorer, it has made under bin-folder some folders, e.g. \lib and \typeproviders (and under them per framework like \net48 \net6.0 \netstandard2.0 \netstandard2.1)
  3. Open another explorer, go to your location of Nuget cache, the version you are using e.g. C:\Users\me\.nuget\packages\sqlprovider\1.4.8
  4. Replace the Nuget cache \typeproviders folder with your fresh bin typeproviders folder.
  5. Replace the Nuget cache \lib folder with your fresh bin lib folder.
  6. Open another instance of VS2022 to the start-screen, but don't open any project yet.
  7. Go back to your first instance of VS2022 with SQLProvider.sln. Add some breakpoints. Select from the top menu: Debug - Attach to Process...
  8. Select devenv.exe, which is another VS2022 instance.
  9. Switch to this new instance and load your own project that uses SQLProvider, and it'll stop at the breakpoints.

Other things to know

namespace System
Multiple items
namespace FSharp

--------------------
namespace Microsoft.FSharp
Multiple items
namespace FSharp.Data

--------------------
namespace Microsoft.FSharp.Data
namespace FSharp.Data.Sql
Multiple items
type LiteralAttribute = inherit Attribute new: unit -> LiteralAttribute

--------------------
new: unit -> LiteralAttribute
[<Literal>] val connectionString: string = "Data Source=C:\git\SQLProvider\docs\content\core/../../../tests/SqlProvider.Tests/scripts/northwindEF.db;Version=3;foreign keys=true"
[<Literal>] val resolutionPath: string = "C:\git\SQLProvider\docs\content\core/../../../tests/SqlProvider.Tests/libs"
type sql = SqlDataProvider<...>
type SqlDataProvider
<summary>Typed representation of a database</summary> <param name='ConnectionString'>The connection string for the SQL database</param> <param name='ConnectionStringName'>The connection string name to select from a configuration file</param> <param name='DatabaseVendor'> The target database vendor</param> <param name='IndividualsAmount'>The amount of sample entities to project into the type system for each SQL entity type. Default 50. Note GDPR/PII regulations if using individuals with ContextSchemaPath.</param> <param name='UseOptionTypes'>If set, F# option types will be used in place of nullable database columns. If not, you will always receive the default value of the column's type even if it is null in the database.</param> <param name='ResolutionPath'>The location to look for dynamically loaded assemblies containing database vendor specific connections and custom types. Types used in desing-time: If no better clue, prefer .NET Standard 2.0 versions. Semicolon to separate multiple.</param> <param name='Owner'>Oracle: The owner of the schema for this provider to resolve. PostgreSQL: A list of schemas to resolve, separated by spaces, newlines, commas, or semicolons.</param> <param name='CaseSensitivityChange'>Should we do ToUpper or ToLower when generating table names?</param> <param name='TableNames'>Comma separated table names list to limit a number of tables in big instances. The names can have '%' sign to handle it as in the 'LIKE' query (Oracle and MSSQL Only)</param> <param name='ContextSchemaPath'>The location of the context schema previously saved with SaveContextSchema. When not empty, will be used to populate the database schema instead of retrieving it from then database.</param> <param name='OdbcQuote'>Odbc quote characters: Quote characters for the table and column names: `alias`, [alias]</param> <param name='SQLiteLibrary'>Use System.Data.SQLite or Mono.Data.SQLite or select automatically (SQLite only)</param> <param name='SsdtPath'>A path to an SSDT .dacpac file.'</param>
namespace FSharp.Data.Sql.Common
[<Struct>] type DatabaseProviderTypes = | MSSQLSERVER = 0 | SQLITE = 1 | POSTGRESQL = 2 | MYSQL = 3 | ORACLE = 4 | MSACCESS = 5 | ODBC = 6 | FIREBIRD = 7 | MSSQLSERVER_DYNAMIC = 8 | MSSQLSERVER_SSDT = 9 | DUCKDB = 10 | EXTERNAL = 11
Common.DatabaseProviderTypes.SQLITE: Common.DatabaseProviderTypes = 1
[<Struct>] type SQLiteLibrary = | SystemDataSQLite = 0 | MonoDataSQLite = 1 | AutoSelect = 2 | MicrosoftDataSqlite = 3
Common.SQLiteLibrary.SystemDataSQLite: Common.SQLiteLibrary = 0
<summary> .NET Framework default </summary>
[<Struct>] type CaseSensitivityChange = | ORIGINAL = 0 | TOUPPER = 1 | TOLOWER = 2
Common.CaseSensitivityChange.ORIGINAL: Common.CaseSensitivityChange = 0
val dc: SqlDataProvider<...>.dataContext
SqlDataProvider<...>.GetDataContext() : SqlDataProvider<...>.dataContext
<summary>Returns an instance of the SQL Provider using the static parameters</summary>
   (+0 other overloads)
SqlDataProvider<...>.GetDataContext(selectOperations: SelectOperations) : SqlDataProvider<...>.dataContext
<summary>Returns an instance of the SQL Provider using the static parameters</summary><param name='selectOperations'>Execute select-clause operations in SQL database rather than .NET-side.</param>
   (+0 other overloads)
SqlDataProvider<...>.GetDataContext(commandTimeout: int) : SqlDataProvider<...>.dataContext
<summary>Returns an instance of the SQL Provider using the static parameters</summary><param name='commandTimeout'>SQL command timeout. Maximum time for single SQL-command in seconds.</param>
   (+0 other overloads)
SqlDataProvider<...>.GetDataContext(transactionOptions: Transactions.TransactionOptions) : SqlDataProvider<...>.dataContext
<summary>Returns an instance of the SQL Provider using the static parameters</summary><param name='transactionOptions'>TransactionOptions for the transaction created on SubmitChanges.</param>
   (+0 other overloads)
SqlDataProvider<...>.GetDataContext(connectionString: string) : SqlDataProvider<...>.dataContext
<summary>Returns an instance of the SQL Provider using the static parameters</summary><param name='connectionString'>The database runtime connection string</param>
   (+0 other overloads)
SqlDataProvider<...>.GetDataContext(connectionString: string, selectOperations: SelectOperations) : SqlDataProvider<...>.dataContext
<summary>Returns an instance of the SQL Provider using the static parameters</summary><param name='connectionString'>The database runtime connection string</param><param name='selectOperations'>Execute select-clause operations in SQL database rather than .NET-side.</param>
   (+0 other overloads)
SqlDataProvider<...>.GetDataContext(transactionOptions: Transactions.TransactionOptions, commandTimeout: int) : SqlDataProvider<...>.dataContext
<summary>Returns an instance of the SQL Provider using the static parameters</summary><param name='transactionOptions'>TransactionOptions for the transaction created on SubmitChanges.</param><param name='commandTimeout'>SQL command timeout. Maximum time for single SQL-command in seconds.</param>
   (+0 other overloads)
SqlDataProvider<...>.GetDataContext(connectionString: string, commandTimeout: int) : SqlDataProvider<...>.dataContext
<summary>Returns an instance of the SQL Provider using the static parameters</summary><param name='connectionString'>The database runtime connection string</param><param name='commandTimeout'>SQL command timeout. Maximum time for single SQL-command in seconds.</param>
   (+0 other overloads)
SqlDataProvider<...>.GetDataContext(connectionString: string, transactionOptions: Transactions.TransactionOptions) : SqlDataProvider<...>.dataContext
<summary>Returns an instance of the SQL Provider using the static parameters</summary><param name='connectionString'>The database runtime connection string</param><param name='transactionOptions'>TransactionOptions for the transaction created on SubmitChanges.</param>
   (+0 other overloads)
SqlDataProvider<...>.GetDataContext(connectionString: string, resolutionPath: string) : SqlDataProvider<...>.dataContext
<summary>Returns an instance of the SQL Provider using the static parameters</summary><param name='connectionString'>The database runtime connection string</param><param name='resolutionPath'>The location to look for dynamically loaded assemblies containing database vendor specific connections and custom types. Types used in desing-time: If no better clue, prefer .NET Standard 2.0 versions. Semicolon to separate multiple.</param>
   (+0 other overloads)
val qry: SqlDataProvider<...>.dataContext.main.CustomersEntity array
val query: Linq.QueryBuilder
val cust: SqlDataProvider<...>.dataContext.main.CustomersEntity
property SqlDataProvider<...>.dataContext.Main: SqlDataProvider<...>.readDataContext.mainSchema with get
property SqlDataProvider<...>.readDataContext.mainSchema.Customers: SqlDataProvider<...>.readDataContext.mainSchema.main.Customers with get
<summary> The table Customers belonging to schema main</summary>
custom operation: where (bool) Calls Linq.QueryBuilder.Where
property SqlDataProvider<...>.dataContext.main.CustomersEntity.CustomerId: string with get, set
<summary>CustomerID: nchar(5)</summary>
custom operation: select ('Result) Calls Linq.QueryBuilder.Select
Multiple items
module Seq from FSharp.Data.Sql

--------------------
module Seq from Microsoft.FSharp.Collections
val toArray: source: 'T seq -> 'T array
union case Option.None: Option<'T>
val qry2: int array
val emp: SqlDataProvider<...>.dataContext.main.EmployeesEntity
property SqlDataProvider<...>.readDataContext.mainSchema.Employees: SqlDataProvider<...>.readDataContext.mainSchema.main.Employees with get
<summary> The table Employees belonging to schema main</summary>
property SqlDataProvider<...>.dataContext.main.EmployeesEntity.BirthDate: DateTime with get, set
<summary>BirthDate: datetime</summary>
property DateTime.DayOfYear: int with get
<summary>Gets the day of the year represented by this instance.</summary>
<returns>The day of the year, expressed as a value between 1 and 366.</returns>

Type something to start searching.