2003-06-14 10:37:42 +02:00
|
|
|
/* Copyright (C) 2000-2003 MySQL AB
|
2003-05-31 12:15:46 +02:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
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 */
|
|
|
|
|
2003-06-03 12:02:57 +02:00
|
|
|
/*
|
|
|
|
This file is included by both libmysql.c (the MySQL client C API)
|
|
|
|
and the mysqld server to connect to another MYSQL server.
|
|
|
|
|
|
|
|
The differences for the two cases are:
|
|
|
|
|
|
|
|
- Things that only works for the client:
|
|
|
|
- Trying to automaticly determinate user name if not supplied to
|
|
|
|
mysql_real_connect()
|
|
|
|
- Support for reading local file with LOAD DATA LOCAL
|
|
|
|
- SHARED memory handling
|
|
|
|
- Protection against sigpipe
|
|
|
|
- Prepared statements
|
|
|
|
|
|
|
|
- Things that only works for the server
|
|
|
|
- Alarm handling on connect
|
|
|
|
|
|
|
|
In all other cases, the code should be idential for the client and
|
|
|
|
server.
|
|
|
|
*/
|
|
|
|
|
2003-05-31 12:15:46 +02:00
|
|
|
#include <my_global.h>
|
|
|
|
|
|
|
|
#include "mysql.h"
|
2003-06-17 18:32:31 +02:00
|
|
|
|
2004-06-25 20:56:23 +02:00
|
|
|
/* Remove client convenience wrappers */
|
|
|
|
#undef max_allowed_packet
|
|
|
|
#undef net_buffer_length
|
|
|
|
|
2003-06-17 18:32:31 +02:00
|
|
|
#ifdef EMBEDDED_LIBRARY
|
|
|
|
|
|
|
|
#undef MYSQL_SERVER
|
|
|
|
|
|
|
|
#ifndef MYSQL_CLIENT
|
|
|
|
#define MYSQL_CLIENT
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define CLI_MYSQL_REAL_CONNECT cli_mysql_real_connect
|
|
|
|
|
|
|
|
#undef net_flush
|
|
|
|
my_bool net_flush(NET *net);
|
|
|
|
|
|
|
|
#else /*EMBEDDED_LIBRARY*/
|
2003-12-21 01:07:45 +01:00
|
|
|
#define CLI_MYSQL_REAL_CONNECT STDCALL mysql_real_connect
|
2003-06-17 18:32:31 +02:00
|
|
|
#endif /*EMBEDDED_LIBRARY*/
|
2003-05-31 12:15:46 +02:00
|
|
|
#include <my_sys.h>
|
|
|
|
#include <mysys_err.h>
|
|
|
|
#include <m_string.h>
|
|
|
|
#include <m_ctype.h>
|
|
|
|
#include "mysql_version.h"
|
|
|
|
#include "mysqld_error.h"
|
|
|
|
#include "errmsg.h"
|
|
|
|
#include <violite.h>
|
|
|
|
#if defined(THREAD) && !defined(__WIN__)
|
|
|
|
#include <my_pthread.h> /* because of signal() */
|
2003-06-14 10:37:42 +02:00
|
|
|
#endif /* defined(THREAD) && !defined(__WIN__) */
|
2003-05-31 12:15:46 +02:00
|
|
|
|
2003-06-14 10:37:42 +02:00
|
|
|
#if defined(OS2) && defined(MYSQL_SERVER)
|
2003-05-31 12:15:46 +02:00
|
|
|
#undef ER
|
|
|
|
#define ER CER
|
2003-06-14 10:37:42 +02:00
|
|
|
#endif /* defined( OS2) && defined(MYSQL_SERVER) */
|
2003-05-31 12:15:46 +02:00
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <time.h>
|
|
|
|
#ifdef HAVE_PWD_H
|
|
|
|
#include <pwd.h>
|
|
|
|
#endif
|
|
|
|
#if !defined(MSDOS) && !defined(__WIN__)
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#ifdef HAVE_SELECT_H
|
|
|
|
# include <select.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_SELECT_H
|
|
|
|
#include <sys/select.h>
|
|
|
|
#endif
|
|
|
|
#endif /*!defined(MSDOS) && !defined(__WIN__) */
|
|
|
|
#ifdef HAVE_SYS_UN_H
|
|
|
|
# include <sys/un.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MSDOS) || defined(__WIN__)
|
|
|
|
#define perror(A)
|
|
|
|
#else
|
|
|
|
#include <errno.h>
|
|
|
|
#define SOCKET_ERROR -1
|
|
|
|
#endif
|
|
|
|
|
2003-06-14 10:37:42 +02:00
|
|
|
#ifdef __WIN__
|
|
|
|
#define CONNECT_TIMEOUT 20
|
|
|
|
#else
|
|
|
|
#define CONNECT_TIMEOUT 0
|
|
|
|
#endif
|
|
|
|
|
2003-05-31 12:15:46 +02:00
|
|
|
#include "client_settings.h"
|
|
|
|
#include <sql_common.h>
|
|
|
|
|
2003-06-14 10:37:42 +02:00
|
|
|
uint mysql_port=0;
|
|
|
|
char *mysql_unix_port= 0;
|
|
|
|
const char *unknown_sqlstate= "HY000";
|
2003-06-05 22:19:56 +02:00
|
|
|
const char *not_error_sqlstate= "00000";
|
2003-06-14 10:37:42 +02:00
|
|
|
#ifdef HAVE_SMEM
|
|
|
|
char *shared_memory_base_name= 0;
|
2003-06-15 22:24:37 +02:00
|
|
|
const char *def_shared_memory_base_name= default_shared_memory_base_name;
|
2003-06-04 09:21:49 +02:00
|
|
|
#endif
|
|
|
|
|
2003-06-14 10:37:42 +02:00
|
|
|
static void mysql_close_free_options(MYSQL *mysql);
|
|
|
|
static void mysql_close_free(MYSQL *mysql);
|
|
|
|
|
2003-08-18 23:08:08 +02:00
|
|
|
#if !(defined(__WIN__) || defined(OS2) || defined(__NETWARE__))
|
|
|
|
static int wait_for_data(my_socket fd, uint timeout);
|
|
|
|
#endif
|
|
|
|
|
2006-06-19 19:11:01 +02:00
|
|
|
CHARSET_INFO *default_client_charset_info = &my_charset_latin1;
|
|
|
|
|
2004-08-21 05:07:32 +02:00
|
|
|
|
2003-05-02 18:07:41 +02:00
|
|
|
/****************************************************************************
|
|
|
|
A modified version of connect(). my_connect() allows you to specify
|
|
|
|
a timeout value, in seconds, that we should wait until we
|
|
|
|
derermine we can't connect to a particular host. If timeout is 0,
|
|
|
|
my_connect() will behave exactly like connect().
|
|
|
|
|
|
|
|
Base version coded by Steve Bernacki, Jr. <steve@navinet.net>
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2003-08-18 23:08:08 +02:00
|
|
|
int my_connect(my_socket fd, const struct sockaddr *name, uint namelen,
|
|
|
|
uint timeout)
|
2003-05-02 18:07:41 +02:00
|
|
|
{
|
|
|
|
#if defined(__WIN__) || defined(OS2) || defined(__NETWARE__)
|
2003-08-18 23:08:08 +02:00
|
|
|
return connect(fd, (struct sockaddr*) name, namelen);
|
2003-05-02 18:07:41 +02:00
|
|
|
#else
|
|
|
|
int flags, res, s_err;
|
|
|
|
|
|
|
|
/*
|
|
|
|
If they passed us a timeout of zero, we should behave
|
|
|
|
exactly like the normal connect() call does.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (timeout == 0)
|
2003-08-18 23:08:08 +02:00
|
|
|
return connect(fd, (struct sockaddr*) name, namelen);
|
2003-05-02 18:07:41 +02:00
|
|
|
|
2003-08-18 23:08:08 +02:00
|
|
|
flags = fcntl(fd, F_GETFL, 0); /* Set socket to not block */
|
2003-05-02 18:07:41 +02:00
|
|
|
#ifdef O_NONBLOCK
|
2003-08-18 23:08:08 +02:00
|
|
|
fcntl(fd, F_SETFL, flags | O_NONBLOCK); /* and save the flags.. */
|
2003-05-02 18:07:41 +02:00
|
|
|
#endif
|
|
|
|
|
2003-08-18 23:08:08 +02:00
|
|
|
res= connect(fd, (struct sockaddr*) name, namelen);
|
|
|
|
s_err= errno; /* Save the error... */
|
|
|
|
fcntl(fd, F_SETFL, flags);
|
2003-05-02 18:07:41 +02:00
|
|
|
if ((res != 0) && (s_err != EINPROGRESS))
|
|
|
|
{
|
2003-08-18 23:08:08 +02:00
|
|
|
errno= s_err; /* Restore it */
|
|
|
|
return(-1);
|
2003-05-02 18:07:41 +02:00
|
|
|
}
|
|
|
|
if (res == 0) /* Connected quickly! */
|
|
|
|
return(0);
|
2003-08-18 23:08:08 +02:00
|
|
|
return wait_for_data(fd, timeout);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Wait up to timeout seconds for a connection to be established.
|
|
|
|
|
|
|
|
We prefer to do this with poll() as there is no limitations with this.
|
|
|
|
If not, we will use select()
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if !(defined(__WIN__) || defined(OS2) || defined(__NETWARE__))
|
|
|
|
|
|
|
|
static int wait_for_data(my_socket fd, uint timeout)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_POLL
|
|
|
|
struct pollfd ufds;
|
|
|
|
int res;
|
|
|
|
|
|
|
|
ufds.fd= fd;
|
|
|
|
ufds.events= POLLIN | POLLPRI;
|
|
|
|
if (!(res= poll(&ufds, 1, (int) timeout*1000)))
|
|
|
|
{
|
|
|
|
errno= EINTR;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (res < 0 || !(ufds.revents & (POLLIN | POLLPRI)))
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
#else
|
|
|
|
SOCKOPT_OPTLEN_TYPE s_err_size = sizeof(uint);
|
|
|
|
fd_set sfds;
|
|
|
|
struct timeval tv;
|
|
|
|
time_t start_time, now_time;
|
|
|
|
int res, s_err;
|
|
|
|
|
|
|
|
if (fd >= FD_SETSIZE) /* Check if wrong error */
|
|
|
|
return 0; /* Can't use timeout */
|
2003-05-02 18:07:41 +02:00
|
|
|
|
|
|
|
/*
|
2003-08-18 23:08:08 +02:00
|
|
|
Our connection is "in progress." We can use the select() call to wait
|
|
|
|
up to a specified period of time for the connection to suceed.
|
|
|
|
If select() returns 0 (after waiting howevermany seconds), our socket
|
|
|
|
never became writable (host is probably unreachable.) Otherwise, if
|
2003-05-02 18:07:41 +02:00
|
|
|
select() returns 1, then one of two conditions exist:
|
2003-08-18 23:08:08 +02:00
|
|
|
|
2003-05-02 18:07:41 +02:00
|
|
|
1. An error occured. We use getsockopt() to check for this.
|
|
|
|
2. The connection was set up sucessfully: getsockopt() will
|
|
|
|
return 0 as an error.
|
2003-08-18 23:08:08 +02:00
|
|
|
|
2003-05-02 18:07:41 +02:00
|
|
|
Thanks goes to Andrew Gierth <andrew@erlenstar.demon.co.uk>
|
|
|
|
who posted this method of timing out a connect() in
|
|
|
|
comp.unix.programmer on August 15th, 1997.
|
|
|
|
*/
|
|
|
|
|
|
|
|
FD_ZERO(&sfds);
|
2003-08-18 23:08:08 +02:00
|
|
|
FD_SET(fd, &sfds);
|
2003-05-02 18:07:41 +02:00
|
|
|
/*
|
2003-08-18 23:08:08 +02:00
|
|
|
select could be interrupted by a signal, and if it is,
|
2003-05-02 18:07:41 +02:00
|
|
|
the timeout should be adjusted and the select restarted
|
2003-08-18 23:08:08 +02:00
|
|
|
to work around OSes that don't restart select and
|
2003-05-02 18:07:41 +02:00
|
|
|
implementations of select that don't adjust tv upon
|
|
|
|
failure to reflect the time remaining
|
2003-08-18 23:08:08 +02:00
|
|
|
*/
|
2003-05-02 18:07:41 +02:00
|
|
|
start_time = time(NULL);
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
tv.tv_sec = (long) timeout;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
#if defined(HPUX10) && defined(THREAD)
|
2003-08-18 23:08:08 +02:00
|
|
|
if ((res = select(fd+1, NULL, (int*) &sfds, NULL, &tv)) > 0)
|
2003-05-02 18:07:41 +02:00
|
|
|
break;
|
|
|
|
#else
|
2003-08-18 23:08:08 +02:00
|
|
|
if ((res = select(fd+1, NULL, &sfds, NULL, &tv)) > 0)
|
2003-05-02 18:07:41 +02:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
if (res == 0) /* timeout */
|
|
|
|
return -1;
|
|
|
|
now_time=time(NULL);
|
|
|
|
timeout-= (uint) (now_time - start_time);
|
|
|
|
if (errno != EINTR || (int) timeout <= 0)
|
2003-08-18 23:08:08 +02:00
|
|
|
return -1;
|
2003-05-02 18:07:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
select() returned something more interesting than zero, let's
|
|
|
|
see if we have any errors. If the next two statements pass,
|
|
|
|
we've got an open socket!
|
|
|
|
*/
|
|
|
|
|
|
|
|
s_err=0;
|
2003-08-18 23:08:08 +02:00
|
|
|
if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (char*) &s_err, &s_err_size) != 0)
|
|
|
|
return(-1);
|
2003-05-02 18:07:41 +02:00
|
|
|
|
|
|
|
if (s_err)
|
|
|
|
{ /* getsockopt could succeed */
|
|
|
|
errno = s_err;
|
2003-08-18 23:08:08 +02:00
|
|
|
return(-1); /* but return an error... */
|
2003-05-02 18:07:41 +02:00
|
|
|
}
|
|
|
|
return (0); /* ok */
|
2003-08-18 23:08:08 +02:00
|
|
|
#endif /* HAVE_POLL */
|
2003-05-02 18:07:41 +02:00
|
|
|
}
|
2003-08-18 23:08:08 +02:00
|
|
|
#endif /* defined(__WIN__) || defined(OS2) || defined(__NETWARE__) */
|
|
|
|
|
2003-05-02 18:07:41 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
Create a named pipe connection
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef __WIN__
|
|
|
|
|
|
|
|
HANDLE create_named_pipe(NET *net, uint connect_timeout, char **arg_host,
|
|
|
|
char **arg_unix_socket)
|
|
|
|
{
|
|
|
|
HANDLE hPipe=INVALID_HANDLE_VALUE;
|
2004-02-22 07:09:32 +01:00
|
|
|
char pipe_name[1024];
|
2003-05-02 18:07:41 +02:00
|
|
|
DWORD dwMode;
|
|
|
|
int i;
|
|
|
|
my_bool testing_named_pipes=0;
|
|
|
|
char *host= *arg_host, *unix_socket= *arg_unix_socket;
|
|
|
|
|
|
|
|
if ( ! unix_socket || (unix_socket)[0] == 0x00)
|
|
|
|
unix_socket = mysql_unix_port;
|
|
|
|
if (!host || !strcmp(host,LOCAL_HOST))
|
|
|
|
host=LOCAL_HOST_NAMEDPIPE;
|
|
|
|
|
2004-02-22 07:09:32 +01:00
|
|
|
|
|
|
|
pipe_name[sizeof(pipe_name)-1]= 0; /* Safety if too long string */
|
|
|
|
strxnmov(pipe_name, sizeof(pipe_name)-1, "\\\\", host, "\\pipe\\",
|
|
|
|
unix_socket, NullS);
|
|
|
|
DBUG_PRINT("info",("Server name: '%s'. Named Pipe: %s", host, unix_socket));
|
2003-05-02 18:07:41 +02:00
|
|
|
|
|
|
|
for (i=0 ; i < 100 ; i++) /* Don't retry forever */
|
|
|
|
{
|
2004-02-22 07:09:32 +01:00
|
|
|
if ((hPipe = CreateFile(pipe_name,
|
2003-05-02 18:07:41 +02:00
|
|
|
GENERIC_READ | GENERIC_WRITE,
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
OPEN_EXISTING,
|
|
|
|
0,
|
|
|
|
NULL )) != INVALID_HANDLE_VALUE)
|
|
|
|
break;
|
|
|
|
if (GetLastError() != ERROR_PIPE_BUSY)
|
|
|
|
{
|
|
|
|
net->last_errno=CR_NAMEDPIPEOPEN_ERROR;
|
2003-05-31 12:15:46 +02:00
|
|
|
strmov(net->sqlstate, unknown_sqlstate);
|
2005-01-09 02:19:42 +01:00
|
|
|
my_snprintf(net->last_error, sizeof(net->last_error)-1,
|
|
|
|
ER(net->last_errno), host, unix_socket,
|
|
|
|
(ulong) GetLastError());
|
2003-05-02 18:07:41 +02:00
|
|
|
return INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
/* wait for for an other instance */
|
2004-02-22 07:09:32 +01:00
|
|
|
if (! WaitNamedPipe(pipe_name, connect_timeout*1000) )
|
2003-05-02 18:07:41 +02:00
|
|
|
{
|
|
|
|
net->last_errno=CR_NAMEDPIPEWAIT_ERROR;
|
2003-05-31 12:15:46 +02:00
|
|
|
strmov(net->sqlstate, unknown_sqlstate);
|
2005-01-09 02:19:42 +01:00
|
|
|
my_snprintf(net->last_error, sizeof(net->last_error)-1,
|
|
|
|
ER(net->last_errno), host, unix_socket,
|
|
|
|
(ulong) GetLastError());
|
2003-05-02 18:07:41 +02:00
|
|
|
return INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (hPipe == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
net->last_errno=CR_NAMEDPIPEOPEN_ERROR;
|
2003-05-31 12:15:46 +02:00
|
|
|
strmov(net->sqlstate, unknown_sqlstate);
|
2005-01-09 02:19:42 +01:00
|
|
|
my_snprintf(net->last_error, sizeof(net->last_error)-1,
|
|
|
|
ER(net->last_errno), host, unix_socket,
|
|
|
|
(ulong) GetLastError());
|
2003-05-02 18:07:41 +02:00
|
|
|
return INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
dwMode = PIPE_READMODE_BYTE | PIPE_WAIT;
|
|
|
|
if ( !SetNamedPipeHandleState(hPipe, &dwMode, NULL, NULL) )
|
|
|
|
{
|
|
|
|
CloseHandle( hPipe );
|
|
|
|
net->last_errno=CR_NAMEDPIPESETSTATE_ERROR;
|
2003-05-31 12:15:46 +02:00
|
|
|
strmov(net->sqlstate, unknown_sqlstate);
|
2005-01-09 02:19:42 +01:00
|
|
|
my_snprintf(net->last_error, sizeof(net->last_error)-1,
|
|
|
|
ER(net->last_errno),host, unix_socket,
|
|
|
|
(ulong) GetLastError());
|
2003-05-02 18:07:41 +02:00
|
|
|
return INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
*arg_host=host ; *arg_unix_socket=unix_socket; /* connect arg */
|
|
|
|
return (hPipe);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-06-14 10:37:42 +02:00
|
|
|
|
2003-06-04 05:59:32 +02:00
|
|
|
/*
|
|
|
|
Create new shared memory connection, return handler of connection
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
create_shared_memory()
|
|
|
|
mysql Pointer of mysql structure
|
|
|
|
net Pointer of net structure
|
|
|
|
connect_timeout Timeout of connection
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_SMEM
|
|
|
|
HANDLE create_shared_memory(MYSQL *mysql,NET *net, uint connect_timeout)
|
|
|
|
{
|
|
|
|
ulong smem_buffer_length = shared_memory_buffer_length + 4;
|
|
|
|
/*
|
|
|
|
event_connect_request is event object for start connection actions
|
|
|
|
event_connect_answer is event object for confirm, that server put data
|
2003-06-14 10:37:42 +02:00
|
|
|
handle_connect_file_map is file-mapping object, use for create shared
|
|
|
|
memory
|
2003-06-04 05:59:32 +02:00
|
|
|
handle_connect_map is pointer on shared memory
|
|
|
|
handle_map is pointer on shared memory for client
|
|
|
|
event_server_wrote,
|
|
|
|
event_server_read,
|
|
|
|
event_client_wrote,
|
|
|
|
event_client_read are events for transfer data between server and client
|
|
|
|
handle_file_map is file-mapping object, use for create shared memory
|
|
|
|
*/
|
|
|
|
HANDLE event_connect_request = NULL;
|
|
|
|
HANDLE event_connect_answer = NULL;
|
|
|
|
HANDLE handle_connect_file_map = NULL;
|
|
|
|
char *handle_connect_map = NULL;
|
|
|
|
|
|
|
|
char *handle_map = NULL;
|
|
|
|
HANDLE event_server_wrote = NULL;
|
|
|
|
HANDLE event_server_read = NULL;
|
|
|
|
HANDLE event_client_wrote = NULL;
|
|
|
|
HANDLE event_client_read = NULL;
|
2004-12-14 15:24:19 +01:00
|
|
|
HANDLE event_conn_closed = NULL;
|
2003-06-04 05:59:32 +02:00
|
|
|
HANDLE handle_file_map = NULL;
|
|
|
|
ulong connect_number;
|
|
|
|
char connect_number_char[22], *p;
|
|
|
|
char tmp[64];
|
|
|
|
char *suffix_pos;
|
|
|
|
DWORD error_allow = 0;
|
|
|
|
DWORD error_code = 0;
|
2005-03-25 23:21:52 +01:00
|
|
|
DWORD event_access_rights= SYNCHRONIZE | EVENT_MODIFY_STATE;
|
2003-06-04 05:59:32 +02:00
|
|
|
char *shared_memory_base_name = mysql->options.shared_memory_base_name;
|
|
|
|
|
|
|
|
/*
|
|
|
|
The name of event and file-mapping events create agree next rule:
|
2003-06-05 22:19:56 +02:00
|
|
|
shared_memory_base_name+unique_part
|
2003-06-04 05:59:32 +02:00
|
|
|
Where:
|
2003-06-05 22:19:56 +02:00
|
|
|
shared_memory_base_name is unique value for each server
|
|
|
|
unique_part is uniquel value for each object (events and file-mapping)
|
2003-06-04 05:59:32 +02:00
|
|
|
*/
|
|
|
|
suffix_pos = strxmov(tmp,shared_memory_base_name,"_",NullS);
|
|
|
|
strmov(suffix_pos, "CONNECT_REQUEST");
|
2005-03-25 23:21:52 +01:00
|
|
|
if (!(event_connect_request= OpenEvent(event_access_rights, FALSE, tmp)))
|
2003-06-04 05:59:32 +02:00
|
|
|
{
|
|
|
|
error_allow = CR_SHARED_MEMORY_CONNECT_REQUEST_ERROR;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
strmov(suffix_pos, "CONNECT_ANSWER");
|
2005-03-25 23:21:52 +01:00
|
|
|
if (!(event_connect_answer= OpenEvent(event_access_rights,FALSE,tmp)))
|
2003-06-04 05:59:32 +02:00
|
|
|
{
|
|
|
|
error_allow = CR_SHARED_MEMORY_CONNECT_ANSWER_ERROR;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
strmov(suffix_pos, "CONNECT_DATA");
|
|
|
|
if (!(handle_connect_file_map= OpenFileMapping(FILE_MAP_WRITE,FALSE,tmp)))
|
|
|
|
{
|
|
|
|
error_allow = CR_SHARED_MEMORY_CONNECT_FILE_MAP_ERROR;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (!(handle_connect_map= MapViewOfFile(handle_connect_file_map,
|
|
|
|
FILE_MAP_WRITE,0,0,sizeof(DWORD))))
|
|
|
|
{
|
|
|
|
error_allow = CR_SHARED_MEMORY_CONNECT_MAP_ERROR;
|
|
|
|
goto err;
|
|
|
|
}
|
2003-06-05 22:19:56 +02:00
|
|
|
|
|
|
|
/* Send to server request of connection */
|
2003-06-04 05:59:32 +02:00
|
|
|
if (!SetEvent(event_connect_request))
|
|
|
|
{
|
|
|
|
error_allow = CR_SHARED_MEMORY_CONNECT_SET_ERROR;
|
|
|
|
goto err;
|
|
|
|
}
|
2003-06-05 22:19:56 +02:00
|
|
|
|
|
|
|
/* Wait of answer from server */
|
2003-06-04 05:59:32 +02:00
|
|
|
if (WaitForSingleObject(event_connect_answer,connect_timeout*1000) !=
|
|
|
|
WAIT_OBJECT_0)
|
|
|
|
{
|
2004-07-01 05:18:41 +02:00
|
|
|
error_allow = CR_SHARED_MEMORY_CONNECT_ABANDONED_ERROR;
|
2003-06-04 05:59:32 +02:00
|
|
|
goto err;
|
|
|
|
}
|
2003-06-05 22:19:56 +02:00
|
|
|
|
|
|
|
/* Get number of connection */
|
2003-06-04 05:59:32 +02:00
|
|
|
connect_number = uint4korr(handle_connect_map);/*WAX2*/
|
2004-05-27 15:54:40 +02:00
|
|
|
p= int10_to_str(connect_number, connect_number_char, 10);
|
2003-06-04 05:59:32 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
The name of event and file-mapping events create agree next rule:
|
|
|
|
shared_memory_base_name+unique_part+number_of_connection
|
2003-06-05 22:19:56 +02:00
|
|
|
|
2003-06-04 05:59:32 +02:00
|
|
|
Where:
|
2003-06-05 22:19:56 +02:00
|
|
|
shared_memory_base_name is uniquel value for each server
|
|
|
|
unique_part is uniquel value for each object (events and file-mapping)
|
|
|
|
number_of_connection is number of connection between server and client
|
2003-06-04 05:59:32 +02:00
|
|
|
*/
|
|
|
|
suffix_pos = strxmov(tmp,shared_memory_base_name,"_",connect_number_char,
|
|
|
|
"_",NullS);
|
|
|
|
strmov(suffix_pos, "DATA");
|
|
|
|
if ((handle_file_map = OpenFileMapping(FILE_MAP_WRITE,FALSE,tmp)) == NULL)
|
|
|
|
{
|
|
|
|
error_allow = CR_SHARED_MEMORY_FILE_MAP_ERROR;
|
|
|
|
goto err2;
|
|
|
|
}
|
|
|
|
if ((handle_map = MapViewOfFile(handle_file_map,FILE_MAP_WRITE,0,0,
|
|
|
|
smem_buffer_length)) == NULL)
|
|
|
|
{
|
|
|
|
error_allow = CR_SHARED_MEMORY_MAP_ERROR;
|
|
|
|
goto err2;
|
|
|
|
}
|
|
|
|
|
|
|
|
strmov(suffix_pos, "SERVER_WROTE");
|
2005-03-25 23:21:52 +01:00
|
|
|
if ((event_server_wrote = OpenEvent(event_access_rights,FALSE,tmp)) == NULL)
|
2003-06-04 05:59:32 +02:00
|
|
|
{
|
|
|
|
error_allow = CR_SHARED_MEMORY_EVENT_ERROR;
|
|
|
|
goto err2;
|
|
|
|
}
|
|
|
|
|
|
|
|
strmov(suffix_pos, "SERVER_READ");
|
2005-03-25 23:21:52 +01:00
|
|
|
if ((event_server_read = OpenEvent(event_access_rights,FALSE,tmp)) == NULL)
|
2003-06-04 05:59:32 +02:00
|
|
|
{
|
|
|
|
error_allow = CR_SHARED_MEMORY_EVENT_ERROR;
|
|
|
|
goto err2;
|
|
|
|
}
|
|
|
|
|
|
|
|
strmov(suffix_pos, "CLIENT_WROTE");
|
2005-03-25 23:21:52 +01:00
|
|
|
if ((event_client_wrote = OpenEvent(event_access_rights,FALSE,tmp)) == NULL)
|
2003-06-04 05:59:32 +02:00
|
|
|
{
|
|
|
|
error_allow = CR_SHARED_MEMORY_EVENT_ERROR;
|
|
|
|
goto err2;
|
|
|
|
}
|
|
|
|
|
|
|
|
strmov(suffix_pos, "CLIENT_READ");
|
2005-03-25 23:21:52 +01:00
|
|
|
if ((event_client_read = OpenEvent(event_access_rights,FALSE,tmp)) == NULL)
|
2003-06-04 05:59:32 +02:00
|
|
|
{
|
|
|
|
error_allow = CR_SHARED_MEMORY_EVENT_ERROR;
|
|
|
|
goto err2;
|
|
|
|
}
|
2004-12-14 15:24:19 +01:00
|
|
|
|
|
|
|
strmov(suffix_pos, "CONNECTION_CLOSED");
|
2005-03-25 23:21:52 +01:00
|
|
|
if ((event_conn_closed = OpenEvent(event_access_rights,FALSE,tmp)) == NULL)
|
2004-12-14 15:24:19 +01:00
|
|
|
{
|
|
|
|
error_allow = CR_SHARED_MEMORY_EVENT_ERROR;
|
|
|
|
goto err2;
|
|
|
|
}
|
2003-06-04 05:59:32 +02:00
|
|
|
/*
|
|
|
|
Set event that server should send data
|
|
|
|
*/
|
|
|
|
SetEvent(event_server_read);
|
|
|
|
|
|
|
|
err2:
|
|
|
|
if (error_allow == 0)
|
|
|
|
{
|
|
|
|
net->vio= vio_new_win32shared_memory(net,handle_file_map,handle_map,
|
2004-12-23 12:04:40 +01:00
|
|
|
event_server_wrote,
|
2003-06-04 05:59:32 +02:00
|
|
|
event_server_read,event_client_wrote,
|
2004-12-23 12:04:40 +01:00
|
|
|
event_client_read,event_conn_closed);
|
2003-06-04 05:59:32 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
error_code = GetLastError();
|
|
|
|
if (event_server_read)
|
|
|
|
CloseHandle(event_server_read);
|
|
|
|
if (event_server_wrote)
|
|
|
|
CloseHandle(event_server_wrote);
|
|
|
|
if (event_client_read)
|
|
|
|
CloseHandle(event_client_read);
|
|
|
|
if (event_client_wrote)
|
|
|
|
CloseHandle(event_client_wrote);
|
2004-12-14 15:24:19 +01:00
|
|
|
if (event_conn_closed)
|
|
|
|
CloseHandle(event_conn_closed);
|
2003-06-04 05:59:32 +02:00
|
|
|
if (handle_map)
|
|
|
|
UnmapViewOfFile(handle_map);
|
|
|
|
if (handle_file_map)
|
|
|
|
CloseHandle(handle_file_map);
|
|
|
|
}
|
|
|
|
err:
|
|
|
|
if (error_allow)
|
|
|
|
error_code = GetLastError();
|
|
|
|
if (event_connect_request)
|
|
|
|
CloseHandle(event_connect_request);
|
|
|
|
if (event_connect_answer)
|
|
|
|
CloseHandle(event_connect_answer);
|
|
|
|
if (handle_connect_map)
|
|
|
|
UnmapViewOfFile(handle_connect_map);
|
|
|
|
if (handle_connect_file_map)
|
|
|
|
CloseHandle(handle_connect_file_map);
|
|
|
|
if (error_allow)
|
|
|
|
{
|
|
|
|
net->last_errno=error_allow;
|
|
|
|
strmov(net->sqlstate, unknown_sqlstate);
|
|
|
|
if (error_allow == CR_SHARED_MEMORY_EVENT_ERROR)
|
2005-01-09 02:19:42 +01:00
|
|
|
my_snprintf(net->last_error,sizeof(net->last_error)-1,
|
|
|
|
ER(net->last_errno),suffix_pos,error_code);
|
2003-06-04 05:59:32 +02:00
|
|
|
else
|
2005-01-09 02:19:42 +01:00
|
|
|
my_snprintf(net->last_error,sizeof(net->last_error)-1,
|
|
|
|
ER(net->last_errno),error_code);
|
2003-06-04 05:59:32 +02:00
|
|
|
return(INVALID_HANDLE_VALUE);
|
|
|
|
}
|
|
|
|
return(handle_map);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-05-02 18:07:41 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
Read a packet from server. Give error message if socket was down
|
|
|
|
or packet is an error message
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
ulong
|
A fix and a test case for Bug#15752 "Lost connection to MySQL server
when calling a SP from C API"
The bug was caused by lack of checks for misuse in mysql_real_query.
A stored procedure always returns at least one result, which is the
status of execution of the procedure itself.
This result, or so-called OK packet, is similar to a result
returned by INSERT/UPDATE/CREATE operations: it contains the overall
status of execution, the number of affected rows and the number of
warnings. The client test program attached to the bug did not read this
result and ivnoked the next query. In turn, libmysql had no check for
such scenario and mysql_real_query was simply trying to send that query
without reading the pending response, thus messing up the communication
protocol.
The fix is to return an error from mysql_real_query when it's called
prior to retrieval of all pending results.
client/mysqlbinlog.cc:
net_safe_read -> cli_safe_read
include/mysql.h:
Remove a private function from the public header.
include/mysql_com.h:
Remove a define that is never used.
include/sql_common.h:
Add a declaration for cli_safe_read - a function that reads one packet
from the server.
libmysql/libmysql.c:
net_safe_read -> cli_safe_read
Return CR_COMMANDS_OUT_OF_SYNC on attempt to execute a statement
using a connection which has pending result sets.
sql-common/client.c:
Actual fix for Bug#15752: if the server has pending result sets for
the client, return CR_COMMANDS_OUT_OF_SYNC on attempt to execute
another query. Similarly to the behaviour of mysql_use_result(),
multiple result sets block the connection and must be fetched
before it can be used for another query.
This uncovered an error in the protocol: the server doesn't drop
SERVER_MORE_RESULTS_EXISTS status flag upon an error, so in case of
a multi-query like SELECT 1; SELECT syntax_error; SELECT 2;
the client has no way to know that the server won't ever come to
execution of the third query and won't return any result sets for it.
For now, fix it in cli_safe_read, as a proper fix requires extension
of the client-server protocol.
sql/protocol.cc:
Remove a name that is never used.
sql/slave.cc:
net_safe_read -> cli_safe_read
tests/mysql_client_test.c:
Make 'query' a local variable to avoid name clash.
Add a test case for Bug#15752 "Lost connection to MySQL server when
calling an SP from C API"
2006-07-24 12:56:53 +02:00
|
|
|
cli_safe_read(MYSQL *mysql)
|
2003-05-02 18:07:41 +02:00
|
|
|
{
|
|
|
|
NET *net= &mysql->net;
|
|
|
|
ulong len=0;
|
2003-06-16 00:13:22 +02:00
|
|
|
init_sigpipe_variables
|
2003-05-02 18:07:41 +02:00
|
|
|
|
|
|
|
/* Don't give sigpipe errors if the client doesn't want them */
|
|
|
|
set_sigpipe(mysql);
|
|
|
|
if (net->vio != 0)
|
|
|
|
len=my_net_read(net);
|
|
|
|
reset_sigpipe(mysql);
|
|
|
|
|
|
|
|
if (len == packet_error || len == 0)
|
|
|
|
{
|
2006-11-20 21:42:06 +01:00
|
|
|
DBUG_PRINT("error",("Wrong connection or packet. fd: %s len: %lu",
|
2003-05-02 18:07:41 +02:00
|
|
|
vio_description(net->vio),len));
|
2003-07-28 17:16:11 +02:00
|
|
|
#ifdef MYSQL_SERVER
|
2006-01-03 17:54:54 +01:00
|
|
|
if (net->vio && vio_was_interrupted(net->vio))
|
2003-07-28 17:16:11 +02:00
|
|
|
return (packet_error);
|
|
|
|
#endif /*MYSQL_SERVER*/
|
2003-05-02 18:07:41 +02:00
|
|
|
end_server(mysql);
|
2004-03-12 13:21:48 +01:00
|
|
|
set_mysql_error(mysql, net->last_errno == ER_NET_PACKET_TOO_LARGE ?
|
|
|
|
CR_NET_PACKET_TOO_LARGE: CR_SERVER_LOST, unknown_sqlstate);
|
2003-05-02 18:07:41 +02:00
|
|
|
return (packet_error);
|
|
|
|
}
|
|
|
|
if (net->read_pos[0] == 255)
|
|
|
|
{
|
|
|
|
if (len > 3)
|
|
|
|
{
|
|
|
|
char *pos=(char*) net->read_pos+1;
|
|
|
|
net->last_errno=uint2korr(pos);
|
|
|
|
pos+=2;
|
|
|
|
len-=2;
|
2003-05-31 12:15:46 +02:00
|
|
|
if (protocol_41(mysql) && pos[0] == '#')
|
|
|
|
{
|
|
|
|
strmake(net->sqlstate, pos+1, SQLSTATE_LENGTH);
|
|
|
|
pos+= SQLSTATE_LENGTH+1;
|
|
|
|
}
|
2003-05-02 18:07:41 +02:00
|
|
|
(void) strmake(net->last_error,(char*) pos,
|
|
|
|
min((uint) len,(uint) sizeof(net->last_error)-1));
|
|
|
|
}
|
|
|
|
else
|
2004-03-12 13:21:48 +01:00
|
|
|
set_mysql_error(mysql, CR_UNKNOWN_ERROR, unknown_sqlstate);
|
A fix and a test case for Bug#15752 "Lost connection to MySQL server
when calling a SP from C API"
The bug was caused by lack of checks for misuse in mysql_real_query.
A stored procedure always returns at least one result, which is the
status of execution of the procedure itself.
This result, or so-called OK packet, is similar to a result
returned by INSERT/UPDATE/CREATE operations: it contains the overall
status of execution, the number of affected rows and the number of
warnings. The client test program attached to the bug did not read this
result and ivnoked the next query. In turn, libmysql had no check for
such scenario and mysql_real_query was simply trying to send that query
without reading the pending response, thus messing up the communication
protocol.
The fix is to return an error from mysql_real_query when it's called
prior to retrieval of all pending results.
client/mysqlbinlog.cc:
net_safe_read -> cli_safe_read
include/mysql.h:
Remove a private function from the public header.
include/mysql_com.h:
Remove a define that is never used.
include/sql_common.h:
Add a declaration for cli_safe_read - a function that reads one packet
from the server.
libmysql/libmysql.c:
net_safe_read -> cli_safe_read
Return CR_COMMANDS_OUT_OF_SYNC on attempt to execute a statement
using a connection which has pending result sets.
sql-common/client.c:
Actual fix for Bug#15752: if the server has pending result sets for
the client, return CR_COMMANDS_OUT_OF_SYNC on attempt to execute
another query. Similarly to the behaviour of mysql_use_result(),
multiple result sets block the connection and must be fetched
before it can be used for another query.
This uncovered an error in the protocol: the server doesn't drop
SERVER_MORE_RESULTS_EXISTS status flag upon an error, so in case of
a multi-query like SELECT 1; SELECT syntax_error; SELECT 2;
the client has no way to know that the server won't ever come to
execution of the third query and won't return any result sets for it.
For now, fix it in cli_safe_read, as a proper fix requires extension
of the client-server protocol.
sql/protocol.cc:
Remove a name that is never used.
sql/slave.cc:
net_safe_read -> cli_safe_read
tests/mysql_client_test.c:
Make 'query' a local variable to avoid name clash.
Add a test case for Bug#15752 "Lost connection to MySQL server when
calling an SP from C API"
2006-07-24 12:56:53 +02:00
|
|
|
/*
|
|
|
|
Cover a protocol design error: error packet does not
|
|
|
|
contain the server status. Therefore, the client has no way
|
|
|
|
to find out whether there are more result sets of
|
|
|
|
a multiple-result-set statement pending. Luckily, in 5.0 an
|
|
|
|
error always aborts execution of a statement, wherever it is
|
|
|
|
a multi-statement or a stored procedure, so it should be
|
|
|
|
safe to unconditionally turn off the flag here.
|
|
|
|
*/
|
|
|
|
mysql->server_status&= ~SERVER_MORE_RESULTS_EXISTS;
|
2004-03-12 13:21:48 +01:00
|
|
|
|
2003-05-31 12:15:46 +02:00
|
|
|
DBUG_PRINT("error",("Got error: %d/%s (%s)",
|
|
|
|
net->last_errno, net->sqlstate, net->last_error));
|
2003-05-02 18:07:41 +02:00
|
|
|
return(packet_error);
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2003-05-31 12:15:46 +02:00
|
|
|
void free_rows(MYSQL_DATA *cur)
|
2003-05-02 18:07:41 +02:00
|
|
|
{
|
|
|
|
if (cur)
|
|
|
|
{
|
|
|
|
free_root(&cur->alloc,MYF(0));
|
|
|
|
my_free((gptr) cur,MYF(0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-11-20 21:06:25 +01:00
|
|
|
my_bool
|
2003-06-17 18:32:31 +02:00
|
|
|
cli_advanced_command(MYSQL *mysql, enum enum_server_command command,
|
2003-09-17 12:18:18 +02:00
|
|
|
const char *header, ulong header_length,
|
2006-06-01 14:06:42 +02:00
|
|
|
const char *arg, ulong arg_length, my_bool skip_check,
|
|
|
|
MYSQL_STMT *stmt __attribute__((unused)))
|
2003-05-02 18:07:41 +02:00
|
|
|
{
|
|
|
|
NET *net= &mysql->net;
|
|
|
|
my_bool result= 1;
|
2003-06-16 00:13:22 +02:00
|
|
|
init_sigpipe_variables
|
A fix and a test case for Bug#15752 "Lost connection to MySQL server
when calling a SP from C API"
The bug was caused by lack of checks for misuse in mysql_real_query.
A stored procedure always returns at least one result, which is the
status of execution of the procedure itself.
This result, or so-called OK packet, is similar to a result
returned by INSERT/UPDATE/CREATE operations: it contains the overall
status of execution, the number of affected rows and the number of
warnings. The client test program attached to the bug did not read this
result and ivnoked the next query. In turn, libmysql had no check for
such scenario and mysql_real_query was simply trying to send that query
without reading the pending response, thus messing up the communication
protocol.
The fix is to return an error from mysql_real_query when it's called
prior to retrieval of all pending results.
client/mysqlbinlog.cc:
net_safe_read -> cli_safe_read
include/mysql.h:
Remove a private function from the public header.
include/mysql_com.h:
Remove a define that is never used.
include/sql_common.h:
Add a declaration for cli_safe_read - a function that reads one packet
from the server.
libmysql/libmysql.c:
net_safe_read -> cli_safe_read
Return CR_COMMANDS_OUT_OF_SYNC on attempt to execute a statement
using a connection which has pending result sets.
sql-common/client.c:
Actual fix for Bug#15752: if the server has pending result sets for
the client, return CR_COMMANDS_OUT_OF_SYNC on attempt to execute
another query. Similarly to the behaviour of mysql_use_result(),
multiple result sets block the connection and must be fetched
before it can be used for another query.
This uncovered an error in the protocol: the server doesn't drop
SERVER_MORE_RESULTS_EXISTS status flag upon an error, so in case of
a multi-query like SELECT 1; SELECT syntax_error; SELECT 2;
the client has no way to know that the server won't ever come to
execution of the third query and won't return any result sets for it.
For now, fix it in cli_safe_read, as a proper fix requires extension
of the client-server protocol.
sql/protocol.cc:
Remove a name that is never used.
sql/slave.cc:
net_safe_read -> cli_safe_read
tests/mysql_client_test.c:
Make 'query' a local variable to avoid name clash.
Add a test case for Bug#15752 "Lost connection to MySQL server when
calling an SP from C API"
2006-07-24 12:56:53 +02:00
|
|
|
DBUG_ENTER("cli_advanced_command");
|
2003-05-02 18:07:41 +02:00
|
|
|
|
|
|
|
/* Don't give sigpipe errors if the client doesn't want them */
|
|
|
|
set_sigpipe(mysql);
|
|
|
|
|
|
|
|
if (mysql->net.vio == 0)
|
|
|
|
{ /* Do reconnect if possible */
|
|
|
|
if (mysql_reconnect(mysql))
|
2005-05-16 12:34:23 +02:00
|
|
|
DBUG_RETURN(1);
|
2003-05-02 18:07:41 +02:00
|
|
|
}
|
A fix and a test case for Bug#15752 "Lost connection to MySQL server
when calling a SP from C API"
The bug was caused by lack of checks for misuse in mysql_real_query.
A stored procedure always returns at least one result, which is the
status of execution of the procedure itself.
This result, or so-called OK packet, is similar to a result
returned by INSERT/UPDATE/CREATE operations: it contains the overall
status of execution, the number of affected rows and the number of
warnings. The client test program attached to the bug did not read this
result and ivnoked the next query. In turn, libmysql had no check for
such scenario and mysql_real_query was simply trying to send that query
without reading the pending response, thus messing up the communication
protocol.
The fix is to return an error from mysql_real_query when it's called
prior to retrieval of all pending results.
client/mysqlbinlog.cc:
net_safe_read -> cli_safe_read
include/mysql.h:
Remove a private function from the public header.
include/mysql_com.h:
Remove a define that is never used.
include/sql_common.h:
Add a declaration for cli_safe_read - a function that reads one packet
from the server.
libmysql/libmysql.c:
net_safe_read -> cli_safe_read
Return CR_COMMANDS_OUT_OF_SYNC on attempt to execute a statement
using a connection which has pending result sets.
sql-common/client.c:
Actual fix for Bug#15752: if the server has pending result sets for
the client, return CR_COMMANDS_OUT_OF_SYNC on attempt to execute
another query. Similarly to the behaviour of mysql_use_result(),
multiple result sets block the connection and must be fetched
before it can be used for another query.
This uncovered an error in the protocol: the server doesn't drop
SERVER_MORE_RESULTS_EXISTS status flag upon an error, so in case of
a multi-query like SELECT 1; SELECT syntax_error; SELECT 2;
the client has no way to know that the server won't ever come to
execution of the third query and won't return any result sets for it.
For now, fix it in cli_safe_read, as a proper fix requires extension
of the client-server protocol.
sql/protocol.cc:
Remove a name that is never used.
sql/slave.cc:
net_safe_read -> cli_safe_read
tests/mysql_client_test.c:
Make 'query' a local variable to avoid name clash.
Add a test case for Bug#15752 "Lost connection to MySQL server when
calling an SP from C API"
2006-07-24 12:56:53 +02:00
|
|
|
if (mysql->status != MYSQL_STATUS_READY ||
|
|
|
|
mysql->server_status & SERVER_MORE_RESULTS_EXISTS)
|
2003-05-02 18:07:41 +02:00
|
|
|
{
|
2004-11-03 11:39:38 +01:00
|
|
|
DBUG_PRINT("error",("state: %d", mysql->status));
|
2004-03-12 13:21:48 +01:00
|
|
|
set_mysql_error(mysql, CR_COMMANDS_OUT_OF_SYNC, unknown_sqlstate);
|
2005-05-16 12:34:23 +02:00
|
|
|
DBUG_RETURN(1);
|
2003-05-02 18:07:41 +02:00
|
|
|
}
|
|
|
|
|
2003-05-31 12:15:46 +02:00
|
|
|
net->last_error[0]=0;
|
2003-06-05 22:19:56 +02:00
|
|
|
net->last_errno= 0;
|
|
|
|
strmov(net->sqlstate, not_error_sqlstate);
|
2003-05-02 18:07:41 +02:00
|
|
|
mysql->net.report_error=0;
|
|
|
|
mysql->info=0;
|
|
|
|
mysql->affected_rows= ~(my_ulonglong) 0;
|
|
|
|
net_clear(&mysql->net); /* Clear receive buffer */
|
|
|
|
|
|
|
|
if (net_write_command(net,(uchar) command, header, header_length,
|
|
|
|
arg, arg_length))
|
|
|
|
{
|
|
|
|
DBUG_PRINT("error",("Can't send command to server. Error: %d",
|
|
|
|
socket_errno));
|
|
|
|
if (net->last_errno == ER_NET_PACKET_TOO_LARGE)
|
|
|
|
{
|
|
|
|
net->last_errno=CR_NET_PACKET_TOO_LARGE;
|
|
|
|
strmov(net->last_error,ER(net->last_errno));
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
end_server(mysql);
|
|
|
|
if (mysql_reconnect(mysql))
|
|
|
|
goto end;
|
|
|
|
if (net_write_command(net,(uchar) command, header, header_length,
|
|
|
|
arg, arg_length))
|
|
|
|
{
|
|
|
|
net->last_errno=CR_SERVER_GONE_ERROR;
|
|
|
|
strmov(net->last_error,ER(net->last_errno));
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result=0;
|
|
|
|
if (!skip_check)
|
A fix and a test case for Bug#15752 "Lost connection to MySQL server
when calling a SP from C API"
The bug was caused by lack of checks for misuse in mysql_real_query.
A stored procedure always returns at least one result, which is the
status of execution of the procedure itself.
This result, or so-called OK packet, is similar to a result
returned by INSERT/UPDATE/CREATE operations: it contains the overall
status of execution, the number of affected rows and the number of
warnings. The client test program attached to the bug did not read this
result and ivnoked the next query. In turn, libmysql had no check for
such scenario and mysql_real_query was simply trying to send that query
without reading the pending response, thus messing up the communication
protocol.
The fix is to return an error from mysql_real_query when it's called
prior to retrieval of all pending results.
client/mysqlbinlog.cc:
net_safe_read -> cli_safe_read
include/mysql.h:
Remove a private function from the public header.
include/mysql_com.h:
Remove a define that is never used.
include/sql_common.h:
Add a declaration for cli_safe_read - a function that reads one packet
from the server.
libmysql/libmysql.c:
net_safe_read -> cli_safe_read
Return CR_COMMANDS_OUT_OF_SYNC on attempt to execute a statement
using a connection which has pending result sets.
sql-common/client.c:
Actual fix for Bug#15752: if the server has pending result sets for
the client, return CR_COMMANDS_OUT_OF_SYNC on attempt to execute
another query. Similarly to the behaviour of mysql_use_result(),
multiple result sets block the connection and must be fetched
before it can be used for another query.
This uncovered an error in the protocol: the server doesn't drop
SERVER_MORE_RESULTS_EXISTS status flag upon an error, so in case of
a multi-query like SELECT 1; SELECT syntax_error; SELECT 2;
the client has no way to know that the server won't ever come to
execution of the third query and won't return any result sets for it.
For now, fix it in cli_safe_read, as a proper fix requires extension
of the client-server protocol.
sql/protocol.cc:
Remove a name that is never used.
sql/slave.cc:
net_safe_read -> cli_safe_read
tests/mysql_client_test.c:
Make 'query' a local variable to avoid name clash.
Add a test case for Bug#15752 "Lost connection to MySQL server when
calling an SP from C API"
2006-07-24 12:56:53 +02:00
|
|
|
result= ((mysql->packet_length=cli_safe_read(mysql)) == packet_error ?
|
2003-05-02 18:07:41 +02:00
|
|
|
1 : 0);
|
2003-06-05 22:19:56 +02:00
|
|
|
end:
|
2003-05-02 18:07:41 +02:00
|
|
|
reset_sigpipe(mysql);
|
2005-05-16 12:34:23 +02:00
|
|
|
DBUG_PRINT("exit",("result: %d", result));
|
|
|
|
DBUG_RETURN(result);
|
2003-05-02 18:07:41 +02:00
|
|
|
}
|
|
|
|
|
2003-05-31 12:15:46 +02:00
|
|
|
void free_old_query(MYSQL *mysql)
|
2003-05-02 18:07:41 +02:00
|
|
|
{
|
|
|
|
DBUG_ENTER("free_old_query");
|
|
|
|
if (mysql->fields)
|
|
|
|
free_root(&mysql->field_alloc,MYF(0));
|
2003-06-14 10:37:42 +02:00
|
|
|
init_alloc_root(&mysql->field_alloc,8192,0); /* Assume rowlength < 8192 */
|
2005-08-11 03:32:17 +02:00
|
|
|
mysql->fields= 0;
|
|
|
|
mysql->field_count= 0; /* For API */
|
2005-12-07 00:50:03 +01:00
|
|
|
mysql->warning_count= 0;
|
2005-08-11 03:32:17 +02:00
|
|
|
mysql->info= 0;
|
2003-05-02 18:07:41 +02:00
|
|
|
DBUG_VOID_RETURN;
|
|
|
|
}
|
|
|
|
|
2004-03-12 13:21:48 +01:00
|
|
|
/*
|
|
|
|
Set the internal error message to mysql handler
|
|
|
|
*/
|
|
|
|
|
|
|
|
void set_mysql_error(MYSQL *mysql, int errcode, const char *sqlstate)
|
|
|
|
{
|
|
|
|
NET *net;
|
|
|
|
DBUG_ENTER("set_mysql_error");
|
|
|
|
DBUG_PRINT("enter", ("error :%d '%s'", errcode, ER(errcode)));
|
|
|
|
DBUG_ASSERT(mysql != 0);
|
|
|
|
|
|
|
|
net= &mysql->net;
|
|
|
|
net->last_errno= errcode;
|
|
|
|
strmov(net->last_error, ER(errcode));
|
|
|
|
strmov(net->sqlstate, sqlstate);
|
Intermediate commit of client library (cleanups + fixes of 3 items from
flaws list)
TODO:
* verify that no sequence of API calls produces SIGSEGV.
That is, verify that mysql_stmt_init -> mysql_stmt_fetch is OK,
or mysql_stmt_prepare -> mysql_stmt_fetch_column is OK and sets
meaningful error.
* remove alloc_stmt_fields call
* revise stmt->state codes and statement states.
* there are other items in prepared statements 'to fix' document.
Done:
- cleanups and comments
- revision of prepared statement error codes.
- mysql_stmt_prepare is now can always be called (that is, you can reprepare
a statement)
- new implementation of mysql_stmt_close and fetch cancellation
include/errmsg.h:
- CR_NOT_ALL_PARAMS_BOUND - this error code wasn't used until now.
Apparently it was added in advance, but then interface of
mysql_stmt_bind_param changed. Now it's not possible to bind only some
parameters - either all or none of parameters are bound.
This error code is renamed to CR_PARAMS_NOT_BOUND
- CR_FETCH_CANCELLED - error code set on server side when fetch from
MYSQL_RES or MYSQL_STMT (in blocking mode) was cancelled because of
intercepting call to mysql_stmt_close
- CR_NO_DATA - this is proposed error code to return from
mysql_stmt_fetch_column if no row was fetched (by any type of fetch).
We always can fall back to CR_COMMANDS_OUT_OF_SYNC though.
Need reviewer's opinion on this one.
include/mysql.h:
- added unbuffered_fetch_owner member to MYSQL to point to MYSQL_RES
or MYSQL_STMT which is used to fetch result at the moment.
This is to be able to set CR_FETCH_CANCELLED error without fantoms.
- added unbuffered_fetch_cancelled boolean variable to MYSQL_STMT and
MYSQL_RES structures
- rename PREP_STMT_STATE -> enum enum_mysql_stmt_state
- members of MYSQL_STMT ordered by size.
- removed members of MYSQL_STMT: current_row, result_buffered,
last_fetched_column, last_fetched_buffer, query
- renamed members of MYSQL_STMT: param_buffers -> bind_param_done,
res_buffers -> bind_result_done
- now mysql_stmt_fetch calls stmt->read_row_func to read row either from
buffer or from network.
include/sql_common.h:
declaration for flush_use_result
libmysql/client_settings.h:
stmt_close declaration removed
libmysql/errmsg.c:
Error messages for changed and added error codes.
libmysql/libmysql.c:
Many changes:
- some unused variables removed
- cleanups
- better error reporting
- some function calls commented
- alloc_stmt_fields is now called right after execute, to not read
mysql->fields of some other statement
- new implementation of mysql_stmt_fetch - this is also with cursor
fetch in mind (to implement cursor fetch I'll just need to write
special read_row function for it, so this change will be local)
- implementation of fetch cancellation, including complete rewrite of
mysql_stmt_close
- now mysql_stmt_free_result doesn't free results of other statements.
sql-common/client.c:
- implementation of flush_use_result
- implementation of fetch cancellation
- changed behaviour of mysql_close in regard to mysql_stmt_close - now
mysql_close just set stmt->mysql to 0
2004-03-15 23:04:04 +01:00
|
|
|
|
|
|
|
DBUG_VOID_RETURN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Flush result set sent from server
|
|
|
|
*/
|
|
|
|
|
2004-07-22 17:54:25 +02:00
|
|
|
static void cli_flush_use_result(MYSQL *mysql)
|
Intermediate commit of client library (cleanups + fixes of 3 items from
flaws list)
TODO:
* verify that no sequence of API calls produces SIGSEGV.
That is, verify that mysql_stmt_init -> mysql_stmt_fetch is OK,
or mysql_stmt_prepare -> mysql_stmt_fetch_column is OK and sets
meaningful error.
* remove alloc_stmt_fields call
* revise stmt->state codes and statement states.
* there are other items in prepared statements 'to fix' document.
Done:
- cleanups and comments
- revision of prepared statement error codes.
- mysql_stmt_prepare is now can always be called (that is, you can reprepare
a statement)
- new implementation of mysql_stmt_close and fetch cancellation
include/errmsg.h:
- CR_NOT_ALL_PARAMS_BOUND - this error code wasn't used until now.
Apparently it was added in advance, but then interface of
mysql_stmt_bind_param changed. Now it's not possible to bind only some
parameters - either all or none of parameters are bound.
This error code is renamed to CR_PARAMS_NOT_BOUND
- CR_FETCH_CANCELLED - error code set on server side when fetch from
MYSQL_RES or MYSQL_STMT (in blocking mode) was cancelled because of
intercepting call to mysql_stmt_close
- CR_NO_DATA - this is proposed error code to return from
mysql_stmt_fetch_column if no row was fetched (by any type of fetch).
We always can fall back to CR_COMMANDS_OUT_OF_SYNC though.
Need reviewer's opinion on this one.
include/mysql.h:
- added unbuffered_fetch_owner member to MYSQL to point to MYSQL_RES
or MYSQL_STMT which is used to fetch result at the moment.
This is to be able to set CR_FETCH_CANCELLED error without fantoms.
- added unbuffered_fetch_cancelled boolean variable to MYSQL_STMT and
MYSQL_RES structures
- rename PREP_STMT_STATE -> enum enum_mysql_stmt_state
- members of MYSQL_STMT ordered by size.
- removed members of MYSQL_STMT: current_row, result_buffered,
last_fetched_column, last_fetched_buffer, query
- renamed members of MYSQL_STMT: param_buffers -> bind_param_done,
res_buffers -> bind_result_done
- now mysql_stmt_fetch calls stmt->read_row_func to read row either from
buffer or from network.
include/sql_common.h:
declaration for flush_use_result
libmysql/client_settings.h:
stmt_close declaration removed
libmysql/errmsg.c:
Error messages for changed and added error codes.
libmysql/libmysql.c:
Many changes:
- some unused variables removed
- cleanups
- better error reporting
- some function calls commented
- alloc_stmt_fields is now called right after execute, to not read
mysql->fields of some other statement
- new implementation of mysql_stmt_fetch - this is also with cursor
fetch in mind (to implement cursor fetch I'll just need to write
special read_row function for it, so this change will be local)
- implementation of fetch cancellation, including complete rewrite of
mysql_stmt_close
- now mysql_stmt_free_result doesn't free results of other statements.
sql-common/client.c:
- implementation of flush_use_result
- implementation of fetch cancellation
- changed behaviour of mysql_close in regard to mysql_stmt_close - now
mysql_close just set stmt->mysql to 0
2004-03-15 23:04:04 +01:00
|
|
|
{
|
|
|
|
/* Clear the current execution status */
|
2004-10-26 18:30:01 +02:00
|
|
|
DBUG_ENTER("cli_flush_use_result");
|
Intermediate commit of client library (cleanups + fixes of 3 items from
flaws list)
TODO:
* verify that no sequence of API calls produces SIGSEGV.
That is, verify that mysql_stmt_init -> mysql_stmt_fetch is OK,
or mysql_stmt_prepare -> mysql_stmt_fetch_column is OK and sets
meaningful error.
* remove alloc_stmt_fields call
* revise stmt->state codes and statement states.
* there are other items in prepared statements 'to fix' document.
Done:
- cleanups and comments
- revision of prepared statement error codes.
- mysql_stmt_prepare is now can always be called (that is, you can reprepare
a statement)
- new implementation of mysql_stmt_close and fetch cancellation
include/errmsg.h:
- CR_NOT_ALL_PARAMS_BOUND - this error code wasn't used until now.
Apparently it was added in advance, but then interface of
mysql_stmt_bind_param changed. Now it's not possible to bind only some
parameters - either all or none of parameters are bound.
This error code is renamed to CR_PARAMS_NOT_BOUND
- CR_FETCH_CANCELLED - error code set on server side when fetch from
MYSQL_RES or MYSQL_STMT (in blocking mode) was cancelled because of
intercepting call to mysql_stmt_close
- CR_NO_DATA - this is proposed error code to return from
mysql_stmt_fetch_column if no row was fetched (by any type of fetch).
We always can fall back to CR_COMMANDS_OUT_OF_SYNC though.
Need reviewer's opinion on this one.
include/mysql.h:
- added unbuffered_fetch_owner member to MYSQL to point to MYSQL_RES
or MYSQL_STMT which is used to fetch result at the moment.
This is to be able to set CR_FETCH_CANCELLED error without fantoms.
- added unbuffered_fetch_cancelled boolean variable to MYSQL_STMT and
MYSQL_RES structures
- rename PREP_STMT_STATE -> enum enum_mysql_stmt_state
- members of MYSQL_STMT ordered by size.
- removed members of MYSQL_STMT: current_row, result_buffered,
last_fetched_column, last_fetched_buffer, query
- renamed members of MYSQL_STMT: param_buffers -> bind_param_done,
res_buffers -> bind_result_done
- now mysql_stmt_fetch calls stmt->read_row_func to read row either from
buffer or from network.
include/sql_common.h:
declaration for flush_use_result
libmysql/client_settings.h:
stmt_close declaration removed
libmysql/errmsg.c:
Error messages for changed and added error codes.
libmysql/libmysql.c:
Many changes:
- some unused variables removed
- cleanups
- better error reporting
- some function calls commented
- alloc_stmt_fields is now called right after execute, to not read
mysql->fields of some other statement
- new implementation of mysql_stmt_fetch - this is also with cursor
fetch in mind (to implement cursor fetch I'll just need to write
special read_row function for it, so this change will be local)
- implementation of fetch cancellation, including complete rewrite of
mysql_stmt_close
- now mysql_stmt_free_result doesn't free results of other statements.
sql-common/client.c:
- implementation of flush_use_result
- implementation of fetch cancellation
- changed behaviour of mysql_close in regard to mysql_stmt_close - now
mysql_close just set stmt->mysql to 0
2004-03-15 23:04:04 +01:00
|
|
|
DBUG_PRINT("warning",("Not all packets read, clearing them"));
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
ulong pkt_len;
|
A fix and a test case for Bug#15752 "Lost connection to MySQL server
when calling a SP from C API"
The bug was caused by lack of checks for misuse in mysql_real_query.
A stored procedure always returns at least one result, which is the
status of execution of the procedure itself.
This result, or so-called OK packet, is similar to a result
returned by INSERT/UPDATE/CREATE operations: it contains the overall
status of execution, the number of affected rows and the number of
warnings. The client test program attached to the bug did not read this
result and ivnoked the next query. In turn, libmysql had no check for
such scenario and mysql_real_query was simply trying to send that query
without reading the pending response, thus messing up the communication
protocol.
The fix is to return an error from mysql_real_query when it's called
prior to retrieval of all pending results.
client/mysqlbinlog.cc:
net_safe_read -> cli_safe_read
include/mysql.h:
Remove a private function from the public header.
include/mysql_com.h:
Remove a define that is never used.
include/sql_common.h:
Add a declaration for cli_safe_read - a function that reads one packet
from the server.
libmysql/libmysql.c:
net_safe_read -> cli_safe_read
Return CR_COMMANDS_OUT_OF_SYNC on attempt to execute a statement
using a connection which has pending result sets.
sql-common/client.c:
Actual fix for Bug#15752: if the server has pending result sets for
the client, return CR_COMMANDS_OUT_OF_SYNC on attempt to execute
another query. Similarly to the behaviour of mysql_use_result(),
multiple result sets block the connection and must be fetched
before it can be used for another query.
This uncovered an error in the protocol: the server doesn't drop
SERVER_MORE_RESULTS_EXISTS status flag upon an error, so in case of
a multi-query like SELECT 1; SELECT syntax_error; SELECT 2;
the client has no way to know that the server won't ever come to
execution of the third query and won't return any result sets for it.
For now, fix it in cli_safe_read, as a proper fix requires extension
of the client-server protocol.
sql/protocol.cc:
Remove a name that is never used.
sql/slave.cc:
net_safe_read -> cli_safe_read
tests/mysql_client_test.c:
Make 'query' a local variable to avoid name clash.
Add a test case for Bug#15752 "Lost connection to MySQL server when
calling an SP from C API"
2006-07-24 12:56:53 +02:00
|
|
|
if ((pkt_len=cli_safe_read(mysql)) == packet_error)
|
Intermediate commit of client library (cleanups + fixes of 3 items from
flaws list)
TODO:
* verify that no sequence of API calls produces SIGSEGV.
That is, verify that mysql_stmt_init -> mysql_stmt_fetch is OK,
or mysql_stmt_prepare -> mysql_stmt_fetch_column is OK and sets
meaningful error.
* remove alloc_stmt_fields call
* revise stmt->state codes and statement states.
* there are other items in prepared statements 'to fix' document.
Done:
- cleanups and comments
- revision of prepared statement error codes.
- mysql_stmt_prepare is now can always be called (that is, you can reprepare
a statement)
- new implementation of mysql_stmt_close and fetch cancellation
include/errmsg.h:
- CR_NOT_ALL_PARAMS_BOUND - this error code wasn't used until now.
Apparently it was added in advance, but then interface of
mysql_stmt_bind_param changed. Now it's not possible to bind only some
parameters - either all or none of parameters are bound.
This error code is renamed to CR_PARAMS_NOT_BOUND
- CR_FETCH_CANCELLED - error code set on server side when fetch from
MYSQL_RES or MYSQL_STMT (in blocking mode) was cancelled because of
intercepting call to mysql_stmt_close
- CR_NO_DATA - this is proposed error code to return from
mysql_stmt_fetch_column if no row was fetched (by any type of fetch).
We always can fall back to CR_COMMANDS_OUT_OF_SYNC though.
Need reviewer's opinion on this one.
include/mysql.h:
- added unbuffered_fetch_owner member to MYSQL to point to MYSQL_RES
or MYSQL_STMT which is used to fetch result at the moment.
This is to be able to set CR_FETCH_CANCELLED error without fantoms.
- added unbuffered_fetch_cancelled boolean variable to MYSQL_STMT and
MYSQL_RES structures
- rename PREP_STMT_STATE -> enum enum_mysql_stmt_state
- members of MYSQL_STMT ordered by size.
- removed members of MYSQL_STMT: current_row, result_buffered,
last_fetched_column, last_fetched_buffer, query
- renamed members of MYSQL_STMT: param_buffers -> bind_param_done,
res_buffers -> bind_result_done
- now mysql_stmt_fetch calls stmt->read_row_func to read row either from
buffer or from network.
include/sql_common.h:
declaration for flush_use_result
libmysql/client_settings.h:
stmt_close declaration removed
libmysql/errmsg.c:
Error messages for changed and added error codes.
libmysql/libmysql.c:
Many changes:
- some unused variables removed
- cleanups
- better error reporting
- some function calls commented
- alloc_stmt_fields is now called right after execute, to not read
mysql->fields of some other statement
- new implementation of mysql_stmt_fetch - this is also with cursor
fetch in mind (to implement cursor fetch I'll just need to write
special read_row function for it, so this change will be local)
- implementation of fetch cancellation, including complete rewrite of
mysql_stmt_close
- now mysql_stmt_free_result doesn't free results of other statements.
sql-common/client.c:
- implementation of flush_use_result
- implementation of fetch cancellation
- changed behaviour of mysql_close in regard to mysql_stmt_close - now
mysql_close just set stmt->mysql to 0
2004-03-15 23:04:04 +01:00
|
|
|
break;
|
|
|
|
if (pkt_len <= 8 && mysql->net.read_pos[0] == 254)
|
|
|
|
{
|
|
|
|
if (protocol_41(mysql))
|
|
|
|
{
|
2005-02-17 20:45:13 +01:00
|
|
|
char *pos= (char*) mysql->net.read_pos + 1;
|
Intermediate commit of client library (cleanups + fixes of 3 items from
flaws list)
TODO:
* verify that no sequence of API calls produces SIGSEGV.
That is, verify that mysql_stmt_init -> mysql_stmt_fetch is OK,
or mysql_stmt_prepare -> mysql_stmt_fetch_column is OK and sets
meaningful error.
* remove alloc_stmt_fields call
* revise stmt->state codes and statement states.
* there are other items in prepared statements 'to fix' document.
Done:
- cleanups and comments
- revision of prepared statement error codes.
- mysql_stmt_prepare is now can always be called (that is, you can reprepare
a statement)
- new implementation of mysql_stmt_close and fetch cancellation
include/errmsg.h:
- CR_NOT_ALL_PARAMS_BOUND - this error code wasn't used until now.
Apparently it was added in advance, but then interface of
mysql_stmt_bind_param changed. Now it's not possible to bind only some
parameters - either all or none of parameters are bound.
This error code is renamed to CR_PARAMS_NOT_BOUND
- CR_FETCH_CANCELLED - error code set on server side when fetch from
MYSQL_RES or MYSQL_STMT (in blocking mode) was cancelled because of
intercepting call to mysql_stmt_close
- CR_NO_DATA - this is proposed error code to return from
mysql_stmt_fetch_column if no row was fetched (by any type of fetch).
We always can fall back to CR_COMMANDS_OUT_OF_SYNC though.
Need reviewer's opinion on this one.
include/mysql.h:
- added unbuffered_fetch_owner member to MYSQL to point to MYSQL_RES
or MYSQL_STMT which is used to fetch result at the moment.
This is to be able to set CR_FETCH_CANCELLED error without fantoms.
- added unbuffered_fetch_cancelled boolean variable to MYSQL_STMT and
MYSQL_RES structures
- rename PREP_STMT_STATE -> enum enum_mysql_stmt_state
- members of MYSQL_STMT ordered by size.
- removed members of MYSQL_STMT: current_row, result_buffered,
last_fetched_column, last_fetched_buffer, query
- renamed members of MYSQL_STMT: param_buffers -> bind_param_done,
res_buffers -> bind_result_done
- now mysql_stmt_fetch calls stmt->read_row_func to read row either from
buffer or from network.
include/sql_common.h:
declaration for flush_use_result
libmysql/client_settings.h:
stmt_close declaration removed
libmysql/errmsg.c:
Error messages for changed and added error codes.
libmysql/libmysql.c:
Many changes:
- some unused variables removed
- cleanups
- better error reporting
- some function calls commented
- alloc_stmt_fields is now called right after execute, to not read
mysql->fields of some other statement
- new implementation of mysql_stmt_fetch - this is also with cursor
fetch in mind (to implement cursor fetch I'll just need to write
special read_row function for it, so this change will be local)
- implementation of fetch cancellation, including complete rewrite of
mysql_stmt_close
- now mysql_stmt_free_result doesn't free results of other statements.
sql-common/client.c:
- implementation of flush_use_result
- implementation of fetch cancellation
- changed behaviour of mysql_close in regard to mysql_stmt_close - now
mysql_close just set stmt->mysql to 0
2004-03-15 23:04:04 +01:00
|
|
|
mysql->warning_count=uint2korr(pos); pos+=2;
|
|
|
|
mysql->server_status=uint2korr(pos); pos+=2;
|
|
|
|
}
|
|
|
|
break; /* End of data */
|
|
|
|
}
|
|
|
|
}
|
2004-10-26 18:30:01 +02:00
|
|
|
DBUG_VOID_RETURN;
|
2004-03-12 13:21:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-05-02 18:07:41 +02:00
|
|
|
#ifdef __WIN__
|
|
|
|
static my_bool is_NT(void)
|
|
|
|
{
|
|
|
|
char *os=getenv("OS");
|
|
|
|
return (os && !strcmp(os, "Windows_NT")) ? 1 : 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-06-03 00:55:47 +02:00
|
|
|
|
|
|
|
#ifdef CHECK_LICENSE
|
|
|
|
/*
|
|
|
|
Check server side variable 'license'.
|
|
|
|
If the variable does not exist or does not contain 'Commercial',
|
|
|
|
we're talking to non-commercial server from commercial client.
|
|
|
|
SYNOPSIS
|
|
|
|
check_license()
|
|
|
|
RETURN VALUE
|
|
|
|
0 success
|
|
|
|
!0 network error or the server is not commercial.
|
|
|
|
Error code is saved in mysql->net.last_errno.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int check_license(MYSQL *mysql)
|
|
|
|
{
|
|
|
|
MYSQL_ROW row;
|
|
|
|
MYSQL_RES *res;
|
|
|
|
NET *net= &mysql->net;
|
|
|
|
static const char query[]= "SELECT @@license";
|
|
|
|
static const char required_license[]= STRINGIFY_ARG(LICENSE);
|
|
|
|
|
|
|
|
if (mysql_real_query(mysql, query, sizeof(query)-1))
|
|
|
|
{
|
|
|
|
if (net->last_errno == ER_UNKNOWN_SYSTEM_VARIABLE)
|
|
|
|
{
|
|
|
|
net->last_errno= CR_WRONG_LICENSE;
|
2005-01-09 02:19:42 +01:00
|
|
|
my_snprintf(net->last_error, sizeof(net->last_error)-1,
|
|
|
|
ER(net->last_errno), required_license);
|
2004-06-03 00:55:47 +02:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (!(res= mysql_use_result(mysql)))
|
|
|
|
return 1;
|
|
|
|
row= mysql_fetch_row(res);
|
|
|
|
/*
|
|
|
|
If no rows in result set, or column value is NULL (none of these
|
|
|
|
two is ever true for server variables now), or column value
|
|
|
|
mismatch, set wrong license error.
|
|
|
|
*/
|
|
|
|
if (!net->last_errno &&
|
|
|
|
(!row || !row[0] ||
|
|
|
|
strncmp(row[0], required_license, sizeof(required_license))))
|
|
|
|
{
|
|
|
|
net->last_errno= CR_WRONG_LICENSE;
|
2005-01-09 02:19:42 +01:00
|
|
|
my_snprintf(net->last_error, sizeof(net->last_error)-1,
|
|
|
|
ER(net->last_errno), required_license);
|
2004-06-03 00:55:47 +02:00
|
|
|
}
|
|
|
|
mysql_free_result(res);
|
|
|
|
return net->last_errno;
|
|
|
|
}
|
|
|
|
#endif /* CHECK_LICENSE */
|
|
|
|
|
|
|
|
|
2003-05-02 18:07:41 +02:00
|
|
|
/**************************************************************************
|
|
|
|
Shut down connection
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
void end_server(MYSQL *mysql)
|
|
|
|
{
|
|
|
|
DBUG_ENTER("end_server");
|
|
|
|
if (mysql->net.vio != 0)
|
|
|
|
{
|
2003-06-16 00:13:22 +02:00
|
|
|
init_sigpipe_variables
|
2003-05-02 18:07:41 +02:00
|
|
|
DBUG_PRINT("info",("Net: %s", vio_description(mysql->net.vio)));
|
|
|
|
set_sigpipe(mysql);
|
|
|
|
vio_delete(mysql->net.vio);
|
|
|
|
reset_sigpipe(mysql);
|
|
|
|
mysql->net.vio= 0; /* Marker */
|
|
|
|
}
|
|
|
|
net_end(&mysql->net);
|
|
|
|
free_old_query(mysql);
|
|
|
|
DBUG_VOID_RETURN;
|
|
|
|
}
|
|
|
|
|
2003-06-14 10:37:42 +02:00
|
|
|
|
2003-05-02 18:07:41 +02:00
|
|
|
void STDCALL
|
|
|
|
mysql_free_result(MYSQL_RES *result)
|
|
|
|
{
|
|
|
|
DBUG_ENTER("mysql_free_result");
|
2006-11-20 21:42:06 +01:00
|
|
|
DBUG_PRINT("enter",("mysql_res: 0x%lx", (long) result));
|
2003-05-02 18:07:41 +02:00
|
|
|
if (result)
|
|
|
|
{
|
Intermediate commit of client library (cleanups + fixes of 3 items from
flaws list)
TODO:
* verify that no sequence of API calls produces SIGSEGV.
That is, verify that mysql_stmt_init -> mysql_stmt_fetch is OK,
or mysql_stmt_prepare -> mysql_stmt_fetch_column is OK and sets
meaningful error.
* remove alloc_stmt_fields call
* revise stmt->state codes and statement states.
* there are other items in prepared statements 'to fix' document.
Done:
- cleanups and comments
- revision of prepared statement error codes.
- mysql_stmt_prepare is now can always be called (that is, you can reprepare
a statement)
- new implementation of mysql_stmt_close and fetch cancellation
include/errmsg.h:
- CR_NOT_ALL_PARAMS_BOUND - this error code wasn't used until now.
Apparently it was added in advance, but then interface of
mysql_stmt_bind_param changed. Now it's not possible to bind only some
parameters - either all or none of parameters are bound.
This error code is renamed to CR_PARAMS_NOT_BOUND
- CR_FETCH_CANCELLED - error code set on server side when fetch from
MYSQL_RES or MYSQL_STMT (in blocking mode) was cancelled because of
intercepting call to mysql_stmt_close
- CR_NO_DATA - this is proposed error code to return from
mysql_stmt_fetch_column if no row was fetched (by any type of fetch).
We always can fall back to CR_COMMANDS_OUT_OF_SYNC though.
Need reviewer's opinion on this one.
include/mysql.h:
- added unbuffered_fetch_owner member to MYSQL to point to MYSQL_RES
or MYSQL_STMT which is used to fetch result at the moment.
This is to be able to set CR_FETCH_CANCELLED error without fantoms.
- added unbuffered_fetch_cancelled boolean variable to MYSQL_STMT and
MYSQL_RES structures
- rename PREP_STMT_STATE -> enum enum_mysql_stmt_state
- members of MYSQL_STMT ordered by size.
- removed members of MYSQL_STMT: current_row, result_buffered,
last_fetched_column, last_fetched_buffer, query
- renamed members of MYSQL_STMT: param_buffers -> bind_param_done,
res_buffers -> bind_result_done
- now mysql_stmt_fetch calls stmt->read_row_func to read row either from
buffer or from network.
include/sql_common.h:
declaration for flush_use_result
libmysql/client_settings.h:
stmt_close declaration removed
libmysql/errmsg.c:
Error messages for changed and added error codes.
libmysql/libmysql.c:
Many changes:
- some unused variables removed
- cleanups
- better error reporting
- some function calls commented
- alloc_stmt_fields is now called right after execute, to not read
mysql->fields of some other statement
- new implementation of mysql_stmt_fetch - this is also with cursor
fetch in mind (to implement cursor fetch I'll just need to write
special read_row function for it, so this change will be local)
- implementation of fetch cancellation, including complete rewrite of
mysql_stmt_close
- now mysql_stmt_free_result doesn't free results of other statements.
sql-common/client.c:
- implementation of flush_use_result
- implementation of fetch cancellation
- changed behaviour of mysql_close in regard to mysql_stmt_close - now
mysql_close just set stmt->mysql to 0
2004-03-15 23:04:04 +01:00
|
|
|
MYSQL *mysql= result->handle;
|
|
|
|
if (mysql)
|
2003-05-02 18:07:41 +02:00
|
|
|
{
|
Intermediate commit of client library (cleanups + fixes of 3 items from
flaws list)
TODO:
* verify that no sequence of API calls produces SIGSEGV.
That is, verify that mysql_stmt_init -> mysql_stmt_fetch is OK,
or mysql_stmt_prepare -> mysql_stmt_fetch_column is OK and sets
meaningful error.
* remove alloc_stmt_fields call
* revise stmt->state codes and statement states.
* there are other items in prepared statements 'to fix' document.
Done:
- cleanups and comments
- revision of prepared statement error codes.
- mysql_stmt_prepare is now can always be called (that is, you can reprepare
a statement)
- new implementation of mysql_stmt_close and fetch cancellation
include/errmsg.h:
- CR_NOT_ALL_PARAMS_BOUND - this error code wasn't used until now.
Apparently it was added in advance, but then interface of
mysql_stmt_bind_param changed. Now it's not possible to bind only some
parameters - either all or none of parameters are bound.
This error code is renamed to CR_PARAMS_NOT_BOUND
- CR_FETCH_CANCELLED - error code set on server side when fetch from
MYSQL_RES or MYSQL_STMT (in blocking mode) was cancelled because of
intercepting call to mysql_stmt_close
- CR_NO_DATA - this is proposed error code to return from
mysql_stmt_fetch_column if no row was fetched (by any type of fetch).
We always can fall back to CR_COMMANDS_OUT_OF_SYNC though.
Need reviewer's opinion on this one.
include/mysql.h:
- added unbuffered_fetch_owner member to MYSQL to point to MYSQL_RES
or MYSQL_STMT which is used to fetch result at the moment.
This is to be able to set CR_FETCH_CANCELLED error without fantoms.
- added unbuffered_fetch_cancelled boolean variable to MYSQL_STMT and
MYSQL_RES structures
- rename PREP_STMT_STATE -> enum enum_mysql_stmt_state
- members of MYSQL_STMT ordered by size.
- removed members of MYSQL_STMT: current_row, result_buffered,
last_fetched_column, last_fetched_buffer, query
- renamed members of MYSQL_STMT: param_buffers -> bind_param_done,
res_buffers -> bind_result_done
- now mysql_stmt_fetch calls stmt->read_row_func to read row either from
buffer or from network.
include/sql_common.h:
declaration for flush_use_result
libmysql/client_settings.h:
stmt_close declaration removed
libmysql/errmsg.c:
Error messages for changed and added error codes.
libmysql/libmysql.c:
Many changes:
- some unused variables removed
- cleanups
- better error reporting
- some function calls commented
- alloc_stmt_fields is now called right after execute, to not read
mysql->fields of some other statement
- new implementation of mysql_stmt_fetch - this is also with cursor
fetch in mind (to implement cursor fetch I'll just need to write
special read_row function for it, so this change will be local)
- implementation of fetch cancellation, including complete rewrite of
mysql_stmt_close
- now mysql_stmt_free_result doesn't free results of other statements.
sql-common/client.c:
- implementation of flush_use_result
- implementation of fetch cancellation
- changed behaviour of mysql_close in regard to mysql_stmt_close - now
mysql_close just set stmt->mysql to 0
2004-03-15 23:04:04 +01:00
|
|
|
if (mysql->unbuffered_fetch_owner == &result->unbuffered_fetch_cancelled)
|
|
|
|
mysql->unbuffered_fetch_owner= 0;
|
|
|
|
if (mysql->status == MYSQL_STATUS_USE_RESULT)
|
2003-05-02 18:07:41 +02:00
|
|
|
{
|
2004-07-22 17:54:25 +02:00
|
|
|
(*mysql->methods->flush_use_result)(mysql);
|
Intermediate commit of client library (cleanups + fixes of 3 items from
flaws list)
TODO:
* verify that no sequence of API calls produces SIGSEGV.
That is, verify that mysql_stmt_init -> mysql_stmt_fetch is OK,
or mysql_stmt_prepare -> mysql_stmt_fetch_column is OK and sets
meaningful error.
* remove alloc_stmt_fields call
* revise stmt->state codes and statement states.
* there are other items in prepared statements 'to fix' document.
Done:
- cleanups and comments
- revision of prepared statement error codes.
- mysql_stmt_prepare is now can always be called (that is, you can reprepare
a statement)
- new implementation of mysql_stmt_close and fetch cancellation
include/errmsg.h:
- CR_NOT_ALL_PARAMS_BOUND - this error code wasn't used until now.
Apparently it was added in advance, but then interface of
mysql_stmt_bind_param changed. Now it's not possible to bind only some
parameters - either all or none of parameters are bound.
This error code is renamed to CR_PARAMS_NOT_BOUND
- CR_FETCH_CANCELLED - error code set on server side when fetch from
MYSQL_RES or MYSQL_STMT (in blocking mode) was cancelled because of
intercepting call to mysql_stmt_close
- CR_NO_DATA - this is proposed error code to return from
mysql_stmt_fetch_column if no row was fetched (by any type of fetch).
We always can fall back to CR_COMMANDS_OUT_OF_SYNC though.
Need reviewer's opinion on this one.
include/mysql.h:
- added unbuffered_fetch_owner member to MYSQL to point to MYSQL_RES
or MYSQL_STMT which is used to fetch result at the moment.
This is to be able to set CR_FETCH_CANCELLED error without fantoms.
- added unbuffered_fetch_cancelled boolean variable to MYSQL_STMT and
MYSQL_RES structures
- rename PREP_STMT_STATE -> enum enum_mysql_stmt_state
- members of MYSQL_STMT ordered by size.
- removed members of MYSQL_STMT: current_row, result_buffered,
last_fetched_column, last_fetched_buffer, query
- renamed members of MYSQL_STMT: param_buffers -> bind_param_done,
res_buffers -> bind_result_done
- now mysql_stmt_fetch calls stmt->read_row_func to read row either from
buffer or from network.
include/sql_common.h:
declaration for flush_use_result
libmysql/client_settings.h:
stmt_close declaration removed
libmysql/errmsg.c:
Error messages for changed and added error codes.
libmysql/libmysql.c:
Many changes:
- some unused variables removed
- cleanups
- better error reporting
- some function calls commented
- alloc_stmt_fields is now called right after execute, to not read
mysql->fields of some other statement
- new implementation of mysql_stmt_fetch - this is also with cursor
fetch in mind (to implement cursor fetch I'll just need to write
special read_row function for it, so this change will be local)
- implementation of fetch cancellation, including complete rewrite of
mysql_stmt_close
- now mysql_stmt_free_result doesn't free results of other statements.
sql-common/client.c:
- implementation of flush_use_result
- implementation of fetch cancellation
- changed behaviour of mysql_close in regard to mysql_stmt_close - now
mysql_close just set stmt->mysql to 0
2004-03-15 23:04:04 +01:00
|
|
|
mysql->status=MYSQL_STATUS_READY;
|
2005-05-12 09:16:12 +02:00
|
|
|
if (mysql->unbuffered_fetch_owner)
|
|
|
|
*mysql->unbuffered_fetch_owner= TRUE;
|
2003-05-02 18:07:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
free_rows(result->data);
|
|
|
|
if (result->fields)
|
|
|
|
free_root(&result->field_alloc,MYF(0));
|
|
|
|
if (result->row)
|
|
|
|
my_free((gptr) result->row,MYF(0));
|
|
|
|
my_free((gptr) result,MYF(0));
|
|
|
|
}
|
|
|
|
DBUG_VOID_RETURN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
Get options from my.cnf
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static const char *default_options[]=
|
|
|
|
{
|
|
|
|
"port","socket","compress","password","pipe", "timeout", "user",
|
|
|
|
"init-command", "host", "database", "debug", "return-found-rows",
|
|
|
|
"ssl-key" ,"ssl-cert" ,"ssl-ca" ,"ssl-capath",
|
|
|
|
"character-sets-dir", "default-character-set", "interactive-timeout",
|
|
|
|
"connect-timeout", "local-infile", "disable-local-infile",
|
|
|
|
"replication-probe", "enable-reads-from-master", "repl-parse-query",
|
2003-05-31 12:15:46 +02:00
|
|
|
"ssl-cipher", "max-allowed-packet", "protocol", "shared-memory-base-name",
|
2005-01-04 17:32:42 +01:00
|
|
|
"multi-results", "multi-statements", "multi-queries", "secure-auth",
|
2004-12-17 22:17:25 +01:00
|
|
|
"report-data-truncation",
|
2003-05-02 18:07:41 +02:00
|
|
|
NullS
|
|
|
|
};
|
|
|
|
|
|
|
|
static TYPELIB option_types={array_elements(default_options)-1,
|
2004-10-26 18:59:33 +02:00
|
|
|
"options",default_options, NULL};
|
2003-05-02 18:07:41 +02:00
|
|
|
|
2003-06-14 10:37:42 +02:00
|
|
|
const char *sql_protocol_names_lib[] =
|
|
|
|
{ "TCP", "SOCKET", "PIPE", "MEMORY", NullS };
|
|
|
|
TYPELIB sql_protocol_typelib = {array_elements(sql_protocol_names_lib)-1,"",
|
2004-10-26 18:59:33 +02:00
|
|
|
sql_protocol_names_lib, NULL};
|
2003-06-14 10:37:42 +02:00
|
|
|
|
2003-05-02 18:07:41 +02:00
|
|
|
static int add_init_command(struct st_mysql_options *options, const char *cmd)
|
|
|
|
{
|
|
|
|
char *tmp;
|
|
|
|
|
|
|
|
if (!options->init_commands)
|
|
|
|
{
|
|
|
|
options->init_commands= (DYNAMIC_ARRAY*)my_malloc(sizeof(DYNAMIC_ARRAY),
|
|
|
|
MYF(MY_WME));
|
|
|
|
init_dynamic_array(options->init_commands,sizeof(char*),0,5 CALLER_INFO);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(tmp= my_strdup(cmd,MYF(MY_WME))) ||
|
|
|
|
insert_dynamic(options->init_commands, (gptr)&tmp))
|
|
|
|
{
|
|
|
|
my_free(tmp, MYF(MY_ALLOW_ZERO_PTR));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-06-17 18:32:31 +02:00
|
|
|
void mysql_read_default_options(struct st_mysql_options *options,
|
|
|
|
const char *filename,const char *group)
|
2003-05-02 18:07:41 +02:00
|
|
|
{
|
|
|
|
int argc;
|
|
|
|
char *argv_buff[1],**argv;
|
|
|
|
const char *groups[3];
|
|
|
|
DBUG_ENTER("mysql_read_default_options");
|
|
|
|
DBUG_PRINT("enter",("file: %s group: %s",filename,group ? group :"NULL"));
|
|
|
|
|
|
|
|
argc=1; argv=argv_buff; argv_buff[0]= (char*) "client";
|
|
|
|
groups[0]= (char*) "client"; groups[1]= (char*) group; groups[2]=0;
|
|
|
|
|
|
|
|
load_defaults(filename, groups, &argc, &argv);
|
|
|
|
if (argc != 1) /* If some default option */
|
|
|
|
{
|
|
|
|
char **option=argv;
|
|
|
|
while (*++option)
|
|
|
|
{
|
|
|
|
/* DBUG_PRINT("info",("option: %s",option[0])); */
|
|
|
|
if (option[0][0] == '-' && option[0][1] == '-')
|
|
|
|
{
|
|
|
|
char *end=strcend(*option,'=');
|
|
|
|
char *opt_arg=0;
|
|
|
|
if (*end)
|
|
|
|
{
|
|
|
|
opt_arg=end+1;
|
|
|
|
*end=0; /* Remove '=' */
|
|
|
|
}
|
|
|
|
/* Change all '_' in variable name to '-' */
|
|
|
|
for (end= *option ; *(end= strcend(end,'_')) ; )
|
|
|
|
*end= '-';
|
|
|
|
switch (find_type(*option+2,&option_types,2)) {
|
|
|
|
case 1: /* port */
|
|
|
|
if (opt_arg)
|
|
|
|
options->port=atoi(opt_arg);
|
|
|
|
break;
|
|
|
|
case 2: /* socket */
|
|
|
|
if (opt_arg)
|
|
|
|
{
|
|
|
|
my_free(options->unix_socket,MYF(MY_ALLOW_ZERO_PTR));
|
|
|
|
options->unix_socket=my_strdup(opt_arg,MYF(MY_WME));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 3: /* compress */
|
|
|
|
options->compress=1;
|
|
|
|
options->client_flag|= CLIENT_COMPRESS;
|
|
|
|
break;
|
|
|
|
case 4: /* password */
|
|
|
|
if (opt_arg)
|
|
|
|
{
|
|
|
|
my_free(options->password,MYF(MY_ALLOW_ZERO_PTR));
|
|
|
|
options->password=my_strdup(opt_arg,MYF(MY_WME));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
options->protocol = MYSQL_PROTOCOL_PIPE;
|
|
|
|
case 20: /* connect_timeout */
|
|
|
|
case 6: /* timeout */
|
|
|
|
if (opt_arg)
|
|
|
|
options->connect_timeout=atoi(opt_arg);
|
|
|
|
break;
|
|
|
|
case 7: /* user */
|
|
|
|
if (opt_arg)
|
|
|
|
{
|
|
|
|
my_free(options->user,MYF(MY_ALLOW_ZERO_PTR));
|
|
|
|
options->user=my_strdup(opt_arg,MYF(MY_WME));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 8: /* init-command */
|
|
|
|
add_init_command(options,opt_arg);
|
|
|
|
break;
|
|
|
|
case 9: /* host */
|
|
|
|
if (opt_arg)
|
|
|
|
{
|
|
|
|
my_free(options->host,MYF(MY_ALLOW_ZERO_PTR));
|
|
|
|
options->host=my_strdup(opt_arg,MYF(MY_WME));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 10: /* database */
|
|
|
|
if (opt_arg)
|
|
|
|
{
|
|
|
|
my_free(options->db,MYF(MY_ALLOW_ZERO_PTR));
|
|
|
|
options->db=my_strdup(opt_arg,MYF(MY_WME));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 11: /* debug */
|
2003-06-14 10:37:42 +02:00
|
|
|
#ifdef MYSQL_CLIENT
|
2003-05-02 18:07:41 +02:00
|
|
|
mysql_debug(opt_arg ? opt_arg : "d:t:o,/tmp/client.trace");
|
|
|
|
break;
|
2003-05-31 12:15:46 +02:00
|
|
|
#endif
|
2003-05-02 18:07:41 +02:00
|
|
|
case 12: /* return-found-rows */
|
|
|
|
options->client_flag|=CLIENT_FOUND_ROWS;
|
|
|
|
break;
|
|
|
|
#ifdef HAVE_OPENSSL
|
|
|
|
case 13: /* ssl_key */
|
|
|
|
my_free(options->ssl_key, MYF(MY_ALLOW_ZERO_PTR));
|
|
|
|
options->ssl_key = my_strdup(opt_arg, MYF(MY_WME));
|
|
|
|
break;
|
|
|
|
case 14: /* ssl_cert */
|
|
|
|
my_free(options->ssl_cert, MYF(MY_ALLOW_ZERO_PTR));
|
|
|
|
options->ssl_cert = my_strdup(opt_arg, MYF(MY_WME));
|
|
|
|
break;
|
|
|
|
case 15: /* ssl_ca */
|
|
|
|
my_free(options->ssl_ca, MYF(MY_ALLOW_ZERO_PTR));
|
|
|
|
options->ssl_ca = my_strdup(opt_arg, MYF(MY_WME));
|
|
|
|
break;
|
|
|
|
case 16: /* ssl_capath */
|
|
|
|
my_free(options->ssl_capath, MYF(MY_ALLOW_ZERO_PTR));
|
|
|
|
options->ssl_capath = my_strdup(opt_arg, MYF(MY_WME));
|
|
|
|
break;
|
|
|
|
#else
|
|
|
|
case 13: /* Ignore SSL options */
|
|
|
|
case 14:
|
|
|
|
case 15:
|
|
|
|
case 16:
|
|
|
|
break;
|
|
|
|
#endif /* HAVE_OPENSSL */
|
|
|
|
case 17: /* charset-lib */
|
|
|
|
my_free(options->charset_dir,MYF(MY_ALLOW_ZERO_PTR));
|
|
|
|
options->charset_dir = my_strdup(opt_arg, MYF(MY_WME));
|
|
|
|
break;
|
|
|
|
case 18:
|
|
|
|
my_free(options->charset_name,MYF(MY_ALLOW_ZERO_PTR));
|
|
|
|
options->charset_name = my_strdup(opt_arg, MYF(MY_WME));
|
|
|
|
break;
|
|
|
|
case 19: /* Interactive-timeout */
|
|
|
|
options->client_flag|= CLIENT_INTERACTIVE;
|
|
|
|
break;
|
|
|
|
case 21:
|
|
|
|
if (!opt_arg || atoi(opt_arg) != 0)
|
|
|
|
options->client_flag|= CLIENT_LOCAL_FILES;
|
|
|
|
else
|
|
|
|
options->client_flag&= ~CLIENT_LOCAL_FILES;
|
|
|
|
break;
|
|
|
|
case 22:
|
2004-08-19 03:02:09 +02:00
|
|
|
options->client_flag&= ~CLIENT_LOCAL_FILES;
|
2003-05-02 18:07:41 +02:00
|
|
|
break;
|
|
|
|
case 23: /* replication probe */
|
2003-06-14 10:37:42 +02:00
|
|
|
#ifndef TO_BE_DELETED
|
2003-05-02 18:07:41 +02:00
|
|
|
options->rpl_probe= 1;
|
2003-06-14 10:37:42 +02:00
|
|
|
#endif
|
2003-05-02 18:07:41 +02:00
|
|
|
break;
|
|
|
|
case 24: /* enable-reads-from-master */
|
|
|
|
options->no_master_reads= 0;
|
|
|
|
break;
|
|
|
|
case 25: /* repl-parse-query */
|
2003-06-14 10:37:42 +02:00
|
|
|
#ifndef TO_BE_DELETED
|
2003-05-02 18:07:41 +02:00
|
|
|
options->rpl_parse= 1;
|
2003-06-14 10:37:42 +02:00
|
|
|
#endif
|
2003-05-02 18:07:41 +02:00
|
|
|
break;
|
|
|
|
case 27:
|
2003-12-19 15:25:50 +01:00
|
|
|
if (opt_arg)
|
|
|
|
options->max_allowed_packet= atoi(opt_arg);
|
2003-05-02 18:07:41 +02:00
|
|
|
break;
|
|
|
|
case 28: /* protocol */
|
2004-08-24 17:13:31 +02:00
|
|
|
if ((options->protocol= find_type(opt_arg,
|
|
|
|
&sql_protocol_typelib,0)) <= 0)
|
2003-05-02 18:07:41 +02:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Unknown option to protocol: %s\n", opt_arg);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 29: /* shared_memory_base_name */
|
|
|
|
#ifdef HAVE_SMEM
|
|
|
|
if (options->shared_memory_base_name != def_shared_memory_base_name)
|
|
|
|
my_free(options->shared_memory_base_name,MYF(MY_ALLOW_ZERO_PTR));
|
|
|
|
options->shared_memory_base_name=my_strdup(opt_arg,MYF(MY_WME));
|
|
|
|
#endif
|
|
|
|
break;
|
2003-05-31 12:15:46 +02:00
|
|
|
case 30:
|
|
|
|
options->client_flag|= CLIENT_MULTI_RESULTS;
|
|
|
|
break;
|
|
|
|
case 31:
|
2005-01-04 17:32:42 +01:00
|
|
|
case 32:
|
2003-11-18 12:47:27 +01:00
|
|
|
options->client_flag|= CLIENT_MULTI_STATEMENTS | CLIENT_MULTI_RESULTS;
|
2003-05-31 12:15:46 +02:00
|
|
|
break;
|
2005-01-04 17:32:42 +01:00
|
|
|
case 33: /* secure-auth */
|
2003-11-28 11:11:44 +01:00
|
|
|
options->secure_auth= TRUE;
|
|
|
|
break;
|
2005-01-06 18:32:16 +01:00
|
|
|
case 34: /* report-data-truncation */
|
2004-12-17 22:17:25 +01:00
|
|
|
options->report_data_truncation= opt_arg ? test(atoi(opt_arg)) : 1;
|
|
|
|
break;
|
2003-05-02 18:07:41 +02:00
|
|
|
default:
|
|
|
|
DBUG_PRINT("warning",("unknown option: %s",option[0]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free_defaults(argv);
|
|
|
|
DBUG_VOID_RETURN;
|
|
|
|
}
|
|
|
|
|
2003-06-14 10:37:42 +02:00
|
|
|
|
2003-05-02 18:07:41 +02:00
|
|
|
/**************************************************************************
|
|
|
|
Get column lengths of the current row
|
|
|
|
If one uses mysql_use_result, res->lengths contains the length information,
|
|
|
|
else the lengths are calculated from the offset between pointers.
|
|
|
|
**************************************************************************/
|
|
|
|
|
2005-02-23 09:29:57 +01:00
|
|
|
static void cli_fetch_lengths(ulong *to, MYSQL_ROW column,
|
2003-11-20 21:06:25 +01:00
|
|
|
unsigned int field_count)
|
2003-05-02 18:07:41 +02:00
|
|
|
{
|
2003-07-23 12:23:20 +02:00
|
|
|
ulong *prev_length;
|
|
|
|
byte *start=0;
|
2003-05-02 18:07:41 +02:00
|
|
|
MYSQL_ROW end;
|
2003-07-23 12:23:20 +02:00
|
|
|
|
|
|
|
prev_length=0; /* Keep gcc happy */
|
|
|
|
for (end=column + field_count + 1 ; column != end ; column++, to++)
|
|
|
|
{
|
|
|
|
if (!*column)
|
|
|
|
{
|
|
|
|
*to= 0; /* Null */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (start) /* Found end of prev string */
|
|
|
|
*prev_length= (ulong) (*column-start-1);
|
|
|
|
start= *column;
|
|
|
|
prev_length= to;
|
|
|
|
}
|
2003-05-02 18:07:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************************************
|
|
|
|
Change field rows to field structs
|
|
|
|
***************************************************************************/
|
|
|
|
|
2003-05-31 12:15:46 +02:00
|
|
|
MYSQL_FIELD *
|
2003-05-02 18:07:41 +02:00
|
|
|
unpack_fields(MYSQL_DATA *data,MEM_ROOT *alloc,uint fields,
|
|
|
|
my_bool default_value, uint server_capabilities)
|
|
|
|
{
|
|
|
|
MYSQL_ROWS *row;
|
|
|
|
MYSQL_FIELD *field,*result;
|
2003-05-31 12:15:46 +02:00
|
|
|
ulong lengths[9]; /* Max of fields */
|
2003-05-02 18:07:41 +02:00
|
|
|
DBUG_ENTER("unpack_fields");
|
|
|
|
|
2003-06-14 10:37:42 +02:00
|
|
|
field= result= (MYSQL_FIELD*) alloc_root(alloc,
|
|
|
|
(uint) sizeof(*field)*fields);
|
2003-05-02 18:07:41 +02:00
|
|
|
if (!result)
|
|
|
|
{
|
|
|
|
free_rows(data); /* Free old data */
|
|
|
|
DBUG_RETURN(0);
|
|
|
|
}
|
|
|
|
bzero((char*) field, (uint) sizeof(MYSQL_FIELD)*fields);
|
|
|
|
if (server_capabilities & CLIENT_PROTOCOL_41)
|
|
|
|
{
|
|
|
|
/* server is 4.1, and returns the new field result format */
|
|
|
|
for (row=data->data; row ; row = row->next,field++)
|
|
|
|
{
|
|
|
|
uchar *pos;
|
2003-07-23 12:23:20 +02:00
|
|
|
cli_fetch_lengths(&lengths[0], row->data, default_value ? 8 : 7);
|
2003-05-31 12:15:46 +02:00
|
|
|
field->catalog = strdup_root(alloc,(char*) row->data[0]);
|
|
|
|
field->db = strdup_root(alloc,(char*) row->data[1]);
|
|
|
|
field->table = strdup_root(alloc,(char*) row->data[2]);
|
|
|
|
field->org_table= strdup_root(alloc,(char*) row->data[3]);
|
|
|
|
field->name = strdup_root(alloc,(char*) row->data[4]);
|
|
|
|
field->org_name = strdup_root(alloc,(char*) row->data[5]);
|
|
|
|
|
|
|
|
field->catalog_length= lengths[0];
|
|
|
|
field->db_length= lengths[1];
|
|
|
|
field->table_length= lengths[2];
|
|
|
|
field->org_table_length= lengths[3];
|
|
|
|
field->name_length= lengths[4];
|
|
|
|
field->org_name_length= lengths[5];
|
2003-05-02 18:07:41 +02:00
|
|
|
|
|
|
|
/* Unpack fixed length parts */
|
2003-05-31 12:15:46 +02:00
|
|
|
pos= (uchar*) row->data[6];
|
2003-05-02 18:07:41 +02:00
|
|
|
field->charsetnr= uint2korr(pos);
|
2003-05-31 12:15:46 +02:00
|
|
|
field->length= (uint) uint4korr(pos+2);
|
|
|
|
field->type= (enum enum_field_types) pos[6];
|
|
|
|
field->flags= uint2korr(pos+7);
|
|
|
|
field->decimals= (uint) pos[9];
|
2003-05-02 18:07:41 +02:00
|
|
|
|
|
|
|
if (INTERNAL_NUM_FIELD(field))
|
|
|
|
field->flags|= NUM_FLAG;
|
2003-05-31 12:15:46 +02:00
|
|
|
if (default_value && row->data[7])
|
2003-05-02 18:07:41 +02:00
|
|
|
{
|
2003-05-31 12:15:46 +02:00
|
|
|
field->def=strdup_root(alloc,(char*) row->data[7]);
|
|
|
|
field->def_length= lengths[7];
|
2003-05-02 18:07:41 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
field->def=0;
|
|
|
|
field->max_length= 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifndef DELETE_SUPPORT_OF_4_0_PROTOCOL
|
|
|
|
else
|
2003-06-14 10:37:42 +02:00
|
|
|
{
|
|
|
|
/* old protocol, for backward compatibility */
|
|
|
|
for (row=data->data; row ; row = row->next,field++)
|
|
|
|
{
|
2003-07-23 12:23:20 +02:00
|
|
|
cli_fetch_lengths(&lengths[0], row->data, default_value ? 6 : 5);
|
2003-06-14 10:37:42 +02:00
|
|
|
field->org_table= field->table= strdup_root(alloc,(char*) row->data[0]);
|
|
|
|
field->name= strdup_root(alloc,(char*) row->data[1]);
|
|
|
|
field->length= (uint) uint3korr(row->data[2]);
|
|
|
|
field->type= (enum enum_field_types) (uchar) row->data[3][0];
|
|
|
|
|
|
|
|
field->catalog=(char*) "";
|
|
|
|
field->db= (char*) "";
|
|
|
|
field->catalog_length= 0;
|
|
|
|
field->db_length= 0;
|
|
|
|
field->org_table_length= field->table_length= lengths[0];
|
|
|
|
field->name_length= lengths[1];
|
|
|
|
|
|
|
|
if (server_capabilities & CLIENT_LONG_FLAG)
|
|
|
|
{
|
|
|
|
field->flags= uint2korr(row->data[4]);
|
|
|
|
field->decimals=(uint) (uchar) row->data[4][2];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
field->flags= (uint) (uchar) row->data[4][0];
|
|
|
|
field->decimals=(uint) (uchar) row->data[4][1];
|
|
|
|
}
|
|
|
|
if (INTERNAL_NUM_FIELD(field))
|
|
|
|
field->flags|= NUM_FLAG;
|
|
|
|
if (default_value && row->data[5])
|
|
|
|
{
|
|
|
|
field->def=strdup_root(alloc,(char*) row->data[5]);
|
|
|
|
field->def_length= lengths[5];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
field->def=0;
|
|
|
|
field->max_length= 0;
|
|
|
|
}
|
|
|
|
}
|
2003-05-02 18:07:41 +02:00
|
|
|
#endif /* DELETE_SUPPORT_OF_4_0_PROTOCOL */
|
|
|
|
free_rows(data); /* Free old data */
|
|
|
|
DBUG_RETURN(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read all rows (fields or data) from server */
|
|
|
|
|
2003-11-20 21:06:25 +01:00
|
|
|
MYSQL_DATA *cli_read_rows(MYSQL *mysql,MYSQL_FIELD *mysql_fields,
|
|
|
|
unsigned int fields)
|
2003-05-02 18:07:41 +02:00
|
|
|
{
|
|
|
|
uint field;
|
|
|
|
ulong pkt_len;
|
|
|
|
ulong len;
|
|
|
|
uchar *cp;
|
|
|
|
char *to, *end_to;
|
|
|
|
MYSQL_DATA *result;
|
|
|
|
MYSQL_ROWS **prev_ptr,*cur;
|
|
|
|
NET *net = &mysql->net;
|
2003-09-16 13:06:25 +02:00
|
|
|
DBUG_ENTER("cli_read_rows");
|
2003-05-02 18:07:41 +02:00
|
|
|
|
A fix and a test case for Bug#15752 "Lost connection to MySQL server
when calling a SP from C API"
The bug was caused by lack of checks for misuse in mysql_real_query.
A stored procedure always returns at least one result, which is the
status of execution of the procedure itself.
This result, or so-called OK packet, is similar to a result
returned by INSERT/UPDATE/CREATE operations: it contains the overall
status of execution, the number of affected rows and the number of
warnings. The client test program attached to the bug did not read this
result and ivnoked the next query. In turn, libmysql had no check for
such scenario and mysql_real_query was simply trying to send that query
without reading the pending response, thus messing up the communication
protocol.
The fix is to return an error from mysql_real_query when it's called
prior to retrieval of all pending results.
client/mysqlbinlog.cc:
net_safe_read -> cli_safe_read
include/mysql.h:
Remove a private function from the public header.
include/mysql_com.h:
Remove a define that is never used.
include/sql_common.h:
Add a declaration for cli_safe_read - a function that reads one packet
from the server.
libmysql/libmysql.c:
net_safe_read -> cli_safe_read
Return CR_COMMANDS_OUT_OF_SYNC on attempt to execute a statement
using a connection which has pending result sets.
sql-common/client.c:
Actual fix for Bug#15752: if the server has pending result sets for
the client, return CR_COMMANDS_OUT_OF_SYNC on attempt to execute
another query. Similarly to the behaviour of mysql_use_result(),
multiple result sets block the connection and must be fetched
before it can be used for another query.
This uncovered an error in the protocol: the server doesn't drop
SERVER_MORE_RESULTS_EXISTS status flag upon an error, so in case of
a multi-query like SELECT 1; SELECT syntax_error; SELECT 2;
the client has no way to know that the server won't ever come to
execution of the third query and won't return any result sets for it.
For now, fix it in cli_safe_read, as a proper fix requires extension
of the client-server protocol.
sql/protocol.cc:
Remove a name that is never used.
sql/slave.cc:
net_safe_read -> cli_safe_read
tests/mysql_client_test.c:
Make 'query' a local variable to avoid name clash.
Add a test case for Bug#15752 "Lost connection to MySQL server when
calling an SP from C API"
2006-07-24 12:56:53 +02:00
|
|
|
if ((pkt_len= cli_safe_read(mysql)) == packet_error)
|
2003-05-02 18:07:41 +02:00
|
|
|
DBUG_RETURN(0);
|
|
|
|
if (!(result=(MYSQL_DATA*) my_malloc(sizeof(MYSQL_DATA),
|
|
|
|
MYF(MY_WME | MY_ZEROFILL))))
|
|
|
|
{
|
2004-03-12 13:21:48 +01:00
|
|
|
set_mysql_error(mysql, CR_OUT_OF_MEMORY, unknown_sqlstate);
|
2003-05-02 18:07:41 +02:00
|
|
|
DBUG_RETURN(0);
|
|
|
|
}
|
|
|
|
init_alloc_root(&result->alloc,8192,0); /* Assume rowlength < 8192 */
|
|
|
|
result->alloc.min_malloc=sizeof(MYSQL_ROWS);
|
|
|
|
prev_ptr= &result->data;
|
|
|
|
result->rows=0;
|
|
|
|
result->fields=fields;
|
|
|
|
|
|
|
|
/*
|
|
|
|
The last EOF packet is either a single 254 character or (in MySQL 4.1)
|
|
|
|
254 followed by 1-7 status bytes.
|
|
|
|
|
|
|
|
This doesn't conflict with normal usage of 254 which stands for a
|
|
|
|
string where the length of the string is 8 bytes. (see net_field_length())
|
|
|
|
*/
|
|
|
|
|
|
|
|
while (*(cp=net->read_pos) != 254 || pkt_len >= 8)
|
|
|
|
{
|
|
|
|
result->rows++;
|
|
|
|
if (!(cur= (MYSQL_ROWS*) alloc_root(&result->alloc,
|
|
|
|
sizeof(MYSQL_ROWS))) ||
|
|
|
|
!(cur->data= ((MYSQL_ROW)
|
|
|
|
alloc_root(&result->alloc,
|
|
|
|
(fields+1)*sizeof(char *)+pkt_len))))
|
|
|
|
{
|
|
|
|
free_rows(result);
|
2004-03-12 13:21:48 +01:00
|
|
|
set_mysql_error(mysql, CR_OUT_OF_MEMORY, unknown_sqlstate);
|
2003-05-02 18:07:41 +02:00
|
|
|
DBUG_RETURN(0);
|
|
|
|
}
|
|
|
|
*prev_ptr=cur;
|
|
|
|
prev_ptr= &cur->next;
|
|
|
|
to= (char*) (cur->data+fields+1);
|
|
|
|
end_to=to+pkt_len-1;
|
|
|
|
for (field=0 ; field < fields ; field++)
|
|
|
|
{
|
|
|
|
if ((len=(ulong) net_field_length(&cp)) == NULL_LENGTH)
|
|
|
|
{ /* null field */
|
|
|
|
cur->data[field] = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cur->data[field] = to;
|
|
|
|
if (len > (ulong) (end_to - to))
|
|
|
|
{
|
|
|
|
free_rows(result);
|
2004-03-12 13:21:48 +01:00
|
|
|
set_mysql_error(mysql, CR_MALFORMED_PACKET, unknown_sqlstate);
|
2003-05-02 18:07:41 +02:00
|
|
|
DBUG_RETURN(0);
|
|
|
|
}
|
|
|
|
memcpy(to,(char*) cp,len); to[len]=0;
|
|
|
|
to+=len+1;
|
|
|
|
cp+=len;
|
|
|
|
if (mysql_fields)
|
|
|
|
{
|
|
|
|
if (mysql_fields[field].max_length < len)
|
|
|
|
mysql_fields[field].max_length=len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cur->data[field]=to; /* End of last field */
|
A fix and a test case for Bug#15752 "Lost connection to MySQL server
when calling a SP from C API"
The bug was caused by lack of checks for misuse in mysql_real_query.
A stored procedure always returns at least one result, which is the
status of execution of the procedure itself.
This result, or so-called OK packet, is similar to a result
returned by INSERT/UPDATE/CREATE operations: it contains the overall
status of execution, the number of affected rows and the number of
warnings. The client test program attached to the bug did not read this
result and ivnoked the next query. In turn, libmysql had no check for
such scenario and mysql_real_query was simply trying to send that query
without reading the pending response, thus messing up the communication
protocol.
The fix is to return an error from mysql_real_query when it's called
prior to retrieval of all pending results.
client/mysqlbinlog.cc:
net_safe_read -> cli_safe_read
include/mysql.h:
Remove a private function from the public header.
include/mysql_com.h:
Remove a define that is never used.
include/sql_common.h:
Add a declaration for cli_safe_read - a function that reads one packet
from the server.
libmysql/libmysql.c:
net_safe_read -> cli_safe_read
Return CR_COMMANDS_OUT_OF_SYNC on attempt to execute a statement
using a connection which has pending result sets.
sql-common/client.c:
Actual fix for Bug#15752: if the server has pending result sets for
the client, return CR_COMMANDS_OUT_OF_SYNC on attempt to execute
another query. Similarly to the behaviour of mysql_use_result(),
multiple result sets block the connection and must be fetched
before it can be used for another query.
This uncovered an error in the protocol: the server doesn't drop
SERVER_MORE_RESULTS_EXISTS status flag upon an error, so in case of
a multi-query like SELECT 1; SELECT syntax_error; SELECT 2;
the client has no way to know that the server won't ever come to
execution of the third query and won't return any result sets for it.
For now, fix it in cli_safe_read, as a proper fix requires extension
of the client-server protocol.
sql/protocol.cc:
Remove a name that is never used.
sql/slave.cc:
net_safe_read -> cli_safe_read
tests/mysql_client_test.c:
Make 'query' a local variable to avoid name clash.
Add a test case for Bug#15752 "Lost connection to MySQL server when
calling an SP from C API"
2006-07-24 12:56:53 +02:00
|
|
|
if ((pkt_len=cli_safe_read(mysql)) == packet_error)
|
2003-05-02 18:07:41 +02:00
|
|
|
{
|
|
|
|
free_rows(result);
|
|
|
|
DBUG_RETURN(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*prev_ptr=0; /* last pointer is null */
|
|
|
|
if (pkt_len > 1) /* MySQL 4.1 protocol */
|
|
|
|
{
|
|
|
|
mysql->warning_count= uint2korr(cp+1);
|
2003-11-18 12:47:27 +01:00
|
|
|
mysql->server_status= uint2korr(cp+3);
|
2003-11-28 11:18:13 +01:00
|
|
|
DBUG_PRINT("info",("status: %u warning_count: %u",
|
|
|
|
mysql->server_status, mysql->warning_count));
|
2003-05-02 18:07:41 +02:00
|
|
|
}
|
2006-11-20 21:42:06 +01:00
|
|
|
DBUG_PRINT("exit", ("Got %lu rows", (ulong) result->rows));
|
2003-05-02 18:07:41 +02:00
|
|
|
DBUG_RETURN(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Read one row. Uses packet buffer as storage for fields.
|
|
|
|
When next packet is read, the previous field values are destroyed
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
read_one_row(MYSQL *mysql,uint fields,MYSQL_ROW row, ulong *lengths)
|
|
|
|
{
|
|
|
|
uint field;
|
|
|
|
ulong pkt_len,len;
|
2003-05-31 12:15:46 +02:00
|
|
|
uchar *pos, *prev_pos, *end_pos;
|
|
|
|
NET *net= &mysql->net;
|
2003-05-02 18:07:41 +02:00
|
|
|
|
A fix and a test case for Bug#15752 "Lost connection to MySQL server
when calling a SP from C API"
The bug was caused by lack of checks for misuse in mysql_real_query.
A stored procedure always returns at least one result, which is the
status of execution of the procedure itself.
This result, or so-called OK packet, is similar to a result
returned by INSERT/UPDATE/CREATE operations: it contains the overall
status of execution, the number of affected rows and the number of
warnings. The client test program attached to the bug did not read this
result and ivnoked the next query. In turn, libmysql had no check for
such scenario and mysql_real_query was simply trying to send that query
without reading the pending response, thus messing up the communication
protocol.
The fix is to return an error from mysql_real_query when it's called
prior to retrieval of all pending results.
client/mysqlbinlog.cc:
net_safe_read -> cli_safe_read
include/mysql.h:
Remove a private function from the public header.
include/mysql_com.h:
Remove a define that is never used.
include/sql_common.h:
Add a declaration for cli_safe_read - a function that reads one packet
from the server.
libmysql/libmysql.c:
net_safe_read -> cli_safe_read
Return CR_COMMANDS_OUT_OF_SYNC on attempt to execute a statement
using a connection which has pending result sets.
sql-common/client.c:
Actual fix for Bug#15752: if the server has pending result sets for
the client, return CR_COMMANDS_OUT_OF_SYNC on attempt to execute
another query. Similarly to the behaviour of mysql_use_result(),
multiple result sets block the connection and must be fetched
before it can be used for another query.
This uncovered an error in the protocol: the server doesn't drop
SERVER_MORE_RESULTS_EXISTS status flag upon an error, so in case of
a multi-query like SELECT 1; SELECT syntax_error; SELECT 2;
the client has no way to know that the server won't ever come to
execution of the third query and won't return any result sets for it.
For now, fix it in cli_safe_read, as a proper fix requires extension
of the client-server protocol.
sql/protocol.cc:
Remove a name that is never used.
sql/slave.cc:
net_safe_read -> cli_safe_read
tests/mysql_client_test.c:
Make 'query' a local variable to avoid name clash.
Add a test case for Bug#15752 "Lost connection to MySQL server when
calling an SP from C API"
2006-07-24 12:56:53 +02:00
|
|
|
if ((pkt_len=cli_safe_read(mysql)) == packet_error)
|
2003-05-02 18:07:41 +02:00
|
|
|
return -1;
|
2003-05-31 12:15:46 +02:00
|
|
|
if (pkt_len <= 8 && net->read_pos[0] == 254)
|
2003-05-02 18:07:41 +02:00
|
|
|
{
|
|
|
|
if (pkt_len > 1) /* MySQL 4.1 protocol */
|
2003-11-18 12:47:27 +01:00
|
|
|
{
|
2003-05-31 12:15:46 +02:00
|
|
|
mysql->warning_count= uint2korr(net->read_pos+1);
|
2003-11-18 12:47:27 +01:00
|
|
|
mysql->server_status= uint2korr(net->read_pos+3);
|
|
|
|
}
|
2003-05-02 18:07:41 +02:00
|
|
|
return 1; /* End of data */
|
|
|
|
}
|
|
|
|
prev_pos= 0; /* allowed to write at packet[-1] */
|
2003-05-31 12:15:46 +02:00
|
|
|
pos=net->read_pos;
|
2003-05-02 18:07:41 +02:00
|
|
|
end_pos=pos+pkt_len;
|
|
|
|
for (field=0 ; field < fields ; field++)
|
|
|
|
{
|
|
|
|
if ((len=(ulong) net_field_length(&pos)) == NULL_LENGTH)
|
|
|
|
{ /* null field */
|
|
|
|
row[field] = 0;
|
|
|
|
*lengths++=0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (len > (ulong) (end_pos - pos))
|
|
|
|
{
|
2004-03-12 13:21:48 +01:00
|
|
|
set_mysql_error(mysql, CR_UNKNOWN_ERROR, unknown_sqlstate);
|
2003-05-02 18:07:41 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
row[field] = (char*) pos;
|
|
|
|
pos+=len;
|
|
|
|
*lengths++=len;
|
|
|
|
}
|
|
|
|
if (prev_pos)
|
|
|
|
*prev_pos=0; /* Terminate prev field */
|
|
|
|
prev_pos=pos;
|
|
|
|
}
|
|
|
|
row[field]=(char*) prev_pos+1; /* End of last field */
|
|
|
|
*prev_pos=0; /* Terminate last field */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
Init MySQL structure or allocate one
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
MYSQL * STDCALL
|
|
|
|
mysql_init(MYSQL *mysql)
|
|
|
|
{
|
2003-12-18 12:51:22 +01:00
|
|
|
if (mysql_server_init(0, NULL, NULL))
|
|
|
|
return 0;
|
2003-05-02 18:07:41 +02:00
|
|
|
if (!mysql)
|
|
|
|
{
|
|
|
|
if (!(mysql=(MYSQL*) my_malloc(sizeof(*mysql),MYF(MY_WME | MY_ZEROFILL))))
|
|
|
|
return 0;
|
|
|
|
mysql->free_me=1;
|
|
|
|
}
|
|
|
|
else
|
2006-03-22 23:37:40 +01:00
|
|
|
bzero((char*) (mysql), sizeof(*(mysql)));
|
2003-06-14 10:37:42 +02:00
|
|
|
mysql->options.connect_timeout= CONNECT_TIMEOUT;
|
|
|
|
mysql->last_used_con= mysql->next_slave= mysql->master = mysql;
|
2006-06-19 19:11:01 +02:00
|
|
|
mysql->charset=default_client_charset_info;
|
2004-09-06 16:23:51 +02:00
|
|
|
strmov(mysql->net.sqlstate, not_error_sqlstate);
|
2003-05-02 18:07:41 +02:00
|
|
|
/*
|
|
|
|
By default, we are a replication pivot. The caller must reset it
|
|
|
|
after we return if this is not the case.
|
|
|
|
*/
|
2003-06-14 10:37:42 +02:00
|
|
|
#ifndef TO_BE_DELETED
|
2003-05-02 18:07:41 +02:00
|
|
|
mysql->rpl_pivot = 1;
|
2003-06-14 10:37:42 +02:00
|
|
|
#endif
|
2003-05-02 18:07:41 +02:00
|
|
|
|
2003-06-14 10:37:42 +02:00
|
|
|
/*
|
|
|
|
Only enable LOAD DATA INFILE by default if configured with
|
|
|
|
--enable-local-infile
|
|
|
|
*/
|
2004-03-23 01:58:49 +01:00
|
|
|
|
2003-06-14 10:37:42 +02:00
|
|
|
#if defined(ENABLED_LOCAL_INFILE) && !defined(MYSQL_SERVER)
|
2003-05-02 18:07:41 +02:00
|
|
|
mysql->options.client_flag|= CLIENT_LOCAL_FILES;
|
|
|
|
#endif
|
2004-03-23 01:58:49 +01:00
|
|
|
|
2003-05-02 18:07:41 +02:00
|
|
|
#ifdef HAVE_SMEM
|
2003-06-14 10:37:42 +02:00
|
|
|
mysql->options.shared_memory_base_name= (char*) def_shared_memory_base_name;
|
2003-05-02 18:07:41 +02:00
|
|
|
#endif
|
2004-03-23 01:58:49 +01:00
|
|
|
|
2003-06-18 12:58:57 +02:00
|
|
|
mysql->options.methods_to_use= MYSQL_OPT_GUESS_CONNECTION;
|
2004-12-17 22:17:25 +01:00
|
|
|
mysql->options.report_data_truncation= TRUE; /* default */
|
2006-01-26 11:20:59 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
By default we don't reconnect because it could silently corrupt data (after
|
|
|
|
reconnection you potentially lose table locks, user variables, session
|
|
|
|
variables (transactions but they are specifically dealt with in
|
|
|
|
mysql_reconnect()).
|
|
|
|
This is a change: < 5.0.3 mysql->reconnect was set to 1 by default.
|
|
|
|
How this change impacts existing apps:
|
|
|
|
- existing apps which relyed on the default will see a behaviour change;
|
|
|
|
they will have to set reconnect=1 after mysql_real_connect().
|
|
|
|
- existing apps which explicitely asked for reconnection (the only way they
|
|
|
|
could do it was by setting mysql.reconnect to 1 after mysql_real_connect())
|
|
|
|
will not see a behaviour change.
|
|
|
|
- existing apps which explicitely asked for no reconnection
|
|
|
|
(mysql.reconnect=0) will not see a behaviour change.
|
|
|
|
*/
|
|
|
|
mysql->reconnect= 0;
|
|
|
|
|
2003-05-02 18:07:41 +02:00
|
|
|
return mysql;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2003-06-14 10:37:42 +02:00
|
|
|
Fill in SSL part of MYSQL structure and set 'use_ssl' flag.
|
|
|
|
NB! Errors are not reported until you do mysql_real_connect.
|
2003-05-02 18:07:41 +02:00
|
|
|
*/
|
|
|
|
|
2003-06-14 10:37:42 +02:00
|
|
|
#define strdup_if_not_null(A) (A) == 0 ? 0 : my_strdup((A),MYF(MY_WME))
|
|
|
|
|
|
|
|
my_bool STDCALL
|
|
|
|
mysql_ssl_set(MYSQL *mysql __attribute__((unused)) ,
|
|
|
|
const char *key __attribute__((unused)),
|
|
|
|
const char *cert __attribute__((unused)),
|
|
|
|
const char *ca __attribute__((unused)),
|
|
|
|
const char *capath __attribute__((unused)),
|
|
|
|
const char *cipher __attribute__((unused)))
|
2003-05-02 18:07:41 +02:00
|
|
|
{
|
2005-10-04 15:43:55 +02:00
|
|
|
DBUG_ENTER("mysql_ssl_set");
|
2003-06-14 10:37:42 +02:00
|
|
|
#ifdef HAVE_OPENSSL
|
|
|
|
mysql->options.ssl_key= strdup_if_not_null(key);
|
|
|
|
mysql->options.ssl_cert= strdup_if_not_null(cert);
|
|
|
|
mysql->options.ssl_ca= strdup_if_not_null(ca);
|
|
|
|
mysql->options.ssl_capath= strdup_if_not_null(capath);
|
|
|
|
mysql->options.ssl_cipher= strdup_if_not_null(cipher);
|
|
|
|
#endif /* HAVE_OPENSSL */
|
2005-10-04 15:43:55 +02:00
|
|
|
DBUG_RETURN(0);
|
2003-06-14 10:37:42 +02:00
|
|
|
}
|
2003-05-02 18:07:41 +02:00
|
|
|
|
|
|
|
|
2003-06-14 10:37:42 +02:00
|
|
|
/*
|
|
|
|
Free strings in the SSL structure and clear 'use_ssl' flag.
|
|
|
|
NB! Errors are not reported until you do mysql_real_connect.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_OPENSSL
|
2006-07-03 20:08:38 +02:00
|
|
|
|
2003-06-14 10:37:42 +02:00
|
|
|
static void
|
|
|
|
mysql_ssl_free(MYSQL *mysql __attribute__((unused)))
|
|
|
|
{
|
2006-03-10 16:41:14 +01:00
|
|
|
struct st_VioSSLFd *ssl_fd= (struct st_VioSSLFd*) mysql->connector_fd;
|
2005-10-04 15:43:55 +02:00
|
|
|
DBUG_ENTER("mysql_ssl_free");
|
2005-11-25 11:57:13 +01:00
|
|
|
|
2003-06-14 10:37:42 +02:00
|
|
|
my_free(mysql->options.ssl_key, MYF(MY_ALLOW_ZERO_PTR));
|
|
|
|
my_free(mysql->options.ssl_cert, MYF(MY_ALLOW_ZERO_PTR));
|
|
|
|
my_free(mysql->options.ssl_ca, MYF(MY_ALLOW_ZERO_PTR));
|
|
|
|
my_free(mysql->options.ssl_capath, MYF(MY_ALLOW_ZERO_PTR));
|
2006-04-18 17:58:27 +02:00
|
|
|
my_free(mysql->options.ssl_cipher, MYF(MY_ALLOW_ZERO_PTR));
|
2006-03-10 16:41:14 +01:00
|
|
|
if (ssl_fd)
|
|
|
|
SSL_CTX_free(ssl_fd->ssl_context);
|
2003-06-14 10:37:42 +02:00
|
|
|
my_free(mysql->connector_fd,MYF(MY_ALLOW_ZERO_PTR));
|
|
|
|
mysql->options.ssl_key = 0;
|
|
|
|
mysql->options.ssl_cert = 0;
|
|
|
|
mysql->options.ssl_ca = 0;
|
|
|
|
mysql->options.ssl_capath = 0;
|
|
|
|
mysql->options.ssl_cipher= 0;
|
|
|
|
mysql->options.use_ssl = FALSE;
|
|
|
|
mysql->connector_fd = 0;
|
2005-10-04 15:43:55 +02:00
|
|
|
DBUG_VOID_RETURN;
|
2003-05-02 18:07:41 +02:00
|
|
|
}
|
2006-04-22 00:48:13 +02:00
|
|
|
|
2006-07-03 20:08:38 +02:00
|
|
|
#endif /* HAVE_OPENSSL */
|
2006-04-22 00:48:13 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
Return the SSL cipher (if any) used for current
|
|
|
|
connection to the server.
|
|
|
|
|
|
|
|
SYNOPSYS
|
|
|
|
mysql_get_ssl_cipher()
|
|
|
|
mysql pointer to the mysql connection
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
const char * STDCALL
|
|
|
|
mysql_get_ssl_cipher(MYSQL *mysql)
|
|
|
|
{
|
|
|
|
DBUG_ENTER("mysql_get_ssl_cipher");
|
2006-07-03 20:08:38 +02:00
|
|
|
#ifdef HAVE_OPENSSL
|
2006-04-22 00:48:13 +02:00
|
|
|
if (mysql->net.vio && mysql->net.vio->ssl_arg)
|
|
|
|
DBUG_RETURN(SSL_get_cipher_name((SSL*)mysql->net.vio->ssl_arg));
|
2006-07-03 20:08:38 +02:00
|
|
|
#endif /* HAVE_OPENSSL */
|
2006-04-22 00:48:13 +02:00
|
|
|
DBUG_RETURN(NULL);
|
|
|
|
}
|
|
|
|
|
2006-04-26 22:24:25 +02:00
|
|
|
|
2006-04-18 17:58:27 +02:00
|
|
|
/*
|
|
|
|
Check the server's (subject) Common Name against the
|
|
|
|
hostname we connected to
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
ssl_verify_server_cert()
|
|
|
|
vio pointer to a SSL connected vio
|
|
|
|
server_hostname name of the server that we connected to
|
|
|
|
|
|
|
|
RETURN VALUES
|
|
|
|
0 Success
|
|
|
|
1 Failed to validate server
|
|
|
|
|
|
|
|
*/
|
2006-07-03 20:08:38 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_OPENSSL
|
|
|
|
|
2006-04-18 17:58:27 +02:00
|
|
|
static int ssl_verify_server_cert(Vio *vio, const char* server_hostname)
|
|
|
|
{
|
|
|
|
SSL *ssl;
|
|
|
|
X509 *server_cert;
|
|
|
|
char *cp1, *cp2;
|
|
|
|
char buf[256];
|
|
|
|
DBUG_ENTER("ssl_verify_server_cert");
|
|
|
|
DBUG_PRINT("enter", ("server_hostname: %s", server_hostname));
|
|
|
|
|
|
|
|
if (!(ssl= (SSL*)vio->ssl_arg))
|
|
|
|
{
|
|
|
|
DBUG_PRINT("error", ("No SSL pointer found"));
|
|
|
|
DBUG_RETURN(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!server_hostname)
|
|
|
|
{
|
|
|
|
DBUG_PRINT("error", ("No server hostname supplied"));
|
|
|
|
DBUG_RETURN(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(server_cert= SSL_get_peer_certificate(ssl)))
|
|
|
|
{
|
|
|
|
DBUG_PRINT("error", ("Could not get server certificate"));
|
|
|
|
DBUG_RETURN(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
We already know that the certificate exchanged was valid; the SSL library
|
|
|
|
handled that. Now we need to verify that the contents of the certificate
|
|
|
|
are what we expect.
|
|
|
|
*/
|
|
|
|
|
|
|
|
X509_NAME_oneline(X509_get_subject_name(server_cert), buf, sizeof(buf));
|
|
|
|
X509_free (server_cert);
|
|
|
|
|
|
|
|
DBUG_PRINT("info", ("hostname in cert: %s", buf));
|
2006-05-03 16:59:02 +02:00
|
|
|
cp1= strstr(buf, "/CN=");
|
2006-04-18 17:58:27 +02:00
|
|
|
if (cp1)
|
|
|
|
{
|
2006-05-03 16:59:02 +02:00
|
|
|
cp1+= 4; /* Skip the "/CN=" that we found */
|
|
|
|
/* Search for next / which might be the delimiter for email */
|
|
|
|
cp2= strchr(cp1, '/');
|
2006-04-18 17:58:27 +02:00
|
|
|
if (cp2)
|
2006-05-03 16:59:02 +02:00
|
|
|
*cp2= '\0';
|
2006-04-18 17:58:27 +02:00
|
|
|
DBUG_PRINT("info", ("Server hostname in cert: %s", cp1));
|
|
|
|
if (!strcmp(cp1, server_hostname))
|
|
|
|
{
|
|
|
|
/* Success */
|
|
|
|
DBUG_RETURN(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DBUG_PRINT("error", ("SSL certificate validation failure"));
|
|
|
|
DBUG_RETURN(1);
|
|
|
|
}
|
|
|
|
|
2003-06-14 10:37:42 +02:00
|
|
|
#endif /* HAVE_OPENSSL */
|
2003-05-02 18:07:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Note that the mysql argument must be initialized with mysql_init()
|
|
|
|
before calling mysql_real_connect !
|
|
|
|
*/
|
|
|
|
|
2003-12-22 13:57:34 +01:00
|
|
|
static my_bool cli_read_query_result(MYSQL *mysql);
|
|
|
|
static MYSQL_RES *cli_use_result(MYSQL *mysql);
|
2003-06-17 18:32:31 +02:00
|
|
|
|
|
|
|
static MYSQL_METHODS client_methods=
|
|
|
|
{
|
A fix and a test case for Bug#15752 "Lost connection to MySQL server
when calling a SP from C API"
The bug was caused by lack of checks for misuse in mysql_real_query.
A stored procedure always returns at least one result, which is the
status of execution of the procedure itself.
This result, or so-called OK packet, is similar to a result
returned by INSERT/UPDATE/CREATE operations: it contains the overall
status of execution, the number of affected rows and the number of
warnings. The client test program attached to the bug did not read this
result and ivnoked the next query. In turn, libmysql had no check for
such scenario and mysql_real_query was simply trying to send that query
without reading the pending response, thus messing up the communication
protocol.
The fix is to return an error from mysql_real_query when it's called
prior to retrieval of all pending results.
client/mysqlbinlog.cc:
net_safe_read -> cli_safe_read
include/mysql.h:
Remove a private function from the public header.
include/mysql_com.h:
Remove a define that is never used.
include/sql_common.h:
Add a declaration for cli_safe_read - a function that reads one packet
from the server.
libmysql/libmysql.c:
net_safe_read -> cli_safe_read
Return CR_COMMANDS_OUT_OF_SYNC on attempt to execute a statement
using a connection which has pending result sets.
sql-common/client.c:
Actual fix for Bug#15752: if the server has pending result sets for
the client, return CR_COMMANDS_OUT_OF_SYNC on attempt to execute
another query. Similarly to the behaviour of mysql_use_result(),
multiple result sets block the connection and must be fetched
before it can be used for another query.
This uncovered an error in the protocol: the server doesn't drop
SERVER_MORE_RESULTS_EXISTS status flag upon an error, so in case of
a multi-query like SELECT 1; SELECT syntax_error; SELECT 2;
the client has no way to know that the server won't ever come to
execution of the third query and won't return any result sets for it.
For now, fix it in cli_safe_read, as a proper fix requires extension
of the client-server protocol.
sql/protocol.cc:
Remove a name that is never used.
sql/slave.cc:
net_safe_read -> cli_safe_read
tests/mysql_client_test.c:
Make 'query' a local variable to avoid name clash.
Add a test case for Bug#15752 "Lost connection to MySQL server when
calling an SP from C API"
2006-07-24 12:56:53 +02:00
|
|
|
cli_read_query_result, /* read_query_result */
|
|
|
|
cli_advanced_command, /* advanced_command */
|
|
|
|
cli_read_rows, /* read_rows */
|
|
|
|
cli_use_result, /* use_result */
|
|
|
|
cli_fetch_lengths, /* fetch_lengths */
|
|
|
|
cli_flush_use_result /* flush_use_result */
|
2003-09-19 11:05:28 +02:00
|
|
|
#ifndef MYSQL_SERVER
|
A fix and a test case for Bug#15752 "Lost connection to MySQL server
when calling a SP from C API"
The bug was caused by lack of checks for misuse in mysql_real_query.
A stored procedure always returns at least one result, which is the
status of execution of the procedure itself.
This result, or so-called OK packet, is similar to a result
returned by INSERT/UPDATE/CREATE operations: it contains the overall
status of execution, the number of affected rows and the number of
warnings. The client test program attached to the bug did not read this
result and ivnoked the next query. In turn, libmysql had no check for
such scenario and mysql_real_query was simply trying to send that query
without reading the pending response, thus messing up the communication
protocol.
The fix is to return an error from mysql_real_query when it's called
prior to retrieval of all pending results.
client/mysqlbinlog.cc:
net_safe_read -> cli_safe_read
include/mysql.h:
Remove a private function from the public header.
include/mysql_com.h:
Remove a define that is never used.
include/sql_common.h:
Add a declaration for cli_safe_read - a function that reads one packet
from the server.
libmysql/libmysql.c:
net_safe_read -> cli_safe_read
Return CR_COMMANDS_OUT_OF_SYNC on attempt to execute a statement
using a connection which has pending result sets.
sql-common/client.c:
Actual fix for Bug#15752: if the server has pending result sets for
the client, return CR_COMMANDS_OUT_OF_SYNC on attempt to execute
another query. Similarly to the behaviour of mysql_use_result(),
multiple result sets block the connection and must be fetched
before it can be used for another query.
This uncovered an error in the protocol: the server doesn't drop
SERVER_MORE_RESULTS_EXISTS status flag upon an error, so in case of
a multi-query like SELECT 1; SELECT syntax_error; SELECT 2;
the client has no way to know that the server won't ever come to
execution of the third query and won't return any result sets for it.
For now, fix it in cli_safe_read, as a proper fix requires extension
of the client-server protocol.
sql/protocol.cc:
Remove a name that is never used.
sql/slave.cc:
net_safe_read -> cli_safe_read
tests/mysql_client_test.c:
Make 'query' a local variable to avoid name clash.
Add a test case for Bug#15752 "Lost connection to MySQL server when
calling an SP from C API"
2006-07-24 12:56:53 +02:00
|
|
|
,cli_list_fields, /* list_fields */
|
|
|
|
cli_read_prepare_result, /* read_prepare_result */
|
|
|
|
cli_stmt_execute, /* stmt_execute */
|
|
|
|
cli_read_binary_rows, /* read_binary_rows */
|
|
|
|
cli_unbuffered_fetch, /* unbuffered_fetch */
|
|
|
|
NULL, /* free_embedded_thd */
|
|
|
|
cli_read_statistics, /* read_statistics */
|
|
|
|
cli_read_query_result, /* next_result */
|
|
|
|
cli_read_change_user_result, /* read_change_user_result */
|
|
|
|
cli_read_binary_rows /* read_rows_from_cursor */
|
2003-09-19 11:05:28 +02:00
|
|
|
#endif
|
2003-06-17 18:32:31 +02:00
|
|
|
};
|
|
|
|
|
2006-06-19 19:11:01 +02:00
|
|
|
C_MODE_START
|
|
|
|
int mysql_init_character_set(MYSQL *mysql)
|
|
|
|
{
|
|
|
|
NET *net= &mysql->net;
|
2006-06-30 13:48:18 +02:00
|
|
|
const char *default_collation_name;
|
|
|
|
|
2006-06-19 19:11:01 +02:00
|
|
|
/* Set character set */
|
2006-06-30 13:48:18 +02:00
|
|
|
if (!mysql->options.charset_name)
|
|
|
|
{
|
|
|
|
default_collation_name= MYSQL_DEFAULT_COLLATION_NAME;
|
|
|
|
if (!(mysql->options.charset_name=
|
2006-06-19 19:11:01 +02:00
|
|
|
my_strdup(MYSQL_DEFAULT_CHARSET_NAME,MYF(MY_WME))))
|
|
|
|
return 1;
|
2006-06-30 13:48:18 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
default_collation_name= NULL;
|
2006-06-19 19:11:01 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
const char *save= charsets_dir;
|
|
|
|
if (mysql->options.charset_dir)
|
|
|
|
charsets_dir=mysql->options.charset_dir;
|
|
|
|
mysql->charset=get_charset_by_csname(mysql->options.charset_name,
|
|
|
|
MY_CS_PRIMARY, MYF(MY_WME));
|
2006-06-30 13:48:18 +02:00
|
|
|
if (mysql->charset && default_collation_name)
|
|
|
|
{
|
|
|
|
CHARSET_INFO *collation;
|
|
|
|
if ((collation=
|
|
|
|
get_charset_by_name(default_collation_name, MYF(MY_WME))))
|
|
|
|
{
|
|
|
|
if (!my_charset_same(mysql->charset, collation))
|
|
|
|
{
|
|
|
|
my_printf_error(ER_UNKNOWN_ERROR,
|
|
|
|
"COLLATION %s is not valid for CHARACTER SET %s",
|
|
|
|
MYF(0),
|
|
|
|
default_collation_name, mysql->options.charset_name);
|
|
|
|
mysql->charset= NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mysql->charset= collation;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
mysql->charset= NULL;
|
|
|
|
}
|
2006-06-19 19:11:01 +02:00
|
|
|
charsets_dir= save;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mysql->charset)
|
|
|
|
{
|
|
|
|
net->last_errno=CR_CANT_READ_CHARSET;
|
|
|
|
strmov(net->sqlstate, unknown_sqlstate);
|
|
|
|
if (mysql->options.charset_dir)
|
|
|
|
my_snprintf(net->last_error, sizeof(net->last_error)-1,
|
|
|
|
ER(net->last_errno),
|
|
|
|
mysql->options.charset_name,
|
|
|
|
mysql->options.charset_dir);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char cs_dir_name[FN_REFLEN];
|
|
|
|
get_charsets_dir(cs_dir_name);
|
|
|
|
my_snprintf(net->last_error, sizeof(net->last_error)-1,
|
|
|
|
ER(net->last_errno),
|
|
|
|
mysql->options.charset_name,
|
|
|
|
cs_dir_name);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
C_MODE_END
|
|
|
|
|
|
|
|
|
2003-06-17 18:32:31 +02:00
|
|
|
MYSQL * STDCALL
|
|
|
|
CLI_MYSQL_REAL_CONNECT(MYSQL *mysql,const char *host, const char *user,
|
|
|
|
const char *passwd, const char *db,
|
|
|
|
uint port, const char *unix_socket,ulong client_flag)
|
2003-05-02 18:07:41 +02:00
|
|
|
{
|
2006-09-27 14:49:16 +02:00
|
|
|
char buff[NAME_LEN+USERNAME_LENGTH+100];
|
2004-02-04 09:49:54 +01:00
|
|
|
char *end,*host_info;
|
2003-05-02 18:07:41 +02:00
|
|
|
my_socket sock;
|
2003-11-04 13:09:03 +01:00
|
|
|
in_addr_t ip_addr;
|
2003-05-02 18:07:41 +02:00
|
|
|
struct sockaddr_in sock_addr;
|
|
|
|
ulong pkt_length;
|
|
|
|
NET *net= &mysql->net;
|
2003-05-31 12:15:46 +02:00
|
|
|
#ifdef MYSQL_SERVER
|
2003-05-02 18:07:41 +02:00
|
|
|
thr_alarm_t alarmed;
|
|
|
|
ALARM alarm_buff;
|
|
|
|
#endif
|
|
|
|
#ifdef __WIN__
|
|
|
|
HANDLE hPipe=INVALID_HANDLE_VALUE;
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_UN_H
|
|
|
|
struct sockaddr_un UNIXaddr;
|
|
|
|
#endif
|
2003-06-16 00:13:22 +02:00
|
|
|
init_sigpipe_variables
|
2003-05-02 18:07:41 +02:00
|
|
|
DBUG_ENTER("mysql_real_connect");
|
|
|
|
LINT_INIT(host_info);
|
|
|
|
|
|
|
|
DBUG_PRINT("enter",("host: %s db: %s user: %s",
|
|
|
|
host ? host : "(Null)",
|
|
|
|
db ? db : "(Null)",
|
|
|
|
user ? user : "(Null)"));
|
|
|
|
|
|
|
|
/* Don't give sigpipe errors if the client doesn't want them */
|
|
|
|
set_sigpipe(mysql);
|
2003-11-28 15:28:04 +01:00
|
|
|
mysql->methods= &client_methods;
|
2003-05-02 18:07:41 +02:00
|
|
|
net->vio = 0; /* If something goes wrong */
|
2003-05-31 12:15:46 +02:00
|
|
|
mysql->client_flag=0; /* For handshake */
|
2003-05-02 18:07:41 +02:00
|
|
|
|
|
|
|
/* use default options */
|
|
|
|
if (mysql->options.my_cnf_file || mysql->options.my_cnf_group)
|
|
|
|
{
|
|
|
|
mysql_read_default_options(&mysql->options,
|
|
|
|
(mysql->options.my_cnf_file ?
|
|
|
|
mysql->options.my_cnf_file : "my"),
|
|
|
|
mysql->options.my_cnf_group);
|
|
|
|
my_free(mysql->options.my_cnf_file,MYF(MY_ALLOW_ZERO_PTR));
|
|
|
|
my_free(mysql->options.my_cnf_group,MYF(MY_ALLOW_ZERO_PTR));
|
|
|
|
mysql->options.my_cnf_file=mysql->options.my_cnf_group=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Some empty-string-tests are done because of ODBC */
|
|
|
|
if (!host || !host[0])
|
|
|
|
host=mysql->options.host;
|
|
|
|
if (!user || !user[0])
|
2003-11-28 11:11:44 +01:00
|
|
|
{
|
2003-05-02 18:07:41 +02:00
|
|
|
user=mysql->options.user;
|
2003-11-28 11:11:44 +01:00
|
|
|
if (!user)
|
|
|
|
user= "";
|
|
|
|
}
|
2003-05-02 18:07:41 +02:00
|
|
|
if (!passwd)
|
|
|
|
{
|
|
|
|
passwd=mysql->options.password;
|
2003-06-14 10:37:42 +02:00
|
|
|
#if !defined(DONT_USE_MYSQL_PWD) && !defined(MYSQL_SERVER)
|
2003-05-02 18:07:41 +02:00
|
|
|
if (!passwd)
|
|
|
|
passwd=getenv("MYSQL_PWD"); /* get it from environment */
|
|
|
|
#endif
|
2003-11-28 11:11:44 +01:00
|
|
|
if (!passwd)
|
|
|
|
passwd= "";
|
2003-05-02 18:07:41 +02:00
|
|
|
}
|
|
|
|
if (!db || !db[0])
|
|
|
|
db=mysql->options.db;
|
|
|
|
if (!port)
|
|
|
|
port=mysql->options.port;
|
|
|
|
if (!unix_socket)
|
|
|
|
unix_socket=mysql->options.unix_socket;
|
|
|
|
|
|
|
|
mysql->server_status=SERVER_STATUS_AUTOCOMMIT;
|
|
|
|
|
|
|
|
/*
|
2003-07-01 21:40:59 +02:00
|
|
|
Part 0: Grab a socket and connect it to the server
|
2003-05-02 18:07:41 +02:00
|
|
|
*/
|
2003-06-14 10:37:42 +02:00
|
|
|
#if defined(HAVE_SMEM)
|
2003-05-02 18:07:41 +02:00
|
|
|
if ((!mysql->options.protocol ||
|
|
|
|
mysql->options.protocol == MYSQL_PROTOCOL_MEMORY) &&
|
|
|
|
(!host || !strcmp(host,LOCAL_HOST)))
|
|
|
|
{
|
|
|
|
if ((create_shared_memory(mysql,net, mysql->options.connect_timeout)) ==
|
|
|
|
INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
DBUG_PRINT("error",
|
|
|
|
("host: '%s' socket: '%s' shared memory: %s have_tcpip: %d",
|
|
|
|
host ? host : "<null>",
|
|
|
|
unix_socket ? unix_socket : "<null>",
|
|
|
|
(int) mysql->options.shared_memory_base_name,
|
|
|
|
(int) have_tcpip));
|
|
|
|
if (mysql->options.protocol == MYSQL_PROTOCOL_MEMORY)
|
|
|
|
goto error;
|
|
|
|
/* Try also with PIPE or TCP/IP */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mysql->options.protocol=MYSQL_PROTOCOL_MEMORY;
|
|
|
|
sock=0;
|
|
|
|
unix_socket = 0;
|
|
|
|
host=mysql->options.shared_memory_base_name;
|
2005-01-09 02:19:42 +01:00
|
|
|
my_snprintf(host_info=buff, sizeof(buff)-1,
|
|
|
|
ER(CR_SHARED_MEMORY_CONNECTION), host);
|
2003-05-02 18:07:41 +02:00
|
|
|
}
|
2003-06-14 10:37:42 +02:00
|
|
|
}
|
2003-05-02 18:07:41 +02:00
|
|
|
#endif /* HAVE_SMEM */
|
|
|
|
#if defined(HAVE_SYS_UN_H)
|
2003-06-14 10:37:42 +02:00
|
|
|
if (!net->vio &&
|
|
|
|
(!mysql->options.protocol ||
|
|
|
|
mysql->options.protocol == MYSQL_PROTOCOL_SOCKET) &&
|
|
|
|
(unix_socket || mysql_unix_port) &&
|
|
|
|
(!host || !strcmp(host,LOCAL_HOST)))
|
|
|
|
{
|
|
|
|
host=LOCAL_HOST;
|
|
|
|
if (!unix_socket)
|
|
|
|
unix_socket=mysql_unix_port;
|
|
|
|
host_info=(char*) ER(CR_LOCALHOST_CONNECTION);
|
|
|
|
DBUG_PRINT("info",("Using UNIX sock '%s'",unix_socket));
|
|
|
|
if ((sock = socket(AF_UNIX,SOCK_STREAM,0)) == SOCKET_ERROR)
|
2003-05-02 18:07:41 +02:00
|
|
|
{
|
2003-06-14 10:37:42 +02:00
|
|
|
net->last_errno=CR_SOCKET_CREATE_ERROR;
|
|
|
|
strmov(net->sqlstate, unknown_sqlstate);
|
2005-01-09 02:19:42 +01:00
|
|
|
my_snprintf(net->last_error,sizeof(net->last_error)-1,
|
|
|
|
ER(net->last_errno),socket_errno);
|
2003-06-14 10:37:42 +02:00
|
|
|
goto error;
|
|
|
|
}
|
2005-03-05 22:10:08 +01:00
|
|
|
net->vio= vio_new(sock, VIO_TYPE_SOCKET,
|
|
|
|
VIO_LOCALHOST | VIO_BUFFERED_READ);
|
2003-06-14 10:37:42 +02:00
|
|
|
bzero((char*) &UNIXaddr,sizeof(UNIXaddr));
|
|
|
|
UNIXaddr.sun_family = AF_UNIX;
|
|
|
|
strmake(UNIXaddr.sun_path, unix_socket, sizeof(UNIXaddr.sun_path)-1);
|
|
|
|
if (my_connect(sock,(struct sockaddr *) &UNIXaddr, sizeof(UNIXaddr),
|
|
|
|
mysql->options.connect_timeout))
|
|
|
|
{
|
|
|
|
DBUG_PRINT("error",("Got error %d on connect to local server",
|
|
|
|
socket_errno));
|
|
|
|
net->last_errno=CR_CONNECTION_ERROR;
|
|
|
|
strmov(net->sqlstate, unknown_sqlstate);
|
2005-01-09 02:19:42 +01:00
|
|
|
my_snprintf(net->last_error,sizeof(net->last_error)-1,
|
|
|
|
ER(net->last_errno),unix_socket,socket_errno);
|
2003-06-14 10:37:42 +02:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
mysql->options.protocol=MYSQL_PROTOCOL_SOCKET;
|
|
|
|
}
|
|
|
|
#elif defined(__WIN__)
|
|
|
|
if (!net->vio &&
|
|
|
|
(mysql->options.protocol == MYSQL_PROTOCOL_PIPE ||
|
|
|
|
(host && !strcmp(host,LOCAL_HOST_NAMEDPIPE)) ||
|
|
|
|
(! have_tcpip && (unix_socket || !host && is_NT()))))
|
|
|
|
{
|
|
|
|
sock=0;
|
|
|
|
if ((hPipe=create_named_pipe(net, mysql->options.connect_timeout,
|
|
|
|
(char**) &host, (char**) &unix_socket)) ==
|
|
|
|
INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
DBUG_PRINT("error",
|
|
|
|
("host: '%s' socket: '%s' have_tcpip: %d",
|
|
|
|
host ? host : "<null>",
|
|
|
|
unix_socket ? unix_socket : "<null>",
|
|
|
|
(int) have_tcpip));
|
|
|
|
if (mysql->options.protocol == MYSQL_PROTOCOL_PIPE ||
|
|
|
|
(host && !strcmp(host,LOCAL_HOST_NAMEDPIPE)) ||
|
|
|
|
(unix_socket && !strcmp(unix_socket,MYSQL_NAMEDPIPE)))
|
2003-05-02 18:07:41 +02:00
|
|
|
goto error;
|
2003-06-14 10:37:42 +02:00
|
|
|
/* Try also with TCP/IP */
|
2003-05-02 18:07:41 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-06-14 10:37:42 +02:00
|
|
|
net->vio=vio_new_win32pipe(hPipe);
|
2005-01-09 02:19:42 +01:00
|
|
|
my_snprintf(host_info=buff, sizeof(buff)-1,
|
|
|
|
ER(CR_NAMEDPIPE_CONNECTION), unix_socket);
|
2003-05-02 18:07:41 +02:00
|
|
|
}
|
2003-06-14 10:37:42 +02:00
|
|
|
}
|
2003-05-02 18:07:41 +02:00
|
|
|
#endif
|
2003-06-14 10:37:42 +02:00
|
|
|
if (!net->vio &&
|
|
|
|
(!mysql->options.protocol ||
|
|
|
|
mysql->options.protocol == MYSQL_PROTOCOL_TCP))
|
2003-05-02 18:07:41 +02:00
|
|
|
{
|
|
|
|
unix_socket=0; /* This is not used */
|
|
|
|
if (!port)
|
|
|
|
port=mysql_port;
|
|
|
|
if (!host)
|
|
|
|
host=LOCAL_HOST;
|
2005-01-09 02:19:42 +01:00
|
|
|
my_snprintf(host_info=buff,sizeof(buff)-1,ER(CR_TCP_CONNECTION),host);
|
2003-05-02 18:07:41 +02:00
|
|
|
DBUG_PRINT("info",("Server name: '%s'. TCP sock: %d", host,port));
|
2003-05-31 12:15:46 +02:00
|
|
|
#ifdef MYSQL_SERVER
|
2003-05-02 18:07:41 +02:00
|
|
|
thr_alarm_init(&alarmed);
|
2003-05-31 12:15:46 +02:00
|
|
|
thr_alarm(&alarmed, mysql->options.connect_timeout, &alarm_buff);
|
2003-05-02 18:07:41 +02:00
|
|
|
#endif
|
2003-06-14 10:37:42 +02:00
|
|
|
/* _WIN64 ; Assume that the (int) range is enough for socket() */
|
2003-05-02 18:07:41 +02:00
|
|
|
sock = (my_socket) socket(AF_INET,SOCK_STREAM,0);
|
2003-05-31 12:15:46 +02:00
|
|
|
#ifdef MYSQL_SERVER
|
2003-05-02 18:07:41 +02:00
|
|
|
thr_end_alarm(&alarmed);
|
|
|
|
#endif
|
|
|
|
if (sock == SOCKET_ERROR)
|
|
|
|
{
|
|
|
|
net->last_errno=CR_IPSOCK_ERROR;
|
2003-05-31 12:15:46 +02:00
|
|
|
strmov(net->sqlstate, unknown_sqlstate);
|
2005-01-09 02:19:42 +01:00
|
|
|
my_snprintf(net->last_error,sizeof(net->last_error)-1,
|
|
|
|
ER(net->last_errno),socket_errno);
|
2003-05-02 18:07:41 +02:00
|
|
|
goto error;
|
|
|
|
}
|
2005-03-05 22:10:08 +01:00
|
|
|
net->vio= vio_new(sock, VIO_TYPE_TCPIP, VIO_BUFFERED_READ);
|
2003-05-02 18:07:41 +02:00
|
|
|
bzero((char*) &sock_addr,sizeof(sock_addr));
|
|
|
|
sock_addr.sin_family = AF_INET;
|
|
|
|
|
|
|
|
/*
|
|
|
|
The server name may be a host name or IP address
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((int) (ip_addr = inet_addr(host)) != (int) INADDR_NONE)
|
|
|
|
{
|
|
|
|
memcpy_fixed(&sock_addr.sin_addr,&ip_addr,sizeof(ip_addr));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int tmp_errno;
|
|
|
|
struct hostent tmp_hostent,*hp;
|
|
|
|
char buff2[GETHOSTBYNAME_BUFF_SIZE];
|
|
|
|
hp = my_gethostbyname_r(host,&tmp_hostent,buff2,sizeof(buff2),
|
|
|
|
&tmp_errno);
|
|
|
|
if (!hp)
|
|
|
|
{
|
|
|
|
my_gethostbyname_r_free();
|
|
|
|
net->last_errno=CR_UNKNOWN_HOST;
|
2003-05-31 12:15:46 +02:00
|
|
|
strmov(net->sqlstate, unknown_sqlstate);
|
2005-01-09 02:19:42 +01:00
|
|
|
my_snprintf(net->last_error, sizeof(net->last_error)-1,
|
|
|
|
ER(CR_UNKNOWN_HOST), host, tmp_errno);
|
2003-05-02 18:07:41 +02:00
|
|
|
goto error;
|
|
|
|
}
|
2004-06-21 09:21:20 +02:00
|
|
|
memcpy(&sock_addr.sin_addr, hp->h_addr,
|
|
|
|
min(sizeof(sock_addr.sin_addr), (size_t) hp->h_length));
|
2003-05-02 18:07:41 +02:00
|
|
|
my_gethostbyname_r_free();
|
|
|
|
}
|
|
|
|
sock_addr.sin_port = (ushort) htons((ushort) port);
|
|
|
|
if (my_connect(sock,(struct sockaddr *) &sock_addr, sizeof(sock_addr),
|
|
|
|
mysql->options.connect_timeout))
|
|
|
|
{
|
|
|
|
DBUG_PRINT("error",("Got error %d on connect to '%s'",socket_errno,
|
|
|
|
host));
|
|
|
|
net->last_errno= CR_CONN_HOST_ERROR;
|
2003-05-31 12:15:46 +02:00
|
|
|
strmov(net->sqlstate, unknown_sqlstate);
|
2005-01-09 02:19:42 +01:00
|
|
|
my_snprintf(net->last_error, sizeof(net->last_error)-1,
|
|
|
|
ER(CR_CONN_HOST_ERROR), host, socket_errno);
|
2003-05-02 18:07:41 +02:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
2003-06-14 10:37:42 +02:00
|
|
|
if (!net->vio)
|
2003-05-02 18:07:41 +02:00
|
|
|
{
|
|
|
|
DBUG_PRINT("error",("Unknow protocol %d ",mysql->options.protocol));
|
2004-03-12 13:21:48 +01:00
|
|
|
set_mysql_error(mysql, CR_CONN_UNKNOW_PROTOCOL, unknown_sqlstate);
|
2003-05-02 18:07:41 +02:00
|
|
|
goto error;
|
|
|
|
}
|
2003-06-14 10:37:42 +02:00
|
|
|
|
|
|
|
if (my_net_init(net, net->vio))
|
2003-05-02 18:07:41 +02:00
|
|
|
{
|
|
|
|
vio_delete(net->vio);
|
|
|
|
net->vio = 0;
|
2004-03-12 13:21:48 +01:00
|
|
|
set_mysql_error(mysql, CR_OUT_OF_MEMORY, unknown_sqlstate);
|
2003-05-02 18:07:41 +02:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
vio_keepalive(net->vio,TRUE);
|
2003-06-14 10:37:42 +02:00
|
|
|
/* Override local client variables */
|
|
|
|
if (mysql->options.read_timeout)
|
|
|
|
net->read_timeout= mysql->options.read_timeout;
|
|
|
|
if (mysql->options.write_timeout)
|
|
|
|
net->write_timeout= mysql->options.write_timeout;
|
|
|
|
if (mysql->options.max_allowed_packet)
|
|
|
|
net->max_packet_size= mysql->options.max_allowed_packet;
|
|
|
|
|
2003-05-02 18:07:41 +02:00
|
|
|
/* Get version info */
|
|
|
|
mysql->protocol_version= PROTOCOL_VERSION; /* Assume this */
|
|
|
|
if (mysql->options.connect_timeout &&
|
|
|
|
vio_poll_read(net->vio, mysql->options.connect_timeout))
|
|
|
|
{
|
2004-03-12 13:21:48 +01:00
|
|
|
set_mysql_error(mysql, CR_SERVER_LOST, unknown_sqlstate);
|
2003-05-02 18:07:41 +02:00
|
|
|
goto error;
|
|
|
|
}
|
2003-07-01 21:40:59 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
Part 1: Connection established, read and parse first packet
|
|
|
|
*/
|
|
|
|
|
A fix and a test case for Bug#15752 "Lost connection to MySQL server
when calling a SP from C API"
The bug was caused by lack of checks for misuse in mysql_real_query.
A stored procedure always returns at least one result, which is the
status of execution of the procedure itself.
This result, or so-called OK packet, is similar to a result
returned by INSERT/UPDATE/CREATE operations: it contains the overall
status of execution, the number of affected rows and the number of
warnings. The client test program attached to the bug did not read this
result and ivnoked the next query. In turn, libmysql had no check for
such scenario and mysql_real_query was simply trying to send that query
without reading the pending response, thus messing up the communication
protocol.
The fix is to return an error from mysql_real_query when it's called
prior to retrieval of all pending results.
client/mysqlbinlog.cc:
net_safe_read -> cli_safe_read
include/mysql.h:
Remove a private function from the public header.
include/mysql_com.h:
Remove a define that is never used.
include/sql_common.h:
Add a declaration for cli_safe_read - a function that reads one packet
from the server.
libmysql/libmysql.c:
net_safe_read -> cli_safe_read
Return CR_COMMANDS_OUT_OF_SYNC on attempt to execute a statement
using a connection which has pending result sets.
sql-common/client.c:
Actual fix for Bug#15752: if the server has pending result sets for
the client, return CR_COMMANDS_OUT_OF_SYNC on attempt to execute
another query. Similarly to the behaviour of mysql_use_result(),
multiple result sets block the connection and must be fetched
before it can be used for another query.
This uncovered an error in the protocol: the server doesn't drop
SERVER_MORE_RESULTS_EXISTS status flag upon an error, so in case of
a multi-query like SELECT 1; SELECT syntax_error; SELECT 2;
the client has no way to know that the server won't ever come to
execution of the third query and won't return any result sets for it.
For now, fix it in cli_safe_read, as a proper fix requires extension
of the client-server protocol.
sql/protocol.cc:
Remove a name that is never used.
sql/slave.cc:
net_safe_read -> cli_safe_read
tests/mysql_client_test.c:
Make 'query' a local variable to avoid name clash.
Add a test case for Bug#15752 "Lost connection to MySQL server when
calling an SP from C API"
2006-07-24 12:56:53 +02:00
|
|
|
if ((pkt_length=cli_safe_read(mysql)) == packet_error)
|
2003-05-02 18:07:41 +02:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
/* Check if version of protocol matches current one */
|
|
|
|
|
|
|
|
mysql->protocol_version= net->read_pos[0];
|
|
|
|
DBUG_DUMP("packet",(char*) net->read_pos,10);
|
|
|
|
DBUG_PRINT("info",("mysql protocol version %d, server=%d",
|
|
|
|
PROTOCOL_VERSION, mysql->protocol_version));
|
2003-06-14 10:37:42 +02:00
|
|
|
if (mysql->protocol_version != PROTOCOL_VERSION)
|
2003-05-02 18:07:41 +02:00
|
|
|
{
|
2003-06-14 10:37:42 +02:00
|
|
|
strmov(net->sqlstate, unknown_sqlstate);
|
2003-05-02 18:07:41 +02:00
|
|
|
net->last_errno= CR_VERSION_ERROR;
|
2005-01-09 02:19:42 +01:00
|
|
|
my_snprintf(net->last_error, sizeof(net->last_error)-1,
|
|
|
|
ER(CR_VERSION_ERROR), mysql->protocol_version,
|
|
|
|
PROTOCOL_VERSION);
|
2003-05-02 18:07:41 +02:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
end=strend((char*) net->read_pos+1);
|
|
|
|
mysql->thread_id=uint4korr(end+1);
|
|
|
|
end+=5;
|
2003-07-01 21:40:59 +02:00
|
|
|
/*
|
|
|
|
Scramble is split into two parts because old clients does not understand
|
|
|
|
long scrambles; here goes the first part.
|
|
|
|
*/
|
2003-07-18 16:25:54 +02:00
|
|
|
strmake(mysql->scramble, end, SCRAMBLE_LENGTH_323);
|
2003-07-01 21:40:59 +02:00
|
|
|
end+= SCRAMBLE_LENGTH_323+1;
|
|
|
|
|
2003-05-02 18:07:41 +02:00
|
|
|
if (pkt_length >= (uint) (end+1 - (char*) net->read_pos))
|
|
|
|
mysql->server_capabilities=uint2korr(end);
|
|
|
|
if (pkt_length >= (uint) (end+18 - (char*) net->read_pos))
|
|
|
|
{
|
|
|
|
/* New protocol with 16 bytes to describe server characteristics */
|
|
|
|
mysql->server_language=end[2];
|
|
|
|
mysql->server_status=uint2korr(end+3);
|
|
|
|
}
|
2003-07-01 21:40:59 +02:00
|
|
|
end+= 18;
|
|
|
|
if (pkt_length >= (uint) (end + SCRAMBLE_LENGTH - SCRAMBLE_LENGTH_323 + 1 -
|
|
|
|
(char *) net->read_pos))
|
|
|
|
strmake(mysql->scramble+SCRAMBLE_LENGTH_323, end,
|
|
|
|
SCRAMBLE_LENGTH-SCRAMBLE_LENGTH_323);
|
|
|
|
else
|
|
|
|
mysql->server_capabilities&= ~CLIENT_SECURE_CONNECTION;
|
2003-05-02 18:07:41 +02:00
|
|
|
|
2003-11-28 11:11:44 +01:00
|
|
|
if (mysql->options.secure_auth && passwd[0] &&
|
|
|
|
!(mysql->server_capabilities & CLIENT_SECURE_CONNECTION))
|
|
|
|
{
|
2004-03-12 13:21:48 +01:00
|
|
|
set_mysql_error(mysql, CR_SECURE_AUTH, unknown_sqlstate);
|
2003-11-28 11:11:44 +01:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2006-06-19 19:11:01 +02:00
|
|
|
if (mysql_init_character_set(mysql))
|
2004-06-07 14:28:31 +02:00
|
|
|
goto error;
|
2003-05-02 18:07:41 +02:00
|
|
|
|
|
|
|
/* Save connection information */
|
|
|
|
if (!my_multi_malloc(MYF(0),
|
|
|
|
&mysql->host_info, (uint) strlen(host_info)+1,
|
|
|
|
&mysql->host, (uint) strlen(host)+1,
|
|
|
|
&mysql->unix_socket,unix_socket ?
|
|
|
|
(uint) strlen(unix_socket)+1 : (uint) 1,
|
|
|
|
&mysql->server_version,
|
|
|
|
(uint) (end - (char*) net->read_pos),
|
|
|
|
NullS) ||
|
|
|
|
!(mysql->user=my_strdup(user,MYF(0))) ||
|
|
|
|
!(mysql->passwd=my_strdup(passwd,MYF(0))))
|
|
|
|
{
|
2004-03-12 13:21:48 +01:00
|
|
|
set_mysql_error(mysql, CR_OUT_OF_MEMORY, unknown_sqlstate);
|
2003-05-02 18:07:41 +02:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
strmov(mysql->host_info,host_info);
|
|
|
|
strmov(mysql->host,host);
|
|
|
|
if (unix_socket)
|
|
|
|
strmov(mysql->unix_socket,unix_socket);
|
|
|
|
else
|
|
|
|
mysql->unix_socket=0;
|
|
|
|
strmov(mysql->server_version,(char*) net->read_pos+1);
|
|
|
|
mysql->port=port;
|
|
|
|
|
2003-07-01 21:40:59 +02:00
|
|
|
/*
|
|
|
|
Part 2: format and send client info to the server for access check
|
|
|
|
*/
|
|
|
|
|
|
|
|
client_flag|=mysql->options.client_flag;
|
2003-05-02 18:07:41 +02:00
|
|
|
client_flag|=CLIENT_CAPABILITIES;
|
2003-11-18 12:47:27 +01:00
|
|
|
if (client_flag & CLIENT_MULTI_STATEMENTS)
|
2003-05-31 12:15:46 +02:00
|
|
|
client_flag|= CLIENT_MULTI_RESULTS;
|
2003-05-02 18:07:41 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_OPENSSL
|
|
|
|
if (mysql->options.ssl_key || mysql->options.ssl_cert ||
|
|
|
|
mysql->options.ssl_ca || mysql->options.ssl_capath ||
|
|
|
|
mysql->options.ssl_cipher)
|
|
|
|
mysql->options.use_ssl= 1;
|
|
|
|
if (mysql->options.use_ssl)
|
|
|
|
client_flag|=CLIENT_SSL;
|
|
|
|
#endif /* HAVE_OPENSSL */
|
|
|
|
if (db)
|
|
|
|
client_flag|=CLIENT_CONNECT_WITH_DB;
|
|
|
|
|
|
|
|
/* Remove options that server doesn't support */
|
|
|
|
client_flag= ((client_flag &
|
|
|
|
~(CLIENT_COMPRESS | CLIENT_SSL | CLIENT_PROTOCOL_41)) |
|
|
|
|
(client_flag & mysql->server_capabilities));
|
|
|
|
#ifndef HAVE_COMPRESS
|
|
|
|
client_flag&= ~CLIENT_COMPRESS;
|
|
|
|
#endif
|
2003-06-14 10:37:42 +02:00
|
|
|
|
2003-05-02 18:07:41 +02:00
|
|
|
if (client_flag & CLIENT_PROTOCOL_41)
|
|
|
|
{
|
2003-05-31 12:15:46 +02:00
|
|
|
/* 4.1 server and 4.1 client has a 32 byte option flag */
|
2003-05-02 18:07:41 +02:00
|
|
|
int4store(buff,client_flag);
|
2003-06-14 10:37:42 +02:00
|
|
|
int4store(buff+4, net->max_packet_size);
|
2004-02-04 09:49:54 +01:00
|
|
|
buff[8]= (char) mysql->charset->number;
|
2003-05-31 12:15:46 +02:00
|
|
|
bzero(buff+9, 32-9);
|
|
|
|
end= buff+32;
|
2003-05-02 18:07:41 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int2store(buff,client_flag);
|
2003-06-14 10:37:42 +02:00
|
|
|
int3store(buff+2,net->max_packet_size);
|
2003-05-02 18:07:41 +02:00
|
|
|
end= buff+5;
|
|
|
|
}
|
|
|
|
mysql->client_flag=client_flag;
|
|
|
|
|
|
|
|
#ifdef HAVE_OPENSSL
|
|
|
|
if (client_flag & CLIENT_SSL)
|
|
|
|
{
|
2006-03-10 16:41:14 +01:00
|
|
|
/* Do the SSL layering. */
|
2003-05-02 18:07:41 +02:00
|
|
|
struct st_mysql_options *options= &mysql->options;
|
2006-03-10 16:41:14 +01:00
|
|
|
struct st_VioSSLFd *ssl_fd;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Send client_flag, max_packet_size - unencrypted otherwise
|
|
|
|
the server does not know we want to do SSL
|
|
|
|
*/
|
2003-05-02 18:07:41 +02:00
|
|
|
if (my_net_write(net,buff,(uint) (end-buff)) || net_flush(net))
|
|
|
|
{
|
2004-03-12 13:21:48 +01:00
|
|
|
set_mysql_error(mysql, CR_SERVER_LOST, unknown_sqlstate);
|
2003-05-02 18:07:41 +02:00
|
|
|
goto error;
|
|
|
|
}
|
2006-03-10 16:41:14 +01:00
|
|
|
|
|
|
|
/* Create the VioSSLConnectorFd - init SSL and load certs */
|
|
|
|
if (!(ssl_fd= new_VioSSLConnectorFd(options->ssl_key,
|
|
|
|
options->ssl_cert,
|
|
|
|
options->ssl_ca,
|
|
|
|
options->ssl_capath,
|
|
|
|
options->ssl_cipher)))
|
2003-05-02 18:07:41 +02:00
|
|
|
{
|
2004-03-12 13:21:48 +01:00
|
|
|
set_mysql_error(mysql, CR_SSL_CONNECTION_ERROR, unknown_sqlstate);
|
2003-05-02 18:07:41 +02:00
|
|
|
goto error;
|
|
|
|
}
|
2006-03-10 16:41:14 +01:00
|
|
|
mysql->connector_fd= (void*)ssl_fd;
|
|
|
|
|
|
|
|
/* Connect to the server */
|
2003-05-02 18:07:41 +02:00
|
|
|
DBUG_PRINT("info", ("IO layer change in progress..."));
|
2006-03-10 16:41:14 +01:00
|
|
|
if (sslconnect(ssl_fd, mysql->net.vio,
|
|
|
|
(long) (mysql->options.connect_timeout)))
|
2003-05-02 18:07:41 +02:00
|
|
|
{
|
2004-03-12 13:21:48 +01:00
|
|
|
set_mysql_error(mysql, CR_SSL_CONNECTION_ERROR, unknown_sqlstate);
|
2003-05-02 18:07:41 +02:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
DBUG_PRINT("info", ("IO layer change done!"));
|
2006-03-10 16:41:14 +01:00
|
|
|
|
2006-04-18 17:58:27 +02:00
|
|
|
/* Verify server cert */
|
2006-08-25 17:54:33 +02:00
|
|
|
if ((client_flag & CLIENT_SSL_VERIFY_SERVER_CERT) &&
|
2006-04-18 17:58:27 +02:00
|
|
|
ssl_verify_server_cert(mysql->net.vio, mysql->host))
|
|
|
|
{
|
|
|
|
set_mysql_error(mysql, CR_SSL_CONNECTION_ERROR, unknown_sqlstate);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2003-05-02 18:07:41 +02:00
|
|
|
}
|
|
|
|
#endif /* HAVE_OPENSSL */
|
|
|
|
|
|
|
|
DBUG_PRINT("info",("Server version = '%s' capabilites: %lu status: %u client_flag: %lu",
|
|
|
|
mysql->server_version,mysql->server_capabilities,
|
|
|
|
mysql->server_status, client_flag));
|
|
|
|
/* This needs to be changed as it's not useful with big packets */
|
|
|
|
if (user && user[0])
|
2006-09-27 14:49:16 +02:00
|
|
|
strmake(end,user,USERNAME_LENGTH); /* Max user name */
|
2003-05-02 18:07:41 +02:00
|
|
|
else
|
|
|
|
read_user_name((char*) end);
|
2003-06-14 10:37:42 +02:00
|
|
|
|
2003-05-02 18:07:41 +02:00
|
|
|
/* We have to handle different version of handshake here */
|
2003-06-14 10:37:42 +02:00
|
|
|
#ifdef _CUSTOMCONFIG_
|
|
|
|
#include "_cust_libmysql.h"
|
2003-05-02 18:07:41 +02:00
|
|
|
#endif
|
|
|
|
DBUG_PRINT("info",("user: %s",end));
|
2003-07-01 21:40:59 +02:00
|
|
|
end= strend(end) + 1;
|
|
|
|
if (passwd[0])
|
2003-05-02 18:07:41 +02:00
|
|
|
{
|
2003-07-04 18:52:04 +02:00
|
|
|
if (mysql->server_capabilities & CLIENT_SECURE_CONNECTION)
|
2003-05-02 18:07:41 +02:00
|
|
|
{
|
2003-07-04 18:52:04 +02:00
|
|
|
*end++= SCRAMBLE_LENGTH;
|
|
|
|
scramble(end, mysql->scramble, passwd);
|
|
|
|
end+= SCRAMBLE_LENGTH;
|
2003-05-02 18:07:41 +02:00
|
|
|
}
|
2003-07-04 18:52:04 +02:00
|
|
|
else
|
2003-05-02 18:07:41 +02:00
|
|
|
{
|
2003-07-18 16:25:54 +02:00
|
|
|
scramble_323(end, mysql->scramble, passwd);
|
|
|
|
end+= SCRAMBLE_LENGTH_323 + 1;
|
2003-05-02 18:07:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2003-07-04 18:52:04 +02:00
|
|
|
*end++= '\0'; /* empty password */
|
2003-07-01 21:40:59 +02:00
|
|
|
|
2003-05-02 18:07:41 +02:00
|
|
|
/* Add database if needed */
|
|
|
|
if (db && (mysql->server_capabilities & CLIENT_CONNECT_WITH_DB))
|
|
|
|
{
|
2006-09-27 14:49:16 +02:00
|
|
|
end= strmake(end, db, NAME_LEN) + 1;
|
2003-07-04 18:52:04 +02:00
|
|
|
mysql->db= my_strdup(db,MYF(MY_WME));
|
|
|
|
db= 0;
|
2003-05-02 18:07:41 +02:00
|
|
|
}
|
|
|
|
/* Write authentication package */
|
|
|
|
if (my_net_write(net,buff,(ulong) (end-buff)) || net_flush(net))
|
|
|
|
{
|
2004-03-12 13:21:48 +01:00
|
|
|
set_mysql_error(mysql, CR_SERVER_LOST, unknown_sqlstate);
|
2003-05-02 18:07:41 +02:00
|
|
|
goto error;
|
|
|
|
}
|
2003-07-01 21:40:59 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
Part 3: Authorization data's been sent. Now server can reply with
|
|
|
|
OK-packet, or re-request scrambled password.
|
|
|
|
*/
|
2003-05-02 18:07:41 +02:00
|
|
|
|
A fix and a test case for Bug#15752 "Lost connection to MySQL server
when calling a SP from C API"
The bug was caused by lack of checks for misuse in mysql_real_query.
A stored procedure always returns at least one result, which is the
status of execution of the procedure itself.
This result, or so-called OK packet, is similar to a result
returned by INSERT/UPDATE/CREATE operations: it contains the overall
status of execution, the number of affected rows and the number of
warnings. The client test program attached to the bug did not read this
result and ivnoked the next query. In turn, libmysql had no check for
such scenario and mysql_real_query was simply trying to send that query
without reading the pending response, thus messing up the communication
protocol.
The fix is to return an error from mysql_real_query when it's called
prior to retrieval of all pending results.
client/mysqlbinlog.cc:
net_safe_read -> cli_safe_read
include/mysql.h:
Remove a private function from the public header.
include/mysql_com.h:
Remove a define that is never used.
include/sql_common.h:
Add a declaration for cli_safe_read - a function that reads one packet
from the server.
libmysql/libmysql.c:
net_safe_read -> cli_safe_read
Return CR_COMMANDS_OUT_OF_SYNC on attempt to execute a statement
using a connection which has pending result sets.
sql-common/client.c:
Actual fix for Bug#15752: if the server has pending result sets for
the client, return CR_COMMANDS_OUT_OF_SYNC on attempt to execute
another query. Similarly to the behaviour of mysql_use_result(),
multiple result sets block the connection and must be fetched
before it can be used for another query.
This uncovered an error in the protocol: the server doesn't drop
SERVER_MORE_RESULTS_EXISTS status flag upon an error, so in case of
a multi-query like SELECT 1; SELECT syntax_error; SELECT 2;
the client has no way to know that the server won't ever come to
execution of the third query and won't return any result sets for it.
For now, fix it in cli_safe_read, as a proper fix requires extension
of the client-server protocol.
sql/protocol.cc:
Remove a name that is never used.
sql/slave.cc:
net_safe_read -> cli_safe_read
tests/mysql_client_test.c:
Make 'query' a local variable to avoid name clash.
Add a test case for Bug#15752 "Lost connection to MySQL server when
calling an SP from C API"
2006-07-24 12:56:53 +02:00
|
|
|
if ((pkt_length=cli_safe_read(mysql)) == packet_error)
|
2003-05-02 18:07:41 +02:00
|
|
|
goto error;
|
|
|
|
|
2003-07-18 16:25:54 +02:00
|
|
|
if (pkt_length == 1 && net->read_pos[0] == 254 &&
|
2003-07-01 21:40:59 +02:00
|
|
|
mysql->server_capabilities & CLIENT_SECURE_CONNECTION)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
By sending this very specific reply server asks us to send scrambled
|
2003-07-31 13:47:00 +02:00
|
|
|
password in old format.
|
2003-07-01 21:40:59 +02:00
|
|
|
*/
|
2003-07-18 16:25:54 +02:00
|
|
|
scramble_323(buff, mysql->scramble, passwd);
|
2003-07-01 21:40:59 +02:00
|
|
|
if (my_net_write(net, buff, SCRAMBLE_LENGTH_323 + 1) || net_flush(net))
|
|
|
|
{
|
2004-03-12 13:21:48 +01:00
|
|
|
set_mysql_error(mysql, CR_SERVER_LOST, unknown_sqlstate);
|
2003-07-01 21:40:59 +02:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
/* Read what server thinks about out new auth message report */
|
A fix and a test case for Bug#15752 "Lost connection to MySQL server
when calling a SP from C API"
The bug was caused by lack of checks for misuse in mysql_real_query.
A stored procedure always returns at least one result, which is the
status of execution of the procedure itself.
This result, or so-called OK packet, is similar to a result
returned by INSERT/UPDATE/CREATE operations: it contains the overall
status of execution, the number of affected rows and the number of
warnings. The client test program attached to the bug did not read this
result and ivnoked the next query. In turn, libmysql had no check for
such scenario and mysql_real_query was simply trying to send that query
without reading the pending response, thus messing up the communication
protocol.
The fix is to return an error from mysql_real_query when it's called
prior to retrieval of all pending results.
client/mysqlbinlog.cc:
net_safe_read -> cli_safe_read
include/mysql.h:
Remove a private function from the public header.
include/mysql_com.h:
Remove a define that is never used.
include/sql_common.h:
Add a declaration for cli_safe_read - a function that reads one packet
from the server.
libmysql/libmysql.c:
net_safe_read -> cli_safe_read
Return CR_COMMANDS_OUT_OF_SYNC on attempt to execute a statement
using a connection which has pending result sets.
sql-common/client.c:
Actual fix for Bug#15752: if the server has pending result sets for
the client, return CR_COMMANDS_OUT_OF_SYNC on attempt to execute
another query. Similarly to the behaviour of mysql_use_result(),
multiple result sets block the connection and must be fetched
before it can be used for another query.
This uncovered an error in the protocol: the server doesn't drop
SERVER_MORE_RESULTS_EXISTS status flag upon an error, so in case of
a multi-query like SELECT 1; SELECT syntax_error; SELECT 2;
the client has no way to know that the server won't ever come to
execution of the third query and won't return any result sets for it.
For now, fix it in cli_safe_read, as a proper fix requires extension
of the client-server protocol.
sql/protocol.cc:
Remove a name that is never used.
sql/slave.cc:
net_safe_read -> cli_safe_read
tests/mysql_client_test.c:
Make 'query' a local variable to avoid name clash.
Add a test case for Bug#15752 "Lost connection to MySQL server when
calling an SP from C API"
2006-07-24 12:56:53 +02:00
|
|
|
if (cli_safe_read(mysql) == packet_error)
|
2003-07-01 21:40:59 +02:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2003-05-02 18:07:41 +02:00
|
|
|
if (client_flag & CLIENT_COMPRESS) /* We will use compression */
|
|
|
|
net->compress=1;
|
|
|
|
|
2004-06-03 00:55:47 +02:00
|
|
|
#ifdef CHECK_LICENSE
|
|
|
|
if (check_license(mysql))
|
|
|
|
goto error;
|
|
|
|
#endif
|
|
|
|
|
2003-05-02 18:07:41 +02:00
|
|
|
if (db && mysql_select_db(mysql,db))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (mysql->options.init_commands)
|
|
|
|
{
|
|
|
|
DYNAMIC_ARRAY *init_commands= mysql->options.init_commands;
|
|
|
|
char **ptr= (char**)init_commands->buffer;
|
|
|
|
char **end= ptr + init_commands->elements;
|
|
|
|
|
|
|
|
my_bool reconnect=mysql->reconnect;
|
|
|
|
mysql->reconnect=0;
|
|
|
|
|
|
|
|
for (; ptr<end; ptr++)
|
|
|
|
{
|
|
|
|
MYSQL_RES *res;
|
2005-06-13 12:41:15 +02:00
|
|
|
if (mysql_real_query(mysql,*ptr, (ulong) strlen(*ptr)))
|
2003-05-02 18:07:41 +02:00
|
|
|
goto error;
|
|
|
|
if (mysql->fields)
|
|
|
|
{
|
2003-12-22 13:57:34 +01:00
|
|
|
if (!(res= cli_use_result(mysql)))
|
2003-05-02 18:07:41 +02:00
|
|
|
goto error;
|
|
|
|
mysql_free_result(res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mysql->reconnect=reconnect;
|
|
|
|
}
|
|
|
|
|
2003-06-14 10:37:42 +02:00
|
|
|
#ifndef TO_BE_DELETED
|
2003-05-02 18:07:41 +02:00
|
|
|
if (mysql->options.rpl_probe && mysql_rpl_probe(mysql))
|
|
|
|
goto error;
|
2003-06-14 10:37:42 +02:00
|
|
|
#endif
|
2003-05-02 18:07:41 +02:00
|
|
|
|
2006-11-20 21:42:06 +01:00
|
|
|
DBUG_PRINT("exit", ("Mysql handler: 0x%lx", (long) mysql));
|
2003-05-02 18:07:41 +02:00
|
|
|
reset_sigpipe(mysql);
|
|
|
|
DBUG_RETURN(mysql);
|
|
|
|
|
|
|
|
error:
|
|
|
|
reset_sigpipe(mysql);
|
2003-05-31 12:15:46 +02:00
|
|
|
DBUG_PRINT("error",("message: %u/%s (%s)",
|
|
|
|
net->last_errno, net->sqlstate, net->last_error));
|
2003-05-02 18:07:41 +02:00
|
|
|
{
|
|
|
|
/* Free alloced memory */
|
|
|
|
end_server(mysql);
|
2003-06-14 10:37:42 +02:00
|
|
|
mysql_close_free(mysql);
|
2004-04-05 12:56:05 +02:00
|
|
|
if (!(((ulong) client_flag) & CLIENT_REMEMBER_OPTIONS))
|
2003-06-14 10:37:42 +02:00
|
|
|
mysql_close_free_options(mysql);
|
2003-05-02 18:07:41 +02:00
|
|
|
}
|
|
|
|
DBUG_RETURN(0);
|
|
|
|
}
|
|
|
|
|
2003-06-14 10:37:42 +02:00
|
|
|
|
2003-05-02 18:07:41 +02:00
|
|
|
/* needed when we move MYSQL structure to a different address */
|
|
|
|
|
2003-06-14 10:37:42 +02:00
|
|
|
#ifndef TO_BE_DELETED
|
2003-05-02 18:07:41 +02:00
|
|
|
static void mysql_fix_pointers(MYSQL* mysql, MYSQL* old_mysql)
|
|
|
|
{
|
2003-06-14 10:37:42 +02:00
|
|
|
MYSQL *tmp, *tmp_prev;
|
2003-05-02 18:07:41 +02:00
|
|
|
if (mysql->master == old_mysql)
|
2003-06-14 10:37:42 +02:00
|
|
|
mysql->master= mysql;
|
2003-05-02 18:07:41 +02:00
|
|
|
if (mysql->last_used_con == old_mysql)
|
2003-06-14 10:37:42 +02:00
|
|
|
mysql->last_used_con= mysql;
|
2003-05-02 18:07:41 +02:00
|
|
|
if (mysql->last_used_slave == old_mysql)
|
2003-06-14 10:37:42 +02:00
|
|
|
mysql->last_used_slave= mysql;
|
|
|
|
for (tmp_prev = mysql, tmp = mysql->next_slave;
|
|
|
|
tmp != old_mysql;tmp = tmp->next_slave)
|
2003-05-02 18:07:41 +02:00
|
|
|
{
|
2003-06-14 10:37:42 +02:00
|
|
|
tmp_prev= tmp;
|
2003-05-02 18:07:41 +02:00
|
|
|
}
|
2003-06-14 10:37:42 +02:00
|
|
|
tmp_prev->next_slave= mysql;
|
2003-06-03 12:02:57 +02:00
|
|
|
}
|
2003-06-14 10:37:42 +02:00
|
|
|
#endif
|
2003-05-02 18:07:41 +02:00
|
|
|
|
2003-06-05 22:19:56 +02:00
|
|
|
|
2003-05-02 18:07:41 +02:00
|
|
|
my_bool mysql_reconnect(MYSQL *mysql)
|
|
|
|
{
|
|
|
|
MYSQL tmp_mysql;
|
|
|
|
DBUG_ENTER("mysql_reconnect");
|
2006-10-12 10:27:44 +02:00
|
|
|
DBUG_ASSERT(mysql);
|
|
|
|
DBUG_PRINT("enter", ("mysql->reconnect: %d", mysql->reconnect));
|
2003-05-02 18:07:41 +02:00
|
|
|
|
2003-06-05 22:19:56 +02:00
|
|
|
if (!mysql->reconnect ||
|
|
|
|
(mysql->server_status & SERVER_STATUS_IN_TRANS) || !mysql->host_info)
|
2003-05-02 18:07:41 +02:00
|
|
|
{
|
2003-06-05 22:19:56 +02:00
|
|
|
/* Allow reconnect next time */
|
2003-05-02 18:07:41 +02:00
|
|
|
mysql->server_status&= ~SERVER_STATUS_IN_TRANS;
|
2004-03-12 13:21:48 +01:00
|
|
|
set_mysql_error(mysql, CR_SERVER_GONE_ERROR, unknown_sqlstate);
|
2003-05-02 18:07:41 +02:00
|
|
|
DBUG_RETURN(1);
|
|
|
|
}
|
|
|
|
mysql_init(&tmp_mysql);
|
Added option --valgrind-mysqltest to mysql-test-run
Added flag to Field::store(longlong) to specify if value is unsigned.
This fixes bug #12750: Incorrect storage of 9999999999999999999 in DECIMAL(19, 0)
Fixed warning from valgrind in CREATE ... SELECT
Fixed double free of mysql.options if reconnect failed
mysql-test/mysql-test-run.sh:
Added option --valgrind-mysqltest to allow one to run mysqltest with valgrind
mysql-test/r/bigint.result:
Update results after fix for Field::store(longlong)
mysql-test/r/range.result:
Update results after fix for Field::store(longlong)
mysql-test/r/strict.result:
Update results after fix for Field::store(longlong)
(This fixes some wrong results when storing things into bigint columns)
mysql-test/r/type_ranges.result:
Update results after fix for Field::store(longlong)
mysql-test/t/bigint.test:
Added testing for #12750: Incorrect storage of 9999999999999999999 in DECIMAL(19, 0)
mysql-test/t/innodb.test:
Removed comments affected by this bug fix
mysql-test/t/mysqldump.test:
Fixed result to not depend on existing config files
mysql-test/t/range.test:
0xff numbers are now unsigned
mysql-test/t/strict.test:
Added errors for things that previously (wrongly) succeeded
sql-common/client.c:
Fixed double free of mysql.options if reconnect failed
sql/field.cc:
Added flag to Field::store(longlong) to specify if value is unsigned
sql/field.h:
Added flag to Field::store(longlong) to specify if value is unsigned
sql/field_conv.cc:
Fixed calls to Field::store(longlong,flag)
sql/ha_ndbcluster.cc:
Fixed calls to Field::store(longlong,flag)
sql/handler.cc:
Fixed calls to Field::store(longlong,flag)
sql/item.cc:
Fixed calls to Field::store(longlong,flag)
sql/item_sum.cc:
Fixed calls to Field::store(longlong,flag)
sql/sp.cc:
Fixed calls to Field::store(longlong,flag)
sql/sql_acl.cc:
Fixed calls to Field::store(longlong,flag)
sql/sql_help.cc:
Fixed calls to Field::store(longlong,flag)
sql/sql_show.cc:
Fixed calls to Field::store(longlong,flag)
sql/sql_table.cc:
Fixed varning from valgrind
sql/sql_udf.cc:
Fixed calls to Field::store(longlong,flag)
sql/tztime.cc:
Fixed calls to Field::store(longlong,flag)
sql/unireg.cc:
Fixed calls to Field::store(longlong,flag)
2005-09-14 00:41:44 +02:00
|
|
|
tmp_mysql.options= mysql->options;
|
|
|
|
tmp_mysql.rpl_pivot= mysql->rpl_pivot;
|
|
|
|
|
2003-05-02 18:07:41 +02:00
|
|
|
if (!mysql_real_connect(&tmp_mysql,mysql->host,mysql->user,mysql->passwd,
|
|
|
|
mysql->db, mysql->port, mysql->unix_socket,
|
2005-07-28 15:10:14 +02:00
|
|
|
mysql->client_flag | CLIENT_REMEMBER_OPTIONS))
|
2003-05-02 18:07:41 +02:00
|
|
|
{
|
|
|
|
mysql->net.last_errno= tmp_mysql.net.last_errno;
|
|
|
|
strmov(mysql->net.last_error, tmp_mysql.net.last_error);
|
2003-06-14 10:37:42 +02:00
|
|
|
strmov(mysql->net.sqlstate, tmp_mysql.net.sqlstate);
|
2003-05-02 18:07:41 +02:00
|
|
|
DBUG_RETURN(1);
|
|
|
|
}
|
2005-07-28 15:10:14 +02:00
|
|
|
if (mysql_set_character_set(&tmp_mysql, mysql->charset->csname))
|
|
|
|
{
|
Added option --valgrind-mysqltest to mysql-test-run
Added flag to Field::store(longlong) to specify if value is unsigned.
This fixes bug #12750: Incorrect storage of 9999999999999999999 in DECIMAL(19, 0)
Fixed warning from valgrind in CREATE ... SELECT
Fixed double free of mysql.options if reconnect failed
mysql-test/mysql-test-run.sh:
Added option --valgrind-mysqltest to allow one to run mysqltest with valgrind
mysql-test/r/bigint.result:
Update results after fix for Field::store(longlong)
mysql-test/r/range.result:
Update results after fix for Field::store(longlong)
mysql-test/r/strict.result:
Update results after fix for Field::store(longlong)
(This fixes some wrong results when storing things into bigint columns)
mysql-test/r/type_ranges.result:
Update results after fix for Field::store(longlong)
mysql-test/t/bigint.test:
Added testing for #12750: Incorrect storage of 9999999999999999999 in DECIMAL(19, 0)
mysql-test/t/innodb.test:
Removed comments affected by this bug fix
mysql-test/t/mysqldump.test:
Fixed result to not depend on existing config files
mysql-test/t/range.test:
0xff numbers are now unsigned
mysql-test/t/strict.test:
Added errors for things that previously (wrongly) succeeded
sql-common/client.c:
Fixed double free of mysql.options if reconnect failed
sql/field.cc:
Added flag to Field::store(longlong) to specify if value is unsigned
sql/field.h:
Added flag to Field::store(longlong) to specify if value is unsigned
sql/field_conv.cc:
Fixed calls to Field::store(longlong,flag)
sql/ha_ndbcluster.cc:
Fixed calls to Field::store(longlong,flag)
sql/handler.cc:
Fixed calls to Field::store(longlong,flag)
sql/item.cc:
Fixed calls to Field::store(longlong,flag)
sql/item_sum.cc:
Fixed calls to Field::store(longlong,flag)
sql/sp.cc:
Fixed calls to Field::store(longlong,flag)
sql/sql_acl.cc:
Fixed calls to Field::store(longlong,flag)
sql/sql_help.cc:
Fixed calls to Field::store(longlong,flag)
sql/sql_show.cc:
Fixed calls to Field::store(longlong,flag)
sql/sql_table.cc:
Fixed varning from valgrind
sql/sql_udf.cc:
Fixed calls to Field::store(longlong,flag)
sql/tztime.cc:
Fixed calls to Field::store(longlong,flag)
sql/unireg.cc:
Fixed calls to Field::store(longlong,flag)
2005-09-14 00:41:44 +02:00
|
|
|
DBUG_PRINT("error", ("mysql_set_character_set() failed"));
|
|
|
|
bzero((char*) &tmp_mysql.options,sizeof(tmp_mysql.options));
|
2005-07-28 15:10:14 +02:00
|
|
|
mysql_close(&tmp_mysql);
|
|
|
|
mysql->net.last_errno= tmp_mysql.net.last_errno;
|
|
|
|
strmov(mysql->net.last_error, tmp_mysql.net.last_error);
|
|
|
|
strmov(mysql->net.sqlstate, tmp_mysql.net.sqlstate);
|
|
|
|
DBUG_RETURN(1);
|
|
|
|
}
|
|
|
|
|
Added option --valgrind-mysqltest to mysql-test-run
Added flag to Field::store(longlong) to specify if value is unsigned.
This fixes bug #12750: Incorrect storage of 9999999999999999999 in DECIMAL(19, 0)
Fixed warning from valgrind in CREATE ... SELECT
Fixed double free of mysql.options if reconnect failed
mysql-test/mysql-test-run.sh:
Added option --valgrind-mysqltest to allow one to run mysqltest with valgrind
mysql-test/r/bigint.result:
Update results after fix for Field::store(longlong)
mysql-test/r/range.result:
Update results after fix for Field::store(longlong)
mysql-test/r/strict.result:
Update results after fix for Field::store(longlong)
(This fixes some wrong results when storing things into bigint columns)
mysql-test/r/type_ranges.result:
Update results after fix for Field::store(longlong)
mysql-test/t/bigint.test:
Added testing for #12750: Incorrect storage of 9999999999999999999 in DECIMAL(19, 0)
mysql-test/t/innodb.test:
Removed comments affected by this bug fix
mysql-test/t/mysqldump.test:
Fixed result to not depend on existing config files
mysql-test/t/range.test:
0xff numbers are now unsigned
mysql-test/t/strict.test:
Added errors for things that previously (wrongly) succeeded
sql-common/client.c:
Fixed double free of mysql.options if reconnect failed
sql/field.cc:
Added flag to Field::store(longlong) to specify if value is unsigned
sql/field.h:
Added flag to Field::store(longlong) to specify if value is unsigned
sql/field_conv.cc:
Fixed calls to Field::store(longlong,flag)
sql/ha_ndbcluster.cc:
Fixed calls to Field::store(longlong,flag)
sql/handler.cc:
Fixed calls to Field::store(longlong,flag)
sql/item.cc:
Fixed calls to Field::store(longlong,flag)
sql/item_sum.cc:
Fixed calls to Field::store(longlong,flag)
sql/sp.cc:
Fixed calls to Field::store(longlong,flag)
sql/sql_acl.cc:
Fixed calls to Field::store(longlong,flag)
sql/sql_help.cc:
Fixed calls to Field::store(longlong,flag)
sql/sql_show.cc:
Fixed calls to Field::store(longlong,flag)
sql/sql_table.cc:
Fixed varning from valgrind
sql/sql_udf.cc:
Fixed calls to Field::store(longlong,flag)
sql/tztime.cc:
Fixed calls to Field::store(longlong,flag)
sql/unireg.cc:
Fixed calls to Field::store(longlong,flag)
2005-09-14 00:41:44 +02:00
|
|
|
DBUG_PRINT("info", ("reconnect succeded"));
|
2004-12-09 14:44:10 +01:00
|
|
|
tmp_mysql.reconnect= 1;
|
2003-06-24 11:10:35 +02:00
|
|
|
tmp_mysql.free_me= mysql->free_me;
|
2005-03-28 19:59:41 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
For each stmt in mysql->stmts, move it to tmp_mysql if it is
|
|
|
|
in state MYSQL_STMT_INIT_DONE, otherwise close it.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
LIST *element= mysql->stmts;
|
|
|
|
for (; element; element= element->next)
|
|
|
|
{
|
|
|
|
MYSQL_STMT *stmt= (MYSQL_STMT *) element->data;
|
|
|
|
if (stmt->state != MYSQL_STMT_INIT_DONE)
|
|
|
|
{
|
|
|
|
stmt->mysql= 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tmp_mysql.stmts= list_add(tmp_mysql.stmts, &stmt->list);
|
|
|
|
}
|
|
|
|
/* No need to call list_delete for statement here */
|
|
|
|
}
|
|
|
|
mysql->stmts= NULL;
|
|
|
|
}
|
|
|
|
|
2003-06-24 11:10:35 +02:00
|
|
|
/* Don't free options as these are now used in tmp_mysql */
|
|
|
|
bzero((char*) &mysql->options,sizeof(mysql->options));
|
2003-05-02 18:07:41 +02:00
|
|
|
mysql->free_me=0;
|
|
|
|
mysql_close(mysql);
|
|
|
|
*mysql=tmp_mysql;
|
|
|
|
mysql_fix_pointers(mysql, &tmp_mysql); /* adjust connection pointers */
|
|
|
|
net_clear(&mysql->net);
|
|
|
|
mysql->affected_rows= ~(my_ulonglong) 0;
|
|
|
|
DBUG_RETURN(0);
|
|
|
|
}
|
|
|
|
|
2003-06-14 10:37:42 +02:00
|
|
|
|
2003-05-02 18:07:41 +02:00
|
|
|
/**************************************************************************
|
|
|
|
Set current database
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
int STDCALL
|
|
|
|
mysql_select_db(MYSQL *mysql, const char *db)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
DBUG_ENTER("mysql_select_db");
|
|
|
|
DBUG_PRINT("enter",("db: '%s'",db));
|
|
|
|
|
|
|
|
if ((error=simple_command(mysql,COM_INIT_DB,db,(ulong) strlen(db),0)))
|
|
|
|
DBUG_RETURN(error);
|
|
|
|
my_free(mysql->db,MYF(MY_ALLOW_ZERO_PTR));
|
|
|
|
mysql->db=my_strdup(db,MYF(MY_WME));
|
|
|
|
DBUG_RETURN(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*************************************************************************
|
|
|
|
Send a QUIT to the server and close the connection
|
|
|
|
If handle is alloced by mysql connect free it.
|
|
|
|
*************************************************************************/
|
|
|
|
|
2003-06-14 10:37:42 +02:00
|
|
|
static void mysql_close_free_options(MYSQL *mysql)
|
|
|
|
{
|
Added option --valgrind-mysqltest to mysql-test-run
Added flag to Field::store(longlong) to specify if value is unsigned.
This fixes bug #12750: Incorrect storage of 9999999999999999999 in DECIMAL(19, 0)
Fixed warning from valgrind in CREATE ... SELECT
Fixed double free of mysql.options if reconnect failed
mysql-test/mysql-test-run.sh:
Added option --valgrind-mysqltest to allow one to run mysqltest with valgrind
mysql-test/r/bigint.result:
Update results after fix for Field::store(longlong)
mysql-test/r/range.result:
Update results after fix for Field::store(longlong)
mysql-test/r/strict.result:
Update results after fix for Field::store(longlong)
(This fixes some wrong results when storing things into bigint columns)
mysql-test/r/type_ranges.result:
Update results after fix for Field::store(longlong)
mysql-test/t/bigint.test:
Added testing for #12750: Incorrect storage of 9999999999999999999 in DECIMAL(19, 0)
mysql-test/t/innodb.test:
Removed comments affected by this bug fix
mysql-test/t/mysqldump.test:
Fixed result to not depend on existing config files
mysql-test/t/range.test:
0xff numbers are now unsigned
mysql-test/t/strict.test:
Added errors for things that previously (wrongly) succeeded
sql-common/client.c:
Fixed double free of mysql.options if reconnect failed
sql/field.cc:
Added flag to Field::store(longlong) to specify if value is unsigned
sql/field.h:
Added flag to Field::store(longlong) to specify if value is unsigned
sql/field_conv.cc:
Fixed calls to Field::store(longlong,flag)
sql/ha_ndbcluster.cc:
Fixed calls to Field::store(longlong,flag)
sql/handler.cc:
Fixed calls to Field::store(longlong,flag)
sql/item.cc:
Fixed calls to Field::store(longlong,flag)
sql/item_sum.cc:
Fixed calls to Field::store(longlong,flag)
sql/sp.cc:
Fixed calls to Field::store(longlong,flag)
sql/sql_acl.cc:
Fixed calls to Field::store(longlong,flag)
sql/sql_help.cc:
Fixed calls to Field::store(longlong,flag)
sql/sql_show.cc:
Fixed calls to Field::store(longlong,flag)
sql/sql_table.cc:
Fixed varning from valgrind
sql/sql_udf.cc:
Fixed calls to Field::store(longlong,flag)
sql/tztime.cc:
Fixed calls to Field::store(longlong,flag)
sql/unireg.cc:
Fixed calls to Field::store(longlong,flag)
2005-09-14 00:41:44 +02:00
|
|
|
DBUG_ENTER("mysql_close_free_options");
|
|
|
|
|
2003-06-14 10:37:42 +02:00
|
|
|
my_free(mysql->options.user,MYF(MY_ALLOW_ZERO_PTR));
|
|
|
|
my_free(mysql->options.host,MYF(MY_ALLOW_ZERO_PTR));
|
|
|
|
my_free(mysql->options.password,MYF(MY_ALLOW_ZERO_PTR));
|
|
|
|
my_free(mysql->options.unix_socket,MYF(MY_ALLOW_ZERO_PTR));
|
|
|
|
my_free(mysql->options.db,MYF(MY_ALLOW_ZERO_PTR));
|
|
|
|
my_free(mysql->options.my_cnf_file,MYF(MY_ALLOW_ZERO_PTR));
|
|
|
|
my_free(mysql->options.my_cnf_group,MYF(MY_ALLOW_ZERO_PTR));
|
|
|
|
my_free(mysql->options.charset_dir,MYF(MY_ALLOW_ZERO_PTR));
|
|
|
|
my_free(mysql->options.charset_name,MYF(MY_ALLOW_ZERO_PTR));
|
2003-09-26 12:33:13 +02:00
|
|
|
my_free(mysql->options.client_ip,MYF(MY_ALLOW_ZERO_PTR));
|
2003-06-14 10:37:42 +02:00
|
|
|
if (mysql->options.init_commands)
|
|
|
|
{
|
|
|
|
DYNAMIC_ARRAY *init_commands= mysql->options.init_commands;
|
|
|
|
char **ptr= (char**)init_commands->buffer;
|
|
|
|
char **end= ptr + init_commands->elements;
|
|
|
|
for (; ptr<end; ptr++)
|
|
|
|
my_free(*ptr,MYF(MY_WME));
|
|
|
|
delete_dynamic(init_commands);
|
|
|
|
my_free((char*)init_commands,MYF(MY_WME));
|
|
|
|
}
|
|
|
|
#ifdef HAVE_OPENSSL
|
|
|
|
mysql_ssl_free(mysql);
|
|
|
|
#endif /* HAVE_OPENSSL */
|
|
|
|
#ifdef HAVE_SMEM
|
|
|
|
if (mysql->options.shared_memory_base_name != def_shared_memory_base_name)
|
|
|
|
my_free(mysql->options.shared_memory_base_name,MYF(MY_ALLOW_ZERO_PTR));
|
|
|
|
#endif /* HAVE_SMEM */
|
2003-06-24 11:10:35 +02:00
|
|
|
bzero((char*) &mysql->options,sizeof(mysql->options));
|
Added option --valgrind-mysqltest to mysql-test-run
Added flag to Field::store(longlong) to specify if value is unsigned.
This fixes bug #12750: Incorrect storage of 9999999999999999999 in DECIMAL(19, 0)
Fixed warning from valgrind in CREATE ... SELECT
Fixed double free of mysql.options if reconnect failed
mysql-test/mysql-test-run.sh:
Added option --valgrind-mysqltest to allow one to run mysqltest with valgrind
mysql-test/r/bigint.result:
Update results after fix for Field::store(longlong)
mysql-test/r/range.result:
Update results after fix for Field::store(longlong)
mysql-test/r/strict.result:
Update results after fix for Field::store(longlong)
(This fixes some wrong results when storing things into bigint columns)
mysql-test/r/type_ranges.result:
Update results after fix for Field::store(longlong)
mysql-test/t/bigint.test:
Added testing for #12750: Incorrect storage of 9999999999999999999 in DECIMAL(19, 0)
mysql-test/t/innodb.test:
Removed comments affected by this bug fix
mysql-test/t/mysqldump.test:
Fixed result to not depend on existing config files
mysql-test/t/range.test:
0xff numbers are now unsigned
mysql-test/t/strict.test:
Added errors for things that previously (wrongly) succeeded
sql-common/client.c:
Fixed double free of mysql.options if reconnect failed
sql/field.cc:
Added flag to Field::store(longlong) to specify if value is unsigned
sql/field.h:
Added flag to Field::store(longlong) to specify if value is unsigned
sql/field_conv.cc:
Fixed calls to Field::store(longlong,flag)
sql/ha_ndbcluster.cc:
Fixed calls to Field::store(longlong,flag)
sql/handler.cc:
Fixed calls to Field::store(longlong,flag)
sql/item.cc:
Fixed calls to Field::store(longlong,flag)
sql/item_sum.cc:
Fixed calls to Field::store(longlong,flag)
sql/sp.cc:
Fixed calls to Field::store(longlong,flag)
sql/sql_acl.cc:
Fixed calls to Field::store(longlong,flag)
sql/sql_help.cc:
Fixed calls to Field::store(longlong,flag)
sql/sql_show.cc:
Fixed calls to Field::store(longlong,flag)
sql/sql_table.cc:
Fixed varning from valgrind
sql/sql_udf.cc:
Fixed calls to Field::store(longlong,flag)
sql/tztime.cc:
Fixed calls to Field::store(longlong,flag)
sql/unireg.cc:
Fixed calls to Field::store(longlong,flag)
2005-09-14 00:41:44 +02:00
|
|
|
DBUG_VOID_RETURN;
|
2003-06-14 10:37:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void mysql_close_free(MYSQL *mysql)
|
|
|
|
{
|
|
|
|
my_free((gptr) mysql->host_info,MYF(MY_ALLOW_ZERO_PTR));
|
2003-06-24 11:10:35 +02:00
|
|
|
my_free(mysql->user,MYF(MY_ALLOW_ZERO_PTR));
|
|
|
|
my_free(mysql->passwd,MYF(MY_ALLOW_ZERO_PTR));
|
|
|
|
my_free(mysql->db,MYF(MY_ALLOW_ZERO_PTR));
|
2006-03-22 23:37:40 +01:00
|
|
|
#if defined(EMBEDDED_LIBRARY) || MYSQL_VERSION_ID >= 50100
|
2006-02-24 17:34:15 +01:00
|
|
|
my_free(mysql->info_buffer,MYF(MY_ALLOW_ZERO_PTR));
|
2006-03-22 23:37:40 +01:00
|
|
|
mysql->info_buffer= 0;
|
|
|
|
#endif
|
2003-06-24 11:10:35 +02:00
|
|
|
/* Clear pointers for better safety */
|
2006-03-22 23:37:40 +01:00
|
|
|
mysql->host_info= mysql->user= mysql->passwd= mysql->db= 0;
|
2003-06-14 10:37:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-09-22 13:50:07 +02:00
|
|
|
/*
|
|
|
|
Clear connection pointer of every statement: this is necessary
|
|
|
|
to give error on attempt to use a prepared statement of closed
|
|
|
|
connection.
|
|
|
|
|
|
|
|
SYNOPSYS
|
|
|
|
mysql_detach_stmt_list()
|
|
|
|
stmt_list pointer to mysql->stmts
|
2005-03-28 19:59:41 +02:00
|
|
|
|
|
|
|
NOTE
|
|
|
|
There is similar code in mysql_reconnect(), so changes here
|
|
|
|
should also be reflected there.
|
2004-09-22 13:50:07 +02:00
|
|
|
*/
|
|
|
|
|
2004-10-20 00:28:42 +02:00
|
|
|
void mysql_detach_stmt_list(LIST **stmt_list __attribute__((unused)))
|
2004-09-22 13:50:07 +02:00
|
|
|
{
|
|
|
|
#ifdef MYSQL_CLIENT
|
|
|
|
/* Reset connection handle in all prepared statements. */
|
|
|
|
LIST *element= *stmt_list;
|
|
|
|
for (; element; element= element->next)
|
|
|
|
{
|
|
|
|
MYSQL_STMT *stmt= (MYSQL_STMT *) element->data;
|
|
|
|
stmt->mysql= 0;
|
|
|
|
/* No need to call list_delete for statement here */
|
|
|
|
}
|
|
|
|
*stmt_list= 0;
|
|
|
|
#endif /* MYSQL_CLIENT */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-09-26 12:33:13 +02:00
|
|
|
void STDCALL mysql_close(MYSQL *mysql)
|
2003-05-02 18:07:41 +02:00
|
|
|
{
|
|
|
|
DBUG_ENTER("mysql_close");
|
|
|
|
if (mysql) /* Some simple safety */
|
|
|
|
{
|
2003-06-14 10:37:42 +02:00
|
|
|
/* If connection is still up, send a QUIT message */
|
2003-05-02 18:07:41 +02:00
|
|
|
if (mysql->net.vio != 0)
|
|
|
|
{
|
|
|
|
free_old_query(mysql);
|
|
|
|
mysql->status=MYSQL_STATUS_READY; /* Force command */
|
|
|
|
mysql->reconnect=0;
|
|
|
|
simple_command(mysql,COM_QUIT,NullS,0,1);
|
|
|
|
end_server(mysql); /* Sets mysql->net.vio= 0 */
|
|
|
|
}
|
2003-06-14 10:37:42 +02:00
|
|
|
mysql_close_free_options(mysql);
|
|
|
|
mysql_close_free(mysql);
|
2004-09-22 13:50:07 +02:00
|
|
|
mysql_detach_stmt_list(&mysql->stmts);
|
2003-06-14 10:37:42 +02:00
|
|
|
#ifndef TO_BE_DELETED
|
2003-05-02 18:07:41 +02:00
|
|
|
/* free/close slave list */
|
|
|
|
if (mysql->rpl_pivot)
|
|
|
|
{
|
|
|
|
MYSQL* tmp;
|
|
|
|
for (tmp = mysql->next_slave; tmp != mysql; )
|
|
|
|
{
|
|
|
|
/* trick to avoid following freed pointer */
|
|
|
|
MYSQL* tmp1 = tmp->next_slave;
|
|
|
|
mysql_close(tmp);
|
|
|
|
tmp = tmp1;
|
|
|
|
}
|
|
|
|
mysql->rpl_pivot=0;
|
|
|
|
}
|
2003-06-14 10:37:42 +02:00
|
|
|
#endif
|
2003-05-02 18:07:41 +02:00
|
|
|
if (mysql != mysql->master)
|
|
|
|
mysql_close(mysql->master);
|
2003-09-29 11:09:51 +02:00
|
|
|
#ifndef MYSQL_SERVER
|
|
|
|
if (mysql->thd)
|
|
|
|
(*mysql->methods->free_embedded_thd)(mysql);
|
|
|
|
#endif
|
2003-05-02 18:07:41 +02:00
|
|
|
if (mysql->free_me)
|
|
|
|
my_free((gptr) mysql,MYF(0));
|
|
|
|
}
|
|
|
|
DBUG_VOID_RETURN;
|
|
|
|
}
|
|
|
|
|
2004-03-18 13:53:38 +01:00
|
|
|
|
2003-12-22 13:57:34 +01:00
|
|
|
static my_bool cli_read_query_result(MYSQL *mysql)
|
2003-05-02 18:07:41 +02:00
|
|
|
{
|
|
|
|
uchar *pos;
|
|
|
|
ulong field_count;
|
|
|
|
MYSQL_DATA *fields;
|
|
|
|
ulong length;
|
2003-12-22 13:57:34 +01:00
|
|
|
DBUG_ENTER("cli_read_query_result");
|
2003-05-02 18:07:41 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
Read from the connection which we actually used, which
|
|
|
|
could differ from the original connection if we have slaves
|
|
|
|
*/
|
|
|
|
mysql = mysql->last_used_con;
|
|
|
|
|
A fix and a test case for Bug#15752 "Lost connection to MySQL server
when calling a SP from C API"
The bug was caused by lack of checks for misuse in mysql_real_query.
A stored procedure always returns at least one result, which is the
status of execution of the procedure itself.
This result, or so-called OK packet, is similar to a result
returned by INSERT/UPDATE/CREATE operations: it contains the overall
status of execution, the number of affected rows and the number of
warnings. The client test program attached to the bug did not read this
result and ivnoked the next query. In turn, libmysql had no check for
such scenario and mysql_real_query was simply trying to send that query
without reading the pending response, thus messing up the communication
protocol.
The fix is to return an error from mysql_real_query when it's called
prior to retrieval of all pending results.
client/mysqlbinlog.cc:
net_safe_read -> cli_safe_read
include/mysql.h:
Remove a private function from the public header.
include/mysql_com.h:
Remove a define that is never used.
include/sql_common.h:
Add a declaration for cli_safe_read - a function that reads one packet
from the server.
libmysql/libmysql.c:
net_safe_read -> cli_safe_read
Return CR_COMMANDS_OUT_OF_SYNC on attempt to execute a statement
using a connection which has pending result sets.
sql-common/client.c:
Actual fix for Bug#15752: if the server has pending result sets for
the client, return CR_COMMANDS_OUT_OF_SYNC on attempt to execute
another query. Similarly to the behaviour of mysql_use_result(),
multiple result sets block the connection and must be fetched
before it can be used for another query.
This uncovered an error in the protocol: the server doesn't drop
SERVER_MORE_RESULTS_EXISTS status flag upon an error, so in case of
a multi-query like SELECT 1; SELECT syntax_error; SELECT 2;
the client has no way to know that the server won't ever come to
execution of the third query and won't return any result sets for it.
For now, fix it in cli_safe_read, as a proper fix requires extension
of the client-server protocol.
sql/protocol.cc:
Remove a name that is never used.
sql/slave.cc:
net_safe_read -> cli_safe_read
tests/mysql_client_test.c:
Make 'query' a local variable to avoid name clash.
Add a test case for Bug#15752 "Lost connection to MySQL server when
calling an SP from C API"
2006-07-24 12:56:53 +02:00
|
|
|
if ((length = cli_safe_read(mysql)) == packet_error)
|
2003-05-02 18:07:41 +02:00
|
|
|
DBUG_RETURN(1);
|
2003-06-14 10:37:42 +02:00
|
|
|
free_old_query(mysql); /* Free old result */
|
|
|
|
#ifdef MYSQL_CLIENT /* Avoid warn of unused labels*/
|
2003-05-02 18:07:41 +02:00
|
|
|
get_info:
|
2003-05-31 12:15:46 +02:00
|
|
|
#endif
|
2003-05-02 18:07:41 +02:00
|
|
|
pos=(uchar*) mysql->net.read_pos;
|
|
|
|
if ((field_count= net_field_length(&pos)) == 0)
|
|
|
|
{
|
|
|
|
mysql->affected_rows= net_field_length_ll(&pos);
|
|
|
|
mysql->insert_id= net_field_length_ll(&pos);
|
2003-11-28 11:18:13 +01:00
|
|
|
DBUG_PRINT("info",("affected_rows: %lu insert_id: %lu",
|
|
|
|
(ulong) mysql->affected_rows,
|
|
|
|
(ulong) mysql->insert_id));
|
2003-05-02 18:07:41 +02:00
|
|
|
if (protocol_41(mysql))
|
|
|
|
{
|
|
|
|
mysql->server_status=uint2korr(pos); pos+=2;
|
|
|
|
mysql->warning_count=uint2korr(pos); pos+=2;
|
|
|
|
}
|
2003-05-31 12:15:46 +02:00
|
|
|
else if (mysql->server_capabilities & CLIENT_TRANSACTIONS)
|
2003-05-02 18:07:41 +02:00
|
|
|
{
|
2003-11-28 11:18:13 +01:00
|
|
|
/* MySQL 4.0 protocol */
|
2003-05-02 18:07:41 +02:00
|
|
|
mysql->server_status=uint2korr(pos); pos+=2;
|
|
|
|
mysql->warning_count= 0;
|
|
|
|
}
|
2003-11-28 11:18:13 +01:00
|
|
|
DBUG_PRINT("info",("status: %u warning_count: %u",
|
2003-05-02 18:07:41 +02:00
|
|
|
mysql->server_status, mysql->warning_count));
|
|
|
|
if (pos < mysql->net.read_pos+length && net_field_length(&pos))
|
|
|
|
mysql->info=(char*) pos;
|
|
|
|
DBUG_RETURN(0);
|
|
|
|
}
|
2003-05-31 12:15:46 +02:00
|
|
|
#ifdef MYSQL_CLIENT
|
2003-05-02 18:07:41 +02:00
|
|
|
if (field_count == NULL_LENGTH) /* LOAD DATA LOCAL INFILE */
|
|
|
|
{
|
2004-03-23 01:58:49 +01:00
|
|
|
int error=handle_local_infile(mysql,(char*) pos);
|
A fix and a test case for Bug#15752 "Lost connection to MySQL server
when calling a SP from C API"
The bug was caused by lack of checks for misuse in mysql_real_query.
A stored procedure always returns at least one result, which is the
status of execution of the procedure itself.
This result, or so-called OK packet, is similar to a result
returned by INSERT/UPDATE/CREATE operations: it contains the overall
status of execution, the number of affected rows and the number of
warnings. The client test program attached to the bug did not read this
result and ivnoked the next query. In turn, libmysql had no check for
such scenario and mysql_real_query was simply trying to send that query
without reading the pending response, thus messing up the communication
protocol.
The fix is to return an error from mysql_real_query when it's called
prior to retrieval of all pending results.
client/mysqlbinlog.cc:
net_safe_read -> cli_safe_read
include/mysql.h:
Remove a private function from the public header.
include/mysql_com.h:
Remove a define that is never used.
include/sql_common.h:
Add a declaration for cli_safe_read - a function that reads one packet
from the server.
libmysql/libmysql.c:
net_safe_read -> cli_safe_read
Return CR_COMMANDS_OUT_OF_SYNC on attempt to execute a statement
using a connection which has pending result sets.
sql-common/client.c:
Actual fix for Bug#15752: if the server has pending result sets for
the client, return CR_COMMANDS_OUT_OF_SYNC on attempt to execute
another query. Similarly to the behaviour of mysql_use_result(),
multiple result sets block the connection and must be fetched
before it can be used for another query.
This uncovered an error in the protocol: the server doesn't drop
SERVER_MORE_RESULTS_EXISTS status flag upon an error, so in case of
a multi-query like SELECT 1; SELECT syntax_error; SELECT 2;
the client has no way to know that the server won't ever come to
execution of the third query and won't return any result sets for it.
For now, fix it in cli_safe_read, as a proper fix requires extension
of the client-server protocol.
sql/protocol.cc:
Remove a name that is never used.
sql/slave.cc:
net_safe_read -> cli_safe_read
tests/mysql_client_test.c:
Make 'query' a local variable to avoid name clash.
Add a test case for Bug#15752 "Lost connection to MySQL server when
calling an SP from C API"
2006-07-24 12:56:53 +02:00
|
|
|
if ((length= cli_safe_read(mysql)) == packet_error || error)
|
2003-05-02 18:07:41 +02:00
|
|
|
DBUG_RETURN(1);
|
|
|
|
goto get_info; /* Get info packet */
|
|
|
|
}
|
2003-05-31 12:15:46 +02:00
|
|
|
#endif
|
2003-05-02 18:07:41 +02:00
|
|
|
if (!(mysql->server_status & SERVER_STATUS_AUTOCOMMIT))
|
|
|
|
mysql->server_status|= SERVER_STATUS_IN_TRANS;
|
|
|
|
|
2006-02-24 17:34:15 +01:00
|
|
|
if (!(fields=cli_read_rows(mysql,(MYSQL_FIELD*)0, protocol_41(mysql) ? 7:5)))
|
2003-05-02 18:07:41 +02:00
|
|
|
DBUG_RETURN(1);
|
|
|
|
if (!(mysql->fields=unpack_fields(fields,&mysql->field_alloc,
|
|
|
|
(uint) field_count,0,
|
|
|
|
mysql->server_capabilities)))
|
|
|
|
DBUG_RETURN(1);
|
|
|
|
mysql->status= MYSQL_STATUS_GET_RESULT;
|
|
|
|
mysql->field_count= (uint) field_count;
|
2005-05-16 12:34:23 +02:00
|
|
|
DBUG_PRINT("exit",("ok"));
|
2003-05-02 18:07:41 +02:00
|
|
|
DBUG_RETURN(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Send the query and return so we can do something else.
|
|
|
|
Needs to be followed by mysql_read_query_result() when we want to
|
|
|
|
finish processing it.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int STDCALL
|
|
|
|
mysql_send_query(MYSQL* mysql, const char* query, ulong length)
|
|
|
|
{
|
|
|
|
DBUG_ENTER("mysql_send_query");
|
|
|
|
DBUG_PRINT("enter",("rpl_parse: %d rpl_pivot: %d",
|
|
|
|
mysql->options.rpl_parse, mysql->rpl_pivot));
|
2003-06-14 10:37:42 +02:00
|
|
|
#ifndef TO_BE_DELETED
|
2003-05-02 18:07:41 +02:00
|
|
|
if (mysql->options.rpl_parse && mysql->rpl_pivot)
|
|
|
|
{
|
|
|
|
switch (mysql_rpl_query_type(query, length)) {
|
|
|
|
case MYSQL_RPL_MASTER:
|
|
|
|
DBUG_RETURN(mysql_master_send_query(mysql, query, length));
|
|
|
|
case MYSQL_RPL_SLAVE:
|
|
|
|
DBUG_RETURN(mysql_slave_send_query(mysql, query, length));
|
|
|
|
case MYSQL_RPL_ADMIN:
|
|
|
|
break; /* fall through */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mysql->last_used_con = mysql;
|
2003-06-14 10:37:42 +02:00
|
|
|
#endif
|
2003-05-02 18:07:41 +02:00
|
|
|
|
|
|
|
DBUG_RETURN(simple_command(mysql, COM_QUERY, query, length, 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int STDCALL
|
|
|
|
mysql_real_query(MYSQL *mysql, const char *query, ulong length)
|
|
|
|
{
|
|
|
|
DBUG_ENTER("mysql_real_query");
|
2006-11-20 21:42:06 +01:00
|
|
|
DBUG_PRINT("enter",("handle: 0x%lx", (long) mysql));
|
2003-05-02 18:07:41 +02:00
|
|
|
DBUG_PRINT("query",("Query = '%-.4096s'",query));
|
|
|
|
|
|
|
|
if (mysql_send_query(mysql,query,length))
|
|
|
|
DBUG_RETURN(1);
|
2003-09-18 15:28:42 +02:00
|
|
|
DBUG_RETURN((int) (*mysql->methods->read_query_result)(mysql));
|
2003-05-02 18:07:41 +02:00
|
|
|
}
|
|
|
|
|
2003-06-14 10:37:42 +02:00
|
|
|
|
2003-05-02 18:07:41 +02:00
|
|
|
/**************************************************************************
|
|
|
|
Alloc result struct for buffered results. All rows are read to buffer.
|
|
|
|
mysql_data_seek may be used.
|
|
|
|
**************************************************************************/
|
|
|
|
|
2003-09-16 13:06:25 +02:00
|
|
|
MYSQL_RES * STDCALL mysql_store_result(MYSQL *mysql)
|
2003-05-02 18:07:41 +02:00
|
|
|
{
|
|
|
|
MYSQL_RES *result;
|
|
|
|
DBUG_ENTER("mysql_store_result");
|
|
|
|
/* read from the actually used connection */
|
|
|
|
mysql = mysql->last_used_con;
|
|
|
|
if (!mysql->fields)
|
|
|
|
DBUG_RETURN(0);
|
|
|
|
if (mysql->status != MYSQL_STATUS_GET_RESULT)
|
|
|
|
{
|
2004-03-12 13:21:48 +01:00
|
|
|
set_mysql_error(mysql, CR_COMMANDS_OUT_OF_SYNC, unknown_sqlstate);
|
2003-05-02 18:07:41 +02:00
|
|
|
DBUG_RETURN(0);
|
|
|
|
}
|
|
|
|
mysql->status=MYSQL_STATUS_READY; /* server is ready */
|
|
|
|
if (!(result=(MYSQL_RES*) my_malloc((uint) (sizeof(MYSQL_RES)+
|
|
|
|
sizeof(ulong) *
|
|
|
|
mysql->field_count),
|
|
|
|
MYF(MY_WME | MY_ZEROFILL))))
|
|
|
|
{
|
2004-03-12 13:21:48 +01:00
|
|
|
set_mysql_error(mysql, CR_OUT_OF_MEMORY, unknown_sqlstate);
|
2003-05-02 18:07:41 +02:00
|
|
|
DBUG_RETURN(0);
|
|
|
|
}
|
2003-07-23 12:23:20 +02:00
|
|
|
result->methods= mysql->methods;
|
2003-05-02 18:07:41 +02:00
|
|
|
result->eof=1; /* Marker for buffered */
|
|
|
|
result->lengths=(ulong*) (result+1);
|
2003-09-16 13:06:25 +02:00
|
|
|
if (!(result->data=
|
|
|
|
(*mysql->methods->read_rows)(mysql,mysql->fields,mysql->field_count)))
|
2003-05-02 18:07:41 +02:00
|
|
|
{
|
|
|
|
my_free((gptr) result,MYF(0));
|
|
|
|
DBUG_RETURN(0);
|
|
|
|
}
|
|
|
|
mysql->affected_rows= result->row_count= result->data->rows;
|
|
|
|
result->data_cursor= result->data->data;
|
|
|
|
result->fields= mysql->fields;
|
|
|
|
result->field_alloc= mysql->field_alloc;
|
|
|
|
result->field_count= mysql->field_count;
|
Intermediate commit of client library (cleanups + fixes of 3 items from
flaws list)
TODO:
* verify that no sequence of API calls produces SIGSEGV.
That is, verify that mysql_stmt_init -> mysql_stmt_fetch is OK,
or mysql_stmt_prepare -> mysql_stmt_fetch_column is OK and sets
meaningful error.
* remove alloc_stmt_fields call
* revise stmt->state codes and statement states.
* there are other items in prepared statements 'to fix' document.
Done:
- cleanups and comments
- revision of prepared statement error codes.
- mysql_stmt_prepare is now can always be called (that is, you can reprepare
a statement)
- new implementation of mysql_stmt_close and fetch cancellation
include/errmsg.h:
- CR_NOT_ALL_PARAMS_BOUND - this error code wasn't used until now.
Apparently it was added in advance, but then interface of
mysql_stmt_bind_param changed. Now it's not possible to bind only some
parameters - either all or none of parameters are bound.
This error code is renamed to CR_PARAMS_NOT_BOUND
- CR_FETCH_CANCELLED - error code set on server side when fetch from
MYSQL_RES or MYSQL_STMT (in blocking mode) was cancelled because of
intercepting call to mysql_stmt_close
- CR_NO_DATA - this is proposed error code to return from
mysql_stmt_fetch_column if no row was fetched (by any type of fetch).
We always can fall back to CR_COMMANDS_OUT_OF_SYNC though.
Need reviewer's opinion on this one.
include/mysql.h:
- added unbuffered_fetch_owner member to MYSQL to point to MYSQL_RES
or MYSQL_STMT which is used to fetch result at the moment.
This is to be able to set CR_FETCH_CANCELLED error without fantoms.
- added unbuffered_fetch_cancelled boolean variable to MYSQL_STMT and
MYSQL_RES structures
- rename PREP_STMT_STATE -> enum enum_mysql_stmt_state
- members of MYSQL_STMT ordered by size.
- removed members of MYSQL_STMT: current_row, result_buffered,
last_fetched_column, last_fetched_buffer, query
- renamed members of MYSQL_STMT: param_buffers -> bind_param_done,
res_buffers -> bind_result_done
- now mysql_stmt_fetch calls stmt->read_row_func to read row either from
buffer or from network.
include/sql_common.h:
declaration for flush_use_result
libmysql/client_settings.h:
stmt_close declaration removed
libmysql/errmsg.c:
Error messages for changed and added error codes.
libmysql/libmysql.c:
Many changes:
- some unused variables removed
- cleanups
- better error reporting
- some function calls commented
- alloc_stmt_fields is now called right after execute, to not read
mysql->fields of some other statement
- new implementation of mysql_stmt_fetch - this is also with cursor
fetch in mind (to implement cursor fetch I'll just need to write
special read_row function for it, so this change will be local)
- implementation of fetch cancellation, including complete rewrite of
mysql_stmt_close
- now mysql_stmt_free_result doesn't free results of other statements.
sql-common/client.c:
- implementation of flush_use_result
- implementation of fetch cancellation
- changed behaviour of mysql_close in regard to mysql_stmt_close - now
mysql_close just set stmt->mysql to 0
2004-03-15 23:04:04 +01:00
|
|
|
/* The rest of result members is bzeroed in malloc */
|
2003-05-02 18:07:41 +02:00
|
|
|
mysql->fields=0; /* fields is now in result */
|
Intermediate commit of client library (cleanups + fixes of 3 items from
flaws list)
TODO:
* verify that no sequence of API calls produces SIGSEGV.
That is, verify that mysql_stmt_init -> mysql_stmt_fetch is OK,
or mysql_stmt_prepare -> mysql_stmt_fetch_column is OK and sets
meaningful error.
* remove alloc_stmt_fields call
* revise stmt->state codes and statement states.
* there are other items in prepared statements 'to fix' document.
Done:
- cleanups and comments
- revision of prepared statement error codes.
- mysql_stmt_prepare is now can always be called (that is, you can reprepare
a statement)
- new implementation of mysql_stmt_close and fetch cancellation
include/errmsg.h:
- CR_NOT_ALL_PARAMS_BOUND - this error code wasn't used until now.
Apparently it was added in advance, but then interface of
mysql_stmt_bind_param changed. Now it's not possible to bind only some
parameters - either all or none of parameters are bound.
This error code is renamed to CR_PARAMS_NOT_BOUND
- CR_FETCH_CANCELLED - error code set on server side when fetch from
MYSQL_RES or MYSQL_STMT (in blocking mode) was cancelled because of
intercepting call to mysql_stmt_close
- CR_NO_DATA - this is proposed error code to return from
mysql_stmt_fetch_column if no row was fetched (by any type of fetch).
We always can fall back to CR_COMMANDS_OUT_OF_SYNC though.
Need reviewer's opinion on this one.
include/mysql.h:
- added unbuffered_fetch_owner member to MYSQL to point to MYSQL_RES
or MYSQL_STMT which is used to fetch result at the moment.
This is to be able to set CR_FETCH_CANCELLED error without fantoms.
- added unbuffered_fetch_cancelled boolean variable to MYSQL_STMT and
MYSQL_RES structures
- rename PREP_STMT_STATE -> enum enum_mysql_stmt_state
- members of MYSQL_STMT ordered by size.
- removed members of MYSQL_STMT: current_row, result_buffered,
last_fetched_column, last_fetched_buffer, query
- renamed members of MYSQL_STMT: param_buffers -> bind_param_done,
res_buffers -> bind_result_done
- now mysql_stmt_fetch calls stmt->read_row_func to read row either from
buffer or from network.
include/sql_common.h:
declaration for flush_use_result
libmysql/client_settings.h:
stmt_close declaration removed
libmysql/errmsg.c:
Error messages for changed and added error codes.
libmysql/libmysql.c:
Many changes:
- some unused variables removed
- cleanups
- better error reporting
- some function calls commented
- alloc_stmt_fields is now called right after execute, to not read
mysql->fields of some other statement
- new implementation of mysql_stmt_fetch - this is also with cursor
fetch in mind (to implement cursor fetch I'll just need to write
special read_row function for it, so this change will be local)
- implementation of fetch cancellation, including complete rewrite of
mysql_stmt_close
- now mysql_stmt_free_result doesn't free results of other statements.
sql-common/client.c:
- implementation of flush_use_result
- implementation of fetch cancellation
- changed behaviour of mysql_close in regard to mysql_stmt_close - now
mysql_close just set stmt->mysql to 0
2004-03-15 23:04:04 +01:00
|
|
|
/* just in case this was mistakenly called after mysql_stmt_execute() */
|
|
|
|
mysql->unbuffered_fetch_owner= 0;
|
2003-05-02 18:07:41 +02:00
|
|
|
DBUG_RETURN(result); /* Data fetched */
|
|
|
|
}
|
|
|
|
|
2003-06-14 10:37:42 +02:00
|
|
|
|
|
|
|
/**************************************************************************
|
|
|
|
Alloc struct for use with unbuffered reads. Data is fetched by domand
|
|
|
|
when calling to mysql_fetch_row.
|
|
|
|
mysql_data_seek is a noop.
|
|
|
|
|
|
|
|
No other queries may be specified with the same MYSQL handle.
|
|
|
|
There shouldn't be much processing per row because mysql server shouldn't
|
|
|
|
have to wait for the client (and will not wait more than 30 sec/packet).
|
|
|
|
**************************************************************************/
|
|
|
|
|
2003-12-22 13:57:34 +01:00
|
|
|
static MYSQL_RES * cli_use_result(MYSQL *mysql)
|
2003-06-14 10:37:42 +02:00
|
|
|
{
|
|
|
|
MYSQL_RES *result;
|
2003-12-22 13:57:34 +01:00
|
|
|
DBUG_ENTER("cli_use_result");
|
2003-06-14 10:37:42 +02:00
|
|
|
|
|
|
|
mysql = mysql->last_used_con;
|
|
|
|
|
|
|
|
if (!mysql->fields)
|
|
|
|
DBUG_RETURN(0);
|
|
|
|
if (mysql->status != MYSQL_STATUS_GET_RESULT)
|
|
|
|
{
|
2004-03-12 13:21:48 +01:00
|
|
|
set_mysql_error(mysql, CR_COMMANDS_OUT_OF_SYNC, unknown_sqlstate);
|
2003-06-14 10:37:42 +02:00
|
|
|
DBUG_RETURN(0);
|
|
|
|
}
|
|
|
|
if (!(result=(MYSQL_RES*) my_malloc(sizeof(*result)+
|
|
|
|
sizeof(ulong)*mysql->field_count,
|
|
|
|
MYF(MY_WME | MY_ZEROFILL))))
|
|
|
|
DBUG_RETURN(0);
|
|
|
|
result->lengths=(ulong*) (result+1);
|
2003-07-23 12:23:20 +02:00
|
|
|
result->methods= mysql->methods;
|
2003-06-14 10:37:42 +02:00
|
|
|
if (!(result->row=(MYSQL_ROW)
|
|
|
|
my_malloc(sizeof(result->row[0])*(mysql->field_count+1), MYF(MY_WME))))
|
|
|
|
{ /* Ptrs: to one row */
|
|
|
|
my_free((gptr) result,MYF(0));
|
|
|
|
DBUG_RETURN(0);
|
|
|
|
}
|
|
|
|
result->fields= mysql->fields;
|
|
|
|
result->field_alloc= mysql->field_alloc;
|
|
|
|
result->field_count= mysql->field_count;
|
|
|
|
result->current_field=0;
|
|
|
|
result->handle= mysql;
|
|
|
|
result->current_row= 0;
|
|
|
|
mysql->fields=0; /* fields is now in result */
|
|
|
|
mysql->status=MYSQL_STATUS_USE_RESULT;
|
Intermediate commit of client library (cleanups + fixes of 3 items from
flaws list)
TODO:
* verify that no sequence of API calls produces SIGSEGV.
That is, verify that mysql_stmt_init -> mysql_stmt_fetch is OK,
or mysql_stmt_prepare -> mysql_stmt_fetch_column is OK and sets
meaningful error.
* remove alloc_stmt_fields call
* revise stmt->state codes and statement states.
* there are other items in prepared statements 'to fix' document.
Done:
- cleanups and comments
- revision of prepared statement error codes.
- mysql_stmt_prepare is now can always be called (that is, you can reprepare
a statement)
- new implementation of mysql_stmt_close and fetch cancellation
include/errmsg.h:
- CR_NOT_ALL_PARAMS_BOUND - this error code wasn't used until now.
Apparently it was added in advance, but then interface of
mysql_stmt_bind_param changed. Now it's not possible to bind only some
parameters - either all or none of parameters are bound.
This error code is renamed to CR_PARAMS_NOT_BOUND
- CR_FETCH_CANCELLED - error code set on server side when fetch from
MYSQL_RES or MYSQL_STMT (in blocking mode) was cancelled because of
intercepting call to mysql_stmt_close
- CR_NO_DATA - this is proposed error code to return from
mysql_stmt_fetch_column if no row was fetched (by any type of fetch).
We always can fall back to CR_COMMANDS_OUT_OF_SYNC though.
Need reviewer's opinion on this one.
include/mysql.h:
- added unbuffered_fetch_owner member to MYSQL to point to MYSQL_RES
or MYSQL_STMT which is used to fetch result at the moment.
This is to be able to set CR_FETCH_CANCELLED error without fantoms.
- added unbuffered_fetch_cancelled boolean variable to MYSQL_STMT and
MYSQL_RES structures
- rename PREP_STMT_STATE -> enum enum_mysql_stmt_state
- members of MYSQL_STMT ordered by size.
- removed members of MYSQL_STMT: current_row, result_buffered,
last_fetched_column, last_fetched_buffer, query
- renamed members of MYSQL_STMT: param_buffers -> bind_param_done,
res_buffers -> bind_result_done
- now mysql_stmt_fetch calls stmt->read_row_func to read row either from
buffer or from network.
include/sql_common.h:
declaration for flush_use_result
libmysql/client_settings.h:
stmt_close declaration removed
libmysql/errmsg.c:
Error messages for changed and added error codes.
libmysql/libmysql.c:
Many changes:
- some unused variables removed
- cleanups
- better error reporting
- some function calls commented
- alloc_stmt_fields is now called right after execute, to not read
mysql->fields of some other statement
- new implementation of mysql_stmt_fetch - this is also with cursor
fetch in mind (to implement cursor fetch I'll just need to write
special read_row function for it, so this change will be local)
- implementation of fetch cancellation, including complete rewrite of
mysql_stmt_close
- now mysql_stmt_free_result doesn't free results of other statements.
sql-common/client.c:
- implementation of flush_use_result
- implementation of fetch cancellation
- changed behaviour of mysql_close in regard to mysql_stmt_close - now
mysql_close just set stmt->mysql to 0
2004-03-15 23:04:04 +01:00
|
|
|
mysql->unbuffered_fetch_owner= &result->unbuffered_fetch_cancelled;
|
2003-06-14 10:37:42 +02:00
|
|
|
DBUG_RETURN(result); /* Data is read to be fetched */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-05-02 18:07:41 +02:00
|
|
|
/**************************************************************************
|
2003-06-14 10:37:42 +02:00
|
|
|
Return next row of the query results
|
2003-05-02 18:07:41 +02:00
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
MYSQL_ROW STDCALL
|
|
|
|
mysql_fetch_row(MYSQL_RES *res)
|
|
|
|
{
|
|
|
|
DBUG_ENTER("mysql_fetch_row");
|
|
|
|
if (!res->data)
|
|
|
|
{ /* Unbufferred fetch */
|
|
|
|
if (!res->eof)
|
|
|
|
{
|
Intermediate commit of client library (cleanups + fixes of 3 items from
flaws list)
TODO:
* verify that no sequence of API calls produces SIGSEGV.
That is, verify that mysql_stmt_init -> mysql_stmt_fetch is OK,
or mysql_stmt_prepare -> mysql_stmt_fetch_column is OK and sets
meaningful error.
* remove alloc_stmt_fields call
* revise stmt->state codes and statement states.
* there are other items in prepared statements 'to fix' document.
Done:
- cleanups and comments
- revision of prepared statement error codes.
- mysql_stmt_prepare is now can always be called (that is, you can reprepare
a statement)
- new implementation of mysql_stmt_close and fetch cancellation
include/errmsg.h:
- CR_NOT_ALL_PARAMS_BOUND - this error code wasn't used until now.
Apparently it was added in advance, but then interface of
mysql_stmt_bind_param changed. Now it's not possible to bind only some
parameters - either all or none of parameters are bound.
This error code is renamed to CR_PARAMS_NOT_BOUND
- CR_FETCH_CANCELLED - error code set on server side when fetch from
MYSQL_RES or MYSQL_STMT (in blocking mode) was cancelled because of
intercepting call to mysql_stmt_close
- CR_NO_DATA - this is proposed error code to return from
mysql_stmt_fetch_column if no row was fetched (by any type of fetch).
We always can fall back to CR_COMMANDS_OUT_OF_SYNC though.
Need reviewer's opinion on this one.
include/mysql.h:
- added unbuffered_fetch_owner member to MYSQL to point to MYSQL_RES
or MYSQL_STMT which is used to fetch result at the moment.
This is to be able to set CR_FETCH_CANCELLED error without fantoms.
- added unbuffered_fetch_cancelled boolean variable to MYSQL_STMT and
MYSQL_RES structures
- rename PREP_STMT_STATE -> enum enum_mysql_stmt_state
- members of MYSQL_STMT ordered by size.
- removed members of MYSQL_STMT: current_row, result_buffered,
last_fetched_column, last_fetched_buffer, query
- renamed members of MYSQL_STMT: param_buffers -> bind_param_done,
res_buffers -> bind_result_done
- now mysql_stmt_fetch calls stmt->read_row_func to read row either from
buffer or from network.
include/sql_common.h:
declaration for flush_use_result
libmysql/client_settings.h:
stmt_close declaration removed
libmysql/errmsg.c:
Error messages for changed and added error codes.
libmysql/libmysql.c:
Many changes:
- some unused variables removed
- cleanups
- better error reporting
- some function calls commented
- alloc_stmt_fields is now called right after execute, to not read
mysql->fields of some other statement
- new implementation of mysql_stmt_fetch - this is also with cursor
fetch in mind (to implement cursor fetch I'll just need to write
special read_row function for it, so this change will be local)
- implementation of fetch cancellation, including complete rewrite of
mysql_stmt_close
- now mysql_stmt_free_result doesn't free results of other statements.
sql-common/client.c:
- implementation of flush_use_result
- implementation of fetch cancellation
- changed behaviour of mysql_close in regard to mysql_stmt_close - now
mysql_close just set stmt->mysql to 0
2004-03-15 23:04:04 +01:00
|
|
|
MYSQL *mysql= res->handle;
|
|
|
|
if (mysql->status != MYSQL_STATUS_USE_RESULT)
|
2003-05-02 18:07:41 +02:00
|
|
|
{
|
Intermediate commit of client library (cleanups + fixes of 3 items from
flaws list)
TODO:
* verify that no sequence of API calls produces SIGSEGV.
That is, verify that mysql_stmt_init -> mysql_stmt_fetch is OK,
or mysql_stmt_prepare -> mysql_stmt_fetch_column is OK and sets
meaningful error.
* remove alloc_stmt_fields call
* revise stmt->state codes and statement states.
* there are other items in prepared statements 'to fix' document.
Done:
- cleanups and comments
- revision of prepared statement error codes.
- mysql_stmt_prepare is now can always be called (that is, you can reprepare
a statement)
- new implementation of mysql_stmt_close and fetch cancellation
include/errmsg.h:
- CR_NOT_ALL_PARAMS_BOUND - this error code wasn't used until now.
Apparently it was added in advance, but then interface of
mysql_stmt_bind_param changed. Now it's not possible to bind only some
parameters - either all or none of parameters are bound.
This error code is renamed to CR_PARAMS_NOT_BOUND
- CR_FETCH_CANCELLED - error code set on server side when fetch from
MYSQL_RES or MYSQL_STMT (in blocking mode) was cancelled because of
intercepting call to mysql_stmt_close
- CR_NO_DATA - this is proposed error code to return from
mysql_stmt_fetch_column if no row was fetched (by any type of fetch).
We always can fall back to CR_COMMANDS_OUT_OF_SYNC though.
Need reviewer's opinion on this one.
include/mysql.h:
- added unbuffered_fetch_owner member to MYSQL to point to MYSQL_RES
or MYSQL_STMT which is used to fetch result at the moment.
This is to be able to set CR_FETCH_CANCELLED error without fantoms.
- added unbuffered_fetch_cancelled boolean variable to MYSQL_STMT and
MYSQL_RES structures
- rename PREP_STMT_STATE -> enum enum_mysql_stmt_state
- members of MYSQL_STMT ordered by size.
- removed members of MYSQL_STMT: current_row, result_buffered,
last_fetched_column, last_fetched_buffer, query
- renamed members of MYSQL_STMT: param_buffers -> bind_param_done,
res_buffers -> bind_result_done
- now mysql_stmt_fetch calls stmt->read_row_func to read row either from
buffer or from network.
include/sql_common.h:
declaration for flush_use_result
libmysql/client_settings.h:
stmt_close declaration removed
libmysql/errmsg.c:
Error messages for changed and added error codes.
libmysql/libmysql.c:
Many changes:
- some unused variables removed
- cleanups
- better error reporting
- some function calls commented
- alloc_stmt_fields is now called right after execute, to not read
mysql->fields of some other statement
- new implementation of mysql_stmt_fetch - this is also with cursor
fetch in mind (to implement cursor fetch I'll just need to write
special read_row function for it, so this change will be local)
- implementation of fetch cancellation, including complete rewrite of
mysql_stmt_close
- now mysql_stmt_free_result doesn't free results of other statements.
sql-common/client.c:
- implementation of flush_use_result
- implementation of fetch cancellation
- changed behaviour of mysql_close in regard to mysql_stmt_close - now
mysql_close just set stmt->mysql to 0
2004-03-15 23:04:04 +01:00
|
|
|
set_mysql_error(mysql,
|
|
|
|
res->unbuffered_fetch_cancelled ?
|
2004-07-01 05:18:41 +02:00
|
|
|
CR_FETCH_CANCELED : CR_COMMANDS_OUT_OF_SYNC,
|
Intermediate commit of client library (cleanups + fixes of 3 items from
flaws list)
TODO:
* verify that no sequence of API calls produces SIGSEGV.
That is, verify that mysql_stmt_init -> mysql_stmt_fetch is OK,
or mysql_stmt_prepare -> mysql_stmt_fetch_column is OK and sets
meaningful error.
* remove alloc_stmt_fields call
* revise stmt->state codes and statement states.
* there are other items in prepared statements 'to fix' document.
Done:
- cleanups and comments
- revision of prepared statement error codes.
- mysql_stmt_prepare is now can always be called (that is, you can reprepare
a statement)
- new implementation of mysql_stmt_close and fetch cancellation
include/errmsg.h:
- CR_NOT_ALL_PARAMS_BOUND - this error code wasn't used until now.
Apparently it was added in advance, but then interface of
mysql_stmt_bind_param changed. Now it's not possible to bind only some
parameters - either all or none of parameters are bound.
This error code is renamed to CR_PARAMS_NOT_BOUND
- CR_FETCH_CANCELLED - error code set on server side when fetch from
MYSQL_RES or MYSQL_STMT (in blocking mode) was cancelled because of
intercepting call to mysql_stmt_close
- CR_NO_DATA - this is proposed error code to return from
mysql_stmt_fetch_column if no row was fetched (by any type of fetch).
We always can fall back to CR_COMMANDS_OUT_OF_SYNC though.
Need reviewer's opinion on this one.
include/mysql.h:
- added unbuffered_fetch_owner member to MYSQL to point to MYSQL_RES
or MYSQL_STMT which is used to fetch result at the moment.
This is to be able to set CR_FETCH_CANCELLED error without fantoms.
- added unbuffered_fetch_cancelled boolean variable to MYSQL_STMT and
MYSQL_RES structures
- rename PREP_STMT_STATE -> enum enum_mysql_stmt_state
- members of MYSQL_STMT ordered by size.
- removed members of MYSQL_STMT: current_row, result_buffered,
last_fetched_column, last_fetched_buffer, query
- renamed members of MYSQL_STMT: param_buffers -> bind_param_done,
res_buffers -> bind_result_done
- now mysql_stmt_fetch calls stmt->read_row_func to read row either from
buffer or from network.
include/sql_common.h:
declaration for flush_use_result
libmysql/client_settings.h:
stmt_close declaration removed
libmysql/errmsg.c:
Error messages for changed and added error codes.
libmysql/libmysql.c:
Many changes:
- some unused variables removed
- cleanups
- better error reporting
- some function calls commented
- alloc_stmt_fields is now called right after execute, to not read
mysql->fields of some other statement
- new implementation of mysql_stmt_fetch - this is also with cursor
fetch in mind (to implement cursor fetch I'll just need to write
special read_row function for it, so this change will be local)
- implementation of fetch cancellation, including complete rewrite of
mysql_stmt_close
- now mysql_stmt_free_result doesn't free results of other statements.
sql-common/client.c:
- implementation of flush_use_result
- implementation of fetch cancellation
- changed behaviour of mysql_close in regard to mysql_stmt_close - now
mysql_close just set stmt->mysql to 0
2004-03-15 23:04:04 +01:00
|
|
|
unknown_sqlstate);
|
2003-05-02 18:07:41 +02:00
|
|
|
}
|
Intermediate commit of client library (cleanups + fixes of 3 items from
flaws list)
TODO:
* verify that no sequence of API calls produces SIGSEGV.
That is, verify that mysql_stmt_init -> mysql_stmt_fetch is OK,
or mysql_stmt_prepare -> mysql_stmt_fetch_column is OK and sets
meaningful error.
* remove alloc_stmt_fields call
* revise stmt->state codes and statement states.
* there are other items in prepared statements 'to fix' document.
Done:
- cleanups and comments
- revision of prepared statement error codes.
- mysql_stmt_prepare is now can always be called (that is, you can reprepare
a statement)
- new implementation of mysql_stmt_close and fetch cancellation
include/errmsg.h:
- CR_NOT_ALL_PARAMS_BOUND - this error code wasn't used until now.
Apparently it was added in advance, but then interface of
mysql_stmt_bind_param changed. Now it's not possible to bind only some
parameters - either all or none of parameters are bound.
This error code is renamed to CR_PARAMS_NOT_BOUND
- CR_FETCH_CANCELLED - error code set on server side when fetch from
MYSQL_RES or MYSQL_STMT (in blocking mode) was cancelled because of
intercepting call to mysql_stmt_close
- CR_NO_DATA - this is proposed error code to return from
mysql_stmt_fetch_column if no row was fetched (by any type of fetch).
We always can fall back to CR_COMMANDS_OUT_OF_SYNC though.
Need reviewer's opinion on this one.
include/mysql.h:
- added unbuffered_fetch_owner member to MYSQL to point to MYSQL_RES
or MYSQL_STMT which is used to fetch result at the moment.
This is to be able to set CR_FETCH_CANCELLED error without fantoms.
- added unbuffered_fetch_cancelled boolean variable to MYSQL_STMT and
MYSQL_RES structures
- rename PREP_STMT_STATE -> enum enum_mysql_stmt_state
- members of MYSQL_STMT ordered by size.
- removed members of MYSQL_STMT: current_row, result_buffered,
last_fetched_column, last_fetched_buffer, query
- renamed members of MYSQL_STMT: param_buffers -> bind_param_done,
res_buffers -> bind_result_done
- now mysql_stmt_fetch calls stmt->read_row_func to read row either from
buffer or from network.
include/sql_common.h:
declaration for flush_use_result
libmysql/client_settings.h:
stmt_close declaration removed
libmysql/errmsg.c:
Error messages for changed and added error codes.
libmysql/libmysql.c:
Many changes:
- some unused variables removed
- cleanups
- better error reporting
- some function calls commented
- alloc_stmt_fields is now called right after execute, to not read
mysql->fields of some other statement
- new implementation of mysql_stmt_fetch - this is also with cursor
fetch in mind (to implement cursor fetch I'll just need to write
special read_row function for it, so this change will be local)
- implementation of fetch cancellation, including complete rewrite of
mysql_stmt_close
- now mysql_stmt_free_result doesn't free results of other statements.
sql-common/client.c:
- implementation of flush_use_result
- implementation of fetch cancellation
- changed behaviour of mysql_close in regard to mysql_stmt_close - now
mysql_close just set stmt->mysql to 0
2004-03-15 23:04:04 +01:00
|
|
|
else if (!(read_one_row(mysql, res->field_count, res->row, res->lengths)))
|
2003-05-02 18:07:41 +02:00
|
|
|
{
|
Intermediate commit of client library (cleanups + fixes of 3 items from
flaws list)
TODO:
* verify that no sequence of API calls produces SIGSEGV.
That is, verify that mysql_stmt_init -> mysql_stmt_fetch is OK,
or mysql_stmt_prepare -> mysql_stmt_fetch_column is OK and sets
meaningful error.
* remove alloc_stmt_fields call
* revise stmt->state codes and statement states.
* there are other items in prepared statements 'to fix' document.
Done:
- cleanups and comments
- revision of prepared statement error codes.
- mysql_stmt_prepare is now can always be called (that is, you can reprepare
a statement)
- new implementation of mysql_stmt_close and fetch cancellation
include/errmsg.h:
- CR_NOT_ALL_PARAMS_BOUND - this error code wasn't used until now.
Apparently it was added in advance, but then interface of
mysql_stmt_bind_param changed. Now it's not possible to bind only some
parameters - either all or none of parameters are bound.
This error code is renamed to CR_PARAMS_NOT_BOUND
- CR_FETCH_CANCELLED - error code set on server side when fetch from
MYSQL_RES or MYSQL_STMT (in blocking mode) was cancelled because of
intercepting call to mysql_stmt_close
- CR_NO_DATA - this is proposed error code to return from
mysql_stmt_fetch_column if no row was fetched (by any type of fetch).
We always can fall back to CR_COMMANDS_OUT_OF_SYNC though.
Need reviewer's opinion on this one.
include/mysql.h:
- added unbuffered_fetch_owner member to MYSQL to point to MYSQL_RES
or MYSQL_STMT which is used to fetch result at the moment.
This is to be able to set CR_FETCH_CANCELLED error without fantoms.
- added unbuffered_fetch_cancelled boolean variable to MYSQL_STMT and
MYSQL_RES structures
- rename PREP_STMT_STATE -> enum enum_mysql_stmt_state
- members of MYSQL_STMT ordered by size.
- removed members of MYSQL_STMT: current_row, result_buffered,
last_fetched_column, last_fetched_buffer, query
- renamed members of MYSQL_STMT: param_buffers -> bind_param_done,
res_buffers -> bind_result_done
- now mysql_stmt_fetch calls stmt->read_row_func to read row either from
buffer or from network.
include/sql_common.h:
declaration for flush_use_result
libmysql/client_settings.h:
stmt_close declaration removed
libmysql/errmsg.c:
Error messages for changed and added error codes.
libmysql/libmysql.c:
Many changes:
- some unused variables removed
- cleanups
- better error reporting
- some function calls commented
- alloc_stmt_fields is now called right after execute, to not read
mysql->fields of some other statement
- new implementation of mysql_stmt_fetch - this is also with cursor
fetch in mind (to implement cursor fetch I'll just need to write
special read_row function for it, so this change will be local)
- implementation of fetch cancellation, including complete rewrite of
mysql_stmt_close
- now mysql_stmt_free_result doesn't free results of other statements.
sql-common/client.c:
- implementation of flush_use_result
- implementation of fetch cancellation
- changed behaviour of mysql_close in regard to mysql_stmt_close - now
mysql_close just set stmt->mysql to 0
2004-03-15 23:04:04 +01:00
|
|
|
res->row_count++;
|
|
|
|
DBUG_RETURN(res->current_row=res->row);
|
2003-05-02 18:07:41 +02:00
|
|
|
}
|
Intermediate commit of client library (cleanups + fixes of 3 items from
flaws list)
TODO:
* verify that no sequence of API calls produces SIGSEGV.
That is, verify that mysql_stmt_init -> mysql_stmt_fetch is OK,
or mysql_stmt_prepare -> mysql_stmt_fetch_column is OK and sets
meaningful error.
* remove alloc_stmt_fields call
* revise stmt->state codes and statement states.
* there are other items in prepared statements 'to fix' document.
Done:
- cleanups and comments
- revision of prepared statement error codes.
- mysql_stmt_prepare is now can always be called (that is, you can reprepare
a statement)
- new implementation of mysql_stmt_close and fetch cancellation
include/errmsg.h:
- CR_NOT_ALL_PARAMS_BOUND - this error code wasn't used until now.
Apparently it was added in advance, but then interface of
mysql_stmt_bind_param changed. Now it's not possible to bind only some
parameters - either all or none of parameters are bound.
This error code is renamed to CR_PARAMS_NOT_BOUND
- CR_FETCH_CANCELLED - error code set on server side when fetch from
MYSQL_RES or MYSQL_STMT (in blocking mode) was cancelled because of
intercepting call to mysql_stmt_close
- CR_NO_DATA - this is proposed error code to return from
mysql_stmt_fetch_column if no row was fetched (by any type of fetch).
We always can fall back to CR_COMMANDS_OUT_OF_SYNC though.
Need reviewer's opinion on this one.
include/mysql.h:
- added unbuffered_fetch_owner member to MYSQL to point to MYSQL_RES
or MYSQL_STMT which is used to fetch result at the moment.
This is to be able to set CR_FETCH_CANCELLED error without fantoms.
- added unbuffered_fetch_cancelled boolean variable to MYSQL_STMT and
MYSQL_RES structures
- rename PREP_STMT_STATE -> enum enum_mysql_stmt_state
- members of MYSQL_STMT ordered by size.
- removed members of MYSQL_STMT: current_row, result_buffered,
last_fetched_column, last_fetched_buffer, query
- renamed members of MYSQL_STMT: param_buffers -> bind_param_done,
res_buffers -> bind_result_done
- now mysql_stmt_fetch calls stmt->read_row_func to read row either from
buffer or from network.
include/sql_common.h:
declaration for flush_use_result
libmysql/client_settings.h:
stmt_close declaration removed
libmysql/errmsg.c:
Error messages for changed and added error codes.
libmysql/libmysql.c:
Many changes:
- some unused variables removed
- cleanups
- better error reporting
- some function calls commented
- alloc_stmt_fields is now called right after execute, to not read
mysql->fields of some other statement
- new implementation of mysql_stmt_fetch - this is also with cursor
fetch in mind (to implement cursor fetch I'll just need to write
special read_row function for it, so this change will be local)
- implementation of fetch cancellation, including complete rewrite of
mysql_stmt_close
- now mysql_stmt_free_result doesn't free results of other statements.
sql-common/client.c:
- implementation of flush_use_result
- implementation of fetch cancellation
- changed behaviour of mysql_close in regard to mysql_stmt_close - now
mysql_close just set stmt->mysql to 0
2004-03-15 23:04:04 +01:00
|
|
|
DBUG_PRINT("info",("end of data"));
|
|
|
|
res->eof=1;
|
|
|
|
mysql->status=MYSQL_STATUS_READY;
|
|
|
|
/*
|
|
|
|
Reset only if owner points to us: there is a chance that somebody
|
|
|
|
started new query after mysql_stmt_close():
|
|
|
|
*/
|
|
|
|
if (mysql->unbuffered_fetch_owner == &res->unbuffered_fetch_cancelled)
|
|
|
|
mysql->unbuffered_fetch_owner= 0;
|
|
|
|
/* Don't clear handle in mysql_free_result */
|
|
|
|
res->handle=0;
|
2003-05-02 18:07:41 +02:00
|
|
|
}
|
|
|
|
DBUG_RETURN((MYSQL_ROW) NULL);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
MYSQL_ROW tmp;
|
|
|
|
if (!res->data_cursor)
|
|
|
|
{
|
|
|
|
DBUG_PRINT("info",("end of data"));
|
|
|
|
DBUG_RETURN(res->current_row=(MYSQL_ROW) NULL);
|
|
|
|
}
|
|
|
|
tmp = res->data_cursor->data;
|
|
|
|
res->data_cursor = res->data_cursor->next;
|
|
|
|
DBUG_RETURN(res->current_row=tmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-23 09:29:57 +01:00
|
|
|
/**************************************************************************
|
|
|
|
Get column lengths of the current row
|
|
|
|
If one uses mysql_use_result, res->lengths contains the length information,
|
|
|
|
else the lengths are calculated from the offset between pointers.
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
ulong * STDCALL
|
|
|
|
mysql_fetch_lengths(MYSQL_RES *res)
|
|
|
|
{
|
|
|
|
MYSQL_ROW column;
|
|
|
|
|
|
|
|
if (!(column=res->current_row))
|
|
|
|
return 0; /* Something is wrong */
|
|
|
|
if (res->data)
|
|
|
|
(*res->methods->fetch_lengths)(res->lengths, column, res->field_count);
|
|
|
|
return res->lengths;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-05-31 12:15:46 +02:00
|
|
|
int STDCALL
|
|
|
|
mysql_options(MYSQL *mysql,enum mysql_option option, const char *arg)
|
|
|
|
{
|
|
|
|
DBUG_ENTER("mysql_option");
|
|
|
|
DBUG_PRINT("enter",("option: %d",(int) option));
|
|
|
|
switch (option) {
|
|
|
|
case MYSQL_OPT_CONNECT_TIMEOUT:
|
|
|
|
mysql->options.connect_timeout= *(uint*) arg;
|
|
|
|
break;
|
2003-06-14 10:37:42 +02:00
|
|
|
case MYSQL_OPT_READ_TIMEOUT:
|
|
|
|
mysql->options.read_timeout= *(uint*) arg;
|
|
|
|
break;
|
|
|
|
case MYSQL_OPT_WRITE_TIMEOUT:
|
|
|
|
mysql->options.write_timeout= *(uint*) arg;
|
|
|
|
break;
|
2003-05-31 12:15:46 +02:00
|
|
|
case MYSQL_OPT_COMPRESS:
|
|
|
|
mysql->options.compress= 1; /* Remember for connect */
|
|
|
|
mysql->options.client_flag|= CLIENT_COMPRESS;
|
|
|
|
break;
|
2003-06-14 10:37:42 +02:00
|
|
|
case MYSQL_OPT_NAMED_PIPE: /* This option is depricated */
|
2003-05-31 12:15:46 +02:00
|
|
|
mysql->options.protocol=MYSQL_PROTOCOL_PIPE; /* Force named pipe */
|
|
|
|
break;
|
|
|
|
case MYSQL_OPT_LOCAL_INFILE: /* Allow LOAD DATA LOCAL ?*/
|
|
|
|
if (!arg || test(*(uint*) arg))
|
|
|
|
mysql->options.client_flag|= CLIENT_LOCAL_FILES;
|
|
|
|
else
|
|
|
|
mysql->options.client_flag&= ~CLIENT_LOCAL_FILES;
|
|
|
|
break;
|
|
|
|
case MYSQL_INIT_COMMAND:
|
|
|
|
add_init_command(&mysql->options,arg);
|
|
|
|
break;
|
|
|
|
case MYSQL_READ_DEFAULT_FILE:
|
|
|
|
my_free(mysql->options.my_cnf_file,MYF(MY_ALLOW_ZERO_PTR));
|
|
|
|
mysql->options.my_cnf_file=my_strdup(arg,MYF(MY_WME));
|
|
|
|
break;
|
|
|
|
case MYSQL_READ_DEFAULT_GROUP:
|
|
|
|
my_free(mysql->options.my_cnf_group,MYF(MY_ALLOW_ZERO_PTR));
|
|
|
|
mysql->options.my_cnf_group=my_strdup(arg,MYF(MY_WME));
|
|
|
|
break;
|
|
|
|
case MYSQL_SET_CHARSET_DIR:
|
|
|
|
my_free(mysql->options.charset_dir,MYF(MY_ALLOW_ZERO_PTR));
|
|
|
|
mysql->options.charset_dir=my_strdup(arg,MYF(MY_WME));
|
|
|
|
break;
|
|
|
|
case MYSQL_SET_CHARSET_NAME:
|
|
|
|
my_free(mysql->options.charset_name,MYF(MY_ALLOW_ZERO_PTR));
|
|
|
|
mysql->options.charset_name=my_strdup(arg,MYF(MY_WME));
|
|
|
|
break;
|
|
|
|
case MYSQL_OPT_PROTOCOL:
|
|
|
|
mysql->options.protocol= *(uint*) arg;
|
|
|
|
break;
|
|
|
|
case MYSQL_SHARED_MEMORY_BASE_NAME:
|
|
|
|
#ifdef HAVE_SMEM
|
|
|
|
if (mysql->options.shared_memory_base_name != def_shared_memory_base_name)
|
|
|
|
my_free(mysql->options.shared_memory_base_name,MYF(MY_ALLOW_ZERO_PTR));
|
|
|
|
mysql->options.shared_memory_base_name=my_strdup(arg,MYF(MY_WME));
|
|
|
|
#endif
|
2003-11-28 00:50:58 +01:00
|
|
|
break;
|
2003-06-18 12:58:57 +02:00
|
|
|
case MYSQL_OPT_USE_REMOTE_CONNECTION:
|
|
|
|
case MYSQL_OPT_USE_EMBEDDED_CONNECTION:
|
|
|
|
case MYSQL_OPT_GUESS_CONNECTION:
|
|
|
|
mysql->options.methods_to_use= option;
|
2003-05-31 12:15:46 +02:00
|
|
|
break;
|
2003-09-26 12:33:13 +02:00
|
|
|
case MYSQL_SET_CLIENT_IP:
|
|
|
|
mysql->options.client_ip= my_strdup(arg, MYF(MY_WME));
|
2003-11-28 11:14:10 +01:00
|
|
|
break;
|
2003-11-28 11:11:44 +01:00
|
|
|
case MYSQL_SECURE_AUTH:
|
|
|
|
mysql->options.secure_auth= *(my_bool *) arg;
|
2003-11-28 00:50:58 +01:00
|
|
|
break;
|
2004-12-17 22:17:25 +01:00
|
|
|
case MYSQL_REPORT_DATA_TRUNCATION:
|
|
|
|
mysql->options.report_data_truncation= test(*(my_bool *) arg);
|
|
|
|
break;
|
2005-07-07 01:29:31 +02:00
|
|
|
case MYSQL_OPT_RECONNECT:
|
|
|
|
mysql->reconnect= *(my_bool *) arg;
|
|
|
|
break;
|
2006-04-18 17:58:27 +02:00
|
|
|
case MYSQL_OPT_SSL_VERIFY_SERVER_CERT:
|
2006-08-25 17:54:33 +02:00
|
|
|
if (!arg || test(*(uint*) arg))
|
|
|
|
mysql->options.client_flag|= CLIENT_SSL_VERIFY_SERVER_CERT;
|
|
|
|
else
|
|
|
|
mysql->options.client_flag&= ~CLIENT_SSL_VERIFY_SERVER_CERT;
|
2006-04-18 17:58:27 +02:00
|
|
|
break;
|
2003-05-31 12:15:46 +02:00
|
|
|
default:
|
|
|
|
DBUG_RETURN(1);
|
|
|
|
}
|
|
|
|
DBUG_RETURN(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-05-02 18:07:41 +02:00
|
|
|
/****************************************************************************
|
|
|
|
Functions to get information from the MySQL structure
|
|
|
|
These are functions to make shared libraries more usable.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* MYSQL_RES */
|
|
|
|
my_ulonglong STDCALL mysql_num_rows(MYSQL_RES *res)
|
|
|
|
{
|
|
|
|
return res->row_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int STDCALL mysql_num_fields(MYSQL_RES *res)
|
|
|
|
{
|
|
|
|
return res->field_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint STDCALL mysql_errno(MYSQL *mysql)
|
|
|
|
{
|
|
|
|
return mysql->net.last_errno;
|
|
|
|
}
|
|
|
|
|
2005-07-16 07:13:40 +02:00
|
|
|
|
2003-05-02 18:07:41 +02:00
|
|
|
const char * STDCALL mysql_error(MYSQL *mysql)
|
|
|
|
{
|
|
|
|
return mysql->net.last_error;
|
|
|
|
}
|
2004-03-12 13:21:48 +01:00
|
|
|
|
2006-04-06 10:03:22 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
Get version number for server in a form easy to test on
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
mysql_get_server_version()
|
|
|
|
mysql Connection
|
|
|
|
|
|
|
|
EXAMPLE
|
|
|
|
4.1.0-alfa -> 40100
|
|
|
|
|
|
|
|
NOTES
|
|
|
|
We will ensure that a newer server always has a bigger number.
|
|
|
|
|
|
|
|
RETURN
|
|
|
|
Signed number > 323000
|
|
|
|
*/
|
|
|
|
|
|
|
|
ulong STDCALL
|
|
|
|
mysql_get_server_version(MYSQL *mysql)
|
|
|
|
{
|
|
|
|
uint major, minor, version;
|
|
|
|
char *pos= mysql->server_version, *end_pos;
|
|
|
|
major= (uint) strtoul(pos, &end_pos, 10); pos=end_pos+1;
|
|
|
|
minor= (uint) strtoul(pos, &end_pos, 10); pos=end_pos+1;
|
|
|
|
version= (uint) strtoul(pos, &end_pos, 10);
|
|
|
|
return (ulong) major*10000L+(ulong) (minor*100+version);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-16 07:13:40 +02:00
|
|
|
/*
|
|
|
|
mysql_set_character_set function sends SET NAMES cs_name to
|
|
|
|
the server (which changes character_set_client, character_set_result
|
|
|
|
and character_set_connection) and updates mysql->charset so other
|
|
|
|
functions like mysql_real_escape will work correctly.
|
|
|
|
*/
|
|
|
|
int STDCALL mysql_set_character_set(MYSQL *mysql, const char *cs_name)
|
|
|
|
{
|
|
|
|
struct charset_info_st *cs;
|
|
|
|
const char *save_csdir= charsets_dir;
|
|
|
|
|
|
|
|
if (mysql->options.charset_dir)
|
|
|
|
charsets_dir= mysql->options.charset_dir;
|
|
|
|
|
|
|
|
if (strlen(cs_name) < MY_CS_NAME_SIZE &&
|
|
|
|
(cs= get_charset_by_csname(cs_name, MY_CS_PRIMARY, MYF(0))))
|
|
|
|
{
|
|
|
|
char buff[MY_CS_NAME_SIZE + 10];
|
|
|
|
charsets_dir= save_csdir;
|
2006-04-06 09:43:13 +02:00
|
|
|
/* Skip execution of "SET NAMES" for pre-4.1 servers */
|
|
|
|
if (mysql_get_server_version(mysql) < 40100)
|
|
|
|
return 0;
|
2005-07-16 07:13:40 +02:00
|
|
|
sprintf(buff, "SET NAMES %s", cs_name);
|
|
|
|
if (!mysql_real_query(mysql, buff, strlen(buff)))
|
|
|
|
{
|
|
|
|
mysql->charset= cs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char cs_dir_name[FN_REFLEN];
|
|
|
|
get_charsets_dir(cs_dir_name);
|
|
|
|
mysql->net.last_errno= CR_CANT_READ_CHARSET;
|
|
|
|
strmov(mysql->net.sqlstate, unknown_sqlstate);
|
|
|
|
my_snprintf(mysql->net.last_error, sizeof(mysql->net.last_error) - 1,
|
|
|
|
ER(mysql->net.last_errno), cs_name, cs_dir_name);
|
|
|
|
|
|
|
|
}
|
|
|
|
charsets_dir= save_csdir;
|
|
|
|
return mysql->net.last_errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
|