-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathConvertTo-View.ps1
More file actions
150 lines (131 loc) · 4.44 KB
/
ConvertTo-View.ps1
File metadata and controls
150 lines (131 loc) · 4.44 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<#
.SYNOPSIS
Converts a tabular object to a read-only view for SQL Server, PostgreSQL, MySQL,
MriaDB and SQLite
.DESCRIPTION
A quick way to create the code for a view from an array of objects that all
have the same keys in the same order for every row. You'd get this from reading
in a JSON result. You can specify a list of one or more columns to exclude
.PARAMETER TheObject
an array of psCustomObjects, each of which have the same noteproperty names for the
column values
.PARAMETER TheNameOfTheView
A description of the TheNameOfTheView parameter.
.PARAMETER Exclude
list of columns/Keys in the tabular object to exclude from the view.
.PARAMETER TypeOfView
RDBMSs seem to conform to either the MySQL syntax (a CTE) or the SQL Server/Postgresql
table-bale Constructor (TVC) syntax of the DDL code for the view
.EXAMPLE
PS C:\> ConvertToView MyObjectReadFromJSON 'TheNameOfTheView'
#>
function ConvertTo-View
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true,
ValueFromPipeline = $true,
Position = 1)]
[Object[]]$TheObject,
[Parameter(Mandatory = $true,
Position = 2)]
[string]$TheNameOfTheView,
[Parameter(Mandatory = $False,
Position = 3)]
[string]$style = 'TVC',
[Parameter(Mandatory = $false,
Position = 4)]
[Array]$exclude = @(),
[Parameter(Mandatory = $false,
Position = 5)]
[Object[]]$Rules = $null
)
$Lines = @()
$columnList = @()
$firstLine = $true
$TheValuesStatements =
$TheObject | ForEach-Object {
$line = $_;
$LineProperties = $line.PSObject.Properties
If ($columnList.count -eq 0)
{
$columnList = ($LineProperties | where { $_.Name -notin $exclude } | foreach{
$_.Name
}
) -join ', '
}
$Values = $LineProperties | where { $_.Name -notin $exclude } | foreach{
$TheColumn = $_;
if ($TheColumn.Value -eq $null) { 'NULL' }
elseif ($TheColumn.Value.ToString() -eq 'NULL') { 'NULL' }
elseif ($TheColumn.TypeNameOfValue -eq 'System.String')
{
$TheString = '''' + $TheColumn.Value.Replace("'", "''").Trim() + ''''
if ($Rules.("$($TheColumn.Name)-column") -ne $null)
{
#write-warning "$($_.Name)-column Rule was used for $($_.Name)!"
$TheRule = [string]$Rules.("$($TheColumn.Name)-column")
$TheRule.replace('xxx', $TheString)
}
else { $TheString }
}
elseif ($TheColumn.TypeNameOfValue -eq 'System.Boolean')
{
if ($TheColumn.Value) { '1' }
else { '0' }
}
elseif ($TheColumn.TypeNameOfValue -eq 'System.DateTime')
{ '''' + $TheColumn.Value + '''' }
else { $TheColumn.Value }
} | foreach{
if ($firstLine -and $style -notin @('TVC', 'CTE'))
{ "$($_) AS `"$($TheColumn.Name)`"" }
else { $_ }
}
$lines += "$($Values -join ', ')";
$FirstLine = $False;
}
$joinString = "),`r`n(";
If ($Style -eq 'TVC')
{
@"
CREATE VIEW $TheNameOfTheView
AS
SELECT $columnList
FROM
( VALUES ($($Lines -join $joinString))) AS xxx (
$columnList);
"@
}
elseIf ($Style -eq 'CTE')
{@"
CREATE VIEW $TheNameOfTheView
as
WITH xxx($columnList) as
(VALUES ($($Lines -join $joinstring)))
select $columnList from xxx;
"@
}
else
{ @"
CREATE VIEW $TheNameOfTheView
as
SELECT $($Lines -join "`r`nUNION ALL`r`n SELECT ");
"@
}
}
<#
$result=@'
[
{"Country":"Irish","First":"Dé Luan", "Second":"Dé Mairt", "Third":"Dé Céadaoin","Fourth":"Déardaoin","Fifth":"Dé h-Aoine","Sixth":"Dé Sathairn","Seventh":"Dé Domhnaigh"},
{"Country":"German","First":"Montag","Second":"Dienstag","Third":"Mittwoch","Fourth":"Donnerstag","Fifth":"Freitag","Sixth":"Samstag","Seventh":"Sonntag"},
{"Country":"Galician","First":"luns","Second":"martes","Third":"mércores","Fourth":"xoves","Fifth":"venres","Sixth":"sábado","Seventh":"domingo"},
{"Country":"British","First":"Monday","Second":"Tuesday","Third":"Wednesday","Fourth":"Thursday","Fifth":"Friday","Sixth":"Saturday","Seventh":"Sunday"},
{"Country":"French","First":"Lund1","Second":"mardi","Third":"mercredi","Fourth":"jeudi","Fifth":"vendredi","Sixth":"samedi","Seventh":"Dimanche"},
{"Country":"Italian","First":" lunedì","Second":"martedì","Third":"mercoledì","Fourth":"giovedì","Fifth":"venerdì","Sixth":" sabato","Seventh":"domenica"}]
'@|convertfrom-json
convertTo-View -TheObject $result -TheNameOfTheView 'WordsForWeekdays' -style 'CTE'
convertTo-View -TheObject $result -TheNameOfTheView 'employee' -style 'TVC'
convertTo-View -TheObject $result -TheNameOfTheView 'employee' -style 'SIMPLE'
#>