~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/drizzledump.cc

  • Committer: Brian Aker
  • Date: 2008-10-06 06:47:29 UTC
  • Revision ID: brian@tangent.org-20081006064729-2i9mhjkzyvow9xsm
Remove uint.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright 2000-2008 MySQL AB, 2008, 2009 Sun Microsystems, Inc.
2
 
 
3
 
  This program is free software; you can redistribute it and/or modify
4
 
  it under the terms of the GNU General Public License as published by
5
 
  the Free Software Foundation; version 2 of the License.
6
 
 
7
 
  This program is distributed in the hope that it will be useful,
8
 
  but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 
  GNU General Public License for more details.
11
 
 
12
 
  You should have received a copy of the GNU General Public License
13
 
  along with this program; if not, write to the Free Software
14
 
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
1
/* Copyright (C) 2008 Drizzle Open Source Development Project
 
2
 
 
3
   This program is free software; you can redistribute it and/or modify
 
4
   it under the terms of the GNU General Public License as published by
 
5
   the Free Software Foundation; version 2 of the License.
 
6
 
 
7
   This program is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
   GNU General Public License for more details.
 
11
 
 
12
   You should have received a copy of the GNU General Public License
 
13
   along with this program; if not, write to the Free Software
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
16
/* drizzledump.cc  - Dump a tables contents and format to an ASCII file
17
 
 
18
 
 * Derived from mysqldump, which originally came from:
19
 
 **
20
 
 ** The author's original notes follow :-
21
 
 **
22
 
 ** AUTHOR: Igor Romanenko (igor@frog.kiev.ua)
23
 
 ** DATE:   December 3, 1994
24
 
 ** WARRANTY: None, expressed, impressed, implied
25
 
 **          or other
26
 
 ** STATUS: Public domain
27
 
 
28
 
 * and more work by Monty, Jani & Sinisa
29
 
 * and all the MySQL developers over the years.
 
17
**
 
18
** The author's original notes follow :-
 
19
**
 
20
** AUTHOR: Igor Romanenko (igor@frog.kiev.ua)
 
21
** DATE:   December 3, 1994
 
22
** WARRANTY: None, expressed, impressed, implied
 
23
**          or other
 
24
** STATUS: Public domain
30
25
*/
31
26
 
 
27
#define DUMP_VERSION "10.13"
 
28
 
 
29
#include "config.h"
 
30
#include <string>
32
31
#include "client_priv.h"
33
 
#include <string>
34
32
 
35
 
#include "drizzled/internal/my_sys.h"
36
 
#include "drizzled/internal/m_string.h"
37
 
#include "drizzled/charset_info.h"
38
 
#include "drizzled/hash.h"
 
33
#include <mysys/my_sys.h>
 
34
#include <mystrings/m_string.h>
 
35
#include <mystrings/m_ctype.h>
 
36
#include <mysys/hash.h>
39
37
#include <stdarg.h>
40
 
#include <algorithm>
41
 
 
42
 
#include <drizzled/gettext.h>
43
38
 
44
39
#include <drizzled/error.h>
45
40
 
46
41
using namespace std;
47
 
using namespace drizzled;
48
 
 
49
42
/* Exit codes */
50
43
 
51
44
#define EX_USAGE 1
54
47
#define EX_EOM 4
55
48
#define EX_EOF 5 /* ferror for output file was got */
56
49
#define EX_ILLEGAL_TABLE 6
57
 
#define EX_TABLE_STATUS 7
58
50
 
59
51
/* index into 'show fields from table' */
60
52
 
66
58
 
67
59
/* Size of buffer for dump's select query */
68
60
#define QUERY_LENGTH 1536
69
 
#define DRIZZLE_MAX_LINE_LENGTH 1024*1024L-1025
70
61
 
71
62
/* ignore table flags */
72
63
#define IGNORE_NONE 0x00 /* no ignore */
75
66
 
76
67
static void add_load_option(string &str, const char *option,
77
68
                            const char *option_value);
78
 
static uint32_t find_set(TYPELIB *lib, const char *x, uint32_t length,
79
 
                         char **err_pos, uint32_t *err_len);
 
69
static uint32_t find_set(TYPELIB *lib, const char *x, uint length,
 
70
                      char **err_pos, uint *err_len);
80
71
 
81
72
static void field_escape(string &in, const char *from);
82
 
static bool  verbose= false;
83
 
static bool opt_no_create_info= false;
84
 
static bool opt_no_data= false;
85
 
static bool opt_mysql= false;
86
 
static bool quick= true;
87
 
static bool extended_insert= true;
88
 
static bool ignore_errors= false;
89
 
static bool flush_logs= false;
90
 
static bool opt_drop= true; 
91
 
static bool opt_keywords= false;
92
 
static bool opt_compress= false;
93
 
static bool opt_delayed= false; 
94
 
static bool create_options= true; 
95
 
static bool opt_quoted= false;
96
 
static bool opt_databases= false; 
97
 
static bool opt_alldbs= false; 
98
 
static bool opt_create_db= false;
99
 
static bool opt_lock_all_tables= false;
100
 
static bool opt_set_charset= false; 
101
 
static bool opt_dump_date= true;
102
 
static bool opt_autocommit= false; 
103
 
static bool opt_disable_keys= true;
104
 
static bool opt_xml= false;
105
 
static bool tty_password= false;
106
 
static bool opt_single_transaction= false; 
107
 
static bool opt_comments= false;
108
 
static bool opt_compact= false;
109
 
static bool opt_hex_blob= false;
110
 
static bool opt_order_by_primary=false; 
111
 
static bool opt_ignore= false;
112
 
static bool opt_complete_insert= false;
113
 
static bool opt_drop_database= false;
114
 
static bool opt_replace_into= false;
115
 
static bool opt_routines= false;
116
 
static bool opt_alltspcs= false;
117
 
static uint32_t show_progress_size= 0;
118
 
static uint64_t total_rows= 0;
119
 
static drizzle_st drizzle;
120
 
static drizzle_con_st dcon;
 
73
static bool  verbose= 0, opt_no_create_info= 0, opt_no_data= 0,
 
74
                quick= 1, extended_insert= 1,
 
75
                lock_tables=1,ignore_errors=0,flush_logs=0,flush_privileges=0,
 
76
                opt_drop=1,opt_keywords=0,opt_lock=1,opt_compress=0,
 
77
                opt_delayed=0,create_options=1,opt_quoted=0,opt_databases=0,
 
78
                opt_alldbs=0,opt_create_db=0,opt_lock_all_tables=0,
 
79
                opt_set_charset=0, opt_dump_date=1,
 
80
                opt_autocommit=0,opt_disable_keys=1,opt_xml=0,
 
81
                opt_delete_master_logs=0, tty_password=0,
 
82
                opt_single_transaction=0, opt_comments= 0, opt_compact= 0,
 
83
                opt_hex_blob=0, opt_order_by_primary=0, opt_ignore=0,
 
84
                opt_complete_insert= 0, opt_drop_database= 0,
 
85
                opt_replace_into= 0,
 
86
                opt_routines=0, opt_tz_utc=1,
 
87
                opt_slave_apply= 0, 
 
88
                opt_include_master_host_port= 0,
 
89
                opt_events= 0,
 
90
                opt_alltspcs=0, opt_notspcs= 0;
 
91
static bool debug_info_flag= 0, debug_check_flag= 0;
 
92
static uint32_t opt_max_allowed_packet, opt_net_buffer_length;
 
93
static DRIZZLE drizzle_connection,*drizzle=0;
121
94
static string insert_pat;
122
 
static char  *opt_password= NULL;
123
 
static char *current_user= NULL;
124
 
static char  *current_host= NULL;
125
 
static char *path= NULL;
126
 
static char *fields_terminated= NULL;
127
 
static char *lines_terminated= NULL; 
128
 
static char *enclosed= NULL;
129
 
static char *opt_enclosed= NULL;
130
 
static char *escaped= NULL;
131
 
static char *where= NULL; 
132
 
static char *order_by= NULL;
133
 
static char *opt_compatible_mode_str= NULL;
134
 
static char *err_ptr= NULL;
135
 
static char **defaults_argv= NULL;
 
95
static char  *opt_password=0,*current_user=0,
 
96
             *current_host=0,*path=0,*fields_terminated=0,
 
97
             *lines_terminated=0, *enclosed=0, *opt_enclosed=0, *escaped=0,
 
98
             *where=0, *order_by=0,
 
99
             *opt_compatible_mode_str= 0,
 
100
             *err_ptr= 0,
 
101
             *log_error_file= NULL;
 
102
static char **defaults_argv= 0;
136
103
static char compatible_mode_normal_str[255];
 
104
/* Server supports character_set_results session variable? */
 
105
static bool server_supports_switching_charsets= true;
137
106
static uint32_t opt_compatible_mode= 0;
138
 
static uint32_t opt_drizzle_port= 0;
139
 
static int first_error= 0;
 
107
#define DRIZZLE_OPT_MASTER_DATA_EFFECTIVE_SQL 1
 
108
#define DRIZZLE_OPT_MASTER_DATA_COMMENTED_SQL 2
 
109
#define DRIZZLE_OPT_SLAVE_DATA_EFFECTIVE_SQL 1
 
110
#define DRIZZLE_OPT_SLAVE_DATA_COMMENTED_SQL 2
 
111
static uint opt_drizzle_port= 0, opt_master_data;
 
112
static uint opt_slave_data;
 
113
static uint my_end_arg;
 
114
static int   first_error=0;
140
115
static string extended_row;
141
116
FILE *md_result_file= 0;
142
 
FILE *stderror_file= 0;
 
117
FILE *stderror_file=0;
143
118
 
 
119
/*
 
120
  Constant for detection of default value of default_charset.
 
121
  If default_charset is equal to drizzle_universal_client_charset, then
 
122
  it is the default value which assigned at the very beginning of main().
 
123
*/
 
124
static const char *drizzle_universal_client_charset=
 
125
  DRIZZLE_UNIVERSAL_CLIENT_CHARSET;
 
126
static char *default_charset;
144
127
static const CHARSET_INFO *charset_info= &my_charset_utf8_general_ci;
145
 
 
146
 
static const char *compatible_mode_names[]=
 
128
const char *default_dbug_option="d:t:o,/tmp/drizzledump.trace";
 
129
/* have we seen any VIEWs during table scanning? */
 
130
bool seen_views= 0;
 
131
const char *compatible_mode_names[]=
147
132
{
148
133
  "MYSQL323", "MYSQL40", "POSTGRESQL", "ORACLE", "MSSQL", "DB2",
149
134
  "MAXDB", "NO_KEY_OPTIONS", "NO_TABLE_OPTIONS", "NO_FIELD_OPTIONS",
150
135
  "ANSI",
151
136
  NULL
152
137
};
153
 
static TYPELIB compatible_mode_typelib= {array_elements(compatible_mode_names) - 1,
154
 
  "", compatible_mode_names, NULL};
155
 
 
156
 
drizzled::hash_set<string> ignore_table;
157
 
 
158
 
static struct option my_long_options[] =
 
138
#define MASK_ANSI_QUOTES \
 
139
(\
 
140
 (1<<2)  | /* POSTGRESQL */\
 
141
 (1<<3)  | /* ORACLE     */\
 
142
 (1<<4)  | /* MSSQL      */\
 
143
 (1<<5)  | /* DB2        */\
 
144
 (1<<6)  | /* MAXDB      */\
 
145
 (1<<10)   /* ANSI       */\
 
146
)
 
147
TYPELIB compatible_mode_typelib= {array_elements(compatible_mode_names) - 1,
 
148
                                  "", compatible_mode_names, NULL};
 
149
 
 
150
HASH ignore_table;
 
151
 
 
152
static struct my_option my_long_options[] =
159
153
{
160
154
  {"all", 'a', "Deprecated. Use --create-options instead.",
161
 
    (char**) &create_options, (char**) &create_options, 0, GET_BOOL, NO_ARG, 1,
162
 
    0, 0, 0, 0, 0},
 
155
   (char**) &create_options, (char**) &create_options, 0, GET_BOOL, NO_ARG, 1,
 
156
   0, 0, 0, 0, 0},
163
157
  {"all-databases", 'A',
164
 
    "Dump all the databases. This will be same as --databases with all databases selected.",
165
 
    (char**) &opt_alldbs, (char**) &opt_alldbs, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0,
166
 
    0, 0},
 
158
   "Dump all the databases. This will be same as --databases with all databases selected.",
 
159
   (char**) &opt_alldbs, (char**) &opt_alldbs, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0,
 
160
   0, 0},
167
161
  {"all-tablespaces", 'Y',
168
 
    "Dump all the tablespaces.",
169
 
    (char**) &opt_alltspcs, (char**) &opt_alltspcs, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0,
170
 
    0, 0},
 
162
   "Dump all the tablespaces.",
 
163
   (char**) &opt_alltspcs, (char**) &opt_alltspcs, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0,
 
164
   0, 0},
 
165
  {"no-tablespaces", 'y',
 
166
   "Do not dump any tablespace information.",
 
167
   (char**) &opt_notspcs, (char**) &opt_notspcs, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0,
 
168
   0, 0},
171
169
  {"add-drop-database", OPT_DROP_DATABASE, "Add a 'DROP DATABASE' before each create.",
172
 
    (char**) &opt_drop_database, (char**) &opt_drop_database, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0,
173
 
    0},
 
170
   (char**) &opt_drop_database, (char**) &opt_drop_database, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0,
 
171
   0},
174
172
  {"add-drop-table", OPT_DROP, "Add a 'drop table' before each create.",
175
 
    (char**) &opt_drop, (char**) &opt_drop, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0, 0,
176
 
    0},
 
173
   (char**) &opt_drop, (char**) &opt_drop, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0, 0,
 
174
   0},
 
175
  {"add-locks", OPT_LOCKS, "Add locks around insert statements.",
 
176
   (char**) &opt_lock, (char**) &opt_lock, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0, 0,
 
177
   0},
177
178
  {"allow-keywords", OPT_KEYWORDS,
178
 
    "Allow creation of column names that are keywords.", (char**) &opt_keywords,
179
 
    (char**) &opt_keywords, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
179
   "Allow creation of column names that are keywords.", (char**) &opt_keywords,
 
180
   (char**) &opt_keywords, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
181
  {"apply-slave-statements", OPT_DRIZZLEDUMP_SLAVE_APPLY,
 
182
   "Adds 'STOP SLAVE' prior to 'CHANGE MASTER' and 'START SLAVE' to bottom of dump.",
 
183
   (char**) &opt_slave_apply, (char**) &opt_slave_apply, 0, GET_BOOL, NO_ARG,
 
184
   0, 0, 0, 0, 0, 0},
 
185
  {"character-sets-dir", OPT_CHARSETS_DIR,
 
186
   "Directory where character sets are.", (char**) &charsets_dir,
 
187
   (char**) &charsets_dir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
180
188
  {"comments", 'i', "Write additional information.",
181
 
    (char**) &opt_comments, (char**) &opt_comments, 0, GET_BOOL, NO_ARG,
182
 
    1, 0, 0, 0, 0, 0},
 
189
   (char**) &opt_comments, (char**) &opt_comments, 0, GET_BOOL, NO_ARG,
 
190
   1, 0, 0, 0, 0, 0},
183
191
  {"compatible", OPT_COMPATIBLE,
184
 
    "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 DRIZZLE server version 4.1.0 or higher. This option is ignored with earlier server versions.",
185
 
    (char**) &opt_compatible_mode_str, (char**) &opt_compatible_mode_str, 0,
186
 
    GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
192
   "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 DRIZZLE server version 4.1.0 or higher. This option is ignored with earlier server versions.",
 
193
   (char**) &opt_compatible_mode_str, (char**) &opt_compatible_mode_str, 0,
 
194
   GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
187
195
  {"compact", OPT_COMPACT,
188
 
    "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",
189
 
    (char**) &opt_compact, (char**) &opt_compact, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0,
190
 
    0, 0},
 
196
   "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",
 
197
   (char**) &opt_compact, (char**) &opt_compact, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0,
 
198
   0, 0},
191
199
  {"complete-insert", 'c', "Use complete insert statements.",
192
 
    (char**) &opt_complete_insert, (char**) &opt_complete_insert, 0, GET_BOOL,
193
 
    NO_ARG, 0, 0, 0, 0, 0, 0},
 
200
   (char**) &opt_complete_insert, (char**) &opt_complete_insert, 0, GET_BOOL,
 
201
   NO_ARG, 0, 0, 0, 0, 0, 0},
194
202
  {"compress", 'C', "Use compression in server/client protocol.",
195
 
    (char**) &opt_compress, (char**) &opt_compress, 0, GET_BOOL, NO_ARG, 0, 0, 0,
196
 
    0, 0, 0},
 
203
   (char**) &opt_compress, (char**) &opt_compress, 0, GET_BOOL, NO_ARG, 0, 0, 0,
 
204
   0, 0, 0},
197
205
  {"create-options", OPT_CREATE_OPTIONS,
198
 
    "Include all DRIZZLE specific create options.",
199
 
    (char**) &create_options, (char**) &create_options, 0, GET_BOOL, NO_ARG, 1,
200
 
    0, 0, 0, 0, 0},
 
206
   "Include all DRIZZLE specific create options.",
 
207
   (char**) &create_options, (char**) &create_options, 0, GET_BOOL, NO_ARG, 1,
 
208
   0, 0, 0, 0, 0},
201
209
  {"databases", 'B',
202
 
    "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.",
203
 
    (char**) &opt_databases, (char**) &opt_databases, 0, GET_BOOL, NO_ARG, 0, 0,
204
 
    0, 0, 0, 0},
 
210
   "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.",
 
211
   (char**) &opt_databases, (char**) &opt_databases, 0, GET_BOOL, NO_ARG, 0, 0,
 
212
   0, 0, 0, 0},
 
213
  {"debug-check", OPT_DEBUG_CHECK, "Check memory and open file usage at exit.",
 
214
   (char**) &debug_check_flag, (char**) &debug_check_flag, 0,
 
215
   GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
216
  {"debug-info", OPT_DEBUG_INFO, "Print some debug info at exit.",
 
217
   (char**) &debug_info_flag, (char**) &debug_info_flag,
 
218
   0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
219
  {"default-character-set", OPT_DEFAULT_CHARSET,
 
220
   "Set the default character set.", (char**) &default_charset,
 
221
   (char**) &default_charset, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
205
222
  {"delayed-insert", OPT_DELAYED, "Insert rows with INSERT DELAYED; ",
206
 
    (char**) &opt_delayed, (char**) &opt_delayed, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0,
207
 
    0, 0},
 
223
   (char**) &opt_delayed, (char**) &opt_delayed, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0,
 
224
   0, 0},
 
225
  {"delete-master-logs", OPT_DELETE_MASTER_LOGS,
 
226
   "Delete logs on master after backup. This automatically enables --master-data.",
 
227
   (char**) &opt_delete_master_logs, (char**) &opt_delete_master_logs, 0,
 
228
   GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
208
229
  {"disable-keys", 'K',
209
 
    "'ALTER TABLE tb_name DISABLE KEYS; and 'ALTER TABLE tb_name ENABLE KEYS; will be put in the output.", (char**) &opt_disable_keys,
210
 
    (char**) &opt_disable_keys, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0, 0, 0},
 
230
   "'/*!40000 ALTER TABLE tb_name DISABLE KEYS */; and '/*!40000 ALTER TABLE tb_name ENABLE KEYS */; will be put in the output.", (char**) &opt_disable_keys,
 
231
   (char**) &opt_disable_keys, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0, 0, 0},
 
232
  {"dump-slave", OPT_DRIZZLEDUMP_SLAVE_DATA,
 
233
   "This causes the binary log position and filename of the master to be "
 
234
   "appended to the dumped data output. Setting the value to 1, will print"
 
235
   "it as a CHANGE MASTER command in the dumped data output; if equal"
 
236
   " to 2, that command will be prefixed with a comment symbol. "
 
237
   "This option will turn --lock-all-tables on, unless "
 
238
   "--single-transaction is specified too (in which case a "
 
239
   "global read lock is only taken a short time at the beginning of the dump "
 
240
   "- don't forget to read about --single-transaction below). In all cases "
 
241
   "any action on logs will happen at the exact moment of the dump."
 
242
   "Option automatically turns --lock-tables off.",
 
243
   (char**) &opt_slave_data, (char**) &opt_slave_data, 0,
 
244
   GET_UINT, OPT_ARG, 0, 0, DRIZZLE_OPT_SLAVE_DATA_COMMENTED_SQL, 0, 0, 0},
 
245
  {"events", 'E', "Dump events.",
 
246
     (char**) &opt_events, (char**) &opt_events, 0, GET_BOOL,
 
247
     NO_ARG, 0, 0, 0, 0, 0, 0},
211
248
  {"extended-insert", 'e',
212
 
    "Allows utilization of the new, much faster INSERT syntax.",
213
 
    (char**) &extended_insert, (char**) &extended_insert, 0, GET_BOOL, NO_ARG,
214
 
    1, 0, 0, 0, 0, 0},
 
249
   "Allows utilization of the new, much faster INSERT syntax.",
 
250
   (char**) &extended_insert, (char**) &extended_insert, 0, GET_BOOL, NO_ARG,
 
251
   1, 0, 0, 0, 0, 0},
215
252
  {"fields-terminated-by", OPT_FTB,
216
 
    "Fields in the textfile are terminated by ...", (char**) &fields_terminated,
217
 
    (char**) &fields_terminated, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
253
   "Fields in the textfile are terminated by ...", (char**) &fields_terminated,
 
254
   (char**) &fields_terminated, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
218
255
  {"fields-enclosed-by", OPT_ENC,
219
 
    "Fields in the importfile are enclosed by ...", (char**) &enclosed,
220
 
    (char**) &enclosed, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0 ,0, 0},
 
256
   "Fields in the importfile are enclosed by ...", (char**) &enclosed,
 
257
   (char**) &enclosed, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0 ,0, 0},
221
258
  {"fields-optionally-enclosed-by", OPT_O_ENC,
222
 
    "Fields in the i.file are opt. enclosed by ...", (char**) &opt_enclosed,
223
 
    (char**) &opt_enclosed, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0 ,0, 0},
 
259
   "Fields in the i.file are opt. enclosed by ...", (char**) &opt_enclosed,
 
260
   (char**) &opt_enclosed, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0 ,0, 0},
224
261
  {"fields-escaped-by", OPT_ESC, "Fields in the i.file are escaped by ...",
225
 
    (char**) &escaped, (char**) &escaped, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
262
   (char**) &escaped, (char**) &escaped, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
263
  {"first-slave", 'x', "Deprecated, renamed to --lock-all-tables.",
 
264
   (char**) &opt_lock_all_tables, (char**) &opt_lock_all_tables, 0, GET_BOOL, NO_ARG,
 
265
   0, 0, 0, 0, 0, 0},
226
266
  {"flush-logs", 'F', "Flush logs file in server before starting dump. "
227
 
    "Note that if you dump many databases at once (using the option "
228
 
      "--databases= or --all-databases), the logs will be flushed for "
229
 
      "each database dumped. The exception is when using --lock-all-tables "
230
 
      "in this case the logs will be flushed only once, corresponding "
231
 
      "to the moment all tables are locked. So if you want your dump and "
232
 
      "the log flush to happen at the same exact moment you should use "
233
 
      "--lock-all-tables or --flush-logs",
234
 
    (char**) &flush_logs, (char**) &flush_logs, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0,
235
 
    0, 0},
 
267
   "Note that if you dump many databases at once (using the option "
 
268
   "--databases= or --all-databases), the logs will be flushed for "
 
269
   "each database dumped. The exception is when using --lock-all-tables "
 
270
   "or --master-data: "
 
271
   "in this case the logs will be flushed only once, corresponding "
 
272
   "to the moment all tables are locked. So if you want your dump and "
 
273
   "the log flush to happen at the same exact moment you should use "
 
274
   "--lock-all-tables or --master-data with --flush-logs",
 
275
   (char**) &flush_logs, (char**) &flush_logs, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0,
 
276
   0, 0},
 
