lib_sql.cc 20.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * Copyright (c)  2000
 * SWsoft  company
 *
 * This material is provided "as is", with absolutely no warranty expressed
 * or implied. Any use is at your own risk.
 *
 * Permission to use or copy this software for any purpose is hereby granted 
 * without fee, provided the above notices are retained on all copies.
 * Permission to modify the code and to distribute modified code is granted,
 * provided the above notices are retained, and a notice that the code was
 * modified is included with the above copyright notice.
 *
14 15 16

  This code was modified by the MySQL team
*/
17

18 19 20 21
/*
  The following is needed to not cause conflicts when we include mysqld.cc
*/

22 23 24 25
#define main main1
#define mysql_unix_port mysql_inix_port1
#define mysql_port mysql_port1

hf@genie.(none)'s avatar
hf@genie.(none) committed
26 27 28 29
static int fake_argc= 1;
static char *fake_argv[]= {(char *)"", 0};
static const char *fake_groups[] = { "server", "embedded", 0 };

30 31 32
#if defined (__WIN__)
#include "../sql/mysqld.cpp"
#else
33
#include "../sql/mysqld.cc"
34
#endif
35

hf@deer.(none)'s avatar
hf@deer.(none) committed
36 37 38
int check_user(THD *thd, enum enum_server_command command, 
	       const char *passwd, uint passwd_len, const char *db,
	       bool check_count);
39
C_MODE_START
40
#include <mysql.h>
hf@deer.(none)'s avatar
hf@deer.(none) committed
41
#undef ER
42
#include "errmsg.h"
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
43
#include <sql_common.h>
44

hf@deer.(none)'s avatar
hf@deer.(none) committed
45 46 47
static my_bool  org_my_init_done;
my_bool         server_inited;

hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
48
static my_bool STDCALL
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
49 50 51
emb_advanced_command(MYSQL *mysql, enum enum_server_command command,
		     const char *header, ulong header_length,
		     const char *arg, ulong arg_length, my_bool skip_check)
52
{
53
  my_bool result= 1;
54
  THD *thd=(THD *) mysql->thd;
hf@deer.(none)'s avatar
hf@deer.(none) committed
55
  NET *net= &mysql->net;
56 57 58 59

  /* Check that we are calling the client functions in right order */
  if (mysql->status != MYSQL_STATUS_READY)
  {
hf@deer.(none)'s avatar
hf@deer.(none) committed
60 61
    strmov(net->last_error,
	   ER(net->last_errno=CR_COMMANDS_OUT_OF_SYNC));
62 63 64 65
    return 1;
  }

  /* Clear result variables */
66
  thd->clear_error();
67
  mysql->affected_rows= ~(my_ulonglong) 0;
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
68
  mysql->field_count= 0;
69

70
  thd->store_globals();				// Fix if more than one connect
hf@deer.(none)'s avatar
hf@deer.(none) committed
71 72 73 74 75 76
  /* 
     We have to call free_old_query before we start to fill mysql->fields 
     for new query. In the case of embedded server we collect field data
     during query execution (not during data retrieval as it is in remote
     client). So we have to call free_old_query here
  */
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
77
  free_old_query(mysql);
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
78 79 80 81 82 83
  if (!arg)
  {
    arg= header;
    arg_length= header_length;
  }

hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
84
  result= dispatch_command(command, thd, (char *) arg, arg_length + 1);
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
85

hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
86
  if (!skip_check)
87
    result= thd->net.last_errno ? -1 : 0;
88

hf@deer.(none)'s avatar
hf@deer.(none) committed
89
  if ((net->last_errno= thd->net.last_errno))
90
  {
hf@deer.(none)'s avatar
hf@deer.(none) committed
91 92
    memcpy(net->last_error, thd->net.last_error, sizeof(net->last_error));
    memcpy(net->sqlstate, thd->net.sqlstate, sizeof(net->sqlstate));
93
  }
hf@deer.(none)'s avatar
hf@deer.(none) committed
94 95
  else
    net->last_error[0]= 0;
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
96
  mysql->warning_count= ((THD*)mysql->thd)->total_warn_count;
97
  return result;
98 99
}

hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
100 101
static MYSQL_DATA * STDCALL 
emb_read_rows(MYSQL *mysql, MYSQL_FIELD *mysql_fields __attribute__((unused)),
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
102
	      unsigned int fields __attribute__((unused)))
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
103 104 105
{
  MYSQL_DATA *result= ((THD*)mysql->thd)->data;
  if (!result)
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
106 107 108 109 110 111 112 113 114 115 116 117
  {
    if (!(result=(MYSQL_DATA*) my_malloc(sizeof(MYSQL_DATA),
					 MYF(MY_WME | MY_ZEROFILL))))
    {
      NET *net = &mysql->net;
      net->last_errno=CR_OUT_OF_MEMORY;
      strmov(net->sqlstate, unknown_sqlstate);
      strmov(net->last_error,ER(net->last_errno));
      return NULL;
    }    
    return result;
  }
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
118 119 120 121 122 123 124 125 126 127 128 129 130
  *result->prev_ptr= NULL;
  ((THD*)mysql->thd)->data= NULL;
  return result;
}

static MYSQL_FIELD * STDCALL emb_list_fields(MYSQL *mysql)
{
  return mysql->fields;
}

static my_bool STDCALL emb_read_prepare_result(MYSQL *mysql, MYSQL_STMT *stmt)
{
  THD *thd= (THD*)mysql->thd;
hf@deer.(none)'s avatar
hf@deer.(none) committed
131 132
  if (mysql->net.last_errno)
    return 1;
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
133 134 135 136 137 138 139 140 141 142 143
  stmt->stmt_id= thd->client_stmt_id;
  stmt->param_count= thd->client_param_count;
  stmt->field_count= mysql->field_count;

  if (stmt->field_count != 0)
  {
    if (!(mysql->server_status & SERVER_STATUS_AUTOCOMMIT))
      mysql->server_status|= SERVER_STATUS_IN_TRANS;

    stmt->fields= mysql->fields;
    stmt->mem_root= mysql->field_alloc;
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
144
    mysql->fields= NULL;
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
145
  }
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
146

hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
147 148 149 150 151 152 153 154 155
  return 0;
}

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

hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
156
static void STDCALL emb_fetch_lengths(ulong *to, MYSQL_ROW column, unsigned int field_count)
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
{ 
  MYSQL_ROW end;

  for (end=column + field_count; column != end ; column++,to++)
    *to= *column ? *(uint *)((*column) - sizeof(uint)) : 0;
}

static my_bool STDCALL emb_mysql_read_query_result(MYSQL *mysql)
{
  if (mysql->net.last_errno)
    return -1;

  if (mysql->field_count)
    mysql->status=MYSQL_STATUS_GET_RESULT;

  return 0;
}

hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
175 176 177 178 179
static int STDCALL emb_stmt_execute(MYSQL_STMT *stmt)
{
  DBUG_ENTER("emb_stmt_execute");
  THD *thd= (THD*)stmt->mysql->thd;
  thd->client_param_count= stmt->param_count;
hf@deer.(none)'s avatar
hf@deer.(none) committed
180
  thd->client_params= stmt->params;
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
181 182 183 184 185 186 187 188 189 190 191
  if (emb_advanced_command(stmt->mysql, COM_EXECUTE,0,0,
			   (const char*)&stmt->stmt_id,sizeof(stmt->stmt_id),1)
      || emb_mysql_read_query_result(stmt->mysql))
  {
    NET *net= &stmt->mysql->net;
    set_stmt_errmsg(stmt, net->last_error, net->last_errno, net->sqlstate);
    DBUG_RETURN(1);
  }
  DBUG_RETURN(0);
}

hf@deer.(none)'s avatar
hf@deer.(none) committed
192 193 194 195 196
MYSQL_DATA *emb_read_binary_rows(MYSQL_STMT *stmt)
{
  return emb_read_rows(stmt->mysql, 0, 0);
}

hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
int STDCALL emb_unbuffered_fetch(MYSQL *mysql, char **row)
{
  MYSQL_DATA *data= ((THD*)mysql->thd)->data;
  if (!data || !data->data)
  {
    *row= NULL;
    if (data)
    {
      free_rows(data);
      ((THD*)mysql->thd)->data= NULL;
    }
  }
  else
  {
    *row= (char *)data->data->data;
    data->data= data->data->next;
  }
  return 0;
}

hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
217 218 219 220 221
static void STDCALL emb_free_embedded_thd(MYSQL *mysql)
{
  THD *thd= (THD*)mysql->thd;
  if (thd->data)
    free_rows(thd->data);
hf@deer.(none)'s avatar
hf@deer.(none) committed
222
  thread_count--;
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
223 224 225
  delete thd;
}

hf@deer.(none)'s avatar
hf@deer.(none) committed
226 227 228 229 230 231
static const char * STDCALL emb_read_statistic(MYSQL *mysql)
{
  THD *thd= (THD*)mysql->thd;
  return thd->net.last_error;
}

hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
232 233 234 235 236 237 238 239
MYSQL_METHODS embedded_methods= 
{
  emb_mysql_read_query_result,
  emb_advanced_command,
  emb_read_rows,
  mysql_store_result,
  emb_fetch_lengths, 
  emb_list_fields,
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
240
  emb_read_prepare_result,
hf@deer.(none)'s avatar
hf@deer.(none) committed
241
  emb_stmt_execute,
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
242
  emb_read_binary_rows,
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
243
  emb_unbuffered_fetch,
hf@deer.(none)'s avatar
hf@deer.(none) committed
244 245
  emb_free_embedded_thd,
  emb_read_statistic
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
246 247
};

248
C_MODE_END
249

250
void THD::clear_error()
251
{
252 253 254
  net.last_error[0]= 0;
  net.last_errno= 0;
  net.report_error= 0;
255 256
}

257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
/*
  Make a copy of array and the strings array points to
*/

char **copy_arguments(int argc, char **argv)
{
  uint length= 0;
  char **from, **res, **end= argv+argc;

  for (from=argv ; from != end ; from++)
    length+= strlen(*from);

  if ((res= (char**) my_malloc(sizeof(argv)*(argc+1)+length+argc,
			       MYF(MY_WME))))
  {
    char **to= res, *to_str= (char*) (res+argc+1);
    for (from=argv ; from != end ;)
    {
      *to++= to_str;
      to_str= strmov(to_str, *from++)+1;
    }
    *to= 0;					// Last ptr should be null
  }
  return res;
}


