Skip to content

Instantly share code, notes, and snippets.

@yreynhout
Created April 24, 2016 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yreynhout/022a37da52cf14a603e5b75a9763d598 to your computer and use it in GitHub Desktop.
Save yreynhout/022a37da52cf14a603e5b75a9763d598 to your computer and use it in GitHub Desktop.
F# Projac meets Sql
#r "packages/Paramol/lib/net45/Paramol.dll"
open System
open System.Data
open System.Data.Common
open Paramol
open Paramol.SqlClient
type PortfolioCreated = { PortfolioId: Guid; Name:String; When: DateTimeOffset; }
type PortfolioRenamed = { PortfolioId: Guid; Name:String; When: DateTimeOffset; }
type PortfolioPhotoAdded = { PortfolioId: Guid; Filename:String; When: DateTimeOffset; }
type PortfolioPhotoRemoved = { PortfolioId: Guid; Filename:String; When: DateTimeOffset; }
type PortfolioDeleted = { PortfolioId: Guid; When: DateTimeOffset; }
type PortfolioViewMessages =
| PortfolioCreated of PortfolioCreated
| PortfolioRenamed of PortfolioRenamed
| PortfolioPhotoAdded of PortfolioPhotoAdded
| PortfolioPhotoRemoved of PortfolioPhotoRemoved
| PortfolioDeleted of PortfolioDeleted
type SqlClientSyntax with
member this.UniqueIdentifier(value: Guid) = this.UniqueIdentifier(new Nullable<Guid>(value))
let Sql = new SqlClientSyntax()
let portfolioViewSqlProjector (message:PortfolioViewMessages) =
match message with
| PortfolioCreated m ->
seq {
yield Sql.NonQueryStatementFormat(
"INSERT INTO [Portfolio] ([PortfolioId], [Name]) VALUES ({0}, {1})",
[| Sql.UniqueIdentifier(m.PortfolioId); Sql.NVarCharMax(m.Name) |])
}
| PortfolioRenamed m ->
seq {
yield Sql.NonQueryStatementFormat(
"UPDATE [Portfolio] SET [Name] = {1} WHERE [PortfolioId] = {0}",
[| Sql.UniqueIdentifier(m.PortfolioId); Sql.NVarCharMax(m.Name) |])
}
| PortfolioPhotoAdded m ->
seq {
yield Sql.NonQueryStatementFormat(
"INSERT INTO [PortfolioPhoto] ([PortfolioId], [Filename]) VALUES ({0}, {1})",
[| Sql.UniqueIdentifier(m.PortfolioId); Sql.NVarCharMax(m.Filename) |])
}
| PortfolioPhotoRemoved m ->
seq {
yield Sql.NonQueryStatementFormat(
"DELETE FROM [PortfolioPhoto] WHERE [PortfolioId] = {0} AND [Filename] = {1}",
[| Sql.UniqueIdentifier(m.PortfolioId); Sql.NVarCharMax(m.Filename) |])
}
| PortfolioDeleted m ->
seq {
yield Sql.NonQueryStatementFormat(
"DELETE FROM [PortfolioPhoto] WHERE [PortfolioId] = {0}",
[| Sql.UniqueIdentifier(m.PortfolioId); |])
yield Sql.NonQueryStatementFormat(
"DELETE FROM [Portfolio] WHERE [PortfolioId] = {0}",
[| Sql.UniqueIdentifier(m.PortfolioId); |])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment