libmysql.c 84.9 KB
Newer Older
unknown's avatar
unknown committed
1
/* Copyright (C) 2000-2003 MySQL AB
unknown's avatar
unknown committed
2 3 4 5 6 7 8

   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,
unknown's avatar
unknown committed
9
   but WITHOUT ANY WARRANTY; without even the implied warranty of
unknown's avatar
unknown committed
10 11 12 13 14 15
   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 */
unknown's avatar
unknown committed
16

unknown's avatar
unknown committed
17
#include <my_global.h>
unknown's avatar
unknown committed
18 19 20 21 22 23 24 25 26 27 28 29
#include <my_sys.h>
#include <mysys_err.h>
#include <m_string.h>
#include <m_ctype.h>
#include "mysql.h"
#include "mysql_version.h"
#include "mysqld_error.h"
#include "errmsg.h"
#include <violite.h>
#include <sys/stat.h>
#include <signal.h>
#include <time.h>
30
#include <assert.h> /* for DBUG_ASSERT() */
unknown's avatar
unknown committed
31 32 33 34 35 36 37 38 39
#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
40
#include <select.h>
unknown's avatar
unknown committed
41 42 43 44
#endif
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
unknown's avatar
unknown committed
45
#endif /* !defined(MSDOS) && !defined(__WIN__) */
46 47
#ifdef HAVE_POLL
#include <sys/poll.h>
unknown's avatar
unknown committed
48 49
#endif
#ifdef HAVE_SYS_UN_H
50
#include <sys/un.h>
unknown's avatar
unknown committed
51 52 53 54 55 56 57 58
#endif
#if defined(THREAD) && !defined(__WIN__)
#include <my_pthread.h>				/* because of signal()	*/
#endif
#ifndef INADDR_NONE
#define INADDR_NONE	-1
#endif

unknown's avatar
unknown committed
59
#include <sql_common.h>
unknown's avatar
SCRUM  
unknown committed
60
#include "client_settings.h"
unknown's avatar
unknown committed
61

unknown's avatar
unknown committed
62
ulong 		net_buffer_length=8192;
63
ulong		max_allowed_packet= 1024L*1024L*1024L;
64 65
ulong		net_read_timeout=  CLIENT_NET_READ_TIMEOUT;
ulong		net_write_timeout= CLIENT_NET_WRITE_TIMEOUT;
unknown's avatar
unknown committed
66

unknown's avatar
unknown committed
67

unknown's avatar
SCRUM  
unknown committed
68 69 70 71 72
#ifdef EMBEDDED_LIBRARY
#undef net_flush
my_bool	net_flush(NET *net);
#endif

unknown's avatar
unknown committed
73
#if defined(MSDOS) || defined(__WIN__)
unknown's avatar
unknown committed
74
/* socket_errno is defined in my_global.h for all platforms */
unknown's avatar
unknown committed
75 76 77 78
#define perror(A)
#else
#include <errno.h>
#define SOCKET_ERROR -1
unknown's avatar
unknown committed
79
#endif /* __WIN__ */
unknown's avatar
unknown committed
80

81 82 83 84 85
/*
  If allowed through some configuration, then this needs to
  be changed
*/
#define MAX_LONG_DATA_LENGTH 8192
86 87
#define unsigned_field(A) ((A)->flags & UNSIGNED_FLAG)

unknown's avatar
unknown committed
88
static void append_wild(char *to,char *end,const char *wild);
unknown's avatar
unknown committed
89
sig_handler pipe_sig_handler(int sig);
unknown's avatar
unknown committed
90 91
static ulong mysql_sub_escape_string(CHARSET_INFO *charset_info, char *to,
				     const char *from, ulong length);
unknown's avatar
unknown committed
92
my_bool stmt_close(MYSQL_STMT *stmt, my_bool skip_list);
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130

static my_bool mysql_client_init= 0;
static my_bool org_my_init_done= 0;

void mysql_once_init(void)
{
  if (!mysql_client_init)
  {
    mysql_client_init=1;
    org_my_init_done=my_init_done;
    my_init();					/* Will init threads */
    init_client_errs();
    if (!mysql_port)
    {
      mysql_port = MYSQL_PORT;
#ifndef MSDOS
      {
	struct servent *serv_ptr;
	char	*env;
	if ((serv_ptr = getservbyname("mysql", "tcp")))
	  mysql_port = (uint) ntohs((ushort) serv_ptr->s_port);
	if ((env = getenv("MYSQL_TCP_PORT")))
	  mysql_port =(uint) atoi(env);
      }
#endif
    }
    if (!mysql_unix_port)
    {
      char *env;
#ifdef __WIN__
      mysql_unix_port = (char*) MYSQL_NAMEDPIPE;
#else
      mysql_unix_port = (char*) MYSQL_UNIX_ADDR;
#endif
      if ((env = getenv("MYSQL_UNIX_PORT")))
	mysql_unix_port = env;
    }
    mysql_debug(NullS);
unknown's avatar
unknown committed
131 132
#if defined(SIGPIPE) && !defined(__WIN__)
    (void) signal(SIGPIPE, SIG_IGN);
133 134 135 136 137 138 139 140
#endif
  }
#ifdef THREAD
  else
    my_thread_init();         /* Init if new thread */
#endif
}

141
#ifndef EMBEDDED_LIBRARY
142 143 144
int STDCALL mysql_server_init(int argc __attribute__((unused)),
			      char **argv __attribute__((unused)),
			      char **groups __attribute__((unused)))
unknown's avatar
unknown committed
145
{
unknown's avatar
unknown committed
146
  mysql_once_init();
unknown's avatar
unknown committed
147 148
  return 0;
}
unknown's avatar
unknown committed
149

150
void STDCALL mysql_server_end()
151 152 153
{
  /* If library called my_init(), free memory allocated by it */
  if (!org_my_init_done)
154
  {
155
    my_end(0);
156 157 158 159 160
#ifndef THREAD
  /* Remove TRACING, if enabled by mysql_debug() */
    DBUG_POP();
#endif
  }
unknown's avatar
unknown committed
161 162
  else
    mysql_thread_end();
163
  mysql_client_init= org_my_init_done= 0;
164
}
unknown's avatar
unknown committed
165

unknown's avatar
SCRUM  
unknown committed
166
#endif /*EMBEDDED_LIBRARY*/
unknown's avatar
unknown committed
167

168
my_bool STDCALL mysql_thread_init()
unknown's avatar
unknown committed
169 170
{
#ifdef THREAD
171
  return my_thread_init();
unknown's avatar
unknown committed
172
#else
173
  return 0;
unknown's avatar
unknown committed
174 175 176
#endif
}

177
void STDCALL mysql_thread_end()
unknown's avatar
unknown committed
178 179
{
#ifdef THREAD
180
  my_thread_end();
unknown's avatar
unknown committed
181 182 183
#endif
}

unknown's avatar
unknown committed
184 185 186 187
/*
  Let the user specify that we don't want SIGPIPE;  This doesn't however work
  with threaded applications as we can have multiple read in progress.
*/
188 189 190 191 192
static MYSQL* spawn_init(MYSQL* parent, const char* host,
			 unsigned int port,
			 const char* user,
			 const char* passwd);

193

unknown's avatar
unknown committed
194 195

/*
196
  Expand wildcard to a sql string
unknown's avatar
unknown committed
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
*/

static void
append_wild(char *to, char *end, const char *wild)
{
  end-=5;					/* Some extra */
  if (wild && wild[0])
  {
    to=strmov(to," like '");
    while (*wild && to < end)
    {
      if (*wild == '\\' || *wild == '\'')
	*to++='\\';
      *to++= *wild++;
    }
    if (*wild)					/* Too small buffer */
      *to++='%';				/* Nicer this way */
    to[0]='\'';
    to[1]=0;
  }
}


/**************************************************************************
221
  Init debugging if MYSQL_DEBUG environment variable is found
unknown's avatar
unknown committed
222 223 224
**************************************************************************/

void STDCALL
unknown's avatar
unknown committed
225
mysql_debug(const char *debug __attribute__((unused)))
unknown's avatar
unknown committed
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
{
#ifndef DBUG_OFF
  char	*env;
  if (_db_on_)
    return;					/* Already using debugging */
  if (debug)
  {
    DEBUGGER_ON;
    DBUG_PUSH(debug);
  }
  else if ((env = getenv("MYSQL_DEBUG")))
  {
    DEBUGGER_ON;
    DBUG_PUSH(env);
#if !defined(_WINVER) && !defined(WINVER)
    puts("\n-------------------------------------------------------");
    puts("MYSQL_DEBUG found. libmysql started with the following:");
    puts(env);
    puts("-------------------------------------------------------\n");
#else
    {
      char buff[80];
      strmov(strmov(buff,"libmysql: "),env);
      MessageBox((HWND) 0,"Debugging variable MYSQL_DEBUG used",buff,MB_OK);
    }
#endif
  }
#endif
}


/**************************************************************************
258
  Close the server connection if we get a SIGPIPE
unknown's avatar
unknown committed
259 260 261
   ARGSUSED
**************************************************************************/

unknown's avatar
unknown committed
262
sig_handler
unknown's avatar
unknown committed
263 264 265 266 267 268 269 270
pipe_sig_handler(int sig __attribute__((unused)))
{
  DBUG_PRINT("info",("Hit by signal %d",sig));
#ifdef DONT_REMEMBER_SIGNAL
  (void) signal(SIGPIPE,pipe_sig_handler);
#endif
}

unknown's avatar
SCRUM  
unknown committed
271 272
/* perform query on master */
my_bool STDCALL mysql_master_query(MYSQL *mysql, const char *q,
unknown's avatar
unknown committed
273
				   unsigned long length)
unknown's avatar
SCRUM  
unknown committed
274 275 276 277
{
  DBUG_ENTER("mysql_master_query");
  if (mysql_master_send_query(mysql, q, length))
    DBUG_RETURN(1);
unknown's avatar
unknown committed
278
  DBUG_RETURN((*mysql->methods->read_query_result)(mysql));
unknown's avatar
SCRUM  
unknown committed
279
}
unknown's avatar
unknown committed
280

unknown's avatar
SCRUM  
unknown committed
281 282
my_bool STDCALL mysql_master_send_query(MYSQL *mysql, const char *q,
					unsigned long length)
unknown's avatar
unknown committed
283
{
unknown's avatar
SCRUM  
unknown committed
284 285 286 287 288 289
  MYSQL *master = mysql->master;
  DBUG_ENTER("mysql_master_send_query");
  if (!master->net.vio && !mysql_real_connect(master,0,0,0,0,0,0,0))
    DBUG_RETURN(1);
  mysql->last_used_con = master;
  DBUG_RETURN(simple_command(master, COM_QUERY, q, length, 1));
unknown's avatar
unknown committed
290 291 292
}


unknown's avatar
SCRUM  
unknown committed
293 294 295
/* perform query on slave */
my_bool STDCALL mysql_slave_query(MYSQL *mysql, const char *q,
				  unsigned long length)
unknown's avatar
unknown committed
296
{
unknown's avatar
SCRUM  
unknown committed
297 298 299
  DBUG_ENTER("mysql_slave_query");
  if (mysql_slave_send_query(mysql, q, length))
    DBUG_RETURN(1);
unknown's avatar
unknown committed
300
  DBUG_RETURN((*mysql->methods->read_query_result)(mysql));
unknown's avatar
unknown committed
301 302 303
}


unknown's avatar
SCRUM  
unknown committed
304 305
my_bool STDCALL mysql_slave_send_query(MYSQL *mysql, const char *q,
				   unsigned long length)
unknown's avatar
unknown committed
306
{
unknown's avatar
SCRUM  
unknown committed
307 308
  MYSQL* last_used_slave, *slave_to_use = 0;
  DBUG_ENTER("mysql_slave_send_query");
unknown's avatar
unknown committed
309

unknown's avatar
SCRUM  
unknown committed
310 311 312 313 314 315 316 317 318 319
  if ((last_used_slave = mysql->last_used_slave))
    slave_to_use = last_used_slave->next_slave;
  else
    slave_to_use = mysql->next_slave;
  /*
    Next_slave is always safe to use - we have a circular list of slaves
    if there are no slaves, mysql->next_slave == mysql
  */
  mysql->last_used_con = mysql->last_used_slave = slave_to_use;
  if (!slave_to_use->net.vio && !mysql_real_connect(slave_to_use, 0,0,0,
unknown's avatar
unknown committed
320
						    0,0,0,0))
unknown's avatar
SCRUM  
unknown committed
321 322 323
    DBUG_RETURN(1);
  DBUG_RETURN(simple_command(slave_to_use, COM_QUERY, q, length, 1));
}
unknown's avatar
unknown committed
324 325


unknown's avatar
SCRUM  
unknown committed
326 327 328
/* enable/disable parsing of all queries to decide
   if they go on master or slave */
void STDCALL mysql_enable_rpl_parse(MYSQL* mysql)
unknown's avatar
unknown committed
329
{
unknown's avatar
SCRUM  
unknown committed
330
  mysql->options.rpl_parse = 1;
unknown's avatar
unknown committed
331 332
}

unknown's avatar
SCRUM  
unknown committed
333
void STDCALL mysql_disable_rpl_parse(MYSQL* mysql)
unknown's avatar
unknown committed
334
{
unknown's avatar
SCRUM  
unknown committed
335
  mysql->options.rpl_parse = 0;
unknown's avatar
unknown committed
336 337
}

unknown's avatar
SCRUM  
unknown committed
338 339
/* get the value of the parse flag */
int STDCALL mysql_rpl_parse_enabled(MYSQL* mysql)
unknown's avatar
unknown committed
340
{
unknown's avatar
SCRUM  
unknown committed
341
  return mysql->options.rpl_parse;
unknown's avatar
unknown committed
342 343
}

unknown's avatar
SCRUM  
unknown committed
344 345
/*  enable/disable reads from master */
void STDCALL mysql_enable_reads_from_master(MYSQL* mysql)
unknown's avatar
unknown committed
346
{
unknown's avatar
SCRUM  
unknown committed
347 348
  mysql->options.no_master_reads = 0;
}
unknown's avatar
unknown committed
349

unknown's avatar
SCRUM  
unknown committed
350
void STDCALL mysql_disable_reads_from_master(MYSQL* mysql)
351 352 353 354
{
  mysql->options.no_master_reads = 1;
}

unknown's avatar
unknown committed
355
/* get the value of the master read flag */
356
my_bool STDCALL mysql_reads_from_master_enabled(MYSQL* mysql)
357 358 359 360
{
  return !(mysql->options.no_master_reads);
}

361 362 363 364 365

/*
  We may get an error while doing replication internals.
  In this case, we add a special explanation to the original
  error
366
*/
367 368

static void expand_error(MYSQL* mysql, int error)
369 370
{
  char tmp[MYSQL_ERRMSG_SIZE];
371 372 373 374 375 376
  char *p;
  uint err_length;
  strmake(tmp, mysql->net.last_error, MYSQL_ERRMSG_SIZE-1);
  p = strmake(mysql->net.last_error, ER(error), MYSQL_ERRMSG_SIZE-1);
  err_length= (uint) (p - mysql->net.last_error);
  strmake(p, tmp, MYSQL_ERRMSG_SIZE-1 - err_length);
377 378 379
  mysql->net.last_errno = error;
}

380 381 382
/*
  This function assumes we have just called SHOW SLAVE STATUS and have
  read the given result and row
383
*/
384

385
static my_bool get_master(MYSQL* mysql, MYSQL_RES* res, MYSQL_ROW row)
386 387
{
  MYSQL* master;
unknown's avatar
unknown committed
388
  DBUG_ENTER("get_master");
unknown's avatar
unknown committed
389
  if (mysql_num_fields(res) < 3)
unknown's avatar
unknown committed
390
    DBUG_RETURN(1); /* safety */
unknown's avatar
unknown committed
391

392
  /* use the same username and password as the original connection */
unknown's avatar
unknown committed
393
  if (!(master = spawn_init(mysql, row[0], atoi(row[2]), 0, 0)))
unknown's avatar
unknown committed
394
    DBUG_RETURN(1);
395
  mysql->master = master;
unknown's avatar
unknown committed
396
  DBUG_RETURN(0);
397 398
}

399 400 401 402

/*
  Assuming we already know that mysql points to a master connection,
  retrieve all the slaves
403
*/
404

