Skip to content

Commit

Permalink
feat(refactor): add specific tc support
Browse files Browse the repository at this point in the history
  • Loading branch information
Kerry Perret committed Jan 23, 2021
1 parent 1534808 commit ed334e7
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 66 deletions.
Original file line number Diff line number Diff line change
@@ -1,75 +1,11 @@
namespace Vp.FSharp.Sql.Sqlite

open System.Data.SQLite
open System.Threading.Tasks

open Vp.FSharp.Sql


/// Native SQLite DB types.
/// See https://www.sqlite.org/datatype3.html
type SqliteDbValue =
| Null
| Integer of int64
| Real of double
| Text of string
| Blob of byte array

type SqliteCommandDefinition =
CommandDefinition<
SQLiteConnection,
SQLiteCommand,
SQLiteParameter,
SQLiteDataReader,
SQLiteTransaction,
SqliteDbValue>

type SqliteConfiguration =
SqlConfigurationCache<
SQLiteConnection,
SQLiteCommand>

type SqliteDependencies =
SqlDependencies<
SQLiteConnection,
SQLiteCommand,
SQLiteParameter,
SQLiteDataReader,
SQLiteTransaction,
SqliteDbValue>

[<RequireQualifiedAccess>]
module SqliteNullDbValue =
let ifNone toDbValue = NullDbValue.ifNone toDbValue SqliteDbValue.Null
let ifError toDbValue = NullDbValue.ifError toDbValue (fun _ -> SqliteDbValue.Null)

[<RequireQualifiedAccess>]
module SqliteCommand =

let private dbValueToParameter name value =
let parameter = SQLiteParameter()
parameter.ParameterName <- name
match value with
| Null ->
parameter.TypeName <- (nameof Null).ToUpperInvariant()
| Integer value ->
parameter.TypeName <- (nameof Integer).ToUpperInvariant()
parameter.Value <- value
| Real value ->
parameter.TypeName <- (nameof Real).ToUpperInvariant()
parameter.Value <- value
| Text value ->
parameter.TypeName <- (nameof Text).ToUpperInvariant()
parameter.Value <- value
| Blob value ->
parameter.TypeName <- (nameof Blob).ToUpperInvariant()
parameter.Value <- value
parameter

let private deps: SqliteDependencies =
{ CreateCommand = fun connection -> connection.CreateCommand()
ExecuteReaderAsync = fun command _ -> Task.FromResult(command.ExecuteReader())
DbValueToParameter = dbValueToParameter }

/// Initialize a command definition with the given text contained in the given string.
let text value : SqliteCommandDefinition =
Expand Down
8 changes: 8 additions & 0 deletions Vp.FSharp.Sql.Sqlite/SqliteNullDbValue.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[<RequireQualifiedAccess>]
module Vp.FSharp.Sql.Sqlite.SqliteNullDbValue

open Vp.FSharp.Sql


let ifNone toDbValue = NullDbValue.ifNone toDbValue SqliteDbValue.Null
let ifError toDbValue = NullDbValue.ifError toDbValue (fun _ -> SqliteDbValue.Null)
22 changes: 22 additions & 0 deletions Vp.FSharp.Sql.Sqlite/SqliteTransaction.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[<RequireQualifiedAccess>]
module Vp.FSharp.Sql.SqlServer.SqliteTransaction

open Vp.FSharp.Sql
open Vp.FSharp.Sql.Sqlite


let beginTransactionAsync = Constants.Deps.BeginTransactionAsync

let commit cancellationToken isolationLevel connection body =
Transaction.commit cancellationToken isolationLevel connection beginTransactionAsync body
let notCommit cancellationToken isolationLevel connection body =
Transaction.notCommit cancellationToken isolationLevel connection beginTransactionAsync body
let commitOnOk cancellationToken isolationLevel connection body =
Transaction.commitOnOk cancellationToken isolationLevel connection beginTransactionAsync body
let commitOnSome cancellationToken isolationLevel connection body =
Transaction.commitOnSome cancellationToken isolationLevel connection beginTransactionAsync body

let defaultCommit connection body = Transaction.defaultCommit connection beginTransactionAsync body
let defaultNotCommit connection body = Transaction.defaultNotCommit connection beginTransactionAsync body
let defaultCommitOnOk connection body = Transaction.defaultCommitOnOk connection beginTransactionAsync body
let defaultCommitOnSome connection body = Transaction.defaultCommitOnSome connection beginTransactionAsync body
76 changes: 76 additions & 0 deletions Vp.FSharp.Sql.Sqlite/Types.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
namespace Vp.FSharp.Sql.Sqlite

open System.Data
open System.Data.SQLite
open System.Threading.Tasks

open Vp.FSharp.Sql


/// Native SQLite DB types.
/// See https://www.sqlite.org/datatype3.html
type SqliteDbValue =
| Null
| Integer of int64
| Real of double
| Text of string
| Blob of byte array

type SqliteCommandDefinition =
CommandDefinition<
SQLiteConnection,
SQLiteCommand,
SQLiteParameter,
SQLiteDataReader,
SQLiteTransaction,
SqliteDbValue>

type SqliteConfiguration =
SqlConfigurationCache<
SQLiteConnection,
SQLiteCommand>

type SqliteDependencies =
SqlDependencies<
SQLiteConnection,
SQLiteCommand,
SQLiteParameter,
SQLiteDataReader,
SQLiteTransaction,
SqliteDbValue>

[<AbstractClass; Sealed>]
type internal Constants private () =

static member DbValueToParameter name value =
let parameter = SQLiteParameter()
parameter.ParameterName <- name
match value with
| Null ->
parameter.TypeName <- (nameof Null).ToUpperInvariant()
| Integer value ->
parameter.TypeName <- (nameof Integer).ToUpperInvariant()
parameter.Value <- value
| Real value ->
parameter.TypeName <- (nameof Real).ToUpperInvariant()
parameter.Value <- value
| Text value ->
parameter.TypeName <- (nameof Text).ToUpperInvariant()
parameter.Value <- value
| Blob value ->
parameter.TypeName <- (nameof Blob).ToUpperInvariant()
parameter.Value <- value
parameter

static member Deps : SqliteDependencies =
let beginTransactionAsync (connection: SQLiteConnection) (isolationLevel: IsolationLevel) _ =
ValueTask.FromResult(connection.BeginTransaction(isolationLevel))

let executeReaderAsync (command: SQLiteCommand) _ =
Task.FromResult(command.ExecuteReader())

{ CreateCommand = fun connection -> connection.CreateCommand()
SetCommandTransaction = fun command transaction -> command.Transaction <- transaction
BeginTransactionAsync = beginTransactionAsync
ExecuteReaderAsync = executeReaderAsync
DbValueToParameter = Constants.DbValueToParameter }
7 changes: 5 additions & 2 deletions Vp.FSharp.Sql.Sqlite/Vp.FSharp.Sql.Sqlite.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@
</PropertyGroup>

<ItemGroup>
<Compile Include="Library.fs" />
<Compile Include="Types.fs" />
<Compile Include="SqliteNullDbValue.fs" />
<Compile Include="SqliteCommand.fs" />
<Compile Include="SqliteTransaction.fs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.113.1" />
<PackageReference Include="Vp.FSharp.Sql" Version="1.0.35" />
<PackageReference Include="Vp.FSharp.Sql" Version="1.0.41" />
</ItemGroup>

</Project>

0 comments on commit ed334e7

Please sign in to comment.