284 285 286
extern "C"
{

287
char **		copy_arguments_ptr= 0; 
288

hf@genie.(none)'s avatar
hf@genie.(none) committed
289 290 291 292 293 294 295 296 297 298 299 300 301
int STDCALL mysql_server_init(int argc, char **argv, char **groups)
{
  char glob_hostname[FN_REFLEN];

  /* This mess is to allow people to call the init function without
   * having to mess with a fake argv */
  int *argcp;
  char ***argvp;
  int fake_argc = 1;
  char *fake_argv[] = { (char *)"", 0 };
  const char *fake_groups[] = { "server", "embedded", 0 };
  if (argc)
  {
302 303
    argcp= &argc;
    argvp= (char***) &argv;
hf@genie.(none)'s avatar
hf@genie.(none) committed
304 305 306
  }
  else
  {
307 308
    argcp= &fake_argc;
    argvp= (char ***) &fake_argv;
hf@genie.(none)'s avatar
hf@genie.(none) committed
309 310
  }
  if (!groups)
311
    groups= (char**) fake_groups;
hf@genie.(none)'s avatar
hf@genie.(none) committed
312 313 314


  /* Only call MY_INIT() if it hasn't been called before */
hf@deer.(none)'s avatar
hf@deer.(none) committed
315
  if (!server_inited)
hf@genie.(none)'s avatar
hf@genie.(none) committed
316
  {
hf@deer.(none)'s avatar
hf@deer.(none) committed
317
    server_inited=1;
hf@genie.(none)'s avatar
hf@genie.(none) committed
318 319 320 321 322 323 324
    org_my_init_done=my_init_done;
  }
  if (!org_my_init_done)
  {
    MY_INIT((char *)"mysql_embedded");	// init my_sys library & pthreads
  }

hf@deer.(none)'s avatar
hf@deer.(none) committed
325
  if (init_common_variables("my", *argcp, *argvp, (const char **)groups))
hf@genie.(none)'s avatar
hf@genie.(none) committed
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
  {
    mysql_server_end();
    return 1;
  }
    
  /* Get default temporary directory */
  opt_mysql_tmpdir=getenv("TMPDIR");	/* Use this if possible */
#if defined( __WIN__) || defined(OS2)
  if (!opt_mysql_tmpdir)
    opt_mysql_tmpdir=getenv("TEMP");
  if (!opt_mysql_tmpdir)
    opt_mysql_tmpdir=getenv("TMP");
#endif
  if (!opt_mysql_tmpdir || !opt_mysql_tmpdir[0])
    opt_mysql_tmpdir=(char*) P_tmpdir;		/* purecov: inspected */

  umask(((~my_umask) & 0666));
  if (init_server_components())
  {
    mysql_server_end();
    return 1;
  }

  error_handler_hook = my_message_sql;

hf@deer.(none)'s avatar
hf@deer.(none) committed
351
#ifndef NO_EMBEDDED_ACCESS_CHECKS
hf@deer.mysql.r18.ru's avatar
hf@deer.mysql.r18.ru committed
352
  if (acl_init((THD *)0, opt_noacl))
hf@genie.(none)'s avatar
hf@genie.(none) committed
353 354 355 356 357
  {
    mysql_server_end();
    return 1;
  }
  if (!opt_noacl)
hf@deer.mysql.r18.ru's avatar
hf@deer.mysql.r18.ru committed
358
    (void) grant_init((THD *)0);
hf@deer.(none)'s avatar
hf@deer.(none) committed
359 360 361

#endif

hf@genie.(none)'s avatar
hf@genie.(none) committed
362 363 364 365
  init_max_user_conn();
  init_update_queries();

#ifdef HAVE_DLOPEN
hf@deer.(none)'s avatar
hf@deer.(none) committed
366
#ifndef NO_EMBEDDED_ACCESS_CHECKS
hf@genie.(none)'s avatar
hf@genie.(none) committed
367
  if (!opt_noacl)
hf@deer.(none)'s avatar
hf@deer.(none) committed
368
#endif
hf@genie.(none)'s avatar
hf@genie.(none) committed
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
    udf_init();
#endif

  if (opt_bin_log)
  {
    if (!opt_bin_logname)
    {
      char tmp[FN_REFLEN];
      /* TODO: The following should be using fn_format();  We just need to
	 first change fn_format() to cut the file name if it's too long.
      */
      strmake(tmp,glob_hostname,FN_REFLEN-5);
      strmov(strcend(tmp,'.'),"-bin");
      opt_bin_logname=my_strdup(tmp,MYF(MY_WME));
    }
    open_log(&mysql_bin_log, glob_hostname, opt_bin_logname, "-bin",
385
	     opt_binlog_index_name, LOG_BIN, 0, 0, max_binlog_size);
hf@genie.(none)'s avatar
hf@genie.(none) committed
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
    using_update_log=1;
  }

  (void) thr_setconcurrency(concurrency);	// 10 by default

  if (
#ifdef HAVE_BERKELEY_DB
      !berkeley_skip ||
#endif
      (flush_time && flush_time != ~(ulong) 0L))
  {
    pthread_t hThread;
    if (pthread_create(&hThread,&connection_attrib,handle_manager,0))
      sql_print_error("Warning: Can't create thread to manage maintenance");
  }

hf@deer.(none)'s avatar
hf@deer.(none) committed
402 403 404 405 406 407 408 409 410
  if (opt_init_file)
  {
    if (read_init_file(opt_init_file))
    {
      mysql_server_end();
      return 1;
    }
  }

hf@genie.(none)'s avatar
hf@genie.(none) committed
411 412 413 414 415 416 417 418 419 420 421
  /*
    Update mysqld variables from client variables if set
    The client variables are set also by get_one_option() in mysqld.cc
  */
  if (max_allowed_packet)
    global_system_variables.max_allowed_packet= max_allowed_packet;
  if (net_buffer_length)
    global_system_variables.net_buffer_length= net_buffer_length;
  return 0;
}

422
void STDCALL mysql_server_end()
423
{
424 425
  my_free((char*) copy_arguments_ptr, MYF(MY_ALLOW_ZERO_PTR));
  copy_arguments_ptr=0;
426 427 428 429 430
  clean_up(0);
#ifdef THREAD
  /* Don't call my_thread_end() if the application is using MY_INIT() */
  if (!org_my_init_done)
    my_thread_end();
431
#endif
432 433 434
  /* If library called my_init(), free memory allocated by it */
  if (!org_my_init_done)
    my_end(0);
tim@black.box's avatar
tim@black.box committed
435 436
}

437
} /* extern "C" */
438