405
static my_bool get_slaves_from_master(MYSQL* mysql)
406
{
407 408
  MYSQL_RES* res = 0;
  MYSQL_ROW row;
409
  my_bool error = 1;
410
  int has_auth_info;
unknown's avatar
unknown committed
411
  int port_ind;
unknown's avatar
unknown committed
412
  DBUG_ENTER("get_slaves_from_master");
unknown's avatar
unknown committed
413

414 415 416
  if (!mysql->net.vio && !mysql_real_connect(mysql,0,0,0,0,0,0,0))
  {
    expand_error(mysql, CR_PROBE_MASTER_CONNECT);
unknown's avatar
unknown committed
417
    DBUG_RETURN(1);
418 419 420
  }

  if (mysql_query(mysql, "SHOW SLAVE HOSTS") ||
421
      !(res = mysql_store_result(mysql)))
422 423
  {
    expand_error(mysql, CR_PROBE_SLAVE_HOSTS);
unknown's avatar
unknown committed
424
    DBUG_RETURN(1);
425 426
  }

427
  switch (mysql_num_fields(res)) {
unknown's avatar
unknown committed
428 429 430 431 432 433 434 435
  case 5:
    has_auth_info = 0;
    port_ind=2;
    break;
  case 7:
    has_auth_info = 1;
    port_ind=4;
    break;
436 437 438 439 440 441 442 443 444 445 446
  default:
    goto err;
  }

  while ((row = mysql_fetch_row(res)))
  {
    MYSQL* slave;
    const char* tmp_user, *tmp_pass;

    if (has_auth_info)
    {
unknown's avatar
unknown committed
447 448
      tmp_user = row[2];
      tmp_pass = row[3];
449 450 451 452 453 454 455
    }
    else
    {
      tmp_user = mysql->user;
      tmp_pass = mysql->passwd;
    }

unknown's avatar
unknown committed
456
    if (!(slave = spawn_init(mysql, row[1], atoi(row[port_ind]),
unknown's avatar
unknown committed
457
			     tmp_user, tmp_pass)))
458
      goto err;
unknown's avatar
unknown committed
459

460 461 462 463 464 465
    /* Now add slave into the circular linked list */
    slave->next_slave = mysql->next_slave;
    mysql->next_slave = slave;
  }
  error = 0;
err:
unknown's avatar
unknown committed
466
  if (res)
unknown's avatar
unknown committed
467
    mysql_free_result(res);
unknown's avatar
unknown committed
468
  DBUG_RETURN(error);
469 470
}

471

472
my_bool STDCALL mysql_rpl_probe(MYSQL* mysql)
473
{
unknown's avatar
unknown committed
474
  MYSQL_RES *res= 0;
475
  MYSQL_ROW row;
476
  my_bool error= 1;
unknown's avatar
unknown committed
477 478
  DBUG_ENTER("mysql_rpl_probe");

479 480 481 482 483
  /*
    First determine the replication role of the server we connected to
    the most reliable way to do this is to run SHOW SLAVE STATUS and see
    if we have a non-empty master host. This is still not fool-proof -
    it is not a sin to have a master that has a dormant slave thread with
unknown's avatar
unknown committed
484
    a non-empty master host. However, it is more reliable to check
485
    for empty master than whether the slave thread is actually running
486
  */
487
  if (mysql_query(mysql, "SHOW SLAVE STATUS") ||
488
      !(res = mysql_store_result(mysql)))
489 490
  {
    expand_error(mysql, CR_PROBE_SLAVE_STATUS);
unknown's avatar
unknown committed
491
    DBUG_RETURN(1);
492
  }
unknown's avatar
unknown committed
493

494 495 496 497 498 499
  row= mysql_fetch_row(res);
  /*
    Check master host for emptiness/NULL
    For MySQL 4.0 it's enough to check for row[0]
  */
  if (row && row[0] && *(row[0]))
500 501
  {
    /* this is a slave, ask it for the master */
502
    if (get_master(mysql, res, row) || get_slaves_from_master(mysql))
503 504 505 506 507
      goto err;
  }
  else
  {
    mysql->master = mysql;
508
    if (get_slaves_from_master(mysql))
509 510 511 512 513
      goto err;
  }

  error = 0;
err:
unknown's avatar
unknown committed
514
  if (res)
515
    mysql_free_result(res);
unknown's avatar
unknown committed
516
  DBUG_RETURN(error);
517 518 519
}


520 521 522 523 524 525 526 527 528 529
/*
  Make a not so fool-proof decision on where the query should go, to
  the master or the slave. Ideally the user should always make this
  decision himself with mysql_master_query() or mysql_slave_query().
  However, to be able to more easily port the old code, we support the
  option of an educated guess - this should work for most applications,
  however, it may make the wrong decision in some particular cases. If
  that happens, the user would have to change the code to call
  mysql_master_query() or mysql_slave_query() explicitly in the place
  where we have made the wrong decision
530
*/
531

532 533
enum mysql_rpl_type
STDCALL mysql_rpl_query_type(const char* q, int len)
534
{
unknown's avatar
unknown committed
535
  const char *q_end= q + len;
536
  for (; q < q_end; ++q)
537 538
  {
    char c;
539
    if (my_isalpha(&my_charset_latin1, (c= *q)))
540
    {
541
      switch (my_tolower(&my_charset_latin1,c)) {
542 543 544 545 546 547 548
      case 'i':  /* insert */
      case 'u':  /* update or unlock tables */
      case 'l':  /* lock tables or load data infile */
      case 'd':  /* drop or delete */
      case 'a':  /* alter */
	return MYSQL_RPL_MASTER;
      case 'c':  /* create or check */
549
	return my_tolower(&my_charset_latin1,q[1]) == 'h' ? MYSQL_RPL_ADMIN :
unknown's avatar
unknown committed
550
	  MYSQL_RPL_MASTER;
551
      case 's': /* select or show */
552
	return my_tolower(&my_charset_latin1,q[1]) == 'h' ? MYSQL_RPL_ADMIN :
unknown's avatar
unknown committed
553
	  MYSQL_RPL_SLAVE;
554 555 556
      case 'f': /* flush */
      case 'r': /* repair */
      case 'g': /* grant */
unknown's avatar
SCRUM  
unknown committed
557 558 559 560 561
	return MYSQL_RPL_ADMIN;
      default:
	return MYSQL_RPL_SLAVE;
      }
    }
unknown's avatar
unknown committed
562
  }
unknown's avatar
SCRUM  
unknown committed
563
  return MYSQL_RPL_MASTER;		/* By default, send to master */
unknown's avatar
unknown committed
564 565 566
}


unknown's avatar
SCRUM  
unknown committed
567 568 569 570
/**************************************************************************
  Connect to sql server
  If host == 0 then use localhost
**************************************************************************/
571

unknown's avatar
SCRUM  
unknown committed
572 573 574 575
#ifdef USE_OLD_FUNCTIONS
MYSQL * STDCALL
mysql_connect(MYSQL *mysql,const char *host,
	      const char *user, const char *passwd)
unknown's avatar
unknown committed
576
{
unknown's avatar
SCRUM  
unknown committed
577 578
  MYSQL *res;
  mysql=mysql_init(mysql);			/* Make it thread safe */
unknown's avatar
unknown committed
579
  {
unknown's avatar
SCRUM  
unknown committed
580 581 582 583 584 585 586
    DBUG_ENTER("mysql_connect");
    if (!(res=mysql_real_connect(mysql,host,user,passwd,NullS,0,NullS,0)))
    {
      if (mysql->free_me)
	my_free((gptr) mysql,MYF(0));
    }
    DBUG_RETURN(res);
unknown's avatar
unknown committed
587
  }
unknown's avatar
unknown committed
588
}
unknown's avatar
SCRUM  
unknown committed
589
#endif
unknown's avatar
unknown committed
590

591

unknown's avatar
unknown committed
592
/**************************************************************************
unknown's avatar
unknown committed
593
  Change user and database
unknown's avatar
unknown committed
594 595
**************************************************************************/

unknown's avatar
unknown committed
596
my_bool	STDCALL mysql_change_user(MYSQL *mysql, const char *user,
unknown's avatar
unknown committed
597 598
				  const char *passwd, const char *db)
{
unknown's avatar
unknown committed
599
  char buff[512],*end=buff;
unknown's avatar
unknown committed
600 601
  NET *net= &mysql->net;
  ulong pkt_length;
unknown's avatar
unknown committed
602 603 604 605 606 607
  DBUG_ENTER("mysql_change_user");

  if (!user)
    user="";
  if (!passwd)
    passwd="";
unknown's avatar
unknown committed
608

609
  /* Store user into the buffer */
unknown's avatar
unknown committed
610
  end=strmov(end,user)+1;
unknown's avatar
unknown committed
611

612 613
  /* write scrambled password according to server capabilities */
  if (passwd[0])
unknown's avatar
unknown committed
614
  {
unknown's avatar
unknown committed
615
    if (mysql->server_capabilities & CLIENT_SECURE_CONNECTION)
unknown's avatar
unknown committed
616
    {
unknown's avatar
unknown committed
617 618 619
      *end++= SCRAMBLE_LENGTH;
      scramble(end, mysql->scramble, passwd);
      end+= SCRAMBLE_LENGTH;
unknown's avatar
unknown committed
620
    }
unknown's avatar
unknown committed
621
    else
622 623 624
    {
      scramble_323(end, mysql->scramble, passwd);
      end+= SCRAMBLE_LENGTH_323 + 1;
unknown's avatar
unknown committed
625
    }
unknown's avatar
unknown committed
626 627
  }
  else
628
    *end++= '\0';                               /* empty password */
unknown's avatar
unknown committed
629
  /* Add database if needed */
unknown's avatar
unknown committed
630
  end= strmov(end, db ? db : "") + 1;
unknown's avatar
unknown committed
631 632

  /* Write authentication package */
unknown's avatar
unknown committed
633
  simple_command(mysql,COM_CHANGE_USER, buff,(ulong) (end-buff),1);
unknown's avatar
unknown committed
634

unknown's avatar
unknown committed
635
  pkt_length= net_safe_read(mysql);
636 637

  if (pkt_length == packet_error)
unknown's avatar
unknown committed
638
    goto error;
unknown's avatar
unknown committed
639

640
  if (pkt_length == 1 && net->read_pos[0] == 254 &&
641 642 643 644 645 646
      mysql->server_capabilities & CLIENT_SECURE_CONNECTION)
  {
    /*
      By sending this very specific reply server asks us to send scrambled
      password in old format. The reply contains scramble_323.
    */
647
    scramble_323(buff, mysql->scramble, passwd);
648 649 650 651 652 653 654 655 656 657 658 659
    if (my_net_write(net, buff, SCRAMBLE_LENGTH_323 + 1) || net_flush(net))
    {
      net->last_errno= CR_SERVER_LOST;
      strmov(net->sqlstate, unknown_sqlstate);
      strmov(net->last_error,ER(net->last_errno));
      goto error;
    }
    /* Read what server thinks about out new auth message report */
    if (net_safe_read(mysql) == packet_error)
      goto error;
  }

660
  /* Free old connect information */
unknown's avatar
unknown committed
661 662 663 664
  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));

665
  /* alloc new connect information */
unknown's avatar
unknown committed
666 667 668 669
  mysql->user=  my_strdup(user,MYF(MY_WME));
  mysql->passwd=my_strdup(passwd,MYF(MY_WME));
  mysql->db=    db ? my_strdup(db,MYF(MY_WME)) : 0;
  DBUG_RETURN(0);
unknown's avatar
unknown committed
670

671
error:
unknown's avatar
unknown committed
672
  DBUG_RETURN(1);
unknown's avatar
unknown committed
673 674
}

unknown's avatar
unknown committed
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
#if defined(HAVE_GETPWUID) && defined(NO_GETPWUID_DECL)
struct passwd *getpwuid(uid_t);
char* getlogin(void);
#endif

#if defined(__NETWARE__)
/* default to "root" on NetWare */
void read_user_name(char *name)
{
  char *str=getenv("USER");
  strmake(name, str ? str : "UNKNOWN_USER", USERNAME_LENGTH);
}

#elif !defined(MSDOS) && ! defined(VMS) && !defined(__WIN__) && !defined(OS2)

void read_user_name(char *name)
{
  DBUG_ENTER("read_user_name");
  if (geteuid() == 0)
    (void) strmov(name,"root");		/* allow use of surun */
  else
  {
#ifdef HAVE_GETPWUID
    struct passwd *skr;
    const char *str;
    if ((str=getlogin()) == NULL)
    {
      if ((skr=getpwuid(geteuid())) != NULL)
	str=skr->pw_name;
      else if (!(str=getenv("USER")) && !(str=getenv("LOGNAME")) &&
	       !(str=getenv("LOGIN")))
	str="UNKNOWN_USER";
    }
    (void) strmake(name,str,USERNAME_LENGTH);
#elif HAVE_CUSERID
    (void) cuserid(name);
#else
    strmov(name,"UNKNOWN_USER");
#endif
  }
  DBUG_VOID_RETURN;
}

#else /* If MSDOS || VMS */

void read_user_name(char *name)
{
  char *str=getenv("USER");		/* ODBC will send user variable */
  strmake(name,str ? str : "ODBC", USERNAME_LENGTH);
}

#endif

my_bool send_file_to_server(MYSQL *mysql, const char *filename)
{
  int fd, readcount;
  my_bool result= 1;
  uint packet_length=MY_ALIGN(mysql->net.max_packet-16,IO_SIZE);
  char *buf, tmp_name[FN_REFLEN];
  NET *net= &mysql->net;
  DBUG_ENTER("send_file_to_server");

  if (!(buf=my_malloc(packet_length,MYF(0))))
  {
    strmov(net->sqlstate, unknown_sqlstate);
    strmov(net->last_error, ER(net->last_errno=CR_OUT_OF_MEMORY));
    DBUG_RETURN(1);
  }

  fn_format(tmp_name,filename,"","",4);		/* Convert to client format */
  if ((fd = my_open(tmp_name,O_RDONLY, MYF(0))) < 0)
  {
    my_net_write(net,"",0);		/* Server needs one packet */
    net_flush(net);
    strmov(net->sqlstate, unknown_sqlstate);
    net->last_errno=EE_FILENOTFOUND;
    my_snprintf(net->last_error,sizeof(net->last_error)-1,
		EE(net->last_errno),tmp_name, errno);
    goto err;
  }

  while ((readcount = (int) my_read(fd,(byte*) buf,packet_length,MYF(0))) > 0)
  {
    if (my_net_write(net,buf,readcount))
    {
      DBUG_PRINT("error",("Lost connection to MySQL server during LOAD DATA of local file"));
      strmov(net->sqlstate, unknown_sqlstate);
      net->last_errno=CR_SERVER_LOST;
      strmov(net->last_error,ER(net->last_errno));
      goto err;
    }
  }
  /* Send empty packet to mark end of file */
  if (my_net_write(net,"",0) || net_flush(net))
  {
    strmov(net->sqlstate, unknown_sqlstate);
    net->last_errno=CR_SERVER_LOST;
    sprintf(net->last_error,ER(net->last_errno),errno);
    goto err;
  }
  if (readcount < 0)
  {
    strmov(net->sqlstate, unknown_sqlstate);
    net->last_errno=EE_READ; /* the errmsg for not entire file read */
    my_snprintf(net->last_error,sizeof(net->last_error)-1,
		tmp_name,errno);
    goto err;
  }
  result=0;					/* Ok */

err:
  if (fd >= 0)
    (void) my_close(fd,MYF(0));
  my_free(buf,MYF(0));
  DBUG_RETURN(result);
}


unknown's avatar
unknown committed
793
/**************************************************************************
794 795
  Do a query. If query returned rows, free old rows.
  Read data by mysql_store_result or by repeat call of mysql_fetch_row
unknown's avatar
unknown committed
796 797 798 799 800 801 802 803
**************************************************************************/

int STDCALL
mysql_query(MYSQL *mysql, const char *query)
{
  return mysql_real_query(mysql,query, (uint) strlen(query));
}

unknown's avatar
unknown committed
804

805
static MYSQL* spawn_init(MYSQL* parent, const char* host,
unknown's avatar
unknown committed
806 807
			 unsigned int port, const char* user,
			 const char* passwd)
808 809 810 811
{
  MYSQL* child;
  if (!(child = mysql_init(0)))
    return 0;
unknown's avatar
unknown committed
812

813 814
  child->options.user = my_strdup((user) ? user :
				  (parent->user ? parent->user :
unknown's avatar
unknown committed
815 816 817 818
				   parent->options.user), MYF(0));
  child->options.password = my_strdup((passwd) ? passwd :
				      (parent->passwd ?
				       parent->passwd :
819 820
				       parent->options.password), MYF(0));
  child->options.port = port;
unknown's avatar
unknown committed
821 822 823 824 825
  child->options.host = my_strdup((host) ? host :
				  (parent->host ?
				   parent->host :
				   parent->options.host), MYF(0));
  if (parent->db)
826
    child->options.db = my_strdup(parent->db, MYF(0));
unknown's avatar
unknown committed
827
  else if (parent->options.db)
828 829 830 831 832 833 834 835
    child->options.db = my_strdup(parent->options.db, MYF(0));

  return child;
}


int
STDCALL mysql_set_master(MYSQL* mysql, const char* host,
unknown's avatar
unknown committed
836 837
			 unsigned int port, const char* user,
			 const char* passwd)
838 839 840
{
  if (mysql->master != mysql && !mysql->master->rpl_pivot)
    mysql_close(mysql->master);
unknown's avatar
unknown committed
841
  if (!(mysql->master = spawn_init(mysql, host, port, user, passwd)))
842 843 844 845 846 847
    return 1;
  return 0;
}

