Skip to content

Commit

Permalink
feat(loggers): add loggers support
Browse files Browse the repository at this point in the history
  • Loading branch information
Kerry Perret committed Jan 8, 2021
1 parent bf7c47b commit 67a6bfa
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 16 deletions.
67 changes: 52 additions & 15 deletions Vp.FSharp.Sql.Sqlite/Library.fs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace Vp.FSharp.Sql.Sqlite

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

open Vp.FSharp.Sql

Expand All @@ -14,7 +15,28 @@ type SqliteDbValue =
| Text of string
| Blob of byte array

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

type SqliteGlobalConf =
SqlGlobalConf<
SQLiteConnection,
SQLiteCommand>

type SqliteDeps =
SqlDeps<
SQLiteConnection,
SQLiteTransaction,
SQLiteCommand,
SQLiteParameter,
SQLiteDataReader,
SqliteDbValue>

[<RequireQualifiedAccess>]
module SqliteCommand =
Expand All @@ -39,6 +61,11 @@ module SqliteCommand =
parameter.Value <- value
parameter

let private deps: SqliteDeps =
{ 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 =
SqlCommand.text value
Expand All @@ -47,6 +74,14 @@ module SqliteCommand =
let textFromList value : SqliteCommandDefinition =
SqlCommand.textFromList value

/// Update the command definition so that when executing the command, it doesn't use any logger.
/// Be it the default one (Global, if any.) or a previously overriden one.
let noLogger commandDefinition = { commandDefinition with Logger = LoggerKind.Nothing }

/// Update the command definition so that when executing the command, it use the given overriding logger.
/// instead of the default one, aka the Global logger, if any.
let overrideLogger value commandDefinition = { commandDefinition with Logger = LoggerKind.Override value }

/// Update the command definition with the given parameters.
let parameters value (commandDefinition: SqliteCommandDefinition) : SqliteCommandDefinition =
SqlCommand.parameters value commandDefinition
Expand All @@ -69,43 +104,45 @@ module SqliteCommand =

/// Return the sets of rows as an AsyncSeq accordingly to the command definition.
let queryAsyncSeq connection read (commandDefinition: SqliteCommandDefinition) =
SqlCommand.queryAsyncSeq connection dbValueToParameter read commandDefinition
SqlCommand.queryAsyncSeq
connection deps (SqliteGlobalConf.Snapshot) read commandDefinition

/// Return the sets of rows as a list accordingly to the command definition.
let queryList connection read (commandDefinition: SqliteCommandDefinition) =
SqlCommand.queryList connection dbValueToParameter read commandDefinition
SqlCommand.queryList
connection deps (SqliteGlobalConf.Snapshot) read commandDefinition

/// Return the first set of rows as a list accordingly to the command definition.
let querySetList connection read (commandDefinition: SqliteCommandDefinition) =
SqlCommand.querySetList connection dbValueToParameter read commandDefinition
SqlCommand.querySetList
connection deps (SqliteGlobalConf.Snapshot) read commandDefinition

/// Return the 2 first sets of rows as a tuple of 2 lists accordingly to the command definition.
let querySetList2 connection read1 read2 (commandDefinition: SqliteCommandDefinition) =
SqlCommand.querySetList2 connection dbValueToParameter read1 read2 commandDefinition
SqlCommand.querySetList2
connection deps (SqliteGlobalConf.Snapshot) read1 read2 commandDefinition

/// Return the 3 first sets of rows as a tuple of 3 lists accordingly to the command definition.
let querySetList3 connection read1 read2 read3 (commandDefinition: SqliteCommandDefinition) =
SqlCommand.querySetList3 connection dbValueToParameter read1 read2 read3 commandDefinition
SqlCommand.querySetList3
connection deps (SqliteGlobalConf.Snapshot) read1 read2 read3 commandDefinition

/// Execute the command accordingly to its definition and,
/// - return the first cell value, if it is available and of the given type.
/// - throw an exception, otherwise.
let executeScalar<'Scalar> connection (commandDefinition: SqliteCommandDefinition) =
SqlCommand.executeScalar<'Scalar, _, _, _>
connection
dbValueToParameter
commandDefinition
SqlCommand.executeScalar<'Scalar, _, _, _, _, _, _, _, _, _>
connection deps (SqliteGlobalConf.Snapshot) commandDefinition

/// Execute the command accordingly to its definition and,
/// - return Some, if the first cell is available and of the given type.
/// - return None, if first cell is DbNull.
/// - throw an exception, otherwise.
let executeScalarOrNone<'Scalar> connection (commandDefinition: SqliteCommandDefinition) =
SqlCommand.executeScalarOrNone<'Scalar, _, _, _>
connection
dbValueToParameter
commandDefinition
SqlCommand.executeScalarOrNone<'Scalar, _, _, _, _, _, _, _, _, _>
connection deps (SqliteGlobalConf.Snapshot) commandDefinition

/// Execute the command accordingly to its definition and, return the number of rows affected.
let executeNonQuery connection (commandDefinition: SqliteCommandDefinition) =
SqlCommand.executeNonQuery connection dbValueToParameter commandDefinition
SqlCommand.executeNonQuery
connection deps (SqliteGlobalConf.Snapshot) commandDefinition
2 changes: 1 addition & 1 deletion Vp.FSharp.Sql.Sqlite/Vp.FSharp.Sql.Sqlite.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

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

</Project>

0 comments on commit 67a6bfa

Please sign in to comment.