mysql.cc
上传用户:tsgydb
上传日期:2007-04-14
资源大小:10674k
文件大小:63k
- /* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
- /* mysql command tool
- * Commands compatible with mSQL by David J. Hughes
- *
- * Written by:
- * Michael 'Monty' Widenius
- * Andi Gutmans <andi@zend.com>
- * Zeev Suraski <zeev@zend.com>
- * Jani Tolonen <jani@mysql.com>
- *
- **/
- #include <global.h>
- #include <my_sys.h>
- #include <m_string.h>
- #include <m_ctype.h>
- #include "mysql.h"
- #include "errmsg.h"
- #include <my_dir.h>
- #ifndef __GNU_LIBRARY__
- #define __GNU_LIBRARY__ // Skip warnings in getopt.h
- #endif
- #include <getopt.h>
- #include "my_readline.h"
- #include <signal.h>
- const char *VER="11.13";
- /* Don't try to make a nice table if the data is too big */
- #define MAX_COLUMN_LENGTH 1024
- gptr sql_alloc(unsigned size); // Don't use mysqld alloc for these
- void sql_element_free(void *ptr);
- #include "sql_string.h"
- extern "C" {
- #if defined(HAVE_CURSES_H) && defined(HAVE_TERM_H)
- #include <curses.h>
- #include <term.h>
- #else
- #if defined(HAVE_TERMIOS_H)
- #include <termios.h>
- #include <unistd.h>
- #elif defined(HAVE_TERMBITS_H)
- #include <termbits.h>
- #elif defined(HAVE_ASM_TERMBITS_H) && (!defined __GLIBC__ || !(__GLIBC__ > 2 || __GLIBC__ == 2 && __GLIBC_MINOR__ > 0))
- #include <asm/termbits.h> // Standard linux
- #endif
- #undef VOID
- #if defined(HAVE_TERMCAP_H)
- #include <termcap.h>
- #else
- #ifdef HAVE_CURSES_H
- #include <curses.h>
- #endif
- #undef SYSV // hack to avoid syntax error
- #ifdef HAVE_TERM_H
- #include <term.h>
- #endif
- #endif
- #endif
- #undef bcmp // Fix problem with new readline
- #undef bzero
- #ifdef __WIN__
- #include <conio.h>
- #else
- #include <readline/readline.h>
- #define HAVE_READLINE
- #endif
- //int vidattr(long unsigned int attrs); // Was missing in sun curses
- }
- #if !defined(HAVE_VIDATTR)
- #undef vidattr
- #define vidattr(A) {} // Can't get this to work
- #endif
- #ifdef __WIN__
- #define cmp_database(A,B) my_strcasecmp((A),(B))
- #else
- #define cmp_database(A,B) strcmp((A),(B))
- #endif
- #include "completion_hash.h"
- typedef struct st_status
- {
- int exit_status;
- ulong query_start_line;
- char *file_name;
- LINE_BUFFER *line_buff;
- bool batch,add_to_history;
- } STATUS;
- static HashTable ht;
- enum enum_info_type { INFO_INFO,INFO_ERROR,INFO_RESULT};
- typedef enum enum_info_type INFO_TYPE;
- static MYSQL mysql; /* The connection */
- static bool info_flag=0,ignore_errors=0,wait_flag=0,quick=0,
- connected=0,opt_raw_data=0,unbuffered=0,output_tables=0,
- no_rehash=0,skip_updates=0,safe_updates=0,one_database=0,
- opt_compress=0,
- vertical=0,skip_line_numbers=0,skip_column_names=0,opt_html=0,
- opt_nopager=1, opt_outfile=0, no_named_cmds=1;
- static uint verbose=0,opt_silent=0,opt_mysql_port=0;
- static my_string opt_mysql_unix_port=0;
- static int connect_flag=CLIENT_INTERACTIVE;
- static char *current_host,*current_db,*current_user=0,*opt_password=0,
- *default_charset;
- static char *histfile;
- static String glob_buffer,old_buffer;
- static int wait_time = 5;
- static STATUS status;
- static ulong select_limit,max_join_size,opt_connect_timeout=0;
- static char default_pager[FN_REFLEN];
- char pager[FN_REFLEN], outfile[FN_REFLEN];
- FILE *PAGER, *OUTFILE;
- #include "sslopt-vars.h"
- #ifndef DBUG_OFF
- const char *default_dbug_option="d:t:o,/tmp/mysql.trace";
- #endif
- void tee_fprintf(FILE *file, const char *fmt, ...);
- void tee_fputs(const char *s, FILE *file);
- void tee_puts(const char *s, FILE *file);
- void tee_putc(int c, FILE *file);
- /* The names of functions that actually do the manipulation. */
- static int get_options(int argc,char **argv);
- static int com_quit(String *str,char*),
- com_go(String *str,char*), com_ego(String *str,char*),
- com_print(String *str,char*),
- com_help(String *str,char*), com_clear(String *str,char*),
- com_connect(String *str,char*), com_status(String *str,char*),
- com_use(String *str,char*), com_source(String *str, char*),
- com_rehash(String *str, char*), com_tee(String *str, char*),
- com_notee(String *str, char*);
- #ifndef __WIN__
- static int com_nopager(String *str, char*), com_pager(String *str, char*),
- com_edit(String *str,char*);
- #endif
- static int read_lines(bool execute_commands);
- static int sql_connect(char *host,char *database,char *user,char *password,
- uint silent);
- static int put_info(const char *str,INFO_TYPE info,uint error=0);
- static void safe_put_field(const char *pos,ulong length);
- static void init_pager();
- static void end_pager();
- static void init_tee();
- static void end_tee();
- /* A structure which contains information on the commands this program
- can understand. */
- typedef struct {
- const char *name; /* User printable name of the function. */
- char cmd_char; /* msql command character */
- int (*func)(String *str,char *); /* Function to call to do the job. */
- bool takes_params; /* Max parameters for command */
- const char *doc; /* Documentation for this function. */
- } COMMANDS;
- static COMMANDS commands[] = {
- { "help", 'h', com_help, 0, "Display this help." },
- { "?", '?', com_help, 0, "Synonym for `help'." },
- { "clear", 'c', com_clear, 0, "Clear command."},
- { "connect",'r', com_connect,1,
- "Reconnect to the server. Optional arguments are db and host." },
- #ifndef __WIN__
- { "edit", 'e', com_edit, 0, "Edit command with $EDITOR."},
- #endif
- { "ego", 'G', com_ego, 0,
- "Send command to mysql server, display result vertically."},
- { "exit", 'q', com_quit, 0, "Exit mysql. Same as quit."},
- { "go", 'g', com_go, 0, "Send command to mysql server." },
- #ifndef __WIN__
- { "nopager",'n', com_nopager,0, "Disable pager, print to stdout." },
- #endif
- { "notee", 't', com_notee, 0, "Don't write into outfile." },
- #ifndef __WIN__
- { "pager", 'P', com_pager, 1,
- "Set PAGER [to_pager]. Print the query results via PAGER." },
- #endif
- { "print", 'p', com_print, 0, "Print current command." },
- { "quit", 'q', com_quit, 0, "Quit mysql." },
- { "rehash", '#', com_rehash, 0, "Rebuild completion hash." },
- { "source", '.', com_source, 1,
- "Execute a SQL script file. Takes a file name as an argument."},
- { "status", 's', com_status, 0, "Get status information from the server."},
- { "tee", 'T', com_tee, 1,
- "Set outfile [to_outfile]. Append everything into given outfile." },
- { "use", 'u', com_use, 1,
- "Use another database. Takes database name as argument." },
- /* Get bash-like expansion for some commands */
- { "create table", 0, 0, 0, ""},
- { "create database", 0, 0, 0, ""},
- { "drop", 0, 0, 0, ""},
- { "select", 0, 0, 0, ""},
- { "insert", 0, 0, 0, ""},
- { "replace", 0, 0, 0, ""},
- { "update", 0, 0, 0, ""},
- { "delete", 0, 0, 0, ""},
- { "explain", 0, 0, 0, ""},
- { "show databases", 0, 0, 0, ""},
- { "show fields from", 0, 0, 0, ""},
- { "show keys from", 0, 0, 0, ""},
- { "show tables", 0, 0, 0, ""},
- { "load data from", 0, 0, 0, ""},
- { "alter table", 0, 0, 0, ""},
- { "set option", 0, 0, 0, ""},
- { "lock tables", 0, 0, 0, ""},
- { "unlock tables", 0, 0, 0, ""},
- { (char *)NULL, 0, 0, 0, ""}
- };
- static const char *load_default_groups[]= { "mysql","client",0 };
- #ifdef HAVE_READLINE
- extern "C" void add_history(char *command); /* From readline directory */
- extern "C" int read_history(char *command);
- extern "C" int write_history(char *command);
- static void initialize_readline (char *name);
- #endif
- static COMMANDS *find_command (char *name,char cmd_name);
- static bool add_line(String &buffer,char *line,char *in_string);
- static void remove_cntrl(String &buffer);
- static void print_table_data(MYSQL_RES *result);
- static void print_table_data_html(MYSQL_RES *result);
- static void print_tab_data(MYSQL_RES *result);
- static void print_table_data_vertically(MYSQL_RES *result);
- static ulong start_timer(void);
- static void end_timer(ulong start_time,char *buff);
- static void mysql_end_timer(ulong start_time,char *buff);
- static void nice_time(double sec,char *buff,bool part_second);
- static sig_handler mysql_end(int sig);
- int main(int argc,char *argv[])
- {
- char buff[80];
- MY_INIT(argv[0]);
- DBUG_ENTER("main");
- DBUG_PROCESS(argv[0]);
- strmov(outfile, "