int
STDCALL mysql_add_slave(MYSQL* mysql, const char* host,
unknown's avatar
unknown committed
848 849 850
			unsigned int port,
			const char* user,
			const char* passwd)
851 852
{
  MYSQL* slave;
unknown's avatar
unknown committed
853
  if (!(slave = spawn_init(mysql, host, port, user, passwd)))
854 855 856 857 858 859
    return 1;
  slave->next_slave = mysql->next_slave;
  mysql->next_slave = slave;
  return 0;
}

unknown's avatar
unknown committed
860
/**************************************************************************
861
  Return next field of the query results
unknown's avatar
unknown committed
862 863 864 865 866 867 868 869 870 871
**************************************************************************/

MYSQL_FIELD * STDCALL
mysql_fetch_field(MYSQL_RES *result)
{
  if (result->current_field >= result->field_count)
    return(NULL);
  return &result->fields[result->current_field++];
}

872 873 874 875 876 877 878

/**************************************************************************
  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.
**************************************************************************/

unknown's avatar
unknown committed
879 880 881
ulong * STDCALL
mysql_fetch_lengths(MYSQL_RES *res)
{
882
  MYSQL_ROW column;
unknown's avatar
unknown committed
883 884 885 886

  if (!(column=res->current_row))
    return 0;					/* Something is wrong */
  if (res->data)
887
    (*res->methods->fetch_lengths)(res->lengths, column, res->field_count);
unknown's avatar
unknown committed
888 889 890
  return res->lengths;
}

891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907

/**************************************************************************
  Move to a specific row and column
**************************************************************************/

void STDCALL
mysql_data_seek(MYSQL_RES *result, my_ulonglong row)
{
  MYSQL_ROWS	*tmp=0;
  DBUG_PRINT("info",("mysql_data_seek(%ld)",(long) row));
  if (result->data)
    for (tmp=result->data->data; row-- && tmp ; tmp = tmp->next) ;
  result->current_row=0;
  result->data_cursor = tmp;
}


unknown's avatar
unknown committed
908
/*************************************************************************
909 910 911
  put the row or field cursor one a position one got from mysql_row_tell()
  This doesn't restore any data. The next mysql_fetch_row or
  mysql_fetch_field will return the next row or field after the last used
unknown's avatar
unknown committed
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931
*************************************************************************/

MYSQL_ROW_OFFSET STDCALL
mysql_row_seek(MYSQL_RES *result, MYSQL_ROW_OFFSET row)
{
  MYSQL_ROW_OFFSET return_value=result->data_cursor;
  result->current_row= 0;
  result->data_cursor= row;
  return return_value;
}


MYSQL_FIELD_OFFSET STDCALL
mysql_field_seek(MYSQL_RES *result, MYSQL_FIELD_OFFSET field_offset)
{
  MYSQL_FIELD_OFFSET return_value=result->current_field;
  result->current_field=field_offset;
  return return_value;
}

932

unknown's avatar
unknown committed
933
/*****************************************************************************
934
  List all databases
unknown's avatar
unknown committed
935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950
*****************************************************************************/

MYSQL_RES * STDCALL
mysql_list_dbs(MYSQL *mysql, const char *wild)
{
  char buff[255];
  DBUG_ENTER("mysql_list_dbs");

  append_wild(strmov(buff,"show databases"),buff+sizeof(buff),wild);
  if (mysql_query(mysql,buff))
    DBUG_RETURN(0);
  DBUG_RETURN (mysql_store_result(mysql));
}


/*****************************************************************************
951 952
  List all tables in a database
  If wild is given then only the tables matching wild is returned
unknown's avatar
unknown committed
953 954 955 956 957 958 959 960 961 962 963 964 965 966
*****************************************************************************/

MYSQL_RES * STDCALL
mysql_list_tables(MYSQL *mysql, const char *wild)
{
  char buff[255];
  DBUG_ENTER("mysql_list_tables");

  append_wild(strmov(buff,"show tables"),buff+sizeof(buff),wild);
  if (mysql_query(mysql,buff))
    DBUG_RETURN(0);
  DBUG_RETURN (mysql_store_result(mysql));
}

unknown's avatar
unknown committed
967
MYSQL_FIELD *cli_list_fields(MYSQL *mysql)
unknown's avatar
SCRUM  
unknown committed
968 969 970 971 972 973
{
  MYSQL_DATA *query;
  if (!(query= cli_read_rows(mysql,(MYSQL_FIELD*) 0, 
			     protocol_41(mysql) ? 8 : 6)))
    return NULL;

unknown's avatar
unknown committed
974
  mysql->field_count= (uint) query->rows;
unknown's avatar
SCRUM  
unknown committed
975
  return unpack_fields(query,&mysql->field_alloc,
unknown's avatar
unknown committed
976
		       mysql->field_count, 1, mysql->server_capabilities);
unknown's avatar
SCRUM  
unknown committed
977 978 979
}


unknown's avatar
unknown committed
980
/**************************************************************************
981 982 983 984
  List all fields in a table
  If wild is given then only the fields matching wild is returned
  Instead of this use query:
  show fields in 'table' like "wild"
unknown's avatar
unknown committed
985 986 987
**************************************************************************/

MYSQL_RES * STDCALL
unknown's avatar
SCRUM  
unknown committed
988
mysql_list_fields(MYSQL *mysql, const char *table, const char *wild)
unknown's avatar
unknown committed
989
{
unknown's avatar
SCRUM  
unknown committed
990 991
  MYSQL_RES   *result;
  MYSQL_FIELD *fields;
unknown's avatar
unknown committed
992 993 994 995 996
  char	     buff[257],*end;
  DBUG_ENTER("mysql_list_fields");
  DBUG_PRINT("enter",("table: '%s'  wild: '%s'",table,wild ? wild : ""));

  end=strmake(strmake(buff, table,128)+1,wild ? wild : "",128);
unknown's avatar
SCRUM  
unknown committed
997
  free_old_query(mysql);
unknown's avatar
unknown committed
998
  if (simple_command(mysql,COM_FIELD_LIST,buff,(ulong) (end-buff),1) ||
unknown's avatar
SCRUM  
unknown committed
999
      !(fields= (*mysql->methods->list_fields)(mysql)))
unknown's avatar
unknown committed
1000 1001 1002 1003 1004
    DBUG_RETURN(NULL);

  if (!(result = (MYSQL_RES *) my_malloc(sizeof(MYSQL_RES),
					 MYF(MY_WME | MY_ZEROFILL))))
    DBUG_RETURN(NULL);
unknown's avatar
SCRUM  
unknown committed
1005

1006
  result->methods= mysql->methods;
unknown's avatar
unknown committed
1007 1008
  result->field_alloc=mysql->field_alloc;
  mysql->fields=0;
unknown's avatar
SCRUM  
unknown committed
1009 1010
  result->field_count = mysql->field_count;
  result->fields= fields;
unknown's avatar
unknown committed
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
  result->eof=1;
  DBUG_RETURN(result);
}

/* List all running processes (threads) in server */

MYSQL_RES * STDCALL
mysql_list_processes(MYSQL *mysql)
{
  MYSQL_DATA *fields;
  uint field_count;
  uchar *pos;
  DBUG_ENTER("mysql_list_processes");

  LINT_INIT(fields);
  if (simple_command(mysql,COM_PROCESS_INFO,0,0,0))
    DBUG_RETURN(0);
  free_old_query(mysql);
  pos=(uchar*) mysql->net.read_pos;
  field_count=(uint) net_field_length(&pos);
unknown's avatar
SCRUM  
unknown committed
1031 1032
  if (!(fields = (*mysql->methods->read_rows)(mysql,(MYSQL_FIELD*) 0,
					      protocol_41(mysql) ? 7 : 5)))
unknown's avatar
unknown committed
1033 1034
    DBUG_RETURN(NULL);
  if (!(mysql->fields=unpack_fields(fields,&mysql->field_alloc,field_count,0,
1035
				    mysql->server_capabilities)))
unknown's avatar
unknown committed
1036 1037 1038 1039 1040 1041
    DBUG_RETURN(0);
  mysql->status=MYSQL_STATUS_GET_RESULT;
  mysql->field_count=field_count;
  DBUG_RETURN(mysql_store_result(mysql));
}

1042
#ifdef USE_OLD_FUNCTIONS
unknown's avatar
unknown committed
1043 1044 1045 1046 1047
int  STDCALL
mysql_create_db(MYSQL *mysql, const char *db)
{
  DBUG_ENTER("mysql_createdb");
  DBUG_PRINT("enter",("db: %s",db));
unknown's avatar
unknown committed
1048
  DBUG_RETURN(simple_command(mysql,COM_CREATE_DB,db, (ulong) strlen(db),0));
unknown's avatar
unknown committed
1049 1050 1051 1052 1053 1054 1055 1056
}


int  STDCALL
mysql_drop_db(MYSQL *mysql, const char *db)
{
  DBUG_ENTER("mysql_drop_db");
  DBUG_PRINT("enter",("db: %s",db));
unknown's avatar
unknown committed
1057
  DBUG_RETURN(simple_command(mysql,COM_DROP_DB,db,(ulong) strlen(db),0));
unknown's avatar
unknown committed
1058
}
1059
#endif
unknown's avatar
unknown committed
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081


int STDCALL
mysql_shutdown(MYSQL *mysql)
{
  DBUG_ENTER("mysql_shutdown");
  DBUG_RETURN(simple_command(mysql,COM_SHUTDOWN,0,0,0));
}


int STDCALL
mysql_refresh(MYSQL *mysql,uint options)
{
  uchar bits[1];
  DBUG_ENTER("mysql_refresh");
  bits[0]= (uchar) options;
  DBUG_RETURN(simple_command(mysql,COM_REFRESH,(char*) bits,1,0));
}

int STDCALL
mysql_kill(MYSQL *mysql,ulong pid)
{
1082
  char buff[4];
unknown's avatar
unknown committed
1083 1084
  DBUG_ENTER("mysql_kill");
  int4store(buff,pid);
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
  DBUG_RETURN(simple_command(mysql,COM_PROCESS_KILL,buff,sizeof(buff),0));
}


int STDCALL
mysql_set_server_option(MYSQL *mysql, enum enum_mysql_set_option option)
{
  char buff[2];
  DBUG_ENTER("mysql_set_server_option");
  int2store(buff, (uint) option);
  DBUG_RETURN(simple_command(mysql, COM_SET_OPTION, buff, sizeof(buff), 0));
unknown's avatar
unknown committed
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
}


int STDCALL
mysql_dump_debug_info(MYSQL *mysql)
{
  DBUG_ENTER("mysql_dump_debug_info");
  DBUG_RETURN(simple_command(mysql,COM_DEBUG,0,0,0));
}

unknown's avatar
unknown committed
1106
const char *cli_read_statistic(MYSQL *mysql)
unknown's avatar
unknown committed
1107 1108 1109 1110
{
  mysql->net.read_pos[mysql->packet_length]=0;	/* End of stat string */
  if (!mysql->net.read_pos[0])
  {
1111
    strmov(mysql->net.sqlstate, unknown_sqlstate);
unknown's avatar
unknown committed
1112 1113 1114 1115
    mysql->net.last_errno=CR_WRONG_HOST_INFO;
    strmov(mysql->net.last_error, ER(mysql->net.last_errno));
    return mysql->net.last_error;
  }
unknown's avatar
unknown committed
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
  return (char*) mysql->net.read_pos;
}

const char * STDCALL
mysql_stat(MYSQL *mysql)
{
  DBUG_ENTER("mysql_stat");
  if (simple_command(mysql,COM_STATISTICS,0,0,0))
    return mysql->net.last_error;
  DBUG_RETURN((*mysql->methods->read_statistic)(mysql));
unknown's avatar
unknown committed
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
}


int STDCALL
mysql_ping(MYSQL *mysql)
{
  DBUG_ENTER("mysql_ping");
  DBUG_RETURN(simple_command(mysql,COM_PING,0,0,0));
}


unknown's avatar
unknown committed
1137
const char * STDCALL
unknown's avatar
unknown committed
1138 1139 1140 1141 1142 1143
mysql_get_server_info(MYSQL *mysql)
{
  return((char*) mysql->server_version);
}


1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172
/*
  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);
}


unknown's avatar
unknown committed
1173
const char * STDCALL
unknown's avatar
unknown committed
1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185
mysql_get_host_info(MYSQL *mysql)
{
  return(mysql->host_info);
}


uint STDCALL
mysql_get_proto_info(MYSQL *mysql)
{
  return (mysql->protocol_version);
}

unknown's avatar
unknown committed
1186
const char * STDCALL
unknown's avatar
unknown committed
1187 1188 1189 1190 1191
mysql_get_client_info(void)
{
  return (char*) MYSQL_SERVER_VERSION;
}

unknown's avatar
unknown committed
1192 1193 1194 1195 1196
ulong STDCALL mysql_get_client_version(void)
{
  return MYSQL_VERSION_ID;
}

unknown's avatar
unknown committed
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211
my_bool STDCALL mysql_eof(MYSQL_RES *res)
{
  return res->eof;
}

MYSQL_FIELD * STDCALL mysql_fetch_field_direct(MYSQL_RES *res,uint fieldnr)
{
  return &(res)->fields[fieldnr];
}

MYSQL_FIELD * STDCALL mysql_fetch_fields(MYSQL_RES *res)
{
  return (res)->fields;
}

unknown's avatar
unknown committed
1212
MYSQL_ROW_OFFSET STDCALL mysql_row_tell(MYSQL_RES *res)
unknown's avatar
unknown committed
1213 1214 1215 1216
{
  return res->data_cursor;
}

unknown's avatar
unknown committed
1217
MYSQL_FIELD_OFFSET STDCALL mysql_field_tell(MYSQL_RES *res)
unknown's avatar
unknown committed
1218 1219 1220 1221 1222 1223 1224 1225
{
  return (res)->current_field;
}

/* MYSQL */

unsigned int STDCALL mysql_field_count(MYSQL *mysql)
{
1226
  return mysql->last_used_con->field_count;
unknown's avatar
unknown committed
1227 1228 1229 1230
}

my_ulonglong STDCALL mysql_affected_rows(MYSQL *mysql)
{
1231
  return mysql->last_used_con->affected_rows;
unknown's avatar
unknown committed
1232 1233 1234 1235
}

my_ulonglong STDCALL mysql_insert_id(MYSQL *mysql)
{
1236
  return mysql->last_used_con->insert_id;
unknown's avatar
unknown committed
1237 1238
}

1239 1240 1241 1242 1243
const char *STDCALL mysql_sqlstate(MYSQL *mysql)
{
  return mysql->net.sqlstate;
}

1244 1245 1246 1247 1248
uint STDCALL mysql_warning_count(MYSQL *mysql)
{
  return mysql->warning_count;
}

unknown's avatar
unknown committed
1249
const char *STDCALL mysql_info(MYSQL *mysql)
unknown's avatar
unknown committed
1250
{
1251
  return mysql->info;
unknown's avatar
unknown committed
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274
}

ulong STDCALL mysql_thread_id(MYSQL *mysql)
{
  return (mysql)->thread_id;
}

const char * STDCALL mysql_character_set_name(MYSQL *mysql)
{
  return mysql->charset->name;
}


uint STDCALL mysql_thread_safe(void)
{
#ifdef THREAD
  return 1;
#else
  return 0;
#endif
}

/****************************************************************************
1275
  Some support functions
unknown's avatar
unknown committed
1276 1277
****************************************************************************/

unknown's avatar
unknown committed
1278 1279 1280 1281 1282 1283 1284 1285 1286
/*
  Functions called my my_net_init() to set some application specific variables
*/

void my_net_local_init(NET *net)
{
  net->max_packet=   (uint) net_buffer_length;
  net->read_timeout= (uint) net_read_timeout;
  net->write_timeout=(uint) net_write_timeout;
1287
  net->retry_count=  1;
unknown's avatar
unknown committed
1288 1289 1290
  net->max_packet_size= max(net_buffer_length, max_allowed_packet);
}

unknown's avatar
unknown committed
1291
/*
1292 1293 1294
  Add escape characters to a string (blob?) to make it suitable for a insert
  to should at least have place for length*2+1 chars
  Returns the length of the to string
unknown's avatar
unknown committed
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326
*/

ulong STDCALL
mysql_escape_string(char *to,const char *from,ulong length)
{
  return mysql_sub_escape_string(default_charset_info,to,from,length);
}

ulong STDCALL
mysql_real_escape_string(MYSQL *mysql, char *to,const char *from,
			 ulong length)
{
  return mysql_sub_escape_string(mysql->charset,to,from,length);
}