277
  {"flush-privileges", OPT_ESC, "Emit a FLUSH PRIVILEGES statement "
 
278
   "after dumping the DRIZZLE database.  This option should be used any "
 
279
   "time the dump contains the DRIZZLE database and any other database "
 
280
   "that depends on the data in the DRIZZLE database for proper restore. ",
 
281
   (char**) &flush_privileges, (char**) &flush_privileges, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0,
 
282
   0, 0},
236
283
  {"force", 'f', "Continue even if we get an sql-error.",
237
 
    (char**) &ignore_errors, (char**) &ignore_errors, 0, GET_BOOL, NO_ARG,
238
 
    0, 0, 0, 0, 0, 0},
 
284
   (char**) &ignore_errors, (char**) &ignore_errors, 0, GET_BOOL, NO_ARG,
 
285
   0, 0, 0, 0, 0, 0},
239
286
  {"help", '?', "Display this help message and exit.", 0, 0, 0, GET_NO_ARG,
240
 
    NO_ARG, 0, 0, 0, 0, 0, 0},
 
287
   NO_ARG, 0, 0, 0, 0, 0, 0},
241
288
  {"hex-blob", OPT_HEXBLOB, "Dump binary strings (BINARY, "
242
289
    "VARBINARY, BLOB) in hexadecimal format.",
243
 
    (char**) &opt_hex_blob, (char**) &opt_hex_blob, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
290
   (char**) &opt_hex_blob, (char**) &opt_hex_blob, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
244
291
  {"host", 'h', "Connect to host.", (char**) &current_host,
245
 
    (char**) &current_host, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
292
   (char**) &current_host, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
246
293
  {"ignore-table", OPT_IGNORE_TABLE,
247
 
    "Do not dump the specified table. To specify more than one table to ignore, "
248
 
      "use the directive multiple times, once for each table.  Each table must "
249
 
      "be specified with both database and table names, e.g. --ignore-table=database.table",
250
 
    0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
294
   "Do not dump the specified table. To specify more than one table to ignore, "
 
295
   "use the directive multiple times, once for each table.  Each table must "
 
296
   "be specified with both database and table names, e.g. --ignore-table=database.table",
 
297
   0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
298
  {"include-master-host-port", OPT_DRIZZLEDUMP_INCLUDE_MASTER_HOST_PORT,
 
299
   "Adds 'MASTER_HOST=<host>, MASTER_PORT=<port>' to 'CHANGE MASTER TO..' in dump produced with --dump-slave.",
 
300
   (char**) &opt_include_master_host_port, 
 
301
   (char**) &opt_include_master_host_port, 
 
302
   0, GET_BOOL, NO_ARG,
 
303
   0, 0, 0, 0, 0, 0},
251
304
  {"insert-ignore", OPT_INSERT_IGNORE, "Insert rows with INSERT IGNORE.",
252
 
    (char**) &opt_ignore, (char**) &opt_ignore, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0,
253
 
    0, 0},
 
305
   (char**) &opt_ignore, (char**) &opt_ignore, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0,
 
306
   0, 0},
254
307
  {"lines-terminated-by", OPT_LTB, "Lines in the i.file are terminated by ...",
255
 
    (char**) &lines_terminated, (char**) &lines_terminated, 0, GET_STR,
256
 
    REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
308
   (char**) &lines_terminated, (char**) &lines_terminated, 0, GET_STR,
 
309
   REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
257
310
  {"lock-all-tables", 'x', "Locks all tables across all databases. This "
258
 
    "is achieved by taking a global read lock for the duration of the whole "
259
 
      "dump. Automatically turns --single-transaction and --lock-tables off.",
260
 
    (char**) &opt_lock_all_tables, (char**) &opt_lock_all_tables, 0, GET_BOOL, NO_ARG,
261
 
    0, 0, 0, 0, 0, 0},
262
 
  {"mysql", 'm', N_("Use MySQL Protocol."),
263
 
    (char**) &opt_mysql, (char**) &opt_mysql, 0, GET_BOOL, NO_ARG, 1, 0, 0,
264
 
    0, 0, 0},
 
311
   "is achieved by taking a global read lock for the duration of the whole "
 
312
   "dump. Automatically turns --single-transaction and --lock-tables off.",
 
313
   (char**) &opt_lock_all_tables, (char**) &opt_lock_all_tables, 0, GET_BOOL, NO_ARG,
 
314
   0, 0, 0, 0, 0, 0},
 
315
  {"lock-tables", 'l', "Lock all tables for read.", (char**) &lock_tables,
 
316
   (char**) &lock_tables, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0, 0, 0},
 
317
  {"log-error", OPT_ERROR_LOG_FILE, "Append warnings and errors to given file.",
 
318
   (char**) &log_error_file, (char**) &log_error_file, 0, GET_STR,
 
319
   REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
320
  {"master-data", OPT_MASTER_DATA,
 
321
   "This causes the binary log position and filename to be appended to the "
 
322
   "output. If equal to 1, will print it as a CHANGE MASTER command; if equal"
 
323
   " to 2, that command will be prefixed with a comment symbol. "
 
324
   "This option will turn --lock-all-tables on, unless "
 
325
   "--single-transaction is specified too (in which case a "
 
326
   "global read lock is only taken a short time at the beginning of the dump "
 
327
   "- don't forget to read about --single-transaction below). In all cases "
 
328
   "any action on logs will happen at the exact moment of the dump."
 
329
   "Option automatically turns --lock-tables off.",
 
330
   (char**) &opt_master_data, (char**) &opt_master_data, 0,
 
331
   GET_UINT, OPT_ARG, 0, 0, DRIZZLE_OPT_MASTER_DATA_COMMENTED_SQL, 0, 0, 0},
 
332
  {"max_allowed_packet", OPT_MAX_ALLOWED_PACKET, "",
 
333
    (char**) &opt_max_allowed_packet, (char**) &opt_max_allowed_packet, 0,
 
334
    GET_ULONG, REQUIRED_ARG, 24*1024*1024, 4096,
 
335
   (int64_t) 2L*1024L*1024L*1024L, MALLOC_OVERHEAD, 1024, 0},
 
336
  {"net_buffer_length", OPT_NET_BUFFER_LENGTH, "",
 
337
    (char**) &opt_net_buffer_length, (char**) &opt_net_buffer_length, 0,
 
338
    GET_ULONG, REQUIRED_ARG, 1024*1024L-1025, 4096, 16*1024L*1024L,
 
339
   MALLOC_OVERHEAD-1024, 1024, 0},
265
340
  {"no-autocommit", OPT_AUTOCOMMIT,
266
 
    "Wrap tables with autocommit/commit statements.",
267
 
    (char**) &opt_autocommit, (char**) &opt_autocommit, 0, GET_BOOL, NO_ARG,
268
 
    0, 0, 0, 0, 0, 0},
 
341
   "Wrap tables with autocommit/commit statements.",
 
342
   (char**) &opt_autocommit, (char**) &opt_autocommit, 0, GET_BOOL, NO_ARG,
 
343
   0, 0, 0, 0, 0, 0},
269
344
  {"no-create-db", 'n',
270
 
    "'CREATE DATABASE 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.}.",
271
 
    (char**) &opt_create_db, (char**) &opt_create_db, 0, GET_BOOL, NO_ARG, 0, 0,
272
 
    0, 0, 0, 0},
 
345
   "'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.}.",
 
346
   (char**) &opt_create_db, (char**) &opt_create_db, 0, GET_BOOL, NO_ARG, 0, 0,
 
347
   0, 0, 0, 0},
273
348
  {"no-create-info", 't', "Don't write table creation info.",
274
 
    (char**) &opt_no_create_info, (char**) &opt_no_create_info, 0, GET_BOOL,
275
 
    NO_ARG, 0, 0, 0, 0, 0, 0},
 
349
   (char**) &opt_no_create_info, (char**) &opt_no_create_info, 0, GET_BOOL,
 
350
   NO_ARG, 0, 0, 0, 0, 0, 0},
