2001-12-06 13:10:51 +01:00
/* Copyright (C) 2000 MySQL AB
2000-08-30 19:40:59 +02:00
2000-07-31 21:29:14 +02:00
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 .
2000-08-30 19:40:59 +02:00
2000-07-31 21:29:14 +02:00
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 .
2000-08-30 19:40:59 +02:00
2000-07-31 21:29:14 +02:00
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 */
/* mysqldump.c - Dump a tables contents and format to an ASCII file
* *
* * The author ' s original notes follow : -
* *
2001-10-04 19:43:06 +02:00
* * AUTHOR : Igor Romanenko ( igor @ frog . kiev . ua )
* * DATE : December 3 , 1994
* * WARRANTY : None , expressed , impressed , implied
* * or other
* * STATUS : Public domain
* * Adapted and optimized for MySQL by
* * Michael Widenius , Sinisa Milivojevic , Jani Tolonen
* * - w - - where added 9 / 10 / 98 by Jim Faucette
* * slave code by David Saez Padros < david @ ols . es >
* * master / autocommit code by Brian Aker < brian @ tangent . org >
* * SSL by
* * Andrei Errapart < andreie @ no . spam . ee >
2003-03-28 20:24:32 +01:00
* * Tõnu Samuel < tonu @ please . do . not . remove . this . spam . ee >
2001-11-05 22:48:03 +01:00
* * XML by Gary Huntress < ghuntress @ mediaone . net > 10 / 10 / 01 , cleaned up
* * and adapted to mysqldump 05 / 11 / 01 by Jani Tolonen
2002-06-11 11:37:48 +02:00
* * Added - - single - transaction option 06 / 06 / 2002 by Peter Zaitsev
2003-06-10 08:34:00 +02:00
* * 10 Jun 2003 : SET NAMES and - - no - set - names by Alexander Barkov
2001-11-05 22:48:03 +01:00
*/
2000-07-31 21:29:14 +02:00
2004-11-10 17:56:45 +01:00
# define DUMP_VERSION "10.9"
2000-07-31 21:29:14 +02:00
2001-09-14 01:54:33 +02:00
# include <my_global.h>
2000-07-31 21:29:14 +02:00
# include <my_sys.h>
# include <m_string.h>
# include <m_ctype.h>
2004-12-27 19:10:30 +01:00
# include <hash.h>
2000-07-31 21:29:14 +02:00
2001-10-31 02:22:31 +01:00
# include "client_priv.h"
2000-07-31 21:29:14 +02:00
# include "mysql.h"
# include "mysql_version.h"
# include "mysqld_error.h"
/* Exit codes */
# define EX_USAGE 1
# define EX_MYSQLERR 2
# define EX_CONSCHECK 3
# define EX_EOM 4
2004-06-24 22:14:12 +02:00
# define EX_EOF 5 /* ferror for output file was got */
2000-07-31 21:29:14 +02:00
/* index into 'show fields from table' */
# define SHOW_FIELDNAME 0
# define SHOW_TYPE 1
# define SHOW_NULL 2
# define SHOW_DEFAULT 4
# define SHOW_EXTRA 5
2002-04-27 11:09:59 +02:00
/* Size of buffer for dump's select query */
# define QUERY_LENGTH 1536
2000-07-31 21:29:14 +02:00
static char * add_load_option ( char * ptr , const char * object ,
const char * statement ) ;
2003-01-16 04:35:59 +01:00
static ulong find_set ( TYPELIB * lib , const char * x , uint length ,
char * * err_pos , uint * err_len ) ;
2000-07-31 21:29:14 +02:00
static char * field_escape ( char * to , const char * from , uint length ) ;
2003-01-16 04:35:59 +01:00
static my_bool verbose = 0 , tFlag = 0 , cFlag = 0 , dFlag = 0 , quick = 1 , extended_insert = 1 ,
2004-11-16 23:45:24 +01:00
lock_tables = 1 , ignore_errors = 0 , flush_logs = 0 ,
opt_drop = 1 , opt_keywords = 0 , opt_lock = 1 , opt_compress = 0 ,
2003-01-16 04:35:59 +01:00
opt_delayed = 0 , create_options = 1 , opt_quoted = 0 , opt_databases = 0 ,
2004-11-10 17:56:45 +01:00
opt_alldbs = 0 , opt_create_db = 0 , opt_lock_all_tables = 0 , opt_set_charset ,
opt_autocommit = 0 , opt_disable_keys = 1 , opt_xml = 0 ,
2004-03-17 09:30:40 +01:00
opt_delete_master_logs = 0 , tty_password = 0 ,
2004-10-19 07:40:59 +02:00
opt_single_transaction = 0 , opt_comments = 0 , opt_compact = 0 ,
2004-11-16 23:45:24 +01:00
opt_hex_blob = 0 , opt_order_by_primary = 0 ;
2004-05-26 18:40:27 +02:00
static ulong opt_max_allowed_packet , opt_net_buffer_length ;
2004-03-17 09:30:40 +01:00
static MYSQL mysql_connection , * sock = 0 ;
2000-11-13 22:55:10 +01:00
static char insert_pat [ 12 * 1024 ] , * opt_password = 0 , * current_user = 0 ,
2000-07-31 21:29:14 +02:00
* current_host = 0 , * path = 0 , * fields_terminated = 0 ,
* lines_terminated = 0 , * enclosed = 0 , * opt_enclosed = 0 , * escaped = 0 ,
2004-11-16 23:45:24 +01:00
* where = 0 , * order_by = 0 ,
2003-03-16 08:20:45 +01:00
* opt_compatible_mode_str = 0 ,
2003-01-16 04:35:59 +01:00
* err_ptr = 0 ;
2004-03-11 15:46:27 +01:00
static char compatible_mode_normal_str [ 255 ] ;
2003-01-16 04:35:59 +01:00
static ulong opt_compatible_mode = 0 ;
2004-11-10 17:56:45 +01:00
# define MYSQL_OPT_MASTER_DATA_EFFECTIVE_SQL 1
# define MYSQL_OPT_MASTER_DATA_COMMENTED_SQL 2
static uint opt_mysql_port = 0 , err_len = 0 , opt_master_data ;
2000-07-31 21:29:14 +02:00
static my_string opt_mysql_unix_port = 0 ;
static int first_error = 0 ;
static DYNAMIC_STRING extended_row ;
2002-09-17 22:46:53 +02:00
# include <sslopt-vars.h>
2001-08-22 00:45:07 +02:00
FILE * md_result_file ;
2002-11-14 20:16:30 +01:00
# ifdef HAVE_SMEM
static char * shared_memory_base_name = 0 ;
# endif
2003-01-16 04:35:59 +01:00
static uint opt_protocol = 0 ;
2005-01-06 18:13:58 +01:00
/*
2005-01-06 19:47:08 +01:00
Constant for detection of default value of default_charset .
If default_charset is equal to mysql_universal_client_charset , then
it is the default value which assigned at the very beginning of main ( ) .
2005-01-06 18:13:58 +01:00
*/
2005-01-06 19:47:08 +01:00
static const char * mysql_universal_client_charset =
2005-01-06 18:13:58 +01:00
MYSQL_UNIVERSAL_CLIENT_CHARSET ;
static char * default_charset ;
2003-03-16 08:20:45 +01:00
static CHARSET_INFO * charset_info = & my_charset_latin1 ;
2004-04-27 16:32:40 +02:00
const char * default_dbug_option = " d:t:o,/tmp/mysqldump.trace " ;
2003-01-16 04:35:59 +01:00
const char * compatible_mode_names [ ] =
{
" MYSQL323 " , " MYSQL40 " , " POSTGRESQL " , " ORACLE " , " MSSQL " , " DB2 " ,
2004-01-15 03:26:57 +01:00
" MAXDB " , " NO_KEY_OPTIONS " , " NO_TABLE_OPTIONS " , " NO_FIELD_OPTIONS " ,
" ANSI " ,
2003-01-16 04:35:59 +01:00
NullS
} ;
2004-03-11 15:46:27 +01:00
# define MASK_ANSI_QUOTES \
( \
( 1 < < 2 ) | /* POSTGRESQL */ \
( 1 < < 3 ) | /* ORACLE */ \
( 1 < < 4 ) | /* MSSQL */ \
( 1 < < 5 ) | /* DB2 */ \
( 1 < < 6 ) | /* MAXDB */ \
( 1 < < 10 ) /* ANSI */ \
)
2003-01-16 04:35:59 +01:00
TYPELIB compatible_mode_typelib = { array_elements ( compatible_mode_names ) - 1 ,
2004-11-05 05:54:52 +01:00
" " , compatible_mode_names , NULL } ;
2003-01-16 04:35:59 +01:00
2004-12-27 19:10:30 +01:00
# define TABLE_RULE_HASH_SIZE 16
typedef struct st_table_rule_ent
{
char * key ; /* dbname.tablename */
uint key_len ;
} TABLE_RULE_ENT ;
HASH ignore_table ;
2000-07-31 21:29:14 +02:00
2002-04-02 19:29:53 +02:00
static struct my_option my_long_options [ ] =
2000-07-31 21:29:14 +02:00
{
2004-06-26 23:00:23 +02:00
{ " all " , ' a ' , " Deprecated. Use --create-options instead. " ,
( gptr * ) & create_options , ( gptr * ) & create_options , 0 , GET_BOOL , NO_ARG , 1 ,
0 , 0 , 0 , 0 , 0 } ,
2002-04-02 19:29:53 +02:00
{ " all-databases " , ' A ' ,
" Dump all the databases. This will be same as --databases with all databases selected. " ,
( gptr * ) & opt_alldbs , ( gptr * ) & opt_alldbs , 0 , GET_BOOL , NO_ARG , 0 , 0 , 0 , 0 ,
0 , 0 } ,
{ " add-drop-table " , OPT_DROP , " Add a 'drop table' before each create. " ,
2003-01-16 04:35:59 +01:00
( gptr * ) & opt_drop , ( gptr * ) & opt_drop , 0 , GET_BOOL , NO_ARG , 1 , 0 , 0 , 0 , 0 ,
2002-04-02 19:29:53 +02:00
0 } ,
{ " add-locks " , OPT_LOCKS , " Add locks around insert statements. " ,
2003-01-16 04:35:59 +01:00
( gptr * ) & opt_lock , ( gptr * ) & opt_lock , 0 , GET_BOOL , NO_ARG , 1 , 0 , 0 , 0 , 0 ,
2002-04-02 19:29:53 +02:00
0 } ,
{ " allow-keywords " , OPT_KEYWORDS ,
" Allow creation of column names that are keywords. " , ( gptr * ) & opt_keywords ,
( gptr * ) & opt_keywords , 0 , GET_BOOL , NO_ARG , 0 , 0 , 0 , 0 , 0 , 0 } ,
{ " character-sets-dir " , OPT_CHARSETS_DIR ,
2003-06-13 10:59:02 +02:00
" Directory where character sets are. " , ( gptr * ) & charsets_dir ,
2002-04-02 19:29:53 +02:00
( gptr * ) & charsets_dir , 0 , GET_STR , REQUIRED_ARG , 0 , 0 , 0 , 0 , 0 , 0 } ,
2004-11-11 21:55:55 +01:00
{ " comments " , ' i ' , " Write additional information. " ,
( gptr * ) & opt_comments , ( gptr * ) & opt_comments , 0 , GET_BOOL , NO_ARG ,
1 , 0 , 0 , 0 , 0 , 0 } ,
2003-01-16 04:35:59 +01:00
{ " compatible " , OPT_COMPATIBLE ,
2004-04-27 14:33:40 +02:00
" Change the dump to be compatible with a given mode. By default tables are dumped in a format optimized for MySQL. Legal modes are: ansi, mysql323, mysql40, postgresql, oracle, mssql, db2, maxdb, no_key_options, no_table_options, no_field_options. One can use several modes separated by commas. Note: Requires MySQL server version 4.1.0 or higher. This option is ignored with earlier server versions. " ,
2003-01-16 04:35:59 +01:00
( gptr * ) & opt_compatible_mode_str , ( gptr * ) & opt_compatible_mode_str , 0 ,
GET_STR , REQUIRED_ARG , 0 , 0 , 0 , 0 , 0 , 0 } ,
2004-02-09 12:31:03 +01:00
{ " compact " , OPT_COMPACT ,
2004-12-04 10:06:38 +01:00
" Give less verbose output (useful for debugging). Disables structure comments and header/footer constructs. Enables options --skip-add-drop-table --no-set-names --skip-disable-keys --skip-add-locks " ,
2004-02-09 12:31:03 +01:00
( gptr * ) & opt_compact , ( gptr * ) & opt_compact , 0 , GET_BOOL , NO_ARG , 0 , 0 , 0 , 0 ,
0 , 0 } ,
2002-05-14 20:41:55 +02:00
{ " complete-insert " , ' c ' , " Use complete insert statements. " , ( gptr * ) & cFlag ,
( gptr * ) & cFlag , 0 , GET_BOOL , NO_ARG , 0 , 0 , 0 , 0 , 0 , 0 } ,
2002-04-02 19:29:53 +02:00
{ " compress " , ' C ' , " Use compression in server/client protocol. " ,
( gptr * ) & opt_compress , ( gptr * ) & opt_compress , 0 , GET_BOOL , NO_ARG , 0 , 0 , 0 ,
0 , 0 , 0 } ,
2004-04-27 14:33:40 +02:00
{ " create-options " , OPT_CREATE_OPTIONS ,
" Include all MySQL specific create options. " ,
( gptr * ) & create_options , ( gptr * ) & create_options , 0 , GET_BOOL , NO_ARG , 1 ,
0 , 0 , 0 , 0 , 0 } ,
2002-04-02 19:29:53 +02:00
{ " databases " , ' B ' ,
" To dump several databases. Note the difference in usage; In this case no tables are given. All name arguments are regarded as databasenames. 'USE db_name;' will be included in the output. " ,
( gptr * ) & opt_databases , ( gptr * ) & opt_databases , 0 , GET_BOOL , NO_ARG , 0 , 0 ,
0 , 0 , 0 , 0 } ,
2004-04-27 16:32:40 +02:00
# ifdef DBUG_OFF
{ " debug " , ' # ' , " This is a non-debug version. Catch this and exit " ,
0 , 0 , 0 , GET_DISABLED , OPT_ARG , 0 , 0 , 0 , 0 , 0 , 0 } ,
# else
{ " debug " , ' # ' , " Output debug log " , ( gptr * ) & default_dbug_option ,
( gptr * ) & default_dbug_option , 0 , GET_STR , OPT_ARG , 0 , 0 , 0 , 0 , 0 , 0 } ,
# endif
2002-04-02 19:29:53 +02:00
{ " default-character-set " , OPT_DEFAULT_CHARSET ,
" Set the default character set. " , ( gptr * ) & default_charset ,
( gptr * ) & default_charset , 0 , GET_STR , REQUIRED_ARG , 0 , 0 , 0 , 0 , 0 , 0 } ,
{ " delayed-insert " , OPT_DELAYED , " Insert rows with INSERT DELAYED. " ,
( gptr * ) & opt_delayed , ( gptr * ) & opt_delayed , 0 , GET_BOOL , NO_ARG , 0 , 0 , 0 , 0 ,
0 , 0 } ,
2003-04-03 11:33:13 +02:00
{ " delete-master-logs " , OPT_DELETE_MASTER_LOGS ,
2004-11-10 17:56:45 +01:00
" Delete logs on master after backup. This automatically enables --master-data. " ,
( gptr * ) & opt_delete_master_logs , ( gptr * ) & opt_delete_master_logs , 0 ,
GET_BOOL , NO_ARG , 0 , 0 , 0 , 0 , 0 , 0 } ,
2002-04-02 19:29:53 +02:00
{ " disable-keys " , ' K ' ,
" '/*!40000 ALTER TABLE tb_name DISABLE KEYS */; and '/*!40000 ALTER TABLE tb_name ENABLE KEYS */; will be put in the output. " , ( gptr * ) & opt_disable_keys ,
2003-01-16 04:35:59 +01:00
( gptr * ) & opt_disable_keys , 0 , GET_BOOL , NO_ARG , 1 , 0 , 0 , 0 , 0 , 0 } ,
2002-04-02 19:29:53 +02:00
{ " extended-insert " , ' e ' ,
" Allows utilization of the new, much faster INSERT syntax. " ,
( gptr * ) & extended_insert , ( gptr * ) & extended_insert , 0 , GET_BOOL , NO_ARG ,
2003-01-16 04:35:59 +01:00
1 , 0 , 0 , 0 , 0 , 0 } ,
2002-04-02 19:29:53 +02:00
{ " fields-terminated-by " , OPT_FTB ,
" Fields in the textfile are terminated by ... " , ( gptr * ) & fields_terminated ,
( gptr * ) & fields_terminated , 0 , GET_STR , REQUIRED_ARG , 0 , 0 , 0 , 0 , 0 , 0 } ,
{ " fields-enclosed-by " , OPT_ENC ,
" Fields in the importfile are enclosed by ... " , ( gptr * ) & enclosed ,
( gptr * ) & enclosed , 0 , GET_STR , REQUIRED_ARG , 0 , 0 , 0 , 0 , 0 , 0 } ,
{ " fields-optionally-enclosed-by " , OPT_O_ENC ,
" Fields in the i.file are opt. enclosed by ... " , ( gptr * ) & opt_enclosed ,
( gptr * ) & opt_enclosed , 0 , GET_STR , REQUIRED_ARG , 0 , 0 , 0 , 0 , 0 , 0 } ,
{ " fields-escaped-by " , OPT_ESC , " Fields in the i.file are escaped by ... " ,
2004-02-21 14:32:22 +01:00
( gptr * ) & escaped , ( gptr * ) & escaped , 0 , GET_STR , REQUIRED_ARG , 0 , 0 , 0 , 0 , 0 , 0 } ,
2004-11-10 17:56:45 +01:00
{ " first-slave " , ' x ' , " Deprecated, renamed to --lock-all-tables. " ,
( gptr * ) & opt_lock_all_tables , ( gptr * ) & opt_lock_all_tables , 0 , GET_BOOL , NO_ARG ,
2002-04-02 19:29:53 +02:00
0 , 0 , 0 , 0 , 0 , 0 } ,
2004-02-04 16:50:33 +01:00
{ " flush-logs " , ' F ' , " Flush logs file in server before starting dump. "
2004-11-10 17:56:45 +01:00
" Note that if you dump many databases at once (using the option "
" --databases= or --all-databases), the logs will be flushed for "
" each database dumped. The exception is when using --lock-all-tables "
" or --master-data: "
" in this case the logs will be flushed only once, corresponding "
" to the moment all tables are locked. So if you want your dump and "
" the log flush to happen at the same exact moment you should use "
" --lock-all-tables or --master-data with --flush-logs " ,
2002-04-02 19:29:53 +02:00
( gptr * ) & flush_logs , ( gptr * ) & flush_logs , 0 , GET_BOOL , NO_ARG , 0 , 0 , 0 , 0 ,
0 , 0 } ,
{ " force " , ' f ' , " Continue even if we get an sql-error. " ,
( gptr * ) & ignore_errors , ( gptr * ) & ignore_errors , 0 , GET_BOOL , NO_ARG ,
0 , 0 , 0 , 0 , 0 , 0 } ,
{ " help " , ' ? ' , " Display this help message and exit. " , 0 , 0 , 0 , GET_NO_ARG ,
NO_ARG , 0 , 0 , 0 , 0 , 0 , 0 } ,
2004-11-11 21:55:55 +01:00
{ " hex-blob " , OPT_HEXBLOB , " Dump binary strings (BINARY, "
" VARBINARY, BLOB) in hexadecimal format. " ,
( gptr * ) & opt_hex_blob , ( gptr * ) & opt_hex_blob , 0 , GET_BOOL , NO_ARG , 0 , 0 , 0 , 0 , 0 , 0 } ,
2002-04-02 19:29:53 +02:00
{ " host " , ' h ' , " Connect to host. " , ( gptr * ) & current_host ,
2002-05-15 19:24:00 +02:00
( gptr * ) & current_host , 0 , GET_STR_ALLOC , REQUIRED_ARG , 0 , 0 , 0 , 0 , 0 , 0 } ,
2004-12-27 19:10:30 +01:00
{ " ignore-table " , OPT_IGNORE_TABLE ,
" Do not dump the specified table. To specify more than one table to ignore, "
" use the directive multiple times, once for each table. Each table must "
" be specified with both database and table names, e.g. --ignore-table=database.table " ,
0 , 0 , 0 , GET_STR , REQUIRED_ARG , 0 , 0 , 0 , 0 , 0 , 0 } ,
2002-04-02 19:29:53 +02:00
{ " lines-terminated-by " , OPT_LTB , " Lines in the i.file are terminated by ... " ,
( gptr * ) & lines_terminated , ( gptr * ) & lines_terminated , 0 , GET_STR ,
REQUIRED_ARG , 0 , 0 , 0 , 0 , 0 , 0 } ,
2004-11-10 17:56:45 +01:00
{ " lock-all-tables " , ' x ' , " Locks all tables across all databases. This "
" is achieved by taking a global read lock for the duration of the whole "
" dump. Automatically turns --single-transaction and --lock-tables off. " ,
( gptr * ) & opt_lock_all_tables , ( gptr * ) & opt_lock_all_tables , 0 , GET_BOOL , NO_ARG ,
0 , 0 , 0 , 0 , 0 , 0 } ,
2002-04-02 19:29:53 +02:00
{ " lock-tables " , ' l ' , " Lock all tables for read. " , ( gptr * ) & lock_tables ,
2003-01-16 04:35:59 +01:00
( gptr * ) & lock_tables , 0 , GET_BOOL , NO_ARG , 1 , 0 , 0 , 0 , 0 , 0 } ,
2002-04-02 19:29:53 +02:00
{ " master-data " , OPT_MASTER_DATA ,
2004-11-10 17:56:45 +01:00
" This causes the binary log position and filename to be appended to the "
" output. If equal to 1, will print it as a CHANGE MASTER command; if equal "
" to 2, that command will be prefixed with a comment symbol. "
" This option will turn --lock-all-tables on, unless "
" --single-transaction is specified too (in which case a "
" global read lock is only taken a short time at the beginning of the dump "
" - don't forget to read about --single-transaction below). In all cases "
" any action on logs will happen at the exact moment of the dump. "
" Option automatically turns --lock-tables off. " ,
( gptr * ) & opt_master_data , ( gptr * ) & opt_master_data , 0 ,
2004-11-14 17:18:31 +01:00
GET_UINT , OPT_ARG , 0 , 0 , MYSQL_OPT_MASTER_DATA_COMMENTED_SQL , 0 , 0 , 0 } ,
2004-11-12 16:44:17 +01:00
{ " max_allowed_packet " , OPT_MAX_ALLOWED_PACKET , " " ,
( gptr * ) & opt_max_allowed_packet , ( gptr * ) & opt_max_allowed_packet , 0 ,
GET_ULONG , REQUIRED_ARG , 24 * 1024 * 1024 , 4096 ,
( longlong ) 2L * 1024L * 1024L * 1024L , MALLOC_OVERHEAD , 1024 , 0 } ,
{ " net_buffer_length " , OPT_NET_BUFFER_LENGTH , " " ,
( gptr * ) & opt_net_buffer_length , ( gptr * ) & opt_net_buffer_length , 0 ,
GET_ULONG , REQUIRED_ARG , 1024 * 1024L - 1025 , 4096 , 16 * 1024L * 1024L ,
MALLOC_OVERHEAD - 1024 , 1024 , 0 } ,
2002-04-02 19:29:53 +02:00
{ " no-autocommit " , OPT_AUTOCOMMIT ,
" Wrap tables with autocommit/commit statements. " ,
( gptr * ) & opt_autocommit , ( gptr * ) & opt_autocommit , 0 , GET_BOOL , NO_ARG ,
0 , 0 , 0 , 0 , 0 , 0 } ,
{ " no-create-db " , ' n ' ,
2003-06-13 10:59:02 +02:00
" 'CREATE DATABASE /*!32312 IF NOT EXISTS*/ db_name;' will not be put in the output. The above line will be added otherwise, if --databases or --all-databases option was given.}. " ,
2002-04-02 19:29:53 +02:00
( gptr * ) & opt_create_db , ( gptr * ) & opt_create_db , 0 , GET_BOOL , NO_ARG , 0 , 0 ,
0 , 0 , 0 , 0 } ,
{ " no-create-info " , ' t ' , " Don't write table creation info. " ,
2002-05-14 20:41:55 +02:00
( gptr * ) & tFlag , ( gptr * ) & tFlag , 0 , GET_BOOL , NO_ARG , 0 , 0 , 0 , 0 , 0 , 0 } ,
{ " no-data " , ' d ' , " No row information. " , ( gptr * ) & dFlag , ( gptr * ) & dFlag , 0 ,
GET_BOOL , NO_ARG , 0 , 0 , 0 , 0 , 0 , 0 } ,
2003-05-30 12:21:03 +02:00
{ " no-set-names " , ' N ' ,
2004-06-26 23:00:23 +02:00
" Deprecated. Use --skip-set-charset instead. " ,
2004-03-31 04:36:29 +02:00
0 , 0 , 0 , GET_NO_ARG , NO_ARG , 0 , 0 , 0 , 0 , 0 , 0 } ,
2002-04-02 19:29:53 +02:00
{ " opt " , OPT_OPTIMIZE ,
2004-05-01 11:07:42 +02:00
" Same as --add-drop-table, --add-locks, --create-options, --quick, --extended-insert, --lock-tables, --set-charset, and --disable-keys. Enabled by default, disable with --skip-opt. " ,
2002-04-02 19:29:53 +02:00
0 , 0 , 0 , GET_NO_ARG , NO_ARG , 0 , 0 , 0 , 0 , 0 , 0 } ,
2005-01-17 07:09:47 +01:00
{ " order-by-primary " , OPT_ORDER_BY_PRIMARY ,
" Sorts each table's rows by primary key, or first unique key, if such a key exists. Useful when dumping a MyISAM table to be loaded into an InnoDB table, but will make the dump itself take considerably longer. " ,
( gptr * ) & opt_order_by_primary , ( gptr * ) & opt_order_by_primary , 0 , GET_BOOL , NO_ARG , 0 , 0 , 0 , 0 , 0 , 0 } ,
2002-04-02 19:29:53 +02:00
{ " password " , ' p ' ,
" Password to use when connecting to server. If password is not given it's solicited on the tty. " ,
0 , 0 , 0 , GET_STR , OPT_ARG , 0 , 0 , 0 , 0 , 0 , 0 } ,
2000-07-31 21:29:14 +02:00
# ifdef __WIN__
2003-06-13 10:59:02 +02:00
{ " pipe " , ' W ' , " Use named pipes to connect to server. " , 0 , 0 , 0 , GET_NO_ARG ,
2002-04-02 19:29:53 +02:00
NO_ARG , 0 , 0 , 0 , 0 , 0 , 0 } ,
2000-07-31 21:29:14 +02:00
# endif
2002-05-11 13:36:34 +02:00
{ " port " , ' P ' , " Port number to use for connection. " , ( gptr * ) & opt_mysql_port ,
( gptr * ) & opt_mysql_port , 0 , GET_UINT , REQUIRED_ARG , MYSQL_PORT , 0 , 0 , 0 , 0 ,
0 } ,
2003-06-13 10:59:02 +02:00
{ " protocol " , OPT_MYSQL_PROTOCOL , " The protocol of connection (tcp,socket,pipe,memory). " ,
2002-11-14 20:16:30 +01:00
0 , 0 , 0 , GET_STR , REQUIRED_ARG , 0 , 0 , 0 , 0 , 0 , 0 } ,
2002-04-02 19:29:53 +02:00
{ " quick " , ' q ' , " Don't buffer query, dump directly to stdout. " ,
2003-01-16 04:35:59 +01:00
( gptr * ) & quick , ( gptr * ) & quick , 0 , GET_BOOL , NO_ARG , 1 , 0 , 0 , 0 , 0 , 0 } ,
2003-06-13 10:59:02 +02:00
{ " quote-names " , ' Q ' , " Quote table and column names with backticks (`). " ,
2004-01-15 03:26:57 +01:00
( gptr * ) & opt_quoted , ( gptr * ) & opt_quoted , 0 , GET_BOOL , NO_ARG , 1 , 0 , 0 , 0 ,
2002-04-02 19:29:53 +02:00
0 , 0 } ,
{ " result-file " , ' r ' ,
2002-08-05 09:00:38 +02:00
" Direct output to a given file. This option should be used in MSDOS, because it prevents new line ' \\ n' from being converted to ' \\ r \\ n' (carriage return + line feed). " ,
2002-04-30 11:32:45 +02:00
0 , 0 , 0 , GET_STR , REQUIRED_ARG , 0 , 0 , 0 , 0 , 0 , 0 } ,
2004-11-12 10:17:53 +01:00
{ " set-charset " , OPT_SET_CHARSET ,
" Add 'SET NAMES default_character_set' to the output. Enabled by default; suppress with --skip-set-charset. " ,
( gptr * ) & opt_set_charset , ( gptr * ) & opt_set_charset , 0 , GET_BOOL , NO_ARG , 1 ,
0 , 0 , 0 , 0 , 0 } ,
2004-11-09 18:03:02 +01:00
{ " set-variable " , ' O ' ,
" Change the value of a variable. Please note that this option is deprecated; you can set variables directly with --variable-name=value. " ,
0 , 0 , 0 , GET_STR , REQUIRED_ARG , 0 , 0 , 0 , 0 , 0 , 0 } ,
2002-11-14 20:16:30 +01:00
# ifdef HAVE_SMEM
2004-08-30 18:11:10 +02:00
{ " shared-memory-base-name " , OPT_SHARED_MEMORY_BASE_NAME ,
2003-06-13 10:59:02 +02:00
" Base name of shared memory. " , ( gptr * ) & shared_memory_base_name , ( gptr * ) & shared_memory_base_name ,
2002-11-14 20:16:30 +01:00
0 , GET_STR_ALLOC , REQUIRED_ARG , 0 , 0 , 0 , 0 , 0 , 0 } ,
# endif
2004-11-12 10:17:53 +01:00
/*
Note that the combination - - single - transaction - - master - data
will give bullet - proof binlog position only if server > = 4.1 .3 . That ' s the
old " FLUSH TABLES WITH READ LOCK does not block commit " fixed bug .
*/
2004-11-09 18:03:02 +01:00
{ " single-transaction " , OPT_TRANSACTION ,
2004-11-12 10:17:53 +01:00
" Creates a consistent snapshot by dumping all tables in a single "
" transaction. Works ONLY for tables stored in storage engines which "
" support multiversioning (currently only InnoDB does); the dump is NOT "
" guaranteed to be consistent for other storage engines. Option "
" automatically turns off --lock-tables. " ,
( gptr * ) & opt_single_transaction , ( gptr * ) & opt_single_transaction , 0 ,
GET_BOOL , NO_ARG , 0 , 0 , 0 , 0 , 0 , 0 } ,
2003-01-16 04:35:59 +01:00
{ " skip-opt " , OPT_SKIP_OPTIMIZATION ,
2004-05-01 11:07:42 +02:00
" Disable --opt. Disables --add-drop-table, --add-locks, --create-options, --quick, --extended-insert, --lock-tables, --set-charset, and --disable-keys. " ,
2003-01-16 04:35:59 +01:00
0 , 0 , 0 , GET_NO_ARG , NO_ARG , 0 , 0 , 0 , 0 , 0 , 0 } ,
2002-04-02 19:29:53 +02:00
{ " socket " , ' S ' , " Socket file to use for connection. " ,
( gptr * ) & opt_mysql_unix_port , ( gptr * ) & opt_mysql_unix_port , 0 , GET_STR ,
REQUIRED_ARG , 0 , 0 , 0 , 0 , 0 , 0 } ,
2002-09-17 22:46:53 +02:00
# include <sslopt-longopts.h>
2002-04-02 19:29:53 +02:00
{ " tab " , ' T ' ,
" Creates tab separated textfile for each table to given path. (creates .sql and .txt files). NOTE: This only works if mysqldump is run on the same machine as the mysqld daemon. " ,
( gptr * ) & path , ( gptr * ) & path , 0 , GET_STR , REQUIRED_ARG , 0 , 0 , 0 , 0 , 0 , 0 } ,
{ " tables " , OPT_TABLES , " Overrides option --databases (-B). " ,
0 , 0 , 0 , GET_NO_ARG , NO_ARG , 0 , 0 , 0 , 0 , 0 , 0 } ,
2000-07-31 21:29:14 +02:00
# ifndef DONT_ALLOW_USER_CHANGE
2002-04-02 19:29:53 +02:00
{ " user " , ' u ' , " User for login if not current user. " ,
( gptr * ) & current_user , ( gptr * ) & current_user , 0 , GET_STR , REQUIRED_ARG ,
0 , 0 , 0 , 0 , 0 , 0 } ,
2000-07-31 21:29:14 +02:00
# endif
2002-04-02 19:29:53 +02:00
{ " verbose " , ' v ' , " Print info about the various stages. " ,
( gptr * ) & verbose , ( gptr * ) & verbose , 0 , GET_BOOL , NO_ARG , 0 , 0 , 0 , 0 , 0 , 0 } ,
{ " version " , ' V ' , " Output version information and exit. " , 0 , 0 , 0 ,
GET_NO_ARG , NO_ARG , 0 , 0 , 0 , 0 , 0 , 0 } ,
{ " where " , ' w ' , " Dump only selected records; QUOTES mandatory! " ,
( gptr * ) & where , ( gptr * ) & where , 0 , GET_STR , REQUIRED_ARG , 0 , 0 , 0 , 0 , 0 , 0 } ,
{ " xml " , ' X ' , " Dump a database as well formed XML. " , 0 , 0 , 0 , GET_NO_ARG ,
NO_ARG , 0 , 0 , 0 , 0 , 0 , 0 } ,
2004-11-12 16:44:17 +01:00
{ 0 , 0 , 0 , 0 , 0 , 0 , GET_NO_ARG , NO_ARG , 0 , 0 , 0 , 0 , 0 , 0 }
2000-07-31 21:29:14 +02:00
} ;
static const char * load_default_groups [ ] = { " mysqldump " , " client " , 0 } ;
static void safe_exit ( int error ) ;
2002-01-02 20:29:41 +01:00
static void write_header ( FILE * sql_file , char * db_name ) ;
2000-07-31 21:29:14 +02:00
static void print_value ( FILE * file , MYSQL_RES * result , MYSQL_ROW row ,
const char * prefix , const char * name ,
int string_value ) ;
static int dump_selected_tables ( char * db , char * * table_names , int tables ) ;
static int dump_all_tables_in_db ( char * db ) ;
static int init_dumping ( char * ) ;
static int dump_databases ( char * * ) ;
static int dump_all_databases ( ) ;
2003-09-03 17:48:10 +02:00
static char * quote_name ( const char * name , char * buff , my_bool force ) ;
2003-12-14 05:39:52 +01:00
static const char * check_if_ignore_table ( const char * table_name ) ;
2004-11-16 23:45:24 +01:00
static char * primary_key_fields ( const char * table_name ) ;
2003-12-14 05:39:52 +01:00
2004-05-25 21:00:14 +02:00
# include <help_start.h>
2000-07-31 21:29:14 +02:00
2004-06-24 22:14:12 +02:00
/*
exit with message if ferror ( file )
SYNOPSIS
check_io ( )
file - checked file
*/
void check_io ( FILE * file )
{
if ( ferror ( file ) )
{
fprintf ( stderr , " %s: Got errno %d on write \n " , my_progname , errno ) ;
safe_exit ( EX_EOF ) ;
}
}
2000-07-31 21:29:14 +02:00
static void print_version ( void )
{
printf ( " %s Ver %s Distrib %s, for %s (%s) \n " , my_progname , DUMP_VERSION ,
2004-05-25 21:00:14 +02:00
MYSQL_SERVER_VERSION , SYSTEM_TYPE , MACHINE_TYPE ) ;
NETWARE_SET_SCREEN_MODE ( 1 ) ;
2000-07-31 21:29:14 +02:00
} /* print_version */
2002-09-24 16:11:59 +02:00
static void short_usage_sub ( void )
{
printf ( " Usage: %s [OPTIONS] database [tables] \n " , my_progname ) ;
printf ( " OR %s [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...] \n " ,
my_progname ) ;
printf ( " OR %s [OPTIONS] --all-databases [OPTIONS] \n " , my_progname ) ;
2004-05-25 21:00:14 +02:00
NETWARE_SET_SCREEN_MODE ( 1 ) ;
2002-09-24 16:11:59 +02:00
}
2004-05-25 21:00:14 +02:00
2000-07-31 21:29:14 +02:00
static void usage ( void )
{
print_version ( ) ;
puts ( " By Igor Romanenko, Monty, Jani & Sinisa " ) ;
puts ( " This software comes with ABSOLUTELY NO WARRANTY. This is free software, \n and you are welcome to modify and redistribute it under the GPL license \n " ) ;
puts ( " Dumping definition and data mysql database or table " ) ;
2002-09-24 16:11:59 +02:00
short_usage_sub ( ) ;
2000-07-31 21:29:14 +02:00
print_defaults ( " my " , load_default_groups ) ;
2002-04-02 19:29:53 +02:00
my_print_help ( my_long_options ) ;
my_print_variables ( my_long_options ) ;
2000-07-31 21:29:14 +02:00
} /* usage */
2002-09-24 16:11:59 +02:00
static void short_usage ( void )
{
short_usage_sub ( ) ;
printf ( " For more options, use %s --help \n " , my_progname ) ;
}
2004-05-25 21:00:14 +02:00
# include <help_end.h>
2002-09-24 16:11:59 +02:00
2002-01-02 20:29:41 +01:00
static void write_header ( FILE * sql_file , char * db_name )
2000-07-31 21:29:14 +02:00
{
2001-11-05 22:48:03 +01:00
if ( opt_xml )
2002-11-18 17:50:42 +01:00
{
2003-10-30 11:58:30 +01:00
fputs ( " <?xml version= \" 1.0 \" ?> \n " , sql_file ) ;
fputs ( " <mysqldump> \n " , sql_file ) ;
2004-06-24 22:14:12 +02:00
check_io ( sql_file ) ;
2002-11-18 17:50:42 +01:00
}
2004-02-09 12:31:03 +01:00
else if ( ! opt_compact )
2001-11-05 22:48:03 +01:00
{
2004-02-09 12:31:03 +01:00
if ( opt_comments )
{
fprintf ( sql_file , " -- MySQL dump %s \n -- \n " , DUMP_VERSION ) ;
fprintf ( sql_file , " -- Host: %s Database: %s \n " ,
current_host ? current_host : " localhost " , db_name ? db_name :
" " ) ;
fputs ( " -- ------------------------------------------------------ \n " ,
sql_file ) ;
fprintf ( sql_file , " -- Server version \t %s \n " ,
mysql_get_server_info ( & mysql_connection ) ) ;
}
2004-03-31 04:36:29 +02:00
if ( opt_set_charset )
2004-06-07 15:12:23 +02:00
fprintf ( sql_file ,
" \n /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; "
" \n /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; "
" \n /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; "
" \n /*!40101 SET NAMES %s */; \n " , default_charset ) ;
2004-02-13 22:21:46 +01:00
if ( ! path )
{
fprintf ( md_result_file , " \
2003-07-23 00:58:30 +02:00
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */ ; \ n \
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */ ; \ n \
" );
2004-02-13 22:21:46 +01:00
}
fprintf ( sql_file ,
2004-03-11 19:01:25 +01:00
" /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE= \" %s%s%s \" */; \n " ,
path ? " " : " NO_AUTO_VALUE_ON_ZERO " , compatible_mode_normal_str [ 0 ] = = 0 ? " " : " , " ,
compatible_mode_normal_str ) ;
2004-06-24 22:14:12 +02:00
check_io ( sql_file ) ;
2001-11-05 22:48:03 +01:00
}
2002-01-02 20:29:41 +01:00
} /* write_header */
2000-07-31 21:29:14 +02:00
2003-01-16 04:35:59 +01:00
2002-11-18 17:50:42 +01:00
static void write_footer ( FILE * sql_file )
{
if ( opt_xml )
2004-06-24 22:14:12 +02:00
{
2004-02-09 12:31:03 +01:00
fputs ( " </mysqldump> \n " , sql_file ) ;
2004-06-24 22:14:12 +02:00
check_io ( sql_file ) ;
}
2004-02-09 12:31:03 +01:00
else if ( ! opt_compact )
2003-07-23 00:58:30 +02:00
{
2004-02-13 22:21:46 +01:00
fprintf ( sql_file , " \n /*!40101 SET SQL_MODE=@OLD_SQL_MODE */; \n " ) ;
if ( ! path )
{
fprintf ( md_result_file , " \
2003-07-23 00:58:30 +02:00
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */ ; \ n \
2004-02-09 12:31:03 +01:00
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */ ; \ n " );
2004-02-13 22:21:46 +01:00
}
2004-03-31 04:36:29 +02:00
if ( opt_set_charset )
2004-02-13 22:21:46 +01:00
fprintf ( sql_file ,
2004-06-07 15:12:23 +02:00
" /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; \n "
" /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; \n "
" /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; \n " ) ;
2004-02-09 12:31:03 +01:00
fputs ( " \n " , sql_file ) ;
2004-06-24 22:14:12 +02:00
check_io ( sql_file ) ;
2003-07-23 00:58:30 +02:00
}
2002-11-18 17:50:42 +01:00
} /* write_footer */
2000-07-31 21:29:14 +02:00
2003-01-16 04:35:59 +01:00
2004-12-27 19:10:30 +01:00
static void free_table_ent ( TABLE_RULE_ENT * e )
{
my_free ( ( gptr ) e , MYF ( 0 ) ) ;
}
static byte * get_table_key ( TABLE_RULE_ENT * e , uint * len ,
my_bool not_used __attribute__ ( ( unused ) ) )
{
* len = e - > key_len ;
return ( byte * ) e - > key ;
}
2004-12-28 17:33:49 +01:00
void init_table_rule_hash ( HASH * h )
2004-12-27 19:10:30 +01:00
{
if ( hash_init ( h , charset_info , TABLE_RULE_HASH_SIZE , 0 , 0 ,
( hash_get_key ) get_table_key ,
( hash_free_key ) free_table_ent , 0 ) )
2004-12-28 17:33:49 +01:00
exit ( EX_EOM ) ;
2004-12-27 19:10:30 +01:00
}
2003-04-03 20:19:12 +02:00
2002-04-02 19:29:53 +02:00
static my_bool
get_one_option ( int optid , const struct my_option * opt __attribute__ ( ( unused ) ) ,
char * argument )
2000-07-31 21:29:14 +02:00
{
2003-04-03 11:33:13 +02:00
switch ( optid ) {
2002-04-02 19:29:53 +02:00
case ' p ' :
if ( argument )
{
char * start = argument ;
my_free ( opt_password , MYF ( MY_ALLOW_ZERO_PTR ) ) ;
opt_password = my_strdup ( argument , MYF ( MY_FAE ) ) ;
while ( * argument ) * argument + + = ' x ' ; /* Destroy argument */
if ( * start )
start [ 1 ] = 0 ; /* Cut length of argument */
2004-10-07 21:08:17 +02:00
tty_password = 0 ;
2002-04-02 19:29:53 +02:00
}
else
tty_password = 1 ;
break ;
case ' r ' :
2003-01-28 07:38:28 +01:00
if ( ! ( md_result_file = my_fopen ( argument , O_WRONLY | FILE_BINARY ,
2002-04-02 19:29:53 +02:00
MYF ( MY_WME ) ) ) )
exit ( 1 ) ;
break ;
case ' W ' :
2000-07-31 21:29:14 +02:00
# ifdef __WIN__
2002-11-14 20:16:30 +01:00
opt_protocol = MYSQL_PROTOCOL_PIPE ;
2000-07-31 21:29:14 +02:00
# endif
2002-04-02 19:29:53 +02:00
break ;
2004-03-31 04:36:29 +02:00
case ' N ' :
opt_set_charset = 0 ;
break ;
2002-04-02 19:29:53 +02:00
case ' T ' :
opt_disable_keys = 0 ;
break ;
case ' # ' :
2004-04-27 16:32:40 +02:00
DBUG_PUSH ( argument ? argument : default_dbug_option ) ;
2002-04-02 19:29:53 +02:00
break ;
2002-09-17 22:46:53 +02:00
# include <sslopt-case.h>
2002-04-02 19:29:53 +02:00
case ' V ' : print_version ( ) ; exit ( 0 ) ;
case ' X ' :
opt_xml = 1 ;
2003-10-14 12:19:54 +02:00
extended_insert = opt_drop = opt_lock =
opt_disable_keys = opt_autocommit = opt_create_db = 0 ;
2002-04-02 19:29:53 +02:00
break ;
case ' I ' :
case ' ? ' :
usage ( ) ;
exit ( 0 ) ;
2004-11-14 17:18:31 +01:00
case ( int ) OPT_MASTER_DATA :
if ( ! argument ) /* work like in old versions */
opt_master_data = MYSQL_OPT_MASTER_DATA_EFFECTIVE_SQL ;
break ;
2002-04-02 19:29:53 +02:00
case ( int ) OPT_OPTIMIZE :
2003-01-16 04:35:59 +01:00
extended_insert = opt_drop = opt_lock = quick = create_options =
2004-03-31 04:36:29 +02:00
opt_disable_keys = lock_tables = opt_set_charset = 1 ;
2002-04-02 19:29:53 +02:00
break ;
2003-01-16 04:35:59 +01:00
case ( int ) OPT_SKIP_OPTIMIZATION :
extended_insert = opt_drop = opt_lock = quick = create_options =
2004-04-27 14:33:40 +02:00
opt_disable_keys = lock_tables = opt_set_charset = 0 ;
2003-01-16 04:35:59 +01:00
break ;
2004-02-09 12:31:03 +01:00
case ( int ) OPT_COMPACT :
if ( opt_compact )
{
opt_comments = opt_drop = opt_disable_keys = opt_lock = 0 ;
2004-03-31 04:36:29 +02:00
opt_set_charset = 0 ;
2004-02-09 12:31:03 +01:00
}
2002-04-02 19:29:53 +02:00
case ( int ) OPT_TABLES :
opt_databases = 0 ;
break ;
2004-12-27 19:10:30 +01:00
case ( int ) OPT_IGNORE_TABLE :
{
uint len = ( uint ) strlen ( argument ) ;
2004-12-28 17:33:49 +01:00
TABLE_RULE_ENT * e ;
if ( ! strchr ( argument , ' . ' ) )
2004-12-27 19:10:30 +01:00
{
2004-12-28 17:33:49 +01:00
fprintf ( stderr , " Illegal use of option --ignore-table=<database>.<table> \n " ) ;
2004-12-27 19:10:30 +01:00
exit ( 1 ) ;
}
2004-12-28 17:33:49 +01:00
/* len is always > 0 because we know the there exists a '.' */
e = ( TABLE_RULE_ENT * ) my_malloc ( sizeof ( TABLE_RULE_ENT ) + len , MYF ( MY_WME ) ) ;
if ( ! e )
exit ( EX_EOM ) ;
2004-12-27 19:10:30 +01:00
e - > key = ( char * ) e + sizeof ( TABLE_RULE_ENT ) ;
e - > key_len = len ;
memcpy ( e - > key , argument , len ) ;
2004-12-28 17:33:49 +01:00
if ( ! hash_inited ( & ignore_table ) )
init_table_rule_hash ( & ignore_table ) ;
2004-12-27 19:10:30 +01:00
if ( my_hash_insert ( & ignore_table , ( byte * ) e ) )
2004-12-28 17:33:49 +01:00
exit ( EX_EOM ) ;
2004-12-27 19:10:30 +01:00
break ;
}
2003-01-16 04:35:59 +01:00
case ( int ) OPT_COMPATIBLE :
2004-12-28 17:33:49 +01:00
{
2003-01-16 04:35:59 +01:00
char buff [ 255 ] ;
2004-03-11 15:46:27 +01:00
char * end = compatible_mode_normal_str ;
int i ;
ulong mode ;
2003-01-16 04:35:59 +01:00
opt_quoted = 1 ;
2004-03-31 04:36:29 +02:00
opt_set_charset = 0 ;
2003-01-16 04:35:59 +01:00
opt_compatible_mode_str = argument ;
opt_compatible_mode = find_set ( & compatible_mode_typelib ,
argument , strlen ( argument ) ,
& err_ptr , & err_len ) ;
if ( err_len )
{
strmake ( buff , err_ptr , min ( sizeof ( buff ) , err_len ) ) ;
fprintf ( stderr , " Invalid mode to --compatible: %s \n " , buff ) ;
exit ( 1 ) ;
}
2004-03-11 15:46:27 +01:00
# if !defined(DBUG_OFF)
{
2004-03-17 09:30:40 +01:00
uint size_for_sql_mode = 0 ;
2004-03-11 15:46:27 +01:00
const char * * ptr ;
for ( ptr = compatible_mode_names ; * ptr ; ptr + + )
size_for_sql_mode + = strlen ( * ptr ) ;
size_for_sql_mode + = sizeof ( compatible_mode_names ) - 1 ;
DBUG_ASSERT ( sizeof ( compatible_mode_normal_str ) > = size_for_sql_mode ) ;
}
# endif
mode = opt_compatible_mode ;
for ( i = 0 , mode = opt_compatible_mode ; mode ; mode > > = 1 , i + + )
{
if ( mode & 1 )
{
end = strmov ( end , compatible_mode_names [ i ] ) ;
end = strmov ( end , " , " ) ;
}
}
if ( end ! = compatible_mode_normal_str )
end [ - 1 ] = 0 ;
2004-12-22 13:02:27 +01:00
/*
Set charset to the default compiled value if it hasn ' t
been reset yet by - - default - character - set = xxx .
*/
2005-01-06 18:13:58 +01:00
if ( default_charset = = mysql_universal_client_charset )
2004-12-22 13:02:27 +01:00
default_charset = ( char * ) MYSQL_DEFAULT_CHARSET_NAME ;
2003-01-16 04:35:59 +01:00
break ;
}
case ( int ) OPT_MYSQL_PROTOCOL :
2002-11-14 20:16:30 +01:00
{
2004-08-24 17:13:31 +02:00
if ( ( opt_protocol = find_type ( argument , & sql_protocol_typelib , 0 ) ) < = 0 )
2003-01-16 04:35:59 +01:00
{
fprintf ( stderr , " Unknown option to protocol: %s \n " , argument ) ;
exit ( 1 ) ;
}
break ;
2002-11-14 20:16:30 +01:00
}
2000-07-31 21:29:14 +02:00
}
2002-04-02 19:29:53 +02:00
return 0 ;
}
static int get_options ( int * argc , char * * * argv )
{
int ho_error ;
2004-05-26 18:40:27 +02:00
MYSQL_PARAMETERS * mysql_params = mysql_get_parameters ( ) ;
opt_max_allowed_packet = * mysql_params - > p_max_allowed_packet ;
opt_net_buffer_length = * mysql_params - > p_net_buffer_length ;
2002-04-02 19:29:53 +02:00
md_result_file = stdout ;
load_defaults ( " my " , load_default_groups , argc , argv ) ;
2004-08-31 18:27:58 +02:00
if ( ( ho_error = handle_options ( argc , argv , my_long_options , get_one_option ) ) )
2002-05-29 14:07:30 +02:00
exit ( ho_error ) ;
2002-04-02 19:29:53 +02:00
2004-05-26 18:40:27 +02:00
* mysql_params - > p_max_allowed_packet = opt_max_allowed_packet ;
* mysql_params - > p_net_buffer_length = opt_net_buffer_length ;
2000-07-31 21:29:14 +02:00
if ( opt_delayed )
opt_lock = 0 ; /* Can't have lock with delayed */
if ( ! path & & ( enclosed | | opt_enclosed | | escaped | | lines_terminated | |
fields_terminated ) )
{
2002-04-02 19:29:53 +02:00
fprintf ( stderr ,
" %s: You must use option --tab with --fields-... \n " , my_progname ) ;
2000-07-31 21:29:14 +02:00
return ( 1 ) ;
}
2004-11-10 17:56:45 +01:00
/* Ensure consistency of the set of binlog & locking options */
if ( opt_delete_master_logs & & ! opt_master_data )
opt_master_data = MYSQL_OPT_MASTER_DATA_COMMENTED_SQL ;
if ( opt_single_transaction & & opt_lock_all_tables )
{
fprintf ( stderr , " %s: You can't use --single-transaction and "
" --lock-all-tables at the same time. \n " , my_progname ) ;
return ( 1 ) ;
}
if ( opt_master_data )
opt_lock_all_tables = ! opt_single_transaction ;
if ( opt_single_transaction | | opt_lock_all_tables )
2003-01-16 04:35:59 +01:00
lock_tables = 0 ;
2000-07-31 21:29:14 +02:00
if ( enclosed & & opt_enclosed )
{
fprintf ( stderr , " %s: You can't use ..enclosed.. and ..optionally-enclosed.. at the same time. \n " , my_progname ) ;
return ( 1 ) ;
}
if ( ( opt_databases | | opt_alldbs ) & & path )
{
fprintf ( stderr ,
" %s: --databases or --all-databases can't be used with --tab. \n " ,
my_progname ) ;
return ( 1 ) ;
}
2004-02-11 12:15:39 +01:00
if ( strcmp ( default_charset , charset_info - > csname ) & &
! ( charset_info = get_charset_by_csname ( default_charset ,
2003-05-22 11:37:01 +02:00
MY_CS_PRIMARY , MYF ( MY_WME ) ) ) )
2003-03-16 08:20:45 +01:00
exit ( 1 ) ;
2000-07-31 21:29:14 +02:00
if ( ( * argc < 1 & & ! opt_alldbs ) | | ( * argc > 0 & & opt_alldbs ) )
{
2002-09-24 16:11:59 +02:00
short_usage ( ) ;
2000-07-31 21:29:14 +02:00
return 1 ;
}
if ( tty_password )
2000-11-13 22:55:10 +01:00
opt_password = get_tty_password ( NullS ) ;
2000-07-31 21:29:14 +02:00
return ( 0 ) ;
} /* get_options */
/*
* * DBerror - - prints mysql error message and exits the program .
*/
static void DBerror ( MYSQL * mysql , const char * when )
{
DBUG_ENTER ( " DBerror " ) ;
2000-08-30 19:40:59 +02:00
my_printf_error ( 0 , " Got error: %d: %s %s " , MYF ( 0 ) ,
2000-07-31 21:29:14 +02:00
mysql_errno ( mysql ) , mysql_error ( mysql ) , when ) ;
safe_exit ( EX_MYSQLERR ) ;
DBUG_VOID_RETURN ;
} /* DBerror */
2004-11-10 17:56:45 +01:00
/*
Sends a query to server , optionally reads result , prints error message if
some .
SYNOPSIS
mysql_query_with_error_report ( )
mysql_con connection to use
res if non zero , result will be put there with mysql_store_result
query query to send to server
RETURN VALUES
0 query sending and ( if res ! = 0 ) result reading went ok
1 error
*/
static int mysql_query_with_error_report ( MYSQL * mysql_con , MYSQL_RES * * res ,
const char * query )
{
if ( mysql_query ( mysql_con , query ) | |
( res & & ! ( ( * res ) = mysql_store_result ( mysql_con ) ) ) )
{
my_printf_error ( 0 , " %s: Couldn't execute '%s': %s (%d) " ,
MYF ( 0 ) , my_progname , query ,
mysql_error ( mysql_con ) , mysql_errno ( mysql_con ) ) ;
return 1 ;
}
return 0 ;
}
2000-07-31 21:29:14 +02:00
static void safe_exit ( int error )
{
if ( ! first_error )
first_error = error ;
if ( ignore_errors )
return ;
if ( sock )
mysql_close ( sock ) ;
exit ( error ) ;
}
/* safe_exit */
/*
* * dbConnect - - connects to the host and selects DB .
*/
static int dbConnect ( char * host , char * user , char * passwd )
{
2004-03-11 15:46:27 +01:00
char buff [ 20 + FN_REFLEN ] ;
2000-07-31 21:29:14 +02:00
DBUG_ENTER ( " dbConnect " ) ;
if ( verbose )
{
2001-07-04 08:39:58 +02:00
fprintf ( stderr , " -- Connecting to %s... \n " , host ? host : " localhost " ) ;
2000-07-31 21:29:14 +02:00
}
mysql_init ( & mysql_connection ) ;
if ( opt_compress )
mysql_options ( & mysql_connection , MYSQL_OPT_COMPRESS , NullS ) ;
# ifdef HAVE_OPENSSL
if ( opt_use_ssl )
mysql_ssl_set ( & mysql_connection , opt_ssl_key , opt_ssl_cert , opt_ssl_ca ,
2001-09-30 04:46:20 +02:00
opt_ssl_capath , opt_ssl_cipher ) ;
2002-11-14 20:16:30 +01:00
# endif
if ( opt_protocol )
mysql_options ( & mysql_connection , MYSQL_OPT_PROTOCOL , ( char * ) & opt_protocol ) ;
# ifdef HAVE_SMEM
if ( shared_memory_base_name )
mysql_options ( & mysql_connection , MYSQL_SHARED_MEMORY_BASE_NAME , shared_memory_base_name ) ;
2000-07-31 21:29:14 +02:00
# endif
2004-05-01 11:07:42 +02:00
mysql_options ( & mysql_connection , MYSQL_SET_CHARSET_NAME , default_charset ) ;
2000-07-31 21:29:14 +02:00
if ( ! ( sock = mysql_real_connect ( & mysql_connection , host , user , passwd ,
NULL , opt_mysql_port , opt_mysql_unix_port ,
0 ) ) )
{
DBerror ( & mysql_connection , " when trying to connect " ) ;
return 1 ;
}
2004-11-10 17:56:45 +01:00
/*
As we ' re going to set SQL_MODE , it would be lost on reconnect , so we
cannot reconnect .
*/
sock - > reconnect = 0 ;
2004-03-11 15:46:27 +01:00
sprintf ( buff , " /*!40100 SET @@SQL_MODE= \" %s \" */ " ,
compatible_mode_normal_str ) ;
2004-11-10 17:56:45 +01:00
if ( mysql_query_with_error_report ( sock , 0 , buff ) )
2004-03-11 15:46:27 +01:00
{
mysql_close ( sock ) ;
safe_exit ( EX_MYSQLERR ) ;
return 1 ;
}
2000-07-31 21:29:14 +02:00
return 0 ;
} /* dbConnect */
/*
* * dbDisconnect - - disconnects from the host .
*/
static void dbDisconnect ( char * host )
{
if ( verbose )
2001-07-04 08:39:58 +02:00
fprintf ( stderr , " -- Disconnecting from %s... \n " , host ? host : " localhost " ) ;
2000-07-31 21:29:14 +02:00
mysql_close ( sock ) ;
} /* dbDisconnect */
static void unescape ( FILE * file , char * pos , uint length )
{
char * tmp ;
DBUG_ENTER ( " unescape " ) ;
if ( ! ( tmp = ( char * ) my_malloc ( length * 2 + 1 , MYF ( MY_WME ) ) ) )
{
ignore_errors = 0 ; /* Fatal error */
safe_exit ( EX_MYSQLERR ) ; /* Force exit */
}
2001-11-05 22:48:03 +01:00
mysql_real_escape_string ( & mysql_connection , tmp , pos , length ) ;
2000-07-31 21:29:14 +02:00
fputc ( ' \' ' , file ) ;
fputs ( tmp , file ) ;
fputc ( ' \' ' , file ) ;
2004-06-24 22:14:12 +02:00
check_io ( file ) ;
2000-07-31 21:29:14 +02:00
my_free ( tmp , MYF ( MY_WME ) ) ;
DBUG_VOID_RETURN ;
} /* unescape */
static my_bool test_if_special_chars ( const char * str )
{
# if MYSQL_VERSION_ID >= 32300
for ( ; * str ; str + + )
2003-03-16 08:20:45 +01:00
if ( ! my_isvar ( charset_info , * str ) & & * str ! = ' $ ' )
2000-07-31 21:29:14 +02:00
return 1 ;
# endif
return 0 ;
} /* test_if_special_chars */
2003-09-03 17:48:10 +02:00
2003-11-04 08:40:36 +01:00
2003-09-03 17:48:10 +02:00
static char * quote_name ( const char * name , char * buff , my_bool force )
2000-07-31 21:29:14 +02:00
{
2003-09-03 17:48:10 +02:00
char * to = buff ;
2004-03-11 15:46:27 +01:00
char qtype = ( opt_compatible_mode & MASK_ANSI_QUOTES ) ? ' \" ' : ' ` ' ;
2003-09-03 17:48:10 +02:00
if ( ! force & & ! opt_quoted & & ! test_if_special_chars ( name ) )
return ( char * ) name ;
2004-03-11 15:46:27 +01:00
* to + + = qtype ;
2003-09-03 17:48:10 +02:00
while ( * name )
{
2004-03-11 15:46:27 +01:00
if ( * name = = qtype )
* to + + = qtype ;
2003-09-03 17:48:10 +02:00
* to + + = * name + + ;
}
2004-03-11 15:46:27 +01:00
to [ 0 ] = qtype ;
to [ 1 ] = 0 ;
2000-07-31 21:29:14 +02:00
return buff ;
} /* quote_name */
2003-12-14 05:39:52 +01:00
static char * quote_for_like ( const char * name , char * buff )
{
char * to = buff ;
* to + + = ' \' ' ;
while ( * name )
{
if ( * name = = ' \' ' | | * name = = ' _ ' | | * name = = ' \\ ' | | * name = = ' % ' )
* to + + = ' \\ ' ;
* to + + = * name + + ;
}
to [ 0 ] = ' \' ' ;
to [ 1 ] = 0 ;
return buff ;
}
2003-11-03 13:49:39 +01:00
/*
Quote and print a string .
SYNOPSIS
print_quoted_xml ( )
output - output file
str - string to print
len - its length
DESCRIPTION
2004-11-16 23:45:24 +01:00
Quote ' < ' ' > ' ' & ' ' \" ' chars and print a string to the xml_file .
2003-11-03 13:49:39 +01:00
*/
static void print_quoted_xml ( FILE * xml_file , const char * str , ulong len )
{
const char * end ;
for ( end = str + len ; str ! = end ; str + + )
{
switch ( * str ) {
case ' < ' :
fputs ( " < " , xml_file ) ;
break ;
case ' > ' :
fputs ( " > " , xml_file ) ;
break ;
case ' & ' :
fputs ( " & " , xml_file ) ;
break ;
case ' \" ' :
fputs ( " " " , xml_file ) ;
break ;
default :
fputc ( * str , xml_file ) ;
break ;
}
}
2004-06-24 22:14:12 +02:00
check_io ( xml_file ) ;
2003-11-03 13:49:39 +01:00
}
/*
Print xml tag with one attribute .
SYNOPSIS
print_xml_tag1 ( )
xml_file - output file
sbeg - line beginning
stag_atr - tag and attribute
sval - value of attribute
send - line ending
DESCRIPTION
Print tag with one attribute to the xml_file . Format is :
sbeg < stag_atr = " sval " > send
NOTE
sval MUST be a NULL terminated string .
sval string will be qouted before output .
*/
static void print_xml_tag1 ( FILE * xml_file , const char * sbeg ,
const char * stag_atr , const char * sval ,
const char * send )
{
fputs ( sbeg , xml_file ) ;
fputs ( " < " , xml_file ) ;
fputs ( stag_atr , xml_file ) ;
fputs ( " \" " , xml_file ) ;
print_quoted_xml ( xml_file , sval , strlen ( sval ) ) ;
fputs ( " \" > " , xml_file ) ;
fputs ( send , xml_file ) ;
2004-06-24 22:14:12 +02:00
check_io ( xml_file ) ;
2003-11-03 13:49:39 +01:00
}
/*
Print xml tag with many attributes .
SYNOPSIS
print_xml_row ( )
xml_file - output file
row_name - xml tag name
tableRes - query result
row - result row
DESCRIPTION
Print tag with many attribute to the xml_file . Format is :
\ t \ t < row_name Atr1 = " Val1 " Atr2 = " Val2 " . . . / >
NOTE
All atributes and values will be quoted before output .
*/
static void print_xml_row ( FILE * xml_file , const char * row_name ,
MYSQL_RES * tableRes , MYSQL_ROW * row )
2003-10-30 11:58:30 +01:00
{
uint i ;
MYSQL_FIELD * field ;
ulong * lengths = mysql_fetch_lengths ( tableRes ) ;
fprintf ( xml_file , " \t \t <%s " , row_name ) ;
2004-06-24 22:14:12 +02:00
check_io ( xml_file ) ;
2003-10-30 11:58:30 +01:00
mysql_field_seek ( tableRes , 0 ) ;
for ( i = 0 ; ( field = mysql_fetch_field ( tableRes ) ) ; i + + )
{
2003-11-03 13:49:39 +01:00
if ( ( * row ) [ i ] )
2003-10-30 11:58:30 +01:00
{
2004-04-26 14:53:31 +02:00
fputc ( ' ' , xml_file ) ;
2003-11-03 13:49:39 +01:00
print_quoted_xml ( xml_file , field - > name , field - > name_length ) ;
2003-10-30 11:58:30 +01:00
fputs ( " = \" " , xml_file ) ;
print_quoted_xml ( xml_file , ( * row ) [ i ] , lengths [ i ] ) ;
2004-04-27 14:33:40 +02:00
fputc ( ' " ' , xml_file ) ;
2004-06-24 22:14:12 +02:00
check_io ( xml_file ) ;
2003-10-30 11:58:30 +01:00
}
}
fputs ( " /> \n " , xml_file ) ;
2004-06-24 22:14:12 +02:00
check_io ( xml_file ) ;
2003-10-30 11:58:30 +01:00
}
2000-07-31 21:29:14 +02:00
/*
2003-09-03 17:48:10 +02:00
getStructure - - retrievs database structure , prints out corresponding
CREATE statement and fills out insert_pat .
RETURN
number of fields in table , 0 if error
2000-07-31 21:29:14 +02:00
*/
2003-09-03 17:48:10 +02:00
2000-07-31 21:29:14 +02:00
static uint getTableStructure ( char * table , char * db )
{
MYSQL_RES * tableRes ;
MYSQL_ROW row ;
my_bool init = 0 ;
uint numFields ;
2003-09-03 17:48:10 +02:00
char * strpos , * result_table , * opt_quoted_table ;
2000-07-31 21:29:14 +02:00
const char * delayed ;
2003-09-03 17:48:10 +02:00
char name_buff [ NAME_LEN + 3 ] , table_buff [ NAME_LEN * 2 + 3 ] ;
char table_buff2 [ NAME_LEN * 2 + 3 ] ;
2001-08-22 00:45:07 +02:00
FILE * sql_file = md_result_file ;
2000-07-31 21:29:14 +02:00
DBUG_ENTER ( " getTableStructure " ) ;
delayed = opt_delayed ? " DELAYED " : " " ;
if ( verbose )
2001-07-04 08:39:58 +02:00
fprintf ( stderr , " -- Retrieving table structure for table %s... \n " , table ) ;
2000-07-31 21:29:14 +02:00
2003-11-04 08:40:36 +01:00
sprintf ( insert_pat , " SET OPTION SQL_QUOTE_SHOW_CREATE=%d " ,
( opt_quoted | | opt_keywords ) ) ;
2004-04-27 14:33:40 +02:00
if ( ! create_options )
2004-05-05 20:24:21 +02:00
strmov ( strend ( insert_pat ) , " /*!40102 ,SQL_MODE=concat(@@sql_mode, _utf8 ',NO_KEY_OPTIONS,NO_TABLE_OPTIONS,NO_FIELD_OPTIONS') */ " ) ;
2004-04-27 14:33:40 +02:00
2003-09-03 17:48:10 +02:00
result_table = quote_name ( table , table_buff , 1 ) ;
opt_quoted_table = quote_name ( table , table_buff2 , 0 ) ;
2004-11-16 23:45:24 +01:00
if ( opt_order_by_primary )
order_by = primary_key_fields ( opt_quoted_table ) ;
2004-11-10 17:56:45 +01:00
if ( ! opt_xml & & ! mysql_query_with_error_report ( sock , 0 , insert_pat ) )
2000-07-31 21:29:14 +02:00
{
2000-10-29 13:49:42 +01:00
/* using SHOW CREATE statement */
if ( ! tFlag )
2000-07-31 21:29:14 +02:00
{
2000-10-29 13:49:42 +01:00
/* Make an sql-file, if path was given iow. option -T was given */
char buff [ 20 + FN_REFLEN ] ;
2003-09-03 17:48:10 +02:00
sprintf ( buff , " show create table %s " , result_table ) ;
2004-11-10 17:56:45 +01:00
if ( mysql_query_with_error_report ( sock , 0 , buff ) )
2000-07-31 21:29:14 +02:00
{
2000-10-29 13:49:42 +01:00
safe_exit ( EX_MYSQLERR ) ;
DBUG_RETURN ( 0 ) ;
2000-07-31 21:29:14 +02:00
}
2000-10-29 13:49:42 +01:00
if ( path )
{
char filename [ FN_REFLEN ] , tmp_path [ FN_REFLEN ] ;
2001-10-08 03:58:07 +02:00
convert_dirname ( tmp_path , path , NullS ) ;
2000-10-29 13:49:42 +01:00
sql_file = my_fopen ( fn_format ( filename , table , tmp_path , " .sql " , 4 ) ,
2001-12-20 19:14:55 +01:00
O_WRONLY , MYF ( MY_WME ) ) ;
2000-10-29 13:49:42 +01:00
if ( ! sql_file ) /* If file couldn't be opened */
{
2001-04-23 23:31:34 +02:00
safe_exit ( EX_MYSQLERR ) ;
DBUG_RETURN ( 0 ) ;
2000-10-29 13:49:42 +01:00
}
2002-01-02 20:29:41 +01:00
write_header ( sql_file , db ) ;
2000-10-29 13:49:42 +01:00
}
2003-12-11 14:37:45 +01:00
if ( ! opt_xml & & opt_comments )
2004-06-24 22:14:12 +02:00
{
2003-12-17 16:35:34 +01:00
fprintf ( sql_file , " \n -- \n -- Table structure for table %s \n -- \n \n " ,
2003-09-03 17:48:10 +02:00
result_table ) ;
2004-06-24 22:14:12 +02:00
check_io ( sql_file ) ;
}
2000-10-29 13:49:42 +01:00
if ( opt_drop )
2004-06-24 22:14:12 +02:00
{
2003-09-03 17:48:10 +02:00
fprintf ( sql_file , " DROP TABLE IF EXISTS %s; \n " , opt_quoted_table ) ;
2004-06-24 22:14:12 +02:00
check_io ( sql_file ) ;
}
2000-10-29 13:49:42 +01:00
tableRes = mysql_store_result ( sock ) ;
row = mysql_fetch_row ( tableRes ) ;
2003-10-30 11:58:30 +01:00
fprintf ( sql_file , " %s; \n " , row [ 1 ] ) ;
2004-06-24 22:14:12 +02:00
check_io ( sql_file ) ;
2000-10-29 13:49:42 +01:00
mysql_free_result ( tableRes ) ;
}
2003-09-03 17:48:10 +02:00
sprintf ( insert_pat , " show fields from %s " , result_table ) ;
2004-11-10 17:56:45 +01:00
if ( mysql_query_with_error_report ( sock , & tableRes , insert_pat ) )
2000-07-31 21:29:14 +02:00
{
2002-01-02 20:29:41 +01:00
if ( path )
my_fclose ( sql_file , MYF ( MY_WME ) ) ;
2000-10-29 13:49:42 +01:00
safe_exit ( EX_MYSQLERR ) ;
DBUG_RETURN ( 0 ) ;
2000-07-31 21:29:14 +02:00
}
2000-10-29 13:49:42 +01:00
2000-07-31 21:29:14 +02:00
if ( cFlag )
2003-09-03 17:48:10 +02:00
sprintf ( insert_pat , " INSERT %sINTO %s ( " , delayed , opt_quoted_table ) ;
2000-10-29 13:49:42 +01:00
else
2000-07-31 21:29:14 +02:00
{
2003-09-03 17:48:10 +02:00
sprintf ( insert_pat , " INSERT %sINTO %s VALUES " , delayed ,
opt_quoted_table ) ;
2000-10-29 13:49:42 +01:00
if ( ! extended_insert )
strcat ( insert_pat , " ( " ) ;
}
strpos = strend ( insert_pat ) ;
while ( ( row = mysql_fetch_row ( tableRes ) ) )
{
if ( init )
2000-07-31 21:29:14 +02:00
{
2000-10-29 13:49:42 +01:00
if ( cFlag )
2000-12-15 09:58:17 +01:00
strpos = strmov ( strpos , " , " ) ;
2000-07-31 21:29:14 +02:00
}
2000-10-29 13:49:42 +01:00
init = 1 ;
if ( cFlag )
2003-09-03 17:48:10 +02:00
strpos = strmov ( strpos , quote_name ( row [ SHOW_FIELDNAME ] , name_buff , 0 ) ) ;
2000-07-31 21:29:14 +02:00
}
2000-10-29 13:49:42 +01:00
numFields = ( uint ) mysql_num_rows ( tableRes ) ;
mysql_free_result ( tableRes ) ;
2000-07-31 21:29:14 +02:00
}
2000-10-29 13:49:42 +01:00
else
2000-07-31 21:29:14 +02:00
{
2004-04-27 14:33:40 +02:00
if ( verbose )
fprintf ( stderr ,
" %s: Warning: Can't set SQL_QUOTE_SHOW_CREATE option (%s) \n " ,
my_progname , mysql_error ( sock ) ) ;
2000-10-29 13:49:42 +01:00
2003-09-03 17:48:10 +02:00
sprintf ( insert_pat , " show fields from %s " , result_table ) ;
2004-11-10 17:56:45 +01:00
if ( mysql_query_with_error_report ( sock , & tableRes , insert_pat ) )
2000-07-31 21:29:14 +02:00
{
safe_exit ( EX_MYSQLERR ) ;
DBUG_RETURN ( 0 ) ;
}
2000-10-29 13:49:42 +01:00
/* Make an sql-file, if path was given iow. option -T was given */
if ( ! tFlag )
2000-07-31 21:29:14 +02:00
{
2000-10-29 13:49:42 +01:00
if ( path )
2000-07-31 21:29:14 +02:00
{
2000-10-29 13:49:42 +01:00
char filename [ FN_REFLEN ] , tmp_path [ FN_REFLEN ] ;
2001-10-08 03:58:07 +02:00
convert_dirname ( tmp_path , path , NullS ) ;
2000-10-29 13:49:42 +01:00
sql_file = my_fopen ( fn_format ( filename , table , tmp_path , " .sql " , 4 ) ,
O_WRONLY , MYF ( MY_WME ) ) ;
if ( ! sql_file ) /* If file couldn't be opened */
{
2002-01-02 20:29:41 +01:00
safe_exit ( EX_MYSQLERR ) ;
DBUG_RETURN ( 0 ) ;
2000-10-29 13:49:42 +01:00
}
2002-01-02 20:29:41 +01:00
write_header ( sql_file , db ) ;
2000-07-31 21:29:14 +02:00
}
2003-12-11 14:37:45 +01:00
if ( ! opt_xml & & opt_comments )
2003-09-03 17:48:10 +02:00
fprintf ( sql_file , " \n -- \n -- Table structure for table %s \n -- \n \n " ,
result_table ) ;
2000-10-29 13:49:42 +01:00
if ( opt_drop )
2003-09-02 18:30:34 +02:00
fprintf ( sql_file , " DROP TABLE IF EXISTS %s; \n " , result_table ) ;
2003-10-30 11:58:30 +01:00
if ( ! opt_xml )
fprintf ( sql_file , " CREATE TABLE %s ( \n " , result_table ) ;
else
2003-11-03 13:49:39 +01:00
print_xml_tag1 ( sql_file , " \t " , " table_structure name= " , table , " \n " ) ;
2004-06-24 22:14:12 +02:00
check_io ( sql_file ) ;
2000-10-29 13:49:42 +01:00
}
if ( cFlag )
2003-09-02 18:30:34 +02:00
sprintf ( insert_pat , " INSERT %sINTO %s ( " , delayed , result_table ) ;
2000-10-29 13:49:42 +01:00
else
{
2003-09-02 18:30:34 +02:00
sprintf ( insert_pat , " INSERT %sINTO %s VALUES " , delayed , result_table ) ;
2000-10-29 13:49:42 +01:00
if ( ! extended_insert )
strcat ( insert_pat , " ( " ) ;
2000-07-31 21:29:14 +02:00
}
2000-10-29 13:49:42 +01:00
strpos = strend ( insert_pat ) ;
2000-07-31 21:29:14 +02:00
while ( ( row = mysql_fetch_row ( tableRes ) ) )
{
2000-10-29 13:49:42 +01:00
ulong * lengths = mysql_fetch_lengths ( tableRes ) ;
if ( init )
2000-07-31 21:29:14 +02:00
{
2003-10-30 11:58:30 +01:00
if ( ! opt_xml & & ! tFlag )
2004-06-24 22:14:12 +02:00
{
2001-04-25 23:10:43 +02:00
fputs ( " , \n " , sql_file ) ;
2004-06-24 22:14:12 +02:00
check_io ( sql_file ) ;
}
2000-10-29 13:49:42 +01:00
if ( cFlag )
2001-04-25 23:10:43 +02:00
strpos = strmov ( strpos , " , " ) ;
2000-10-29 13:49:42 +01:00
}
init = 1 ;
if ( cFlag )
2003-09-03 17:48:10 +02:00
strpos = strmov ( strpos , quote_name ( row [ SHOW_FIELDNAME ] , name_buff , 0 ) ) ;
2000-10-29 13:49:42 +01:00
if ( ! tFlag )
{
2003-10-30 11:58:30 +01:00
if ( opt_xml )
{
print_xml_row ( sql_file , " field " , tableRes , & row ) ;
continue ;
}
2000-10-29 13:49:42 +01:00
if ( opt_keywords )
2003-09-02 18:30:34 +02:00
fprintf ( sql_file , " %s.%s %s " , result_table ,
2003-09-03 17:48:10 +02:00
quote_name ( row [ SHOW_FIELDNAME ] , name_buff , 0 ) ,
row [ SHOW_TYPE ] ) ;
2000-10-29 13:49:42 +01:00
else
2001-04-23 23:31:34 +02:00
fprintf ( sql_file , " %s %s " , quote_name ( row [ SHOW_FIELDNAME ] ,
2003-09-03 17:48:10 +02:00
name_buff , 0 ) ,
row [ SHOW_TYPE ] ) ;
2000-10-29 13:49:42 +01:00
if ( row [ SHOW_DEFAULT ] )
{
2001-04-23 23:31:34 +02:00
fputs ( " DEFAULT " , sql_file ) ;
2001-11-05 22:48:03 +01:00
unescape ( sql_file , row [ SHOW_DEFAULT ] , lengths [ SHOW_DEFAULT ] ) ;
2000-10-29 13:49:42 +01:00
}
if ( ! row [ SHOW_NULL ] [ 0 ] )
2001-04-23 23:31:34 +02:00
fputs ( " NOT NULL " , sql_file ) ;
2000-10-29 13:49:42 +01:00
if ( row [ SHOW_EXTRA ] [ 0 ] )
2001-04-23 23:31:34 +02:00
fprintf ( sql_file , " %s " , row [ SHOW_EXTRA ] ) ;
2004-06-24 22:14:12 +02:00
check_io ( sql_file ) ;
2000-07-31 21:29:14 +02:00
}
}
2000-10-29 13:49:42 +01:00
numFields = ( uint ) mysql_num_rows ( tableRes ) ;
mysql_free_result ( tableRes ) ;
if ( ! tFlag )
2000-07-31 21:29:14 +02:00
{
2000-10-29 13:49:42 +01:00
/* Make an sql-file, if path was given iow. option -T was given */
char buff [ 20 + FN_REFLEN ] ;
uint keynr , primary_key ;
2003-09-03 17:48:10 +02:00
sprintf ( buff , " show keys from %s " , result_table ) ;
2004-11-10 17:56:45 +01:00
if ( mysql_query_with_error_report ( sock , & tableRes , buff ) )
2000-07-31 21:29:14 +02:00
{
2002-01-02 20:29:41 +01:00
if ( path )
2001-04-23 23:31:34 +02:00
my_fclose ( sql_file , MYF ( MY_WME ) ) ;
2000-10-29 13:49:42 +01:00
safe_exit ( EX_MYSQLERR ) ;
DBUG_RETURN ( 0 ) ;
2000-07-31 21:29:14 +02:00
}
2000-10-29 13:49:42 +01:00
/* Find first which key is primary key */
keynr = 0 ;
primary_key = INT_MAX ;
while ( ( row = mysql_fetch_row ( tableRes ) ) )
2000-07-31 21:29:14 +02:00
{
2000-10-29 13:49:42 +01:00
if ( atoi ( row [ 3 ] ) = = 1 )
{
2001-04-23 23:31:34 +02:00
keynr + + ;
# ifdef FORCE_PRIMARY_KEY
if ( atoi ( row [ 1 ] ) = = 0 & & primary_key = = INT_MAX )
primary_key = keynr ;
# endif
if ( ! strcmp ( row [ 2 ] , " PRIMARY " ) )
{
primary_key = keynr ;
break ;
}
2000-10-29 13:49:42 +01:00
}
2000-07-31 21:29:14 +02:00
}
2000-10-29 13:49:42 +01:00
mysql_data_seek ( tableRes , 0 ) ;
keynr = 0 ;
while ( ( row = mysql_fetch_row ( tableRes ) ) )
{
2003-10-30 11:58:30 +01:00
if ( opt_xml )
{
print_xml_row ( sql_file , " key " , tableRes , & row ) ;
continue ;
}
2000-10-29 13:49:42 +01:00
if ( atoi ( row [ 3 ] ) = = 1 )
{
2001-04-23 23:31:34 +02:00
if ( keynr + + )
putc ( ' ) ' , sql_file ) ;
if ( atoi ( row [ 1 ] ) ) /* Test if duplicate key */
/* Duplicate allowed */
2003-09-03 17:48:10 +02:00
fprintf ( sql_file , " , \n KEY %s ( " , quote_name ( row [ 2 ] , name_buff , 0 ) ) ;
2001-04-23 23:31:34 +02:00
else if ( keynr = = primary_key )
fputs ( " , \n PRIMARY KEY ( " , sql_file ) ; /* First UNIQUE is primary */
else
2003-11-04 08:40:36 +01:00
fprintf ( sql_file , " , \n UNIQUE %s ( " , quote_name ( row [ 2 ] , name_buff ,
0 ) ) ;
2000-10-29 13:49:42 +01:00
}
else
2001-04-23 23:31:34 +02:00
putc ( ' , ' , sql_file ) ;
2003-09-03 17:48:10 +02:00
fputs ( quote_name ( row [ 4 ] , name_buff , 0 ) , sql_file ) ;
2000-10-29 13:49:42 +01:00
if ( row [ 7 ] )
2001-04-23 23:31:34 +02:00
fprintf ( sql_file , " (%s) " , row [ 7 ] ) ; /* Sub key */
2004-06-24 22:14:12 +02:00
check_io ( sql_file ) ;
2000-10-29 13:49:42 +01:00
}
2003-10-30 11:58:30 +01:00
if ( ! opt_xml )
{
if ( keynr )
putc ( ' ) ' , sql_file ) ;
fputs ( " \n ) " , sql_file ) ;
2004-06-24 22:14:12 +02:00
check_io ( sql_file ) ;
2003-10-30 11:58:30 +01:00
}
2000-10-29 13:49:42 +01:00
/* Get MySQL specific create options */
if ( create_options )
2000-07-31 21:29:14 +02:00
{
2003-12-14 05:39:52 +01:00
char show_name_buff [ FN_REFLEN ] ;
sprintf ( buff , " show table status like %s " ,
quote_for_like ( table , show_name_buff ) ) ;
2004-11-10 17:56:45 +01:00
if ( mysql_query_with_error_report ( sock , & tableRes , buff ) )
2000-10-29 13:49:42 +01:00
{
2001-04-23 23:31:34 +02:00
if ( mysql_errno ( sock ) ! = ER_PARSE_ERROR )
{ /* If old MySQL version */
if ( verbose )
fprintf ( stderr ,
2003-09-03 17:48:10 +02:00
" -- Warning: Couldn't get status information for table %s (%s) \n " ,
result_table , mysql_error ( sock ) ) ;
2001-04-23 23:31:34 +02:00
}
2000-10-29 13:49:42 +01:00
}
2004-11-10 17:56:45 +01:00
else if ( ! ( row = mysql_fetch_row ( tableRes ) ) )
2000-10-29 13:49:42 +01:00
{
2001-04-23 23:31:34 +02:00
fprintf ( stderr ,
2003-09-03 17:48:10 +02:00
" Error: Couldn't read status information for table %s (%s) \n " ,
result_table , mysql_error ( sock ) ) ;
2000-10-29 13:49:42 +01:00
}
else
{
2003-10-30 11:58:30 +01:00
if ( opt_xml )
{
print_xml_row ( sql_file , " options " , tableRes , & row ) ;
}
else
{
fputs ( " /*! " , sql_file ) ;
2004-04-27 14:33:40 +02:00
print_value ( sql_file , tableRes , row , " engine= " , " Engine " , 0 ) ;
2003-10-30 11:58:30 +01:00
print_value ( sql_file , tableRes , row , " " , " Create_options " , 0 ) ;
print_value ( sql_file , tableRes , row , " comment= " , " Comment " , 1 ) ;
fputs ( " */ " , sql_file ) ;
2004-06-24 22:14:12 +02:00
check_io ( sql_file ) ;
2003-10-30 11:58:30 +01:00
}
2000-10-29 13:49:42 +01:00
}
mysql_free_result ( tableRes ) ; /* Is always safe to free */
2000-07-31 21:29:14 +02:00
}
2003-10-30 11:58:30 +01:00
if ( ! opt_xml )
fputs ( " ; \n " , sql_file ) ;
else
fputs ( " \t </table_structure> \n " , sql_file ) ;
2004-06-24 22:14:12 +02:00
check_io ( sql_file ) ;
2000-07-31 21:29:14 +02:00
}
}
if ( cFlag )
{
strpos = strmov ( strpos , " ) VALUES " ) ;
if ( ! extended_insert )
strpos = strmov ( strpos , " ( " ) ;
}
2002-01-02 20:29:41 +01:00
if ( sql_file ! = md_result_file )
2004-02-13 22:21:46 +01:00
{
fputs ( " \n " , sql_file ) ;
write_footer ( sql_file ) ;
2002-01-02 20:29:41 +01:00
my_fclose ( sql_file , MYF ( MY_WME ) ) ;
2004-02-13 22:21:46 +01:00
}
2000-07-31 21:29:14 +02:00
DBUG_RETURN ( numFields ) ;
} /* getTableStructure */
static char * add_load_option ( char * ptr , const char * object ,
const char * statement )
{
if ( object )
{
2001-01-02 13:29:47 +01:00
/* Don't escape hex constants */
if ( object [ 0 ] = = ' 0 ' & & ( object [ 1 ] = = ' x ' | | object [ 1 ] = = ' X ' ) )
ptr = strxmov ( ptr , " " , statement , " " , object , NullS ) ;
else
{
/* char constant; escape */
ptr = strxmov ( ptr , " " , statement , " ' " , NullS ) ;
ptr = field_escape ( ptr , object , ( uint ) strlen ( object ) ) ;
* ptr + + = ' \' ' ;
}
2000-07-31 21:29:14 +02:00
}
return ptr ;
} /* add_load_option */
/*
* * Allow the user to specify field terminator strings like :
* * " ' " , " \" , " \ \ " (escaped backslash), " \ t " (tab), " \ n " (newline)
* * This is done by doubleing ' and add a end - \ if needed to avoid
* * syntax errors from the SQL parser .
*/
static char * field_escape ( char * to , const char * from , uint length )
{
const char * end ;
uint end_backslashes = 0 ;
for ( end = from + length ; from ! = end ; from + + )
{
* to + + = * from ;
if ( * from = = ' \\ ' )
end_backslashes ^ = 1 ; /* find odd number of backslashes */
else
{
if ( * from = = ' \' ' & & ! end_backslashes )
* to + + = * from ; /* We want a duplicate of "'" for MySQL */
end_backslashes = 0 ;
}
}
/* Add missing backslashes if user has specified odd number of backs.*/
if ( end_backslashes )
* to + + = ' \\ ' ;
return to ;
} /* field_escape */
2004-05-07 00:02:57 +02:00
static char * alloc_query_str ( ulong size )
{
char * query ;
if ( ! ( query = ( char * ) my_malloc ( size , MYF ( MY_WME ) ) ) )
{
ignore_errors = 0 ; /* Fatal error */
safe_exit ( EX_MYSQLERR ) ; /* Force exit */
}
return query ;
}
2000-07-31 21:29:14 +02:00
/*
* * dumpTable saves database contents as a series of INSERT statements .
*/
static void dumpTable ( uint numFields , char * table )
{
2004-05-07 00:02:57 +02:00
char query_buf [ QUERY_LENGTH ] , * end , buff [ 256 ] , table_buff [ NAME_LEN + 3 ] ;
2003-09-05 05:56:28 +02:00
char * result_table , table_buff2 [ NAME_LEN * 2 + 3 ] , * opt_quoted_table ;
2004-05-07 00:02:57 +02:00
char * query = query_buf ;
2000-07-31 21:29:14 +02:00
MYSQL_RES * res ;
2003-02-16 21:50:00 +01:00
MYSQL_FIELD * field ;
MYSQL_ROW row ;
2000-07-31 21:29:14 +02:00
ulong rownr , row_break , total_length , init_length ;
2003-12-14 05:39:52 +01:00
const char * table_type ;
2004-05-07 00:02:57 +02:00
int error = 0 ;
2000-07-31 21:29:14 +02:00
2003-09-03 17:48:10 +02:00
result_table = quote_name ( table , table_buff , 1 ) ;
opt_quoted_table = quote_name ( table , table_buff2 , 0 ) ;
2003-12-14 05:39:52 +01:00
/* Check table type */
if ( ( table_type = check_if_ignore_table ( table ) ) )
{
if ( verbose )
fprintf ( stderr ,
" -- Skipping data for table '%s' because it's of type %s \n " ,
table , table_type ) ;
return ;
}
if ( verbose )
fprintf ( stderr , " -- Sending SELECT query... \n " ) ;
2000-07-31 21:29:14 +02:00
if ( path )
{
char filename [ FN_REFLEN ] , tmp_path [ FN_REFLEN ] ;
2001-10-08 03:58:07 +02:00
convert_dirname ( tmp_path , path , NullS ) ;
2000-07-31 21:29:14 +02:00
my_load_path ( tmp_path , tmp_path , NULL ) ;
fn_format ( filename , table , tmp_path , " .txt " , 4 ) ;
my_delete ( filename , MYF ( 0 ) ) ; /* 'INTO OUTFILE' doesn't work, if
filename wasn ' t deleted */
to_unix_path ( filename ) ;
2002-04-27 11:09:59 +02:00
sprintf ( query , " SELECT /*!40001 SQL_NO_CACHE */ * INTO OUTFILE '%s' " ,
filename ) ;
2000-07-31 21:29:14 +02:00
end = strend ( query ) ;
if ( fields_terminated | | enclosed | | opt_enclosed | | escaped )
end = strmov ( end , " FIELDS " ) ;
end = add_load_option ( end , fields_terminated , " TERMINATED BY " ) ;
end = add_load_option ( end , enclosed , " ENCLOSED BY " ) ;
end = add_load_option ( end , opt_enclosed , " OPTIONALLY ENCLOSED BY " ) ;
end = add_load_option ( end , escaped , " ESCAPED BY " ) ;
end = add_load_option ( end , lines_terminated , " LINES TERMINATED BY " ) ;
* end = ' \0 ' ;
2003-09-03 17:48:10 +02:00
sprintf ( buff , " FROM %s " , result_table ) ;
2000-07-31 21:29:14 +02:00
end = strmov ( end , buff ) ;
2004-11-16 23:45:24 +01:00
if ( where | | order_by )
2004-05-07 00:02:57 +02:00
{
2004-11-16 23:45:24 +01:00
query = alloc_query_str ( ( ulong ) ( ( end - query ) + 1 +
( where ? strlen ( where ) + 7 : 0 ) +
( order_by ? strlen ( order_by ) + 10 : 0 ) ) ) ;
end = strmov ( query , query_buf ) ;
if ( where )
end = strxmov ( end , " WHERE " , where , NullS ) ;
if ( order_by )
end = strxmov ( end , " ORDER BY " , order_by , NullS ) ;
2004-05-07 00:02:57 +02:00
}
if ( mysql_real_query ( sock , query , ( uint ) ( end - query ) ) )
2000-07-31 21:29:14 +02:00
{
DBerror ( sock , " when executing 'SELECT INTO OUTFILE' " ) ;
return ;
}
}
else
{
2003-12-11 14:37:45 +01:00
if ( ! opt_xml & & opt_comments )
2004-06-24 22:14:12 +02:00
{
2003-09-03 17:48:10 +02:00
fprintf ( md_result_file , " \n -- \n -- Dumping data for table %s \n -- \n " ,
result_table ) ;
2004-06-24 22:14:12 +02:00
check_io ( md_result_file ) ;
}
2002-04-27 11:09:59 +02:00
sprintf ( query , " SELECT /*!40001 SQL_NO_CACHE */ * FROM %s " ,
2003-09-05 05:56:28 +02:00
result_table ) ;
2004-11-16 23:45:24 +01:00
if ( where | | order_by )
2000-07-31 21:29:14 +02:00
{
2004-11-16 23:45:24 +01:00
query = alloc_query_str ( ( ulong ) ( strlen ( query ) + 1 +
( where ? strlen ( where ) + 7 : 0 ) +
( order_by ? strlen ( order_by ) + 10 : 0 ) ) ) ;
end = strmov ( query , query_buf ) ;
if ( where )
2004-06-24 22:14:12 +02:00
{
2004-11-16 23:45:24 +01:00
if ( ! opt_xml & & opt_comments )
{
fprintf ( md_result_file , " -- WHERE: %s \n " , where ) ;
check_io ( md_result_file ) ;
}
end = strxmov ( end , " WHERE " , where , NullS ) ;
}
if ( order_by )
{
if ( ! opt_xml & & opt_comments )
{
fprintf ( md_result_file , " -- ORDER BY: %s \n " , order_by ) ;
check_io ( md_result_file ) ;
}
end = strxmov ( end , " ORDER BY " , order_by , NullS ) ;
2004-06-24 22:14:12 +02:00
}
2000-07-31 21:29:14 +02:00
}
2004-02-09 12:31:03 +01:00
if ( ! opt_xml & & ! opt_compact )
2004-06-24 22:14:12 +02:00
{
2002-04-03 12:36:01 +02:00
fputs ( " \n " , md_result_file ) ;
2004-06-24 22:14:12 +02:00
check_io ( md_result_file ) ;
}
2004-11-10 17:56:45 +01:00
if ( mysql_query_with_error_report ( sock , 0 , query ) )
2000-07-31 21:29:14 +02:00
DBerror ( sock , " when retrieving data from server " ) ;
if ( quick )
res = mysql_use_result ( sock ) ;
else
res = mysql_store_result ( sock ) ;
if ( ! res )
DBerror ( sock , " when retrieving data from server " ) ;
if ( verbose )
2001-07-04 08:39:58 +02:00
fprintf ( stderr , " -- Retrieving rows... \n " ) ;
2000-07-31 21:29:14 +02:00
if ( mysql_num_fields ( res ) ! = numFields )
{
2003-09-03 17:48:10 +02:00
fprintf ( stderr , " %s: Error in field count for table: %s ! Aborting. \n " ,
my_progname , result_table ) ;
2004-05-07 00:02:57 +02:00
error = EX_CONSCHECK ;
goto err ;
2000-07-31 21:29:14 +02:00
}
2002-04-03 12:36:01 +02:00
if ( opt_disable_keys )
2004-06-24 22:14:12 +02:00
{
2003-09-05 07:16:13 +02:00
fprintf ( md_result_file , " \n /*!40000 ALTER TABLE %s DISABLE KEYS */; \n " ,
2003-09-05 05:56:28 +02:00
opt_quoted_table ) ;
2004-06-24 22:14:12 +02:00
check_io ( md_result_file ) ;
}
2000-07-31 21:29:14 +02:00
if ( opt_lock )
2004-06-24 22:14:12 +02:00
{
2003-09-03 17:48:10 +02:00
fprintf ( md_result_file , " LOCK TABLES %s WRITE; \n " , opt_quoted_table ) ;
2004-06-24 22:14:12 +02:00
check_io ( md_result_file ) ;
}
2000-07-31 21:29:14 +02:00
2004-05-27 01:47:04 +02:00
total_length = opt_net_buffer_length ; /* Force row break */
2000-07-31 21:29:14 +02:00
row_break = 0 ;
rownr = 0 ;
2000-08-21 23:18:32 +02:00
init_length = ( uint ) strlen ( insert_pat ) + 4 ;
2001-11-05 22:48:03 +01:00
if ( opt_xml )
2003-11-03 13:49:39 +01:00
print_xml_tag1 ( md_result_file , " \t " , " table_data name= " , table , " \n " ) ;
2000-07-31 21:29:14 +02:00
2001-10-04 19:43:06 +02:00
if ( opt_autocommit )
2004-06-24 22:14:12 +02:00
{
2001-10-04 19:43:06 +02:00
fprintf ( md_result_file , " set autocommit=0; \n " ) ;
2004-06-24 22:14:12 +02:00
check_io ( md_result_file ) ;
}
2001-10-04 19:43:06 +02:00
2000-07-31 21:29:14 +02:00
while ( ( row = mysql_fetch_row ( res ) ) )
{
uint i ;
ulong * lengths = mysql_fetch_lengths ( res ) ;
rownr + + ;
2001-11-05 22:48:03 +01:00
if ( ! extended_insert & & ! opt_xml )
2004-06-24 22:14:12 +02:00
{
2001-08-22 00:45:07 +02:00
fputs ( insert_pat , md_result_file ) ;
2004-06-24 22:14:12 +02:00
check_io ( md_result_file ) ;
}
2000-07-31 21:29:14 +02:00
mysql_field_seek ( res , 0 ) ;
2002-04-03 12:36:01 +02:00
if ( opt_xml )
2004-06-24 22:14:12 +02:00
{
2003-10-30 11:58:30 +01:00
fputs ( " \t <row> \n " , md_result_file ) ;
2004-06-24 22:14:12 +02:00
check_io ( md_result_file ) ;
}
2002-04-03 12:36:01 +02:00
2000-07-31 21:29:14 +02:00
for ( i = 0 ; i < mysql_num_fields ( res ) ; i + + )
{
2004-10-19 07:40:59 +02:00
int is_blob ;
2000-07-31 21:29:14 +02:00
if ( ! ( field = mysql_fetch_field ( res ) ) )
{
2003-09-03 17:48:10 +02:00
sprintf ( query , " %s: Not enough fields from table %s! Aborting. \n " ,
my_progname , result_table ) ;
2000-07-31 21:29:14 +02:00
fputs ( query , stderr ) ;
2004-05-07 00:02:57 +02:00
error = EX_CONSCHECK ;
goto err ;
2000-07-31 21:29:14 +02:00
}
2004-10-19 07:40:59 +02:00
/*
63 is my_charset_bin . If charsetnr is not 63 ,
we have not a BLOB but a TEXT column .
2004-11-05 05:54:52 +01:00
we ' ll dump in hex only BLOB columns .
2004-10-19 07:40:59 +02:00
*/
is_blob = ( opt_hex_blob & & field - > charsetnr = = 63 & &
2004-11-05 05:54:52 +01:00
( field - > type = = FIELD_TYPE_STRING | |
2004-11-05 17:35:56 +01:00
field - > type = = FIELD_TYPE_VAR_STRING | |
2004-11-05 05:54:52 +01:00
field - > type = = FIELD_TYPE_BLOB | |
2004-10-19 07:40:59 +02:00
field - > type = = FIELD_TYPE_LONG_BLOB | |
field - > type = = FIELD_TYPE_MEDIUM_BLOB | |
field - > type = = FIELD_TYPE_TINY_BLOB ) ) ? 1 : 0 ;
2003-10-14 12:19:54 +02:00
if ( extended_insert )
2000-07-31 21:29:14 +02:00
{
ulong length = lengths [ i ] ;
if ( i = = 0 )
dynstr_set ( & extended_row , " ( " ) ;
else
dynstr_append ( & extended_row , " , " ) ;
if ( row [ i ] )
{
if ( length )
{
2000-09-02 06:58:42 +02:00
if ( ! IS_NUM_FIELD ( field ) )
2000-07-31 21:29:14 +02:00
{
2004-11-05 05:54:52 +01:00
/*
" length * 2 + 2 " is OK for both HEX and non - HEX modes :
- In HEX mode we need exactly 2 bytes per character
plus 2 bytes for ' 0 x ' prefix .
- In non - HEX mode we need up to 2 bytes per character ,
plus 2 bytes for leading and trailing ' \' ' characters .
*/
2000-07-31 21:29:14 +02:00
if ( dynstr_realloc ( & extended_row , length * 2 + 2 ) )
{
fputs ( " Aborting dump (out of memory) " , stderr ) ;
2004-05-07 00:02:57 +02:00
error = EX_EOM ;
goto err ;
2000-07-31 21:29:14 +02:00
}
2004-10-19 07:40:59 +02:00
if ( opt_hex_blob & & is_blob )
{
dynstr_append ( & extended_row , " 0x " ) ;
2004-11-05 05:54:52 +01:00
extended_row . length + = mysql_hex_string ( extended_row . str +
extended_row . length ,
row [ i ] , length ) ;
extended_row . str [ extended_row . length ] = ' \0 ' ;
2004-10-19 07:40:59 +02:00
}
else
{
dynstr_append ( & extended_row , " ' " ) ;
extended_row . length + =
mysql_real_escape_string ( & mysql_connection ,
& extended_row . str [ extended_row . length ] ,
row [ i ] , length ) ;
extended_row . str [ extended_row . length ] = ' \0 ' ;
dynstr_append ( & extended_row , " ' " ) ;
}
2000-07-31 21:29:14 +02:00
}
else
2001-09-03 15:09:25 +02:00
{
2003-12-09 14:33:29 +01:00
/* change any strings ("inf", "-inf", "nan") into NULL */
2001-09-03 15:09:25 +02:00
char * ptr = row [ i ] ;
2003-12-17 16:35:34 +01:00
if ( my_isalpha ( charset_info , * ptr ) | | ( * ptr = = ' - ' & &
my_isalpha ( charset_info , ptr [ 1 ] ) ) )
2003-12-09 14:33:29 +01:00
dynstr_append ( & extended_row , " NULL " ) ;
else
{
if ( field - > type = = FIELD_TYPE_DECIMAL )
{
/* add " signs around */
2004-04-26 14:53:31 +02:00
dynstr_append ( & extended_row , " ' " ) ;
2003-12-09 14:33:29 +01:00
dynstr_append ( & extended_row , ptr ) ;
2004-04-26 14:53:31 +02:00
dynstr_append ( & extended_row , " ' " ) ;
2003-12-09 14:33:29 +01:00
}
else
dynstr_append ( & extended_row , ptr ) ;
}
2001-09-03 15:09:25 +02:00
}
2000-07-31 21:29:14 +02:00
}
else
2004-04-26 14:53:31 +02:00
dynstr_append ( & extended_row , " '' " ) ;
2000-07-31 21:29:14 +02:00
}
else if ( dynstr_append ( & extended_row , " NULL " ) )
{
fputs ( " Aborting dump (out of memory) " , stderr ) ;
2004-05-07 00:02:57 +02:00
error = EX_EOM ;
goto err ;
2000-07-31 21:29:14 +02:00
}
}
else
{
2001-11-05 22:48:03 +01:00
if ( i & & ! opt_xml )
2004-06-24 22:14:12 +02:00
{
2001-11-05 22:48:03 +01:00
fputc ( ' , ' , md_result_file ) ;
2004-06-24 22:14:12 +02:00
check_io ( md_result_file ) ;
}
2000-07-31 21:29:14 +02:00
if ( row [ i ] )
{
2000-09-02 06:58:42 +02:00
if ( ! IS_NUM_FIELD ( field ) )
2001-12-20 19:14:55 +01:00
{
2001-11-26 18:14:26 +01:00
if ( opt_xml )
2003-10-30 11:58:30 +01:00
{
2003-11-03 13:49:39 +01:00
print_xml_tag1 ( md_result_file , " \t \t " , " field name= " ,
field - > name , " " ) ;
2003-10-30 11:58:30 +01:00
print_quoted_xml ( md_result_file , row [ i ] , lengths [ i ] ) ;
fputs ( " </field> \n " , md_result_file ) ;
}
2004-10-19 07:40:59 +02:00
else if ( opt_hex_blob & & is_blob )
2004-11-12 10:17:53 +01:00
{
/* sakaik got the idea to to provide blob's in hex notation. */
2004-11-16 00:10:32 +01:00
char * ptr = row [ i ] , * end = ptr + lengths [ i ] ;
2004-10-19 07:40:59 +02:00
fputs ( " 0x " , md_result_file ) ;
2004-11-12 10:17:53 +01:00
for ( ; ptr < end ; ptr + + )
fprintf ( md_result_file , " %02X " , * ptr ) ;
2004-10-19 07:40:59 +02:00
}
else
unescape ( md_result_file , row [ i ] , lengths [ i ] ) ;
2001-11-05 22:48:03 +01:00
}
2000-07-31 21:29:14 +02:00
else
2001-09-03 15:09:25 +02:00
{
2003-12-09 14:33:29 +01:00
/* change any strings ("inf", "-inf", "nan") into NULL */
2001-09-03 15:09:25 +02:00
char * ptr = row [ i ] ;
2001-11-26 18:14:26 +01:00
if ( opt_xml )
2003-10-30 11:58:30 +01:00
{
2003-11-03 13:49:39 +01:00
print_xml_tag1 ( md_result_file , " \t \t " , " field name= " ,
field - > name , " " ) ;
2003-10-30 11:58:30 +01:00
fputs ( ! my_isalpha ( charset_info , * ptr ) ? ptr : " NULL " ,
md_result_file ) ;
fputs ( " </field> \n " , md_result_file ) ;
}
2003-12-17 16:35:34 +01:00
else if ( my_isalpha ( charset_info , * ptr ) | |
( * ptr = = ' - ' & & my_isalpha ( charset_info , ptr [ 1 ] ) ) )
fputs ( " NULL " , md_result_file ) ;
else if ( field - > type = = FIELD_TYPE_DECIMAL )
2003-12-19 15:25:50 +01:00
{
/* add " signs around */
2004-04-26 14:53:31 +02:00
fputc ( ' \' ' , md_result_file ) ;
2003-12-19 15:25:50 +01:00
fputs ( ptr , md_result_file ) ;
2004-04-26 14:53:31 +02:00
fputc ( ' \' ' , md_result_file ) ;
2003-12-19 15:25:50 +01:00
}
2001-11-26 18:14:26 +01:00
else
2003-12-19 15:25:50 +01:00
fputs ( ptr , md_result_file ) ;
2001-09-03 15:09:25 +02:00
}
2000-07-31 21:29:14 +02:00
}
2004-06-23 21:46:17 +02:00
else
fputs ( " NULL " , md_result_file ) ;
2004-06-24 23:29:27 +02:00
check_io ( md_result_file ) ;
2000-07-31 21:29:14 +02:00
}
}
2002-04-03 12:36:01 +02:00
if ( opt_xml )
2004-06-24 22:14:12 +02:00
{
2003-10-30 11:58:30 +01:00
fputs ( " \t </row> \n " , md_result_file ) ;
2004-06-24 22:14:12 +02:00
check_io ( md_result_file ) ;
}
2002-04-03 12:36:01 +02:00
2003-10-14 12:19:54 +02:00
if ( extended_insert )
2000-07-31 21:29:14 +02:00
{
ulong row_length ;
dynstr_append ( & extended_row , " ) " ) ;
row_length = 2 + extended_row . length ;
2004-05-27 01:47:04 +02:00
if ( total_length + row_length < opt_net_buffer_length )
2000-07-31 21:29:14 +02:00
{
total_length + = row_length ;
2001-08-22 00:45:07 +02:00
fputc ( ' , ' , md_result_file ) ; /* Always row break */
fputs ( extended_row . str , md_result_file ) ;
2000-07-31 21:29:14 +02:00
}
else
{
2003-10-14 12:19:54 +02:00
if ( row_break )
2001-08-22 00:45:07 +02:00
fputs ( " ; \n " , md_result_file ) ;
2000-07-31 21:29:14 +02:00
row_break = 1 ; /* This is first row */
2001-12-06 13:10:51 +01:00
2003-10-14 12:19:54 +02:00
fputs ( insert_pat , md_result_file ) ;
fputs ( extended_row . str , md_result_file ) ;
2000-07-31 21:29:14 +02:00
total_length = row_length + init_length ;
}
2004-06-24 22:14:12 +02:00
check_io ( md_result_file ) ;
2000-07-31 21:29:14 +02:00
}
2001-11-05 22:48:03 +01:00
else if ( ! opt_xml )
2004-06-24 22:14:12 +02:00
{
2001-08-22 00:45:07 +02:00
fputs ( " ); \n " , md_result_file ) ;
2004-06-24 22:14:12 +02:00
check_io ( md_result_file ) ;
}
2000-07-31 21:29:14 +02:00
}
2001-12-06 13:10:51 +01:00
2002-01-22 23:04:43 +01:00
/* XML - close table tag and supress regular output */
2001-11-05 22:48:03 +01:00
if ( opt_xml )
2003-10-30 11:58:30 +01:00
fputs ( " \t </table_data> \n " , md_result_file ) ;
2001-11-05 22:48:03 +01:00
else if ( extended_insert & & row_break )
2001-08-22 00:45:07 +02:00
fputs ( " ; \n " , md_result_file ) ; /* If not empty table */
fflush ( md_result_file ) ;
2004-06-24 22:14:12 +02:00
check_io ( md_result_file ) ;
2000-07-31 21:29:14 +02:00
if ( mysql_errno ( sock ) )
{
2003-09-03 17:48:10 +02:00
sprintf ( query , " %s: Error %d: %s when dumping table %s at row: %ld \n " ,
2000-07-31 21:29:14 +02:00
my_progname ,
mysql_errno ( sock ) ,
mysql_error ( sock ) ,
2003-09-03 17:48:10 +02:00
result_table ,
2000-07-31 21:29:14 +02:00
rownr ) ;
fputs ( query , stderr ) ;
2004-05-07 00:02:57 +02:00
error = EX_CONSCHECK ;
goto err ;
2000-07-31 21:29:14 +02:00
}
if ( opt_lock )
2004-06-24 22:14:12 +02:00
{
2001-08-22 00:45:07 +02:00
fputs ( " UNLOCK TABLES; \n " , md_result_file ) ;
2004-06-24 22:14:12 +02:00
check_io ( md_result_file ) ;
}
2002-04-03 12:36:01 +02:00
if ( opt_disable_keys )
2004-06-24 22:14:12 +02:00
{
2002-04-03 12:36:01 +02:00
fprintf ( md_result_file , " /*!40000 ALTER TABLE %s ENABLE KEYS */; \n " ,
2003-09-05 05:56:28 +02:00
opt_quoted_table ) ;
2004-06-24 22:14:12 +02:00
check_io ( md_result_file ) ;
}
2001-10-04 19:43:06 +02:00
if ( opt_autocommit )
2004-06-24 22:14:12 +02:00
{
2001-10-04 19:43:06 +02:00
fprintf ( md_result_file , " commit; \n " ) ;
2004-06-24 22:14:12 +02:00
check_io ( md_result_file ) ;
}
2001-10-10 22:44:07 +02:00
mysql_free_result ( res ) ;
2004-05-07 00:02:57 +02:00
if ( query ! = query_buf )
my_free ( query , MYF ( MY_ALLOW_ZERO_PTR ) ) ;
}
return ;
err :
if ( query ! = query_buf )
my_free ( query , MYF ( MY_ALLOW_ZERO_PTR ) ) ;
safe_exit ( error ) ;
return ;
2000-07-31 21:29:14 +02:00
} /* dumpTable */
static char * getTableName ( int reset )
{
static MYSQL_RES * res = NULL ;
MYSQL_ROW row ;
if ( ! res )
{
if ( ! ( res = mysql_list_tables ( sock , NullS ) ) )
return ( NULL ) ;
}
if ( ( row = mysql_fetch_row ( res ) ) )
return ( ( char * ) row [ 0 ] ) ;
2000-09-07 03:55:17 +02:00
2000-07-31 21:29:14 +02:00
if ( reset )
mysql_data_seek ( res , 0 ) ; /* We want to read again */
else
{
mysql_free_result ( res ) ;
res = NULL ;
}
return ( NULL ) ;
} /* getTableName */
static int dump_all_databases ( )
{
MYSQL_ROW row ;
MYSQL_RES * tableres ;
int result = 0 ;
2004-11-10 17:56:45 +01:00
if ( mysql_query_with_error_report ( sock , & tableres , " SHOW DATABASES " ) )
2000-07-31 21:29:14 +02:00
return 1 ;
while ( ( row = mysql_fetch_row ( tableres ) ) )
{
if ( dump_all_tables_in_db ( row [ 0 ] ) )
result = 1 ;
}
return result ;
}
/* dump_all_databases */
static int dump_databases ( char * * db_names )
{
int result = 0 ;
for ( ; * db_names ; db_names + + )
2001-12-20 19:14:55 +01:00
{
2000-07-31 21:29:14 +02:00
if ( dump_all_tables_in_db ( * db_names ) )
result = 1 ;
}
return result ;
} /* dump_databases */
static int init_dumping ( char * database )
{
if ( mysql_select_db ( sock , database ) )
{
DBerror ( sock , " when selecting the database " ) ;
return 1 ; /* If --force */
}
2002-11-18 17:50:42 +01:00
if ( ! path & & ! opt_xml )
2000-07-31 21:29:14 +02:00
{
if ( opt_databases | | opt_alldbs )
{
2003-11-04 08:40:36 +01:00
/*
2004-10-03 03:16:14 +02:00
length of table name * 2 ( if name contains quotes ) , 2 quotes and 0
2003-11-04 08:40:36 +01:00
*/
2003-10-24 23:26:26 +02:00
char quoted_database_buf [ 64 * 2 + 3 ] ;
char * qdatabase = quote_name ( database , quoted_database_buf , opt_quoted ) ;
2003-12-11 14:37:45 +01:00
if ( opt_comments )
2004-06-24 22:14:12 +02:00
{
2003-12-17 16:35:34 +01:00
fprintf ( md_result_file , " \n -- \n -- Current Database: %s \n -- \n " , qdatabase ) ;
2004-06-24 22:14:12 +02:00
check_io ( md_result_file ) ;
}
2000-07-31 21:29:14 +02:00
if ( ! opt_create_db )
2002-11-05 12:10:13 +01:00
{
2003-11-04 08:40:36 +01:00
char qbuf [ 256 ] ;
2002-11-05 12:10:13 +01:00
MYSQL_ROW row ;
MYSQL_RES * dbinfo ;
2003-10-14 12:19:54 +02:00
2004-10-19 08:00:44 +02:00
sprintf ( qbuf , " SHOW CREATE DATABASE IF NOT EXISTS %s " ,
2003-10-24 23:26:26 +02:00
qdatabase ) ;
2003-10-14 12:19:54 +02:00
2004-12-17 23:37:43 +01:00
if ( mysql_query ( sock , qbuf ) | | ! ( dbinfo = mysql_store_result ( sock ) ) )
2002-11-05 12:10:13 +01:00
{
/* Old server version, dump generic CREATE DATABASE */
2003-03-30 18:36:14 +02:00
fprintf ( md_result_file ,
2003-11-04 08:40:36 +01:00
" \n CREATE DATABASE /*!32312 IF NOT EXISTS*/ %s; \n " ,
qdatabase ) ;
2002-11-05 12:10:13 +01:00
}
else
{
row = mysql_fetch_row ( dbinfo ) ;
if ( row [ 1 ] )
{
fprintf ( md_result_file , " \n %s; \n " , row [ 1 ] ) ;
}
}
}
2003-11-04 08:40:36 +01:00
fprintf ( md_result_file , " \n USE %s; \n " , qdatabase ) ;
2004-06-24 22:14:12 +02:00
check_io ( md_result_file ) ;
2000-07-31 21:29:14 +02:00
}
}
2003-11-04 08:40:36 +01:00
if ( extended_insert & & init_dynamic_string ( & extended_row , " " , 1024 , 1024 ) )
exit ( EX_EOM ) ;
2000-07-31 21:29:14 +02:00
return 0 ;
} /* init_dumping */
2004-12-27 19:10:30 +01:00
my_bool include_table ( byte * hash_key , uint len )
{
2004-12-28 17:33:49 +01:00
if ( hash_search ( & ignore_table , ( byte * ) hash_key , len ) )
2004-12-27 19:10:30 +01:00
return FALSE ;
return TRUE ;
}
2003-11-04 08:40:36 +01:00
2000-07-31 21:29:14 +02:00
static int dump_all_tables_in_db ( char * database )
{
char * table ;
uint numrows ;
2003-09-03 17:48:10 +02:00
char table_buff [ NAME_LEN * 2 + 3 ] ;
2000-07-31 21:29:14 +02:00
2004-12-27 19:10:30 +01:00
char hash_key [ 2 * NAME_LEN + 2 ] ; /* "db.tablename" */
char * afterdot ;
afterdot = strmov ( hash_key , database ) ;
* afterdot + + = ' . ' ;
2000-07-31 21:29:14 +02:00
if ( init_dumping ( database ) )
return 1 ;
2002-11-18 17:50:42 +01:00
if ( opt_xml )
2003-11-03 13:49:39 +01:00
print_xml_tag1 ( md_result_file , " " , " database name= " , database , " \n " ) ;
2000-07-31 21:29:14 +02:00
if ( lock_tables )
{
DYNAMIC_STRING query ;
init_dynamic_string ( & query , " LOCK TABLES " , 256 , 1024 ) ;
2004-12-27 19:10:30 +01:00
for ( numrows = 0 ; ( table = getTableName ( 1 ) ) ; numrows + + )
2000-07-31 21:29:14 +02:00
{
2003-09-03 17:48:10 +02:00
dynstr_append ( & query , quote_name ( table , table_buff , 1 ) ) ;
2000-07-31 21:29:14 +02:00
dynstr_append ( & query , " READ /*!32311 LOCAL */, " ) ;
}
if ( numrows & & mysql_real_query ( sock , query . str , query . length - 1 ) )
2000-09-07 03:55:17 +02:00
DBerror ( sock , " when using LOCK TABLES " ) ;
2000-07-31 21:29:14 +02:00
/* We shall continue here, if --force was given */
dynstr_free ( & query ) ;
}
if ( flush_logs )
{
if ( mysql_refresh ( sock , REFRESH_LOG ) )
2000-09-07 03:55:17 +02:00
DBerror ( sock , " when doing refresh " ) ;
2000-07-31 21:29:14 +02:00
/* We shall continue here, if --force was given */
}
2004-12-27 19:10:30 +01:00
while ( ( table = getTableName ( 0 ) ) )
2000-07-31 21:29:14 +02:00
{
2004-12-27 19:10:30 +01:00
char * end = strmov ( afterdot , table ) ;
if ( include_table ( hash_key , end - hash_key ) )
{
numrows = getTableStructure ( table , database ) ;
if ( ! dFlag & & numrows > 0 )
dumpTable ( numrows , table ) ;
my_free ( order_by , MYF ( MY_ALLOW_ZERO_PTR ) ) ;
order_by = 0 ;
}
2000-07-31 21:29:14 +02:00
}
2002-11-18 17:50:42 +01:00
if ( opt_xml )
2004-06-24 22:14:12 +02:00
{
2003-10-30 11:58:30 +01:00
fputs ( " </database> \n " , md_result_file ) ;
2004-06-24 22:14:12 +02:00
check_io ( md_result_file ) ;
}
2000-07-31 21:29:14 +02:00
if ( lock_tables )
2004-11-10 17:56:45 +01:00
mysql_query_with_error_report ( sock , 0 , " UNLOCK TABLES " ) ;
2000-07-31 21:29:14 +02:00
return 0 ;
} /* dump_all_tables_in_db */
2005-01-27 21:48:26 +01:00
/*
get_actual_table_name - - executes a SHOW TABLES LIKE ' % s ' to get the actual table name
from the server for the table name given on the command line . we do this because
the table name given on the command line may be a different case ( e . g . T1 vs t1 )
RETURN
void
*/
static void get_actual_table_name ( const char * old_table_name , char * new_table_name , int buf_size )
{
MYSQL_RES * tableRes ;
MYSQL_ROW row ;
char query [ NAME_LEN * 2 + 3 + 50 ] ;
DBUG_ENTER ( " get_actual_table_name " ) ;
sprintf ( query , " SHOW TABLES LIKE '%s' " , old_table_name ) ;
if ( mysql_query_with_error_report ( sock , 0 , query ) )
{
safe_exit ( EX_MYSQLERR ) ;
}
tableRes = mysql_store_result ( sock ) ;
row = mysql_fetch_row ( tableRes ) ;
strncpy ( new_table_name , row [ 0 ] , buf_size ) ;
mysql_free_result ( tableRes ) ;
} /* get_actual_table_name */
2000-07-31 21:29:14 +02:00
static int dump_selected_tables ( char * db , char * * table_names , int tables )
{
uint numrows ;
2003-09-03 17:48:10 +02:00
char table_buff [ NAME_LEN * + 3 ] ;
2000-07-31 21:29:14 +02:00
if ( init_dumping ( db ) )
return 1 ;
if ( lock_tables )
{
DYNAMIC_STRING query ;
int i ;
init_dynamic_string ( & query , " LOCK TABLES " , 256 , 1024 ) ;
for ( i = 0 ; i < tables ; i + + )
{
2003-09-03 17:48:10 +02:00
dynstr_append ( & query , quote_name ( table_names [ i ] , table_buff , 1 ) ) ;
2000-07-31 21:29:14 +02:00
dynstr_append ( & query , " READ /*!32311 LOCAL */, " ) ;
}
if ( mysql_real_query ( sock , query . str , query . length - 1 ) )
2000-09-07 03:55:17 +02:00
DBerror ( sock , " when doing LOCK TABLES " ) ;
2000-07-31 21:29:14 +02:00
/* We shall countinue here, if --force was given */
dynstr_free ( & query ) ;
}
if ( flush_logs )
{
if ( mysql_refresh ( sock , REFRESH_LOG ) )
2000-09-07 03:55:17 +02:00
DBerror ( sock , " when doing refresh " ) ;
2000-07-31 21:29:14 +02:00
/* We shall countinue here, if --force was given */
}
2002-11-18 17:50:42 +01:00
if ( opt_xml )
2003-11-03 13:49:39 +01:00
print_xml_tag1 ( md_result_file , " " , " database name= " , db , " \n " ) ;
2000-07-31 21:29:14 +02:00
for ( ; tables > 0 ; tables - - , table_names + + )
{
2005-01-27 21:48:26 +01:00
char new_table_name [ NAME_LEN * + 3 ] ;
/* the table name passed on commandline may be wrong case */
get_actual_table_name ( * table_names , new_table_name , sizeof ( new_table_name ) ) ;
numrows = getTableStructure ( new_table_name , db ) ;
2000-07-31 21:29:14 +02:00
if ( ! dFlag & & numrows > 0 )
2005-01-27 21:48:26 +01:00
dumpTable ( numrows , new_table_name ) ;
2004-12-09 11:47:20 +01:00
my_free ( order_by , MYF ( MY_ALLOW_ZERO_PTR ) ) ;
order_by = 0 ;
2000-07-31 21:29:14 +02:00
}
2002-11-18 17:50:42 +01:00
if ( opt_xml )
2004-06-24 22:14:12 +02:00
{
2003-10-30 11:58:30 +01:00
fputs ( " </database> \n " , md_result_file ) ;
2004-06-24 22:14:12 +02:00
check_io ( md_result_file ) ;
}
2000-07-31 21:29:14 +02:00
if ( lock_tables )
2004-11-10 17:56:45 +01:00
mysql_query_with_error_report ( sock , 0 , " UNLOCK TABLES " ) ;
2000-07-31 21:29:14 +02:00
return 0 ;
} /* dump_selected_tables */
2004-11-10 17:56:45 +01:00
static int do_show_master_status ( MYSQL * mysql_con )
{
MYSQL_ROW row ;
MYSQL_RES * master ;
const char * comment_prefix =
( opt_master_data = = MYSQL_OPT_MASTER_DATA_COMMENTED_SQL ) ? " -- " : " " ;
if ( mysql_query_with_error_report ( mysql_con , & master , " SHOW MASTER STATUS " ) )
{
my_printf_error ( 0 , " Error: Couldn't execute 'SHOW MASTER STATUS': %s " ,
MYF ( 0 ) , mysql_error ( mysql_con ) ) ;
return 1 ;
}
else
{
row = mysql_fetch_row ( master ) ;
if ( row & & row [ 0 ] & & row [ 1 ] )
{
if ( opt_comments )
fprintf ( md_result_file ,
" \n -- \n -- Position to start replication or point-in-time "
" recovery from \n -- \n \n " ) ;
fprintf ( md_result_file ,
" %sCHANGE MASTER TO MASTER_LOG_FILE='%s', MASTER_LOG_POS=%s; \n " ,
comment_prefix , row [ 0 ] , row [ 1 ] ) ;
check_io ( md_result_file ) ;
}
mysql_free_result ( master ) ;
}
return 0 ;
}
static int do_flush_tables_read_lock ( MYSQL * mysql_con )
{
2004-11-14 17:45:37 +01:00
/*
We do first a FLUSH TABLES . If a long update is running , the FLUSH TABLES
will wait but will not stall the whole mysqld , and when the long update is
done the FLUSH TABLES WITH READ LOCK will start and succeed quickly . So ,
FLUSH TABLES is to lower the probability of a stage where both mysqldump
and most client connections are stalled . Of course , if a second long
update starts between the two FLUSHes , we have that bad stall .
*/
2004-11-10 17:56:45 +01:00
return
2004-11-14 17:45:37 +01:00
( mysql_query_with_error_report ( mysql_con , 0 , " FLUSH TABLES " ) | |
mysql_query_with_error_report ( mysql_con , 0 ,
" FLUSH TABLES WITH READ LOCK " ) ) ;
2004-11-10 17:56:45 +01:00
}
static int do_unlock_tables ( MYSQL * mysql_con )
{
return mysql_query_with_error_report ( mysql_con , 0 , " UNLOCK TABLES " ) ;
}
static int do_reset_master ( MYSQL * mysql_con )
{
return mysql_query_with_error_report ( mysql_con , 0 , " RESET MASTER " ) ;
}
static int start_transaction ( MYSQL * mysql_con , my_bool consistent_read_now )
{
/*
We use BEGIN for old servers . - - single - transaction - - master - data will fail
on old servers , but that ' s ok as it was already silently broken ( it didn ' t
do a consistent read , so better tell people frankly , with the error ) .
2005-01-13 00:44:13 +01:00
We want the first consistent read to be used for all tables to dump so we
need the REPEATABLE READ level ( not anything lower , for example READ
COMMITTED would give one new consistent read per dumped table ) .
2004-11-10 17:56:45 +01:00
*/
return ( mysql_query_with_error_report ( mysql_con , 0 ,
2005-01-13 00:44:13 +01:00
" SET SESSION TRANSACTION ISOLATION "
" LEVEL REPEATABLE READ " ) | |
mysql_query_with_error_report ( mysql_con , 0 ,
2004-11-10 17:56:45 +01:00
consistent_read_now ?
" START TRANSACTION "
" WITH CONSISTENT SNAPSHOT " :
" BEGIN " ) ) ;
}
2003-01-16 04:35:59 +01:00
static ulong find_set ( TYPELIB * lib , const char * x , uint length ,
char * * err_pos , uint * err_len )
{
const char * end = x + length ;
ulong found = 0 ;
uint find ;
char buff [ 255 ] ;
2003-03-06 13:51:37 +01:00
* err_pos = 0 ; /* No error yet */
2003-03-16 08:20:45 +01:00
while ( end > x & & my_isspace ( charset_info , end [ - 1 ] ) )
2003-01-16 04:35:59 +01:00
end - - ;
* err_len = 0 ;
if ( x ! = end )
{
const char * start = x ;
for ( ; ; )
{
const char * pos = start ;
uint var_len ;
for ( ; pos ! = end & & * pos ! = ' , ' ; pos + + ) ;
var_len = ( uint ) ( pos - start ) ;
strmake ( buff , start , min ( sizeof ( buff ) , var_len ) ) ;
find = find_type ( buff , lib , var_len ) ;
if ( ! find )
{
* err_pos = ( char * ) start ;
* err_len = var_len ;
}
else
found | = ( ( longlong ) 1 < < ( find - 1 ) ) ;
if ( pos = = end )
break ;
start = pos + 1 ;
}
}
return found ;
}
2000-07-31 21:29:14 +02:00
/* Print a value with a prefix on file */
static void print_value ( FILE * file , MYSQL_RES * result , MYSQL_ROW row ,
const char * prefix , const char * name ,
int string_value )
{
MYSQL_FIELD * field ;
mysql_field_seek ( result , 0 ) ;
for ( ; ( field = mysql_fetch_field ( result ) ) ; row + + )
{
if ( ! strcmp ( field - > name , name ) )
{
if ( row [ 0 ] & & row [ 0 ] [ 0 ] & & strcmp ( row [ 0 ] , " 0 " ) ) /* Skip default */
{
fputc ( ' ' , file ) ;
fputs ( prefix , file ) ;
if ( string_value )
2000-08-21 23:18:32 +02:00
unescape ( file , row [ 0 ] , ( uint ) strlen ( row [ 0 ] ) ) ;
2000-07-31 21:29:14 +02:00
else
fputs ( row [ 0 ] , file ) ;
2004-06-24 22:14:12 +02:00
check_io ( file ) ;
2000-07-31 21:29:14 +02:00
return ;
}
}
}
return ; /* This shouldn't happen */
} /* print_value */
2003-12-14 05:39:52 +01:00
/*
Check if we the table is one of the table types that should be ignored :
MRG_ISAM , MRG_MYISAM
SYNOPSIS
check_if_ignore_table ( )
table_name Table name to check
GLOBAL VARIABLES
sock MySQL socket
verbose Write warning messages
RETURN
0 Table should be backuped
# Type of table (that should be skipped)
*/
static const char * check_if_ignore_table ( const char * table_name )
{
char buff [ FN_REFLEN + 80 ] , show_name_buff [ FN_REFLEN ] ;
MYSQL_RES * res ;
MYSQL_ROW row ;
const char * result = 0 ;
sprintf ( buff , " show table status like %s " ,
quote_for_like ( table_name , show_name_buff ) ) ;
2004-11-10 17:56:45 +01:00
if ( mysql_query_with_error_report ( sock , & res , buff ) )
2003-12-14 05:39:52 +01:00
{
if ( mysql_errno ( sock ) ! = ER_PARSE_ERROR )
{ /* If old MySQL version */
if ( verbose )
fprintf ( stderr ,
" -- Warning: Couldn't get status information for table %s (%s) \n " ,
table_name , mysql_error ( sock ) ) ;
return 0 ; /* assume table is ok */
}
}
2004-11-10 17:56:45 +01:00
if ( ! ( row = mysql_fetch_row ( res ) ) )
2003-12-14 05:39:52 +01:00
{
fprintf ( stderr ,
" Error: Couldn't read status information for table %s (%s) \n " ,
table_name , mysql_error ( sock ) ) ;
2005-01-24 15:48:25 +01:00
mysql_free_result ( res ) ;
2003-12-14 05:39:52 +01:00
return 0 ; /* assume table is ok */
}
if ( strcmp ( row [ 1 ] , ( result = " MRG_MyISAM " ) ) & &
strcmp ( row [ 1 ] , ( result = " MRG_ISAM " ) ) )
result = 0 ;
2005-01-24 15:48:25 +01:00
mysql_free_result ( res ) ;
2003-12-14 05:39:52 +01:00
return result ;
}
2004-11-16 23:45:24 +01:00
/*
Get string of comma - separated primary key field names
SYNOPSIS
char * primary_key_fields ( const char * table_name )
RETURNS pointer to allocated buffer ( must be freed by caller )
table_name quoted table name
DESCRIPTION
Use SHOW KEYS FROM table_name , allocate a buffer to hold the
field names , and then build that string and return the pointer
to that buffer .
Returns NULL if there is no PRIMARY or UNIQUE key on the table ,
or if there is some failure . It is better to continue to dump
the table unsorted , rather than exit without dumping the data .
*/
static char * primary_key_fields ( const char * table_name )
{
MYSQL_RES * res = NULL ;
MYSQL_ROW row ;
/* SHOW KEYS FROM + table name * 2 (escaped) + 2 quotes + \0 */
char show_keys_buff [ 15 + 64 * 2 + 3 ] ;
2004-11-30 02:26:24 +01:00
uint result_length = 0 ;
2004-11-16 23:45:24 +01:00
char * result = 0 ;
sprintf ( show_keys_buff , " SHOW KEYS FROM %s " , table_name ) ;
if ( mysql_query ( sock , show_keys_buff ) | |
! ( res = mysql_store_result ( sock ) ) )
{
fprintf ( stderr , " Warning: Couldn't read keys from table %s; "
" records are NOT sorted (%s) \n " ,
table_name , mysql_error ( sock ) ) ;
/* Don't exit, because it's better to print out unsorted records */
goto cleanup ;
}
2004-11-30 02:26:24 +01:00
/*
* Figure out the length of the ORDER BY clause result .
* Note that SHOW KEYS is ordered : a PRIMARY key is always the first
* row , and UNIQUE keys come before others . So we only need to check
* the first key , not all keys .
*/
if ( ( row = mysql_fetch_row ( res ) ) & & atoi ( row [ 1 ] ) = = 0 )
2004-11-16 23:45:24 +01:00
{
2004-11-30 02:26:24 +01:00
/* Key is unique */
do
result_length + = strlen ( row [ 4 ] ) + 1 ; /* + 1 for ',' or \0 */
while ( ( row = mysql_fetch_row ( res ) ) & & atoi ( row [ 3 ] ) > 1 ) ;
2004-11-16 23:45:24 +01:00
}
/* Build the ORDER BY clause result */
if ( result_length ) {
char * end ;
/* result (terminating \0 is already in result_length) */
result = my_malloc ( result_length + 10 , MYF ( MY_WME ) ) ;
if ( ! result ) {
fprintf ( stderr , " Error: Not enough memory to store ORDER BY clause \n " ) ;
goto cleanup ;
}
2004-11-30 02:26:24 +01:00
mysql_data_seek ( res , 0 ) ;
2004-11-16 23:45:24 +01:00
row = mysql_fetch_row ( res ) ;
end = strmov ( result , row [ 4 ] ) ;
while ( ( row = mysql_fetch_row ( res ) ) & & atoi ( row [ 3 ] ) > 1 )
end = strxmov ( end , " , " , row [ 4 ] , NullS ) ;
}
cleanup :
if ( res )
mysql_free_result ( res ) ;
return result ;
}
2003-12-14 05:39:52 +01:00
2000-07-31 21:29:14 +02:00
int main ( int argc , char * * argv )
{
2004-03-11 15:46:27 +01:00
compatible_mode_normal_str [ 0 ] = 0 ;
2005-01-06 18:13:58 +01:00
default_charset = ( char * ) mysql_universal_client_charset ;
2001-10-04 19:43:06 +02:00
2000-07-31 21:29:14 +02:00
MY_INIT ( argv [ 0 ] ) ;
if ( get_options ( & argc , & argv ) )
{
my_end ( 0 ) ;
exit ( EX_USAGE ) ;
}
2000-11-13 22:55:10 +01:00
if ( dbConnect ( current_host , current_user , opt_password ) )
2000-07-31 21:29:14 +02:00
exit ( EX_MYSQLERR ) ;
if ( ! path )
2002-01-02 20:29:41 +01:00
write_header ( md_result_file , * argv ) ;
2004-11-10 17:56:45 +01:00
if ( ( opt_lock_all_tables | | opt_master_data ) & &
do_flush_tables_read_lock ( sock ) )
goto err ;
if ( opt_single_transaction & & start_transaction ( sock , test ( opt_master_data ) ) )
goto err ;
if ( opt_delete_master_logs & & do_reset_master ( sock ) )
goto err ;
if ( opt_lock_all_tables | | opt_master_data )
2002-06-14 13:14:30 +02:00
{
2004-11-10 17:56:45 +01:00
if ( flush_logs & & mysql_refresh ( sock , REFRESH_LOG ) )
goto err ;
flush_logs = 0 ; /* not anymore; that would not be sensible */
2002-06-14 13:14:30 +02:00
}
2004-11-10 17:56:45 +01:00
if ( opt_master_data & & do_show_master_status ( sock ) )
goto err ;
2004-11-16 00:10:32 +01:00
if ( opt_single_transaction & & do_unlock_tables ( sock ) ) /* unlock but no commit! */
2004-11-10 17:56:45 +01:00
goto err ;
2000-07-31 21:29:14 +02:00
if ( opt_alldbs )
dump_all_databases ( ) ;
else if ( argc > 1 & & ! opt_databases )
2002-06-11 10:20:31 +02:00
{
/* Only one database and selected table(s) */
2000-07-31 21:29:14 +02:00
dump_selected_tables ( * argv , ( argv + 1 ) , ( argc - 1 ) ) ;
2002-06-11 10:20:31 +02:00
}
2000-09-07 03:55:17 +02:00
else
2002-06-11 10:20:31 +02:00
{
/* One or more databases, all tables */
2000-07-31 21:29:14 +02:00
dump_databases ( argv ) ;
2002-06-11 10:20:31 +02:00
}
2002-11-14 20:16:30 +01:00
# ifdef HAVE_SMEM
my_free ( shared_memory_base_name , MYF ( MY_ALLOW_ZERO_PTR ) ) ;
# endif
2004-11-10 17:56:45 +01:00
/*
No reason to explicitely COMMIT the transaction , neither to explicitely
UNLOCK TABLES : these will be automatically be done by the server when we
disconnect now . Saves some code here , some network trips , adds nothing to
server .
*/
err :
2000-07-31 21:29:14 +02:00
dbDisconnect ( current_host ) ;
2004-02-13 22:21:46 +01:00
if ( ! path )
write_footer ( md_result_file ) ;
2001-08-22 00:45:07 +02:00
if ( md_result_file ! = stdout )
my_fclose ( md_result_file , MYF ( 0 ) ) ;
2000-11-13 22:55:10 +01:00
my_free ( opt_password , MYF ( MY_ALLOW_ZERO_PTR ) ) ;
2003-10-14 12:19:54 +02:00
if ( extended_insert )
2000-07-31 21:29:14 +02:00
dynstr_free ( & extended_row ) ;
my_end ( 0 ) ;
return ( first_error ) ;
} /* main */