Skip to content

Commit

Permalink
Support 'merge' scripts for tables with only PK columns; bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
Christer van der Meeren committed Oct 16, 2024
1 parent aba7840 commit 5ba39d2
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>

<PropertyGroup>
<Version>2.11.0</Version>
<Version>2.12.0</Version>
<DisableImplicitLibraryPacksFolder>true</DisableImplicitLibraryPacksFolder>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<WarningsNotAsErrors>NU1903;$(WarningsNotAsErrors)</WarningsNotAsErrors>
Expand Down
4 changes: 3 additions & 1 deletion RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
Release notes
==============

### Unreleased
### 2.12.0 (2024-10-16)

* Can now generate `merge` table scripts for tables with only primary key columns (will then simply insert if the row
does not exist)
* Updated Microsoft.Data.SqlClient from 5.2.1 to 5.2.2
* Updated generator dependency Microsoft.SqlServer.TransactSql.ScriptDom from 161.9123.0 to 161.9142.1
* Updated generator dependency FSharp.Core from 8.0.301 to 8.0.401
Expand Down
109 changes: 107 additions & 2 deletions src/DbTests.DbGen/DbGen.fs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Edit or remove this or the below line to regenerate on next build
// Hash: 84a6faa9bec6030d0e97ae1261ffa04e8f96d41591c37543b904f7a9bc189d2f
// Hash: 1952bc1e72c53de1e1ce29ac35d111d94a36fbd0abf577a0b35d2d44bf6911fd

//////////////////////////////////////////
//
// THIS FILE IS AUTOMATICALLY GENERATED
//
// Facil 2.11.0+d9bf5204297674fcc5b70b7187da31090ea17b5c
// Facil 2.12.0+aba7840c0d3f333a8cc9c139131e3dcb9d3a6d58
//
//////////////////////////////////////////

Expand Down Expand Up @@ -33083,6 +33083,111 @@ INNER JOIN
``TableWithIdentityCol_UpdateBatch_Executable``(this.connStr, this.conn, this.configureConn, this.userConfigureCmd, getSqlParams, tempTableData, this.tran)


[<EditorBrowsable(EditorBrowsableState.Never)>]
type ``TableWithOnlyPkColumns_Merge_Executable`` (connStr: string, conn: SqlConnection, configureConn: SqlConnection -> unit, userConfigureCmd: SqlCommand -> unit, getSqlParams: unit -> SqlParameter [], tempTableData: seq<TempTableData>, tran: SqlTransaction) =

let configureCmd sqlParams (cmd: SqlCommand) =
cmd.CommandText <- """-- TableWithOnlyPkColumns_Merge
MERGE [dbo].[TableWithOnlyPkColumns]
USING
(
SELECT
[Key1] = @key1,
[Key2] = @key2
)
AS x
ON
[TableWithOnlyPkColumns].[Key1] = x.[Key1]
AND [TableWithOnlyPkColumns].[Key2] = x.[Key2]

WHEN NOT MATCHED THEN
INSERT
(
[Key1],
[Key2]
)
VALUES
(
x.[Key1],
x.[Key2]
)
;"""
cmd.Parameters.AddRange sqlParams
userConfigureCmd cmd

member _.ExecuteAsync(?cancellationToken) =
let sqlParams = getSqlParams ()
executeNonQueryAsync connStr conn tran configureConn (configureCmd sqlParams) tempTableData (defaultArg cancellationToken CancellationToken.None)

member this.AsyncExecute() =
async {
let! ct = Async.CancellationToken
return! this.ExecuteAsync(ct) |> Async.AwaitTask
}

member _.Execute() =
let sqlParams = getSqlParams ()
executeNonQuery connStr conn tran configureConn (configureCmd sqlParams) tempTableData


type ``TableWithOnlyPkColumns_Merge`` private (connStr: string, conn: SqlConnection, tran: SqlTransaction) =

[<EditorBrowsable(EditorBrowsableState.Never)>]
new() =
failwith "This constructor is for aiding reflection and type constraints only"
``TableWithOnlyPkColumns_Merge``(null, null, null)

[<EditorBrowsable(EditorBrowsableState.Never)>]
member val connStr = connStr

[<EditorBrowsable(EditorBrowsableState.Never)>]
member val conn = conn