276
351
  {"no-data", 'd', "No row information.", (char**) &opt_no_data,
277
 
    (char**) &opt_no_data, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
352
   (char**) &opt_no_data, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
278
353
  {"no-set-names", 'N',
279
 
    "Deprecated. Use --skip-set-charset instead.",
280
 
    0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
354
   "Deprecated. Use --skip-set-charset instead.",
 
355
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
281
356
  {"opt", OPT_OPTIMIZE,
282
 
    "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.",
283
 
    0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
357
   "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.",
 
358
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
284
359
  {"order-by-primary", OPT_ORDER_BY_PRIMARY,
285
 
    "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.",
286
 
    (char**) &opt_order_by_primary, (char**) &opt_order_by_primary, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
287
 
  {"password", 'P',
288
 
    "Password to use when connecting to server. If password is not given it's solicited on the tty.",
289
 
    0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
290
 
  {"port", 'p', "Port number to use for connection.", 
291
 
    0, 0, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
360
   "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.",
 
361
   (char**) &opt_order_by_primary, (char**) &opt_order_by_primary, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
362
  {"password", 'p',
 
363
   "Password to use when connecting to server. If password is not given it's solicited on the tty.",
 
364
   0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
 
365
  {"port", 'P', "Port number to use for connection.", (char**) &opt_drizzle_port,
 
366
   (char**) &opt_drizzle_port, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0,
 
367
   0},
292
368
  {"quick", 'q', "Don't buffer query, dump directly to stdout.",
293
 
    (char**) &quick, (char**) &quick, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0, 0, 0},
 
369
   (char**) &quick, (char**) &quick, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0, 0, 0},
294
370
  {"quote-names",'Q', "Quote table and column names with backticks (`).",
295
 
    (char**) &opt_quoted, (char**) &opt_quoted, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0,
296
 
    0, 0},
 
371
   (char**) &opt_quoted, (char**) &opt_quoted, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0,
 
372
   0, 0},
297
373
  {"replace", OPT_DRIZZLE_REPLACE_INTO, "Use REPLACE INTO instead of INSERT INTO.",
298
 
    (char**) &opt_replace_into, (char**) &opt_replace_into, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0,
299
 
    0, 0},
 
374
   (char**) &opt_replace_into, (char**) &opt_replace_into, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0,
 
375
   0, 0},
300
376
  {"result-file", 'r',
301
 
    "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).",
302
 
    0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
377
   "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).",
 
378
   0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
303
379
  {"routines", 'R', "Dump stored routines (functions and procedures).",
304
 
    (char**) &opt_routines, (char**) &opt_routines, 0, GET_BOOL,
305
 
    NO_ARG, 0, 0, 0, 0, 0, 0},
 
380
     (char**) &opt_routines, (char**) &opt_routines, 0, GET_BOOL,
 
381
     NO_ARG, 0, 0, 0, 0, 0, 0},
 
382
  {"set-charset", OPT_SET_CHARSET,
 
383
   "Add 'SET NAMES default_character_set' to the output.",
 
384
   (char**) &opt_set_charset, (char**) &opt_set_charset, 0, GET_BOOL, NO_ARG, 1,
 
385
   0, 0, 0, 0, 0},
 
386
  {"set-variable", 'O',
 
387
   "Change the value of a variable. Please note that this option is deprecated; you can set variables directly with --variable-name=value.",
 
388
   0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
389
  /*
 
390
    Note that the combination --single-transaction --master-data
 
391
    will give bullet-proof binlog position only if server >=4.1.3. That's the
 
392
    old "FLUSH TABLES WITH READ LOCK does not block commit" fixed bug.
 
393
  */
306
394
  {"single-transaction", OPT_TRANSACTION,
307
 
    "Creates a consistent snapshot by dumping all tables in a single "
308
 
      "transaction. Works ONLY for tables stored in storage engines which "
309
 
      "support multiversioning (currently only InnoDB does); the dump is NOT "
310
 
      "guaranteed to be consistent for other storage engines. "
311
 
      "While a --single-transaction dump is in process, to ensure a valid "
312
 
      "dump file (correct table contents), no other "
313
 
      "connection should use the following statements: ALTER TABLE, DROP "
314
 
      "TABLE, RENAME TABLE, TRUNCATE TABLE, as consistent snapshot is not "
315
 
      "isolated from them. Option automatically turns off --lock-tables.",
316
 
    (char**) &opt_single_transaction, (char**) &opt_single_transaction, 0,
317
 
    GET_BOOL, NO_ARG,  0, 0, 0, 0, 0, 0},
 
395
   "Creates a consistent snapshot by dumping all tables in a single "
 
396
   "transaction. Works ONLY for tables stored in storage engines which "
 
397
   "support multiversioning (currently only InnoDB does); the dump is NOT "
 
398
   "guaranteed to be consistent for other storage engines. "
 
399
   "While a --single-transaction dump is in process, to ensure a valid "
 
400
   "dump file (correct table contents and binary log position), no other "
 
401
   "connection should use the following statements: ALTER TABLE, DROP "
 
402
   "TABLE, RENAME TABLE, TRUNCATE TABLE, as consistent snapshot is not "
 
403
   "isolated from them. Option automatically turns off --lock-tables.",
 
404
   (char**) &opt_single_transaction, (char**) &opt_single_transaction, 0,
 
405
   GET_BOOL, NO_ARG,  0, 0, 0, 0, 0, 0},
318
406
  {"dump-date", OPT_DUMP_DATE, "Put a dump date to the end of the output.",
319
 
    (char**) &opt_dump_date, (char**) &opt_dump_date, 0,
320
 
    GET_BOOL, NO_ARG, 1, 0, 0, 0, 0, 0},
 
407
   (char**) &opt_dump_date, (char**) &opt_dump_date, 0,
 
408
   GET_BOOL, NO_ARG, 1, 0, 0, 0, 0, 0},
321
409
  {"skip-opt", OPT_SKIP_OPTIMIZATION,
322
 
    "Disable --opt. Disables --add-drop-table, --add-locks, --create-options, --quick, --extended-insert, --lock-tables, --set-charset, and --disable-keys.",
323
 
    0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
410
   "Disable --opt. Disables --add-drop-table, --add-locks, --create-options, --quick, --extended-insert, --lock-tables, --set-charset, and --disable-keys.",
 
411
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
324
412
  {"tab",'T',
325
 
    "Creates tab separated textfile for each table to given path. (creates .sql and .txt files). NOTE: This only works if drizzledump is run on the same machine as the drizzled daemon.",
326
 
    (char**) &path, (char**) &path, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
413
   "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.",
 
414
   (char**) &path, (char**) &path, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
327
415
  {"tables", OPT_TABLES, "Overrides option --databases (-B).",
328
 
    0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
329
 
  {"show-progress-size", OPT_SHOW_PROGRESS_SIZE, N_("Number of rows before each output progress report (requires --verbose)."),
330
 
    (char**) &show_progress_size, (char**) &show_progress_size, 0, GET_UINT32, REQUIRED_ARG,
331
 
    10000, 0, 0, 0, 0, 0},
 
416
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
417
  {"tz-utc", OPT_TZ_UTC,
 
418
    "SET TIME_ZONE='+00:00' at top of dump to allow dumping of TIMESTAMP data when a server has data in different time zones or data is being moved between servers with different time zones.",
 
419
    (char**) &opt_tz_utc, (char**) &opt_tz_utc, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0, 0, 0},
 
420
#ifndef DONT_ALLOW_USER_CHANGE
332
421
  {"user", 'u', "User for login if not current user.",
333
 
    (char**) &current_user, (char**) &current_user, 0, GET_STR, REQUIRED_ARG,
334
 
    0, 0, 0, 0, 0, 0},
 
422
   (char**) &current_user, (char**) &current_user, 0, GET_STR, REQUIRED_ARG,
 
423
   0, 0, 0, 0, 0, 0},
 
424
#endif
335
425
  {"verbose", 'v', "Print info about the various stages.",
336
 
    (char**) &verbose, (char**) &verbose, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
426
   (char**) &verbose, (char**) &verbose, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
337
427
  {"version",'V', "Output version information and exit.", 0, 0, 0,
338
 
    GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
428
   GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
339
429
  {"where", 'w', "Dump only selected records; QUOTES mandatory!",
340
 
    (char**) &where, (char**) &where, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
430
   (char**) &where, (char**) &where, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
341
431
  {"xml", 'X', "Dump a database as well formed XML.", 0, 0, 0, GET_NO_ARG,
342
 
    NO_ARG, 0, 0, 0, 0, 0, 0},
 
432
   NO_ARG, 0, 0, 0, 0, 0, 0},
343
433
  {0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
344
434
};
345
435
 
349
439
static void die(int error, const char* reason, ...);
350
440
static void maybe_die(int error, const char* reason, ...);
351
441
static void write_header(FILE *sql_file, char *db_name);
352
 
static void print_value(FILE *file, drizzle_result_st *result,
353
 
                        drizzle_row_t row, const char *prefix, const char *name,
 
442
static void print_value(FILE *file, DRIZZLE_RES  *result, DRIZZLE_ROW row,
 
443
                        const char *prefix,const char *name,
354
444
                        int string_value);
355
 
static const char* fetch_named_row(drizzle_result_st *result, drizzle_row_t row,
356
 
                                   const char* name);
357
445
static int dump_selected_tables(char *db, char **table_names, int tables);
358
446
static int dump_all_tables_in_db(char *db);
359
447
static int init_dumping_tables(char *);
368
456
  Print the supplied message if in verbose mode
369
457
 
370
458
  SYNOPSIS
371
 
  verbose_msg()
372
 
  fmt   format specifier
373
 
  ...   variable number of parameters
 
459
    verbose_msg()
 
460
    fmt   format specifier
 
461
    ...   variable number of parameters
374
462
*/
375
463
static void verbose_msg(const char *fmt, ...)
376
464
{
383
471
  va_start(args, fmt);
384
472
  vfprintf(stderr, fmt, args);
385
473
  va_end(args);
 
474
 
 
475
  return;
386
476
}
387
477
 
388
478
/*
389
479
  exit with message if ferror(file)
390
480
 
391
481
  SYNOPSIS
392
 
  check_io()
393
 
  file        - checked file
 
482
    check_io()
 
483
    file        - checked file
394
484
*/
395
485
 
396
486
static void check_io(FILE *file)
397
487
{
398
488
  if (ferror(file))
399
 
    die(EX_EOF, _("Got errno %d on write"), errno);
 
489
    die(EX_EOF, "Got errno %d on write", errno);
400
490
}
401
491
 
402
492
static void print_version(void)
403
493
{
404
 
  printf(_("%s  Drizzle %s libdrizzle %s, for %s-%s (%s)\n"), internal::my_progname,
405
 
         VERSION, drizzle_version(), HOST_VENDOR, HOST_OS, HOST_CPU);
 
494
  printf("%s  Ver %s Distrib %s, for %s (%s)\n",my_progname,DUMP_VERSION,
 
495
         drizzle_get_client_info(),SYSTEM_TYPE,MACHINE_TYPE);
406
496
} /* print_version */
407
497
 
408
498
 
409
499
static void short_usage_sub(void)
410
500
{
411
 
  printf(_("Usage: %s [OPTIONS] database [tables]\n"), internal::my_progname);
412
 
  printf(_("OR     %s [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]\n"),
413
 
         internal::my_progname);
414
 
  printf(_("OR     %s [OPTIONS] --all-databases [OPTIONS]\n"), internal::my_progname);
 
501
  printf("Usage: %s [OPTIONS] database [tables]\n", my_progname);
 
502
  printf("OR     %s [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]\n",
 
503
         my_progname);
 
504
  printf("OR     %s [OPTIONS] --all-databases [OPTIONS]\n", my_progname);
415
505
}
416
506
 
417
507
 
418
508
static void usage(void)
419
509
{
420
510
  print_version();
421
 
  puts("");
422
 
  puts(_("This software comes with ABSOLUTELY NO WARRANTY. This is free software,\nand you are welcome to modify and redistribute it under the GPL license\n"));
423
 
  puts(_("Dumps definitions and data from a Drizzle database server"));
 
511
  puts("By Igor Romanenko, Monty, Jani & Sinisa");
 
512
  puts("This software comes with ABSOLUTELY NO WARRANTY. This is free software,\nand you are welcome to modify and redistribute it under the GPL license\n");
 
513
  puts("Dumping definition and data DRIZZLE database or table");
424
514
  short_usage_sub();
425
 
  internal::print_defaults("drizzle",load_default_groups);
 
515
  print_defaults("my",load_default_groups);
426
516
  my_print_help(my_long_options);
427
517
  my_print_variables(my_long_options);
428
518
} /* usage */
431
521
static void short_usage(void)
432
522
{
433
523
  short_usage_sub();
434
 
  printf(_("For more options, use %s --help\n"), internal::my_progname);
 
524
  printf("For more options, use %s --help\n", my_progname);
435
525
}
436
526
 
437
527
static void write_header(FILE *sql_file, char *db_name)
440
530
  {
441
531
    fputs("<?xml version=\"1.0\"?>\n", sql_file);
442
532
    /*
443
 
      Schema reference.  Allows use of xsi:nil for NULL values and
444
 
xsi:type to define an element's data type.
 
533
      Schema reference.  Allows use of xsi:nil for NULL values and 
 
534
      xsi:type to define an element's data type.
445
535
    */
446
536
    fputs("<drizzledump ", sql_file);
447
537
    fputs("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"",
454
544
    if (opt_comments)
455
545
    {
456
546
      fprintf(sql_file,
457
 
              "-- drizzledump %s libdrizzle %s, for %s-%s (%s)\n--\n",
458
 
              VERSION, drizzle_version(), HOST_VENDOR, HOST_OS, HOST_CPU);
 
547
              "-- DRIZZLE dump %s  Distrib %s, for %s (%s)\n--\n",
 
548
              DUMP_VERSION, drizzle_get_client_info(),
 
549
              SYSTEM_TYPE, MACHINE_TYPE);
459
550
      fprintf(sql_file, "-- Host: %s    Database: %s\n",
460
551
              current_host ? current_host : "localhost", db_name ? db_name :
461
552
              "");
462
553
      fputs("-- ------------------------------------------------------\n",
463
554
            sql_file);
464
555
      fprintf(sql_file, "-- Server version\t%s\n",
465
 
              drizzle_con_server_version(&dcon));
 
556
              drizzle_get_server_info(&drizzle_connection));
466
557
    }
467
558
    if (opt_set_charset)
468
559
      fprintf(sql_file,
469
 
              "\nSET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION;\n");
470
 
 
471
 
    if (path == NULL)
472
 
    {
473
 
      fprintf(md_result_file,"SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;\nSET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;\n");
 
560
"\n/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;"
 
561
"\n/*!40101 SET NAMES %s */;\n",default_charset);
 
562
 
 
563
    if (opt_tz_utc)
 
564
    {
 
565
      fprintf(sql_file, "/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;\n");
 
566
      fprintf(sql_file, "/*!40103 SET TIME_ZONE='+00:00' */;\n");
 
567
    }
 
568
 
 
569
    if (!path)
 
570
    {
 
571
      fprintf(md_result_file,"\
 
572
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;\n\
 
573
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;\n\
 
574
");
474
575
    }
475
576
    check_io(sql_file);
476
577
  }
484
585
    fputs("</drizzledump>\n", sql_file);
485
586
    check_io(sql_file);
486
587
  }
487
 
  else if (opt_compact == false)
 
588
  else if (!opt_compact)
488
589
  {
489
 
    if (path == NULL)
 
590
    if (opt_tz_utc)
 
591
      fprintf(sql_file,"/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;\n");
 
592
 
 
593
    if (!path)
490
594
    {
491
 
      fprintf(md_result_file,"SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;\nSET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;\n");
 
595
      fprintf(md_result_file,"\
 
596
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;\n\
 
597
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;\n");
492
598
    }
493
599
    if (opt_set_charset)
494
 
      fprintf(sql_file, "SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION;\n");
 
600
      fprintf(sql_file,
 
601
"/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;\n");
 
602
    fprintf(sql_file,
 
603
            "/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;\n");
 
604
    fputs("\n", sql_file);
495
605
    if (opt_comments)
496
606
    {
497
607
      if (opt_dump_date)
498
608
      {
499
609
        char time_str[20];
500
 
        internal::get_date(time_str, GETDATE_DATE_TIME, 0);
 
610
        get_date(time_str, GETDATE_DATE_TIME, 0);
501
611
        fprintf(sql_file, "-- Dump completed on %s\n",
502
612
                time_str);
503
613
      }
509
619
} /* write_footer */
510
620
 
511
621
 
512
 
static int get_one_option(int optid, const struct option *, char *argument)
513
 
{
514
 
  char *endchar= NULL;
515
 
  uint64_t temp_drizzle_port= 0;
516
 
 
 
622
static void free_table_ent(char *key)
 
623
{
 
624
  free(key);
 
625
}
 
626
 
 
627
 
 
628
static unsigned char* get_table_key(const char *entry, size_t *length,
 
629
                            bool not_used __attribute__((unused)))
 
630
{
 
631
  *length= strlen(entry);
 
632
  return (unsigned char*) entry;
 
633
}
 
634
 
 
635
 
 
636
static bool
 
637
get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
 
638
               char *argument)
 
639
{
517
640
  switch (optid) {
518
641
  case 'p':
519
 
    temp_drizzle_port= (uint64_t) strtoul(argument, &endchar, 10);
520
 
    /* if there is an alpha character this is not a valid port */
521
 
    if (strlen(endchar) != 0)
522
 
    {
523
 
      fprintf(stderr, _("Non-integer value supplied for port.  If you are trying to enter a password please use --password instead.\n"));
524
 
      return EXIT_ARGUMENT_INVALID;
525
 
    }
526
 
    /* If the port number is > 65535 it is not a valid port
527
 
     *        This also helps with potential data loss casting unsigned long to a
528
 
     *               uint32_t. */
529
 
    if ((temp_drizzle_port == 0) || (temp_drizzle_port > 65535))
530
 
    {
531
 
      fprintf(stderr, _("Value supplied for port is not valid.\n"));
532
 
      return EXIT_ARGUMENT_INVALID;
533
 
    }
534
 
    else
535
 
    {
536
 
      opt_drizzle_port= (uint32_t) temp_drizzle_port;
537
 
    }
538
 
    break;
539
 
  case 'P':
540
642
    if (argument)
541
643
    {
542
 
      char *start= argument;
543
 
      if (opt_password)
544
 
        free(opt_password);
545
 
      opt_password= strdup(argument);
546
 
      if (opt_password == NULL)
547
 
      {
548
 
        fprintf(stderr, _("Memory allocation error while copying password. "
549
 
                          "Aborting.\n"));
550
 
        return EXIT_OUT_OF_MEMORY;
551
 
      }
552
 
      while (*argument)
553
 
      {
554
 
        /* Overwriting password with 'x' */
555
 
        *argument++= 'x';
556
 
      }
 
644
      char *start=argument;
 
645
      free(opt_password);
 
646
      opt_password=my_strdup(argument,MYF(MY_FAE));
 
647
      while (*argument) *argument++= 'x';               /* Destroy argument */
557
648
      if (*start)
558
 
      {
559
 
        /* Cut length of argument */
560
 
        start[1]= 0;
561
 
      }
 
649
        start[1]=0;                             /* Cut length of argument */
562
650
      tty_password= 0;
563
651
    }
564
652
    else
565
 
    {
566
 
      tty_password= 1;
567
 
    }
 
653
      tty_password=1;
568
654
    break;
569
655
  case 'r':
570
 
    if (!(md_result_file= fopen(argument, "w")))
 
656
    if (!(md_result_file= my_fopen(argument, O_WRONLY | FILE_BINARY,
 
657
                                    MYF(MY_WME))))
571
658
      exit(1);
572
659
    break;
573
660
  case 'N':
584
671
        a crash even if the input destination buffer is large enough
585
672
        to hold the output.
586
673
      */
587
 
      fprintf(stderr, _("Input filename too long: %s"), argument);
588
 
      return EXIT_ARGUMENT_INVALID;
 
674
      die(EX_USAGE, "Input filename too long: %s", argument);
589
675
    }
590
676
 
591
677
    break;
592
678
  case 'V': print_version(); exit(0);
593
679
  case 'X':
594
 
            opt_xml= 1;
595
 
            extended_insert= opt_drop=
596
 
              opt_disable_keys= opt_autocommit= opt_create_db= 0;
597
 
            break;
 
680
    opt_xml= 1;
 
681
    extended_insert= opt_drop= opt_lock=
 
682
      opt_disable_keys= opt_autocommit= opt_create_db= 0;
 
683
    break;
598
684
  case 'I':
599
685
  case '?':
600
 
            usage();
601
 
            exit(0);
 
686
    usage();
 
687
    exit(0);
 
688
  case (int) OPT_MASTER_DATA:
 
689
    if (!argument) /* work like in old versions */
 
690
      opt_master_data= DRIZZLE_OPT_MASTER_DATA_EFFECTIVE_SQL;
 
691
    break;
 
692
  case (int) OPT_DRIZZLEDUMP_SLAVE_DATA:
 
693
    if (!argument) /* work like in old versions */
 
694
      opt_slave_data= DRIZZLE_OPT_SLAVE_DATA_EFFECTIVE_SQL;
 
695
    break;
602
696
  case (int) OPT_OPTIMIZE:
603
 
            extended_insert= opt_drop= quick= create_options=
604
 
              opt_disable_keys= opt_set_charset= 1;
605
 
            break;
 
697
    extended_insert= opt_drop= opt_lock= quick= create_options=
 
698
      opt_disable_keys= lock_tables= opt_set_charset= 1;
 
699
    break;
606
700
  case (int) OPT_SKIP_OPTIMIZATION:
607
 
            extended_insert= opt_drop= quick= create_options=
608
 
              opt_disable_keys= opt_set_charset= 0;
609
 
            break;
 
701
    extended_insert= opt_drop= opt_lock= quick= create_options=
 
702
      opt_disable_keys= lock_tables= opt_set_charset= 0;
 
703
    break;
610
704
  case (int) OPT_COMPACT:
611
 
            if (opt_compact)
612
 
            {
613
 
              opt_comments= opt_drop= opt_disable_keys= 0;
614
 
              opt_set_charset= 0;
615
 
            }
 
705
  if (opt_compact)
 
706
  {
 
707
    opt_comments= opt_drop= opt_disable_keys= opt_lock= 0;
 
708
    opt_set_charset= 0;
 
709
  }
616
710
  case (int) OPT_TABLES:
617
 
            opt_databases=0;
618
 
            break;
 
711
    opt_databases=0;
 
712
    break;
619
713
  case (int) OPT_IGNORE_TABLE:
620
 
            {
621
 
              if (!strchr(argument, '.'))
622
 
              {
623
 
                fprintf(stderr, _("Illegal use of option --ignore-table=<database>.<table>\n"));
624
 
                return EXIT_ARGUMENT_INVALID;
625
 
              }
626
 
              string tmpptr(argument);
627
 
              ignore_table.insert(tmpptr); 
628
 
              break;
629
 
            }
 
714
  {
 
715
    if (!strchr(argument, '.'))
 
716
    {
 
717
      fprintf(stderr, "Illegal use of option --ignore-table=<database>.<table>\n");
 
718
      exit(1);
 
719
    }
 
720
    if (my_hash_insert(&ignore_table, (unsigned char*)my_strdup(argument, MYF(0))))
 
721
      exit(EX_EOM);
 
722
    break;
 
723
  }
630
724
  case (int) OPT_COMPATIBLE:
631
 
            {
632
 
              char buff[255];
633
 
              char *end= compatible_mode_normal_str;
634
 
              uint32_t i;
635
 
              uint32_t mode;
636
 
              uint32_t error_len;
 
725
    {
 
726
      char buff[255];
 
727
      char *end= compatible_mode_normal_str;
 
728
      uint32_t i;
 
729
      uint32_t mode;
 
730
      uint32_t error_len;
637
731
 
638
 
              opt_quoted= 1;
639
 
              opt_set_charset= 0;
640
 
              opt_compatible_mode_str= argument;
641
 
              opt_compatible_mode= find_set(&compatible_mode_typelib,
642
 
                                            argument, strlen(argument),
643
 
                                            &err_ptr, &error_len);
644
 
              if (error_len)
645
 
              {
646
 
                strncpy(buff, err_ptr, min((uint32_t)sizeof(buff), error_len+1));
647
 
                fprintf(stderr, _("Invalid mode to --compatible: %s\n"), buff);
648
 
                return EXIT_ARGUMENT_INVALID;
649
 
              }
650
 
              mode= opt_compatible_mode;
651
 
              for (i= 0, mode= opt_compatible_mode; mode; mode>>= 1, i++)
652
 
              {
653
 
                if (mode & 1)
654
 
                {
655
 
                  uint32_t len = strlen(compatible_mode_names[i]);
656
 
                  end= strcpy(end, compatible_mode_names[i]) + len;
657
 
                  end= strcpy(end, ",")+1;
658
 
                }
659
 
              }
660
 
              if (end!=compatible_mode_normal_str)
661
 
                end[-1]= 0;
662
 
            }
 
732
      opt_quoted= 1;
 
733
      opt_set_charset= 0;
 
734
      opt_compatible_mode_str= argument;
 
735
      opt_compatible_mode= find_set(&compatible_mode_typelib,
 
736
                                    argument, strlen(argument),
 
737
                                    &err_ptr, &error_len);
 
738
      if (error_len)
 
739
      {
 
740
        strmake(buff, err_ptr, min((uint32_t)sizeof(buff), error_len));
 
741
        fprintf(stderr, "Invalid mode to --compatible: %s\n", buff);
 
742
        exit(1);
 
743
      }
 
744
      mode= opt_compatible_mode;
 
745
      for (i= 0, mode= opt_compatible_mode; mode; mode>>= 1, i++)
 
746
      {
 
747
        if (mode & 1)
 
748
        {
 
749
          end= my_stpcpy(end, compatible_mode_names[i]);
 
750
          end= my_stpcpy(end, ",");
 
751
        }
 
752
      }
 
753
      if (end!=compatible_mode_normal_str)
 
754
        end[-1]= 0;
 
755
      /*
 
756
        Set charset to the default compiled value if it hasn't
 
757
        been reset yet by --default-character-set=xxx.
 
758
      */
 
759
      if (default_charset == drizzle_universal_client_charset)
 
760
        default_charset= (char*) DRIZZLE_DEFAULT_CHARSET_NAME;
 
761
      break;
 
762
    }
663
763
  }
664
764
  return 0;
665
765
}
667
767
static int get_options(int *argc, char ***argv)
668
768
{
669
769
  int ho_error;
 
770
  const DRIZZLE_PARAMETERS *drizzle_params= drizzle_get_parameters();
 
771
 
 
772
  opt_max_allowed_packet= *drizzle_params->p_max_allowed_packet;
 
773
  opt_net_buffer_length= *drizzle_params->p_net_buffer_length;
670
774
 
671
775
  md_result_file= stdout;
672
 
  internal::load_defaults("drizzle",load_default_groups,argc,argv);
 
776
  load_defaults("my",load_default_groups,argc,argv);
673
777
  defaults_argv= *argv;
674
778
 
 
779
  if (hash_init(&ignore_table, charset_info, 16, 0, 0,
 
780
                (hash_get_key) get_table_key,
 
781
                (hash_free_key) free_table_ent, 0))
 
782
    return(EX_EOM);
 
783
  /* Don't copy internal log tables */
 
784
  if (my_hash_insert(&ignore_table,
 
785
                     (unsigned char*) my_strdup("mysql.apply_status", MYF(MY_WME))) ||
 
786
      my_hash_insert(&ignore_table,
 
787
                     (unsigned char*) my_strdup("mysql.schema", MYF(MY_WME))) ||
 
788
      my_hash_insert(&ignore_table,
 
789
                     (unsigned char*) my_strdup("mysql.general_log", MYF(MY_WME))) ||
 
790
      my_hash_insert(&ignore_table,
 
791
                     (unsigned char*) my_strdup("mysql.slow_log", MYF(MY_WME))) ||
 
792
      my_hash_insert(&ignore_table,
 
793
                     (unsigned char*) my_strdup("mysql.online_backup", MYF(MY_WME))) ||
 
794
      my_hash_insert(&ignore_table,
 
795
                     (unsigned char*) my_strdup("mysql.online_backup_progress", MYF(MY_WME))))
 
796
    return(EX_EOM);
 
797
 
675
798
  if ((ho_error= handle_options(argc, argv, my_long_options, get_one_option)))
676
799
    return(ho_error);
677
800
 
 
801
  *drizzle_params->p_max_allowed_packet= opt_max_allowed_packet;
 
802
  *drizzle_params->p_net_buffer_length= opt_net_buffer_length;
 
803
  if (debug_info_flag)
 
804
    my_end_arg= MY_CHECK_ERROR | MY_GIVE_INFO;
 
805
  if (debug_check_flag)
 
806
    my_end_arg= MY_CHECK_ERROR;
 
807
 
 
808
  if (opt_delayed)
 
809
    opt_lock=0;                         /* Can't have lock with delayed */
678
810
  if (!path && (enclosed || opt_enclosed || escaped || lines_terminated ||
679
811
                fields_terminated))
680
812
  {
681
813
    fprintf(stderr,
682
 
            _("%s: You must use option --tab with --fields-...\n"), internal::my_progname);
 
814
            "%s: You must use option --tab with --fields-...\n", my_progname);
683
815
    return(EX_USAGE);
684
816
  }
685
817
 
 
818
  /* We don't delete master logs if slave data option */
 
819
  if (opt_slave_data)
 
820
  {
 
821
    opt_lock_all_tables= !opt_single_transaction;
 
822
    opt_master_data= 0;
 
823
    opt_delete_master_logs= 0;
 
824
  }
 
825
 
 
826
  /* Ensure consistency of the set of binlog & locking options */
 
827
  if (opt_delete_master_logs && !opt_master_data)
 
828
    opt_master_data= DRIZZLE_OPT_MASTER_DATA_COMMENTED_SQL;
686
829
  if (opt_single_transaction && opt_lock_all_tables)
687
830
  {
688
 
    fprintf(stderr, _("%s: You can't use --single-transaction and "
689
 
                      "--lock-all-tables at the same time.\n"), internal::my_progname);
 
831
    fprintf(stderr, "%s: You can't use --single-transaction and "
 
832
            "--lock-all-tables at the same time.\n", my_progname);
690
833
    return(EX_USAGE);
691
834
  }
 
835
  if (opt_master_data)
 
836
  {
 
837
    opt_lock_all_tables= !opt_single_transaction;
 
838
    opt_slave_data= 0;
 
839
  }
 
840
  if (opt_single_transaction || opt_lock_all_tables)
 
841
    lock_tables= 0;
692
842
  if (enclosed && opt_enclosed)
693
843
  {
694
 
    fprintf(stderr, _("%s: You can't use ..enclosed.. and ..optionally-enclosed.. at the same time.\n"), internal::my_progname);
 
844
    fprintf(stderr, "%s: You can't use ..enclosed.. and ..optionally-enclosed.. at the same time.\n", my_progname);
695
845
    return(EX_USAGE);
696
846
  }
697
847
  if ((opt_databases || opt_alldbs) && path)
698
848
  {
699
849
    fprintf(stderr,
700
 
            _("%s: --databases or --all-databases can't be used with --tab.\n"),
701
 
            internal::my_progname);
 
850
            "%s: --databases or --all-databases can't be used with --tab.\n",
 
851
            my_progname);
702
852
    return(EX_USAGE);
703
853
  }
 
854
  if (strcmp(default_charset, charset_info->csname) &&
 
855
      !(charset_info= get_charset_by_csname(default_charset,
 
856
                                            MY_CS_PRIMARY, MYF(MY_WME))))
 
857
    exit(1);
704
858
  if ((*argc < 1 && !opt_alldbs) || (*argc > 0 && opt_alldbs))
705
859
  {
706
860
    short_usage();
707
861
    return EX_USAGE;
708
862
  }
709
863
  if (tty_password)
710
 
    opt_password=client_get_tty_password(NULL);
 
864
    opt_password=get_tty_password(NULL);
711
865
  return(0);
712
866
} /* get_options */
713
867
 
714
868
 
715
869
/*
716
 
 ** DB_error -- prints DRIZZLE error message and exits the program.
 
870
** DB_error -- prints DRIZZLE error message and exits the program.
717
871
*/
718
 
static void DB_error(drizzle_result_st *res, drizzle_return_t ret,
719
 
                     const char *when)
 
872
static void DB_error(DRIZZLE *drizzle_arg, const char *when)
720
873
{
721
 
  if (ret == DRIZZLE_RETURN_ERROR_CODE)
722
 
  {
723
 
    maybe_die(EX_DRIZZLEERR, _("Got error: %s (%d) %s"),
724
 
              drizzle_result_error(res),
725
 
              drizzle_result_error_code(res),
726
 
              when);
727
 
    drizzle_result_free(res);
728
 
  }
729
 
  else
730
 
    maybe_die(EX_DRIZZLEERR, _("Got error: %d %s"), ret, when);
731
874
 
 
875
  maybe_die(EX_DRIZZLEERR, "Got error: %d: %s %s",
 
876
          drizzle_errno(drizzle_arg), drizzle_error(drizzle_arg), when);
732
877
  return;
733
878
}
734
879
 
738
883
  Prints out an error message and kills the process.
739
884
 
740
885
  SYNOPSIS
741
 
  die()
742
 
  error_num   - process return value
743
 
  fmt_reason  - a format string for use by vsnprintf.
744
 
  ...         - variable arguments for above fmt_reason string
745
 
 
 
886
    die()
 
887
    error_num   - process return value
 
888
    fmt_reason  - a format string for use by vsnprintf.
 
889
    ...         - variable arguments for above fmt_reason string
 
890
  
746
891
  DESCRIPTION
747
 
  This call prints out the formatted error message to stderr and then
748
 
  terminates the process.
 
892
    This call prints out the formatted error message to stderr and then
 
893
    terminates the process.
749
894
*/
750
895
static void die(int error_num, const char* fmt_reason, ...)
751
896
{
755
900
  vsnprintf(buffer, sizeof(buffer), fmt_reason, args);
756
901
  va_end(args);
757
902
 
758
 
  fprintf(stderr, "%s: %s\n", internal::my_progname, buffer);
 
903
  fprintf(stderr, "%s: %s\n", my_progname, buffer);
759
904
  fflush(stderr);
760
905
 
761
906
  ignore_errors= 0; /* force the exit */
767
912
  Prints out an error message and maybe kills the process.
768
913
 
769
914
  SYNOPSIS
770
 
  maybe_die()
771
 
  error_num   - process return value
772
 
  fmt_reason  - a format string for use by vsnprintf.
773
 
  ...         - variable arguments for above fmt_reason string
774
 
 
 
915
    maybe_die()
 
916
    error_num   - process return value
 
917
    fmt_reason  - a format string for use by vsnprintf.
 
918
    ...         - variable arguments for above fmt_reason string
 
919
  
775
920
  DESCRIPTION
776
 
  This call prints out the formatted error message to stderr and then
777
 
  terminates the process, unless the --force command line option is used.
778
 
 
779
 
  This call should be used for non-fatal errors (such as database
780
 
  errors) that the code may still be able to continue to the next unit
781
 
  of work.
782
 
 
 
921
    This call prints out the formatted error message to stderr and then
 
922
    terminates the process, unless the --force command line option is used.
 
923
    
 
924
    This call should be used for non-fatal errors (such as database
 
925
    errors) that the code may still be able to continue to the next unit
 
926
    of work.
 
927
    
783
928
*/
784
929
static void maybe_die(int error_num, const char* fmt_reason, ...)
785
930
{
789
934
  vsnprintf(buffer, sizeof(buffer), fmt_reason, args);
790
935
  va_end(args);
791
936
 
792
 
  fprintf(stderr, "%s: %s\n", internal::my_progname, buffer);
 
937
  fprintf(stderr, "%s: %s\n", my_progname, buffer);
793
938
  fflush(stderr);
794
939
 
795
940
  maybe_exit(error_num);
802
947
  some.
803
948
 
804
949
  SYNOPSIS
805
 
  drizzleclient_query_with_error_report()
806
 
  drizzle_con       connection to use
807
 
  res             if non zero, result will be put there with
808
 
  drizzleclient_store_result()
809
 
  query           query to send to server
 
950
    drizzle_query_with_error_report()
 
951
    drizzle_con       connection to use
 
952
    res             if non zero, result will be put there with
 
953
                    drizzle_store_result()
 
954
    query           query to send to server
810
955
 
811
956
  RETURN VALUES
812
 
  0               query sending and (if res!=0) result reading went ok
813
 
  1               error
 
957
    0               query sending and (if res!=0) result reading went ok
 
958
    1               error
814
959
*/
815
960
 
816
 
static int drizzleclient_query_with_error_report(drizzle_con_st *con,
817
 
                                                 drizzle_result_st *result,
818
 
                                                 const char *query_str,
819
 
                                                 bool no_buffer)
 
961
static int drizzle_query_with_error_report(DRIZZLE *drizzle_con, DRIZZLE_RES **res,
 
962
                                         const char *query)
820
963
{
821
 
  drizzle_return_t ret;
822
 
 
823
 
  if (drizzle_query_str(con, result, query_str, &ret) == NULL ||
824
 
      ret != DRIZZLE_RETURN_OK)
825
 
  {
826
 
    if (ret == DRIZZLE_RETURN_ERROR_CODE)
827
 
    {
828
 
      maybe_die(EX_DRIZZLEERR, _("Couldn't execute '%s': %s (%d)"),
829
 
                query_str, drizzle_result_error(result),
830
 
                drizzle_result_error_code(result));
831
 
      drizzle_result_free(result);
832
 
    }
833
 
    else
834
 
    {
835
 
      maybe_die(EX_DRIZZLEERR, _("Couldn't execute '%s': %s (%d)"),
836
 
                query_str, drizzle_con_error(con), ret);
837
 
    }
838
 
    return 1;
839
 
  }
840
 
 
841
 
  if (no_buffer)
842
 
    ret= drizzle_column_buffer(result);
843
 
  else
844
 
    ret= drizzle_result_buffer(result);
845
 
  if (ret != DRIZZLE_RETURN_OK)
846
 
  {
847
 
    drizzle_result_free(result);
848
 
    maybe_die(EX_DRIZZLEERR, _("Couldn't execute '%s': %s (%d)"),
849
 
              query_str, drizzle_con_error(con), ret);
850
 
    return 1;
851
 
  }
852
 
 
 
964
  if (drizzle_query(drizzle_con, query) ||
 
965
      (res && !((*res)= drizzle_store_result(drizzle_con))))
 
966
  {
 
967
    maybe_die(EX_DRIZZLEERR, "Couldn't execute '%s': %s (%d)",
 
968
            query, drizzle_error(drizzle_con), drizzle_errno(drizzle_con));
 
969
    return 1;
 
970
  }
853
971
  return 0;
854
972
}
855
973
 
 
974
 
 
975
/**
 
976
  Switch charset for results to some specified charset.  If the server does not
 
977
  support character_set_results variable, nothing can be done here.  As for
 
978
  whether something should be done here, future new callers of this function
 
979
  should be aware that the server lacking the facility of switching charsets is
 
980
  treated as success.
 
981
 
 
982
  @note  If the server lacks support, then nothing is changed and no error
 
983
         condition is returned.
 
984
 
 
985
  @returns  whether there was an error or not
 
986
*/
 
987
static int switch_character_set_results(DRIZZLE *drizzle, const char *cs_name)
 
988
{
 
989
  char query_buffer[QUERY_LENGTH];
 
990
  size_t query_length;
 
991
 
 
992
  /* Server lacks facility.  This is not an error, by arbitrary decision . */
 
993
  if (!server_supports_switching_charsets)
 
994
    return false;
 
995
 
 
996
  query_length= snprintf(query_buffer,
 
997
                         sizeof (query_buffer),
 
998
                         "SET SESSION character_set_results = '%s'",
 
999
                         (const char *) cs_name);
 
1000
 
 
1001
  return drizzle_real_query(drizzle, query_buffer, query_length);
 
1002
}
 
1003
 
856
1004
/*
857
1005
  Open a new .sql file to dump the table or view into
858
1006
 
859
1007
  SYNOPSIS
860
 
  open_sql_file_for_table
861
 
  name      name of the table or view
 
1008
    open_sql_file_for_table
 
1009
    name      name of the table or view
862
1010
 
863
1011
  RETURN VALUES
864
 
  0        Failed to open file
865
 
  > 0      Handle of the open file
 
1012
    0        Failed to open file
 
1013
    > 0      Handle of the open file
866
1014
*/
867
1015
static FILE* open_sql_file_for_table(const char* table)
868
1016
{
869
1017
  FILE* res;
870
1018
  char filename[FN_REFLEN], tmp_path[FN_REFLEN];
871
 
  internal::convert_dirname(tmp_path,path,NULL);
872
 
  res= fopen(internal::fn_format(filename, table, tmp_path, ".sql", 4), "w");
873
 
 
 
1019
  convert_dirname(tmp_path,path,NULL);
 
1020
  res= my_fopen(fn_format(filename, table, tmp_path, ".sql", 4),
 
1021
                O_WRONLY, MYF(MY_WME));
874
1022
  return res;
875
1023
}
876
1024
 
878
1026
static void free_resources(void)
879
1027
{
880
1028
  if (md_result_file && md_result_file != stdout)
881
 
    fclose(md_result_file);
 
1029
    my_fclose(md_result_file, MYF(0));
882
1030
  free(opt_password);
 
1031
  if (hash_inited(&ignore_table))
 
1032
    hash_free(&ignore_table);
883
1033
  if (defaults_argv)
884
 
    internal::free_defaults(defaults_argv);
885
 
  internal::my_end();
 
1034
    free_defaults(defaults_argv);
 
1035
  my_end(my_end_arg);
886
1036
}
887
1037
 
888
1038
 
892
1042
    first_error= error;
893
1043
  if (ignore_errors)
894
1044
    return;
895
 
  drizzle_con_free(&dcon);
896
 
  drizzle_free(&drizzle);
 
1045
  if (drizzle)
 
1046
    drizzle_close(drizzle);
897
1047
  free_resources();
898
1048
  exit(error);
899
1049
}
905
1055
 
906
1056
static int connect_to_db(char *host, char *user,char *passwd)
907
1057
{
908
 
  drizzle_return_t ret;
909
 
 
910
 
  verbose_msg(_("-- Connecting to %s...\n"), host ? host : "localhost");
911
 
  drizzle_create(&drizzle);
912
 
  drizzle_con_create(&drizzle, &dcon);
913
 
  drizzle_con_set_tcp(&dcon, host, opt_drizzle_port);
914
 
  drizzle_con_set_auth(&dcon, user, passwd);
915
 
  if (opt_mysql)
916
 
    drizzle_con_add_options(&dcon, DRIZZLE_CON_MYSQL);
917
 
  ret= drizzle_con_connect(&dcon);
918
 
  if (ret != DRIZZLE_RETURN_OK)
 
1058
  char buff[20+FN_REFLEN];
 
1059
 
 
1060
 
 
1061
  verbose_msg("-- Connecting to %s...\n", host ? host : "localhost");
 
1062
  drizzle_create(&drizzle_connection);
 
1063
  if (opt_compress)
 
1064
    drizzle_options(&drizzle_connection,DRIZZLE_OPT_COMPRESS,NULL);
 
1065
  if (!(drizzle= drizzle_connect(&drizzle_connection,host,user,passwd,
 
1066
                                  NULL,opt_drizzle_port, NULL,
 
1067
                                  0)))
919
1068
  {
920
 
    DB_error(NULL, ret, "when trying to connect");
 
1069
    DB_error(&drizzle_connection, "when trying to connect");
921
1070
    return(1);
922
1071
  }
 
1072
  if (drizzle_get_server_version(&drizzle_connection) < 40100)
 
1073
  {
 
1074
    /* Don't dump SET NAMES with a pre-4.1 server (bug#7997).  */
 
1075
    opt_set_charset= 0;
923
1076
 
 
1077
    /* Don't switch charsets for 4.1 and earlier.  (bug#34192). */
 
1078
    server_supports_switching_charsets= false;
 
1079
  } 
 
1080
  /*
 
1081
    set time_zone to UTC to allow dumping date types between servers with
 
1082
    different time zone settings
 
1083
  */
 
1084
  if (opt_tz_utc)
 
1085
  {
 
1086
    snprintf(buff, sizeof(buff), "/*!40103 SET TIME_ZONE='+00:00' */");
 
1087
    if (drizzle_query_with_error_report(drizzle, 0, buff))
 
1088
      return(1);
 
1089
  }
924
1090
  return(0);
925
1091
} /* connect_to_db */
926
1092
 
927
1093
 
928
1094
/*
929
 
 ** dbDisconnect -- disconnects from the host.
 
1095
** dbDisconnect -- disconnects from the host.
930
1096
*/
931
1097
static void dbDisconnect(char *host)
932
1098
{
933
 
  verbose_msg(_("-- Disconnecting from %s...\n"), host ? host : "localhost");
934
 
  drizzle_con_free(&dcon);
935
 
  drizzle_free(&drizzle);
 
1099
  verbose_msg("-- Disconnecting from %s...\n", host ? host : "localhost");
 
1100
  drizzle_close(drizzle);
936
1101
} /* dbDisconnect */
937
1102
 
938
1103
 
939
 
static void unescape(FILE *file,char *pos,uint32_t length)
 
1104
static void unescape(FILE *file,char *pos,uint length)
940
1105
{
941
1106
  char *tmp;
942
1107
 
943
 
  if (!(tmp=(char*) malloc(length*2+1)))
944
 
    die(EX_DRIZZLEERR, _("Couldn't allocate memory"));
 
1108
  if (!(tmp=(char*) my_malloc(length*2+1, MYF(MY_WME))))
 
1109
    die(EX_DRIZZLEERR, "Couldn't allocate memory");
945
1110
 
946
1111
  drizzle_escape_string(tmp, pos, length);
947
1112
  fputc('\'', file);
982
1147
static char *quote_name(const char *name, char *buff, bool force)
983
1148
{
984
1149
  char *to= buff;
985
 
  char qtype= '`';
 
1150
  char qtype= (opt_compatible_mode & MASK_ANSI_QUOTES) ? '\"' : '`';
986
1151
 
987
1152
  if (!force && !opt_quoted && !test_if_special_chars(name))
988
1153
    return (char*) name;
1003
1168
  Quote a table name so it can be used in "SHOW TABLES LIKE <tabname>"
1004
1169
 
1005
1170
  SYNOPSIS
1006
 
  quote_for_like()
1007
 
  name     name of the table
1008
 
  buff     quoted name of the table
 
1171
    quote_for_like()
 
1172
    name     name of the table
 
1173
    buff     quoted name of the table
1009
1174
 
1010
1175
  DESCRIPTION
1011
 
  Quote \, _, ' and % characters
1012
 
 
1013
 
Note: Because DRIZZLE uses the C escape syntax in strings
1014
 
(for example, '\n' to represent newline), you must double
1015
 
any '\' that you use in your LIKE  strings. For example, to
1016
 
search for '\n', specify it as '\\n'. To search for '\', specify
1017
 
it as '\\\\' (the backslashes are stripped once by the parser
1018
 
and another time when the pattern match is done, leaving a
1019
 
single backslash to be matched).
1020
 
 
1021
 
Example: "t\1" => "t\\\\1"
 
1176
    Quote \, _, ' and % characters
 
1177
 
 
1178
    Note: Because DRIZZLE uses the C escape syntax in strings
 
1179
    (for example, '\n' to represent newline), you must double
 
1180
    any '\' that you use in your LIKE  strings. For example, to
 
1181
    search for '\n', specify it as '\\n'. To search for '\', specify
 
1182
    it as '\\\\' (the backslashes are stripped once by the parser
 
1183
    and another time when the pattern match is done, leaving a
 
1184
    single backslash to be matched).
 
1185
 
 
1186
    Example: "t\1" => "t\\\\1"
1022
1187
 
1023
1188
*/
1024
1189
static char *quote_for_like(const char *name, char *buff)
1047
1212
  Quote and print a string.
1048
1213
 
1049
1214
  SYNOPSIS
1050
 
  print_quoted_xml()
1051
 
  xml_file    - output file
1052
 
  str         - string to print
1053
 
  len         - its length
 
1215
    print_quoted_xml()
 
1216
    xml_file    - output file
 
1217
    str         - string to print
 
1218
    len         - its length
1054
1219
 
1055
1220
  DESCRIPTION
1056
 
  Quote '<' '>' '&' '\"' chars and print a string to the xml_file.
 
1221
    Quote '<' '>' '&' '\"' chars and print a string to the xml_file.
1057
1222
*/
1058
1223
 
1059
1224
static void print_quoted_xml(FILE *xml_file, const char *str, uint32_t len)
1072
1237
    case '&':
1073
1238
      fputs("&amp;", xml_file);
1074
1239
      break;
1075
 
      case '\"':
1076
 
        fputs("&quot;", xml_file);
 
1240
    case '\"':
 
1241
      fputs("&quot;", xml_file);
1077
1242
      break;
1078
1243
    default:
1079
1244
      fputc(*str, xml_file);
1088
1253
  Print xml tag. Optionally add attribute(s).
1089
1254
 
1090
1255
  SYNOPSIS
1091
 
  print_xml_tag(xml_file, sbeg, send, tag_name, first_attribute_name,
1092
 
  ..., attribute_name_n, attribute_value_n, NULL)
1093
 
  xml_file              - output file
1094
 
  sbeg                  - line beginning
1095
 
  line_end              - line ending
1096
 
  tag_name              - XML tag name.
1097
 
  first_attribute_name  - tag and first attribute
1098
 
  first_attribute_value - (Implied) value of first attribute
1099
 
  attribute_name_n      - attribute n
1100
 
  attribute_value_n     - value of attribute n
 
1256
    print_xml_tag(xml_file, sbeg, send, tag_name, first_attribute_name, 
 
1257
                    ..., attribute_name_n, attribute_value_n, NULL)
 
1258
    xml_file              - output file
 
1259
    sbeg                  - line beginning
 
1260
    line_end              - line ending
 
1261
    tag_name              - XML tag name.
 
1262
    first_attribute_name  - tag and first attribute
 
1263
    first_attribute_value - (Implied) value of first attribute
 
1264
    attribute_name_n      - attribute n
 
1265
    attribute_value_n     - value of attribute n
1101
1266
 
1102
1267
  DESCRIPTION
1103
 
  Print XML tag with any number of attribute="value" pairs to the xml_file.
 
1268
    Print XML tag with any number of attribute="value" pairs to the xml_file.
1104
1269
 
1105
 
  Format is:
1106
 
  sbeg<tag_name first_attribute_name="first_attribute_value" ...
1107
 
  attribute_name_n="attribute_value_n">send
 
1270
    Format is:
 
1271
      sbeg<tag_name first_attribute_name="first_attribute_value" ... 
 
1272
      attribute_name_n="attribute_value_n">send
1108
1273
  NOTE
1109
 
  Additional arguments must be present in attribute/value pairs.
1110
 
  The last argument should be the null character pointer.
1111
 
  All attribute_value arguments MUST be NULL terminated strings.
1112
 
  All attribute_value arguments will be quoted before output.
 
1274
    Additional arguments must be present in attribute/value pairs.
 
1275
    The last argument should be the null character pointer.
 
1276
    All attribute_value arguments MUST be NULL terminated strings.
 
1277
    All attribute_value arguments will be quoted before output.
1113
1278
*/
1114
1279
 
1115
1280
static void print_xml_tag(FILE * xml_file, const char* sbeg,
1116
 
                          const char* line_end,
1117
 
                          const char* tag_name,
 
1281
                          const char* line_end, 
 
1282
                          const char* tag_name, 
1118
1283
                          const char* first_attribute_name, ...)
1119
1284
{
1120
1285
  va_list arg_list;
1122
1287
 
1123
1288
  fputs(sbeg, xml_file);
1124
1289
  fputc('<', xml_file);
1125
 
  fputs(tag_name, xml_file);
 
1290
  fputs(tag_name, xml_file);  
1126
1291
 
1127
1292
  va_start(arg_list, first_attribute_name);
1128
1293
  attribute_name= first_attribute_name;
1132
1297
    assert(attribute_value != NULL);
1133
1298
 
1134
1299
    fputc(' ', xml_file);
1135
 
    fputs(attribute_name, xml_file);
 
1300
    fputs(attribute_name, xml_file);    
1136
1301
    fputc('\"', xml_file);
1137
 
 
 
1302
    
1138
1303
    print_quoted_xml(xml_file, attribute_value, strlen(attribute_value));
1139
1304
    fputc('\"', xml_file);
1140
1305
 
1152
1317
  Print xml tag with for a field that is null
1153
1318
 
1154
1319
  SYNOPSIS
1155
 
  print_xml_null_tag()
1156
 
  xml_file    - output file
1157
 
  sbeg        - line beginning
1158
 
  stag_atr    - tag and attribute
1159
 
  sval        - value of attribute
1160
 
  line_end        - line ending
 
1320
    print_xml_null_tag()
 
1321
    xml_file    - output file
 
1322
    sbeg        - line beginning
 
1323
    stag_atr    - tag and attribute
 
1324
    sval        - value of attribute
 
1325
    line_end        - line ending
1161
1326
 
1162
1327
  DESCRIPTION
1163
 
  Print tag with one attribute to the xml_file. Format is:
1164
 
  <stag_atr="sval" xsi:nil="true"/>
 
1328
    Print tag with one attribute to the xml_file. Format is:
 
1329
      <stag_atr="sval" xsi:nil="true"/>
1165
1330
  NOTE
1166
 
  sval MUST be a NULL terminated string.
1167
 
  sval string will be qouted before output.
 
1331
    sval MUST be a NULL terminated string.
 
1332
    sval string will be qouted before output.
1168
1333
*/
1169
1334
 
1170
1335
static void print_xml_null_tag(FILE * xml_file, const char* sbeg,
1186
1351
  Print xml tag with many attributes.
1187
1352
 
1188
1353
  SYNOPSIS
1189
 
  print_xml_row()
1190
 
  xml_file    - output file
1191
 
  row_name    - xml tag name
1192
 
  tableRes    - query result
1193
 
  row         - result row
 
1354
    print_xml_row()
 
1355
    xml_file    - output file
 
1356
    row_name    - xml tag name
 
1357
    tableRes    - query result
 
1358
    row         - result row
1194
1359
 
1195
1360
  DESCRIPTION
1196
 
  Print tag with many attribute to the xml_file. Format is:
1197
 
  \t\t<row_name Atr1="Val1" Atr2="Val2"... />
 
1361
    Print tag with many attribute to the xml_file. Format is:
 
1362
      \t\t<row_name Atr1="Val1" Atr2="Val2"... />
1198
1363
  NOTE
1199
 
  All atributes and values will be quoted before output.
 
1364
    All atributes and values will be quoted before output.
1200
1365
*/
1201
1366
 
1202
1367
static void print_xml_row(FILE *xml_file, const char *row_name,
1203
 
                          drizzle_result_st *tableRes, drizzle_row_t *row)
 
1368
                          DRIZZLE_RES *tableRes, DRIZZLE_ROW *row)
1204
1369
{
1205
 
  uint32_t i;
1206
 
  drizzle_column_st *column;
1207
 
  size_t *lengths= drizzle_row_field_sizes(tableRes);
 
1370
  uint i;
 
1371
  DRIZZLE_FIELD *field;
 
1372
  uint32_t *lengths= drizzle_fetch_lengths(tableRes);
1208
1373
 
1209
1374
  fprintf(xml_file, "\t\t<%s", row_name);
1210
1375
  check_io(xml_file);
1211
 
  drizzle_column_seek(tableRes, 0);
1212
 
  for (i= 0; (column= drizzle_column_next(tableRes)); i++)
 
1376
  drizzle_field_seek(tableRes, 0);
 
1377
  for (i= 0; (field= drizzle_fetch_field(tableRes)); i++)
1213
1378
  {
1214
1379
    if ((*row)[i])
1215
1380
    {
1216
1381
      fputc(' ', xml_file);
1217
 
      print_quoted_xml(xml_file, drizzle_column_name(column),
1218
 
                       strlen(drizzle_column_name(column)));
 
1382
      print_quoted_xml(xml_file, field->name, field->name_length);
1219
1383
      fputs("=\"", xml_file);
1220
1384
      print_quoted_xml(xml_file, (*row)[i], lengths[i]);
1221
1385
      fputc('"', xml_file);
1231
1395
  Print hex value for blob data.
1232
1396
 
1233
1397
  SYNOPSIS
1234
 
  print_blob_as_hex()
1235
 
  output_file         - output file
1236
 
  str                 - string to print
1237
 
  len                 - its length
 
1398
    print_blob_as_hex()
 
1399
    output_file         - output file
 
1400
    str                 - string to print
 
1401
    len                 - its length
1238
1402
 
1239
1403
  DESCRIPTION
1240
 
  Print hex value for blob data.
 
1404
    Print hex value for blob data.
1241
1405
*/
1242
1406
 
1243
1407
static void print_blob_as_hex(FILE *output_file, const char *str, uint32_t len)
1244
1408
{
1245
 
  /* sakaik got the idea to to provide blob's in hex notation. */
1246
 
  const char *ptr= str, *end= ptr + len;
1247
 
  for (; ptr < end ; ptr++)
1248
 
    fprintf(output_file, "%02X", *((unsigned char *)ptr));
1249
 
  check_io(output_file);
 
1409
    /* sakaik got the idea to to provide blob's in hex notation. */
 
1410
    const char *ptr= str, *end= ptr + len;
 
1411
    for (; ptr < end ; ptr++)
 
1412
      fprintf(output_file, "%02X", *((unsigned char *)ptr));
 
1413
    check_io(output_file);
1250
1414
}
1251
1415
 
1252
1416
/*
1255
1419
  be dumping.
1256
1420
 
1257
1421
  ARGS
1258
 
  table       - table name
1259
 
  db          - db name
1260
 
  table_type  - table type, e.g. "MyISAM" or "InnoDB", but also "VIEW"
1261
 
  ignore_flag - what we must particularly ignore - see IGNORE_ defines above
1262
 
  num_fields  - number of fields in the table
 
1422
    table       - table name
 
1423
    db          - db name
 
1424
    table_type  - table type, e.g. "MyISAM" or "InnoDB", but also "VIEW"
 
1425
    ignore_flag - what we must particularly ignore - see IGNORE_ defines above
1263
1426
 
1264
1427
  RETURN
1265
 
  true if success, false if error
 
1428
    number of fields in table, 0 if error
1266
1429
*/
1267
1430
 
1268
 
static bool get_table_structure(char *table, char *db, char *table_type,
1269
 
                                char *ignore_flag, uint64_t *num_fields)
 
1431
static uint get_table_structure(char *table, char *db, char *table_type,
 
1432
                                char *ignore_flag)
1270
1433
{
1271
1434
  bool    init=0, delayed, write_data, complete_insert;
 
1435
  uint64_t num_fields;
1272
1436
  char       *result_table, *opt_quoted_table;
1273
1437
  const char *insert_option;
1274
 
  char       name_buff[DRIZZLE_MAX_COLUMN_NAME_SIZE+3];
1275
 
  char       table_buff[DRIZZLE_MAX_COLUMN_NAME_SIZE*2+3];
1276
 
  char       table_buff2[DRIZZLE_MAX_TABLE_SIZE*2+3];
1277
 
  char       query_buff[QUERY_LENGTH];
 
1438
  char       name_buff[NAME_LEN+3],table_buff[NAME_LEN*2+3];
 
1439
  char       table_buff2[NAME_LEN*2+3], query_buff[QUERY_LENGTH];
1278
1440
  FILE       *sql_file= md_result_file;
1279
 
  drizzle_result_st result;
1280
 
  drizzle_row_t  row;
 
1441
  int        len;
 
1442
  DRIZZLE_RES  *result;
 
1443
  DRIZZLE_ROW  row;
1281
1444
 
1282
1445
  *ignore_flag= check_if_ignore_table(table, table_type);
1283
1446
 
1285
1448
  if (delayed && (*ignore_flag & IGNORE_INSERT_DELAYED))
1286
1449
  {
1287
1450
    delayed= 0;
1288
 
    verbose_msg(_("-- Warning: Unable to use delayed inserts for table '%s' "
1289
 
                  "because it's of type %s\n"), table, table_type);
 
1451
    verbose_msg("-- Warning: Unable to use delayed inserts for table '%s' "
 
1452
                "because it's of type %s\n", table, table_type);
1290
1453
  }
1291
1454
 
1292
1455
  complete_insert= 0;
1293
1456
  if ((write_data= !(*ignore_flag & IGNORE_DATA)))
1294
1457
  {
1295
1458
    complete_insert= opt_complete_insert;
1296
 
    insert_pat.clear();
 
1459
    insert_pat= "";
1297
1460
  }
1298
1461
 
1299
1462
  insert_option= ((delayed && opt_ignore) ? " DELAYED IGNORE " :
1300
1463
                  delayed ? " DELAYED " : opt_ignore ? " IGNORE " : "");
1301
1464
 
1302
 
  verbose_msg(_("-- Retrieving table structure for table %s...\n"), table);
 
1465
  verbose_msg("-- Retrieving table structure for table %s...\n", table);
 
1466
 
 
1467
  len= snprintf(query_buff, sizeof(query_buff),
 
1468
                "SET OPTION SQL_QUOTE_SHOW_CREATE=%d",
 
1469
                (opt_quoted || opt_keywords));
1303
1470
 
1304
1471
  result_table=     quote_name(table, table_buff, 1);
1305
1472
  opt_quoted_table= quote_name(table, table_buff2, 0);
1310
1477
    order_by= primary_key_fields(result_table);
1311
1478
  }
1312
1479
 
1313
 
  if (!opt_xml)
 
1480
  if (!opt_xml && !drizzle_query_with_error_report(drizzle, 0, query_buff))
1314
1481
  {
1315
1482
    /* using SHOW CREATE statement */
1316
1483
    if (!opt_no_create_info)
1317
1484
    {
1318
1485
      /* Make an sql-file, if path was given iow. option -T was given */
1319
1486
      char buff[20+FN_REFLEN];
1320
 
      const drizzle_column_st *column;
 
1487
      const DRIZZLE_FIELD *field;
1321
1488
 
1322
1489
      snprintf(buff, sizeof(buff), "show create table %s", result_table);
1323
1490
 
1324
 
      if (drizzleclient_query_with_error_report(&dcon, &result, buff, false))
1325
 
        return false;
 
1491
      if (switch_character_set_results(drizzle, "binary") ||
 
1492
          drizzle_query_with_error_report(drizzle, &result, buff) ||
 
1493
          switch_character_set_results(drizzle, default_charset))
 
1494
        return(0);
1326
1495
 
1327
1496
      if (path)
1328
1497
      {
1329
1498
        if (!(sql_file= open_sql_file_for_table(table)))
1330
 
        {
1331
 
          drizzle_result_free(&result);
1332
 
          return false;
1333
 
        }
 
1499
          return(0);
1334
1500
 
1335
1501
        write_header(sql_file, db);
1336
1502
      }
1337
1503
      if (!opt_xml && opt_comments)
1338
1504
      {
 
1505
      if (strcmp (table_type, "VIEW") == 0)         /* view */
 
1506
        fprintf(sql_file, "\n--\n-- Temporary table structure for view %s\n--\n\n",
 
1507
                result_table);
 
1508
      else
1339
1509
        fprintf(sql_file, "\n--\n-- Table structure for table %s\n--\n\n",
1340
1510
                result_table);
1341
1511
        check_io(sql_file);
1342
1512
      }
1343
1513
      if (opt_drop)
1344
1514
      {
 
1515
      /*
 
1516
        Even if the "table" is a view, we do a DROP TABLE here.  The
 
1517
        view-specific code below fills in the DROP VIEW.
 
1518
       */
 
1519
        fprintf(sql_file, "DROP TABLE IF EXISTS %s;\n",
 
1520
                opt_quoted_table);
 
1521
        check_io(sql_file);
 
1522
      }
 
1523
 
 
1524
      field= drizzle_fetch_field_direct(result, 0);
 
1525
      if (strcmp(field->name, "View") == 0)
 
1526
      {
 
1527
        char *scv_buff= NULL;
 
1528
 
 
1529
        verbose_msg("-- It's a view, create dummy table for view\n");
 
1530
 
 
1531
        /* save "show create" statement for later */
 
1532
        if ((row= drizzle_fetch_row(result)) && (scv_buff=row[1]))
 
1533
          scv_buff= my_strdup(scv_buff, MYF(0));
 
1534
 
 
1535
        drizzle_free_result(result);
 
1536
 
1345
1537
        /*
1346
 
          Even if the "table" is a view, we do a DROP TABLE here.
 
1538
          Create a table with the same name as the view and with columns of
 
1539
          the same name in order to satisfy views that depend on this view.
 
1540
          The table will be removed when the actual view is created.
 
1541
 
 
1542
          The properties of each column, aside from the data type, are not
 
1543
          preserved in this temporary table, because they are not necessary.
 
1544
 
 
1545
          This will not be necessary once we can determine dependencies
 
1546
          between views and can simply dump them in the appropriate order.
1347
1547
        */
1348
 
        fprintf(sql_file, "DROP TABLE IF EXISTS %s;\n", opt_quoted_table);
1349
 
        check_io(sql_file);
 
1548
        snprintf(query_buff, sizeof(query_buff),
 
1549
                 "SHOW FIELDS FROM %s", result_table);
 
1550
        if (switch_character_set_results(drizzle, "binary") ||
 
1551
            drizzle_query_with_error_report(drizzle, &result, query_buff) ||
 
1552
            switch_character_set_results(drizzle, default_charset))
 
1553
        {
 
1554
          /*
 
1555
            View references invalid or privileged table/col/fun (err 1356),
 
1556
            so we cannot create a stand-in table.  Be defensive and dump
 
1557
            a comment with the view's 'show create' statement. (Bug #17371)
 
1558
          */
 
1559
 
 
1560
          if (drizzle_errno(drizzle) == ER_VIEW_INVALID)
 
1561
            fprintf(sql_file, "\n-- failed on view %s: %s\n\n", result_table, scv_buff ? scv_buff : "");
 
1562
 
 
1563
          free(scv_buff);
 
1564
 
 
1565
          return(0);
 
1566
        }
 
1567
        else
 
1568
          free(scv_buff);
 
1569
 
 
1570
        if (drizzle_num_rows(result))
 
1571
        {
 
1572
          if (opt_drop)
 
1573
          {
 
1574
            /*
 
1575
              We have already dropped any table of the same name above, so
 
1576
              here we just drop the view.
 
1577
            */
 
1578
 
 
1579
            fprintf(sql_file, "/*!50001 DROP VIEW IF EXISTS %s*/;\n",
 
1580
                    opt_quoted_table);
 
1581
            check_io(sql_file);
 
1582
          }
 
1583
 
 
1584
          fprintf(sql_file,
 
1585
                  "/*!50001 CREATE TABLE %s (\n",
 
1586
                  result_table);
 
1587
 
 
1588
          /*
 
1589
            Get first row, following loop will prepend comma - keeps from
 
1590
            having to know if the row being printed is last to determine if
 
1591
            there should be a _trailing_ comma.
 
1592
          */
 
1593
 
 
1594
          row= drizzle_fetch_row(result);
 
1595
 
 
1596
          fprintf(sql_file, "  %s %s", quote_name(row[0], name_buff, 0),
 
1597
                  row[1]);
 
1598
 
 
1599
          while((row= drizzle_fetch_row(result)))
 
1600
          {
 
1601
            /* col name, col type */
 
1602
            fprintf(sql_file, ",\n  %s %s",
 
1603
                    quote_name(row[0], name_buff, 0), row[1]);
 
1604
          }
 
1605
          fprintf(sql_file, "\n) */;\n"); 
 
1606
          check_io(sql_file);
 
1607
        }
 
1608
 
 
1609
        drizzle_free_result(result);
 
1610
 
 
1611
        if (path)
 
1612
          my_fclose(sql_file, MYF(MY_WME));
 
1613
 
 
1614
        seen_views= 1;
 
1615
        return(0);
1350
1616
      }
1351
1617
 
1352
 
      column= drizzle_column_index(&result, 0);
1353
 
 
1354
 
      row= drizzle_row_next(&result);
 
1618
      row= drizzle_fetch_row(result);
1355
1619
 
1356
1620
      fprintf(sql_file, "%s;\n", row[1]);
1357
1621
 
1358
1622
      check_io(sql_file);
1359
 
      drizzle_result_free(&result);
 
1623
      drizzle_free_result(result);
1360
1624
    }
1361
 
 
1362
1625
    snprintf(query_buff, sizeof(query_buff), "show fields from %s",
1363
1626
             result_table);
1364
 
 
1365
 
    if (drizzleclient_query_with_error_report(&dcon, &result, query_buff, false))
 
1627
    if (drizzle_query_with_error_report(drizzle, &result, query_buff))
1366
1628
    {
1367
1629
      if (path)
1368
 
        fclose(sql_file);
1369
 
      return false;
 
1630
        my_fclose(sql_file, MYF(MY_WME));
 
1631
      return(0);
1370
1632
    }
1371
1633
 
1372
1634
    /*
1396
1658
      }
1397
1659
    }
1398
1660
 
1399
 
    while ((row= drizzle_row_next(&result)))
 
1661
    while ((row= drizzle_fetch_row(result)))
1400
1662
    {
1401
1663
      if (complete_insert)
1402
1664
      {
1408
1670
        insert_pat.append(quote_name(row[SHOW_FIELDNAME], name_buff, 0));
1409
1671
      }
1410
1672
    }
1411
 
    *num_fields= drizzle_result_row_count(&result);
1412
 
    drizzle_result_free(&result);
 
1673
    num_fields= drizzle_num_rows(result);
 
1674
    drizzle_free_result(result);
1413
1675
  }
1414
1676
  else
1415
1677
  {
1416
 
    verbose_msg(_("%s: Warning: Can't set SQL_QUOTE_SHOW_CREATE option (%s)\n"),
1417
 
                internal::my_progname, drizzle_con_error(&dcon));
 
1678
    verbose_msg("%s: Warning: Can't set SQL_QUOTE_SHOW_CREATE option (%s)\n",
 
1679
                my_progname, drizzle_error(drizzle));
1418
1680
 
1419
1681
    snprintf(query_buff, sizeof(query_buff), "show fields from %s",
1420
1682
             result_table);
1421
 
    if (drizzleclient_query_with_error_report(&dcon, &result, query_buff, false))
1422
 
      return false;
 
1683
    if (drizzle_query_with_error_report(drizzle, &result, query_buff))
 
1684
      return(0);
1423
1685
 
1424
1686
    /* Make an sql-file, if path was given iow. option -T was given */
1425
1687
    if (!opt_no_create_info)
1427
1689
      if (path)
1428
1690
      {
1429
1691
        if (!(sql_file= open_sql_file_for_table(table)))
1430
 
        {
1431
 
          drizzle_result_free(&result);
1432
 
          return false;
1433
 
        }
 
1692
          return(0);
1434
1693
        write_header(sql_file, db);
1435
1694
      }
1436
1695
      if (!opt_xml && opt_comments)
1441
1700
      if (!opt_xml)
1442
1701
        fprintf(sql_file, "CREATE TABLE %s (\n", result_table);
1443
1702
      else
1444
 
        print_xml_tag(sql_file, "\t", "\n", "table_structure", "name=", table,
1445
 
                      NULL);
 
1703
        print_xml_tag(sql_file, "\t", "\n", "table_structure", "name=", table, 
 
1704
                NULL);
1446
1705
      check_io(sql_file);
1447
1706
    }
1448
1707
 
1465
1724
      }
1466
1725
    }
1467
1726
 
1468
 
    while ((row= drizzle_row_next(&result)))
 
1727
    while ((row= drizzle_fetch_row(result)))
1469
1728
    {
1470
 
      size_t *lengths= drizzle_row_field_sizes(&result);
 
1729
      uint32_t *lengths= drizzle_fetch_lengths(result);
1471
1730
      if (init)
1472
1731
      {
1473
1732
        if (!opt_xml && !opt_no_create_info)
1485
1744
      {
1486
1745
        if (opt_xml)
1487
1746
        {
1488
 
          print_xml_row(sql_file, "field", &result, &row);
 
1747
          print_xml_row(sql_file, "field", result, &row);
1489
1748
          continue;
1490
1749
        }
1491
1750
 
1509
1768
        check_io(sql_file);
1510
1769
      }
1511
1770
    }
1512
 
    *num_fields= drizzle_result_row_count(&result);
1513
 
    drizzle_result_free(&result);
1514
 
 
 
1771
    num_fields= drizzle_num_rows(result);
 
1772
    drizzle_free_result(result);
1515
1773
    if (!opt_no_create_info)
1516
1774
    {
1517
1775
      /* Make an sql-file, if path was given iow. option -T was given */
1518
1776
      char buff[20+FN_REFLEN];
1519
 
      uint32_t keynr,primary_key;
 
1777
      uint keynr,primary_key;
1520
1778
      snprintf(buff, sizeof(buff), "show keys from %s", result_table);
1521
 
      if (drizzleclient_query_with_error_report(&dcon, &result, buff, false))
 
1779
      if (drizzle_query_with_error_report(drizzle, &result, buff))
1522
1780
      {
1523
 
        fprintf(stderr, _("%s: Can't get keys for table %s\n"),
1524
 
                internal::my_progname, result_table);
 
1781
        if (drizzle_errno(drizzle) == ER_WRONG_OBJECT)
 
1782
        {
 
1783
          /* it is VIEW */
 
1784
          fputs("\t\t<options Comment=\"view\" />\n", sql_file);
 
1785
          goto continue_xml;
 
1786
        }
 
1787
        fprintf(stderr, "%s: Can't get keys for table %s (%s)\n",
 
1788
                my_progname, result_table, drizzle_error(drizzle));
1525
1789
        if (path)
1526
 
          fclose(sql_file);
1527
 
        return false;
 
1790
          my_fclose(sql_file, MYF(MY_WME));
 
1791
        return(0);
1528
1792
      }
1529
1793
 
1530
1794
      /* Find first which key is primary key */
1531
1795
      keynr=0;
1532
1796
      primary_key=INT_MAX;
1533
 
      while ((row= drizzle_row_next(&result)))
 
1797
      while ((row= drizzle_fetch_row(result)))
1534
1798
      {
1535
1799
        if (atoi(row[3]) == 1)
1536
1800
        {
1546
1810
          }
1547
1811
        }
1548
1812
      }
1549
 
      drizzle_row_seek(&result,0);
 
1813
      drizzle_data_seek(result,0);
1550
1814
      keynr=0;
1551
 
      while ((row= drizzle_row_next(&result)))
 
1815
      while ((row= drizzle_fetch_row(result)))
1552
1816
      {
1553
1817
        if (opt_xml)
1554
1818
        {
1555
 
          print_xml_row(sql_file, "key", &result, &row);
 
1819
          print_xml_row(sql_file, "key", result, &row);
1556
1820
          continue;
1557
1821
        }
1558
1822
 
1576
1840
          fprintf(sql_file, " (%s)",row[7]);      /* Sub key */
1577
1841
        check_io(sql_file);
1578
1842
      }
1579
 
      drizzle_result_free(&result);
 
1843
      drizzle_free_result(result);
1580
1844
      if (!opt_xml)
1581
1845
      {
1582
1846
        if (keynr)
1584
1848
        fputs("\n)",sql_file);
1585
1849
        check_io(sql_file);
1586
1850
      }
 
1851
 
1587
1852
      /* Get DRIZZLE specific create options */
1588
1853
      if (create_options)
1589
1854
      {
1590
 
        char show_name_buff[DRIZZLE_MAX_COLUMN_NAME_SIZE*2+2+24];
 
1855
        char show_name_buff[NAME_LEN*2+2+24];
1591
1856
 
1592
1857
        /* Check memory for quote_for_like() */
1593
1858
        snprintf(buff, sizeof(buff), "show table status like %s",
1594
1859
                 quote_for_like(table, show_name_buff));
1595
1860
 
1596
 
        if (!drizzleclient_query_with_error_report(&dcon, &result, buff, false))
 
1861
        if (drizzle_query_with_error_report(drizzle, &result, buff))
1597
1862
        {
1598
 
          if (!(row= drizzle_row_next(&result)))
1599
 
          {
1600
 
            fprintf(stderr,
1601
 
                    _("Error: Couldn't read status information for table %s\n"),
1602
 
                    result_table);
 
1863
          if (drizzle_errno(drizzle) != ER_PARSE_ERROR)
 
1864
          {                                     /* If old DRIZZLE version */
 
1865
            verbose_msg("-- Warning: Couldn't get status information for " \
 
1866
                        "table %s (%s)\n", result_table,drizzle_error(drizzle));
1603
1867
          }
 
1868
        }
 
1869
        else if (!(row= drizzle_fetch_row(result)))
 
1870
        {
 
1871
          fprintf(stderr,
 
1872
                  "Error: Couldn't read status information for table %s (%s)\n",
 
1873
                  result_table,drizzle_error(drizzle));
 
1874
        }
 
1875
        else
 
1876
        {
 
1877
          if (opt_xml)
 
1878
            print_xml_row(sql_file, "options", result, &row);
1604
1879
          else
1605
1880
          {
1606
 
            if (opt_xml)
1607
 
              print_xml_row(sql_file, "options", &result, &row);
1608
 
            else
1609
 
            {
1610
 
              fputs("/*!",sql_file);
1611
 
              print_value(sql_file,&result,row,"engine=","Engine",0);
1612
 
              print_value(sql_file,&result,row,"","Create_options",0);
1613
 
              print_value(sql_file,&result,row,"comment=","Comment",1);
1614
 
 
1615
 
              fputs(" */",sql_file);
1616
 
              check_io(sql_file);
1617
 
            }
 
1881
            fputs("/*!",sql_file);
 
1882
            print_value(sql_file,result,row,"engine=","Engine",0);
 
1883
            print_value(sql_file,result,row,"","Create_options",0);
 
1884
            print_value(sql_file,result,row,"comment=","Comment",1);
 
1885
            fputs(" */",sql_file);
 
1886
            check_io(sql_file);
1618
1887
          }
1619
 
          drizzle_result_free(&result);
1620
1888
        }
 
1889
        drizzle_free_result(result);              /* Is always safe to free */
1621
1890
      }
 
1891
continue_xml:
1622
1892
      if (!opt_xml)
1623
1893
        fputs(";\n", sql_file);
1624
1894
      else
1626
1896
      check_io(sql_file);
1627
1897
    }
1628
1898
  }
1629
 
  if (complete_insert) {
 
1899
  if (complete_insert)
 
1900
  {
1630
1901
    insert_pat.append(") VALUES ");
1631
1902
    if (!extended_insert)
1632
1903
      insert_pat.append("(");
1635
1906
  {
1636
1907
    fputs("\n", sql_file);
1637
1908
    write_footer(sql_file);
1638
 
    fclose(sql_file);
 
1909
    my_fclose(sql_file, MYF(MY_WME));
1639
1910
  }
1640
 
  return true;
 
1911
  return((uint) num_fields);
1641
1912
} /* get_table_structure */
1642
1913
 
1643
1914
static void add_load_option(string &str, const char *option,
1650
1921
  }
1651
1922
 
1652
1923
  str.append(option);
1653
 
 
 
1924
  
1654
1925
  if (strncmp(option_value, "0x", sizeof("0x")-1) == 0)
1655
1926
  {
1656
1927
    /* It's a hex constant, don't escape */
1673
1944
 
1674
1945
static void field_escape(string &in, const char *from)
1675
1946
{
1676
 
  uint32_t end_backslashes= 0;
 
1947
  uint end_backslashes= 0; 
1677
1948
 
1678
1949
  in.append("'");
1679
1950
 
1705
1976
 
1706
1977
/*
1707
1978
 
1708
 
  SYNOPSIS
 
1979
 SYNOPSIS
1709
1980
  dump_table()
1710
1981
 
1711
1982
  dump_table saves database contents as a series of INSERT statements.
1712
1983
 
1713
1984
  ARGS
1714
 
  table - table name
1715
 
  db    - db name
 
1985
   table - table name
 
1986
   db    - db name
1716
1987
 
1717
 
  RETURNS
1718
 
  void
 
1988
   RETURNS
 
1989
    void
1719
1990
*/
1720
1991
 
1721
1992
 
1722
1993
static void dump_table(char *table, char *db)
1723
1994
{
1724
1995
  char ignore_flag;
1725
 
  char table_buff[DRIZZLE_MAX_TABLE_SIZE+3];
 
1996
  char buf[200], table_buff[NAME_LEN+3];
1726
1997
  string query_string;
1727
 
  char table_type[DRIZZLE_MAX_TABLE_SIZE];
1728
 
  char *result_table, table_buff2[DRIZZLE_MAX_TABLE_SIZE*2+3], *opt_quoted_table;
 
1998
  char table_type[NAME_LEN];
 
1999
  char *result_table, table_buff2[NAME_LEN*2+3], *opt_quoted_table;
1729
2000
  int error= 0;
1730
 
  uint32_t rownr, row_break, total_length, init_length;
1731
 
  uint64_t num_fields= 0;
1732
 
  drizzle_return_t ret;
1733
 
  drizzle_result_st result;
1734
 
  drizzle_column_st *column;
1735
 
  drizzle_row_t row;
 
2001
  uint32_t         rownr, row_break, total_length, init_length;
 
2002
  uint num_fields;
 
2003
  DRIZZLE_RES     *res;
 
2004
  DRIZZLE_FIELD   *field;
 
2005
  DRIZZLE_ROW     row;
1736
2006
 
1737
2007
 
1738
2008
  /*
1739
2009
    Make sure you get the create table info before the following check for
1740
2010
    --no-data flag below. Otherwise, the create table info won't be printed.
1741
2011
  */
1742
 
  if (!get_table_structure(table, db, table_type, &ignore_flag, &num_fields))
1743
 
  {
1744
 
    maybe_die(EX_TABLE_STATUS, _("Error retrieving table structure for table: \"%s\""), table);
 
2012
  num_fields= get_table_structure(table, db, table_type, &ignore_flag);
 
2013
 
 
2014
  /*
 
2015
    The "table" could be a view.  If so, we don't do anything here.
 
2016
  */
 
2017
  if (strcmp(table_type, "VIEW") == 0)
1745
2018
    return;
1746
 
  }
1747
2019
 
1748
2020
  /* Check --no-data flag */
1749
2021
  if (opt_no_data)
1750
2022
  {
1751
 
    verbose_msg(_("-- Skipping dump data for table '%s', --no-data was used\n"),
 
2023
    verbose_msg("-- Skipping dump data for table '%s', --no-data was used\n",
1752
2024
                table);
1753
2025
    return;
1754
2026
  }
1755
2027
 
1756
2028
  /*
1757
2029
    If the table type is a merge table or any type that has to be
1758
 
    _completely_ ignored and no data dumped
 
2030
     _completely_ ignored and no data dumped
1759
2031
  */
1760
2032
  if (ignore_flag & IGNORE_DATA)
1761
2033
  {
1762
 
    verbose_msg(_("-- Warning: Skipping data for table '%s' because " \
1763
 
                  "it's of type %s\n"), table, table_type);
 
2034
    verbose_msg("-- Warning: Skipping data for table '%s' because " \
 
2035
                "it's of type %s\n", table, table_type);
1764
2036
    return;
1765
2037
  }
1766
2038
  /* Check that there are any fields in the table */
1767
2039
  if (num_fields == 0)
1768
2040
  {
1769
 
    verbose_msg(_("-- Skipping dump data for table '%s', it has no fields\n"),
 
2041
    verbose_msg("-- Skipping dump data for table '%s', it has no fields\n",
1770
2042
                table);
1771
2043
    return;
1772
2044
  }
1773
2045
 
 
2046
  /*
 
2047
     Check --skip-events flag: it is not enough to skip creation of events
 
2048
     discarding SHOW CREATE EVENT statements generation. The myslq.event
 
2049
     table data should be skipped too.
 
2050
  */
 
2051
  if (!opt_events && !my_strcasecmp(&my_charset_utf8_general_ci, db, "mysql") &&
 
2052
      !my_strcasecmp(&my_charset_utf8_general_ci, table, "event"))
 
2053
  {
 
2054
    verbose_msg("-- Skipping data table mysql.event, --skip-events was used\n");
 
2055
    return;
 
2056
  }
 
2057
 
1774
2058
  result_table= quote_name(table,table_buff, 1);
1775
2059
  opt_quoted_table= quote_name(table, table_buff2, 0);
1776
2060
 
1777
 
  verbose_msg(_("-- Sending SELECT query...\n"));
 
2061
  verbose_msg("-- Sending SELECT query...\n");
1778
2062
 
1779
2063
  query_string.clear();
1780
2064
  query_string.reserve(1024);
1787
2071
      Convert the path to native os format
1788
2072
      and resolve to the full filepath.
1789
2073
    */
1790
 
    internal::convert_dirname(tmp_path,path,NULL);
1791
 
    internal::my_load_path(tmp_path, tmp_path, NULL);
1792
 
    internal::fn_format(filename, table, tmp_path, ".txt", MYF(MY_UNPACK_FILENAME));
 
2074
    convert_dirname(tmp_path,path,NULL);    
 
2075
    my_load_path(tmp_path, tmp_path, NULL);
 
2076
    fn_format(filename, table, tmp_path, ".txt", MYF(MY_UNPACK_FILENAME));
1793
2077
 
1794
2078
    /* Must delete the file that 'INTO OUTFILE' will write to */
1795
 
    internal::my_delete(filename, MYF(0));
 
2079
    my_delete(filename, MYF(0));
 
2080
 
 
2081
    /* convert to a unix path name to stick into the query */
 
2082
    to_unix_path(filename);
1796
2083
 
1797
2084
    /* now build the query string */
1798
2085
 
1802
2089
 
1803
2090
    if (fields_terminated || enclosed || opt_enclosed || escaped)
1804
2091
      query_string.append( " FIELDS");
1805
 
 
 
2092
    
1806
2093
    add_load_option(query_string, " TERMINATED BY ", fields_terminated);
1807
2094
    add_load_option(query_string, " ENCLOSED BY ", enclosed);
1808
2095
    add_load_option(query_string, " OPTIONALLY ENCLOSED BY ", opt_enclosed);
1824
2111
      query_string.append( order_by);
1825
2112
    }
1826
2113
 
1827
 
    if (drizzle_query(&dcon, &result, query_string.c_str(),
1828
 
                      query_string.length(), &ret) == NULL ||
1829
 
        ret != DRIZZLE_RETURN_OK)
 
2114
    if (drizzle_real_query(drizzle, query_string.c_str(), query_string.length()))
1830
2115
    {
1831
 
      DB_error(&result, ret, _("when executing 'SELECT INTO OUTFILE'"));
1832
 
 
 
2116
      DB_error(drizzle, "when executing 'SELECT INTO OUTFILE'");
1833
2117
      return;
1834
2118
    }
1835
 
    drizzle_result_free(&result);
1836
2119
  }
1837
2120
  else
1838
2121
  {
1839
2122
    if (!opt_xml && opt_comments)
1840
2123
    {
1841
 
      fprintf(md_result_file,_("\n--\n-- Dumping data for table %s\n--\n"),
 
2124
      fprintf(md_result_file,"\n--\n-- Dumping data for table %s\n--\n",
1842
2125
              result_table);
1843
2126
      check_io(md_result_file);
1844
2127
    }
1845
 
 
 
2128
    
1846
2129
    query_string.append( "SELECT * FROM ");
1847
2130
    query_string.append( result_table);
1848
2131
 
1853
2136
        fprintf(md_result_file, "-- WHERE:  %s\n", where);
1854
2137
        check_io(md_result_file);
1855
2138
      }
1856
 
 
 
2139
      
1857
2140
      query_string.append( " WHERE ");
1858
2141
      query_string.append( where);
1859
2142
    }
1873
2156
      fputs("\n", md_result_file);
1874
2157
      check_io(md_result_file);
1875
2158
    }
1876
 
    if (drizzleclient_query_with_error_report(&dcon, &result,
1877
 
                                              query_string.c_str(), quick))
1878
 
    {
 
2159
    if (drizzle_query_with_error_report(drizzle, 0, query_string.c_str()))
 
2160
    {
 
2161
      DB_error(drizzle, "when retrieving data from server");
 
2162
      goto err;
 
2163
    }
 
2164
    if (quick)
 
2165
      res=drizzle_use_result(drizzle);
 
2166
    else
 
2167
      res=drizzle_store_result(drizzle);
 
2168
    if (!res)
 
2169
    {
 
2170
      DB_error(drizzle, "when retrieving data from server");
1879
2171
      goto err;
1880
2172
    }
1881
2173
 
1882
 
    verbose_msg(_("-- Retrieving rows...\n"));
1883
 
    if (drizzle_result_column_count(&result) != num_fields)
 
2174
    verbose_msg("-- Retrieving rows...\n");
 
2175
    if (drizzle_num_fields(res) != num_fields)
1884
2176
    {
1885
 
      fprintf(stderr,_("%s: Error in field count for table: %s !  Aborting.\n"),
1886
 
              internal::my_progname, result_table);
 
2177
      fprintf(stderr,"%s: Error in field count for table: %s !  Aborting.\n",
 
2178
              my_progname, result_table);
1887
2179
      error= EX_CONSCHECK;
1888
 
      drizzle_result_free(&result);
1889
2180
      goto err;
1890
2181
    }
1891
2182
 
 
2183
    if (opt_lock)
 
2184
    {
 
2185
      fprintf(md_result_file,"LOCK TABLES %s WRITE;\n", opt_quoted_table);
 
2186
      check_io(md_result_file);
 
2187
    }
1892
2188
    /* Moved disable keys to after lock per bug 15977 */
1893
2189
    if (opt_disable_keys)
1894
2190
    {
1895
 
      fprintf(md_result_file, "ALTER TABLE %s DISABLE KEYS;\n",
1896
 
              opt_quoted_table);
 
2191
      fprintf(md_result_file, "/*!40000 ALTER TABLE %s DISABLE KEYS */;\n",
 
2192
              opt_quoted_table);
1897
2193
      check_io(md_result_file);
1898
2194
    }
1899
2195
 
1900
 
    total_length= DRIZZLE_MAX_LINE_LENGTH;                /* Force row break */
 
2196
    total_length= opt_net_buffer_length;                /* Force row break */
1901
2197
    row_break=0;
1902
2198
    rownr=0;
1903
 
    init_length=(uint32_t) insert_pat.length()+4;
 
2199
    init_length=(uint) insert_pat.length()+4;
1904
2200
    if (opt_xml)
1905
2201
      print_xml_tag(md_result_file, "\t", "\n", "table_data", "name=", table,
1906
 
                    NULL);
 
2202
              NULL);
1907
2203
    if (opt_autocommit)
1908
2204
    {
1909
2205
      fprintf(md_result_file, "set autocommit=0;\n");
1910
2206
      check_io(md_result_file);
1911
2207
    }
1912
2208
 
1913
 
    row= NULL;
1914
 
 
1915
 
    while (1)
 
2209
    while ((row= drizzle_fetch_row(res)))
1916
2210
    {
1917
 
      uint32_t i;
1918
 
      size_t *lengths;
1919
 
 
1920
 
      if (quick)
1921
 
      {
1922
 
        if (row)
1923
 
          drizzle_row_free(&result, row);
1924
 
 
1925
 
        row= drizzle_row_buffer(&result, &ret);
1926
 
        if (ret != DRIZZLE_RETURN_OK)
1927
 
        {
1928
 
          fprintf(stderr,
1929
 
                  _("%s: Error reading rows for table: %s (%d:%s) ! Aborting.\n"),
1930
 
                  internal::my_progname, result_table, ret, drizzle_con_error(&dcon));
1931
 
          drizzle_result_free(&result);
1932
 
          goto err;
1933
 
        }
1934
 
      }
1935
 
      else
1936
 
        row= drizzle_row_next(&result);
1937
 
 
1938
 
      if (row == NULL)
1939
 
        break;
1940
 
 
1941
 
      lengths= drizzle_row_field_sizes(&result);
1942
 
 
 
2211
      uint i;
 
2212
      uint32_t *lengths= drizzle_fetch_lengths(res);
1943
2213
      rownr++;
1944
 
      if ((rownr % show_progress_size) == 0)
1945
 
      {
1946
 
        verbose_msg(_("-- %"PRIu32" of ~%"PRIu64" rows dumped for table %s\n"), rownr, total_rows, opt_quoted_table);
1947
 
      }
1948
2214
      if (!extended_insert && !opt_xml)
1949
2215
      {
1950
2216
        fputs(insert_pat.c_str(),md_result_file);
1951
2217
        check_io(md_result_file);
1952
2218
      }
1953
 
      drizzle_column_seek(&result,0);
 
2219
      drizzle_field_seek(res,0);
1954
2220
 
1955
2221
      if (opt_xml)
1956
2222
      {
1958
2224
        check_io(md_result_file);
1959
2225
      }
1960
2226
 
1961
 
      for (i= 0; i < drizzle_result_column_count(&result); i++)
 
2227
      for (i= 0; i < drizzle_num_fields(res); i++)
1962
2228
      {
1963
2229
        int is_blob;
1964
2230
        uint32_t length= lengths[i];
1965
2231
 
1966
 
        if (!(column= drizzle_column_next(&result)))
 
2232
        if (!(field= drizzle_fetch_field(res)))
1967
2233
          die(EX_CONSCHECK,
1968
 
              _("Not enough fields from table %s! Aborting.\n"),
1969
 
              result_table);
 
2234
                      "Not enough fields from table %s! Aborting.\n",
 
2235
                      result_table);
1970
2236
 
1971
2237
        /*
1972
 
          63 is my_charset_bin. If charsetnr is not 63,
1973
 
          we have not a BLOB but a TEXT column.
1974
 
          we'll dump in hex only BLOB columns.
 
2238
           63 is my_charset_bin. If charsetnr is not 63,
 
2239
           we have not a BLOB but a TEXT column.
 
2240
           we'll dump in hex only BLOB columns.
1975
2241
        */
1976
 
        is_blob= (opt_hex_blob && drizzle_column_charset(column) == 63 &&
1977
 
                  (drizzle_column_type(column) == DRIZZLE_COLUMN_TYPE_VARCHAR ||
1978
 
                   drizzle_column_type(column) == DRIZZLE_COLUMN_TYPE_BLOB)) ? 1 : 0;
 
2242
        is_blob= (opt_hex_blob && field->charsetnr == 63 &&
 
2243
                  (field->type == DRIZZLE_TYPE_VARCHAR ||
 
2244
                   field->type == DRIZZLE_TYPE_BLOB)) ? 1 : 0;
1979
2245
        if (extended_insert && !opt_xml)
1980
2246
        {
1981
2247
          if (i == 0)
1982
 
          {
1983
 
            extended_row.clear();
1984
 
            extended_row.append("(");
1985
 
          }
 
2248
            extended_row= "(";
1986
2249
          else
1987
2250
            extended_row.append(",");
1988
2251
 
1990
2253
          {
1991
2254
            if (length)
1992
2255
            {
1993
 
              if (!(drizzle_column_flags(column) & DRIZZLE_COLUMN_FLAGS_NUM))
 
2256
              if (!(field->type & NUM_FLAG))
1994
2257
              {
1995
2258
                /*
1996
2259
                  "length * 2 + 2" is OK for both HEX and non-HEX modes:
2011
2274
                else
2012
2275
                {
2013
2276
                  extended_row.append("'");
2014
 
                  drizzle_escape_string(tmp_str, row[i],length);
 
2277
                  drizzle_escape_string(tmp_str,
 
2278
                                        row[i],length);
2015
2279
                  extended_row.append(tmp_str);
2016
2280
                  extended_row.append("'");
2017
2281
                }
2022
2286
                /* change any strings ("inf", "-inf", "nan") into NULL */
2023
2287
                char *ptr= row[i];
2024
2288
                if (my_isalpha(charset_info, *ptr) || (*ptr == '-' &&
2025
 
                                                       my_isalpha(charset_info, ptr[1])))
 
2289
                    my_isalpha(charset_info, ptr[1])))
2026
2290
                  extended_row.append( "NULL");
2027
2291
                else
2028
2292
                {
2045
2309
          }
2046
2310
          if (row[i])
2047
2311
          {
2048
 
            if (!(drizzle_column_flags(column) & DRIZZLE_COLUMN_FLAGS_NUM))
 
2312
            if (!(field->type & NUM_FLAG))
2049
2313
            {
2050
2314
              if (opt_xml)
2051
2315
              {
2053
2317
                {
2054
2318
                  /* Define xsi:type="xs:hexBinary" for hex encoded data */
2055
2319
                  print_xml_tag(md_result_file, "\t\t", "", "field", "name=",
2056
 
                                drizzle_column_name(column), "xsi:type=", "xs:hexBinary", NULL);
 
2320
                                field->name, "xsi:type=", "xs:hexBinary", NULL);
2057
2321
                  print_blob_as_hex(md_result_file, row[i], length);
2058
2322
                }
2059
2323
                else
2060
2324
                {
2061
 
                  print_xml_tag(md_result_file, "\t\t", "", "field", "name=",
2062
 
                                drizzle_column_name(column), NULL);
 
2325
                  print_xml_tag(md_result_file, "\t\t", "", "field", "name=", 
 
2326
                                field->name, NULL);
2063
2327
                  print_quoted_xml(md_result_file, row[i], length);
2064
2328
                }
2065
2329
                fputs("</field>\n", md_result_file);
2079
2343
              if (opt_xml)
2080
2344
              {
2081
2345
                print_xml_tag(md_result_file, "\t\t", "", "field", "name=",
2082
 
                              drizzle_column_name(column), NULL);
 
2346
                        field->name, NULL);
2083
2347
                fputs(!my_isalpha(charset_info, *ptr) ? ptr: "NULL",
2084
2348
                      md_result_file);
2085
2349
                fputs("</field>\n", md_result_file);
2098
2362
              fputs("NULL", md_result_file);
2099
2363
            else
2100
2364
              print_xml_null_tag(md_result_file, "\t\t", "field name=",
2101
 
                                 drizzle_column_name(column), "\n");
 
2365
                                 field->name, "\n");
2102
2366
          }
2103
2367
          check_io(md_result_file);
2104
2368
        }
2115
2379
        uint32_t row_length;
2116
2380
        extended_row.append(")");
2117
2381
        row_length= 2 + extended_row.length();
2118
 
        if (total_length + row_length < DRIZZLE_MAX_LINE_LENGTH)
 
2382
        if (total_length + row_length < opt_net_buffer_length)
2119
2383
        {
2120
2384
          total_length+= row_length;
2121
2385
          fputc(',',md_result_file);            /* Always row break */
2142
2406
 
2143
2407
    /* XML - close table tag and supress regular output */
2144
2408
    if (opt_xml)
2145
 
      fputs("\t</table_data>\n", md_result_file);
 
2409
        fputs("\t</table_data>\n", md_result_file);
2146
2410
    else if (extended_insert && row_break)
2147
2411
      fputs(";\n", md_result_file);             /* If not empty table */
2148
2412
    fflush(md_result_file);
2149
2413
    check_io(md_result_file);
 
2414
    if (drizzle_errno(drizzle))
 
2415
    {
 
2416
      snprintf(buf, sizeof(buf),
 
2417
               "%s: Error %d: %s when dumping table %s at row: %d\n",
 
2418
               my_progname,
 
2419
               drizzle_errno(drizzle),
 
2420
               drizzle_error(drizzle),
 
2421
               result_table,
 
2422
               rownr);
 
2423
      fputs(buf,stderr);
 
2424
      error= EX_CONSCHECK;
 
2425
      goto err;
 
2426
    }
2150
2427
 
2151
2428
    /* Moved enable keys to before unlock per bug 15977 */
2152
2429
    if (opt_disable_keys)
2153
2430
    {
2154
 
      fprintf(md_result_file,"ALTER TABLE %s ENABLE KEYS;\n",
 
2431
      fprintf(md_result_file,"/*!40000 ALTER TABLE %s ENABLE KEYS */;\n",
2155
2432
              opt_quoted_table);
2156
2433
      check_io(md_result_file);
2157
2434
    }
 
2435
    if (opt_lock)
 
2436
    {
 
2437
      fputs("UNLOCK TABLES;\n", md_result_file);
 
2438
      check_io(md_result_file);
 
2439
    }
2158
2440
    if (opt_autocommit)
2159
2441
    {
2160
2442
      fprintf(md_result_file, "commit;\n");
2161
2443
      check_io(md_result_file);
2162
2444
    }
2163
 
    drizzle_result_free(&result);
 
2445
    drizzle_free_result(res);
2164
2446
  }
2165
2447
  return;
2166
2448
 
2172
2454
 
2173
2455
static char *getTableName(int reset)
2174
2456
{
2175
 
  static drizzle_result_st result;
2176
 
  static bool have_result= false;
2177
 
  drizzle_row_t row;
 
2457
  static DRIZZLE_RES *res= NULL;
 
2458
  DRIZZLE_ROW    row;
2178
2459
 
2179
 
  if (!have_result)
 
2460
  if (!res)
2180
2461
  {
2181
 
    if (drizzleclient_query_with_error_report(&dcon, &result, "SHOW TABLES", false))
2182
 
      return NULL;
2183
 
    have_result= true;
 
2462
    if (!(res= drizzle_list_tables(drizzle,NULL)))
 
2463
      return(NULL);
2184
2464
  }
2185
 
 
2186
 
  if ((row= drizzle_row_next(&result)))
2187
 
    return row[0];
 
2465
  if ((row= drizzle_fetch_row(res)))
 
2466
    return((char*) row[0]);
2188
2467
 
2189
2468
  if (reset)
2190
 
    drizzle_row_seek(&result, 0);
 
2469
    drizzle_data_seek(res,0);      /* We want to read again */
2191
2470
  else
2192
2471
  {
2193
 
    drizzle_result_free(&result);
2194
 
    have_result= false;
 
2472
    drizzle_free_result(res);
 
2473
    res= NULL;
2195
2474
  }
2196
 
  return NULL;
 
2475
  return(NULL);
2197
2476
} /* getTableName */
2198
2477
 
2199
2478
 
2200
2479
static int dump_all_databases()
2201
2480
{
2202
 
  drizzle_row_t row;
2203
 
  drizzle_result_st tableres;
 
2481
  DRIZZLE_ROW row;
 
2482
  DRIZZLE_RES *tableres;
2204
2483
  int result=0;
2205
2484
 
2206
 
  if (drizzleclient_query_with_error_report(&dcon, &tableres, "SHOW DATABASES", false))
 
2485
  if (drizzle_query_with_error_report(drizzle, &tableres, "SHOW DATABASES"))
2207
2486
    return 1;
2208
 
  while ((row= drizzle_row_next(&tableres)))
 
2487
  while ((row= drizzle_fetch_row(tableres)))
2209
2488
  {
2210
2489
    if (dump_all_tables_in_db(row[0]))
2211
2490
      result=1;
2212
2491
  }
2213
 
  drizzle_result_free(&tableres);
2214
2492
  return result;
2215
2493
}
2216
2494
/* dump_all_databases */
2232
2510
 
2233
2511
 
2234
2512
/*
2235
 
  Table Specific database initalization.
 
2513
Table Specific database initalization.
2236
2514
 
2237
 
  SYNOPSIS
 
2515
SYNOPSIS
2238
2516
  init_dumping_tables
2239
2517
  qdatabase      quoted name of the database
2240
2518
 
2241
 
  RETURN VALUES
 
2519
RETURN VALUES
2242
2520
  0        Success.
2243
2521
  1        Failure.
2244
2522
*/
2245
2523
 
2246
2524
int init_dumping_tables(char *qdatabase)
2247
2525
{
 
2526
 
 
2527
 
2248
2528
  if (!opt_create_db)
2249
2529
  {
2250
2530
    char qbuf[256];
2251
 
    drizzle_row_t row;
2252
 
    drizzle_result_st result;
2253
 
    drizzle_return_t ret;
 
2531
    DRIZZLE_ROW row;
 
2532
    DRIZZLE_RES *dbinfo;
2254
2533
 
2255
2534
    snprintf(qbuf, sizeof(qbuf),
2256
2535
             "SHOW CREATE DATABASE IF NOT EXISTS %s",
2257
2536
             qdatabase);
2258
2537
 
2259
 
    if (drizzle_query_str(&dcon, &result, qbuf, &ret) == NULL ||
2260
 
        ret != DRIZZLE_RETURN_OK)
 
2538
    if (drizzle_query(drizzle, qbuf) || !(dbinfo = drizzle_store_result(drizzle)))
2261
2539
    {
2262
 
      if (ret == DRIZZLE_RETURN_ERROR_CODE)
2263
 
        drizzle_result_free(&result);
2264
 
 
2265
2540
      /* Old server version, dump generic CREATE DATABASE */
2266
2541
      if (opt_drop_database)
2267
2542
        fprintf(md_result_file,
2268
 
                "\nDROP DATABASE IF EXISTS %s;\n",
 
2543
                "\n/*!40000 DROP DATABASE IF EXISTS %s*/;\n",
2269
2544
                qdatabase);
2270
2545
      fprintf(md_result_file,
2271
 
              "\nCREATE DATABASE IF NOT EXISTS %s;\n",
 
2546
              "\nCREATE DATABASE /*!32312 IF NOT EXISTS*/ %s;\n",
2272
2547
              qdatabase);
2273
2548
    }
2274
2549
    else
2275
2550
    {
2276
 
      if (drizzle_result_buffer(&result) == DRIZZLE_RETURN_OK)
 
2551
      if (opt_drop_database)
 
2552
        fprintf(md_result_file,
 
2553
                "\n/*!40000 DROP DATABASE IF EXISTS %s*/;\n",
 
2554
                qdatabase);
 
2555
      row = drizzle_fetch_row(dbinfo);
 
2556
      if (row[1])
2277
2557
      {
2278
 
        if (opt_drop_database)
2279
 
          fprintf(md_result_file,
2280
 
                  "\nDROP DATABASE IF EXISTS %s;\n",
2281
 
                  qdatabase);
2282
 
        row = drizzle_row_next(&result);
2283
 
        if (row != NULL && row[1])
2284
 
        {
2285
 
          fprintf(md_result_file,"\n%s;\n",row[1]);
2286
 
        }
 
2558
        fprintf(md_result_file,"\n%s;\n",row[1]);
2287
2559
      }
2288
 
      drizzle_result_free(&result);
 
2560
      drizzle_free_result(dbinfo);
2289
2561
    }
2290
2562
  }
2291
2563
  return(0);
2294
2566
 
2295
2567
static int init_dumping(char *database, int init_func(char*))
2296
2568
{
2297
 
  drizzle_result_st result;
2298
 
  drizzle_return_t ret;
2299
 
 
2300
 
  if (!my_strcasecmp(&my_charset_utf8_general_ci, database, "information_schema"))
 
2569
  if (drizzle_get_server_version(drizzle) >= 50003 &&
 
2570
      !my_strcasecmp(&my_charset_utf8_general_ci, database, "information_schema"))
2301
2571
    return 1;
2302
2572
 
2303
 
  if (drizzle_select_db(&dcon, &result, database, &ret) == NULL ||
2304
 
      ret != DRIZZLE_RETURN_OK)
 
2573
  if (drizzle_select_db(drizzle, database))
2305
2574
  {
2306
 
    DB_error(&result, ret, _("when executing 'SELECT INTO OUTFILE'"));
 
2575
    DB_error(drizzle, "when selecting the database");
2307
2576
    return 1;                   /* If --force */
2308
2577
  }
2309
 
  drizzle_result_free(&result);
2310
 
 
2311
2578
  if (!path && !opt_xml)
2312
2579
  {
2313
2580
    if (opt_databases || opt_alldbs)
2315
2582
      /*
2316
2583
        length of table name * 2 (if name contains quotes), 2 quotes and 0
2317
2584
      */
2318
 
      char quoted_database_buf[DRIZZLE_MAX_DB_SIZE*2+3];
 
2585
      char quoted_database_buf[NAME_LEN*2+3];
2319
2586
      char *qdatabase= quote_name(database,quoted_database_buf,opt_quoted);
2320
2587
      if (opt_comments)
2321
2588
      {
2338
2605
 
2339
2606
/* Return 1 if we should copy the table */
2340
2607
 
2341
 
static bool include_table(const char *hash_key, size_t key_size)
 
2608
static bool include_table(const unsigned char *hash_key, size_t len)
2342
2609
{
2343
 
  string match(hash_key, key_size);
2344
 
  drizzled::hash_set<string>::iterator iter= ignore_table.find(match);
2345
 
  return (iter == ignore_table.end());
 
2610
  return !hash_search(&ignore_table, hash_key, len);
2346
2611
}
2347
2612
 
2348
2613
 
2349
2614
static int dump_all_tables_in_db(char *database)
2350
2615
{
2351
2616
  char *table;
2352
 
  char hash_key[DRIZZLE_MAX_DB_SIZE+DRIZZLE_MAX_TABLE_SIZE+2];  /* "db.tablename" */
 
2617
  uint numrows;
 
2618
  char table_buff[NAME_LEN*2+3];
 
2619
  char hash_key[2*NAME_LEN+2];  /* "db.tablename" */
2353
2620
  char *afterdot;
2354
 
  drizzle_result_st result;
2355
 
  drizzle_return_t ret;
2356
 
 
2357
 
  memset(hash_key, 0, DRIZZLE_MAX_DB_SIZE+DRIZZLE_MAX_TABLE_SIZE+2);
2358
 
  afterdot= strcpy(hash_key, database) + strlen(database);
 
2621
  int using_mysql_db= my_strcasecmp(&my_charset_utf8_general_ci, database, "mysql");
 
2622
 
 
2623
 
 
2624
  afterdot= my_stpcpy(hash_key, database);
2359
2625
  *afterdot++= '.';
2360
2626
 
2361
2627
  if (init_dumping(database, init_dumping_tables))
2362
2628
    return(1);
2363
2629
  if (opt_xml)
2364
2630
    print_xml_tag(md_result_file, "", "\n", "database", "name=", database, NULL);
2365
 
  if (flush_logs)
 
2631
  if (lock_tables)
2366
2632
  {
2367
 
    if (drizzle_query_str(&dcon, &result, "FLUSH LOGS", &ret) == NULL ||
2368
 
        ret != DRIZZLE_RETURN_OK)
 
2633
    string query;
 
2634
    query= "LOCK TABLES ";
 
2635
    for (numrows= 0 ; (table= getTableName(1)) ; )
2369
2636
    {
2370
 
      DB_error(&result, ret, _("when doing refresh"));
2371
 
      /* We shall continue here, if --force was given */
 
2637
      char *end= my_stpcpy(afterdot, table);
 
2638
      if (include_table((unsigned char*) hash_key,end - hash_key))
 
2639
      {
 
2640
        numrows++;
 
2641
        query.append( quote_name(table, table_buff, 1));
 
2642
        query.append( " READ /*!32311 LOCAL */,");
 
2643
      }
2372
2644
    }
2373
 
    else
2374
 
      drizzle_result_free(&result);
 
2645
    if (numrows && drizzle_real_query(drizzle, query.c_str(), query.length()-1))
 
2646
      DB_error(drizzle, "when using LOCK TABLES");
 
2647
            /* We shall continue here, if --force was given */
 
2648
    query.clear();
 
2649
  }
 
2650
  if (flush_logs)
 
2651
  {
 
2652
    if (drizzle_refresh(drizzle, REFRESH_LOG))
 
2653
      DB_error(drizzle, "when doing refresh");
 
2654
           /* We shall continue here, if --force was given */
2375
2655
  }
2376
2656
  while ((table= getTableName(0)))
2377
2657
  {
2378
 
    char *end= strcpy(afterdot, table) + strlen(table);
2379
 
    if (include_table(hash_key, end - hash_key))
 
2658
    char *end= my_stpcpy(afterdot, table);
 
2659
    if (include_table((unsigned char*) hash_key, end - hash_key))
2380
2660
    {
2381
2661
      dump_table(table,database);
2382
2662
      free(order_by);
2388
2668
    fputs("</database>\n", md_result_file);
2389
2669
    check_io(md_result_file);
2390
2670
  }
2391
 
 
2392
 
  return 0;
 
2671
  if (lock_tables)
 
2672
    drizzle_query_with_error_report(drizzle, 0, "UNLOCK TABLES");
 
2673
  if (flush_privileges && using_mysql_db == 0)
 
2674
  {
 
2675
    fprintf(md_result_file,"\n--\n-- Flush Grant Tables \n--\n");
 
2676
    fprintf(md_result_file,"\n/*! FLUSH PRIVILEGES */;\n");
 
2677
  }
 
2678
  return(0);
2393
2679
} /* dump_all_tables_in_db */
2394
2680
 
2395
2681
 
2400
2686
  different case (e.g.  T1 vs t1)
2401
2687
 
2402
2688
  RETURN
2403
 
  pointer to the table name
2404
 
  0 if error
 
2689
    pointer to the table name
 
2690
    0 if error
2405
2691
*/
2406
2692
 
2407
 
static char *get_actual_table_name(const char *old_table_name,
2408
 
                                   drizzled::memory::Root *root)
 
2693
static char *get_actual_table_name(const char *old_table_name, MEM_ROOT *root)
2409
2694
{
2410
2695
  char *name= 0;
2411
 
  drizzle_result_st result;
2412
 
  drizzle_row_t  row;
2413
 
  char query[50 + 2*DRIZZLE_MAX_TABLE_SIZE];
 
2696
  DRIZZLE_RES  *table_res;
 
2697
  DRIZZLE_ROW  row;
 
2698
  char query[50 + 2*NAME_LEN];
2414
2699
  char show_name_buff[FN_REFLEN];
2415
 
  uint64_t num_rows;
2416
2700
 
2417
2701
 
2418
2702
  /* Check memory for quote_for_like() */
2420
2704
  snprintf(query, sizeof(query), "SHOW TABLES LIKE %s",
2421
2705
           quote_for_like(old_table_name, show_name_buff));
2422
2706
 
2423
 
  if (drizzleclient_query_with_error_report(&dcon, &result, query, false))
 
2707
  if (drizzle_query_with_error_report(drizzle, 0, query))
2424
2708
    return NULL;
2425
2709
 
2426
 
  num_rows= drizzle_result_row_count(&result);
2427
 
  if (num_rows > 0)
 
2710
  if ((table_res= drizzle_store_result(drizzle)))
2428
2711
  {
2429
 
    size_t *lengths;
2430
 
    /*
2431
 
      Return first row
2432
 
      TODO-> Return all matching rows
2433
 
    */
2434
 
    row= drizzle_row_next(&result);
2435
 
    lengths= drizzle_row_field_sizes(&result);
2436
 
    name= strmake_root(root, row[0], lengths[0]);
 
2712
    uint64_t num_rows= drizzle_num_rows(table_res);
 
2713
    if (num_rows > 0)
 
2714
    {
 
2715
      uint32_t *lengths;
 
2716
      /*
 
2717
        Return first row
 
2718
        TODO: Return all matching rows
 
2719
      */
 
2720
      row= drizzle_fetch_row(table_res);
 
2721
      lengths= drizzle_fetch_lengths(table_res);
 
2722
      name= strmake_root(root, row[0], lengths[0]);
 
2723
    }
 
2724
    drizzle_free_result(table_res);
2437
2725
  }
2438
 
  drizzle_result_free(&result);
2439
 
 
2440
2726
  return(name);
2441
2727
}
2442
2728
 
2443
2729
 
2444
2730
static int dump_selected_tables(char *db, char **table_names, int tables)
2445
2731
{
2446
 
  drizzled::memory::Root root;
 
2732
  char table_buff[NAME_LEN*2+3];
 
2733
  string lock_tables_query;
 
2734
  MEM_ROOT root;
2447
2735
  char **dump_tables, **pos, **end;
2448
 
  drizzle_result_st result;
2449
 
  drizzle_return_t ret;
2450
2736
 
2451
2737
 
2452
2738
  if (init_dumping(db, init_dumping_tables))
2453
2739
    return(1);
2454
2740
 
2455
 
  init_alloc_root(&root, 8192);
 
2741
  init_alloc_root(&root, 8192, 0);
2456
2742
  if (!(dump_tables= pos= (char**) alloc_root(&root, tables * sizeof(char *))))
2457
 
    die(EX_EOM, _("alloc_root failure."));
 
2743
     die(EX_EOM, "alloc_root failure.");
2458
2744
 
 
2745
  lock_tables_query= "LOCK TABLES ";
2459
2746
  for (; tables > 0 ; tables-- , table_names++)
2460
2747
  {
2461
2748
    /* the table name passed on commandline may be wrong case */
2462
2749
    if ((*pos= get_actual_table_name(*table_names, &root)))
2463
2750
    {
 
2751
      /* Add found table name to lock_tables_query */
 
2752
      if (lock_tables)
 
2753
      {
 
2754
        lock_tables_query.append( quote_name(*pos, table_buff, 1));
 
2755
        lock_tables_query.append( " READ /*!32311 LOCAL */,");
 
2756
      }
2464
2757
      pos++;
2465
2758
    }
2466
2759
    else
2469
2762
      {
2470
2763
        free_root(&root, MYF(0));
2471
2764
      }
2472
 
      maybe_die(EX_ILLEGAL_TABLE, _("Couldn't find table: \"%s\""), *table_names);
 
2765
      maybe_die(EX_ILLEGAL_TABLE, "Couldn't find table: \"%s\"", *table_names);
2473
2766
      /* We shall countinue here, if --force was given */
2474
2767
    }
2475
2768
  }
2476
2769
  end= pos;
2477
2770
 
 
2771
  if (lock_tables)
 
2772
  {
 
2773
    if (drizzle_real_query(drizzle, lock_tables_query.c_str(),
 
2774
                         lock_tables_query.length()-1))
 
2775
    {
 
2776
      if (!ignore_errors)
 
2777
      {
 
2778
        free_root(&root, MYF(0));
 
2779
      }
 
2780
      DB_error(drizzle, "when doing LOCK TABLES");
 
2781
       /* We shall countinue here, if --force was given */
 
2782
    }
 
2783
  }
2478
2784
  if (flush_logs)
2479
2785
  {
2480
 
    if (drizzle_query_str(&dcon, &result, "FLUSH LOGS", &ret) == NULL ||
2481
 
        ret != DRIZZLE_RETURN_OK)
 
2786
    if (drizzle_refresh(drizzle, REFRESH_LOG))
2482
2787
    {
2483
2788
      if (!ignore_errors)
2484
2789
        free_root(&root, MYF(0));
2485
 
      DB_error(&result, ret, _("when doing refresh"));
2486
 
      /* We shall countinue here, if --force was given */
 
2790
      DB_error(drizzle, "when doing refresh");
2487
2791
    }
2488
 
    else
2489
 
      drizzle_result_free(&result);
 
2792
     /* We shall countinue here, if --force was given */
2490
2793
  }
2491
2794
  if (opt_xml)
2492
2795
    print_xml_tag(md_result_file, "", "\n", "database", "name=", db, NULL);
2503
2806
    fputs("</database>\n", md_result_file);
2504
2807
    check_io(md_result_file);
2505
2808
  }
2506
 
  return 0;
 
2809
  if (lock_tables)
 
2810
    drizzle_query_with_error_report(drizzle, 0, "UNLOCK TABLES");
 
2811
  return(0);
2507
2812
} /* dump_selected_tables */
2508
2813
 
2509
 
static int do_flush_tables_read_lock(drizzle_con_st *drizzle_con)
 
2814
 
 
2815
static int do_show_master_status(DRIZZLE *drizzle_con)
 
2816
{
 
2817
  DRIZZLE_ROW row;
 
2818
  DRIZZLE_RES *master;
 
2819
  const char *comment_prefix=
 
2820
    (opt_master_data == DRIZZLE_OPT_MASTER_DATA_COMMENTED_SQL) ? "-- " : "";
 
2821
  if (drizzle_query_with_error_report(drizzle_con, &master, "SHOW MASTER STATUS"))
 
2822
  {
 
2823
    return 1;
 
2824
  }
 
2825
  else
 
2826
  {
 
2827
    row= drizzle_fetch_row(master);
 
2828
    if (row && row[0] && row[1])
 
2829
    {
 
2830
      /* SHOW MASTER STATUS reports file and position */
 
2831
      if (opt_comments)
 
2832
        fprintf(md_result_file,
 
2833
                "\n--\n-- Position to start replication or point-in-time "
 
2834
                "recovery from\n--\n\n");
 
2835
      fprintf(md_result_file,
 
2836
              "%sCHANGE MASTER TO MASTER_LOG_FILE='%s', MASTER_LOG_POS=%s;\n",
 
2837
              comment_prefix, row[0], row[1]);
 
2838
      check_io(md_result_file);
 
2839
    }
 
2840
    else if (!ignore_errors)
 
2841
    {
 
2842
      /* SHOW MASTER STATUS reports nothing and --force is not enabled */
 
2843
      my_printf_error(0, "Error: Binlogging on server not active",
 
2844
                      MYF(0));
 
2845
      drizzle_free_result(master);
 
2846
      maybe_exit(EX_DRIZZLEERR);
 
2847
      return 1;
 
2848
    }
 
2849
    drizzle_free_result(master);
 
2850
  }
 
2851
  return 0;
 
2852
}
 
2853
 
 
2854
static int do_stop_slave_sql(DRIZZLE *drizzle_con)
 
2855
{
 
2856
  DRIZZLE_RES *slave;
 
2857
  /* We need to check if the slave sql is running in the first place */
 
2858
  if (drizzle_query_with_error_report(drizzle_con, &slave, "SHOW SLAVE STATUS"))
 
2859
    return(1);
 
2860
  else
 
2861
  {
 
2862
    DRIZZLE_ROW row= drizzle_fetch_row(slave);
 
2863
    if (row && row[11])
 
2864
    {
 
2865
      /* if SLAVE SQL is not running, we don't stop it */
 
2866
      if (!strcmp(row[11],"No"))
 
2867
      {
 
2868
        drizzle_free_result(slave);
 
2869
        /* Silently assume that they don't have the slave running */
 
2870
        return(0);
 
2871
      }
 
2872
    }
 
2873
  }
 
2874
  drizzle_free_result(slave);
 
2875
 
 
2876
  /* now, stop slave if running */
 
2877
  if (drizzle_query_with_error_report(drizzle_con, 0, "STOP SLAVE SQL_THREAD"))
 
2878
    return(1);
 
2879
 
 
2880
  return(0);
 
2881
}
 
2882
 
 
2883
static int add_stop_slave(void)
 
2884
{
 
2885
  if (opt_comments)
 
2886
    fprintf(md_result_file,
 
2887
            "\n--\n-- stop slave statement to make a recovery dump)\n--\n\n");
 
2888
  fprintf(md_result_file, "STOP SLAVE;\n");
 
2889
  return(0);
 
2890
}
 
2891
 
 
2892
static int add_slave_statements(void)
 
2893
{
 
2894
  if (opt_comments)
 
2895
    fprintf(md_result_file,
 
2896
            "\n--\n-- start slave statement to make a recovery dump)\n--\n\n");
 
2897
  fprintf(md_result_file, "START SLAVE;\n");
 
2898
  return(0);
 
2899
}
 
2900
 
 
2901
static int do_show_slave_status(DRIZZLE *drizzle_con)
 
2902
{
 
2903
  DRIZZLE_RES *slave;
 
2904
  const char *comment_prefix=
 
2905
    (opt_slave_data == DRIZZLE_OPT_SLAVE_DATA_COMMENTED_SQL) ? "-- " : "";
 
2906
  if (drizzle_query_with_error_report(drizzle_con, &slave, "SHOW SLAVE STATUS"))
 
2907
  {
 
2908
    if (!ignore_errors)
 
2909
    {
 
2910
      /* SHOW SLAVE STATUS reports nothing and --force is not enabled */
 
2911
      my_printf_error(0, "Error: Slave not set up", MYF(0));
 
2912
    }
 
2913
    return 1;
 
2914
  }
 
2915
  else
 
2916
  {
 
2917
    DRIZZLE_ROW row= drizzle_fetch_row(slave);
 
2918
    if (row && row[9] && row[21])
 
2919
    {
 
2920
      /* SHOW MASTER STATUS reports file and position */
 
2921
      if (opt_comments)
 
2922
        fprintf(md_result_file,
 
2923
                "\n--\n-- Position to start replication or point-in-time "
 
2924
                "recovery from (the master of this slave)\n--\n\n");
 
2925
 
 
2926
      fprintf(md_result_file, "%sCHANGE MASTER TO ", comment_prefix);
 
2927
 
 
2928
      if (opt_include_master_host_port)
 
2929
      {
 
2930
        if (row[1])
 
2931
          fprintf(md_result_file, "MASTER_HOST='%s', ", row[1]);
 
2932
        if (row[3])
 
2933
          fprintf(md_result_file, "MASTER_PORT='%s', ", row[3]);
 
2934
      }
 
2935
      fprintf(md_result_file,
 
2936
              "MASTER_LOG_FILE='%s', MASTER_LOG_POS=%s;\n", row[9], row[21]);
 
2937
 
 
2938
      check_io(md_result_file);
 
2939
    }
 
2940
    drizzle_free_result(slave);
 
2941
  }
 
2942
  return 0;
 
2943
}
 
2944
 
 
2945
static int do_start_slave_sql(DRIZZLE *drizzle_con)
 
2946
{
 
2947
  DRIZZLE_RES *slave;
 
2948
  /* We need to check if the slave sql is stopped in the first place */
 
2949
  if (drizzle_query_with_error_report(drizzle_con, &slave, "SHOW SLAVE STATUS"))
 
2950
    return(1);
 
2951
  else
 
2952
  {
 
2953
    DRIZZLE_ROW row= drizzle_fetch_row(slave);
 
2954
    if (row && row[11])
 
2955
    {
 
2956
      /* if SLAVE SQL is not running, we don't start it */
 
2957
      if (!strcmp(row[11],"Yes"))
 
2958
      {
 
2959
        drizzle_free_result(slave);
 
2960
        /* Silently assume that they don't have the slave running */
 
2961
        return(0);
 
2962
      }
 
2963
    }
 
2964
  }
 
2965
  drizzle_free_result(slave);
 
2966
 
 
2967
  /* now, start slave if stopped */
 
2968
  if (drizzle_query_with_error_report(drizzle_con, 0, "START SLAVE"))
 
2969
  {
 
2970
    my_printf_error(0, "Error: Unable to start slave", MYF(0));
 
2971
    return 1;
 
2972
  }
 
2973
  return(0);
 
2974
}
 
2975
 
 
2976
 
 
2977
 
 
2978
static int do_flush_tables_read_lock(DRIZZLE *drizzle_con)
2510
2979
{
2511
2980
  /*
2512
2981
    We do first a FLUSH TABLES. If a long update is running, the FLUSH TABLES
2513
2982
    will wait but will not stall the whole mysqld, and when the long update is
2514
2983
    done the FLUSH TABLES WITH READ LOCK will start and succeed quickly. So,
2515
 
    FLUSH TABLES is to lower the probability of a stage where both drizzled
 
2984
    FLUSH TABLES is to lower the probability of a stage where both mysqldump
2516
2985
    and most client connections are stalled. Of course, if a second long
2517
2986
    update starts between the two FLUSHes, we have that bad stall.
2518
2987
  */
2519
2988
  return
2520
 
    ( drizzleclient_query_with_error_report(drizzle_con, 0, "FLUSH TABLES", false) ||
2521
 
      drizzleclient_query_with_error_report(drizzle_con, 0,
2522
 
                                            "FLUSH TABLES WITH READ LOCK", false) );
2523
 
}
2524
 
 
2525
 
static int do_unlock_tables(drizzle_con_st *drizzle_con)
2526
 
{
2527
 
  return drizzleclient_query_with_error_report(drizzle_con, 0, "UNLOCK TABLES", false);
2528
 
}
2529
 
 
2530
 
static int start_transaction(drizzle_con_st *drizzle_con)
2531
 
{
2532
 
  return (drizzleclient_query_with_error_report(drizzle_con, 0,
2533
 
                                                "SET SESSION TRANSACTION ISOLATION "
2534
 
                                                "LEVEL REPEATABLE READ", false) ||
2535
 
          drizzleclient_query_with_error_report(drizzle_con, 0,
2536
 
                                                "START TRANSACTION "
2537
 
                                                "WITH CONSISTENT SNAPSHOT", false));
2538
 
}
2539
 
 
2540
 
 
2541
 
static uint32_t find_set(TYPELIB *lib, const char *x, uint32_t length,
2542
 
                         char **err_pos, uint32_t *err_len)
 
2989
    ( drizzle_query_with_error_report(drizzle_con, 0, "FLUSH TABLES") ||
 
2990
      drizzle_query_with_error_report(drizzle_con, 0,
 
2991
                                    "FLUSH TABLES WITH READ LOCK") );
 
2992
}
 
2993
 
 
2994
 
 
2995
static int do_unlock_tables(DRIZZLE *drizzle_con)
 
2996
{
 
2997
  return drizzle_query_with_error_report(drizzle_con, 0, "UNLOCK TABLES");
 
2998
}
 
2999
 
 
3000
static int get_bin_log_name(DRIZZLE *drizzle_con,
 
3001
                            char* buff_log_name, uint buff_len)
 
3002
{
 
3003
  DRIZZLE_RES *res;
 
3004
  DRIZZLE_ROW row;
 
3005
 
 
3006
  if (drizzle_query(drizzle_con, "SHOW MASTER STATUS") ||
 
3007
      !(res= drizzle_store_result(drizzle)))
 
3008
    return 1;
 
3009
 
 
3010
  if (!(row= drizzle_fetch_row(res)))
 
3011
  {
 
3012
    drizzle_free_result(res);
 
3013
    return 1;
 
3014
  }
 
3015
  /*
 
3016
    Only one row is returned, and the first column is the name of the
 
3017
    active log.
 
3018
  */
 
3019
  strmake(buff_log_name, row[0], buff_len - 1);
 
3020
 
 
3021
  drizzle_free_result(res);
 
3022
  return 0;
 
3023
}
 
3024
 
 
3025
static int purge_bin_logs_to(DRIZZLE *drizzle_con, char* log_name)
 
3026
{
 
3027
  int err;
 
3028
  string str= "PURGE BINARY LOGS TO '";
 
3029
  str.append(log_name);
 
3030
  str.append("'");
 
3031
  err = drizzle_query_with_error_report(drizzle_con, 0, str.c_str());
 
3032
  return err;
 
3033
}
 
3034
 
 
3035
 
 
3036
static int start_transaction(DRIZZLE *drizzle_con)
 
3037
{
 
3038
  /*
 
3039
    We use BEGIN for old servers. --single-transaction --master-data will fail
 
3040
    on old servers, but that's ok as it was already silently broken (it didn't
 
3041
    do a consistent read, so better tell people frankly, with the error).
 
3042
 
 
3043
    We want the first consistent read to be used for all tables to dump so we
 
3044
    need the REPEATABLE READ level (not anything lower, for example READ
 
3045
    COMMITTED would give one new consistent read per dumped table).
 
3046
  */
 
3047
  if ((drizzle_get_server_version(drizzle_con) < 40100) && opt_master_data)
 
3048
  {
 
3049
    fprintf(stderr, "-- %s: the combination of --single-transaction and "
 
3050
            "--master-data requires a DRIZZLE server version of at least 4.1 "
 
3051
            "(current server's version is %s). %s\n",
 
3052
            ignore_errors ? "Warning" : "Error",
 
3053
            drizzle_con->server_version ? drizzle_con->server_version : "unknown",
 
3054
            ignore_errors ? "Continuing due to --force, backup may not be consistent across all tables!" : "Aborting.");
 
3055
    if (!ignore_errors)
 
3056
      exit(EX_DRIZZLEERR);
 
3057
  }
 
3058
 
 
3059
  return (drizzle_query_with_error_report(drizzle_con, 0,
 
3060
                                        "SET SESSION TRANSACTION ISOLATION "
 
3061
                                        "LEVEL REPEATABLE READ") ||
 
3062
          drizzle_query_with_error_report(drizzle_con, 0,
 
3063
                                        "START TRANSACTION "
 
3064
                                        "/*!40100 WITH CONSISTENT SNAPSHOT */"));
 
3065
}
 
3066
 
 
3067
 
 
3068
static uint32_t find_set(TYPELIB *lib, const char *x, uint length,
 
3069
                      char **err_pos, uint *err_len)
2543
3070
{
2544
3071
  const char *end= x + length;
2545
3072
  uint32_t found= 0;
2546
 
  uint32_t find;
 
3073
  uint find;
2547
3074
  char buff[255];
2548
3075
 
2549
3076
  *err_pos= 0;                  /* No error yet */
2561
3088
 
2562
3089
      for (; pos != end && *pos != ','; pos++) ;
2563
3090
      var_len= (uint32_t) (pos - start);
2564
 
      strncpy(buff, start, min((uint32_t)sizeof(buff), var_len+1));
 
3091
      strmake(buff, start, min((uint32_t)sizeof(buff), var_len));
2565
3092
      find= find_type(buff, lib, var_len);
2566
3093
      if (!find)
2567
3094
      {
2569
3096
        *err_len= var_len;
2570
3097
      }
2571
3098
      else
2572
 
        found|= (uint32_t)((int64_t) 1 << (find - 1));
 
3099
        found|= ((int64_t) 1 << (find - 1));
2573
3100
      if (pos == end)
2574
3101
        break;
2575
3102
      start= pos + 1;
2580
3107
 
2581
3108
 
2582
3109
/* Print a value with a prefix on file */
2583
 
static void print_value(FILE *file, drizzle_result_st  *result, drizzle_row_t row,
 
3110
static void print_value(FILE *file, DRIZZLE_RES  *result, DRIZZLE_ROW row,
2584
3111
                        const char *prefix, const char *name,
2585
3112
                        int string_value)
2586
3113
{
2587
 
  drizzle_column_st *column;
2588
 
  drizzle_column_seek(result, 0);
 
3114
  DRIZZLE_FIELD   *field;
 
3115
  drizzle_field_seek(result, 0);
2589
3116
 
2590
 
  for ( ; (column= drizzle_column_next(result)) ; row++)
 
3117
  for ( ; (field= drizzle_fetch_field(result)) ; row++)
2591
3118
  {
2592
 
    if (!strcmp(drizzle_column_name(column),name))
 
3119
    if (!strcmp(field->name,name))
2593
3120
    {
2594
3121
      if (row[0] && row[0][0] && strcmp(row[0],"0")) /* Skip default */
2595
3122
      {
2596
3123
        fputc(' ',file);
2597
3124
        fputs(prefix, file);
2598
3125
        if (string_value)
2599
 
          unescape(file,row[0],(uint32_t) strlen(row[0]));
 
3126
          unescape(file,row[0],(uint) strlen(row[0]));
2600
3127
        else
2601
3128
          fputs(row[0], file);
2602
3129
        check_io(file);
2607
3134
  return;                                       /* This shouldn't happen */
2608
3135
} /* print_value */
2609
3136
 
2610
 
/**
2611
 
 * Fetches a row from a result based on a field name
2612
 
 * Returns const char* of the data in that row or NULL if not found
2613
 
 */
2614
 
 
2615
 
static const char* fetch_named_row(drizzle_result_st *result, drizzle_row_t row, const char *name)
2616
 
{
2617
 
  drizzle_column_st *column;
2618
 
  drizzle_column_seek(result, 0);
2619
 
 
2620
 
  for ( ; (column= drizzle_column_next(result)) ; row++)
2621
 
  {
2622
 
    if (!strcmp(drizzle_column_name(column),name))
2623
 
    {
2624
 
      if (row[0] && row[0][0] && strcmp(row[0],"0")) /* Skip default */
2625
 
      {
2626
 
        drizzle_column_seek(result, 0);
2627
 
        return row[0];
2628
 
      }
2629
 
    }
2630
 
  }
2631
 
  drizzle_column_seek(result, 0);
2632
 
  return NULL;
2633
 
}
2634
 
 
2635
3137
 
2636
3138
/*
2637
3139
  SYNOPSIS
2645
3147
 
2646
3148
  ARGS
2647
3149
 
2648
 
  check_if_ignore_table()
2649
 
  table_name                  Table name to check
2650
 
  table_type                  Type of table
 
3150
    check_if_ignore_table()
 
3151
    table_name                  Table name to check
 
3152
    table_type                  Type of table
2651
3153
 
2652
3154
  GLOBAL VARIABLES
2653
 
  drizzle                       Drizzle connection
2654
 
  verbose                     Write warning messages
 
3155
    drizzle                       Drizzle connection
 
3156
    verbose                     Write warning messages
2655
3157
 
2656
3158
  RETURN
2657
 
  char (bit value)            See IGNORE_ values at top
 
3159
    char (bit value)            See IGNORE_ values at top
2658
3160
*/
2659
3161
 
2660
3162
char check_if_ignore_table(const char *table_name, char *table_type)
2661
3163
{
2662
3164
  char result= IGNORE_NONE;
2663
3165
  char buff[FN_REFLEN+80], show_name_buff[FN_REFLEN];
2664
 
  const char *number_of_rows= NULL;
2665
 
  drizzle_result_st res;
2666
 
  drizzle_row_t row;
 
3166
  DRIZZLE_RES *res= NULL;
 
3167
  DRIZZLE_ROW row;
 
3168
 
2667
3169
 
2668
3170
  /* Check memory for quote_for_like() */
2669
3171
  assert(2*sizeof(table_name) < sizeof(show_name_buff));
2670
3172
  snprintf(buff, sizeof(buff), "show table status like %s",
2671
3173
           quote_for_like(table_name, show_name_buff));
2672
 
  if (drizzleclient_query_with_error_report(&dcon, &res, buff, false))
 
3174
  if (drizzle_query_with_error_report(drizzle, &res, buff))
2673
3175
  {
2674
 
    return result;
 
3176
    if (drizzle_errno(drizzle) != ER_PARSE_ERROR)
 
3177
    {                                   /* If old DRIZZLE version */
 
3178
      verbose_msg("-- Warning: Couldn't get status information for "
 
3179
                  "table %s (%s)\n", table_name, drizzle_error(drizzle));
 
3180
      return(result);                       /* assume table is ok */
 
3181
    }
2675
3182
  }
2676
 
  if (!(row= drizzle_row_next(&res)))
 
3183
  if (!(row= drizzle_fetch_row(res)))
2677
3184
  {
2678
3185
    fprintf(stderr,
2679
 
            _("Error: Couldn't read status information for table %s\n"),
2680
 
            table_name);
2681
 
    drizzle_result_free(&res);
 
3186
            "Error: Couldn't read status information for table %s (%s)\n",
 
3187
            table_name, drizzle_error(drizzle));
 
3188
    drizzle_free_result(res);
2682
3189
    return(result);                         /* assume table is ok */
2683
3190
  }
 
3191
  if (!(row[1]))
 
3192
    strmake(table_type, "VIEW", NAME_LEN-1);
2684
3193
  else
2685
3194
  {
2686
 
    if ((number_of_rows= fetch_named_row(&res, row, "Rows")) != NULL)
2687
 
    {
2688
 
      total_rows= strtoul(number_of_rows, NULL, 10);
2689
 
    }
2690
 
  }
2691
 
  /*
2692
 
    If the table type matches any of these, we do support delayed inserts.
2693
 
    Note-> we do not want to skip dumping this table if if is not one of
2694
 
    these types, but we do want to use delayed inserts in the dump if
2695
 
    the table type is _NOT_ one of these types
2696
 
  */
2697
 
  {
2698
 
    strncpy(table_type, row[1], DRIZZLE_MAX_TABLE_SIZE-1);
 
3195
    /*
 
3196
      If the table type matches any of these, we do support delayed inserts.
 
3197
      Note: we do not want to skip dumping this table if if is not one of
 
3198
      these types, but we do want to use delayed inserts in the dump if
 
3199
      the table type is _NOT_ one of these types
 
3200
    */
 
3201
    strmake(table_type, row[1], NAME_LEN-1);
2699
3202
    if (opt_delayed)
2700
3203
    {
2701
3204
      if (strcmp(table_type,"MyISAM") &&
 
3205
          strcmp(table_type,"ISAM") &&
2702
3206
          strcmp(table_type,"ARCHIVE") &&
2703
3207
          strcmp(table_type,"HEAP") &&
2704
3208
          strcmp(table_type,"MEMORY"))
2705
3209
        result= IGNORE_INSERT_DELAYED;
2706
3210
    }
 
3211
 
 
3212
    /*
 
3213
      If these two types, we do want to skip dumping the table
 
3214
    */
 
3215
    if (!opt_no_data &&
 
3216
        (!my_strcasecmp(&my_charset_utf8_general_ci, table_type, "MRG_MyISAM") ||
 
3217
         !strcmp(table_type,"MRG_ISAM")))
 
3218
      result= IGNORE_DATA;
2707
3219
  }
2708
 
  drizzle_result_free(&res);
 
3220
  drizzle_free_result(res);
2709
3221
  return(result);
2710
3222
}
2711
3223
 
2714
3226
  Get string of comma-separated primary key field names
2715
3227
 
2716
3228
  SYNOPSIS
2717
 
  char *primary_key_fields(const char *table_name)
2718
 
  RETURNS     pointer to allocated buffer (must be freed by caller)
2719
 
  table_name  quoted table name
 
3229
    char *primary_key_fields(const char *table_name)
 
3230
    RETURNS     pointer to allocated buffer (must be freed by caller)
 
3231
    table_name  quoted table name
2720
3232
 
2721
3233
  DESCRIPTION
2722
 
  Use SHOW KEYS FROM table_name, allocate a buffer to hold the
2723
 
  field names, and then build that string and return the pointer
2724
 
  to that buffer.
 
3234
    Use SHOW KEYS FROM table_name, allocate a buffer to hold the
 
3235
    field names, and then build that string and return the pointer
 
3236
    to that buffer.
2725
3237
 
2726
 
  Returns NULL if there is no PRIMARY or UNIQUE key on the table,
2727
 
  or if there is some failure.  It is better to continue to dump
2728
 
  the table unsorted, rather than exit without dumping the data.
 
3238
    Returns NULL if there is no PRIMARY or UNIQUE key on the table,
 
3239
    or if there is some failure.  It is better to continue to dump
 
3240
    the table unsorted, rather than exit without dumping the data.
2729
3241
*/
2730
3242
 
2731
3243
static char *primary_key_fields(const char *table_name)
2732
3244
{
2733
 
  drizzle_result_st res;
2734
 
  drizzle_return_t ret;
2735
 
  drizzle_row_t  row;
 
3245
  DRIZZLE_RES  *res= NULL;
 
3246
  DRIZZLE_ROW  row;
2736
3247
  /* SHOW KEYS FROM + table name * 2 (escaped) + 2 quotes + \0 */
2737
 
  char show_keys_buff[15 + DRIZZLE_MAX_TABLE_SIZE * 2 + 3];
2738
 
  uint32_t result_length= 0;
 
3248
  char show_keys_buff[15 + NAME_LEN * 2 + 3];
 
3249
  uint result_length= 0;
2739
3250
  char *result= 0;
2740
 
  char buff[DRIZZLE_MAX_TABLE_SIZE * 2 + 3];
 
3251
  char buff[NAME_LEN * 2 + 3];
2741
3252
  char *quoted_field;
2742
3253
 
2743
3254
  snprintf(show_keys_buff, sizeof(show_keys_buff),
2744
3255
           "SHOW KEYS FROM %s", table_name);
2745
 
  if (drizzle_query_str(&dcon, &res, show_keys_buff, &ret) == NULL ||
2746
 
      ret != DRIZZLE_RETURN_OK)
2747
 
  {
2748
 
    if (ret == DRIZZLE_RETURN_ERROR_CODE)
2749
 
    {
2750
 
      fprintf(stderr, _("Warning: Couldn't read keys from table %s;"
2751
 
                        " records are NOT sorted (%s)\n"),
2752
 
              table_name, drizzle_result_error(&res));
2753
 
      drizzle_result_free(&res);
2754
 
    }
2755
 
    else
2756
 
    {
2757
 
      fprintf(stderr, _("Warning: Couldn't read keys from table %s;"
2758
 
                        " records are NOT sorted (%s)\n"),
2759
 
              table_name, drizzle_con_error(&dcon));
2760
 
    }
2761
 
 
2762
 
    return result;
2763
 
  }
2764
 
 
2765
 
  if (drizzle_result_buffer(&res) != DRIZZLE_RETURN_OK)
2766
 
  {
2767
 
    fprintf(stderr, _("Warning: Couldn't read keys from table %s;"
2768
 
                      " records are NOT sorted (%s)\n"),
2769
 
            table_name, drizzle_con_error(&dcon));
2770
 
    return result;
 
3256
  if (drizzle_query(drizzle, show_keys_buff) ||
 
3257
      !(res= drizzle_store_result(drizzle)))
 
3258
  {
 
3259
    fprintf(stderr, "Warning: Couldn't read keys from table %s;"
 
3260
            " records are NOT sorted (%s)\n",
 
3261
            table_name, drizzle_error(drizzle));
 
3262
    /* Don't exit, because it's better to print out unsorted records */
 
3263
    goto cleanup;
2771
3264
  }
2772
3265
 
2773
3266
  /*
2775
3268
   * Note that SHOW KEYS is ordered:  a PRIMARY key is always the first
2776
3269
   * row, and UNIQUE keys come before others.  So we only need to check
2777
3270
   * the first key, not all keys.
2778
 
 */
2779
 
  if ((row= drizzle_row_next(&res)) && atoi(row[1]) == 0)
 
3271
   */
 
3272
  if ((row= drizzle_fetch_row(res)) && atoi(row[1]) == 0)
2780
3273
  {
2781
3274
    /* Key is unique */
2782
3275
    do
2783
3276
    {
2784
3277
      quoted_field= quote_name(row[4], buff, 0);
2785
3278
      result_length+= strlen(quoted_field) + 1; /* + 1 for ',' or \0 */
2786
 
    } while ((row= drizzle_row_next(&res)) && atoi(row[3]) > 1);
 
3279
    } while ((row= drizzle_fetch_row(res)) && atoi(row[3]) > 1);
2787
3280
  }
2788
3281
 
2789
3282
  /* Build the ORDER BY clause result */
2791
3284
  {
2792
3285
    char *end;
2793
3286
    /* result (terminating \0 is already in result_length) */
2794
 
 
2795
 
    size_t result_length_alloc= result_length + 10;
2796
 
    result= (char *)malloc(result_length_alloc);
 
3287
    result= (char *)my_malloc(result_length + 10, MYF(MY_WME));
2797
3288
    if (!result)
2798
3289
    {
2799
 
      fprintf(stderr, _("Error: Not enough memory to store ORDER BY clause\n"));
2800
 
      drizzle_result_free(&res);
2801
 
      return result;
 
3290
      fprintf(stderr, "Error: Not enough memory to store ORDER BY clause\n");
 
3291
      goto cleanup;
2802
3292
    }
2803
 
    drizzle_row_seek(&res, 0);
2804
 
    row= drizzle_row_next(&res);
 
3293
    drizzle_data_seek(res, 0);
 
3294
    row= drizzle_fetch_row(res);
2805
3295
    quoted_field= quote_name(row[4], buff, 0);
2806
 
    end= strcpy(result, quoted_field) + strlen(quoted_field);
2807
 
    result_length_alloc -= strlen(quoted_field);
2808
 
    while ((row= drizzle_row_next(&res)) && atoi(row[3]) > 1)
 
3296
    end= my_stpcpy(result, quoted_field);
 
3297
    while ((row= drizzle_fetch_row(res)) && atoi(row[3]) > 1)
2809
3298
    {
2810
3299
      quoted_field= quote_name(row[4], buff, 0);
2811
 
      end+= snprintf(end, result_length_alloc, ",%s",quoted_field);
2812
 
      result_length_alloc -= strlen(quoted_field);
 
3300
      end= strxmov(end, ",", quoted_field, NULL);
2813
3301
    }
2814
3302
  }
2815
3303
 
2816
 
  drizzle_result_free(&res);
 
3304
cleanup:
 
3305
  if (res)
 
3306
    drizzle_free_result(res);
 
3307
 
2817
3308
  return result;
2818
3309
}
2819
3310
 
2820
3311
 
2821
3312
int main(int argc, char **argv)
2822
3313
{
 
3314
  char bin_log_name[FN_REFLEN];
2823
3315
  int exit_code;
2824
 
  MY_INIT("drizzledump");
2825
 
  drizzle_result_st result;
 
3316
  MY_INIT("mysqldump");
2826
3317
 
2827
3318
  compatible_mode_normal_str[0]= 0;
 
3319
  default_charset= (char *)drizzle_universal_client_charset;
 
3320
  memset(&ignore_table, 0, sizeof(ignore_table));
2828
3321
 
2829
3322
  exit_code= get_options(&argc, &argv);
2830
3323
  if (exit_code)
2833
3326
    exit(exit_code);
2834
3327
  }
2835
3328
 
 
3329
  if (log_error_file)
 
3330
  {
 
3331
    if(!(stderror_file= freopen(log_error_file, "a+", stderr)))
 
3332
    {
 
3333
      free_resources();
 
3334
      exit(EX_DRIZZLEERR);
 
3335
    }
 
3336
  }
 
3337
 
2836
3338
  if (connect_to_db(current_host, current_user, opt_password))
2837
3339
  {
2838
3340
    free_resources();
2841
3343
  if (!path)
2842
3344
    write_header(md_result_file, *argv);
2843
3345
 
2844
 
  if ((opt_lock_all_tables) && do_flush_tables_read_lock(&dcon))
2845
 
    goto err;
2846
 
  if (opt_single_transaction && start_transaction(&dcon))
2847
 
    goto err;
2848
 
  if (opt_lock_all_tables)
2849
 
  {
2850
 
    if (drizzleclient_query_with_error_report(&dcon, &result, "FLUSH LOGS", false))
2851
 
      goto err;
2852
 
    drizzle_result_free(&result);
 
3346
  if (opt_slave_data && do_stop_slave_sql(drizzle))
 
3347
    goto err;
 
3348
 
 
3349
  if ((opt_lock_all_tables || opt_master_data) &&
 
3350
      do_flush_tables_read_lock(drizzle))
 
3351
    goto err;
 
3352
  if (opt_single_transaction && start_transaction(drizzle))
 
3353
      goto err;
 
3354
  if (opt_delete_master_logs)
 
3355
  {
 
3356
    if (drizzle_refresh(drizzle, REFRESH_LOG) ||
 
3357
        get_bin_log_name(drizzle, bin_log_name, sizeof(bin_log_name)))
 
3358
      goto err;
 
3359
    flush_logs= 0;
 
3360
  }
 
3361
  if (opt_lock_all_tables || opt_master_data)
 
3362
  {
 
3363
    if (flush_logs && drizzle_refresh(drizzle, REFRESH_LOG))
 
3364
      goto err;
2853
3365
    flush_logs= 0; /* not anymore; that would not be sensible */
2854
3366
  }
2855
 
  if (opt_single_transaction && do_unlock_tables(&dcon)) /* unlock but no commit! */
 
3367
  /* Add 'STOP SLAVE to beginning of dump */
 
3368
  if (opt_slave_apply && add_stop_slave())
 
3369
    goto err;
 
3370
  if (opt_master_data && do_show_master_status(drizzle))
 
3371
    goto err;
 
3372
  if (opt_slave_data && do_show_slave_status(drizzle))
 
3373
    goto err;
 
3374
  if (opt_single_transaction && do_unlock_tables(drizzle)) /* unlock but no commit! */
2856
3375
    goto err;
2857
3376
 
2858
3377
  if (opt_alldbs)
2869
3388
    dump_databases(argv);
2870
3389
  }
2871
3390
 
 
3391
  /* if --dump-slave , start the slave sql thread */
 
3392
  if (opt_slave_data && do_start_slave_sql(drizzle))
 
3393
    goto err;
 
3394
 
 
3395
  /* add 'START SLAVE' to end of dump */
 
3396
  if (opt_slave_apply && add_slave_statements())
 
3397
    goto err;
 
3398
 
2872
3399
  /* ensure dumped data flushed */
2873
3400
  if (md_result_file && fflush(md_result_file))
2874
3401
  {
2876
3403
      first_error= EX_DRIZZLEERR;
2877
3404
    goto err;
2878
3405
  }
 
3406
  /* everything successful, purge the old logs files */
 
3407
  if (opt_delete_master_logs && purge_bin_logs_to(drizzle, bin_log_name))
 
3408
    goto err;
2879
3409
 
2880
3410
  /*
2881
3411
    No reason to explicitely COMMIT the transaction, neither to explicitely