439
C_MODE_START
440 441
void init_embedded_mysql(MYSQL *mysql, int client_flag, char *db)
{
442
  THD *thd = (THD *)mysql->thd;
443
  thd->mysql= mysql;
hf@deer.(none)'s avatar
hf@deer.(none) committed
444
  mysql->server_version= server_version;
445
}
446

447
void *create_embedded_thd(int client_flag, char *db)
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
{
  THD * thd= new THD;
  thd->thread_id= thread_id++;

  if (thd->store_globals())
  {
    fprintf(stderr,"store_globals failed.\n");
    return NULL;
  }

  thd->mysys_var= my_thread_var;
  thd->dbug_thread_id= my_thread_id();
  thd->thread_stack= (char*) &thd;

  thd->proc_info=0;				// Remove 'login'
  thd->command=COM_SLEEP;
  thd->version=refresh_version;
  thd->set_time();
  init_sql_alloc(&thd->mem_root,8192,8192);
  thd->client_capabilities= client_flag;

  thd->db= db;
hf@bison.(none)'s avatar
hf@bison.(none) committed
470
  thd->db_length= db ? strip_sp(db) : 0;
hf@deer.(none)'s avatar
hf@deer.(none) committed
471
#ifndef NO_EMBEDDED_ACCESS_CHECKS
472 473
  thd->db_access= DB_ACLS;
  thd->master_access= ~NO_ACCESS;
hf@deer.(none)'s avatar
hf@deer.(none) committed
474
#endif
475
  thd->net.query_cache_query= 0;
476

hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
477 478
  thd->data= 0;

hf@deer.(none)'s avatar
hf@deer.(none) committed
479
  thread_count++;
480 481
  return thd;
}
482

hf@deer.(none)'s avatar
hf@deer.(none) committed
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
#ifndef NO_EMBEDDED_ACCESS_CHECKS
int check_embedded_connection(MYSQL *mysql)
{
  THD *thd= (THD*)mysql->thd;
  int result;
  char scramble_buff[SCRAMBLE_LENGTH];
  int passwd_len;

  thd->host= mysql->options.client_ip ?
    mysql->options.client_ip : (char*)my_localhost;
  thd->ip= thd->host;
  thd->host_or_ip= thd->host;

  if (acl_check_host(thd->host,thd->ip))
  {
    result= ER_HOST_NOT_PRIVILEGED;
    goto err;
  }

  thd->user= mysql->user;
  if (mysql->passwd && mysql->passwd[0])
  {
    memset(thd->scramble, 55, SCRAMBLE_LENGTH); // dummy scramble
    thd->scramble[SCRAMBLE_LENGTH]= 0;
    scramble(scramble_buff, thd->scramble, mysql->passwd);
    passwd_len= SCRAMBLE_LENGTH;
  }
  else
    passwd_len= 0;

  if((result= check_user(thd, COM_CONNECT, 
			 scramble_buff, passwd_len, thd->db, true)))
     goto err;

  return 0;
err:
  {
    NET *net= &mysql->net;
    memcpy(net->last_error, thd->net.last_error, sizeof(net->last_error));
    memcpy(net->sqlstate, thd->net.sqlstate, sizeof(net->sqlstate));
  }
  return result;
}
#endif

528
C_MODE_END
529