static ulong
mysql_sub_escape_string(CHARSET_INFO *charset_info, char *to,
			const char *from, ulong length)
{
  const char *to_start=to;
  const char *end;
#ifdef USE_MB
  my_bool use_mb_flag=use_mb(charset_info);
#endif
  for (end=from+length; from != end ; from++)
  {
#ifdef USE_MB
    int l;
    if (use_mb_flag && (l = my_ismbchar(charset_info, from, end)))
    {
      while (l--)
1327
	*to++ = *from++;
unknown's avatar
unknown committed
1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470
      from--;
      continue;
    }
#endif
    switch (*from) {
    case 0:				/* Must be escaped for 'mysql' */
      *to++= '\\';
      *to++= '0';
      break;
    case '\n':				/* Must be escaped for logs */
      *to++= '\\';
      *to++= 'n';
      break;
    case '\r':
      *to++= '\\';
      *to++= 'r';
      break;
    case '\\':
      *to++= '\\';
      *to++= '\\';
      break;
    case '\'':
      *to++= '\\';
      *to++= '\'';
      break;
    case '"':				/* Better safe than sorry */
      *to++= '\\';
      *to++= '"';
      break;
    case '\032':			/* This gives problems on Win32 */
      *to++= '\\';
      *to++= 'Z';
      break;
    default:
      *to++= *from;
    }
  }
  *to=0;
  return (ulong) (to-to_start);
}


char * STDCALL
mysql_odbc_escape_string(MYSQL *mysql,
			 char *to, ulong to_length,
			 const char *from, ulong from_length,
			 void *param,
			 char * (*extend_buffer)
			 (void *, char *, ulong *))
{
  char *to_end=to+to_length-5;
  const char *end;
#ifdef USE_MB
  my_bool use_mb_flag=use_mb(mysql->charset);
#endif

  for (end=from+from_length; from != end ; from++)
  {
    if (to >= to_end)
    {
      to_length = (ulong) (end-from)+512;	/* We want this much more */
      if (!(to=(*extend_buffer)(param, to, &to_length)))
	return to;
      to_end=to+to_length-5;
    }
#ifdef USE_MB
    {
      int l;
      if (use_mb_flag && (l = my_ismbchar(mysql->charset, from, end)))
      {
	while (l--)
	  *to++ = *from++;
	from--;
	continue;
      }
    }
#endif
    switch (*from) {
    case 0:				/* Must be escaped for 'mysql' */
      *to++= '\\';
      *to++= '0';
      break;
    case '\n':				/* Must be escaped for logs */
      *to++= '\\';
      *to++= 'n';
      break;
    case '\r':
      *to++= '\\';
      *to++= 'r';
      break;
    case '\\':
      *to++= '\\';
      *to++= '\\';
      break;
    case '\'':
      *to++= '\\';
      *to++= '\'';
      break;
    case '"':				/* Better safe than sorry */
      *to++= '\\';
      *to++= '"';
      break;
    case '\032':			/* This gives problems on Win32 */
      *to++= '\\';
      *to++= 'Z';
      break;
    default:
      *to++= *from;
    }
  }
  return to;
}

void STDCALL
myodbc_remove_escape(MYSQL *mysql,char *name)
{
  char *to;
#ifdef USE_MB
  my_bool use_mb_flag=use_mb(mysql->charset);
  char *end;
  LINT_INIT(end);
  if (use_mb_flag)
    for (end=name; *end ; end++) ;
#endif

  for (to=name ; *name ; name++)
  {
#ifdef USE_MB
    int l;
    if (use_mb_flag && (l = my_ismbchar( mysql->charset, name , end ) ) )
    {
      while (l--)
	*to++ = *name++;
      name--;
      continue;
    }
#endif
    if (*name == '\\' && name[1])
      name++;
    *to++= *name;
  }
  *to=0;
}
1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485

/********************************************************************

 Implementation of new client-server prototypes for 4.1 version
 starts from here ..

 mysql_* are real prototypes used by applications

*********************************************************************/

/********************************************************************
 Misc Utility functions
********************************************************************/

/*
1486
  Set the internal stmt error messages
1487 1488
*/

1489 1490
static void set_stmt_error(MYSQL_STMT * stmt, int errcode,
			   const char *sqlstate)
1491 1492
{
  DBUG_ENTER("set_stmt_error");
1493
  DBUG_PRINT("enter", ("error: %d '%s'", errcode, ER(errcode)));
1494 1495
  DBUG_ASSERT(stmt != 0);

1496 1497
  stmt->last_errno= errcode;
  strmov(stmt->last_error, ER(errcode));
1498
  strmov(stmt->sqlstate, sqlstate);
1499 1500 1501 1502

  DBUG_VOID_RETURN;
}

1503

1504
/*
1505
  Copy error message to statement handler
1506 1507
*/

unknown's avatar
SCRUM  
unknown committed
1508 1509
void set_stmt_errmsg(MYSQL_STMT * stmt, const char *err, int errcode,
		     const char *sqlstate)
1510 1511
{
  DBUG_ENTER("set_stmt_error_msg");
1512
  DBUG_PRINT("enter", ("error: %d/%s '%s'", errcode, sqlstate, err));
1513 1514
  DBUG_ASSERT(stmt != 0);

1515
  stmt->last_errno= errcode;
1516
  if (err && err[0])
1517
    strmov(stmt->last_error, err);
1518
  strmov(stmt->sqlstate, sqlstate);
1519 1520 1521 1522

  DBUG_VOID_RETURN;
}

1523

1524
/*
1525
  Set the internal error message to mysql handler
1526 1527
*/

1528
static void set_mysql_error(MYSQL * mysql, int errcode, const char *sqlstate)
1529 1530
{
  DBUG_ENTER("set_mysql_error");
1531
  DBUG_PRINT("enter", ("error :%d '%s'", errcode, ER(errcode)));
1532 1533
  DBUG_ASSERT(mysql != 0);

1534
  mysql->net.last_errno= errcode;
1535
  strmov(mysql->net.last_error, ER(errcode));
1536
  strmov(mysql->net.sqlstate, sqlstate);
1537 1538 1539 1540
}


/*
1541
  Reallocate the NET package to be at least of 'length' bytes
1542

1543 1544 1545 1546
  SYNPOSIS
   my_realloc_str()
   net			The NET structure to modify
   int length		Ensure that net->buff is at least this big
1547

1548 1549 1550
  RETURN VALUES
  0	ok
  1	Error
1551 1552 1553

*/

1554
static my_bool my_realloc_str(NET *net, ulong length)
1555
{
1556 1557 1558 1559
  ulong buf_length= (ulong) (net->write_pos - net->buff);
  my_bool res=0;
  DBUG_ENTER("my_realloc_str");
  if (buf_length + length > net->max_packet)
1560
  {
1561 1562
    res= net_realloc(net, buf_length + length);
    net->write_pos= net->buff+ buf_length;
1563
  }
1564
  DBUG_RETURN(res);
1565 1566 1567
}

/********************************************************************
1568
  Prepare related implementations
1569 1570 1571 1572
********************************************************************/

/*
  Read the prepare statement results ..
1573 1574 1575 1576 1577 1578 1579 1580

  NOTE
    This is only called for connection to servers that supports
    prepared statements (and thus the 4.1 protocol)

  RETURN VALUES
    0	ok
    1	error
1581 1582
*/

unknown's avatar
unknown committed
1583
my_bool cli_read_prepare_result(MYSQL *mysql, MYSQL_STMT *stmt)
1584 1585
{
  uchar *pos;
1586
  uint field_count;
unknown's avatar
unknown committed
1587
  ulong length, param_count;
1588
  MYSQL_DATA *fields_data;
unknown's avatar
SCRUM  
unknown committed
1589
  DBUG_ENTER("read_prepare_result");
1590

1591 1592 1593
  mysql= mysql->last_used_con;
  if ((length= net_safe_read(mysql)) == packet_error)
    DBUG_RETURN(1);
1594

1595
  pos= (uchar*) mysql->net.read_pos;
1596 1597 1598
  stmt->stmt_id= uint4korr(pos+1); pos+= 5;
  field_count=   uint2korr(pos);   pos+= 2;
  param_count=   uint2korr(pos);   pos+= 2;
1599

1600 1601 1602 1603
  if (field_count != 0)
  {
    if (!(mysql->server_status & SERVER_STATUS_AUTOCOMMIT))
      mysql->server_status|= SERVER_STATUS_IN_TRANS;
1604

1605
    mysql->extra_info= net_field_length_ll(&pos);
unknown's avatar
SCRUM  
unknown committed
1606
    if (!(fields_data= (*mysql->methods->read_rows)(mysql,(MYSQL_FIELD*)0,7)))
1607 1608 1609 1610 1611 1612
      DBUG_RETURN(1);
    if (!(stmt->fields= unpack_fields(fields_data,&stmt->mem_root,
				      field_count,0,
				      mysql->server_capabilities)))
      DBUG_RETURN(1);
  }
1613 1614
  stmt->field_count=  (uint) field_count;
  stmt->param_count=  (ulong) param_count;
unknown's avatar
SCRUM  
unknown committed
1615

1616 1617 1618 1619 1620
  DBUG_RETURN(0);
}


/*
1621 1622 1623 1624 1625
  Prepare the query and return the new statement handle to
  caller.

  Also update the total parameter count along with resultset
  metadata information by reading from server
1626 1627
*/

1628 1629 1630

MYSQL_STMT *STDCALL
mysql_prepare(MYSQL  *mysql, const char *query, ulong length)
1631 1632
{
  MYSQL_STMT  *stmt;
unknown's avatar
unknown committed
1633
  DBUG_ENTER("mysql_prepare");
1634 1635
  DBUG_ASSERT(mysql != 0);

unknown's avatar
unknown committed
1636
#ifdef CHECK_EXTRA_ARGUMENTS
1637 1638
  if (!query)
  {
1639
    set_mysql_error(mysql, CR_NULL_POINTER, unknown_sqlstate);
1640 1641 1642 1643
    DBUG_RETURN(0);
  }
#endif

1644 1645 1646 1647 1648
  if (!(stmt= (MYSQL_STMT *) my_malloc(sizeof(MYSQL_STMT),
				       MYF(MY_WME | MY_ZEROFILL))) ||
      !(stmt->query= my_strdup_with_length((byte *) query, length, MYF(0))))
  {
    my_free((gptr) stmt, MYF(MY_ALLOW_ZERO_PTR));
1649
    set_mysql_error(mysql, CR_OUT_OF_MEMORY, unknown_sqlstate);
1650
    DBUG_RETURN(0);
1651 1652 1653
  }
  if (simple_command(mysql, COM_PREPARE, query, length, 1))
  {
unknown's avatar
unknown committed
1654
    stmt_close(stmt, 1);
1655 1656 1657
    DBUG_RETURN(0);
  }

1658
  init_alloc_root(&stmt->mem_root,8192,0);
unknown's avatar
SCRUM  
unknown committed
1659
  if ((*mysql->methods->read_prepare_result)(mysql, stmt))
1660
  {
unknown's avatar
unknown committed
1661
    stmt_close(stmt, 1);
1662 1663
    DBUG_RETURN(0);
  }
unknown's avatar
unknown committed
1664

unknown's avatar
unknown committed
1665 1666 1667 1668 1669 1670 1671 1672 1673
  if (!(stmt->params= (MYSQL_BIND *) alloc_root(&stmt->mem_root,
						sizeof(MYSQL_BIND)*
                                                (stmt->param_count + 
                                                 stmt->field_count))))
  {
    set_stmt_error(stmt, CR_OUT_OF_MEMORY, unknown_sqlstate);
    DBUG_RETURN(0);
  }
  stmt->bind= stmt->params + stmt->param_count;
unknown's avatar
unknown committed
1674
  stmt->state= MY_ST_PREPARE;
unknown's avatar
unknown committed
1675
  stmt->mysql= mysql;
unknown's avatar
unknown committed
1676
  mysql->stmts= list_add(mysql->stmts, &stmt->list);
unknown's avatar
unknown committed
1677
  mysql->status= MYSQL_STATUS_READY;
unknown's avatar
unknown committed
1678
  stmt->list.data= stmt;
1679 1680 1681 1682
  DBUG_PRINT("info", ("Parameter count: %ld", stmt->param_count));
  DBUG_RETURN(stmt);
}

1683 1684 1685 1686 1687 1688 1689
/*
  Get the execute query meta information for non-select 
  statements (on demand).
*/

unsigned int alloc_stmt_fields(MYSQL_STMT *stmt)
{
1690 1691
  MYSQL_FIELD *fields, *field, *end;
  MEM_ROOT *alloc= &stmt->mem_root;
unknown's avatar
unknown committed
1692
  MYSQL *mysql= stmt->mysql->last_used_con;
1693
  
unknown's avatar
unknown committed
1694
  if (stmt->state != MY_ST_EXECUTE || !mysql->field_count)
1695
    return 0;
1696
  
unknown's avatar
unknown committed
1697
  stmt->field_count= mysql->field_count;
1698
  
1699 1700 1701 1702 1703
  /*
    Get the field information for non-select statements 
    like SHOW and DESCRIBE commands
  */
  if (!(stmt->fields= (MYSQL_FIELD *) alloc_root(alloc, 
1704 1705
						 sizeof(MYSQL_FIELD) *
						 stmt->field_count)) || 
1706
      !(stmt->bind= (MYSQL_BIND *) alloc_root(alloc, 
1707 1708
					      sizeof(MYSQL_BIND) *
					      stmt->field_count)))
1709
    return 0;
1710
  
unknown's avatar
unknown committed
1711
  for (fields= mysql->fields, end= fields+stmt->field_count, 
1712
	 field= stmt->fields;
1713 1714 1715 1716 1717 1718 1719
       field && fields < end; fields++, field++)
  {
    field->db       = strdup_root(alloc,fields->db);
    field->table    = strdup_root(alloc,fields->table);
    field->org_table= strdup_root(alloc,fields->org_table);
    field->name     = strdup_root(alloc,fields->name);
    field->org_name = strdup_root(alloc,fields->org_name);
1720
    field->charsetnr= fields->charsetnr;
1721 1722 1723 1724 1725 1726 1727
    field->length   = fields->length;
    field->type     = fields->type;
    field->flags    = fields->flags;
    field->decimals = fields->decimals;
    field->def      = fields->def ? strdup_root(alloc,fields->def): 0;
    field->max_length= 0;
  }
1728 1729
  return stmt->field_count;
}
1730 1731 1732

/*
  Returns prepared meta information in the form of resultset
1733
  to client.
1734 1735 1736
*/

MYSQL_RES * STDCALL
unknown's avatar
unknown committed
1737
mysql_get_metadata(MYSQL_STMT *stmt)
1738 1739
{
  MYSQL_RES *result;
unknown's avatar
unknown committed
1740
  DBUG_ENTER("mysql_get_metadata");
unknown's avatar
unknown committed
1741 1742
  
  if (!stmt->field_count || !stmt->fields)
1743 1744 1745 1746
  {
    if (!alloc_stmt_fields(stmt))
      DBUG_RETURN(0);
  }  
unknown's avatar
unknown committed
1747 1748 1749 1750 1751
  if (!(result=(MYSQL_RES*) my_malloc(sizeof(*result)+
				      sizeof(ulong)*stmt->field_count,
				      MYF(MY_WME | MY_ZEROFILL))))
    return 0;

1752
  result->methods= stmt->mysql->methods;
1753 1754 1755 1756
  result->eof=1;	/* Marker for buffered */
  result->fields=	stmt->fields;
  result->field_count=	stmt->field_count;
  DBUG_RETURN(result);
1757 1758
}

1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779
/*
  Returns parameter columns meta information in the form of 
  resultset.
*/

MYSQL_RES * STDCALL
mysql_param_result(MYSQL_STMT *stmt)
{
  DBUG_ENTER("mysql_param_result");
  
  if (!stmt->param_count)
    DBUG_RETURN(0);

  /*
    TODO: Fix this when server sends the information. 
    Till then keep a dummy prototype 
  */
  DBUG_RETURN(0); 
}


1780 1781 1782 1783
/********************************************************************
 Prepare-execute, and param handling
*********************************************************************/

1784 1785
/*
  Store the buffer type
1786 1787
*/

1788
static void store_param_type(NET *net, uint type)
1789
{
1790 1791
  int2store(net->write_pos, type);
  net->write_pos+=2;
1792 1793
}

unknown's avatar
unknown committed
1794

1795 1796
/****************************************************************************
  Functions to store parameter data from a prepared statement.
1797

1798
  All functions has the following characteristics:
1799

1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812
  SYNOPSIS
    store_param_xxx()
    net			MySQL NET connection
    param		MySQL bind param

  RETURN VALUES
    0	ok
    1	Error	(Can't alloc net->buffer)
****************************************************************************/

static void store_param_tinyint(NET *net, MYSQL_BIND *param)
{
  *(net->write_pos++)= (uchar) *param->buffer;
1813 1814
}

1815 1816 1817 1818 1819 1820
static void store_param_short(NET *net, MYSQL_BIND *param)
{
  short value= *(short*) param->buffer;
  int2store(net->write_pos,value);
  net->write_pos+=2;
}
1821

