Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a stored procedure in Postgresql through F# and Npgsql

I am trying to call a stored procedure in postgresql from F# using the Npgsql type provider.

Currently, I am connected to the database as follows:

open System
open System.Data
open System.Data.Entity
open System.Data.Linq
open Microsoft.FSharp.Data.TypeProviders
open Microsoft.FSharp.Linq
open Npgsql
open NpgsqlTypes

type internal dbSchema = SqlEntityConnection<ConnectionString="**my connection string**", Provider="Npgsql">

let internal db = dbSchema.GetDataContext()

However, I only see the tables on the db type, not any of the stored procedures. Is there a way to use the stored procedures in a statically typed manner through the type provider, instead of just calling the raw query string?

like image 760
Ben Hamner Avatar asked Nov 03 '22 20:11

Ben Hamner


1 Answers

I know this question was asked along time ago, but I thought I would add a reference to the SqlProvider. This has recently had support for PostgreSQL added to it and it includes support for SPROCS.

 [<Literal>]
 let connStr = "User ID=postgres;Password=password;Host=POSTGRESQL;Port=9090;Database=hr;"

 [<Literal>]
 let resolutionFolder = @"D:\Downloads\Npgsql-2.1.3-net40\"

 type HR = SqlDataProvider<ConnectionString=connStr,DatabaseVendor=Common.DatabaseProviderTypes.POSTGRESQL, ResolutionPath = resolutionFolder>
 let ctx = HR.GetDataContext()

 ctx.Procedures.ADD_JOB_HISTORY(100, DateTime(1993, 1, 13), DateTime(1998, 7, 24), "IT_PROG", 60)


 //Support for sprocs that return ref cursors
 let employees =
     [
       for e in ctx.Functions.GET_EMPLOYEES().ReturnValue do
           yield e
     ]

Where the resolution folder points to the location of the NPGSQL .NET assemblies.

like image 163
Colin Bull Avatar answered Nov 09 '22 06:11

Colin Bull