Skip to content

Commit

Permalink
SqlHydra.Query v2.4.1 - added implicit ContextType conversions.
Browse files Browse the repository at this point in the history
  • Loading branch information
JordanMarr committed Apr 23, 2024
1 parent 6b3a805 commit 6ea5d91
Show file tree
Hide file tree
Showing 10 changed files with 190 additions and 164 deletions.
154 changes: 82 additions & 72 deletions README.md

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions src/SqlHydra.Query/SelectBuilders.fs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,21 @@ open System.Threading
open System.Threading.Tasks
open SqlKata

/// The context type that determines how the query context is created and disposed.
/// Can be implicitly converted from a QueryContext, a function that creates a QueryContext, a Task that creates a QueryContext, or an Async that creates a QueryContext.
type ContextType =
/// A new QueryContext will be created and disposed within the select builder.
| Create of create: (unit -> QueryContext)
/// A new QueryContext will be created and disposed within the select builder.
| CreateTask of create: (unit -> Task<QueryContext>)
/// A new QueryContext will be created and disposed within the select builder.
| CreateAsync of create: (unit -> Async<QueryContext>)
/// A shared QueryContext will be used and not disposed within the select builder.
| Shared of QueryContext
static member op_Implicit(ctx: QueryContext) = Shared ctx
static member op_Implicit(createFn: unit -> QueryContext) = Create createFn
static member op_Implicit(createFn: unit -> Task<QueryContext>) = CreateTask createFn
static member op_Implicit(createFn: unit -> Async<QueryContext>) = CreateAsync createFn

