Visitar URL original
PostgreSQL Provider | SQLProvider

SQLProvider

PostgreSQL Provider

SQLProvider with Oracle is available via both NuGet-Packages:

Parameters

DatabaseVendor

From the FSharp.Data.Sql.Common.DatabaseProviderTypes enumeration. For PostgreSQL, use Common.DatabaseProviderTypes.POSTGRESQL.

let [<Literal>] dbVendor = Common.DatabaseProviderTypes.POSTGRESQL

ConnectionString

A basic connection string used to connect to PostgreSQL instance; typical connection strings for the driver apply here. See PostgreSQL Connecting Strings Documentation for a complete list of connection string options.

let [<Literal>] connString = "Host=localhost;Database=test;Username=test;Password=test"

ConnectionStringName

Instead of storing the connection string in the source code / fsx script, you can store values in the App.config file. This is the name of the connectionString key/value pair stored in App.config (TODO: confirm file name).

// found in App.config (TOOD: confirm)
let [<Literal>] connexStringName = "DefaultConnectionString"

Resolution Path

Path to search for assemblies containing database vendor-specific connections and custom types. Type the path where Npgsql.Data.dll is stored.

let [<Literal>] resPath = @"C:\Projects\Libs\Npgsql"

IndividualsAmount

Sets the count to load for each individual. See individuals for further info.

let [<Literal>] indivAmount = 1000

UseOptionTypes

If true, F# option types will be used in place of nullable database columns. If false, you will always receive the default value of the column's type, even if it is null in the database. You can also use VALUE_OPTION.

let [<Literal>] useOptTypes  = FSharp.Data.Sql.Common.NullableColumnType.OPTION

Owner

Indicates the schema or schemas to which SqlProvider will try to provide access to. Multiple schemas can be indicated and separated by commas or semicolons. Defaults to "public".

let [<Literal>] owner = "public, admin, references"


type sql =
    SqlDataProvider<
        dbVendor,
        connString,
        "",         //ConnectionNameString can be left empty
        resPath,
        indivAmount,
        useOptTypes,
        owner>

Functions and procedures

Functions are under ctx.Functions and procedures (PostgreSQL 11 and newer) under ctx.Procedures. Each has Invoke and InvokeAsync, which take the routine's IN and INOUT parameters in their order.

let ctx = sql.GetDataContext()

// A function's value
let total = ctx.Functions.OrderTotal.Invoke(orderId).ReturnValue

// A function that returns a table or a refcursor gives its rows
let rows = ctx.Functions.OrdersOf.Invoke(customerId).ReturnValue

// A procedure; its INOUT and OUT parameters are properties of the result
ctx.Procedures.CloseOrder.Invoke orderId
let next = ctx.Procedures.NextNumber.Invoke("invoice", 0).number

What

How it is called

Name

By schema and name, so the schema needn't be on the connection's search path.

Function

SELECT * FROM schema.function(...)

Procedure

CALL schema.procedure(...); an OUT parameter's place takes a NULL (PostgreSQL 14 and newer).

Transaction

Inside a TransactionScope the call runs in the scope's transaction. Outside one it runs in its own, so a procedure can't commit or roll back itself.

Npgsql

The same calls with every Npgsql version: SQLProvider writes the call's text itself.

A function that returns a set of one column (RETURNS SETOF integer, or a table of one column) gives its first row's value; return a table of two or more columns to read the rows.

val connectionString: string
Multiple items
type LiteralAttribute = inherit Attribute new: unit -> LiteralAttribute

--------------------
new: unit -> LiteralAttribute
val resolutionPath: string = "C:\git\SQLProvider\docs\content\core..\..\..\files\sqlite"
Multiple items
namespace FSharp

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

--------------------
namespace Microsoft.FSharp.Data
namespace FSharp.Data.Sql
val dbVendor: Common.DatabaseProviderTypes = 2
namespace FSharp.Data.Sql.Common
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
<summary> Specifies the database provider type for the SQL type provider. Each provider has its own specific implementation for SQL generation and data type mapping. </summary>
Common.DatabaseProviderTypes.POSTGRESQL: Common.DatabaseProviderTypes = 2
<summary> PostgreSQL using Npgsql provider </summary>
val connString: string = "Host=localhost;Database=test;Username=test;Password=test"
val connexStringName: string = "DefaultConnectionString"
val resPath: string = "C:\Projects\Libs\Npgsql"
val indivAmount: int = 1000
val useOptTypes: Common.NullableColumnType = 1
type NullableColumnType = | NO_OPTION = 0 | OPTION = 1 | VALUE_OPTION = 2
<summary> Specifies how to represent nullable database columns in the generated types. </summary>
Common.NullableColumnType.OPTION: Common.NullableColumnType = 1
<summary> Option types are Option&lt;_&gt;. (Old true.) </summary>
val owner: string = "public, admin, references"
type sql = obj
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 design-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>
val ctx: obj
val total: obj
val rows: obj
val next: obj

Type something to start searching.