hf@deer.mysql.r18.ru's avatar
hf@deer.mysql.r18.ru committed
530 531 532 533
bool Protocol::send_fields(List<Item> *list, uint flag)
{
  List_iterator_fast<Item> it(*list);
  Item                     *item;
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
534
  MYSQL_FIELD              *client_field;
hf@deer.mysql.r18.ru's avatar
hf@deer.mysql.r18.ru committed
535
  MYSQL                    *mysql= thd->mysql;
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
536
  MEM_ROOT                 *field_alloc;
hf@deer.mysql.r18.ru's avatar
hf@deer.mysql.r18.ru committed
537
  
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
538
  DBUG_ENTER("send_fields");
hf@deer.mysql.r18.ru's avatar
hf@deer.mysql.r18.ru committed
539

hf@deer.(none)'s avatar
hf@deer.(none) committed
540 541 542
  if (!mysql)            // bootstrap file handling
    DBUG_RETURN(0);

hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
543
  field_count= list->elements;
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
544 545 546 547
  field_alloc= &mysql->field_alloc;
  if (!(client_field= thd->mysql->fields= 
	(MYSQL_FIELD *)alloc_root(field_alloc, 
				  sizeof(MYSQL_FIELD) * field_count)))
hf@deer.mysql.r18.ru's avatar
hf@deer.mysql.r18.ru committed
548 549 550 551 552 553 554
    goto err;

  while ((item= it++))
  {
    Send_field server_field;
    item->make_field(&server_field);

hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
555 556 557 558 559
    client_field->db=	  strdup_root(field_alloc, server_field.db_name);
    client_field->table=  strdup_root(field_alloc, server_field.table_name);
    client_field->name=   strdup_root(field_alloc, server_field.col_name);
    client_field->org_table= strdup_root(field_alloc, server_field.org_table_name);
    client_field->org_name=  strdup_root(field_alloc, server_field.org_col_name);
hf@deer.mysql.r18.ru's avatar
hf@deer.mysql.r18.ru committed
560 561 562 563
    client_field->length= server_field.length;
    client_field->type=   server_field.type;
    client_field->flags= server_field.flags;
    client_field->decimals= server_field.decimals;
564 565 566 567 568 569 570
    client_field->db_length=		strlen(client_field->db);
    client_field->table_length=		strlen(client_field->table);
    client_field->name_length=		strlen(client_field->name);
    client_field->org_name_length=	strlen(client_field->org_name);
    client_field->org_table_length=	strlen(client_field->org_table);
    client_field->charsetnr=		server_field.charsetnr;
    
hf@deer.mysql.r18.ru's avatar
hf@deer.mysql.r18.ru committed
571 572 573 574 575 576 577 578 579
    if (INTERNAL_NUM_FIELD(client_field))
      client_field->flags|= NUM_FLAG;

    if (flag & 2)
    {
      char buff[80];
      String tmp(buff, sizeof(buff), default_charset_info), *res;

      if (!(res=item->val_str(&tmp)))
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
580
	client_field->def= strdup_root(field_alloc, "");
hf@deer.mysql.r18.ru's avatar
hf@deer.mysql.r18.ru committed
581
      else
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
582
	client_field->def= strdup_root(field_alloc, tmp.ptr());
hf@deer.mysql.r18.ru's avatar
hf@deer.mysql.r18.ru committed
583 584 585 586 587 588
    }
    else
      client_field->def=0;
    client_field->max_length= 0;
    ++client_field;
  }
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
589
  thd->mysql->field_count= field_count;
hf@deer.mysql.r18.ru's avatar
hf@deer.mysql.r18.ru committed
590

hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
591
  DBUG_RETURN(prepare_for_send(list));
hf@deer.mysql.r18.ru's avatar
hf@deer.mysql.r18.ru committed
592 593
 err:
  send_error(thd, ER_OUT_OF_RESOURCES);	/* purecov: inspected */
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
594
  DBUG_RETURN(1);				/* purecov: inspected */
hf@deer.mysql.r18.ru's avatar
hf@deer.mysql.r18.ru committed
595 596
}

hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
597
bool Protocol::send_records_num(List<Item> *list, ulonglong records)
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
598 599 600 601
{
  return false;
}

hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
602
bool Protocol::write()
hf@genie.(none)'s avatar
hf@genie.(none) committed
603
{
hf@deer.(none)'s avatar
hf@deer.(none) committed
604 605 606
  if (!thd->mysql)            // bootstrap file handling
    return false;

hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
607 608
  *next_field= 0;
  return false;
hf@genie.(none)'s avatar
hf@genie.(none) committed
609
}
610