module ContextUtils =
let private tryOpen (ctx: QueryContext) =
Expand Down
2 changes: 1 addition & 1 deletion src/SqlHydra.Query/SqlHydra.Query.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>netstandard2.0;net6.0;net7.0</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<WarnOn>3390;$(WarnOn)</WarnOn>
<Version>2.4.0</Version>
<Version>2.4.1</Version>
<Description>SqlHydra.Query is an F# query builder powered by SqlKata.Query that supports SQL Server, PostgreSQL, Sqlite and Oracle.</Description>
<WarningsAsErrors>
<!-- Incomplete pattern matches on this expression. -->
Expand Down
36 changes: 18 additions & 18 deletions src/Tests/Npgsql/QueryIntegrationTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ let ``Insert and Get Id``() = task {
ctx.BeginTransaction()

let! prodReviewId =
insertTask (Shared ctx) {
insertTask ctx {
for r in production.productreview do
entity
{
Expand Down Expand Up @@ -442,22 +442,22 @@ let ``Distinct Test``() = task {
match currencies with
| Some currencies ->
let! rowsInserted =
insertTask (Shared ctx) {
insertTask ctx {
for e in sales.currency do
entities currencies
}

Assert.AreEqual(rowsInserted, 3, "Expected 3 rows to be inserted")

let! results =
selectTask HydraReader.Read (Shared ctx) {
selectTask HydraReader.Read ctx {
for c in sales.currency do
where (c.currencycode =% "BC%")
select c.name
}

let! distinctResults =
selectTask HydraReader.Read (Shared ctx) {
selectTask HydraReader.Read ctx {
for c in sales.currency do
where (c.currencycode =% "BC%")
select c.name
Expand Down Expand Up @@ -567,13 +567,13 @@ let ``Enum Tests``() = task {
#endif

let! deleteResults =
deleteTask (Shared ctx) {
deleteTask ctx {
for p in ext.person do
deleteAll
}

let! insertResults =
insertTask (Shared ctx) {
insertTask ctx {
into ext.person
entity (
{
Expand All @@ -586,14 +586,14 @@ let ``Enum Tests``() = task {
Assert.IsTrue(insertResults > 0, "Expected insert results > 0")

let! query1Results =
selectTask HydraReader.Read (Shared ctx) {
selectTask HydraReader.Read ctx {
for p in ext.person do
select p
toList
}

let! updateResults =
updateTask (Shared ctx) {
updateTask ctx {
for p in ext.person do
set p.currentmood ext.mood.happy
where (p.currentmood = ext.mood.ok)
Expand All @@ -602,7 +602,7 @@ let ``Enum Tests``() = task {
Assert.IsTrue(updateResults > 0, "Expected update results > 0")

let! query2Results =
selectTask HydraReader.Read (Shared ctx) {
selectTask HydraReader.Read ctx {
for p in ext.person do
select p
toList
Expand All @@ -617,7 +617,7 @@ let ``OnConflictDoUpdate``() = task {
ctx.BeginTransaction()

let upsertCurrency currency =
insertTask (Shared ctx) {
insertTask ctx {
for c in sales.currency do
entity currency
onConflictDoUpdate c.currencycode (c.name, c.modifieddate)
Expand Down Expand Up @@ -693,7 +693,7 @@ let ``Query Employee Record with DateOnly``() = task {
use ctx = openContext()

let! employees =
selectTask HydraReader.Read (Shared ctx) {
selectTask HydraReader.Read ctx {
for e in humanresources.employee do
select e
}
Expand All @@ -706,7 +706,7 @@ let ``Query Employee Column with DateOnly``() = task {
use ctx = openContext()

let! employeeBirthDates =
selectTask HydraReader.Read (Shared ctx) {
selectTask HydraReader.Read ctx {
for e in humanresources.employee do
select e.birthdate
}
Expand All @@ -727,7 +727,7 @@ let ``Test Array Columns``() = task {
}

let! insertResults =
insertTask (Shared ctx) {
insertTask ctx {
into ext.arrays
entity row
}
Expand All @@ -736,7 +736,7 @@ let ``Test Array Columns``() = task {


let! query1Result =
selectTask HydraReader.Read (Shared ctx) {
selectTask HydraReader.Read ctx {
for r in ext.arrays do
select r
tryHead
Expand All @@ -745,7 +745,7 @@ let ``Test Array Columns``() = task {
Assert.AreEqual(query1Result, Some row, "Expected query result to match inserted row.")

let! query2Result =
selectTask HydraReader.Read (Shared ctx) {
selectTask HydraReader.Read ctx {
for r in ext.arrays do
select (r.integer_array, r.text_array)
tryHead
Expand All @@ -762,7 +762,7 @@ let ``Update Employee DateOnly``() = task {
ctx.BeginTransaction()

let! employees =
selectTask HydraReader.Read (Shared ctx) {
selectTask HydraReader.Read ctx {
for e in humanresources.employee do
select e
}
Expand All @@ -773,7 +773,7 @@ let ``Update Employee DateOnly``() = task {
let birthDate = System.DateOnly(1980, 1, 1)

let! result =
updateTask (Shared ctx) {
updateTask ctx {
for e in humanresources.employee do
set e.birthdate birthDate
where (e.businessentityid = emp.businessentityid)
Expand All @@ -782,7 +782,7 @@ let ``Update Employee DateOnly``() = task {
result =! 1

let! refreshedEmp =
selectTask HydraReader.Read (Shared ctx) {
selectTask HydraReader.Read ctx {
for e in humanresources.employee do
where (e.businessentityid = emp.businessentityid)
tryHead
Expand Down
6 changes: 3 additions & 3 deletions src/Tests/Oracle/QueryIntegrationTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -413,22 +413,22 @@ let ``Distinct Test``() = task {
match countriesAL1 with
| Some countries ->
let! rowsInserted =
insertTask (Shared ctx) {
insertTask ctx {
for e in OT.COUNTRIES do
entities countries
}

Assert.AreEqual(rowsInserted, 3, "Expected 3 rows to be inserted")

let! results =
selectTask HydraReader.Read (Shared ctx) {
selectTask HydraReader.Read ctx {
for c in OT.COUNTRIES do
where (c.COUNTRY_ID =% "X%")
select c.COUNTRY_NAME
}

let! distinctResults =
selectTask HydraReader.Read (Shared ctx) {
selectTask HydraReader.Read ctx {
for c in OT.COUNTRIES do
where (c.COUNTRY_ID =% "X%")
select c.REGION_ID
Expand Down
Loading

0 comments on commit 6ea5d91

Please sign in to comment.