Skip to content

Commit

Permalink
readme.md
Browse files Browse the repository at this point in the history
code formatting
  • Loading branch information
pimbrouwers committed Dec 12, 2023
1 parent 52a38b5 commit 234c0d4
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 48 deletions.
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,8 @@ dotnet add package Donald
```fsharp
open Donald
type Author = { FullName : string }
module Author =
let ofDataReader (rd : IDataReader) : Author =
{ FullName = rd.ReadString "full_name" }
type Author =
{ FullName : string }
let authors (conn : IDbConnection) : Author list =
conn
Expand All @@ -56,7 +53,8 @@ let authors (conn : IDbConnection) : Author list =
WHERE author_id = @author_id"
|> Db.setParams [
"author_id", SqlType.Int32 1 ]
|> Db.query Author.ofDataReader
|> Db.query (fun rd ->
{ FullName = rd.ReadString "full_name" })
```

## An Example using SQLite
Expand Down
33 changes: 15 additions & 18 deletions src/Donald/Core.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ namespace Donald
open System
open System.Data
open System.Data.Common
open System.Runtime.Serialization
open System.Threading

[<AutoOpen>]
Expand All @@ -27,8 +26,6 @@ module DbCommandExtensions =

/// Represents a configurable database command.
type DbUnit (cmd : IDbCommand) =
let commandBehavior = CommandBehavior.SequentialAccess

member _.Command = cmd
member val CommandBehavior = CommandBehavior.SequentialAccess with get, set
member val CancellationToken = CancellationToken.None with get,set
Expand All @@ -43,22 +40,22 @@ type DbUnit (cmd : IDbCommand) =
[<RequireQualifiedAccess>]
type SqlType =
| Null
| String of string
| String of string
| AnsiString of string
| Boolean of bool
| Byte of byte
| Char of char
| AnsiChar of char
| Decimal of decimal
| Double of double
| Float of float
| Guid of Guid
| Int16 of int16
| Int32 of int32
| Int of int32
| Int64 of int64
| DateTime of DateTime
| Bytes of byte[]
| Boolean of bool
| Byte of byte
| Char of char
| AnsiChar of char
| Decimal of decimal
| Double of double
| Float of float
| Guid of Guid
| Int16 of int16
| Int32 of int32
| Int of int32
| Int64 of int64
| DateTime of DateTime
| Bytes of byte[]

/// Specifies an input parameter for an IDbCommand.
[<Struct>]
Expand Down
72 changes: 48 additions & 24 deletions src/Donald/IDataReader.fs
Original file line number Diff line number Diff line change
Expand Up @@ -26,80 +26,104 @@ module IDataReaderExtensions =
x.GetOrdinalOption(name) |> Option.map fn

/// Safely retrieve String Option
member x.ReadStringOption(name : string) = name |> x.GetOrdinalOption |> Option.map(fun i -> x.GetString(i))
member x.ReadStringOption(name : string) =
name |> x.GetOrdinalOption |> Option.map(fun i -> x.GetString(i))

/// Safely retrieve Boolean Option
member x.ReadBooleanOption(name : string) = name |> x.GetOption(fun i -> x.GetBoolean(i))
member x.ReadBooleanOption(name : string) =
name |> x.GetOption(fun i -> x.GetBoolean(i))

/// Safely retrieve Byte Option
member x.ReadByteOption(name : string) = name |> x.GetOption(fun i -> x.GetByte(i))
member x.ReadByteOption(name : string) =
name |> x.GetOption(fun i -> x.GetByte(i))

/// Safely retrieve Char Option
member x.ReadCharOption(name : string) = name |> x.GetOption(fun i -> x.GetChar(i))
member x.ReadCharOption(name : string) =
name |> x.GetOption(fun i -> x.GetChar(i))

/// Safely retrieve DateTime Option
member x.ReadDateTimeOption(name : string) = name |> x.GetOption(fun i -> x.GetDateTime(i))
member x.ReadDateTimeOption(name : string) =
name |> x.GetOption(fun i -> x.GetDateTime(i))

/// Safely retrieve Decimal Option
member x.ReadDecimalOption(name : string) = name |> x.GetOption(fun i -> x.GetDecimal(i))
member x.ReadDecimalOption(name : string) =
name |> x.GetOption(fun i -> x.GetDecimal(i))

/// Safely retrieve Double Option
member x.ReadDoubleOption(name : string) = name |> x.GetOption(fun i -> x.GetDouble(i))
member x.ReadDoubleOption(name : string) =
name |> x.GetOption(fun i -> x.GetDouble(i))