1822
static void store_param_int32(NET *net, MYSQL_BIND *param)
1823
{
1824 1825 1826 1827
  int32 value= *(int32*) param->buffer;
  int4store(net->write_pos,value);
  net->write_pos+=4;
}
1828

1829 1830 1831 1832 1833 1834
static void store_param_int64(NET *net, MYSQL_BIND *param)
{
  longlong value= *(longlong*) param->buffer;
  int8store(net->write_pos,value);
  net->write_pos+= 8;
}
1835

1836 1837 1838 1839 1840
static void store_param_float(NET *net, MYSQL_BIND *param)
{
  float value= *(float*) param->buffer;
  float4store(net->write_pos, value);
  net->write_pos+= 4;
1841 1842
}

1843 1844 1845 1846 1847 1848
static void store_param_double(NET *net, MYSQL_BIND *param)
{
  double value= *(double*) param->buffer;
  float8store(net->write_pos, value);
  net->write_pos+= 8;
}
1849

unknown's avatar
unknown committed
1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914
static void store_param_time(NET *net, MYSQL_BIND *param)
{
  MYSQL_TIME *tm= (MYSQL_TIME *) param->buffer;
  char buff[15], *pos;
  uint length;

  pos= buff+1;
  pos[0]= tm->neg ? 1: 0;
  int4store(pos+1, tm->day);
  pos[5]= (uchar) tm->hour;
  pos[6]= (uchar) tm->minute;
  pos[7]= (uchar) tm->second;
  int4store(pos+8, tm->second_part);
  if (tm->second_part)
    length= 11;
  else if (tm->hour || tm->minute || tm->second || tm->day)
    length= 8;
  else
    length= 0;
  buff[0]= (char) length++;  
  memcpy((char *)net->write_pos, buff, length);
  net->write_pos+= length;
}

static void net_store_datetime(NET *net, MYSQL_TIME *tm)
{
  char buff[12], *pos;
  uint length;

  pos= buff+1;

  int2store(pos, tm->year);
  pos[2]= (uchar) tm->month;
  pos[3]= (uchar) tm->day;
  pos[4]= (uchar) tm->hour;
  pos[5]= (uchar) tm->minute;
  pos[6]= (uchar) tm->second;
  int4store(pos+7, tm->second_part);
  if (tm->second_part)
    length= 11;
  else if (tm->hour || tm->minute || tm->second)
    length= 7;
  else if (tm->year || tm->month || tm->day)
    length= 4;
  else
    length= 0;
  buff[0]= (char) length++;  
  memcpy((char *)net->write_pos, buff, length);
  net->write_pos+= length;
}

static void store_param_date(NET *net, MYSQL_BIND *param)
{
  MYSQL_TIME *tm= (MYSQL_TIME *) param->buffer;
  tm->hour= tm->minute= tm->second= 0;
  tm->second_part= 0;
  net_store_datetime(net, tm);
}

static void store_param_datetime(NET *net, MYSQL_BIND *param)
{
  MYSQL_TIME *tm= (MYSQL_TIME *) param->buffer;
  net_store_datetime(net, tm);
}
    
1915
static void store_param_str(NET *net, MYSQL_BIND *param)
1916
{
1917
  ulong length= param->length ? *param->length : param->buffer_length;
1918 1919
  char *to= (char *) net_store_length((char *) net->write_pos, length);
  memcpy(to, param->buffer, length);
unknown's avatar
unknown committed
1920
  net->write_pos= (uchar*) to+length;
1921 1922
}

1923

1924
/*
1925 1926 1927 1928 1929 1930 1931 1932 1933 1934
  Mark if the parameter is NULL.

  SYNOPSIS
    store_param_null()
    net			MySQL NET connection
    param		MySQL bind param

  DESCRIPTION
    A data package starts with a string of bits where we set a bit
    if a parameter is NULL
1935 1936
*/

1937
static void store_param_null(NET *net, MYSQL_BIND *param)
1938
{
1939
  uint pos= param->param_number;
unknown's avatar
unknown committed
1940
  net->buff[pos/8]|=  (uchar) (1 << (pos & 7));
1941
}
1942 1943 1944 1945


/*
  Set parameter data by reading from input buffers from the
1946
  client application
1947 1948
*/

unknown's avatar
unknown committed
1949

1950
static my_bool store_param(MYSQL_STMT *stmt, MYSQL_BIND *param)
1951
{
1952 1953 1954
  MYSQL *mysql= stmt->mysql;
  NET	*net  = &mysql->net;
  DBUG_ENTER("store_param");
unknown's avatar
unknown committed
1955 1956 1957 1958 1959 1960
  DBUG_PRINT("enter",("type: %d, buffer:%lx, length: %lu  is_null: %d",
		      param->buffer_type,
		      param->buffer ? param->buffer : "0", *param->length,
		      *param->is_null));

  if (*param->is_null)
1961 1962
    store_param_null(net, param);
  else
unknown's avatar
unknown committed
1963
  {
unknown's avatar
unknown committed
1964 1965
    /*
      Param->length should ALWAYS point to the correct length for the type
1966
      Either to the length pointer given by the user or param->buffer_length
unknown's avatar
unknown committed
1967 1968
    */
    if ((my_realloc_str(net, 9 + *param->length)))
unknown's avatar
unknown committed
1969
    {
1970
      set_stmt_error(stmt, CR_OUT_OF_MEMORY, unknown_sqlstate);
unknown's avatar
unknown committed
1971
      DBUG_RETURN(1);
unknown's avatar
unknown committed
1972
    }
1973
    (*param->store_param_func)(net, param);
unknown's avatar
unknown committed
1974 1975
  }
  DBUG_RETURN(0);
1976 1977
}

unknown's avatar
unknown committed
1978

1979 1980 1981 1982
/*
  Send the prepare query to server for execution
*/

1983
static my_bool execute(MYSQL_STMT * stmt, char *packet, ulong length)
1984
{
1985 1986
  MYSQL *mysql= stmt->mysql;
  NET	*net= &mysql->net;
unknown's avatar
unknown committed
1987
  char buff[MYSQL_STMT_HEADER];
1988
  DBUG_ENTER("execute");
1989 1990
  DBUG_PRINT("enter",("packet: %s, length :%d",packet ? packet :" ", length));

1991 1992
  mysql->last_used_con= mysql;
  int4store(buff, stmt->stmt_id);		/* Send stmt id to server */
unknown's avatar
SCRUM  
unknown committed
1993 1994 1995
  if (cli_advanced_command(mysql, COM_EXECUTE, buff, 
			    MYSQL_STMT_HEADER, packet, 
			    length, 1) ||
unknown's avatar
unknown committed
1996
      (*mysql->methods->read_query_result)(mysql))
1997
  {
1998
    set_stmt_errmsg(stmt, net->last_error, net->last_errno, net->sqlstate);
1999
    DBUG_RETURN(1);
2000 2001 2002 2003
  }
  DBUG_RETURN(0);
}

unknown's avatar
unknown committed
2004 2005

int cli_stmt_execute(MYSQL_STMT *stmt)
2006
{
unknown's avatar
SCRUM  
unknown committed
2007
  DBUG_ENTER("cli_stmt_execute");
2008 2009 2010

  if (stmt->param_count)
  {
2011 2012
    NET        *net= &stmt->mysql->net;
    MYSQL_BIND *param, *param_end;
2013
    char       *param_data;
unknown's avatar
unknown committed
2014 2015 2016
    ulong length;
    uint null_count;
    my_bool    result;
2017

unknown's avatar
unknown committed
2018 2019
#ifdef CHECK_EXTRA_ARGUMENTS
    if (!stmt->param_buffers)
2020 2021
    {
      /* Parameters exists, but no bound buffers */
2022
      set_stmt_error(stmt, CR_NOT_ALL_PARAMS_BOUND, unknown_sqlstate);
2023
      DBUG_RETURN(1);
2024
    }
unknown's avatar
unknown committed
2025
#endif
2026 2027 2028 2029 2030 2031 2032 2033
    net_clear(net);				/* Sets net->write_pos */
    /* Reserve place for null-marker bytes */
    null_count= (stmt->param_count+7) /8;
    bzero((char*) net->write_pos, null_count);
    net->write_pos+= null_count;
    param_end= stmt->params + stmt->param_count;

    /* In case if buffers (type) altered, indicate to server */
unknown's avatar
unknown committed
2034 2035
    *(net->write_pos)++= (uchar) stmt->send_types_to_server;
    if (stmt->send_types_to_server)
2036
    {
2037 2038 2039 2040 2041 2042 2043 2044 2045
      /*
	Store types of parameters in first in first package
	that is sent to the server.
      */
      for (param= stmt->params;	param < param_end ; param++)
	store_param_type(net, (uint) param->buffer_type);
    }

    for (param= stmt->params; param < param_end; param++)
2046
    {
unknown's avatar
unknown committed
2047 2048 2049
      /* check if mysql_long_data() was used */
      if (param->long_data_used)
	param->long_data_used= 0;	/* Clear for next execute call */
2050
      else if (store_param(stmt, param))
unknown's avatar
unknown committed
2051
	DBUG_RETURN(1);
2052
    }
2053 2054
    length= (ulong) (net->write_pos - net->buff);
    /* TODO: Look into avoding the following memdup */
unknown's avatar
unknown committed
2055
    if (!(param_data= my_memdup((const char*) net->buff, length, MYF(0))))
2056
    {
2057
      set_stmt_error(stmt, CR_OUT_OF_MEMORY, unknown_sqlstate);
2058
      DBUG_RETURN(1);
2059
    }
2060 2061
    net->write_pos= net->buff;			/* Reset for net_write() */
    result= execute(stmt, param_data, length);
unknown's avatar
unknown committed
2062
    stmt->send_types_to_server=0;
2063
    my_free(param_data, MYF(MY_WME));
2064 2065 2066
    DBUG_RETURN(result);
  }
  DBUG_RETURN((int) execute(stmt,0,0));
2067 2068
}

unknown's avatar
SCRUM  
unknown committed
2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092
/*
  Execute the prepare query
*/

int STDCALL mysql_execute(MYSQL_STMT *stmt)
{
  DBUG_ENTER("mysql_execute");

  if (stmt->state == MY_ST_UNKNOWN)
  {
    set_stmt_error(stmt, CR_NO_PREPARE_STMT, unknown_sqlstate);
    DBUG_RETURN(1);
  }
  if ((*stmt->mysql->methods->stmt_execute)(stmt))
    DBUG_RETURN(1);
      
  stmt->state= MY_ST_EXECUTE;
  mysql_free_result(stmt->result);
  stmt->result= (MYSQL_RES *)0;
  stmt->result_buffered= 0;
  stmt->current_row= 0;
  DBUG_RETURN(0);
}

2093

2094 2095 2096 2097 2098 2099 2100 2101 2102 2103
/*
  Return total parameters count in the statement
*/

ulong STDCALL mysql_param_count(MYSQL_STMT * stmt)
{
  DBUG_ENTER("mysql_param_count");
  DBUG_RETURN(stmt->param_count);
}

2104 2105 2106 2107 2108 2109 2110 2111
/*
  Return total affected rows from the last statement
*/

my_ulonglong STDCALL mysql_stmt_affected_rows(MYSQL_STMT *stmt)
{
  return stmt->mysql->last_used_con->affected_rows;
}
2112

unknown's avatar
unknown committed
2113 2114 2115 2116

static my_bool int_is_null_true= 1;		/* Used for MYSQL_TYPE_NULL */
static my_bool int_is_null_false= 0;

2117
/*
2118
  Setup the parameter data buffers from application
2119 2120
*/

2121
my_bool STDCALL mysql_bind_param(MYSQL_STMT *stmt, MYSQL_BIND * bind)
2122
{
2123 2124
  uint count=0;
  MYSQL_BIND *param, *end;
2125 2126 2127 2128 2129
  DBUG_ENTER("mysql_bind_param");

#ifdef CHECK_EXTRA_ARGUMENTS
  if (stmt->state == MY_ST_UNKNOWN)
  {
2130
    set_stmt_error(stmt, CR_NO_PREPARE_STMT, unknown_sqlstate);
2131
    DBUG_RETURN(1);
2132 2133 2134
  }
  if (!stmt->param_count)
  {
2135
    set_stmt_error(stmt, CR_NO_PARAMETERS_EXISTS, unknown_sqlstate);
2136
    DBUG_RETURN(1);
2137 2138 2139
  }
#endif

2140 2141 2142 2143 2144 2145 2146
  /* Allocated on prepare */
  memcpy((char*) stmt->params, (char*) bind,
	 sizeof(MYSQL_BIND) * stmt->param_count);

  for (param= stmt->params, end= param+stmt->param_count;
       param < end ;
       param++)
2147
  {
2148
    param->param_number= count++;
2149 2150
    param->long_data_used= 0;

2151
    /*
2152
      If param->length is not given, change it to point to buffer_length.
2153 2154 2155
      This way we can always use *param->length to get the length of data
    */
    if (!param->length)
2156
      param->length= &param->buffer_length;
unknown's avatar
unknown committed
2157

unknown's avatar
unknown committed
2158 2159 2160 2161
    /* If param->is_null is not set, then the value can never be NULL */
    if (!param->is_null)
      param->is_null= &int_is_null_false;

2162 2163
    /* Setup data copy functions for the different supported types */
    switch (param->buffer_type) {
unknown's avatar
unknown committed
2164
    case MYSQL_TYPE_NULL:
unknown's avatar
unknown committed
2165
      param->is_null= &int_is_null_true;
unknown's avatar
unknown committed
2166
      break;
2167
    case MYSQL_TYPE_TINY:
unknown's avatar
unknown committed
2168
      /* Force param->length as this is fixed for this type */
2169 2170
      param->length= &param->buffer_length;
      param->buffer_length= 1;
2171 2172 2173
      param->store_param_func= store_param_tinyint;
      break;
    case MYSQL_TYPE_SHORT:
2174 2175
      param->length= &param->buffer_length;
      param->buffer_length= 2;
2176 2177 2178
      param->store_param_func= store_param_short;
      break;
    case MYSQL_TYPE_LONG:
2179 2180
      param->length= &param->buffer_length;
      param->buffer_length= 4;
2181 2182 2183
      param->store_param_func= store_param_int32;
      break;
    case MYSQL_TYPE_LONGLONG:
2184 2185
      param->length= &param->buffer_length;
      param->buffer_length= 8;
2186 2187 2188
      param->store_param_func= store_param_int64;
      break;
    case MYSQL_TYPE_FLOAT:
2189 2190
      param->length= &param->buffer_length;
      param->buffer_length= 4;
2191 2192 2193
      param->store_param_func= store_param_float;
      break;
    case MYSQL_TYPE_DOUBLE:
2194 2195
      param->length= &param->buffer_length;
      param->buffer_length= 8;
2196 2197
      param->store_param_func= store_param_double;
      break;
unknown's avatar
unknown committed
2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208
    case MYSQL_TYPE_TIME:
      /* Buffer length ignored for DATE, TIME and DATETIME */
      param->store_param_func= store_param_time;
      break;
    case MYSQL_TYPE_DATE:
      param->store_param_func= store_param_date;
      break;
    case MYSQL_TYPE_DATETIME:
    case MYSQL_TYPE_TIMESTAMP:
      param->store_param_func= store_param_datetime;
      break;
2209 2210 2211
    case MYSQL_TYPE_TINY_BLOB:
    case MYSQL_TYPE_MEDIUM_BLOB:
    case MYSQL_TYPE_LONG_BLOB:
unknown's avatar
unknown committed
2212
    case MYSQL_TYPE_BLOB:
2213 2214 2215 2216 2217
    case MYSQL_TYPE_VAR_STRING:
    case MYSQL_TYPE_STRING:
      param->store_param_func= store_param_str;
      break;
    default:
2218
      strmov(stmt->sqlstate, unknown_sqlstate);
unknown's avatar
unknown committed
2219 2220
      sprintf(stmt->last_error,
	      ER(stmt->last_errno= CR_UNSUPPORTED_PARAM_TYPE),
2221
	      param->buffer_type, count);
2222
      DBUG_RETURN(1);
2223 2224
    }
  }
2225
  /* We have to send/resendtype information to MySQL */
unknown's avatar
unknown committed
2226 2227
  stmt->send_types_to_server= 1;
  stmt->param_buffers= 1;
2228 2229 2230
  DBUG_RETURN(0);
}

2231

2232 2233 2234 2235 2236
/********************************************************************
 Long data implementation
*********************************************************************/

/*
2237 2238 2239 2240 2241 2242 2243 2244
  Send long data in pieces to the server

  SYNOPSIS
    mysql_send_long_data()
    stmt			Statement handler
    param_number		Parameter number (0 - N-1)
    data			Data to send to server
    length			Length of data to send (may be 0)
unknown's avatar
unknown committed
2245

2246
  RETURN VALUES
unknown's avatar
unknown committed
2247 2248
    0	ok
    1	error
2249 2250 2251
*/


