-
Notifications
You must be signed in to change notification settings - Fork 14
/
Add-SQLTable.ps1
327 lines (283 loc) · 26.3 KB
/
Add-SQLTable.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
function Add-SqlTable {
<#
.Synopsis
Adds a SQL Table
.Description
Creates a new Table in SQL
.Example
Add-SqlTable -TableName PatchyComputers -KeyType Sequential -Column MachineName, GroupName, PatchStatus, PatchWindowStart, PatchStartedAt, LastPatchedAt -DataType 'varchar(100)', 'varchar(100)', 'varchar(20)', 'datetime', 'datetime', 'datetime' -OutputSql -ConnectionStringOrSetting SqlAzureConnectionString
.Link
Select-SQL
.Link
Update-SQL
#>
[CmdletBinding(DefaultParameterSetName='SqlServer')]
[OutputType([Nullable])]
param(
# The name of the SQL table
[Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)]
[string]$TableName,
# The columns to create within the table
[Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)]
[string[]]$Column,
# The keytype to use
[ValidateSet('Guid', 'Hex', 'SmallHex', 'Sequential', 'Named', 'Parameter')]
[string]$KeyType = 'Guid',
# The name of the column to use as a key.
[string]
$RowKey = "RowKey",
# The data types of each column
[Parameter(ValueFromPipelineByPropertyName=$true)]
[string[]]$DataType,
# A connection string or a setting containing a connection string.
[Alias('ConnectionString', 'ConnectionSetting')]
[string]$ConnectionStringOrSetting,
# If set, outputs the SQL, and doesn't execute it
[Switch]
$OutputSQL,
# If set, will use SQL server compact edition
[Parameter(Mandatory=$true,ParameterSetName='SqlCompact')]
[Switch]
$UseSQLCompact,
# If set, will use MySql to connect to the database
[Parameter(Mandatory=$true,ParameterSetName='MySql')]
[Switch]
$UseMySql,
# The path to MySql's .NET connector. If not provided, MySql will be loaded from Program Files
[Parameter(ParameterSetName='MySql')]
[string]
$MySqlPath,
# The path to SQL Compact. If not provided, SQL compact will be loaded from the GAC
[Parameter(ParameterSetName='SqlCompact')]
[string]
$SqlCompactPath,
# If set, will use SQL lite
[Parameter(Mandatory=$true,ParameterSetName='Sqlite')]
[Alias('UseSqlLite')]
[switch]
$UseSQLite,
# The path to SQL Lite. If not provided, SQL compact will be loaded from Program Files
[Parameter(ParameterSetName='Sqlite')]
[string]
$SqlitePath,
# The path to a SQL compact or SQL lite database
[Parameter(Mandatory=$true,ParameterSetName='SqlCompact')]
[Parameter(Mandatory=$true,ParameterSetName='Sqlite')]
[Alias('DBPath')]
[string]
$DatabasePath,
# Foreign keys in the table.
[Parameter(ParameterSetName='SqlServer')]
[Hashtable]
$ForeignKey = @{},
# The size of a string key. By default, 100 characters
[Uint32]
$StringKeyLength = 100
)
begin {
#region Resolve Connection String
if ($PSBoundParameters.ConnectionStringOrSetting) {
if ($ConnectionStringOrSetting -notlike "*;*") {
$ConnectionString = Get-SecureSetting -Name $ConnectionStringOrSetting -ValueOnly
} else {
$ConnectionString = $ConnectionStringOrSetting
}
$script:CachedConnectionString = $ConnectionString
} elseif ($psBoundParameters.Server -and $psBoundParameters.Database) {
$ConnectionString = "Server=$Server;Database=$Database;Integrated Security=True;"
$script:CachedConnectionString = $ConnectionString
} elseif ($script:CachedConnectionString){
$ConnectionString = $script:CachedConnectionString
} else {
$ConnectionString = ""
}
#endregion Resolve Connection String
# Exit if we don't have a connection string,
# and are not using SQLite or SQLCompact (which don't need one)
if (-not $ConnectionString -and -not ($UseSQLite -or $UseSQLCompact)) {
throw "No Connection String"
return
}
#region If we're not just going to output SQL, we might as well connect
if (-not $OutputSQL) {
if ($UseSQLCompact) {
# If we're using SQL compact, make sure it's loaded
if (-not ('Data.SqlServerCE.SqlCeConnection' -as [type])) {
if ($SqlCompactPath) {
$resolvedCompactPath = $ExecutionContext.SessionState.Path.GetResolvedPSPathFromPSPath($SqlCompactPath)
$asm = [reflection.assembly]::LoadFrom($resolvedCompactPath)
} else {
$asm = [reflection.assembly]::LoadWithPartialName("System.Data.SqlServerCe")
}
}
# Find the absolute path
$resolvedDatabasePath = $ExecutionContext.SessionState.Path.GetResolvedPSPathFromPSPath($DatabasePath)
# Craft a connection string
$sqlConnection = New-Object Data.SqlServerCE.SqlCeConnection "Data Source=$resolvedDatabasePath"
# Open the DB
$sqlConnection.Open()
} elseif ($UseSqlite) {
# If we're using SQLite, make sure it's loaded
if (-not ('Data.Sqlite.SqliteConnection' -as [type])) {
if ($sqlitePath) {
$resolvedLitePath = $ExecutionContext.SessionState.Path.GetResolvedPSPathFromPSPath($sqlitePath)
$asm = [reflection.assembly]::LoadFrom($resolvedLitePath)
} else {
$asm = [Reflection.Assembly]::LoadFrom("$env:ProgramFiles\System.Data.SQLite\2010\bin\System.Data.SQLite.dll")
}
}
# Find the absolute path
$resolvedDatabasePath = $ExecutionContext.SessionState.Path.GetResolvedPSPathFromPSPath($DatabasePath)
# Craft a connection string
$sqlConnection = New-Object Data.Sqlite.SqliteConnection "Data Source=$resolvedDatabasePath"
# Open the DB
$sqlConnection.Open()
} elseif ($useMySql) {
if (-not ('MySql.Data.MySqlClient.MySqlConnection' -as [type])) {
if (-not $mySqlPath) {
$programDir = if (${env:ProgramFiles(x86)}) {
${env:ProgramFiles(x86)}
} else {
${env:ProgramFiles}
}
$mySqlPath = Get-ChildItem "$programDir\MySQL\Connector NET 6.7.4\Assemblies\"|
Where-Object { $_.Name -like "*v*" } |
Sort-Object { $_.Name.Replace("v", "") -as [Version] } -Descending |
Select-object -First 1 |
Get-ChildItem -filter "MySql.Data.dll" |
Select-Object -ExpandProperty Fullname
}
$asm = [Reflection.Assembly]::LoadFrom($MySqlPath)
$null = $asm
}
$sqlConnection = New-Object MySql.Data.MySqlClient.MySqlConnection "$ConnectionString"
$sqlConnection.Open()
} else {
# We're using SQL server (or SQL Azure), just use the connection string we've got
$sqlConnection = New-Object Data.SqlClient.SqlConnection "$connectionString"
# Open the DB
$sqlConnection.Open()
}
}
#endregion If we're not just going to output SQL, we might as well connect
#region Determine the "real" type of foreign keys
$ForeignDataTypes = @{}
if ($ForeignKey.Count) {
foreach ($kv in $ForeignKey.GetEnumerator()) {
$v = $kv.Value
$chunks = @($v -split "[\(\)]")
$foreignTable = $chunks[0]
$foreignRef = $chunks[1]
$foreignColumn = Select-SQL "select * from information_schema.columns where column_Name = '$ForeignRef' and table_name = '$foreignTable'"
if ($foreignColumn.Data_Type -ne 'char') {
$ForeignDataTypes[$kv.Key] = $foreignColumn.Data_Type
} else {
$ForeignDataTypes[$kv.Key] = "char($($foreignColumn.CHARACTER_MAXIMUM_LENGTH))"
}
$null = $null
}
}
#endregion Determine the "real" type of foreign keys
}
process {
$columnsAndTypes = New-Object Collections.ArrayList
$rowKeySqlType = if ($KeyType -ne 'Sequential') {
if ($useSqlLite) {
"nchar($StringKeyLength)"
} elseif ($useSqlCompact) {
"nchar($StringKeyLength)"
} else {
"char($StringKeyLength)"
}
} else {
if ($UseSQLite) {
"integer"
} else {
"bigint"
}
}
$autoIncrement = $(if ($KeyType -eq 'Sequential') {
if ($UseSQLite) {
"PRIMARY KEY"
} else {
"PRIMARY KEY IDENTITY"
}
} else {
""
})
if ($UseMySql) {
$null = $columnsAndTypes.Add("$RowKey $rowKeySqlType NOT NULL $(if ($KeyType -eq 'Sequential') { 'AUTO_INCREMENT' })")
} else {
$null = $columnsAndTypes.Add("$RowKey $rowKeySqlType NOT NULL $autoIncrement $(if (-not $autoIncrement) { "PRIMARY KEY"})")
}
$null = $columnsAndTypes.AddRange(@(
for($i =0; $i -lt $Column.Count; $i++) {
$columnDataType =
if ($dataType -and $DataType[$i]) {
$datatype[$i]
} else {
if ($UseSQLite) {
"text"
} elseif ($useSqlCompact) {
"ntext"
} elseif ($useMySql) {
"longtext"
} else {
"varchar(max)"
}
}
if ($UseMySql) {
"$($Column[$i]) $columnDataType"
} else {
if ($ForeignKey[$column[$i]]) {
"`"$($Column[$i])`" $($ForeignDataTypes[$Column[$i]]) FOREIGN KEY References $($ForeignKey[$Column[$i]])"
$null = $null
} else {
"`"$($Column[$i])`" $columnDataType"
}
}
}))
if ($UseMySql) {
$null = $columnsAndTypes.Add("PRIMARY KEY($RowKey)")
}
$createstatement = "CREATE TABLE $tableName (
$($ColumnsAndTypes -join (',' + [Environment]::NewLine + " "))
)"
$sqlStatement = $createstatement
if ($outputSql) {
# If we're outputting SQL, just output it and be done
$sqlStatement
} elseif (-not $outputSql -and $psCmdlet.ShouldProcess($sqlStatement)) {
# If we're not, be so nice as to use ShouldProcess first to confirm
Write-Verbose "$sqlStatement"
#region Execute SQL Statement
if ($UseSQLCompact) {
$sqlAdapter = New-Object "Data.SqlServerCE.SqlCeDataAdapter" $sqlStatement, $sqlConnection
$dataSet = New-Object Data.DataSet
$rowCount = $sqlAdapter.Fill($dataSet)
} elseif ($UseSQLite) {
$sqliteCmd = New-Object Data.Sqlite.SqliteCommand $sqlStatement, $sqlConnection
$rowCount = $sqliteCmd.ExecuteNonQuery()
} elseif ($usemySql) {
$sqlAdapter= New-Object "MySql.Data.MySqlClient.MySqlDataAdapter" ($sqlStatement, $sqlConnection)
$sqlAdapter.SelectCommand.CommandTimeout = 0
$dataSet = New-Object Data.DataSet
$rowCount = $sqlAdapter.Fill($dataSet)
} else {
$sqlAdapter= New-Object "Data.SqlClient.SqlDataAdapter" ($sqlStatement, $sqlConnection)
$sqlAdapter.SelectCommand.CommandTimeout = 0
$dataSet = New-Object Data.DataSet
$rowCount = $sqlAdapter.Fill($dataSet)
}
#endregion Execute SQL Statement
}
}
end {
#region If a SQL connection exists, close it and Dispose of it
if ($sqlConnection) {
$sqlConnection.Close()
$sqlConnection.Dispose()
}
#endregion If a SQL connection exists, close it and Dispose of it
}
}