Created
April 24, 2016 15:34
-
-
Save yreynhout/022a37da52cf14a603e5b75a9763d598 to your computer and use it in GitHub Desktop.
F# Projac meets Sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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