2252 2253
my_bool STDCALL
mysql_send_long_data(MYSQL_STMT *stmt, uint param_number,
unknown's avatar
unknown committed
2254
		     const char *data, ulong length)
2255 2256 2257
{
  MYSQL_BIND *param;
  DBUG_ENTER("mysql_send_long_data");
2258
  DBUG_ASSERT(stmt != 0);
2259
  DBUG_PRINT("enter",("param no : %d, data : %lx, length : %ld",
2260
		      param_number, data, length));
2261

2262
  if (param_number >= stmt->param_count)
2263
  {
2264
    set_stmt_error(stmt, CR_INVALID_PARAMETER_NO, unknown_sqlstate);
2265 2266
    DBUG_RETURN(1);
  }
2267
  param= stmt->params+param_number;
unknown's avatar
unknown committed
2268 2269 2270 2271 2272 2273 2274
  if (param->buffer_type < MYSQL_TYPE_TINY_BLOB ||
      param->buffer_type > MYSQL_TYPE_STRING)
  {
    /*
      Long data handling should be used only for string/binary
      types only
    */
2275
    strmov(stmt->sqlstate, unknown_sqlstate);
unknown's avatar
unknown committed
2276 2277 2278 2279 2280 2281
    sprintf(stmt->last_error, ER(stmt->last_errno= CR_INVALID_BUFFER_USE),
	    param->param_number);
    DBUG_RETURN(1);
  }
  /* Mark for execute that the result is already sent */
  param->long_data_used= 1;
2282
  if (length)
2283
  {
2284 2285 2286 2287 2288 2289
    MYSQL *mysql= stmt->mysql;
    char   *packet, extra_data[MYSQL_LONG_DATA_HEADER];

    packet= extra_data;
    int4store(packet, stmt->stmt_id);	   packet+=4;
    int2store(packet, param_number);	   packet+=2;
2290

2291 2292 2293 2294
    /*
      Note that we don't get any ok packet from the server in this case
      This is intentional to save bandwidth.
    */
unknown's avatar
SCRUM  
unknown committed
2295 2296 2297
    if ((*mysql->methods->advanced_command)(mysql, COM_LONG_DATA, extra_data,
					    MYSQL_LONG_DATA_HEADER, data, 
					    length, 1))
2298
    {
2299 2300
      set_stmt_errmsg(stmt, mysql->net.last_error,
		      mysql->net.last_errno, mysql->net.sqlstate);
2301 2302 2303 2304 2305 2306
      DBUG_RETURN(1);
    }
  }
  DBUG_RETURN(0);
}

2307

2308
/********************************************************************
2309
  Fetch-bind related implementations
2310 2311
*********************************************************************/

unknown's avatar
unknown committed
2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326
/****************************************************************************
  Functions to fetch data to application buffers

  All functions has the following characteristics:

  SYNOPSIS
    fetch_result_xxx()
    param   MySQL bind param
    row     Row value

  RETURN VALUES
    0	ok
    1	Error	(Can't alloc net->buffer)
****************************************************************************/

unknown's avatar
unknown committed
2327
static void set_zero_time(MYSQL_TIME *tm)
2328
{
unknown's avatar
unknown committed
2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399
  tm->year= tm->month= tm->day= 0;
  tm->hour= tm->minute= tm->second= 0;
  tm->second_part= 0;
  tm->neg= (bool)0;
}

/* Read TIME from binary packet and return it to MYSQL_TIME */
static uint read_binary_time(MYSQL_TIME *tm, uchar **pos)
{
  uchar *to;
  uint  length;
 
  if (!(length= net_field_length(pos)))
  {
    set_zero_time(tm);
    return 0;
  }
  
  to= *pos;     
  tm->second_part= (length > 8 ) ? (ulong) sint4korr(to+7): 0;

  tm->day=    (ulong) sint4korr(to+1);
  tm->hour=   (uint) to[5];
  tm->minute= (uint) to[6];
  tm->second= (uint) to[7];

  tm->year= tm->month= 0;
  tm->neg= (bool)to[0];
  return length;
}

/* Read DATETIME from binary packet and return it to MYSQL_TIME */
static uint read_binary_datetime(MYSQL_TIME *tm, uchar **pos)
{
  uchar *to;
  uint  length;
 
  if (!(length= net_field_length(pos)))
  {
    set_zero_time(tm);
    return 0;
  }
  
  to= *pos;     
  tm->second_part= (length > 7 ) ? (ulong) sint4korr(to+7): 0;
    
  if (length > 4)
  {
    tm->hour=   (uint) to[4];
    tm->minute= (uint) to[5];
    tm->second= (uint) to[6];
  }
  else
    tm->hour= tm->minute= tm->second= 0;
    
  tm->year=   (uint) sint2korr(to);
  tm->month=  (uint) to[2];
  tm->day=    (uint) to[3];
  tm->neg=    0;
  return length;
}

/* Read DATE from binary packet and return it to MYSQL_TIME */
static uint read_binary_date(MYSQL_TIME *tm, uchar **pos)
{
  uchar *to;
  uint  length;
 
  if (!(length= net_field_length(pos)))
  {
    set_zero_time(tm);
2400
    return 0;
2401
  }
unknown's avatar
unknown committed
2402 2403 2404 2405 2406 2407 2408 2409 2410 2411
  
  to= *pos;     
  tm->year =  (uint) sint2korr(to);
  tm->month=  (uint) to[2];
  tm->day= (uint) to[3];

  tm->hour= tm->minute= tm->second= 0;
  tm->second_part= 0;
  tm->neg= 0;
  return length;
2412 2413
}

2414
/* Convert Numeric to buffer types */
2415 2416 2417 2418
static void send_data_long(MYSQL_BIND *param, longlong value)
{  
  char *buffer= param->buffer;
  
2419
  switch (param->buffer_type) {
unknown's avatar
unknown committed
2420 2421
  case MYSQL_TYPE_NULL: /* do nothing */
    break;
2422 2423 2424 2425
  case MYSQL_TYPE_TINY:
    *param->buffer= (uchar) value;
    break;
  case MYSQL_TYPE_SHORT:
2426
    int2store(buffer, value);
2427 2428
    break;
  case MYSQL_TYPE_LONG:
2429
    int4store(buffer, value);
2430 2431
    break;
  case MYSQL_TYPE_LONGLONG:
2432
    int8store(buffer, value);
2433 2434
    break;
  case MYSQL_TYPE_FLOAT:
2435 2436 2437 2438 2439
  {
    float data= (float)value;
    float4store(buffer, data);
    break;
  }
2440
  case MYSQL_TYPE_DOUBLE:
2441 2442 2443 2444 2445
  {
    double data= (double)value;
    float8store(buffer, data);
    break;
  }
2446
  default:
2447
  {
2448
    char tmp[22];				/* Enough for longlong */
2449 2450
    uint length= (uint)(longlong10_to_str(value,(char *)tmp,10)-tmp);
    ulong copy_length= min((ulong)length-param->offset, param->buffer_length);
unknown's avatar
unknown committed
2451 2452 2453 2454
    if ((long) copy_length < 0)
      copy_length=0;
    else
      memcpy(buffer, (char *)tmp+param->offset, copy_length);
2455
    *param->length= length;		
unknown's avatar
unknown committed
2456
  
2457 2458 2459
    if (copy_length != param->buffer_length)
      *(buffer+copy_length)= '\0';
  }
2460 2461 2462
  } 
}

2463

2464
/* Convert Double to buffer types */
2465

2466 2467 2468 2469 2470
static void send_data_double(MYSQL_BIND *param, double value)
{  
  char *buffer= param->buffer;

  switch(param->buffer_type) {
unknown's avatar
unknown committed
2471 2472
  case MYSQL_TYPE_NULL: /* do nothing */
    break;
2473 2474 2475 2476 2477 2478 2479
  case MYSQL_TYPE_TINY:
    *buffer= (uchar)value;
    break;
  case MYSQL_TYPE_SHORT:
    int2store(buffer, (short)value);
    break;
  case MYSQL_TYPE_LONG:
2480
    int4store(buffer, (long)value);
2481 2482 2483 2484 2485
    break;
  case MYSQL_TYPE_LONGLONG:
    int8store(buffer, (longlong)value);
    break;
  case MYSQL_TYPE_FLOAT:
unknown's avatar
unknown committed
2486 2487 2488 2489 2490
  {
    float data= (float)value;
    float4store(buffer, data);
    break;
  }
2491
  case MYSQL_TYPE_DOUBLE:
unknown's avatar
unknown committed
2492 2493 2494 2495 2496
  {
    double data= (double)value;
    float8store(buffer, data);
    break;
  }
2497
  default:
unknown's avatar
unknown committed
2498
  {
2499
    char tmp[128];
unknown's avatar
unknown committed
2500 2501
    uint length= my_sprintf(tmp,(tmp,"%g",value));
    ulong copy_length= min((ulong)length-param->offset, param->buffer_length);
unknown's avatar
unknown committed
2502 2503 2504 2505
    if ((long) copy_length < 0)
      copy_length=0;
    else
      memcpy(buffer, (char *)tmp+param->offset, copy_length);
unknown's avatar
unknown committed
2506
    *param->length= length;		
unknown's avatar
unknown committed
2507
  
unknown's avatar
unknown committed
2508 2509 2510
    if (copy_length != param->buffer_length)
      *(buffer+copy_length)= '\0';
  }
2511 2512 2513
  } 
}

unknown's avatar
unknown committed
2514

2515
/* Convert string to buffer types */
unknown's avatar
unknown committed
2516

2517
static void send_data_str(MYSQL_BIND *param, char *value, uint length)
2518 2519
{  
  char *buffer= param->buffer;
2520
  int err=0;
2521 2522

  switch(param->buffer_type) {
unknown's avatar
unknown committed
2523 2524
  case MYSQL_TYPE_NULL: /* do nothing */
    break;
2525
  case MYSQL_TYPE_TINY:
2526
  {
2527
    uchar data= (uchar)my_strntol(&my_charset_latin1,value,length,10,NULL,
unknown's avatar
unknown committed
2528
				  &err);
2529
    *buffer= data;
2530
    break;
2531
  }
2532 2533
  case MYSQL_TYPE_SHORT:
  {
2534
    short data= (short)my_strntol(&my_charset_latin1,value,length,10,NULL,
unknown's avatar
unknown committed
2535
				  &err);
2536 2537 2538 2539 2540
    int2store(buffer, data);
    break;
  }
  case MYSQL_TYPE_LONG:
  {
2541
    int32 data= (int32)my_strntol(&my_charset_latin1,value,length,10,NULL,
unknown's avatar
unknown committed
2542
				  &err);
2543 2544 2545 2546 2547
    int4store(buffer, data);    
    break;
  }
  case MYSQL_TYPE_LONGLONG:
  {
2548
    longlong data= my_strntoll(&my_charset_latin1,value,length,10,NULL,&err);
2549 2550 2551 2552 2553
    int8store(buffer, data);
    break;
  }
  case MYSQL_TYPE_FLOAT:
  {
2554
    float data = (float)my_strntod(&my_charset_latin1,value,length,NULL,&err);
2555 2556 2557 2558 2559
    float4store(buffer, data);
    break;
  }
  case MYSQL_TYPE_DOUBLE:
  {
2560
    double data= my_strntod(&my_charset_latin1,value,length,NULL,&err);
2561 2562 2563
    float8store(buffer, data);
    break;
  }
unknown's avatar
unknown committed
2564 2565 2566 2567 2568 2569
  case MYSQL_TYPE_TINY_BLOB:
  case MYSQL_TYPE_MEDIUM_BLOB:
  case MYSQL_TYPE_LONG_BLOB:
  case MYSQL_TYPE_BLOB:
    *param->length= length;
    length= min(length-param->offset, param->buffer_length);
unknown's avatar
unknown committed
2570 2571
    if ((long) length > 0)
      memcpy(buffer, value+param->offset, length);
unknown's avatar
unknown committed
2572
    break;
2573
  default:
2574
    *param->length= length;
unknown's avatar
unknown committed
2575
    length= min(length-param->offset, param->buffer_length);
unknown's avatar
unknown committed
2576 2577 2578 2579
    if ((long) length < 0)
      length= 0;
    else
      memcpy(buffer, value+param->offset, length);
2580
    if (length != param->buffer_length)
unknown's avatar
unknown committed
2581
      buffer[length]= '\0';
unknown's avatar
unknown committed
2582
  }
2583 2584
}

unknown's avatar
unknown committed
2585

unknown's avatar
unknown committed
2586 2587 2588 2589
static void send_data_time(MYSQL_BIND *param, MYSQL_TIME ltime, 
                           uint length)
{
  switch (param->buffer_type) {
unknown's avatar
unknown committed
2590 2591
  case MYSQL_TYPE_NULL: /* do nothing */
    break;
unknown's avatar
unknown committed
2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620

  case MYSQL_TYPE_DATE:
  case MYSQL_TYPE_TIME:
  case MYSQL_TYPE_DATETIME:
  case MYSQL_TYPE_TIMESTAMP:
  {
    MYSQL_TIME *tm= (MYSQL_TIME *)param->buffer;
    
    tm->year= ltime.year;
    tm->month= ltime.month;
    tm->day= ltime.day;

    tm->hour= ltime.hour;
    tm->minute= ltime.minute;
    tm->second= ltime.second;

    tm->second_part= ltime.second_part;
    tm->neg= ltime.neg;
    break;   
  }
  default:
  {
    char buff[25];
    
    if (!length)
      ltime.time_type= MYSQL_TIMESTAMP_NONE;
    switch (ltime.time_type) {
    case MYSQL_TIMESTAMP_DATE:
      length= my_sprintf(buff,(buff, "%04d-%02d-%02d", ltime.year,
2621
			       ltime.month,ltime.day));      
unknown's avatar
unknown committed
2622 2623 2624 2625 2626 2627 2628 2629
      break;
    case MYSQL_TIMESTAMP_FULL:
      length= my_sprintf(buff,(buff, "%04d-%02d-%02d %02d:%02d:%02d",
	                       ltime.year,ltime.month,ltime.day,
	                       ltime.hour,ltime.minute,ltime.second));
      break;
    case MYSQL_TIMESTAMP_TIME:
      length= my_sprintf(buff, (buff, "%02d:%02d:%02d",
2630
				ltime.hour,ltime.minute,ltime.second));
unknown's avatar
unknown committed
2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641
      break;
    default:
      length= 0;
      buff[0]='\0';
    }
    send_data_str(param, (char *)buff, length); 
  }
  }
}
                              

2642
/* Fetch data to buffers */
2643

unknown's avatar
unknown committed
2644
static void fetch_results(MYSQL_BIND *param, MYSQL_FIELD *field, uchar **row)
2645 2646
{
  ulong length;
unknown's avatar
unknown committed
2647 2648
  enum enum_field_types field_type= field->type;

2649 2650 2651
  switch (field_type) {
  case MYSQL_TYPE_TINY:
  {
2652
    char value= (char) **row;
unknown's avatar
unknown committed
2653
    uint field_is_unsigned= (field->flags & UNSIGNED_FLAG);
2654 2655
    longlong data= ((field_is_unsigned) ? (longlong) (unsigned char) value:
		    (longlong) value);
2656
    send_data_long(param,data);
unknown's avatar
unknown committed
2657
    length= 1;
2658 2659 2660 2661 2662
    break;
  }
  case MYSQL_TYPE_SHORT:
  case MYSQL_TYPE_YEAR:
  {
2663
    short value= sint2korr(*row);
unknown's avatar
unknown committed
2664
    uint field_is_unsigned= (field->flags & UNSIGNED_FLAG);
2665 2666
    longlong data= ((field_is_unsigned) ? (longlong) (unsigned short) value:
		    (longlong) value);
2667 2668
    send_data_long(param,data);
    length= 2;    
2669 2670 2671 2672
    break;
  }
  case MYSQL_TYPE_LONG:
  {
2673
    long value= sint4korr(*row);
unknown's avatar
unknown committed
2674
    uint field_is_unsigned= (field->flags & UNSIGNED_FLAG);
2675 2676
    longlong data= ((field_is_unsigned) ? (longlong) (unsigned long) value:
		    (longlong) value);
2677
    send_data_long(param,data);
unknown's avatar
unknown committed
2678
    length= 4;
2679 2680 2681 2682 2683 2684
    break;
  }
  case MYSQL_TYPE_LONGLONG:
  {
    longlong value= (longlong)sint8korr(*row);
    send_data_long(param,value);
unknown's avatar
unknown committed
2685
    length= 8;
2686 2687 2688 2689 2690 2691
    break;
  }
  case MYSQL_TYPE_FLOAT:
  {
    float value;
    float4get(value,*row);
2692
    send_data_double(param,value);
unknown's avatar
unknown committed
2693
    length= 4;
2694 2695 2696 2697 2698 2699
    break;
  }
  case MYSQL_TYPE_DOUBLE:
  {
    double value;
    float8get(value,*row);
2700
    send_data_double(param,value);
unknown's avatar
unknown committed
2701
    length= 8;
2702 2703 2704 2705
    break;
  }
  case MYSQL_TYPE_DATE:
  {
unknown's avatar
unknown committed
2706 2707
    MYSQL_TIME tm;
 
unknown's avatar
SCRUM  
unknown committed
2708
    length= read_binary_date(&tm, row);
unknown's avatar
unknown committed
2709 2710
    tm.time_type= MYSQL_TIMESTAMP_DATE;
    send_data_time(param, tm, length);
2711 2712 2713 2714
    break;
  }
  case MYSQL_TYPE_TIME:
  {
unknown's avatar
unknown committed
2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729
    MYSQL_TIME tm;
 
    length= read_binary_time(&tm, row);
    tm.time_type= MYSQL_TIMESTAMP_TIME;
    send_data_time(param, tm, length);
    break;
  }
  case MYSQL_TYPE_DATETIME:
  case MYSQL_TYPE_TIMESTAMP:
  {
    MYSQL_TIME tm;
 
    length= read_binary_datetime(&tm, row);
    tm.time_type= MYSQL_TIMESTAMP_FULL;
    send_data_time(param, tm, length);
2730 2731 2732 2733
    break;
  }
  default:      
    length= net_field_length(row); 
unknown's avatar
unknown committed
2734
    send_data_str(param,(char*) *row,length);
2735 2736 2737 2738 2739
    break;
  }
  *row+= length;
}

