-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThings_To_Consider_2_Indenting.ps1
128 lines (110 loc) · 4.66 KB
/
Things_To_Consider_2_Indenting.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
########################################################################################################################
#Proper input indentation and bad output indentation
########################################################################################################################
Write-Verbose 'Generating the real function code'
[System.Collections.ArrayList]$FunctionCode = @() #Use an arraylist for efficiency/performance of the code generation
Write-Verbose 'Generate the base function code and make it an advanced function'
[void]$FunctionCode.Add("
function Invoke-SomethingBinary {
#Heres a comment
")
#Generate the code for the process block
$String = '
[System.Collections.ArrayList]$Arguments = @()
#To Do, loop through all the optional parameters and maybe even make them dynamic for an unlimited number
if ($OptionalParameter1) {
[void]$Arguments.Add("$($OptionalParameter1)")
}
if ($OptionalParameter2) {
[void]$Arguments.Add("$($OptionalParameter2)")
}
}
'
Write-Verbose 'Generate the process block'
[void]$FunctionCode.Add($String)
$FunctionCode | clip
########################################################################################################################
#Proper Indentation
########################################################################################################################
Write-Verbose 'Generating the real function code'
[System.Collections.ArrayList]$FunctionCode = @() #Use an arraylist for efficiency/performance of the code generation
Write-Verbose 'Generate the base function code and make it an advanced function'
[void]$FunctionCode.Add("function Invoke-SomethingBinary {
#Heres a comment")
#Generate the code for the process block
$String = ' [System.Collections.ArrayList]$Arguments = @()
#To Do, loop through all the optional parameters and maybe even make them dynamic for an unlimited number
if ($OptionalParameter1) {
[void]$Arguments.Add("$($OptionalParameter1)")
}
if ($OptionalParameter2) {
[void]$Arguments.Add("$($OptionalParameter2)")
}'
Write-Verbose 'Generate the process block'
[void]$FunctionCode.Add($String)
Write-Verbose 'Terminate everything'
[void]$FunctionCode.Add('}')
$FunctionCode | clip
########################################################################################################################
#Source Code Indention
$Target = 'TEMP_Table'
$UniqueIdentifier = Get-Random -Maximum 10
$PrimaryKey = 'PK1'
$InsertColumns = 'Col1,Col2'
$InsertValues = 'Val1,Val2'
$MergeCondition = "AND Target.AttributeName = 'Attrib1'"
$UpdateCondition = "Target.AttributeValue = Source.AttributeValue"
$UpdateConditionWhenMatched = "and Target.AttributeValue != Source.AttributeValue"
#Fully expanded code
$SQLQuery = @"
MERGE INTO $($Target) WITH (READPAST) AS Target
USING $($Target)_$($UniqueIdentifier)_TEMP AS Source
ON Target.[$($PrimaryKey)] = Source.[$($PrimaryKey)]
WHEN NOT MATCHED THEN
INSERT ($InsertColumns) VALUES ($InsertValues)
$(#If condition to see if we actually need a Update Condition
if ($UpdateCondition -notlike $null) {
"WHEN MATCHED $(
if ($UpdateConditionWhenMatched) {
$UpdateConditionWhenMatched
}
)
THEN UPDATE SET $UpdateCondition"
}
)
WHEN NOT MATCHED BY Source $MergeCondition THEN
DELETE;
"@
$SQLQuery | clip
#Output Code Indention, Non expanded code
$SQLQuery = @"
MERGE INTO $($Target) WITH (READPAST) AS Target
USING $($Target)_$($UniqueIdentifier)_TEMP AS Source
ON Target.[$($PrimaryKey)] = Source.[$($PrimaryKey)]
WHEN NOT MATCHED THEN
INSERT ($InsertColumns) VALUES ($InsertValues)
$(#If condition to see if we actually need a Update Condition
if ($UpdateCondition -notlike $null) {
"WHEN MATCHED $(if ($UpdateConditionWhenMatched) {$UpdateConditionWhenMatched})
THEN UPDATE SET $UpdateCondition"})
WHEN NOT MATCHED BY Source $MergeCondition THEN
DELETE;
"@
$SQLQuery | clip
#Output Code Indention, indenting my here string
function Get-IHateMySelf {
$SQLQuery = @"
MERGE INTO $($Target) WITH (READPAST) AS Target
USING $($Target)_$($UniqueIdentifier)_TEMP AS Source
ON Target.[$($PrimaryKey)] = Source.[$($PrimaryKey)]
WHEN NOT MATCHED THEN
INSERT ($InsertColumns) VALUES ($InsertValues)
$(#If condition to see if we actually need a Update Condition
if ($UpdateCondition -notlike $null) {
"WHEN MATCHED $(if ($UpdateConditionWhenMatched) {$UpdateConditionWhenMatched})
THEN UPDATE SET $UpdateCondition"})
WHEN NOT MATCHED BY Source $MergeCondition THEN
DELETE;
"@ #Whitespace is not allowed before the string terminator
$SQLQuery | clip
}