Skip to content

Commit

Permalink
Add UPSERT function for Oracle
Browse files Browse the repository at this point in the history
  • Loading branch information
Akaizoku committed Feb 26, 2020
1 parent 5ae6602 commit df8deb2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Public/Invoke-OracleCmd.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ function Invoke-OracleCmd {
try {
$Connection.Open()
} catch {
Write-Log -Type "ERROR" -Object "Unable to reach database $($Hostname):$PortNumber/$Instance"
Write-Log -Type "ERROR" -Object "Unable to reach database $($Hostname):$PortNumber/$ServiceName"
return $Error
}
# Create SQL command
Expand Down
32 changes: 31 additions & 1 deletion Public/Write-InsertOrUpdate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function Write-InsertOrUpdate {
File name: Write-InsertOrUpdate.ps1
Author: Florian Carrier
Creation date: 15/10/2019
Last modified: 10/02/2020
Last modified: 26/02/2020
#>
[CmdletBinding (
SupportsShouldProcess = $true
Expand Down Expand Up @@ -111,6 +111,36 @@ function Write-InsertOrUpdate {
Process {
switch ($Vendor) {
"Oracle" {
# Define existence check
foreach($Key in $PrimaryKey) {
if ($PrimaryKeyCheck -eq $null) { $PrimaryKeyCheck = "$Key = $($Fields.$Key)" }
else { $PrimaryKeyCheck += " AND $Key = $($Fields.$Key)" }
}
$Check = [System.String]::Concat("MERGE INTO $Table USING dual ON (", $PrimaryKeyCheck, ")")

# Loop through fields
foreach ($Field in $Fields.GetEnumerator()) {
# Select update values
if ($Field.Key -NotIn $PrimaryKey) {
if ($UpdateValues -eq $null) { $UpdateValues = "$($Field.Key) = $($Field.Value)" }
else { $UpdateValues += ", $($Field.Key) = $($Field.Value)" }
}
# Set insert fields
if ($InsertFields -eq $null) { $InsertFields = "$($Field.Key)" }
else { $InsertFields += ", $($Field.Key)" }
# Set insert values
if ($InsertValues -eq $null) { $InsertValues = "$($Field.Value)" }
else { $InsertValues += ", $($Field.Value)" }
}

# Construct update query
$Update = [System.String]::Concat("WHEN MATCHED THEN UPDATE SET ", $UpdateValues)
# Construct insert query
$Insert = [System.String]::Concat("WHEN NOT MATCHED THEN INSERT (", $InsertFields, ") VALUES (", $InsertValues, ")")

# Construct whole SQL query
$Query = [System.String]::Concat($Check, "`n", $Update, "`n", $Insert)

# TODO
}
"SQLServer" {
Expand Down

0 comments on commit df8deb2

Please sign in to comment.