/// Safely retrieve Float Option
member x.ReadFloatOption(name : string) = x.ReadDoubleOption name
member x.ReadFloatOption(name : string) =
x.ReadDoubleOption name

/// Safely retrieve Guid Option
member x.ReadGuidOption(name : string) = name |> x.GetOption(fun i -> x.GetGuid(i))
member x.ReadGuidOption(name : string) =
name |> x.GetOption(fun i -> x.GetGuid(i))

/// Safely retrieve Int16 Option
member x.ReadInt16Option (name : string) = name |> x.GetOption(fun i -> x.GetInt16(i))
member x.ReadInt16Option (name : string) =
name |> x.GetOption(fun i -> x.GetInt16(i))

/// Safely retrieve Int32 Option
member x.ReadInt32Option (name : string) = name |> x.GetOption(fun i -> x.GetInt32(i))
member x.ReadInt32Option (name : string) =
name |> x.GetOption(fun i -> x.GetInt32(i))

/// Safely retrieve Int64 Option
member x.ReadInt64Option (name : string) = name |> x.GetOption(fun i -> x.GetInt64(i))
member x.ReadInt64Option (name : string) =
name |> x.GetOption(fun i -> x.GetInt64(i))

// ------------
// Defaults
// ------------

/// Safely retrieve String or return provided default
member x.ReadString(name : string) = x.ReadStringOption name |> Option.defaultValue String.Empty
member x.ReadString(name : string) =
x.ReadStringOption name |> Option.defaultValue String.Empty

/// Safely retrieve Boolean or return provided default
member x.ReadBoolean(name : string) = x.ReadBooleanOption name |> Option.defaultValue false
member x.ReadBoolean(name : string) =
x.ReadBooleanOption name |> Option.defaultValue false

/// Safely retrieve Byte or return provided default
member x.ReadByte(name : string) = x.ReadByteOption name |> Option.defaultValue Byte.MinValue
member x.ReadByte(name : string) =
x.ReadByteOption name |> Option.defaultValue Byte.MinValue

/// Safely retrieve Char or return provided default
member x.ReadChar(name : string) = x.ReadCharOption name |> Option.defaultValue Char.MinValue
member x.ReadChar(name : string) =
x.ReadCharOption name |> Option.defaultValue Char.MinValue

/// Safely retrieve DateTime or return provided default
member x.ReadDateTime(name : string) = x.ReadDateTimeOption name |> Option.defaultValue DateTime.MinValue
member x.ReadDateTime(name : string) =
x.ReadDateTimeOption name |> Option.defaultValue DateTime.MinValue

/// Safely retrieve Decimal or return provided default
member x.ReadDecimal(name : string) = x.ReadDecimalOption name |> Option.defaultValue 0.0M
member x.ReadDecimal(name : string) =
x.ReadDecimalOption name |> Option.defaultValue 0.0M

/// Safely retrieve Double or return provided default
member x.ReadDouble(name : string) = x.ReadDoubleOption name |> Option.defaultValue 0.0
member x.ReadDouble(name : string) =
x.ReadDoubleOption name |> Option.defaultValue 0.0

/// Safely retrieve Float or return provided default
member x.ReadFloat(name : string) = x.ReadFloatOption name |> Option.defaultValue 0.0
member x.ReadFloat(name : string) =
x.ReadFloatOption name |> Option.defaultValue 0.0

/// Safely retrieve Guid or return provided default
member x.ReadGuid(name : string) = x.ReadGuidOption name |> Option.defaultValue Guid.Empty
member x.ReadGuid(name : string) =
x.ReadGuidOption name |> Option.defaultValue Guid.Empty

/// Safely retrieve Int16 or return provided default
member x.ReadInt16 (name : string) = x.ReadInt16Option name |> Option.defaultValue 0s
member x.ReadInt16 (name : string) =
x.ReadInt16Option name |> Option.defaultValue 0s

/// Safely retrieve Int32 or return provided default
member x.ReadInt32 (name : string) = x.ReadInt32Option name |> Option.defaultValue 0
member x.ReadInt32 (name : string) =
x.ReadInt32Option name |> Option.defaultValue 0

/// Safely retrieve Int64 or return provided default
member x.ReadInt64 (name : string) = x.ReadInt64Option name |> Option.defaultValue 0L
member x.ReadInt64 (name : string) =
x.ReadInt64Option name |> Option.defaultValue 0L

/// Safely retrieve byte[]
member x.ReadBytesOption(name : string) : byte[] option =
Expand Down

0 comments on commit 234c0d4

Please sign in to comment.