#compdef mysql mysqlshow mysqldump mysqladmin

# Adam Spiers <adam@spiers.net>

# Auxiliary functions

_mysql_get_identity () {
  _mysql_user=${(v)opt_args[(i)-u|--user]}
  _mysql_port=${(v)opt_args[(i)-P|--port]}
  _mysql_host=${(v)opt_args[(i)-h|--host]}

  _mysql_params=(
    ${_mysql_user:+"--user=$_mysql_user"}
    ${_mysql_host:+"--host=$_mysql_host"}
    ${_mysql_port:+"--port=$_mysql_port"}
  )
}

_mysql_hosts () {
  local _mysql_user _mysql_port _mysql_host _mysql_params
  _mysql_get_identity

  _wanted hosts expl 'server host' \
    _combination -s '[:@]' '' hosts-ports-users \
      ${_mysql_user:+users=${_mysql_user:q}} \
      ${_mysql_port:+ports=${_mysql_port:q}} \
      hosts -
}

_mysql_ports () {
  local _mysql_user _mysql_port _mysql_host _mysql_params
  _mysql_get_identity

  _wanted ports expl 'server port' \
    _combination -s '[:@]' '' hosts-ports-users \
      ${_mysql_user:+users=${_mysql_user:q}} \
      ${_mysql_host:+hosts=${_mysql_host:q}} \
      ports -
}

_mysql_users () {
  local _mysql_user _mysql_port _mysql_host _mysql_params
  _mysql_get_identity

  _wanted users expl 'server username' \
    _combination -s '[:@]' '' hosts-ports-users \
      ${_mysql_host:+hosts=${_mysql_host:q}} \
      ${_mysql_port:+ports=${_mysql_port:q}} \
      users -
}

_mysql_databases () {
  local _mysql_user _mysql_port _mysql_host _mysql_params
  _mysql_get_identity

  compadd "$@" - ${${(f)~~"$(_call_program databases \
      mysql "$_mysql_params[@]" <<<'show databases' 2>/dev/null)"}[2,-1]}
}

_mysql_tables () {
  local _mysql_user _mysql_port _mysql_host _mysql_params
  _mysql_get_identity

  compadd "$@" - ${${(f)~~"$(_call_program tables \
      mysql "$_mysql_params[@]" $1 <<< 'show tables' 2>/dev/null)"}[2,-1]}
}

_mysql_variables () {
  _values -s , 'MySQL client variables' \
    'max_allowed_packet[maximum allowed packet size]:packet size in bytes:' \
    'net_buffer_length[network buffer length]:buffer length in bytes:'
}


# The actual completion code for the commands

_mysql () {
  _arguments -s \
    "$_mysql_common_opts[@]" \
    '(- :)'{-V,--version}'[display client version]' \
    {-A,--no-auto-rehash}'[no automatic rehashing]' \
    '(-t --table)'{-B,--batch}'[batch mode (no pretty ASCII)]' \
    {-T,--debug-info}'[print debug info on exit]' \
    {-e,--exec-command}':batch-execute specified command and exit: ' \
    {-f,--force}'[continue through errors]' \
    {-i,--ignore-space}'[ignore space after function names]' \
    {-H,--html}'[produce HTML output]' \
    {-n,--unbuffered}'[flush the buffer after each query]' \
    {-O,--set-variable=}':set variable:_mysql_variables' \
    {-o,--one-database}'[only update the default database]' \
    {-q,--quick}'[disable caching of the result]' \
    {-r,--raw}'[write fields without conversion]' \
    {-s,--silent}'[silent mode]' \
    {-L,--skip-line-numbers}"[don't write line number for errors]" \
    {-N,--skip-column-names}"[don't write column names in results]" \
    '(-B --batch)'{-t,--table}'[output in table format]' \
    {-v,--verbose}'[verbose mode]' \
    {-E,--vertical}'[print query output vertically]' \
    {-w,--wait}'[wait and retry server connection if necessary]' \
    ':MySQL database:_mysql_databases'
}