unknown's avatar
unknown committed
2740

unknown's avatar
unknown committed
2741 2742 2743
static void fetch_result_tinyint(MYSQL_BIND *param, uchar **row)
{
  *param->buffer= (uchar) **row;
2744
  (*row)++;
unknown's avatar
unknown committed
2745 2746 2747 2748
}

static void fetch_result_short(MYSQL_BIND *param, uchar **row)
{
2749 2750 2751
  short value = (short)sint2korr(*row);
  int2store(param->buffer, value);
  *row+= 2;
unknown's avatar
unknown committed
2752 2753 2754 2755
}

static void fetch_result_int32(MYSQL_BIND *param, uchar **row)
{
2756
  int32 value= (int32)sint4korr(*row);
unknown's avatar
unknown committed
2757
  int4store(param->buffer, value);
2758
  *row+= 4;
unknown's avatar
unknown committed
2759 2760 2761
}

static void fetch_result_int64(MYSQL_BIND *param, uchar **row)
2762 2763
{  
  longlong value= (longlong)sint8korr(*row);
unknown's avatar
unknown committed
2764
  int8store(param->buffer, value);
2765
  *row+= 8;
unknown's avatar
unknown committed
2766 2767 2768 2769 2770 2771
}

static void fetch_result_float(MYSQL_BIND *param, uchar **row)
{
  float value;
  float4get(value,*row);
unknown's avatar
unknown committed
2772
  float4store(param->buffer, value);
2773
  *row+= 4;
unknown's avatar
unknown committed
2774 2775 2776 2777 2778 2779 2780
}

static void fetch_result_double(MYSQL_BIND *param, uchar **row)
{
  double value;
  float8get(value,*row);
  float8store(param->buffer, value);
2781
  *row+= 8;
unknown's avatar
unknown committed
2782 2783
}

unknown's avatar
unknown committed
2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801
static void fetch_result_time(MYSQL_BIND *param, uchar **row)
{
  MYSQL_TIME *tm= (MYSQL_TIME *)param->buffer;
  *row+= read_binary_time(tm, row);
}

static void fetch_result_date(MYSQL_BIND *param, uchar **row)
{
  MYSQL_TIME *tm= (MYSQL_TIME *)param->buffer;
  *row+= read_binary_date(tm, row);
}

static void fetch_result_datetime(MYSQL_BIND *param, uchar **row)
{
  MYSQL_TIME *tm= (MYSQL_TIME *)param->buffer;
  *row+= read_binary_datetime(tm, row);
}

unknown's avatar
unknown committed
2802 2803 2804 2805 2806 2807 2808 2809
static void fetch_result_bin(MYSQL_BIND *param, uchar **row)
{  
  ulong length= net_field_length(row);
  ulong copy_length= min(length, param->buffer_length);
  memcpy(param->buffer, (char *)*row, copy_length);
  *param->length= length;		
  *row+= length;
}
2810

unknown's avatar
unknown committed
2811 2812 2813
static void fetch_result_str(MYSQL_BIND *param, uchar **row)
{
  ulong length= net_field_length(row);
2814 2815
  ulong copy_length= min(length, param->buffer_length);
  memcpy(param->buffer, (char *)*row, copy_length);
2816 2817 2818
  /* Add an end null if there is room in the buffer */
  if (copy_length != param->buffer_length)
    *(param->buffer+copy_length)= '\0';
2819
  *param->length= length;			/* return total length */
2820
  *row+= length;
unknown's avatar
unknown committed
2821 2822
}

unknown's avatar
unknown committed
2823

2824
/*
2825
  Setup the bind buffers for resultset processing
2826 2827
*/

2828
my_bool STDCALL mysql_bind_result(MYSQL_STMT *stmt, MYSQL_BIND *bind)
2829
{
2830 2831
  MYSQL_BIND *param, *end;
  ulong       bind_count;
2832
  uint        param_count= 0;
2833 2834 2835
  DBUG_ENTER("mysql_bind_result");
  DBUG_ASSERT(stmt != 0);

unknown's avatar
unknown committed
2836
#ifdef CHECK_EXTRA_ARGUMENTS
2837 2838
  if (stmt->state == MY_ST_UNKNOWN)
  {
2839
    set_stmt_error(stmt, CR_NO_PREPARE_STMT, unknown_sqlstate);
2840 2841
    DBUG_RETURN(1);
  }
2842 2843
  if (!bind)
  {
2844
    set_stmt_error(stmt, CR_NULL_POINTER, unknown_sqlstate);
2845
    DBUG_RETURN(1);
2846
  }
2847
#endif
2848 2849 2850 2851
  if (!(bind_count= stmt->field_count) && 
      !(bind_count= alloc_stmt_fields(stmt)))
    DBUG_RETURN(0);
  
2852 2853
  memcpy((char*) stmt->bind, (char*) bind,
	 sizeof(MYSQL_BIND)*bind_count);
2854

2855
  for (param= stmt->bind, end= param+bind_count; param < end ; param++)
2856
  {
unknown's avatar
unknown committed
2857 2858 2859 2860 2861
    /*
      Set param->is_null to point to a dummy variable if it's not set.
      This is to make the excute code easier
    */
    if (!param->is_null)
unknown's avatar
unknown committed
2862
      param->is_null= &param->internal_is_null;
unknown's avatar
unknown committed
2863

unknown's avatar
unknown committed
2864
    if (!param->length)
unknown's avatar
unknown committed
2865
      param->length= &param->internal_length;
unknown's avatar
unknown committed
2866

2867
    param->param_number= param_count++;
unknown's avatar
unknown committed
2868 2869
    param->offset= 0;

unknown's avatar
unknown committed
2870 2871
    /* Setup data copy functions for the different supported types */
    switch (param->buffer_type) {
unknown's avatar
unknown committed
2872 2873
    case MYSQL_TYPE_NULL: /* for dummy binds */
      break;
unknown's avatar
unknown committed
2874 2875
    case MYSQL_TYPE_TINY:
      param->fetch_result= fetch_result_tinyint;
unknown's avatar
unknown committed
2876
      *param->length= 1;
unknown's avatar
unknown committed
2877 2878 2879
      break;
    case MYSQL_TYPE_SHORT:
      param->fetch_result= fetch_result_short;
unknown's avatar
unknown committed
2880
      *param->length= 2;
unknown's avatar
unknown committed
2881 2882 2883
      break;
    case MYSQL_TYPE_LONG:
      param->fetch_result= fetch_result_int32;
unknown's avatar
unknown committed
2884
      *param->length= 4;
unknown's avatar
unknown committed
2885 2886 2887
      break;
    case MYSQL_TYPE_LONGLONG:
      param->fetch_result= fetch_result_int64;
unknown's avatar
unknown committed
2888
      *param->length= 8;
unknown's avatar
unknown committed
2889 2890 2891
      break;
    case MYSQL_TYPE_FLOAT:
      param->fetch_result= fetch_result_float;
unknown's avatar
unknown committed
2892
      *param->length= 4;
unknown's avatar
unknown committed
2893 2894 2895
      break;
    case MYSQL_TYPE_DOUBLE:
      param->fetch_result= fetch_result_double;
unknown's avatar
unknown committed
2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909
      *param->length= 8;
      break;
    case MYSQL_TYPE_TIME:
      param->fetch_result= fetch_result_time;
      *param->length= sizeof(MYSQL_TIME);
      break;
    case MYSQL_TYPE_DATE:
      param->fetch_result= fetch_result_date;
      *param->length= sizeof(MYSQL_TIME);
      break;
    case MYSQL_TYPE_DATETIME:
    case MYSQL_TYPE_TIMESTAMP:
      param->fetch_result= fetch_result_datetime;
      *param->length= sizeof(MYSQL_TIME);
unknown's avatar
unknown committed
2910 2911 2912 2913
      break;
    case MYSQL_TYPE_TINY_BLOB:
    case MYSQL_TYPE_MEDIUM_BLOB:
    case MYSQL_TYPE_LONG_BLOB:
2914
    case MYSQL_TYPE_BLOB:
unknown's avatar
unknown committed
2915 2916 2917
      DBUG_ASSERT(param->buffer_length != 0);
      param->fetch_result= fetch_result_bin;
      break;
unknown's avatar
unknown committed
2918 2919
    case MYSQL_TYPE_VAR_STRING:
    case MYSQL_TYPE_STRING:
unknown's avatar
unknown committed
2920
      DBUG_ASSERT(param->buffer_length != 0);
unknown's avatar
unknown committed
2921 2922 2923
      param->fetch_result= fetch_result_str;
      break;
    default:
2924
      strmov(stmt->sqlstate, unknown_sqlstate);
unknown's avatar
unknown committed
2925 2926
      sprintf(stmt->last_error,
	      ER(stmt->last_errno= CR_UNSUPPORTED_PARAM_TYPE),
2927
	      param->buffer_type, param_count);
unknown's avatar
unknown committed
2928
      DBUG_RETURN(1);
unknown's avatar
unknown committed
2929
    }
2930
  }
unknown's avatar
unknown committed
2931
  stmt->res_buffers= 1;
2932 2933 2934
  DBUG_RETURN(0);
}

unknown's avatar
unknown committed
2935

2936
/*
2937
  Fetch row data to bind buffers
2938 2939
*/

2940
static int stmt_fetch_row(MYSQL_STMT *stmt, uchar *row)
2941
{
2942
  MYSQL_BIND  *bind, *end;
2943
  MYSQL_FIELD *field, *field_end;
unknown's avatar
unknown committed
2944
  uchar *null_ptr, bit;
2945 2946 2947

  if (!row || !stmt->res_buffers)
    return 0;
unknown's avatar
unknown committed
2948 2949
  
  null_ptr= row; 
unknown's avatar
unknown committed
2950 2951
  row+= (stmt->field_count+9)/8;		/* skip null bits */
  bit= 4;					/* first 2 bits are reserved */
unknown's avatar
unknown committed
2952
  
unknown's avatar
unknown committed
2953
  /* Copy complete row to application buffers */
unknown's avatar
unknown committed
2954
  for (bind= stmt->bind, end= (MYSQL_BIND *) bind + stmt->field_count, 
2955 2956
	 field= stmt->fields, 
	 field_end= (MYSQL_FIELD *)stmt->fields+stmt->field_count;
2957
       bind < end && field < field_end;
unknown's avatar
unknown committed
2958 2959
       bind++, field++)
  {         
2960
    if (*null_ptr & bit)
unknown's avatar
unknown committed
2961
      *bind->is_null= bind->null_field= 1;
2962
    else
2963
    { 
unknown's avatar
unknown committed
2964 2965
      *bind->is_null= bind->null_field= 0;
      bind->inter_buffer= row;
2966 2967
      if (field->type == bind->buffer_type)
        (*bind->fetch_result)(bind, &row);
unknown's avatar
unknown committed
2968
      else 
unknown's avatar
unknown committed
2969
        fetch_results(bind, field, &row);
2970
    }
unknown's avatar
unknown committed
2971
    if (!((bit<<=1) & 255))
2972
    {
unknown's avatar
unknown committed
2973
      bit= 1;					/* To next byte */
2974
      null_ptr++;
2975 2976 2977 2978 2979
    }
  }
  return 0;
}

unknown's avatar
unknown committed
2980
int cli_unbuffered_fetch(MYSQL *mysql, char **row)
unknown's avatar
SCRUM  
unknown committed
2981 2982 2983 2984
{
  if (packet_error == net_safe_read(mysql))
    return 1;

unknown's avatar
unknown committed
2985 2986
  *row= ((mysql->net.read_pos[0] == 254) ? NULL :
	 (char*) (mysql->net.read_pos+1));
unknown's avatar
SCRUM  
unknown committed
2987 2988
  return 0;
}
unknown's avatar
unknown committed
2989

2990
/*
2991
  Fetch and return row data to bound buffers, if any
2992 2993
*/

2994
int STDCALL mysql_fetch(MYSQL_STMT *stmt)
2995
{
unknown's avatar
unknown committed
2996
  MYSQL *mysql= stmt->mysql;
2997
  uchar *row;
2998 2999
  DBUG_ENTER("mysql_fetch");

unknown's avatar
unknown committed
3000 3001
  stmt->last_fetched_column= 0;			/* reset */
  if (stmt->result_buffered)			/* buffered */
3002
  {
3003 3004
    MYSQL_RES *res;
    
unknown's avatar
unknown committed
3005
    if (!(res= stmt->result))
3006
      goto no_data;
unknown's avatar
unknown committed
3007 3008 3009

    if (!res->data_cursor) 
    {
3010
      stmt->current_row= 0;
unknown's avatar
unknown committed
3011 3012
      goto no_data;
    }    
3013 3014
    row= (uchar *)res->data_cursor->data;
    res->data_cursor= res->data_cursor->next;
unknown's avatar
unknown committed
3015
  }
unknown's avatar
unknown committed
3016
  else						/* un-buffered */
3017
  {
unknown's avatar
SCRUM  
unknown committed
3018
    if((*mysql->methods->unbuffered_fetch)(mysql, ( char **)&row))
3019
    {
3020 3021
      set_stmt_errmsg(stmt, mysql->net.last_error, mysql->net.last_errno,
		      mysql->net.sqlstate);
3022 3023
      DBUG_RETURN(1);
    }
unknown's avatar
SCRUM  
unknown committed
3024
    if (!row)
3025 3026
    {
      mysql->status= MYSQL_STATUS_READY;
unknown's avatar
unknown committed
3027
      stmt->current_row= 0;
3028 3029
      goto no_data;
    }
unknown's avatar
SCRUM  
unknown committed
3030 3031
  }

3032
  stmt->current_row= row;    
3033 3034 3035 3036 3037 3038 3039
  DBUG_RETURN(stmt_fetch_row(stmt, row));

no_data:
  DBUG_PRINT("info", ("end of data"));    
  DBUG_RETURN(MYSQL_NO_DATA); /* no more data */
}

unknown's avatar
unknown committed
3040

unknown's avatar
unknown committed
3041
/*
unknown's avatar
unknown committed
3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054
  Fetch datat for one specified column data

  SYNOPSIS
    mysql_fetch_column()
    stmt		Prepared statement handler
    bind		Where date should be placed. Should be filled in as
			when calling mysql_bind_param()
    column		Column to fetch (first column is 0)
    ulong offset	Offset in result data (to fetch blob in pieces)
			This is normally 0
  RETURN 
    0	ok
    1	error
unknown's avatar
unknown committed
3055 3056 3057
*/

int STDCALL mysql_fetch_column(MYSQL_STMT *stmt, MYSQL_BIND *bind, 
unknown's avatar
unknown committed
3058
                               uint column, ulong offset)