[<EditorBrowsable(EditorBrowsableState.Never)>]
member val tran = tran

[<EditorBrowsable(EditorBrowsableState.Never)>]
member val configureConn : SqlConnection -> unit = ignore with get, set

[<EditorBrowsable(EditorBrowsableState.Never)>]
member val userConfigureCmd : SqlCommand -> unit = ignore with get, set

member this.ConfigureCommand(configureCommand: SqlCommand -> unit) =
this.userConfigureCmd <- configureCommand
this

static member WithConnection(connectionString, ?configureConnection: SqlConnection -> unit) =
``TableWithOnlyPkColumns_Merge``(connectionString, null, null).ConfigureConnection(?configureConnection=configureConnection)

static member WithConnection(connection, ?transaction) = ``TableWithOnlyPkColumns_Merge``(null, connection, defaultArg transaction null)

member private this.ConfigureConnection(?configureConnection: SqlConnection -> unit) =
match configureConnection with
| None -> ()
| Some config -> this.configureConn <- config
this

member this.WithParameters
(
``key1``: int,
``key2``: int
) =
let getSqlParams () =
[|
SqlParameter("key1", SqlDbType.Int, Value = ``key1``)
SqlParameter("key2", SqlDbType.Int, Value = ``key2``)
|]
``TableWithOnlyPkColumns_Merge_Executable``(this.connStr, this.conn, this.configureConn, this.userConfigureCmd, getSqlParams, [], this.tran)

member inline this.WithParameters(dto: ^a) =
let getSqlParams () =
[|
SqlParameter("key1", SqlDbType.Int, Value = (^a: (member ``Key1``: int) dto))
SqlParameter("key2", SqlDbType.Int, Value = (^a: (member ``Key2``: int) dto))
|]
``TableWithOnlyPkColumns_Merge_Executable``(this.connStr, this.conn, this.configureConn, this.userConfigureCmd, getSqlParams, [], this.tran)


[<EditorBrowsable(EditorBrowsableState.Never)>]
type ``TableWithSkippedPkColumn_ById_Executable`` (connStr: string, conn: SqlConnection, configureConn: SqlConnection -> unit, userConfigureCmd: SqlCommand -> unit, getSqlParams: unit -> SqlParameter [], tempTableData: seq<TempTableData>, tran: SqlTransaction) =

Expand Down
8 changes: 7 additions & 1 deletion src/DbTests.DbGen/facil.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ rulesets:

tableDtos:
- include: '.*'
except: 'WithoutDto|TableWithSqlVariant'
except: 'WithoutDto|TableWithSqlVariant|TableWithOnlyPkColumns'

- for: Voption
voption: true
Expand Down Expand Up @@ -574,3 +574,9 @@ rulesets:
- include: 'dbo\.TableWithSkippedPkColumn'
scripts:
- type: getById

# This is a test in itself: table scripts should work even if the corresponding table DTO has skipped columns that
# the script uses. If this 'test' fails, generation should fail.
- include: 'dbo\.TableWithOnlyPkColumns'
scripts:
- type: merge
17 changes: 9 additions & 8 deletions src/Facil.Generator/Db.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2188,15 +2188,16 @@ let getEverything
|> List.mapAllExceptFirst (sprintf "AND %s")
|> List.map (sprintf " %s")

""
"WHEN MATCHED THEN"
" UPDATE"
" SET"
if not colsToUpdateWithRule.IsEmpty then
""
"WHEN MATCHED THEN"
" UPDATE"
" SET"

yield!
colsToUpdateWithRule
|> List.map (fun (c, _) -> $" [%s{c.Name}] = x.[%s{c.Name}]")
|> List.mapAllExceptLast (sprintf "%s,")
yield!
colsToUpdateWithRule
|> List.map (fun (c, _) -> $" [%s{c.Name}] = x.[%s{c.Name}]")
|> List.mapAllExceptLast (sprintf "%s,")

""
"WHEN NOT MATCHED THEN"
Expand Down
6 changes: 6 additions & 0 deletions src/TestDb/dbo/Tables/TableWithOnlyPkColumns.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE [dbo].[TableWithOnlyPkColumns]
(
[Key1] INT NOT NULL,
[Key2] INT NOT NULL,
PRIMARY KEY ([Key1], [Key2])
)

0 comments on commit 5ba39d2

Please sign in to comment.