_mysqlshow () {
  _arguments -s \
    "$_mysql_common_opts[@]" \
    {-V,--version}'[display version]' \
    {-\#+,--debug=}':debug file: ' \
    ':MySQL database to show:_mysql_databases' \
    ':table to show:{ _mysql_tables "$line[1]" }' \
    ':field wildcard: '
}

_mysqldump () {
  _arguments -s \
    "$_mysql_common_opts[@]" \
    {-V,--version}'[display version]' \
    {-a,--all}'[include all create options]' \
    {-\#+,--debug=}':debug file: ' \
    {-c,--complete-insert}'[use complete insert statements]' \
    {-e,--extended-insert}'[allow new INSERT syntax]' \
    "--add-drop-table[add a 'drop table' before each create]" \
    "--add-locks[add locks around insert statements]" \
    "--allow-keywords[allow creation of column names that are keywords]" \
    "--delayed[insert rows with INSERT DELAYED]" \
    {-F,--flush-logs}'[flush logs file in server before dump]' \
    {-l,--lock-tables}'[lock all tables for read]' \
    {-t,--no-create-info}"[don't write table creation info]" \
    {-d,--no-data}"[don't write row information]" \
    {-O,--set-variable}':set variable:_mysql_variables' \
    '--opt[create fastest possible dump for reading]' \
    {-q,--quick}"[don't buffer, dump directly to stdout]" \
    {-T,--tab=}"[dump tab-separated text files for each table]:directory to store text files:_files -/" \
    {-w+,--where=}'[dump only selected records]:WHERE clause to limit records to dump: ' \
    '--fields-terminated-by=:(with --tab) fields in textfile terminated by ...: ' \
    '--fields-enclosed-by=:(with --tab) fields in import file enclosed by ...: ' \
    '--fields-optionally-enclosed-by=:(with --tab) fields in import file optionally enclosed by ...: ' \
    '--fields-escaped-by=:(with --tab) fields in import file escaped by ...: ' \
    '--lines-terminated-by=:(with --tab) lines in import file terminated by ...: ' \
    ':MySQL database to dump:_mysql_databases' \
    '*:tables to dump:{ _mysql_tables "$line[1]" }'
}

_mysqladmin () {
  local curcontext="$curcontext" state line expl ret=1
  typeset -A opt_args

  _arguments -C -s \
    "$_mysql_common_opts[@]" \
    '(- *)'{-v,--version}'[display version]' \
    '(-\# --debug)'{-\#+,--debug=}'[output debug log]:debug file' \
    '(-f --force)'{-f,--force}'[continue through errors]' \
    '(-i --sleep)'{-i+,--sleep=}'[repeat commands periodically]:number of seconds between executions: ' \
    '(-s --silent)'{-s,--silent}"[silently exit if can't connect to server]" \
    '(-t --timeout)'{-t+,--timeout=}'[specify timeout for connection]:time (seconds)' \
    '(-w --wait)'{-w+,--wait=}'[wait and retry server connection if necessary]:number of retries' \
    '*::mysqladmin command:->mysqladmin-commands' && ret=0

  if [[ -n "$state" ]]; then
    if (( CURRENT == 1 )); then
      _wanted commands expl 'mysqladmin command' compadd - \
	  create drop extended-status \
	  flush-{hosts,logs,status,tables,privileges} \
	  kill password ping processlist \
	  reload refresh shutdown \
	  status variables version && ret=0
    else
      case "${words[1]:t}" in
	create|drop)
	  _wanted databases expl "MySQL databases" _mysql_databases && ret=0
	;;
	kill)
	  _message -e ids 'thread ids'
	;;
	password)
	  _message -e passwords 'new password'
	;;
      esac
    fi
  fi

  return ret
}

_mysql_utils () {
  local _mysql_common_opts

  _mysql_common_opts=(
    '(-)'{-\?,--help}'[display help]'
    '(-S --socket)'{-S+,--socket=}'[specify socket file to use for connection]:server socket file:_files'
    '(-h --host)'{-h+,--host=}'[specify server hostname]:hostname:_mysql_hosts'
    '(-P,--port)'{-P+,--port=}'[specify port number for connection]:server port:_mysql_ports'
    '(-u --user)'{-u+,--user=}'[specify username for login]:server username:_mysql_users'
    '(-p --password)'{-p+,--password=}'[specify password to use for connection]:server password'
    '(-C --compress)'{-C,--compress}'[use compression in server/client protocol]'
  )

  case "$service" in
    mysql)
      _mysql "$@"
    ;;
    mysqlshow)
      _mysqlshow "$@"
    ;;
    mysqldump)
      _mysqldump "$@"
    ;;
    mysqladmin)
      _mysqladmin "$@"
    ;;
  esac
}

_mysql_utils "$@"