unknown's avatar
unknown committed
3059
{
unknown's avatar
unknown committed
3060
  MYSQL_BIND *param= stmt->bind+column; 
unknown's avatar
unknown committed
3061 3062
  DBUG_ENTER("mysql_fetch_column");

unknown's avatar
unknown committed
3063
  if (!stmt->current_row)
unknown's avatar
unknown committed
3064 3065 3066
    goto no_data;

#ifdef CHECK_EXTRA_ARGUMENTS  
unknown's avatar
unknown committed
3067
  if (column >= stmt->field_count)
unknown's avatar
unknown committed
3068
  {
unknown's avatar
unknown committed
3069
    set_stmt_errmsg(stmt, "Invalid column descriptor",1, unknown_sqlstate);
unknown's avatar
unknown committed
3070 3071 3072 3073
    DBUG_RETURN(1);
  }
#endif

unknown's avatar
unknown committed
3074
  if (param->null_field)
unknown's avatar
unknown committed
3075 3076 3077 3078 3079 3080
  {
    if (bind->is_null)
      *bind->is_null= 1;
  }
  else
  {    
unknown's avatar
unknown committed
3081 3082
    MYSQL_FIELD *field= stmt->fields+column; 
    uchar *row= param->inter_buffer;
unknown's avatar
unknown committed
3083 3084 3085 3086
    bind->offset= offset;
    if (bind->is_null)
      *bind->is_null= 0;
    if (bind->length) /* Set the length if non char/binary types */
unknown's avatar
unknown committed
3087
      *bind->length= *param->length;
unknown's avatar
unknown committed
3088
    else
unknown's avatar
unknown committed
3089 3090
      bind->length= &param->internal_length;	/* Needed for fetch_result() */
    fetch_results(bind, field, &row);
unknown's avatar
unknown committed
3091 3092 3093 3094 3095 3096 3097 3098 3099
  }
  DBUG_RETURN(0);

no_data:
  DBUG_PRINT("info", ("end of data"));    
  DBUG_RETURN(MYSQL_NO_DATA); /* no more data */
}


3100
/*
3101 3102 3103
  Read all rows of data from server  (binary format)
*/

unknown's avatar
unknown committed
3104
MYSQL_DATA *cli_read_binary_rows(MYSQL_STMT *stmt)
3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115
{
  ulong      pkt_len;
  uchar      *cp;
  MYSQL      *mysql= stmt->mysql;
  MYSQL_DATA *result;
  MYSQL_ROWS *cur, **prev_ptr;
  NET        *net = &mysql->net;
  DBUG_ENTER("read_binary_rows");
 
  mysql= mysql->last_used_con;
  if ((pkt_len= net_safe_read(mysql)) == packet_error)
unknown's avatar
unknown committed
3116
  {
3117 3118 3119
    set_stmt_errmsg(stmt, mysql->net.last_error, mysql->net.last_errno,
		    mysql->net.sqlstate);

3120 3121 3122 3123 3124 3125 3126 3127
    DBUG_RETURN(0);
  }
  if (mysql->net.read_pos[0] == 254) /* end of data */
    return 0;				

  if (!(result=(MYSQL_DATA*) my_malloc(sizeof(MYSQL_DATA),
				       MYF(MY_WME | MY_ZEROFILL))))
  {
3128 3129
    set_stmt_errmsg(stmt, ER(CR_OUT_OF_MEMORY), CR_OUT_OF_MEMORY,
		    unknown_sqlstate);
3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141
    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;

  while (*(cp=net->read_pos) != 254 || pkt_len >= 8)
  {
    result->rows++;

    if (!(cur= (MYSQL_ROWS*) alloc_root(&result->alloc,sizeof(MYSQL_ROWS))) ||
unknown's avatar
unknown committed
3142
	!(cur->data= ((MYSQL_ROW) alloc_root(&result->alloc, pkt_len))))
3143 3144
    {
      free_rows(result);
3145 3146
      set_stmt_errmsg(stmt, ER(CR_OUT_OF_MEMORY), CR_OUT_OF_MEMORY,
		      unknown_sqlstate);
3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168
      DBUG_RETURN(0);
    }
    *prev_ptr= cur;
    prev_ptr= &cur->next;
    memcpy(cur->data, (char*)cp+1, pkt_len-1); 
	  
    if ((pkt_len=net_safe_read(mysql)) == packet_error)
    {
      free_rows(result);
      DBUG_RETURN(0);
    }
  }
  *prev_ptr= 0;
  if (pkt_len > 1)
  {
    mysql->warning_count= uint2korr(cp+1);
    DBUG_PRINT("info",("warning_count:  %ld", mysql->warning_count));
  }
  DBUG_PRINT("exit",("Got %d rows",result->rows));
  DBUG_RETURN(result);
}

3169

3170 3171 3172 3173 3174 3175 3176 3177
/*
  Store or buffer the binary results to stmt
*/

int STDCALL mysql_stmt_store_result(MYSQL_STMT *stmt)
{
  MYSQL *mysql= stmt->mysql;
  MYSQL_RES *result;
3178
  DBUG_ENTER("mysql_stmt_store_result");
3179 3180 3181 3182 3183 3184 3185

  mysql= mysql->last_used_con;

  if (!stmt->field_count)
    DBUG_RETURN(0);
  if (mysql->status != MYSQL_STATUS_GET_RESULT)
  {
3186
    set_stmt_error(stmt, CR_COMMANDS_OUT_OF_SYNC, unknown_sqlstate);
3187
    DBUG_RETURN(1);
3188 3189 3190
  }
  mysql->status= MYSQL_STATUS_READY;		/* server is ready */
  if (!(result= (MYSQL_RES*) my_malloc((uint) (sizeof(MYSQL_RES)+
3191 3192 3193
					       sizeof(ulong) *
					       stmt->field_count),
				       MYF(MY_WME | MY_ZEROFILL))))
3194
  {
3195
    set_stmt_error(stmt, CR_OUT_OF_MEMORY, unknown_sqlstate);
3196
    DBUG_RETURN(1);
3197
  }
3198
  result->methods= mysql->methods;
3199
  stmt->result_buffered= 1;
unknown's avatar
unknown committed
3200
  if (!(result->data= (*stmt->mysql->methods->read_binary_rows)(stmt)))
3201 3202 3203 3204 3205 3206 3207 3208 3209 3210
  {
    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=	stmt->fields;
  result->field_count=	stmt->field_count;
  stmt->result= result;
  DBUG_RETURN(0); /* Data buffered, must be fetched with mysql_fetch() */
3211 3212
}

3213

3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235
/*
  Seek to desired row in the statement result set
*/

MYSQL_ROW_OFFSET STDCALL
mysql_stmt_row_seek(MYSQL_STMT *stmt, MYSQL_ROW_OFFSET row)
{
  MYSQL_RES *result;
  DBUG_ENTER("mysql_stmt_row_seek");
  
  if ((result= stmt->result))
  {
    MYSQL_ROW_OFFSET return_value= result->data_cursor;
    result->current_row= 0;
    result->data_cursor= row;
    DBUG_RETURN(return_value);
  }
  
  DBUG_PRINT("exit", ("stmt doesn't contain any resultset"));
  DBUG_RETURN(0);
}

3236

3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252
/*
  Return the current statement row cursor position
*/

MYSQL_ROW_OFFSET STDCALL 
mysql_stmt_row_tell(MYSQL_STMT *stmt)
{
  DBUG_ENTER("mysql_stmt_row_tell");
  
  if (stmt->result)
    DBUG_RETURN(stmt->result->data_cursor);
  
  DBUG_PRINT("exit", ("stmt doesn't contain any resultset"));
  DBUG_RETURN(0);
}

unknown's avatar
unknown committed
3253

3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264
/*
  Move the stmt result set data cursor to specified row
*/

void STDCALL
mysql_stmt_data_seek(MYSQL_STMT *stmt, my_ulonglong row)
{
  MYSQL_RES   *result;
  DBUG_ENTER("mysql_stmt_data_seek");
  DBUG_PRINT("enter",("row id to seek: %ld",(long) row));
  
3265
  if ((result= stmt->result))
3266 3267 3268 3269 3270 3271 3272
  {
    MYSQL_ROWS	*tmp= 0;
    if (result->data)
      for (tmp=result->data->data; row-- && tmp ; tmp = tmp->next) ;
    result->current_row= 0;
    result->data_cursor= tmp;
  }
3273 3274
  else
    DBUG_PRINT("exit", ("stmt doesn't contain any resultset"));
3275
  DBUG_VOID_RETURN;
3276 3277
}

unknown's avatar
unknown committed
3278

3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291
/*
  Return total rows the current statement result set
*/

my_ulonglong STDCALL mysql_stmt_num_rows(MYSQL_STMT *stmt)
{
  DBUG_ENTER("mysql_stmt_num_rows");
    
  if (stmt->result)
    DBUG_RETURN(stmt->result->row_count);
  
  DBUG_PRINT("exit", ("stmt doesn't contain any resultset"));
  DBUG_RETURN(0);
3292
}
3293 3294

/********************************************************************
3295
 statement error handling and close
3296
*********************************************************************/
3297

3298
/*
unknown's avatar
unknown committed
3299
  Close the statement handle by freeing all alloced resources
3300

unknown's avatar
unknown committed
3301 3302 3303 3304 3305 3306 3307 3308
  SYNOPSIS
    mysql_stmt_close()
    stmt	       Statement handle
    skip_list    Flag to indicate delete from list or not
  RETURN VALUES
    0	ok
    1	error
*/
3309 3310 3311

my_bool STDCALL mysql_stmt_free_result(MYSQL_STMT *stmt)
{ 
3312
  MYSQL *mysql;
3313
  DBUG_ENTER("mysql_stmt_free_result");
3314

unknown's avatar
unknown committed
3315
  DBUG_ASSERT(stmt != 0);
3316
  
3317
  mysql= stmt->mysql;
3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331
  if (mysql->status != MYSQL_STATUS_READY)
  {
    /* Clear the current execution status */
    DBUG_PRINT("warning",("Not all packets read, clearing them"));
    for (;;)
    {
      ulong pkt_len;
      if ((pkt_len= net_safe_read(mysql)) == packet_error)
        break;
      if (pkt_len <= 8 && mysql->net.read_pos[0] == 254)
        break;	
    }
    mysql->status= MYSQL_STATUS_READY;
  }
3332
  mysql_free_result(stmt->result);
3333 3334 3335
  stmt->result= 0;
  stmt->result_buffered= 0;
  stmt->current_row= 0;
3336 3337 3338
  DBUG_RETURN(0);
}

unknown's avatar
unknown committed
3339

3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352
my_bool stmt_close(MYSQL_STMT *stmt, my_bool skip_list)
{
  MYSQL *mysql;
  DBUG_ENTER("mysql_stmt_close");

  DBUG_ASSERT(stmt != 0);
  
  if (!(mysql= stmt->mysql))
  {
    my_free((gptr) stmt, MYF(MY_WME));
    DBUG_RETURN(0);
  }
  mysql_stmt_free_result(stmt);
unknown's avatar
unknown committed
3353
  if (stmt->state == MY_ST_PREPARE || stmt->state == MY_ST_EXECUTE)
3354 3355 3356
  {
    char buff[4];
    int4store(buff, stmt->stmt_id);
3357 3358
    if (simple_command(mysql, COM_CLOSE_STMT, buff, 4, 1))
    {
3359 3360
      set_stmt_errmsg(stmt, mysql->net.last_error, mysql->net.last_errno,
		      mysql->net.sqlstate);
3361 3362 3363
      stmt->mysql= NULL; /* connection isn't valid anymore */
      DBUG_RETURN(1);
    }
3364
  }
unknown's avatar
unknown committed
3365
  stmt->field_count= 0;
3366 3367 3368 3369
  free_root(&stmt->mem_root, MYF(0));
  if (!skip_list)
    mysql->stmts= list_delete(mysql->stmts, &stmt->list);
  mysql->status= MYSQL_STATUS_READY;
unknown's avatar
unknown committed
3370
  my_free((gptr) stmt->query, MYF(MY_WME));
3371 3372
  my_free((gptr) stmt, MYF(MY_WME));
  DBUG_RETURN(0);
3373 3374
}

unknown's avatar
unknown committed
3375

unknown's avatar
unknown committed
3376 3377 3378 3379 3380
my_bool STDCALL mysql_stmt_close(MYSQL_STMT *stmt)
{
  return stmt_close(stmt, 0);
}

3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393
/*
  Reset the statement buffers in server
*/

my_bool STDCALL mysql_stmt_reset(MYSQL_STMT *stmt)
{
  char buff[MYSQL_STMT_HEADER];
  MYSQL *mysql;
  DBUG_ENTER("mysql_stmt_reset");
  DBUG_ASSERT(stmt != 0);
  
  mysql= stmt->mysql->last_used_con;
  int4store(buff, stmt->stmt_id);		/* Send stmt id to server */
unknown's avatar
SCRUM  
unknown committed
3394
  if ((*mysql->methods->advanced_command)(mysql, COM_RESET_STMT,buff,MYSQL_STMT_HEADER,0,0,1))
3395 3396 3397 3398 3399 3400 3401 3402
  {
    set_stmt_errmsg(stmt, mysql->net.last_error, mysql->net.last_errno, 
                    mysql->net.sqlstate);
    DBUG_RETURN(1);
  }
  DBUG_RETURN(0);
}

3403 3404 3405 3406 3407 3408 3409
/*
  Return statement error code
*/

uint STDCALL mysql_stmt_errno(MYSQL_STMT * stmt)
{
  DBUG_ENTER("mysql_stmt_errno");
3410
  DBUG_RETURN(stmt->last_errno);
3411 3412
}

3413 3414 3415 3416 3417 3418
const char *STDCALL mysql_stmt_sqlstate(MYSQL_STMT * stmt)
{
  DBUG_ENTER("mysql_stmt_sqlstate");
  DBUG_RETURN(stmt->sqlstate);
}

3419 3420 3421 3422 3423 3424 3425
/*
  Return statement error message
*/

const char *STDCALL mysql_stmt_error(MYSQL_STMT * stmt)
{
  DBUG_ENTER("mysql_stmt_error");
3426
  DBUG_RETURN(stmt->last_error);
3427 3428
}

3429 3430 3431 3432
/********************************************************************
 Transactional APIs
*********************************************************************/

3433 3434 3435 3436
/*
  Commit the current transaction
*/

3437
my_bool STDCALL mysql_commit(MYSQL * mysql)
3438 3439
{
  DBUG_ENTER("mysql_commit");
3440
  DBUG_RETURN((my_bool) mysql_real_query(mysql, "commit", 6));
3441 3442 3443
}

/*
3444
  Rollback the current transaction
3445 3446
*/

3447
my_bool STDCALL mysql_rollback(MYSQL * mysql)
3448 3449
{
  DBUG_ENTER("mysql_rollback");
3450
  DBUG_RETURN((my_bool) mysql_real_query(mysql, "rollback", 8));
3451 3452 3453 3454 3455 3456 3457
}


/*
  Set autocommit to either true or false
*/

3458
my_bool STDCALL mysql_autocommit(MYSQL * mysql, my_bool auto_mode)
3459 3460 3461 3462 3463
{
  DBUG_ENTER("mysql_autocommit");
  DBUG_PRINT("enter", ("mode : %d", auto_mode));

  if (auto_mode) /* set to true */
3464 3465
    DBUG_RETURN((my_bool) mysql_real_query(mysql, "set autocommit=1", 16));
  DBUG_RETURN((my_bool) mysql_real_query(mysql, "set autocommit=0", 16));
3466
}
3467 3468 3469


/********************************************************************
3470
 Multi query execution + SPs APIs
3471 3472 3473 3474 3475 3476 3477 3478 3479
*********************************************************************/

/*
  Returns if there are any more query results exists to be read using 
  mysql_next_result()
*/

my_bool STDCALL mysql_more_results(MYSQL *mysql)
{
3480
  my_bool res;
3481 3482
  DBUG_ENTER("mysql_more_results");
  
3483 3484 3485 3486
  res= ((mysql->last_used_con->server_status & SERVER_MORE_RESULTS_EXISTS) ? 
	1: 0);
  DBUG_PRINT("exit",("More results exists ? %d", res)); 
  DBUG_RETURN(res);
3487 3488
}

3489

3490 3491 3492 3493
/*
  Reads and returns the next query results
*/

3494
int STDCALL mysql_next_result(MYSQL *mysql)
3495
{
3496
  DBUG_ENTER("mysql_next_result");
3497
  
3498 3499 3500 3501 3502 3503 3504 3505
  if (mysql->status != MYSQL_STATUS_READY)
  {
    strmov(mysql->net.sqlstate, unknown_sqlstate);
    strmov(mysql->net.last_error,
	   ER(mysql->net.last_errno=CR_COMMANDS_OUT_OF_SYNC));
    DBUG_RETURN(1);
  }

3506 3507
  mysql->net.last_error[0]= 0;
  mysql->net.last_errno= 0;
3508
  strmov(mysql->net.sqlstate, not_error_sqlstate);
3509 3510 3511
  mysql->affected_rows= ~(my_ulonglong) 0;

  if (mysql->last_used_con->server_status & SERVER_MORE_RESULTS_EXISTS)
unknown's avatar
unknown committed
3512
    DBUG_RETURN((*mysql->methods->read_query_result)(mysql));
3513
  
3514
  DBUG_RETURN(-1);				/* No more results */
3515
}
unknown's avatar
unknown committed
3516

3517

unknown's avatar
unknown committed
3518 3519 3520 3521 3522 3523 3524 3525 3526 3527
MYSQL_RES * STDCALL mysql_use_result(MYSQL *mysql)
{
  return (*mysql->methods->use_result)(mysql);
}

my_bool STDCALL mysql_read_query_result(MYSQL *mysql)
{
  return (*mysql->methods->read_query_result)(mysql);
}