mariadb/client/mysqlimport.c

699 lines
21 KiB
C
Raw Normal View History

/* Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
2000-07-31 21:29:14 +02:00
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
2000-07-31 21:29:14 +02:00
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
2000-07-31 21:29:14 +02:00
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
/*
** mysqlimport.c - Imports all given files
** into a table(s).
**
** *************************
** * *
** * AUTHOR: Monty & Jani *
** * DATE: June 24, 1997 *
** * *
** *************************
*/
#define IMPORT_VERSION "3.7"
2000-07-31 21:29:14 +02:00
2001-09-12 22:53:31 +02:00
#include "client_priv.h"
2000-07-31 21:29:14 +02:00
#include "mysql_version.h"
#ifdef HAVE_LIBPTHREAD
#include <my_pthread.h>
#endif
#include <welcome_copyright_notice.h> /* ORACLE_WELCOME_COPYRIGHT_NOTICE */
/* Global Thread counter */
uint counter;
#ifdef HAVE_LIBPTHREAD
pthread_mutex_t counter_mutex;
pthread_cond_t count_threshhold;
#endif
2000-07-31 21:29:14 +02:00
static void db_error_with_table(MYSQL *mysql, char *table);
static void db_error(MYSQL *mysql);
static char *field_escape(char *to,const char *from,uint length);
static char *add_load_option(char *ptr,const char *object,
const char *statement);
static my_bool verbose=0,lock_tables=0,ignore_errors=0,opt_delete=0,
replace=0,silent=0,ignore=0,opt_compress=0,
opt_low_priority= 0, tty_password= 0;
static my_bool debug_info_flag= 0, debug_check_flag= 0;
static uint opt_use_threads=0, opt_local_file=0, my_end_arg= 0;
static char *opt_password=0, *current_user=0,
2000-07-31 21:29:14 +02:00
*current_host=0, *current_db=0, *fields_terminated=0,
*lines_terminated=0, *enclosed=0, *opt_enclosed=0,
*escaped=0, *opt_columns=0,
*default_charset= (char*) MYSQL_AUTODETECT_CHARSET_NAME;
2003-09-11 19:24:14 +02:00
static uint opt_mysql_port= 0, opt_protocol= 0;
WL#3817: Simplify string / memory area types and make things more consistent (first part) The following type conversions was done: - Changed byte to uchar - Changed gptr to uchar* - Change my_string to char * - Change my_size_t to size_t - Change size_s to size_t Removed declaration of byte, gptr, my_string, my_size_t and size_s. Following function parameter changes was done: - All string functions in mysys/strings was changed to use size_t instead of uint for string lengths. - All read()/write() functions changed to use size_t (including vio). - All protocoll functions changed to use size_t instead of uint - Functions that used a pointer to a string length was changed to use size_t* - Changed malloc(), free() and related functions from using gptr to use void * as this requires fewer casts in the code and is more in line with how the standard functions work. - Added extra length argument to dirname_part() to return the length of the created string. - Changed (at least) following functions to take uchar* as argument: - db_dump() - my_net_write() - net_write_command() - net_store_data() - DBUG_DUMP() - decimal2bin() & bin2decimal() - Changed my_compress() and my_uncompress() to use size_t. Changed one argument to my_uncompress() from a pointer to a value as we only return one value (makes function easier to use). - Changed type of 'pack_data' argument to packfrm() to avoid casts. - Changed in readfrm() and writefrom(), ha_discover and handler::discover() the type for argument 'frmdata' to uchar** to avoid casts. - Changed most Field functions to use uchar* instead of char* (reduced a lot of casts). - Changed field->val_xxx(xxx, new_ptr) to take const pointers. Other changes: - Removed a lot of not needed casts - Added a few new cast required by other changes - Added some cast to my_multi_malloc() arguments for safety (as string lengths needs to be uint, not size_t). - Fixed all calls to hash-get-key functions to use size_t*. (Needed to be done explicitely as this conflict was often hided by casting the function to hash_get_key). - Changed some buffers to memory regions to uchar* to avoid casts. - Changed some string lengths from uint to size_t. - Changed field->ptr to be uchar* instead of char*. This allowed us to get rid of a lot of casts. - Some changes from true -> TRUE, false -> FALSE, unsigned char -> uchar - Include zlib.h in some files as we needed declaration of crc32() - Changed MY_FILE_ERROR to be (size_t) -1. - Changed many variables to hold the result of my_read() / my_write() to be size_t. This was needed to properly detect errors (which are returned as (size_t) -1). - Removed some very old VMS code - Changed packfrm()/unpackfrm() to not be depending on uint size (portability fix) - Removed windows specific code to restore cursor position as this causes slowdown on windows and we should not mix read() and pread() calls anyway as this is not thread safe. Updated function comment to reflect this. Changed function that depended on original behavior of my_pwrite() to itself restore the cursor position (one such case). - Added some missing checking of return value of malloc(). - Changed definition of MOD_PAD_CHAR_TO_FULL_LENGTH to avoid 'long' overflow. - Changed type of table_def::m_size from my_size_t to ulong to reflect that m_size is the number of elements in the array, not a string/memory length. - Moved THD::max_row_length() to table.cc (as it's not depending on THD). Inlined max_row_length_blob() into this function. - More function comments - Fixed some compiler warnings when compiled without partitions. - Removed setting of LEX_STRING() arguments in declaration (portability fix). - Some trivial indentation/variable name changes. - Some trivial code simplifications: - Replaced some calls to alloc_root + memcpy to use strmake_root()/strdup_root(). - Changed some calls from memdup() to strmake() (Safety fix) - Simpler loops in client-simple.c
2007-05-10 11:59:39 +02:00
static char * opt_mysql_unix_port=0;
static char *opt_plugin_dir= 0, *opt_default_auth= 0;
static longlong opt_ignore_lines= -1;
#include <sslopt-vars.h>
2000-07-31 21:29:14 +02:00
#ifdef HAVE_SMEM
static char *shared_memory_base_name=0;
#endif
static struct my_option my_long_options[] =
2000-07-31 21:29:14 +02:00
{
{"character-sets-dir", OPT_CHARSETS_DIR,
"Directory for character set files.", &charsets_dir,
&charsets_dir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"default-character-set", OPT_DEFAULT_CHARSET,
"Set the default character set.", &default_charset,
&default_charset, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"columns", 'c',
"Use only these columns to import the data to. Give the column names in a comma separated list. This is same as giving columns to LOAD DATA INFILE.",
&opt_columns, &opt_columns, 0, GET_STR, REQUIRED_ARG, 0, 0, 0,
0, 0, 0},
{"compress", 'C', "Use compression in server/client protocol.",
&opt_compress, &opt_compress, 0, GET_BOOL, NO_ARG, 0, 0, 0,
0, 0, 0},
{"debug",'#', "Output debug log. Often this is 'd:t:o,filename'.", 0, 0, 0,
GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
2007-10-01 14:32:07 +02:00
{"debug-check", OPT_DEBUG_CHECK, "Check memory and open file usage at exit.",
&debug_check_flag, &debug_check_flag, 0,
GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
{"debug-info", OPT_DEBUG_INFO, "Print some debug info at exit.",
&debug_info_flag, &debug_info_flag,
0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
{"default_auth", OPT_DEFAULT_AUTH,
"Default authentication client-side plugin to use.",
(uchar**) &opt_default_auth, (uchar**) &opt_default_auth, 0,
GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"delete", 'd', "First delete all rows from table.", &opt_delete,
&opt_delete, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
{"fields-terminated-by", OPT_FTB,
2010-02-04 13:39:42 +01:00
"Fields in the input file are terminated by the given string.",
&fields_terminated, &fields_terminated, 0,
2010-02-04 13:39:42 +01:00
GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"fields-enclosed-by", OPT_ENC,
2010-02-04 13:39:42 +01:00
"Fields in the import file are enclosed by the given character.",
&enclosed, &enclosed, 0,
2010-02-04 13:39:42 +01:00
GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"fields-optionally-enclosed-by", OPT_O_ENC,
2010-02-04 13:39:42 +01:00
"Fields in the input file are optionally enclosed by the given character.",
&opt_enclosed, &opt_enclosed, 0,
2010-02-04 13:39:42 +01:00
GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"fields-escaped-by", OPT_ESC,
"Fields in the input file are escaped by the given character.",
&escaped, &escaped, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0,
0, 0},
2010-02-04 13:39:42 +01:00
{"force", 'f', "Continue even if we get an SQL error.",
&ignore_errors, &ignore_errors, 0, GET_BOOL, NO_ARG, 0, 0,
0, 0, 0, 0},
{"help", '?', "Displays this help and exits.", 0, 0, 0, GET_NO_ARG, NO_ARG,
0, 0, 0, 0, 0, 0},
{"host", 'h', "Connect to host.", &current_host,
&current_host, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"ignore", 'i', "If duplicate unique key was found, keep old row.",
&ignore, &ignore, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
{"ignore-lines", OPT_IGN_LINES, "Ignore first n lines of data infile.",
&opt_ignore_lines, &opt_ignore_lines, 0, GET_LL,
REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
2010-02-04 13:39:42 +01:00
{"lines-terminated-by", OPT_LTB,
"Lines in the input file are terminated by the given string.",
&lines_terminated, &lines_terminated, 0, GET_STR,
REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"local", 'L', "Read all files through the client.", &opt_local_file,
&opt_local_file, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
{"lock-tables", 'l', "Lock all tables for write (this disables threads).",
&lock_tables, &lock_tables, 0, GET_BOOL, NO_ARG,
0, 0, 0, 0, 0, 0},
{"low-priority", OPT_LOW_PRIORITY,
"Use LOW_PRIORITY when updating the table.", &opt_low_priority,
&opt_low_priority, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
{"password", 'p',
"Password to use when connecting to server. If password is not given it's asked from the tty.",
0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
2000-07-31 21:29:14 +02:00
#ifdef __WIN__
{"pipe", 'W', "Use named pipes to connect to server.", 0, 0, 0, GET_NO_ARG,
NO_ARG, 0, 0, 0, 0, 0, 0},
2000-07-31 21:29:14 +02:00
#endif
{"plugin_dir", OPT_PLUGIN_DIR, "Directory for client-side plugins.",
(uchar**) &opt_plugin_dir, (uchar**) &opt_plugin_dir, 0,
GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"port", 'P', "Port number to use for connection or 0 for default to, in "
"order of preference, my.cnf, $MYSQL_TCP_PORT, "
#if MYSQL_PORT_DEFAULT == 0
"/etc/services, "
#endif
"built-in default (" STRINGIFY_ARG(MYSQL_PORT) ").",
&opt_mysql_port,
&opt_mysql_port, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0,
0},
2010-02-04 13:39:42 +01:00
{"protocol", OPT_MYSQL_PROTOCOL, "The protocol to use for connection (tcp, socket, pipe, memory).",
0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"replace", 'r', "If duplicate unique key was found, replace old row.",
&replace, &replace, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
#ifdef HAVE_SMEM
{"shared-memory-base-name", OPT_SHARED_MEMORY_BASE_NAME,
"Base name of shared memory.", &shared_memory_base_name, &shared_memory_base_name,
0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
#endif
{"silent", 's', "Be more silent.", &silent, &silent, 0,
GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
2010-02-04 13:39:42 +01:00
{"socket", 'S', "The socket file to use for connection.",
&opt_mysql_unix_port, &opt_mysql_unix_port, 0, GET_STR,
REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
#include <sslopt-longopts.h>
{"use-threads", OPT_USE_THREADS,
"Load files in parallel. The argument is the number "
"of threads to use for loading data.",
&opt_use_threads, &opt_use_threads, 0,
GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
2000-07-31 21:29:14 +02:00
#ifndef DONT_ALLOW_USER_CHANGE
{"user", 'u', "User for login if not current user.", &current_user,
&current_user, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
2000-07-31 21:29:14 +02:00
#endif
{"verbose", 'v', "Print info about the various stages.", &verbose,
&verbose, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
{"version", 'V', "Output version information and exit.", 0, 0, 0, GET_NO_ARG,
NO_ARG, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
2000-07-31 21:29:14 +02:00
};
static const char *load_default_groups[]= { "mysqlimport","client",0 };
2004-05-25 21:00:14 +02:00
2000-07-31 21:29:14 +02:00
static void print_version(void)
{
printf("%s Ver %s Distrib %s, for %s (%s)\n" ,my_progname,
IMPORT_VERSION, MYSQL_SERVER_VERSION,SYSTEM_TYPE,MACHINE_TYPE);
}
static void usage(void)
{
print_version();
puts(ORACLE_WELCOME_COPYRIGHT_NOTICE("2000, 2010"));
2000-07-31 21:29:14 +02:00
printf("\
Loads tables from text files in various formats. The base name of the\n\
text file must be the name of the table that should be used.\n\
If one uses sockets to connect to the MySQL server, the server will open and\n\
read the text file directly. In other cases the client will open the text\n\
file. The SQL command 'LOAD DATA INFILE' is used to import the rows.\n");
printf("\nUsage: %s [OPTIONS] database textfile...",my_progname);
print_defaults("my",load_default_groups);
my_print_help(my_long_options);
my_print_variables(my_long_options);
2000-07-31 21:29:14 +02:00
}
static my_bool
get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
char *argument)
{
switch(optid) {
case 'p':
if (argument == disabled_my_option)
argument= (char*) ""; /* Don't require password */
if (argument)
{
char *start=argument;
Bug#34043: Server loops excessively in _checkchunk() when safemalloc is enabled Essentially, the problem is that safemalloc is excruciatingly slow as it checks all allocated blocks for overrun at each memory management primitive, yielding a almost exponential slowdown for the memory management functions (malloc, realloc, free). The overrun check basically consists of verifying some bytes of a block for certain magic keys, which catches some simple forms of overrun. Another minor problem is violation of aliasing rules and that its own internal list of blocks is prone to corruption. Another issue with safemalloc is rather the maintenance cost as the tool has a significant impact on the server code. Given the magnitude of memory debuggers available nowadays, especially those that are provided with the platform malloc implementation, maintenance of a in-house and largely obsolete memory debugger becomes a burden that is not worth the effort due to its slowness and lack of support for detecting more common forms of heap corruption. Since there are third-party tools that can provide the same functionality at a lower or comparable performance cost, the solution is to simply remove safemalloc. Third-party tools can provide the same functionality at a lower or comparable performance cost. The removal of safemalloc also allows a simplification of the malloc wrappers, removing quite a bit of kludge: redefinition of my_malloc, my_free and the removal of the unused second argument of my_free. Since free() always check whether the supplied pointer is null, redudant checks are also removed. Also, this patch adds unit testing for my_malloc and moves my_realloc implementation into the same file as the other memory allocation primitives.
2010-07-08 23:20:08 +02:00
my_free(opt_password);
opt_password=my_strdup(argument,MYF(MY_FAE));
while (*argument) *argument++= 'x'; /* Destroy argument */
if (*start)
start[1]=0; /* Cut length of argument */
tty_password= 0;
}
else
tty_password= 1;
break;
2000-07-31 21:29:14 +02:00
#ifdef __WIN__
case 'W':
opt_protocol = MYSQL_PROTOCOL_PIPE;
opt_local_file=1;
break;
2000-07-31 21:29:14 +02:00
#endif
case OPT_MYSQL_PROTOCOL:
opt_protocol= find_type_or_exit(argument, &sql_protocol_typelib,
opt->name);
break;
case '#':
DBUG_PUSH(argument ? argument : "d:t:o");
debug_check_flag= 1;
break;
#include <sslopt-case.h>
case 'V': print_version(); exit(0);
case 'I':
case '?':
usage();
exit(0);
}
return 0;
}
static int get_options(int *argc, char ***argv)
{
int ho_error;
if ((ho_error=handle_options(argc, argv, my_long_options, get_one_option)))
exit(ho_error);
if (debug_info_flag)
my_end_arg= MY_CHECK_ERROR | MY_GIVE_INFO;
if (debug_check_flag)
my_end_arg= MY_CHECK_ERROR;
2000-07-31 21:29:14 +02:00
if (enclosed && opt_enclosed)
{
fprintf(stderr, "You can't use ..enclosed.. and ..optionally-enclosed.. at the same time.\n");
return(1);
}
if (replace && ignore)
{
fprintf(stderr, "You can't use --ignore (-i) and --replace (-r) at the same time.\n");
return(1);
}
if (*argc < 2)
{
usage();
return 1;
}
current_db= *((*argv)++);
(*argc)--;
if (tty_password)
opt_password=get_tty_password(NullS);
2000-07-31 21:29:14 +02:00
return(0);
}
static int write_to_table(char *filename, MYSQL *mysql)
2000-07-31 21:29:14 +02:00
{
char tablename[FN_REFLEN], hard_path[FN_REFLEN],
escaped_name[FN_REFLEN * 2 + 1],
sql_statement[FN_REFLEN*16+256], *end, *pos;
2000-07-31 21:29:14 +02:00
DBUG_ENTER("write_to_table");
DBUG_PRINT("enter",("filename: %s",filename));
fn_format(tablename, filename, "", "", 1 | 2); /* removes path & ext. */
if (!opt_local_file)
2000-07-31 21:29:14 +02:00
strmov(hard_path,filename);
else
my_load_path(hard_path, filename, NULL); /* filename includes the path */
if (opt_delete)
2000-07-31 21:29:14 +02:00
{
if (verbose)
fprintf(stdout, "Deleting the old data from table %s\n", tablename);
#ifdef HAVE_SNPRINTF
snprintf(sql_statement, FN_REFLEN*16+256, "DELETE FROM %s", tablename);
#else
sprintf(sql_statement, "DELETE FROM %s", tablename);
#endif
if (mysql_query(mysql, sql_statement))
2000-07-31 21:29:14 +02:00
{
db_error_with_table(mysql, tablename);
2000-07-31 21:29:14 +02:00
DBUG_RETURN(1);
}
}
to_unix_path(hard_path);
if (verbose)
{
if (opt_local_file)
2000-07-31 21:29:14 +02:00
fprintf(stdout, "Loading data from LOCAL file: %s into %s\n",
hard_path, tablename);
else
fprintf(stdout, "Loading data from SERVER file: %s into %s\n",
hard_path, tablename);
}
2009-05-20 21:58:01 +02:00
mysql_real_escape_string(mysql, escaped_name, hard_path,
(unsigned long) strlen(hard_path));
2000-07-31 21:29:14 +02:00
sprintf(sql_statement, "LOAD DATA %s %s INFILE '%s'",
opt_low_priority ? "LOW_PRIORITY" : "",
opt_local_file ? "LOCAL" : "", escaped_name);
2000-07-31 21:29:14 +02:00
end= strend(sql_statement);
if (replace)
end= strmov(end, " REPLACE");
if (ignore)
end= strmov(end, " IGNORE");
end= strmov(end, " INTO TABLE `");
/* Turn any ` into `` in table name. */
for (pos= tablename; *pos; pos++)
{
if (*pos == '`')
*end++= '`';
*end++= *pos;
}
end= strmov(end, "`");
2000-07-31 21:29:14 +02:00
if (fields_terminated || enclosed || opt_enclosed || escaped)
end= strmov(end, " FIELDS");
end= add_load_option(end, fields_terminated, " TERMINATED BY");
end= add_load_option(end, enclosed, " ENCLOSED BY");
end= add_load_option(end, opt_enclosed,
" OPTIONALLY ENCLOSED BY");
end= add_load_option(end, escaped, " ESCAPED BY");
end= add_load_option(end, lines_terminated, " LINES TERMINATED BY");
if (opt_ignore_lines >= 0)
end= strmov(longlong10_to_str(opt_ignore_lines,
strmov(end, " IGNORE "),10), " LINES");
2000-07-31 21:29:14 +02:00
if (opt_columns)
end= strmov(strmov(strmov(end, " ("), opt_columns), ")");
*end= '\0';
if (mysql_query(mysql, sql_statement))
2000-07-31 21:29:14 +02:00
{
db_error_with_table(mysql, tablename);
2000-07-31 21:29:14 +02:00
DBUG_RETURN(1);
}
if (!silent)
{
if (mysql_info(mysql)) /* If NULL-pointer, print nothing */
2000-07-31 21:29:14 +02:00
{
fprintf(stdout, "%s.%s: %s\n", current_db, tablename,
mysql_info(mysql));
2000-07-31 21:29:14 +02:00
}
}
DBUG_RETURN(0);
}
static void lock_table(MYSQL *mysql, int tablecount, char **raw_tablename)
2000-07-31 21:29:14 +02:00
{
DYNAMIC_STRING query;
int i;
char tablename[FN_REFLEN];
if (verbose)
fprintf(stdout, "Locking tables for write\n");
init_dynamic_string(&query, "LOCK TABLES ", 256, 1024);
for (i=0 ; i < tablecount ; i++)
{
fn_format(tablename, raw_tablename[i], "", "", 1 | 2);
dynstr_append(&query, tablename);
dynstr_append(&query, " WRITE,");
}
if (mysql_real_query(mysql, query.str, query.length-1))
db_error(mysql); /* We shall countinue here, if --force was given */
2000-07-31 21:29:14 +02:00
}
2006-02-17 17:16:18 +01:00
static MYSQL *db_connect(char *host, char *database,
char *user, char *passwd)
2000-07-31 21:29:14 +02:00
{
2006-02-17 17:16:18 +01:00
MYSQL *mysql;
2000-07-31 21:29:14 +02:00
if (verbose)
fprintf(stdout, "Connecting to %s\n", host ? host : "localhost");
if (!(mysql= mysql_init(NULL)))
return 0;
2000-07-31 21:29:14 +02:00
if (opt_compress)
mysql_options(mysql,MYSQL_OPT_COMPRESS,NullS);
2002-04-03 12:36:01 +02:00
if (opt_local_file)
mysql_options(mysql,MYSQL_OPT_LOCAL_INFILE,
2002-04-03 12:36:01 +02:00
(char*) &opt_local_file);
2000-07-31 21:29:14 +02:00
#ifdef HAVE_OPENSSL
if (opt_use_ssl)
mysql_ssl_set(mysql, opt_ssl_key, opt_ssl_cert, opt_ssl_ca,
opt_ssl_capath, opt_ssl_cipher);
mysql_options(mysql,MYSQL_OPT_SSL_VERIFY_SERVER_CERT,
(char*)&opt_ssl_verify_server_cert);
#endif
if (opt_protocol)
mysql_options(mysql,MYSQL_OPT_PROTOCOL,(char*)&opt_protocol);
#ifdef HAVE_SMEM
if (shared_memory_base_name)
mysql_options(mysql,MYSQL_SHARED_MEMORY_BASE_NAME,shared_memory_base_name);
2000-07-31 21:29:14 +02:00
#endif
if (opt_plugin_dir && *opt_plugin_dir)
mysql_options(mysql, MYSQL_PLUGIN_DIR, opt_plugin_dir);
if (opt_default_auth && *opt_default_auth)
mysql_options(mysql, MYSQL_DEFAULT_AUTH, opt_default_auth);
mysql_options(mysql, MYSQL_SET_CHARSET_NAME, default_charset);
if (!(mysql_real_connect(mysql,host,user,passwd,
database,opt_mysql_port,opt_mysql_unix_port,
0)))
2000-07-31 21:29:14 +02:00
{
ignore_errors=0; /* NO RETURN FROM db_error */
db_error(mysql);
2000-07-31 21:29:14 +02:00
}
mysql->reconnect= 0;
2000-07-31 21:29:14 +02:00
if (verbose)
fprintf(stdout, "Selecting database %s\n", database);
if (mysql_select_db(mysql, database))
2000-07-31 21:29:14 +02:00
{
ignore_errors=0;
db_error(mysql);
2000-07-31 21:29:14 +02:00
}
return mysql;
2000-07-31 21:29:14 +02:00
}
static void db_disconnect(char *host, MYSQL *mysql)
2000-07-31 21:29:14 +02:00
{
if (verbose)
fprintf(stdout, "Disconnecting from %s\n", host ? host : "localhost");
mysql_close(mysql);
2000-07-31 21:29:14 +02:00
}
static void safe_exit(int error, MYSQL *mysql)
2000-07-31 21:29:14 +02:00
{
if (ignore_errors)
return;
if (mysql)
mysql_close(mysql);
2000-07-31 21:29:14 +02:00
exit(error);
}
static void db_error_with_table(MYSQL *mysql, char *table)
{
my_printf_error(0,"Error: %d, %s, when using table: %s",
MYF(0), mysql_errno(mysql), mysql_error(mysql), table);
2000-07-31 21:29:14 +02:00
safe_exit(1, mysql);
}
static void db_error(MYSQL *mysql)
{
my_printf_error(0,"Error: %d %s", MYF(0), mysql_errno(mysql), mysql_error(mysql));
2000-07-31 21:29:14 +02:00
safe_exit(1, mysql);
}
static char *add_load_option(char *ptr, const char *object,
const char *statement)
2000-07-31 21:29:14 +02:00
{
if (object)
{
/* Don't escape hex constants */
if (object[0] == '0' && (object[1] == 'x' || object[1] == 'X'))
ptr= strxmov(ptr," ",statement," ",object,NullS);
else
{
/* char constant; escape */
ptr= strxmov(ptr," ",statement," '",NullS);
ptr= field_escape(ptr,object,(uint) strlen(object));
*ptr++= '\'';
}
2000-07-31 21:29:14 +02:00
}
return ptr;
}
/*
** Allow the user to specify field terminator strings like:
** "'", "\", "\\" (escaped backslash), "\t" (tab), "\n" (newline)
** This is done by doubleing ' and add a end -\ if needed to avoid
** syntax errors from the SQL parser.
*/
2000-07-31 21:29:14 +02:00
static char *field_escape(char *to,const char *from,uint length)
{
const char *end;
uint end_backslashes=0;
for (end= from+length; from != end; from++)
{
*to++= *from;
if (*from == '\\')
end_backslashes^=1; /* find odd number of backslashes */
else
{
if (*from == '\'' && !end_backslashes)
*to++= *from; /* We want a dublicate of "'" for MySQL */
end_backslashes=0;
}
}
/* Add missing backslashes if user has specified odd number of backs.*/
if (end_backslashes)
*to++= '\\';
return to;
}
int exitcode= 0;
2000-07-31 21:29:14 +02:00
#ifdef HAVE_LIBPTHREAD
pthread_handler_t worker_thread(void *arg)
{
int error;
char *raw_table_name= (char *)arg;
MYSQL *mysql= 0;
if (mysql_thread_init())
goto error;
if (!(mysql= db_connect(current_host,current_db,current_user,opt_password)))
{
goto error;
}
if (mysql_query(mysql, "/*!40101 set @@character_set_database=binary */;"))
{
db_error(mysql); /* We shall countinue here, if --force was given */
goto error;
}
/*
We are not currently catching the error here.
*/
if((error= write_to_table(raw_table_name, mysql)))
if (exitcode == 0)
exitcode= error;
error:
if (mysql)
db_disconnect(current_host, mysql);
pthread_mutex_lock(&counter_mutex);
counter--;
pthread_cond_signal(&count_threshhold);
pthread_mutex_unlock(&counter_mutex);
mysql_thread_end();
2006-02-18 16:18:51 +01:00
return 0;
}
#endif
2000-07-31 21:29:14 +02:00
int main(int argc, char **argv)
{
int error=0;
2000-07-31 21:29:14 +02:00
char **argv_to_free;
MY_INIT(argv[0]);
Bug#47216 programs should quit if the file specified by --defaults-file option isn't found added code to exit a tool if the forced config file wasn't found per-file comments: client/mysql.cc Bug#47216 programs should quit if the file specified by --defaults-file option isn't found added code to exit a tool if the forced config file wasn't found client/mysql_upgrade.c Bug#47216 programs should quit if the file specified by --defaults-file option isn't found added code to exit a tool if the forced config file wasn't found client/mysqladmin.cc Bug#47216 programs should quit if the file specified by --defaults-file option isn't found added code to exit a tool if the forced config file wasn't found client/mysqlcheck.c Bug#47216 programs should quit if the file specified by --defaults-file option isn't found added code to exit a tool if the forced config file wasn't found client/mysqldump.c Bug#47216 programs should quit if the file specified by --defaults-file option isn't found added code to exit a tool if the forced config file wasn't found client/mysqlimport.c Bug#47216 programs should quit if the file specified by --defaults-file option isn't found added code to exit a tool if the forced config file wasn't found client/mysqlshow.c Bug#47216 programs should quit if the file specified by --defaults-file option isn't found added code to exit a tool if the forced config file wasn't found client/mysqlslap.c Bug#47216 programs should quit if the file specified by --defaults-file option isn't found added code to exit a tool if the forced config file wasn't found mysql-test/t/mysql.test Bug#47216 programs should quit if the file specified by --defaults-file option isn't found test added sql/mysqld.cc Bug#47216 programs should quit if the file specified by --defaults-file option isn't found added code to exit a tool if the forced config file wasn't found storage/myisam/myisamchk.c Bug#47216 programs should quit if the file specified by --defaults-file option isn't found added code to exit a tool if the forced config file wasn't found storage/myisam/myisampack.c Bug#47216 programs should quit if the file specified by --defaults-file option isn't found added code to exit a tool if the forced config file wasn't found
2009-10-09 16:44:22 +02:00
if (load_defaults("my",load_default_groups,&argc,&argv))
return 1;
2000-07-31 21:29:14 +02:00
/* argv is changed in the program */
argv_to_free= argv;
if (get_options(&argc, &argv))
{
free_defaults(argv_to_free);
2000-07-31 21:29:14 +02:00
return(1);
}
#ifdef HAVE_LIBPTHREAD
if (opt_use_threads && !lock_tables)
{
pthread_t mainthread; /* Thread descriptor */
pthread_attr_t attr; /* Thread attributes */
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr,
PTHREAD_CREATE_DETACHED);
pthread_mutex_init(&counter_mutex, NULL);
pthread_cond_init(&count_threshhold, NULL);
for (counter= 0; *argv != NULL; argv++) /* Loop through tables */
{
pthread_mutex_lock(&counter_mutex);
while (counter == opt_use_threads)
{
struct timespec abstime;
set_timespec(abstime, 3);
pthread_cond_timedwait(&count_threshhold, &counter_mutex, &abstime);
}
/* Before exiting the lock we set ourselves up for the next thread */
counter++;
pthread_mutex_unlock(&counter_mutex);
/* now create the thread */
if (pthread_create(&mainthread, &attr, worker_thread,
(void *)*argv) != 0)
{
pthread_mutex_lock(&counter_mutex);
counter--;
pthread_mutex_unlock(&counter_mutex);
fprintf(stderr,"%s: Could not create thread\n",
my_progname);
}
}
/*
We loop until we know that all children have cleaned up.
*/
pthread_mutex_lock(&counter_mutex);
while (counter)
{
struct timespec abstime;
set_timespec(abstime, 3);
pthread_cond_timedwait(&count_threshhold, &counter_mutex, &abstime);
}
pthread_mutex_unlock(&counter_mutex);
pthread_mutex_destroy(&counter_mutex);
pthread_cond_destroy(&count_threshhold);
pthread_attr_destroy(&attr);
}
else
#endif
{
2006-02-17 17:16:18 +01:00
MYSQL *mysql= 0;
if (!(mysql= db_connect(current_host,current_db,current_user,opt_password)))
{
free_defaults(argv_to_free);
return(1); /* purecov: deadcode */
}
if (mysql_query(mysql, "/*!40101 set @@character_set_database=binary */;"))
{
db_error(mysql); /* We shall countinue here, if --force was given */
return(1);
}
if (lock_tables)
lock_table(mysql, argc, argv);
for (; *argv != NULL; argv++)
if ((error= write_to_table(*argv, mysql)))
if (exitcode == 0)
exitcode= error;
db_disconnect(current_host, mysql);
}
Bug#34043: Server loops excessively in _checkchunk() when safemalloc is enabled Essentially, the problem is that safemalloc is excruciatingly slow as it checks all allocated blocks for overrun at each memory management primitive, yielding a almost exponential slowdown for the memory management functions (malloc, realloc, free). The overrun check basically consists of verifying some bytes of a block for certain magic keys, which catches some simple forms of overrun. Another minor problem is violation of aliasing rules and that its own internal list of blocks is prone to corruption. Another issue with safemalloc is rather the maintenance cost as the tool has a significant impact on the server code. Given the magnitude of memory debuggers available nowadays, especially those that are provided with the platform malloc implementation, maintenance of a in-house and largely obsolete memory debugger becomes a burden that is not worth the effort due to its slowness and lack of support for detecting more common forms of heap corruption. Since there are third-party tools that can provide the same functionality at a lower or comparable performance cost, the solution is to simply remove safemalloc. Third-party tools can provide the same functionality at a lower or comparable performance cost. The removal of safemalloc also allows a simplification of the malloc wrappers, removing quite a bit of kludge: redefinition of my_malloc, my_free and the removal of the unused second argument of my_free. Since free() always check whether the supplied pointer is null, redudant checks are also removed. Also, this patch adds unit testing for my_malloc and moves my_realloc implementation into the same file as the other memory allocation primitives.
2010-07-08 23:20:08 +02:00
my_free(opt_password);
#ifdef HAVE_SMEM
Bug#34043: Server loops excessively in _checkchunk() when safemalloc is enabled Essentially, the problem is that safemalloc is excruciatingly slow as it checks all allocated blocks for overrun at each memory management primitive, yielding a almost exponential slowdown for the memory management functions (malloc, realloc, free). The overrun check basically consists of verifying some bytes of a block for certain magic keys, which catches some simple forms of overrun. Another minor problem is violation of aliasing rules and that its own internal list of blocks is prone to corruption. Another issue with safemalloc is rather the maintenance cost as the tool has a significant impact on the server code. Given the magnitude of memory debuggers available nowadays, especially those that are provided with the platform malloc implementation, maintenance of a in-house and largely obsolete memory debugger becomes a burden that is not worth the effort due to its slowness and lack of support for detecting more common forms of heap corruption. Since there are third-party tools that can provide the same functionality at a lower or comparable performance cost, the solution is to simply remove safemalloc. Third-party tools can provide the same functionality at a lower or comparable performance cost. The removal of safemalloc also allows a simplification of the malloc wrappers, removing quite a bit of kludge: redefinition of my_malloc, my_free and the removal of the unused second argument of my_free. Since free() always check whether the supplied pointer is null, redudant checks are also removed. Also, this patch adds unit testing for my_malloc and moves my_realloc implementation into the same file as the other memory allocation primitives.
2010-07-08 23:20:08 +02:00
my_free(shared_memory_base_name);
#endif
2000-07-31 21:29:14 +02:00
free_defaults(argv_to_free);
my_end(my_end_arg);
2000-07-31 21:29:14 +02:00
return(exitcode);
}