hf@deer.(none)'s avatar
hf@deer.(none) committed
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
bool Protocol_prep::write()
{
  MYSQL_ROWS *cur;
  MYSQL_DATA *data= thd->data;

  if (!data)
  {
    if (!(data= (MYSQL_DATA*) my_malloc(sizeof(MYSQL_DATA),
					MYF(MY_WME | MY_ZEROFILL))))
      return true;
    
    alloc= &data->alloc;
    init_alloc_root(alloc,8192,0);	/* Assume rowlength < 8192 */
    alloc->min_malloc=sizeof(MYSQL_ROWS);
    data->rows=0;
    data->fields=field_count;
    data->prev_ptr= &data->data;
    thd->data= data;
  }

  data->rows++;
  if (!(cur= (MYSQL_ROWS *)alloc_root(alloc, sizeof(MYSQL_ROWS)+packet->length())))
  {
    my_error(ER_OUT_OF_RESOURCES,MYF(0));
    return true;
  }
  cur->data= (MYSQL_ROW)(((char *)cur) + sizeof(MYSQL_ROWS));
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
638
  memcpy(cur->data, packet->ptr()+1, packet->length()-1);
hf@deer.(none)'s avatar
hf@deer.(none) committed
639 640 641

  *data->prev_ptr= cur;
  data->prev_ptr= &cur->next;
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
642 643
  cur->next= 0;
  
hf@deer.(none)'s avatar
hf@deer.(none) committed
644 645 646
  return false;
}

647
void
648
send_ok(THD *thd,ha_rows affected_rows,ulonglong id,const char *message)
649 650 651
{
  DBUG_ENTER("send_ok");
  MYSQL *mysql= current_thd->mysql;
hf@deer.(none)'s avatar
hf@deer.(none) committed
652 653
  if (!mysql)            // bootstrap file handling
    DBUG_VOID_RETURN;
654 655 656
  mysql->affected_rows= affected_rows;
  mysql->insert_id= id;
  if (message)
657
    strmake(thd->net.last_error, message, sizeof(thd->net.last_error)-1);
658 659 660
  DBUG_VOID_RETURN;
}

661 662 663 664 665
void
send_eof(THD *thd, bool no_flush)
{
}

hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
666 667
void Protocol_simple::prepare_for_resend()
{
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
668 669
  MYSQL_ROWS *cur;
  MYSQL_DATA *data= thd->data;
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
670 671 672

  DBUG_ENTER("send_data");

hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
  if (!data)
  {
    if (!(data= (MYSQL_DATA*) my_malloc(sizeof(MYSQL_DATA),
					MYF(MY_WME | MY_ZEROFILL))))
      goto err;
    
    alloc= &data->alloc;
    init_alloc_root(alloc,8192,0);	/* Assume rowlength < 8192 */
    alloc->min_malloc=sizeof(MYSQL_ROWS);
    data->rows=0;
    data->fields=field_count;
    data->prev_ptr= &data->data;
    thd->data= data;
  }

  data->rows++;
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
689 690 691 692 693 694 695
  if (!(cur= (MYSQL_ROWS *)alloc_root(alloc, sizeof(MYSQL_ROWS)+(field_count + 1) * sizeof(char *))))
  {
    my_error(ER_OUT_OF_RESOURCES,MYF(0));
    DBUG_VOID_RETURN;
  }
  cur->data= (MYSQL_ROW)(((char *)cur) + sizeof(MYSQL_ROWS));

hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
696 697
  *data->prev_ptr= cur;
  data->prev_ptr= &cur->next;
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
698
  next_field=cur->data;
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
699 700
  next_mysql_field= thd->mysql->fields;
err:
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
701 702 703
  DBUG_VOID_RETURN;
}

hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
704 705 706 707 708 709 710
bool Protocol_simple::store_null()
{
  *(next_field++)= NULL;
  ++next_mysql_field;
  return false;
}

hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
711 712
bool Protocol::net_store_data(const char *from, uint length)
{
hf@deer.(none)'s avatar
hf@deer.(none) committed
713
  char *field_buf;
hf@deer.(none)'s avatar
hf@deer.(none) committed
714 715 716
  if (!thd->mysql)            // bootstrap file handling
    return false;

hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
717
  if (!(field_buf=alloc_root(alloc, length + sizeof(uint) + 1)))
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
718
    return true;
hf@deer.(none)'s avatar
hf@deer.(none) committed
719 720
  *(uint *)field_buf= length;
  *next_field= field_buf + sizeof(uint);
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
721
  memcpy(*next_field, from, length);
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
722
  (*next_field)[length]= 0;
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
723 724 725 726 727 728 729
  if (next_mysql_field->max_length < length)
    next_mysql_field->max_length=length;
  ++next_field;
  ++next_mysql_field;
  return false;
}

