-
Notifications
You must be signed in to change notification settings - Fork 14
/
Get-SQLTable.ps1
211 lines (180 loc) · 15.5 KB
/
Get-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
function Get-SQLTable
{
<#
.Synopsis
Gets SQL table information
.Description
Gets metadata about a SQL table, including it's columns and their data types
.Example
Get-SqlTable "MySqlTable" -ConnectionString SqlAzureConnectionString
.Link
Add-SqlTable
.Link
Update-SQL
.Link
Remove-SQL
.Link
Select-SQL
#>
[OutputType([PSObject])]
[CmdletBinding(DefaultParameterSetName='SqlServer')]
param(
# The name of the SQL table
[Parameter(Position=0,ValueFromPipelineByPropertyName=$true)]
[string]$TableName,
# 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,
# 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,
# 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
)
begin {
if ($PSBoundParameters.ConnectionStringOrSetting) {
if ($ConnectionStringOrSetting -notlike "*;*") {
$ConnectionString = Get-SecureSetting -Name $ConnectionStringOrSetting -ValueOnly
} else {
$ConnectionString = $ConnectionStringOrSetting
}
$script:CachedConnectionString = $ConnectionString
} elseif ($script:CachedConnectionString){
$ConnectionString = $script:CachedConnectionString
} else {
$ConnectionString = ""
}
if (-not $ConnectionString -and -not ($UseSQLite -or $UseSQLCompact)) {
throw "No Connection String"
return
}
if (-not $OutputSQL) {
if ($UseSQLCompact) {
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")
}
}
$resolvedDatabasePath = $ExecutionContext.SessionState.Path.GetResolvedPSPathFromPSPath($DatabasePath)
$sqlConnection = New-Object Data.SqlServerCE.SqlCeConnection "Data Source=$resolvedDatabasePath"
$sqlConnection.Open()
} elseif ($UseSqlite) {
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")
}
}
$resolvedDatabasePath = $ExecutionContext.SessionState.Path.GetResolvedPSPathFromPSPath($DatabasePath)
$sqlConnection = New-Object Data.Sqlite.SqliteConnection "Data Source=$resolvedDatabasePath"
$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 {
$sqlConnection = New-Object Data.SqlClient.SqlConnection "$connectionString"
$sqlConnection.Open()
}
}
}
process {
$sqlParams = @{} + $psboundparameters
foreach ($k in @($sqlParams.Keys)) {
if ('SqlCompactPath', 'UseSqlCompact', 'SqlitePath', 'UseSqlite', 'DatabasePath', 'ConnectionStringOrSetting'. 'UseMySql', 'MySqlPath' -notcontains $k) {
$sqlParams.Remove($k)
}
}
$columns = try {
$sqlConnection.GetSchema("columns")
} catch {
if (-not $sqlParams.UseSqlLite) {
if ($TableName) {
Select-SQL @sqlParams -FromTable "INFORMATION_SCHEMA.COLUMNS" -Where "Table_Name='$TableName'"
} else {
Select-SQL @sqlParams -FromTable "INFORMATION_SCHEMA.COLUMNS"
}
}
}
$columns | Group-Object Table_Name |
Where-Object {
($TableName -and $_.Name -like $TableName) -or (-not $TableName)
} |
ForEach-Object {
$group = $_.Group
$table = $_.Name
$tableSchema = foreach ($_ in $group) {
$_.Table_Schema
break
}
$columns = @(foreach ($_ in $group) {
$_.Column_Name
})
$dataTypes= @(foreach ($_ in $group) {
$_.Data_Type
})
New-Object PSObject -Property @{
TableName = $table
Columns = $columns
DataTypes = $dataTypes
TableSchema = $tableSchema
}
}
}
end {
if ($sqlConnection) {
$sqlConnection.Close()
$sqlConnection.Dispose()
}
}
}