~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/drizzledump.cc

  • Committer: Jay Pipes
  • Date: 2009-02-21 16:00:06 UTC
  • mto: (907.1.1 trunk-with-temporal)
  • mto: This revision was merged to the branch mainline in revision 908.
  • Revision ID: jpipes@serialcoder-20090221160006-vnk3wt4qbcz62eru
Removes the TIME column type and related time functions.

Show diffs side-by-side

added added

removed removed

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