summaryrefslogtreecommitdiff
path: root/completions/postgresql
blob: f874b67b4effeb173957b528b1e334332849321a (plain)
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
# bash completion for Postgresql

have psql && {
_pg_databases()
{
    # -w was introduced in 8.4, https://launchpad.net/bugs/164772
    # "Access privileges" in output may contain linefeeds, hence the NF > 1
    COMPREPLY=( $( compgen -W "$( psql -AtqwlF $'\t' 2>/dev/null | \
        awk 'NF > 1 { print $1 }' )" -- "$cur" ) )
}

_pg_users()
{
    # -w was introduced in 8.4, https://launchpad.net/bugs/164772
    COMPREPLY=( $( compgen -W "$( psql -Atqwc 'select usename from pg_user' \
        template1 2>/dev/null )" -- "$cur" ) )
    [ ${#COMPREPLY[@]} -eq 0 ] && COMPREPLY=( $( compgen -u -- "$cur" ) )
}

# createdb(1) completion
#
_createdb()
{
    local cur prev split=false

    COMPREPLY=()
    _get_comp_words_by_ref cur prev

    _split_longopt && split=true

    case $prev in
        -h|--host)
            _known_hosts_real "$cur"
            return 0
            ;;
        -U|--username|-O|--owner)
            _pg_users
            return 0
            ;;
        -p|--port|-D|--tablespace|-E|--encoding|-T|--template)
            # argument required but no completions available
            return 0
            ;;
        --help|--version)
            # all other arguments are noop with these
            return 0
            ;;
    esac

    $split && return 0

    if [[ "$cur" == -* ]]; then
        COMPREPLY=( $( compgen -W '--tablespace --template --encoding --host \
            --port --username --password --echo --quiet --help --version' \
            -- "$cur" ) )
    else
        _pg_databases
    fi
}
complete -F _createdb -o default createdb

# dropdb(1) completion
#
_dropdb()
{
    local cur prev split=false

    COMPREPLY=()
    _get_comp_words_by_ref cur prev

    _split_longopt && split=true

    case $prev in
        -h|--host)
            _known_hosts_real "$cur"
            return 0
            ;;
        -U|--username)
            _pg_users
            return 0
            ;;
        --help|--version)
            # all other arguments are noop with these
            return 0
            ;;
    esac

    $split && return 0

    if [[ "$cur" == -* ]]; then
        COMPREPLY=( $( compgen -W '--host --port --username --password \
            --interactive --echo --quiet --help --version' -- "$cur" ) )
    else
        _pg_databases
    fi
}
complete -F _dropdb -o default dropdb

# psql(1) completion
#
_psql()
{
    local cur prev split=false

    COMPREPLY=()
    _get_comp_words_by_ref cur prev

    _split_longopt && split=true

    case $prev in
        -h|--host)
            _known_hosts_real "$cur"
            return 0
            ;;
        -U|--username)
            _pg_users
            return 0
            ;;
        -d|--dbname)
            _pg_databases
            return 0
            ;;
        -o|--output|-f|--file|-L|--log-file)
            _filedir
            return 0
            ;;
        -c|--command|-F|--field-separator|-p|--port|-P|--pset|\
        -R|--record-separator|-T|--table-attr|-v|--set|--variable)
            # argument required but no completions available
            return 0
            ;;
        -\?|--help|-V|--version)
            # all other arguments are noop with these
            return 0
            ;;
    esac

    $split && return 0

    if [[ "$cur" == -* ]]; then
        # return list of available options
        COMPREPLY=( $( compgen -W '--echo-all --no-align --command --dbname \
            --echo-queries --echo-hidden --file --field-separator --host \
            --html --list --log-file --output --port --pset --quiet \
            --record-separator --single-step --single-line --tuples-only \
            --table-attr --username --set --version --password --expanded \
            --no-psqlrc --single-transaction --help' -- "$cur" ) )
    else
        # return list of available databases
        _pg_databases
    fi
}
complete -F _psql psql
}

# Local variables:
# mode: shell-script
# sh-basic-offset: 4
# sh-indent-comment: t
# indent-tabs-mode: nil
# End:
# ex: ts=4 sw=4 et filetype=sh