730
#if 0
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
731 732 733 734 735 736 737 738 739 740
/* The same as Protocol::net_store_data but does the converstion
*/
bool Protocol::convert_str(const char *from, uint length)
{
  if (!(*next_field=alloc_root(alloc, length + 1)))
    return true;
  convert->store_dest(*next_field, from, length);
  (*next_field)[length]= 0;
  if (next_mysql_field->max_length < length)
    next_mysql_field->max_length=length;
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
741
  ++next_field;
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
742 743
  ++next_mysql_field;

hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
744 745
  return false;
}
746
#endif
hf@deer.(none)'s avatar
hf@deer.(none) committed
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763

bool setup_params_data(st_prep_stmt *stmt)
{                                       
  THD *thd= stmt->thd;
  List<Item> &params= thd->lex.param_list;
  List_iterator<Item> param_iterator(params);
  Item_param *param;
  ulong param_no= 0;
  MYSQL_BIND *client_param= thd->client_params;

  DBUG_ENTER("setup_params_data");

  for (;(param= (Item_param *)param_iterator++); client_param++)
  {       
    setup_param_functions(param, client_param->buffer_type);
    if (!param->long_data_supplied)
    {
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
764
      if (*client_param->is_null)
hf@deer.(none)'s avatar
hf@deer.(none) committed
765 766 767 768 769
        param->maybe_null= param->null_value= 1;
      else
      {
	uchar *buff= (uchar*)client_param->buffer;
        param->maybe_null= param->null_value= 0;
hf@deer.(none)'s avatar
hf@deer.(none) committed
770 771 772 773
        param->setup_param_func(param,&buff, 
				client_param->length ? 
				*client_param->length : 
				client_param->buffer_length);
hf@deer.(none)'s avatar
hf@deer.(none) committed
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
      }
    }
    param_no++;
  }
  DBUG_RETURN(0);
}

bool setup_params_data_withlog(st_prep_stmt *stmt)
{                                       
  THD *thd= stmt->thd;
  List<Item> &params= thd->lex.param_list;
  List_iterator<Item> param_iterator(params);
  Item_param *param;
  MYSQL_BIND *client_param= thd->client_params;

  DBUG_ENTER("setup_params_data");

  String str, *res, *query= new String(stmt->query->alloced_length());  
  query->copy(*stmt->query);
  
  ulong param_no= 0;  
  uint32 length= 0;

  for (;(param= (Item_param *)param_iterator++); client_param++)
  {       
    setup_param_functions(param, client_param->buffer_type);
    if (param->long_data_supplied)
      res= param->query_val_str(&str);       
    
    else
    {
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
805
      if (*client_param->is_null)
hf@deer.(none)'s avatar
hf@deer.(none) committed
806 807
      {
        param->maybe_null= param->null_value= 1;
hf@deer.(none)'s avatar
hf@deer.(none) committed
808
        res= &my_null_string;
hf@deer.(none)'s avatar
hf@deer.(none) committed
809 810 811 812 813
      }
      else
      {
	uchar *buff= (uchar*)client_param->buffer;
        param->maybe_null= param->null_value= 0;
hf@deer.(none)'s avatar
hf@deer.(none) committed
814 815 816 817
        param->setup_param_func(param,&buff,
				client_param->length ? 
				*client_param->length : 
				client_param->buffer_length);
hf@deer.(none)'s avatar
hf@deer.(none) committed
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
        res= param->query_val_str(&str);
      }
    }
    if (query->replace(param->pos_in_query+length, 1, *res))
      DBUG_RETURN(1);
    
    length+= res->length()-1;
    param_no++;
  }
  
  if (alloc_query(stmt->thd, (char *)query->ptr(), query->length()+1))
    DBUG_RETURN(1);
  
  query->free();
  DBUG_RETURN(0);
}