1# bash completion for Postgresql                           -*- shell-script -*-
2
3_pg_databases()
4{
5    # -w was introduced in 8.4, https://launchpad.net/bugs/164772
6    # "Access privileges" in output may contain linefeeds, hence the NF > 1
7    COMPREPLY=($(compgen -W "$(psql -XAtqwlF $'\t' 2>/dev/null |
8        awk 'NF > 1 { print $1 }')" -- "$cur"))
9}
10
11_pg_users()
12{
13    # -w was introduced in 8.4, https://launchpad.net/bugs/164772
14    COMPREPLY=($(compgen -W "$(psql -XAtqwc 'select usename from pg_user' \
15        template1 2>/dev/null)" -- "$cur"))
16    ((${#COMPREPLY[@]} == 0)) && COMPREPLY=($(compgen -u -- "$cur"))
17}
18
19# createdb(1) completion
20#
21_createdb()
22{
23    local cur prev words cword split
24    _init_completion -s || return
25
26    case $prev in
27        --host | -!(-*)h)
28            _known_hosts_real -- "$cur"
29            return
30            ;;
31        --username | --owner | -!(-*)[UO])
32            _pg_users
33            return
34            ;;
35        --help | --version | --port | --tablespace | --encoding | --template | -!(-*)[pDET])
36            return
37            ;;
38    esac
39
40    $split && return
41
42    if [[ $cur == -* ]]; then
43        COMPREPLY=($(compgen -W '$(_parse_help "$1")' -- "$cur"))
44        [[ ${COMPREPLY-} == *= ]] && compopt -o nospace
45    else
46        _pg_databases
47    fi
48} &&
49    complete -F _createdb createdb
50
51# createuser(1) completion
52#
53_createuser()
54{
55    local cur prev words cword split
56    _init_completion -s || return
57
58    case $prev in
59        --help | --version | --port | --connection-limit | -!(-*)[pc])
60            return
61            ;;
62        --host | -!(-*)h)
63            _known_hosts_real -- "$cur"
64            return
65            ;;
66        --username | -!(-*)U)
67            _pg_users
68            return
69            ;;
70    esac
71
72    $split && return
73
74    if [[ $cur == -* ]]; then
75        COMPREPLY=($(compgen -W '$(_parse_help "$1")' -- "$cur"))
76        [[ ${COMPREPLY-} == *= ]] && compopt -o nospace
77    fi
78} &&
79    complete -F _createuser createuser
80
81# dropdb(1) completion
82#
83_dropdb()
84{
85    local cur prev words cword split
86    _init_completion -s || return
87
88    case $prev in
89        --host | -!(-*)h)
90            _known_hosts_real -- "$cur"
91            return
92            ;;
93        --username | -!(-*)U)
94            _pg_users
95            return
96            ;;
97        --help | --version)
98            return
99            ;;
100    esac
101
102    $split && return
103
104    if [[ $cur == -* ]]; then
105        COMPREPLY=($(compgen -W '$(_parse_help "$1")' -- "$cur"))
106        [[ ${COMPREPLY-} == *= ]] && compopt -o nospace
107    else
108        _pg_databases
109    fi
110} &&
111    complete -F _dropdb dropdb
112
113# dropuser(1) completion
114#
115_dropuser()
116{
117    local cur prev words cword split
118    _init_completion -s || return
119
120    case $prev in
121        --help | --version | --port | -!(-*)p)
122            return
123            ;;
124        --host | -!(-*)h)
125            _known_hosts_real -- "$cur"
126            return
127            ;;
128        --username | -!(-*)U)
129            _pg_users
130            return
131            ;;
132    esac
133
134    $split && return
135
136    if [[ $cur == -* ]]; then
137        COMPREPLY=($(compgen -W '$(_parse_help "$1")' -- "$cur"))
138        [[ ${COMPREPLY-} == *= ]] && compopt -o nospace
139    else
140        _pg_users
141    fi
142} &&
143    complete -F _dropuser dropuser
144
145# psql(1) completion
146#
147_psql()
148{
149    local cur prev words cword split
150    _init_completion -s || return
151
152    case $prev in
153        --host | -!(-*)h)
154            _known_hosts_real -- "$cur"
155            return
156            ;;
157        --username | -!(-*)U)
158            _pg_users
159            return
160            ;;
161        --dbname | -!(-*)d)
162            _pg_databases
163            return
164            ;;
165        --output | --file | --log-file | -!(-*)[ofL])
166            _filedir
167            return
168            ;;
169        --help | --version | --command | --field-separator | --port | --pset | \
170            --record-separator | --table-attr | --set | --variable | -!(-*)[?VcFpPRTv])
171            return
172            ;;
173    esac
174
175    $split && return
176
177    if [[ $cur == -* ]]; then
178        # return list of available options
179        COMPREPLY=($(compgen -W '$(_parse_help "$1")' -- "$cur"))
180        [[ ${COMPREPLY-} == *= ]] && compopt -o nospace
181    else
182        # return list of available databases
183        _pg_databases
184    fi
185} &&
186    complete -F _psql psql
187
188# ex: filetype=sh
189