~drizzle-trunk/drizzle/development

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