-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathArgumentCompleters.ps1
89 lines (82 loc) · 6.59 KB
/
ArgumentCompleters.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
function SQLDBSourceCompletion {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
(get-item 'HKLM:\SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources','HKCU:\software\ODBC\ODBC.INI\ODBC Data Sources' -ErrorAction SilentlyContinue).Property.Where({$_ -notmatch " Files$" -and $_ -like "$wordToComplete*" }) |
Sort-Object | ForEach-Object {
$tooltip = (Get-ItemProperty -name $_ -path 'HKLM:\SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources', 'HKCU:\software\ODBC\ODBC.INI\ODBC Data Sources' -ErrorAction SilentlyContinue).$_
New-Object System.Management.Automation.CompletionResult "DSN=$_", "DSN=$_", ([System.Management.Automation.CompletionResultType]::ParameterValue) , $tooltip
}
}
function SQLDBSessionCompletion {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
$Global:DbSessions.Keys | Where-Object { $_ -like "$wordToComplete*" } | Sort-Object | ForEach-Object {
New-Object System.Management.Automation.CompletionResult $_,$_, ([System.Management.Automation.CompletionResultType]::ParameterValue) , $_
}
}
function SQLDBNameCompletion {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
$cmdnameused = $commandAst.toString() -replace "^(.*?)\s.*$",'$1'
if ($Global:DbSessions[$cmdnameused]) {
$session = $cmdnameused
}
else { $session = $(if($fakeBoundParameter['Session']) {$fakeBoundParameter['Session']} else {'Default'} ) }
if ($DbSessions[$session] -is [System.Data.SqlClient.SqlConnection]) {
$dbList = (Get-SQL -Session $session -SQL "SELECT name FROM sys.databases" -Quiet).name
}
else { $dblist = (Get-SQL -Session $session -SQL "show databases" -quiet).database}
$dblist | Where-Object { $_ -like "$wordToComplete*" } | Sort-Object | ForEach-Object {
New-Object System.Management.Automation.CompletionResult $_,$_, ([System.Management.Automation.CompletionResultType]::ParameterValue) , $_
}
}
function SQLTableNameCompletion {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
$cmdnameused = $commandAst.toString() -replace "^(.*?)\s.*$",'$1'
if ($Global:DbSessions[$cmdnameused]) {
$session = $cmdnameused
}
else { $session = $(if($fakeBoundParameter['Session']) {$fakeBoundParameter['Session']} else {'Default'} ) }
if (-not $global:DbSessions[$session] -and $fakeBoundParameter['Connection'] ) {
Get-SQL -Connection $fakeBoundParameter['Connection'] -Session $session | Out-Null
}
if ( $global:DbSessions[$session] ) {
$wordToComplete = ($wordToComplete -replace "^`"|^'|'$|`"$", '' )
Get-SQL -Session $session -Showtables | Where-Object { $_ -like "*$wordToComplete*" } | Sort-Object | ForEach-Object {
$display = $_ -replace "^\[(.*)\]$",'$1' -replace "^'(.*)'$",'$1'
$returnValue = """$_"""
New-Object -TypeName System.Management.Automation.CompletionResult -ArgumentList $returnValue,
$display , ([System.Management.Automation.CompletionResultType]::ParameterValue) ,$display
}
}
}
function SQLFieldNameCompletion {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
$TableName = $fakeBoundParameter['Table']
$cmdnameused = $commandAst.toString() -replace "^(.*?)\s.*$",'$1'
if ($Global:DbSessions[$cmdnameused]) {
$session = $cmdnameused
}
else {
$session = $(if($fakeBoundParameter['Session']) {$fakeBoundParameter['Session']} else {'Default'} ) }
$wordToComplete = ($wordToComplete -replace "^`"|^'|'$|`"$", '' )
Get-SQL -Session $session -describe $TableName | Where-Object { $_.column_name -like "*$wordToComplete*" } | Sort-Object -Property column_name |
ForEach-Object {
$display = $_.COLUMN_NAME -replace "^\[(.*)\]$",'$1' -replace "^'(.*)'$",'$1'
$returnValue = '"' + $_.COLUMN_NAME + '"'
New-Object -TypeName System.Management.Automation.CompletionResult -ArgumentList $returnValue,
$display , ([System.Management.Automation.CompletionResultType]::ParameterValue) ,$display
}
}
#In PowerShell 3 and 4 Register-ArgumentCompleter is part of TabExpansion ++. From V5 it is part of Powershell.core
if (Get-Command -ErrorAction SilentlyContinue -name Register-ArgumentCompleter) {
Register-ArgumentCompleter -CommandName 'Get-SQL' -ParameterName 'Connection' -ScriptBlock $Function:SQLDBSourceCompletion #-Description 'Selects an ODBC Data Source'
Register-ArgumentCompleter -CommandName 'Get-SQL' -ParameterName 'Session' -ScriptBlock $Function:SQLDBSessionCompletion #-Description 'Selects a session already opend by Get-SQL '
Register-ArgumentCompleter -CommandName 'Get-SQL' -ParameterName 'changeDB' -ScriptBlock $Function:SQLDBNameCompletion #-Description 'Selects an alternate Database available in a session'
Register-ArgumentCompleter -CommandName 'Get-SQL' -ParameterName 'Table' -ScriptBlock $Function:SQLTableNameCompletion #-Description 'Complete Table names'
Register-ArgumentCompleter -CommandName 'Get-SQL' -ParameterName 'Insert' -ScriptBlock $Function:SQLTableNameCompletion #-Description 'Complete Table names'
Register-ArgumentCompleter -CommandName 'Get-SQL' -ParameterName 'Describe' -ScriptBlock $Function:SQLTableNameCompletion #-Description 'Complete Table names'
Register-ArgumentCompleter -CommandName 'Get-SQL' -ParameterName 'Where' -ScriptBlock $Function:SQLFieldNameCompletion #-Description 'Complete Field names'
Register-ArgumentCompleter -CommandName 'Get-SQL' -ParameterName 'Set' -ScriptBlock $Function:SQLFieldNameCompletion #-Description 'Complete Field names'
Register-ArgumentCompleter -CommandName 'Get-SQL' -ParameterName 'Set' -ScriptBlock $Function:SQLFieldNameCompletion #-Description 'Complete Field names'
Register-ArgumentCompleter -CommandName 'Get-SQL' -ParameterName 'Select' -ScriptBlock $Function:SQLFieldNameCompletion #-Description 'Complete Field names'
Register-ArgumentCompleter -CommandName 'Get-SQL' -ParameterName 'GroupBy' -ScriptBlock $Function:SQLFieldNameCompletion #-Description 'Complete Field names'
Register-ArgumentCompleter -CommandName 'Get-SQL' -ParameterName 'OrderBy' -ScriptBlock $Function:SQLFieldNameCompletion #-Description 'Complete Field names'
}