dict0crea.c 35 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/******************************************************
Database object creation

(c) 1996 Innobase Oy

Created 1/8/1996 Heikki Tuuri
*******************************************************/

#include "dict0crea.h"

#ifdef UNIV_NONINL
#include "dict0crea.ic"
#endif

#include "btr0pcur.h"
#include "btr0btr.h"
#include "page0page.h"
#include "mach0data.h"
#include "dict0boot.h"
20
#include "dict0dict.h"
21 22
#include "que0que.h"
#include "row0ins.h"
23
#include "row0mysql.h"
24
#include "pars0pars.h"
25 26
#include "trx0roll.h"
#include "usr0sess.h"
27 28 29 30 31 32 33 34

/*********************************************************************
Based on a table object, this function builds the entry to be inserted
in the SYS_TABLES system table. */
static
dtuple_t*
dict_create_sys_tables_tuple(
/*=========================*/
unknown's avatar
unknown committed
35
				/* out: the tuple which should be inserted */
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
	dict_table_t*	table, 	/* in: table */
	mem_heap_t*	heap)	/* in: memory heap from which the memory for
				the built tuple is allocated */
{
	dict_table_t*	sys_tables;
	dtuple_t*	entry;
	dfield_t*	dfield;
	byte*		ptr;

	ut_ad(table && heap);

	sys_tables = dict_sys->sys_tables;
	
	entry = dtuple_create(heap, 8 + DATA_N_SYS_COLS);

	/* 0: NAME -----------------------------*/
	dfield = dtuple_get_nth_field(entry, 0);

	dfield_set_data(dfield, table->name, ut_strlen(table->name));
	/* 3: ID -------------------------------*/
	dfield = dtuple_get_nth_field(entry, 1);

	ptr = mem_heap_alloc(heap, 8);
	mach_write_to_8(ptr, table->id);

	dfield_set_data(dfield, ptr, 8);
	/* 4: N_COLS ---------------------------*/
	dfield = dtuple_get_nth_field(entry, 2);

	ptr = mem_heap_alloc(heap, 4);
	mach_write_to_4(ptr, table->n_def);

	dfield_set_data(dfield, ptr, 4);
	/* 5: TYPE -----------------------------*/
	dfield = dtuple_get_nth_field(entry, 3);

	ptr = mem_heap_alloc(heap, 4);
	mach_write_to_4(ptr, table->type);

	dfield_set_data(dfield, ptr, 4);
	/* 6: MIX_ID ---------------------------*/
	dfield = dtuple_get_nth_field(entry, 4);

	ptr = mem_heap_alloc(heap, 8);
	mach_write_to_8(ptr, table->mix_id);

	dfield_set_data(dfield, ptr, 8);
	/* 7: MIX_LEN --------------------------*/
	dfield = dtuple_get_nth_field(entry, 5);

	ptr = mem_heap_alloc(heap, 4);
unknown's avatar
unknown committed
87 88
	mach_write_to_4(ptr, (table->mix_len & 0x7fffffff) |
			((ulint) table->comp << 31));
89 90 91 92 93 94 95 96

	dfield_set_data(dfield, ptr, 4);
	/* 8: CLUSTER_NAME ---------------------*/
	dfield = dtuple_get_nth_field(entry, 6);

 	if (table->type == DICT_TABLE_CLUSTER_MEMBER) {
		dfield_set_data(dfield, table->cluster_name,
				ut_strlen(table->cluster_name));
97
		ut_error; /* Oracle-style clusters are not supported yet */
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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
	} else {
		dfield_set_data(dfield, NULL, UNIV_SQL_NULL);
	}
	/* 9: SPACE ----------------------------*/
	dfield = dtuple_get_nth_field(entry, 7);

	ptr = mem_heap_alloc(heap, 4);
	mach_write_to_4(ptr, table->space);

	dfield_set_data(dfield, ptr, 4);
	/*----------------------------------*/

	dict_table_copy_types(entry, sys_tables);
				
	return(entry);
}	

/*********************************************************************
Based on a table object, this function builds the entry to be inserted
in the SYS_COLUMNS system table. */
static
dtuple_t*
dict_create_sys_columns_tuple(
/*==========================*/
				/* out: the tuple which should be inserted */
	dict_table_t*	table, 	/* in: table */
	ulint		i,	/* in: column number */
	mem_heap_t*	heap)	/* in: memory heap from which the memory for
				the built tuple is allocated */
{
	dict_table_t*	sys_columns;
	dtuple_t*	entry;
	dict_col_t*	column;
	dfield_t*	dfield;
	byte*		ptr;

	ut_ad(table && heap);

	column = dict_table_get_nth_col(table, i);

	sys_columns = dict_sys->sys_columns;
	
	entry = dtuple_create(heap, 7 + DATA_N_SYS_COLS);

	/* 0: TABLE_ID -----------------------*/
	dfield = dtuple_get_nth_field(entry, 0);

	ptr = mem_heap_alloc(heap, 8);
	mach_write_to_8(ptr, table->id);

	dfield_set_data(dfield, ptr, 8);
	/* 1: POS ----------------------------*/
	dfield = dtuple_get_nth_field(entry, 1);

	ptr = mem_heap_alloc(heap, 4);
	mach_write_to_4(ptr, i);

	dfield_set_data(dfield, ptr, 4);
	/* 4: NAME ---------------------------*/
	dfield = dtuple_get_nth_field(entry, 2);

	dfield_set_data(dfield, column->name, ut_strlen(column->name));
	/* 5: MTYPE --------------------------*/
	dfield = dtuple_get_nth_field(entry, 3);

	ptr = mem_heap_alloc(heap, 4);
	mach_write_to_4(ptr, (column->type).mtype);

	dfield_set_data(dfield, ptr, 4);
	/* 6: PRTYPE -------------------------*/
	dfield = dtuple_get_nth_field(entry, 4);

	ptr = mem_heap_alloc(heap, 4);
	mach_write_to_4(ptr, (column->type).prtype);

	dfield_set_data(dfield, ptr, 4);
	/* 7: LEN ----------------------------*/
	dfield = dtuple_get_nth_field(entry, 5);

	ptr = mem_heap_alloc(heap, 4);
	mach_write_to_4(ptr, (column->type).len);

	dfield_set_data(dfield, ptr, 4);
	/* 8: PREC ---------------------------*/
	dfield = dtuple_get_nth_field(entry, 6);

	ptr = mem_heap_alloc(heap, 4);
	mach_write_to_4(ptr, (column->type).prec);

	dfield_set_data(dfield, ptr, 4);
	/*---------------------------------*/

	dict_table_copy_types(entry, sys_columns);

	return(entry);
}	

/*******************************************************************
Builds a table definition to insert. */
static
ulint
dict_build_table_def_step(
/*======================*/
				/* out: DB_SUCCESS or error code */
	que_thr_t*	thr,	/* in: query thread */
	tab_node_t*	node)	/* in: table create node */
{
	dict_table_t*	table;
	dict_table_t*	cluster_table;
	dtuple_t*	row;
unknown's avatar
unknown committed
208
	ulint		error;
unknown's avatar
unknown committed
209 210
	const char*	path_or_name;
	ibool		is_path;
unknown's avatar
unknown committed
211
	mtr_t		mtr;
212

213
#ifdef UNIV_SYNC_DEBUG
214
	ut_ad(mutex_own(&(dict_sys->mutex)));
215 216
#endif /* UNIV_SYNC_DEBUG */

217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
	table = node->table;

	table->id = dict_hdr_get_new_id(DICT_HDR_TABLE_ID);

	thr_get_trx(thr)->table_id = table->id;

	if (table->type == DICT_TABLE_CLUSTER_MEMBER) {

		cluster_table = dict_table_get_low(table->cluster_name);

		if (cluster_table == NULL) {
			
			return(DB_CLUSTER_NOT_FOUND);
		}

		/* Inherit space and mix len from the cluster */

		table->space = cluster_table->space;
		table->mix_len = cluster_table->mix_len;
		
		table->mix_id = dict_hdr_get_new_id(DICT_HDR_MIX_ID);
	}

unknown's avatar
unknown committed
240 241 242 243 244 245 246 247
	if (srv_file_per_table) {
		/* We create a new single-table tablespace for the table.
		We initially let it be 4 pages:
		- page 0 is the fsp header and an extent descriptor page,
		- page 1 is an ibuf bitmap page,
		- page 2 is the first inode page,
		- page 3 will contain the root of the clustered index of the
		  table we create here. */
unknown's avatar
unknown committed
248 249
	
		table->space = 0;	/* reset to zero for the call below */
unknown's avatar
unknown committed
250

unknown's avatar
unknown committed
251 252 253 254 255 256 257 258 259 260 261
		if (table->dir_path_of_temp_table) {
			/* We place tables created with CREATE TEMPORARY
			TABLE in the tmp dir of mysqld server */

			path_or_name = table->dir_path_of_temp_table;
			is_path = TRUE;
		} else {
			path_or_name = table->name;
			is_path = FALSE;
		}

unknown's avatar
unknown committed
262
		error = fil_create_new_single_table_tablespace(
unknown's avatar
unknown committed
263
					&(table->space), path_or_name, is_path,
unknown's avatar
unknown committed
264
					FIL_IBD_FILE_INITIAL_SIZE);
unknown's avatar
unknown committed
265 266 267 268 269 270 271
		if (error != DB_SUCCESS) {

			return(error);
		}

		mtr_start(&mtr);

unknown's avatar
unknown committed
272
		fsp_header_init(table->space, FIL_IBD_FILE_INITIAL_SIZE, &mtr);
unknown's avatar
unknown committed
273 274 275 276
		
		mtr_commit(&mtr);
	}

277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
	row = dict_create_sys_tables_tuple(table, node->heap);

	ins_node_set_new_row(node->tab_def, row);

	return(DB_SUCCESS);
}

/*******************************************************************
Builds a column definition to insert. */
static
ulint
dict_build_col_def_step(
/*====================*/
				/* out: DB_SUCCESS */
	tab_node_t*	node)	/* in: table create node */
{
	dtuple_t*	row;

	row = dict_create_sys_columns_tuple(node->table, node->col_no,
								node->heap);
	ins_node_set_new_row(node->col_def, row);
	
	return(DB_SUCCESS);
}

/*********************************************************************
Based on an index object, this function builds the entry to be inserted
in the SYS_INDEXES system table. */
static
dtuple_t*
dict_create_sys_indexes_tuple(
/*==========================*/
				/* out: the tuple which should be inserted */
	dict_index_t*	index, 	/* in: index */
311
	mem_heap_t*	heap)	/* in: memory heap from which the memory for
312 313 314 315 316 317 318 319
				the built tuple is allocated */
{
	dict_table_t*	sys_indexes;
	dict_table_t*	table;
	dtuple_t*	entry;
	dfield_t*	dfield;
	byte*		ptr;

320
#ifdef UNIV_SYNC_DEBUG
321
	ut_ad(mutex_own(&(dict_sys->mutex)));
322
#endif /* UNIV_SYNC_DEBUG */
323 324 325 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 351 352 353 354 355 356 357 358 359 360 361 362 363 364
	ut_ad(index && heap);

	sys_indexes = dict_sys->sys_indexes;

	table = dict_table_get_low(index->table_name);
	
	entry = dtuple_create(heap, 7 + DATA_N_SYS_COLS);

	/* 0: TABLE_ID -----------------------*/
	dfield = dtuple_get_nth_field(entry, 0);

	ptr = mem_heap_alloc(heap, 8);
	mach_write_to_8(ptr, table->id);

	dfield_set_data(dfield, ptr, 8);
	/* 1: ID ----------------------------*/
	dfield = dtuple_get_nth_field(entry, 1);

	ptr = mem_heap_alloc(heap, 8);
	mach_write_to_8(ptr, index->id);

	dfield_set_data(dfield, ptr, 8);
	/* 4: NAME --------------------------*/
	dfield = dtuple_get_nth_field(entry, 2);

	dfield_set_data(dfield, index->name, ut_strlen(index->name));
	/* 5: N_FIELDS ----------------------*/
	dfield = dtuple_get_nth_field(entry, 3);

	ptr = mem_heap_alloc(heap, 4);
	mach_write_to_4(ptr, index->n_fields);

	dfield_set_data(dfield, ptr, 4);
	/* 6: TYPE --------------------------*/
	dfield = dtuple_get_nth_field(entry, 4);

	ptr = mem_heap_alloc(heap, 4);
	mach_write_to_4(ptr, index->type);

	dfield_set_data(dfield, ptr, 4);
	/* 7: SPACE --------------------------*/

365 366 367
#if DICT_SYS_INDEXES_SPACE_NO_FIELD != 7
#error "DICT_SYS_INDEXES_SPACE_NO_FIELD != 7"
#endif
368 369 370 371 372 373 374 375 376

	dfield = dtuple_get_nth_field(entry, 5);

	ptr = mem_heap_alloc(heap, 4);
	mach_write_to_4(ptr, index->space);

	dfield_set_data(dfield, ptr, 4);
	/* 8: PAGE_NO --------------------------*/

377 378 379
#if DICT_SYS_INDEXES_PAGE_NO_FIELD != 8
#error "DICT_SYS_INDEXES_PAGE_NO_FIELD != 8"
#endif
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411

	dfield = dtuple_get_nth_field(entry, 6);

	ptr = mem_heap_alloc(heap, 4);
	mach_write_to_4(ptr, FIL_NULL);

	dfield_set_data(dfield, ptr, 4);
	/*--------------------------------*/

	dict_table_copy_types(entry, sys_indexes);

	return(entry);
}	

/*********************************************************************
Based on an index object, this function builds the entry to be inserted
in the SYS_FIELDS system table. */
static
dtuple_t*
dict_create_sys_fields_tuple(
/*=========================*/
				/* out: the tuple which should be inserted */
	dict_index_t*	index, 	/* in: index */
	ulint		i,	/* in: field number */
	mem_heap_t*	heap)	/* in: memory heap from which the memory for
				the built tuple is allocated */
{
	dict_table_t*	sys_fields;
	dtuple_t*	entry;
	dict_field_t*	field;
	dfield_t*	dfield;
	byte*		ptr;
unknown's avatar
unknown committed
412 413
	ibool		index_contains_column_prefix_field	= FALSE;
	ulint		j;
414 415 416

	ut_ad(index && heap);

unknown's avatar
unknown committed
417 418 419 420 421 422
	for (j = 0; j < index->n_fields; j++) {
	        if (dict_index_get_nth_field(index, j)->prefix_len > 0) {
	                index_contains_column_prefix_field = TRUE;	   
		}
	}

423 424 425 426 427 428 429 430 431 432 433 434 435
	field = dict_index_get_nth_field(index, i);

	sys_fields = dict_sys->sys_fields;
	
	entry = dtuple_create(heap, 3 + DATA_N_SYS_COLS);

	/* 0: INDEX_ID -----------------------*/
	dfield = dtuple_get_nth_field(entry, 0);

	ptr = mem_heap_alloc(heap, 8);
	mach_write_to_8(ptr, index->id);

	dfield_set_data(dfield, ptr, 8);
unknown's avatar
unknown committed
436 437
	/* 1: POS + PREFIX LENGTH ----------------------------*/

438 439 440
	dfield = dtuple_get_nth_field(entry, 1);

	ptr = mem_heap_alloc(heap, 4);
unknown's avatar
unknown committed
441 442 443 444 445 446 447 448 449 450 451 452 453 454
	
	if (index_contains_column_prefix_field) {
		/* If there are column prefix fields in the index, then
		we store the number of the field to the 2 HIGH bytes
		and the prefix length to the 2 low bytes, */

	        mach_write_to_4(ptr, (i << 16) + field->prefix_len);
	} else {
	        /* Else we store the number of the field to the 2 LOW bytes.
		This is to keep the storage format compatible with
		InnoDB versions < 4.0.14. */
	  
	        mach_write_to_4(ptr, i);
	}
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469

	dfield_set_data(dfield, ptr, 4);
	/* 4: COL_NAME -------------------------*/
	dfield = dtuple_get_nth_field(entry, 2);

	dfield_set_data(dfield, field->name,
				ut_strlen(field->name));
	/*---------------------------------*/

	dict_table_copy_types(entry, sys_fields);
	
	return(entry);
}	

/*********************************************************************
unknown's avatar
unknown committed
470 471
Creates the tuple with which the index entry is searched for writing the index
tree root page number, if such a tree is created. */
472 473 474 475 476 477 478 479 480 481 482 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
static
dtuple_t*
dict_create_search_tuple(
/*=====================*/
				/* out: the tuple for search */
	dtuple_t*	tuple,	/* in: the tuple inserted in the SYS_INDEXES
				table */
	mem_heap_t*	heap)	/* in: memory heap from which the memory for
				the built tuple is allocated */
{
	dtuple_t*	search_tuple;
	dfield_t*	field1;
	dfield_t*	field2;

	ut_ad(tuple && heap);

	search_tuple = dtuple_create(heap, 2);

	field1 = dtuple_get_nth_field(tuple, 0);	
	field2 = dtuple_get_nth_field(search_tuple, 0);	

	dfield_copy(field2, field1);

	field1 = dtuple_get_nth_field(tuple, 1);	
	field2 = dtuple_get_nth_field(search_tuple, 1);	

	dfield_copy(field2, field1);

	ut_ad(dtuple_validate(search_tuple));

	return(search_tuple);
}

/*******************************************************************
Builds an index definition row to insert. */
static
ulint
dict_build_index_def_step(
/*======================*/
				/* out: DB_SUCCESS or error code */
	que_thr_t*	thr,	/* in: query thread */
	ind_node_t*	node)	/* in: index create node */
{
	dict_table_t*	table;
	dict_index_t*	index;
	dtuple_t*	row;
518
	trx_t*		trx;
519

520
#ifdef UNIV_SYNC_DEBUG
521
	ut_ad(mutex_own(&(dict_sys->mutex)));
522
#endif /* UNIV_SYNC_DEBUG */
523

524 525
	trx = thr_get_trx(thr);

526 527 528 529 530 531 532 533
	index = node->index;

	table = dict_table_get_low(index->table_name);

	if (table == NULL) {
		return(DB_TABLE_NOT_FOUND);
	}

534
	trx->table_id = table->id;
535 536 537 538 539 540 541 542

	node->table = table;

	ut_ad((UT_LIST_GET_LEN(table->indexes) > 0)
	      || (index->type & DICT_CLUSTERED));
	
	index->id = dict_hdr_get_new_id(DICT_HDR_INDEX_ID);

unknown's avatar
unknown committed
543 544 545 546
	/* Inherit the space id from the table; we store all indexes of a
	table in the same tablespace */

	index->space = table->space;
547
	node->page_no = FIL_NULL;
548
	row = dict_create_sys_indexes_tuple(index, node->heap);
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
	node->ind_row = row;

	ins_node_set_new_row(node->ind_def, row);

	return(DB_SUCCESS);
}

/*******************************************************************
Builds a field definition row to insert. */
static
ulint
dict_build_field_def_step(
/*======================*/
				/* out: DB_SUCCESS */
	ind_node_t*	node)	/* in: index create node */
{
	dict_index_t*	index;
	dtuple_t*	row;

	index = node->index;
	
	row = dict_create_sys_fields_tuple(index, node->field_no, node->heap);

	ins_node_set_new_row(node->field_def, row);

	return(DB_SUCCESS);
}

/*******************************************************************
Creates an index tree for the index if it is not a member of a cluster. */
static
ulint
dict_create_index_tree_step(
/*========================*/
				/* out: DB_SUCCESS or DB_OUT_OF_FILE_SPACE */
	ind_node_t*	node)	/* in: index create node */
{
	dict_index_t*	index;
	dict_table_t*	sys_indexes;
	dict_table_t*	table;
	dtuple_t*	search_tuple;
	btr_pcur_t	pcur;
	mtr_t		mtr;
592 593

#ifdef UNIV_SYNC_DEBUG
594
	ut_ad(mutex_own(&(dict_sys->mutex)));
595
#endif /* UNIV_SYNC_DEBUG */
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624

	index = node->index;	
	table = node->table;

	sys_indexes = dict_sys->sys_indexes;

	if (index->type & DICT_CLUSTERED
			&& table->type == DICT_TABLE_CLUSTER_MEMBER) {

		/* Do not create a new index tree: entries are put to the
		cluster tree */

		return(DB_SUCCESS);
	}

	/* Run a mini-transaction in which the index tree is allocated for
	the index and its root address is written to the index entry in
	sys_indexes */

	mtr_start(&mtr);

	search_tuple = dict_create_search_tuple(node->ind_row, node->heap);
			    
	btr_pcur_open(UT_LIST_GET_FIRST(sys_indexes->indexes),
				search_tuple, PAGE_CUR_L, BTR_MODIFY_LEAF,
				&pcur, &mtr);

	btr_pcur_move_to_next_user_rec(&pcur, &mtr);

625
	node->page_no = btr_create(index->type, index->space, index->id,
unknown's avatar
unknown committed
626
							table->comp, &mtr);
unknown's avatar
unknown committed
627 628 629
	/* printf("Created a new index tree in space %lu root page %lu\n",
					index->space, index->page_no); */

630 631
	page_rec_write_index_page_no(btr_pcur_get_rec(&pcur),
					DICT_SYS_INDEXES_PAGE_NO_FIELD,
632
					node->page_no, &mtr);
633 634 635
	btr_pcur_close(&pcur);
	mtr_commit(&mtr);

636
	if (node->page_no == FIL_NULL) {
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658

		return(DB_OUT_OF_FILE_SPACE);
	}

	return(DB_SUCCESS);
}

/***********************************************************************
Drops the index tree associated with a row in SYS_INDEXES table. */

void
dict_drop_index_tree(
/*=================*/
	rec_t*	rec,	/* in: record in the clustered index of SYS_INDEXES
			table */
	mtr_t*	mtr)	/* in: mtr having the latch on the record page */
{
	ulint	root_page_no;
	ulint	space;
	byte*	ptr;
	ulint	len;
	
659
#ifdef UNIV_SYNC_DEBUG
660
	ut_ad(mutex_own(&(dict_sys->mutex)));
661
#endif /* UNIV_SYNC_DEBUG */
unknown's avatar
unknown committed
662 663 664

	ut_a(!dict_sys->sys_indexes->comp);
	ptr = rec_get_nth_field_old(rec, DICT_SYS_INDEXES_PAGE_NO_FIELD, &len);
665 666 667 668 669 670 671 672 673 674 675

	ut_ad(len == 4);
	
	root_page_no = mtr_read_ulint(ptr, MLOG_4BYTES, mtr);

	if (root_page_no == FIL_NULL) {
		/* The tree has already been freed */

		return;
	}

unknown's avatar
unknown committed
676 677 678
	ptr = rec_get_nth_field_old(rec,
				DICT_SYS_INDEXES_SPACE_NO_FIELD, &len);

679 680 681
	ut_ad(len == 4);

	space = mtr_read_ulint(ptr, MLOG_4BYTES, mtr);
unknown's avatar
unknown committed
682 683 684 685 686 687 688 689

	if (!fil_tablespace_exists_in_mem(space)) {
		/* It is a single table tablespace and the .ibd file is
		missing: do nothing */

		return;
	}

690 691 692 693 694 695 696 697 698
	/* We free all the pages but the root page first; this operation
	may span several mini-transactions */

	btr_free_but_not_root(space, root_page_no);

	/* Then we free the root page in the same mini-transaction where
	we write FIL_NULL to the appropriate field in the SYS_INDEXES
	record: this mini-transaction marks the B-tree totally freed */
	
unknown's avatar
unknown committed
699 700
	/* printf("Dropping index tree in space %lu root page %lu\n", space,
							 root_page_no); */
701 702
	btr_free_root(space, root_page_no, mtr);

unknown's avatar
unknown committed
703 704
	page_rec_write_index_page_no(rec,
				DICT_SYS_INDEXES_PAGE_NO_FIELD, FIL_NULL, mtr);
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 793
/***********************************************************************
Truncates the index tree associated with a row in SYS_INDEXES table. */

void
dict_truncate_index_tree(
/*=====================*/
	dict_table_t*	table,	/* in: the table the index belongs to */
	rec_t*		rec,	/* in: record in the clustered index of
				SYS_INDEXES table */
	mtr_t*		mtr)	/* in: mtr having the latch
				on the record page */
{
	ulint		root_page_no;
	ulint		space;
	ulint		type;
	dulint		index_id;
	byte*		ptr;
	ulint		len;
	ibool		comp;
	dict_index_t*	index;

#ifdef UNIV_SYNC_DEBUG
	ut_ad(mutex_own(&(dict_sys->mutex)));
#endif /* UNIV_SYNC_DEBUG */

	ut_a(!dict_sys->sys_indexes->comp);
	ptr = rec_get_nth_field_old(rec, DICT_SYS_INDEXES_PAGE_NO_FIELD, &len);

	ut_ad(len == 4);

	root_page_no = mtr_read_ulint(ptr, MLOG_4BYTES, mtr);

	if (root_page_no == FIL_NULL) {
		/* The tree has been freed. */

		return;
	}

	ptr = rec_get_nth_field_old(rec,
				DICT_SYS_INDEXES_SPACE_NO_FIELD, &len);

	ut_ad(len == 4);

	space = mtr_read_ulint(ptr, MLOG_4BYTES, mtr);

	if (!fil_tablespace_exists_in_mem(space)) {
		/* It is a single table tablespace and the .ibd file is
		missing: do nothing */

		return;
	}

	ptr = rec_get_nth_field_old(rec,
				DICT_SYS_INDEXES_TYPE_FIELD, &len);
	ut_ad(len == 4);
	type = mach_read_from_4(ptr);

	ptr = rec_get_nth_field_old(rec, 1, &len);
	ut_ad(len == 8);
	index_id = mach_read_from_8(ptr);

	/* We free all the pages but the root page first; this operation
	may span several mini-transactions */

	btr_free_but_not_root(space, root_page_no);

	/* Then we free the root page in the same mini-transaction where
	we create the b-tree and write its new root page number to the
	appropriate field in the SYS_INDEXES record: this mini-transaction
	marks the B-tree totally truncated */

	comp = page_is_comp(btr_page_get(
				space, root_page_no, RW_X_LATCH, mtr));

	btr_free_root(space, root_page_no, mtr);

	/* Find the index corresponding to this SYS_INDEXES record. */
	for (index = UT_LIST_GET_FIRST(table->indexes);
			index;
			index = UT_LIST_GET_NEXT(indexes, index)) {
		if (!ut_dulint_cmp(index->id, index_id)) {
			break;
		}
	}

	root_page_no = btr_create(type, space, index_id, comp, mtr);
	if (index) {
794
		index->tree->page = root_page_no;
795 796 797 798 799 800 801
	}

	page_rec_write_index_page_no(rec,
				DICT_SYS_INDEXES_PAGE_NO_FIELD,
				root_page_no, mtr);
}

802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857
/*************************************************************************
Creates a table create graph. */

tab_node_t*
tab_create_graph_create(
/*====================*/
				/* out, own: table create node */
	dict_table_t*	table,	/* in: table to create, built as a memory data
				structure */
	mem_heap_t*	heap)	/* in: heap where created */
{
	tab_node_t*	node;

	node = mem_heap_alloc(heap, sizeof(tab_node_t));
	
	node->common.type = QUE_NODE_CREATE_TABLE;

	node->table = table;

	node->state = TABLE_BUILD_TABLE_DEF;
	node->heap = mem_heap_create(256);

	node->tab_def = ins_node_create(INS_DIRECT, dict_sys->sys_tables,
									heap); 
	node->tab_def->common.parent = node;
	
	node->col_def = ins_node_create(INS_DIRECT, dict_sys->sys_columns,
									heap);
	node->col_def->common.parent = node;

	node->commit_node = commit_node_create(heap);
	node->commit_node->common.parent = node;

	return(node);
}

/*************************************************************************
Creates an index create graph. */

ind_node_t*
ind_create_graph_create(
/*====================*/
				/* out, own: index create node */
	dict_index_t*	index,	/* in: index to create, built as a memory data
				structure */
	mem_heap_t*	heap)	/* in: heap where created */
{
	ind_node_t*	node;

	node = mem_heap_alloc(heap, sizeof(ind_node_t));

	node->common.type = QUE_NODE_CREATE_INDEX;

	node->index = index;

	node->state = INDEX_BUILD_INDEX_DEF;
858
	node->page_no = FIL_NULL;
859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884
	node->heap = mem_heap_create(256);

	node->ind_def = ins_node_create(INS_DIRECT,
						dict_sys->sys_indexes, heap); 
	node->ind_def->common.parent = node;

	node->field_def = ins_node_create(INS_DIRECT,
						dict_sys->sys_fields, heap);
	node->field_def->common.parent = node;

	node->commit_node = commit_node_create(heap);
	node->commit_node->common.parent = node;

	return(node);
}

/***************************************************************
Creates a table. This is a high-level function used in SQL execution graphs. */

que_thr_t*
dict_create_table_step(
/*===================*/
				/* out: query thread to run next or NULL */
	que_thr_t*	thr)	/* in: query thread */
{
	tab_node_t*	node;
885
	ulint		err	= DB_ERROR;
886 887 888
	trx_t*		trx;

	ut_ad(thr);
889
#ifdef UNIV_SYNC_DEBUG
890
	ut_ad(mutex_own(&(dict_sys->mutex)));
891 892
#endif /* UNIV_SYNC_DEBUG */

893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994
	trx = thr_get_trx(thr);
	
	node = thr->run_node;

	ut_ad(que_node_get_type(node) == QUE_NODE_CREATE_TABLE);

	if (thr->prev_node == que_node_get_parent(node)) {
		node->state = TABLE_BUILD_TABLE_DEF;
	}

	if (node->state == TABLE_BUILD_TABLE_DEF) {

		/* DO THE CHECKS OF THE CONSISTENCY CONSTRAINTS HERE */

		err = dict_build_table_def_step(thr, node);

		if (err != DB_SUCCESS) {

			goto function_exit;
		}
		
		node->state = TABLE_BUILD_COL_DEF;
		node->col_no = 0;

		thr->run_node = node->tab_def;

		return(thr);
	}

	if (node->state == TABLE_BUILD_COL_DEF) {

		if (node->col_no < (node->table)->n_def) {

			err = dict_build_col_def_step(node);

			if (err != DB_SUCCESS) {

				goto function_exit;
			}

			node->col_no++;
		
			thr->run_node = node->col_def;

			return(thr);
		} else {
			node->state = TABLE_COMMIT_WORK;
		}
	}

	if (node->state == TABLE_COMMIT_WORK) {

		/* Table was correctly defined: do NOT commit the transaction
		(CREATE TABLE does NOT do an implicit commit of the current
		transaction) */
		
		node->state = TABLE_ADD_TO_CACHE;

		/* thr->run_node = node->commit_node;

		return(thr); */
	}

	if (node->state == TABLE_ADD_TO_CACHE) {

		dict_table_add_to_cache(node->table);

		err = DB_SUCCESS;
	}

function_exit:
	trx->error_state = err;

	if (err == DB_SUCCESS) {
		/* Ok: do nothing */

	} else if (err == DB_LOCK_WAIT) {

		return(NULL);
	} else {
		/* SQL error detected */

		return(NULL);
	}

	thr->run_node = que_node_get_parent(node);

	return(thr);
} 

/***************************************************************
Creates an index. This is a high-level function used in SQL execution
graphs. */

que_thr_t*
dict_create_index_step(
/*===================*/
				/* out: query thread to run next or NULL */
	que_thr_t*	thr)	/* in: query thread */
{
	ind_node_t*	node;
	ibool		success;
995
	ulint		err	= DB_ERROR;
996 997 998
	trx_t*		trx;

	ut_ad(thr);
999
#ifdef UNIV_SYNC_DEBUG
1000
	ut_ad(mutex_own(&(dict_sys->mutex)));
1001
#endif /* UNIV_SYNC_DEBUG */
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052

	trx = thr_get_trx(thr);
	
	node = thr->run_node;

	ut_ad(que_node_get_type(node) == QUE_NODE_CREATE_INDEX);

	if (thr->prev_node == que_node_get_parent(node)) {
		node->state = INDEX_BUILD_INDEX_DEF;
	}

	if (node->state == INDEX_BUILD_INDEX_DEF) {
		/* DO THE CHECKS OF THE CONSISTENCY CONSTRAINTS HERE */
		err = dict_build_index_def_step(thr, node);

		if (err != DB_SUCCESS) {

			goto function_exit;
		}
		
		node->state = INDEX_BUILD_FIELD_DEF;
		node->field_no = 0;

		thr->run_node = node->ind_def;

		return(thr);
	}

	if (node->state == INDEX_BUILD_FIELD_DEF) {

		if (node->field_no < (node->index)->n_fields) {

			err = dict_build_field_def_step(node);

			if (err != DB_SUCCESS) {

				goto function_exit;
			}

			node->field_no++;
		
			thr->run_node = node->field_def;

			return(thr);
		} else {
			node->state = INDEX_CREATE_INDEX_TREE;
		}
	}

	if (node->state == INDEX_CREATE_INDEX_TREE) {

1053
		err = dict_create_index_tree_step(node);
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077

		if (err != DB_SUCCESS) {

			goto function_exit;
		}

		node->state = INDEX_COMMIT_WORK;
	}

	if (node->state == INDEX_COMMIT_WORK) {

		/* Index was correctly defined: do NOT commit the transaction
		(CREATE INDEX does NOT currently do an implicit commit of
		the current transaction) */
		
		node->state = INDEX_ADD_TO_CACHE;

		/* thr->run_node = node->commit_node;

		return(thr); */
	}

	if (node->state == INDEX_ADD_TO_CACHE) {

1078 1079
		success = dict_index_add_to_cache(node->table, node->index,
				node->page_no);
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104

		ut_a(success);

		err = DB_SUCCESS;
	}

function_exit:
	trx->error_state = err;

	if (err == DB_SUCCESS) {
		/* Ok: do nothing */

	} else if (err == DB_LOCK_WAIT) {

		return(NULL);
	} else {
		/* SQL error detected */

		return(NULL);
	}

	thr->run_node = que_node_get_parent(node);

	return(thr);
} 
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121

/********************************************************************
Creates the foreign key constraints system tables inside InnoDB
at database creation or database start if they are not found or are
not of the right form. */

ulint
dict_create_or_check_foreign_constraint_tables(void)
/*================================================*/
				/* out: DB_SUCCESS or error code */
{
	dict_table_t*	table1;
	dict_table_t*	table2;
	que_thr_t*	thr;
	que_t*		graph;
	ulint		error;
	trx_t*		trx;
unknown's avatar
unknown committed
1122
	const char*	str;	
1123 1124 1125

	mutex_enter(&(dict_sys->mutex));

unknown's avatar
unknown committed
1126 1127
	table1 = dict_table_get_low("SYS_FOREIGN");
	table2 = dict_table_get_low("SYS_FOREIGN_COLS");
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
	
	if (table1 && table2
            && UT_LIST_GET_LEN(table1->indexes) == 3
            && UT_LIST_GET_LEN(table2->indexes) == 1) {

            	/* Foreign constraint system tables have already been
            	created, and they are ok */

		mutex_exit(&(dict_sys->mutex));

            	return(DB_SUCCESS);
        }

unknown's avatar
unknown committed
1141 1142
	mutex_exit(&(dict_sys->mutex));

1143 1144
	trx = trx_allocate_for_mysql();
	
unknown's avatar
unknown committed
1145
	trx->op_info = "creating foreign key sys tables";
1146

unknown's avatar
unknown committed
1147 1148
	row_mysql_lock_data_dictionary(trx);

1149 1150 1151
	if (table1) {
		fprintf(stderr,
		"InnoDB: dropping incompletely created SYS_FOREIGN table\n");
unknown's avatar
unknown committed
1152
		row_drop_table_for_mysql("SYS_FOREIGN", trx, TRUE);
1153 1154 1155 1156 1157
	}

	if (table2) {
		fprintf(stderr,
	"InnoDB: dropping incompletely created SYS_FOREIGN_COLS table\n");
unknown's avatar
unknown committed
1158
		row_drop_table_for_mysql("SYS_FOREIGN_COLS", trx, TRUE);
1159 1160 1161
	}

	fprintf(stderr,
unknown's avatar
Merge  
unknown committed
1162
		"InnoDB: Creating foreign key constraint system tables\n");
1163 1164 1165 1166 1167

	/* NOTE: in dict_load_foreigns we use the fact that
	there are 2 secondary indexes on SYS_FOREIGN, and they
	are defined just like below */
	
1168 1169 1170 1171 1172 1173
	/* NOTE: when designing InnoDB's foreign key support in 2001, we made
	an error and made the table names and the foreign key id of type
	'CHAR' (internally, really a VARCHAR). We should have made the type
	VARBINARY, like in other InnoDB system tables, to get a clean
	design. */

unknown's avatar
unknown committed
1174
	str =
1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
	"PROCEDURE CREATE_FOREIGN_SYS_TABLES_PROC () IS\n"
	"BEGIN\n"
	"CREATE TABLE\n"
	"SYS_FOREIGN(ID CHAR, FOR_NAME CHAR, REF_NAME CHAR, N_COLS INT);\n"
	"CREATE UNIQUE CLUSTERED INDEX ID_IND ON SYS_FOREIGN (ID);\n"
	"CREATE INDEX FOR_IND ON SYS_FOREIGN (FOR_NAME);\n"
	"CREATE INDEX REF_IND ON SYS_FOREIGN (REF_NAME);\n"
	"CREATE TABLE\n"
  "SYS_FOREIGN_COLS(ID CHAR, POS INT, FOR_COL_NAME CHAR, REF_COL_NAME CHAR);\n"
	"CREATE UNIQUE CLUSTERED INDEX ID_IND ON SYS_FOREIGN_COLS (ID, POS);\n"
	"COMMIT WORK;\n"
	"END;\n";

	graph = pars_sql(str);

	ut_a(graph);

	graph->trx = trx;
	trx->graph = NULL;

	graph->fork_type = QUE_FORK_MYSQL_INTERFACE;

unknown's avatar
unknown committed
1197
	ut_a(thr = que_fork_start_command(graph));
1198 1199 1200 1201 1202 1203

	que_run_threads(thr);

	error = trx->error_state;

	if (error != DB_SUCCESS) {
1204 1205
		fprintf(stderr, "InnoDB: error %lu in creation\n",
			(ulong) error);
unknown's avatar
Merge  
unknown committed
1206
		
1207 1208 1209 1210 1211 1212 1213
		ut_a(error == DB_OUT_OF_FILE_SPACE);

		fprintf(stderr, "InnoDB: creation failed\n");
		fprintf(stderr, "InnoDB: tablespace is full\n");
		fprintf(stderr,
		"InnoDB: dropping incompletely created SYS_FOREIGN tables\n");

unknown's avatar
unknown committed
1214 1215
		row_drop_table_for_mysql("SYS_FOREIGN", trx, TRUE);
		row_drop_table_for_mysql("SYS_FOREIGN_COLS", trx, TRUE);
1216 1217 1218 1219 1220 1221

		error = DB_MUST_GET_MORE_FILE_SPACE;
	}

	que_graph_free(graph);
	
unknown's avatar
unknown committed
1222
	trx->op_info = "";
1223

unknown's avatar
unknown committed
1224 1225
	row_mysql_unlock_data_dictionary(trx);

1226 1227 1228 1229
  	trx_free_for_mysql(trx);

  	if (error == DB_SUCCESS) {
		fprintf(stderr,
unknown's avatar
Merge  
unknown committed
1230
		"InnoDB: Foreign key constraint system tables created\n");
1231 1232 1233 1234 1235 1236
	}

	return(error);
}

/************************************************************************
unknown's avatar
unknown committed
1237 1238 1239 1240 1241 1242
Adds foreign key definitions to data dictionary tables in the database. We
look at table->foreign_list, and also generate names to constraints that were
not named by the user. A generated constraint has a name of the format
databasename/tablename_ibfk_<number>, where the numbers start from 1, and are
given locally for this table, that is, the number is not global, as in the
old format constraints < 4.0.18 it used to be. */
1243 1244 1245 1246 1247

ulint
dict_create_add_foreigns_to_dictionary(
/*===================================*/
				/* out: error code or DB_SUCCESS */
unknown's avatar
unknown committed
1248 1249 1250 1251 1252 1253 1254 1255
	ulint		start_id,/* in: if we are actually doing ALTER TABLE
				ADD CONSTRAINT, we want to generate constraint
				numbers which are bigger than in the table so
				far; we number the constraints from
				start_id + 1 up; start_id should be set to 0 if
				we are creating a new table, or if the table
				so far has no constraints for which the name
				was generated here */
1256 1257 1258 1259 1260 1261
	dict_table_t*	table,	/* in: table */
	trx_t*		trx)	/* in: transaction */
{
	dict_foreign_t*	foreign;
	que_thr_t*	thr;
	que_t*		graph;
unknown's avatar
unknown committed
1262
	ulint		number	= start_id + 1;
1263 1264
	ulint		len;
	ulint		error;
1265
	FILE*		ef	= dict_foreign_err_file;
1266
	ulint		i;
1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
	char*		sql;
	char*		sqlend;
	/* This procedure builds an InnoDB stored procedure which will insert
	the necessary rows into SYS_FOREIGN and SYS_FOREIGN_COLS. */
	static const char str1[] = "PROCEDURE ADD_FOREIGN_DEFS_PROC () IS\n"
	"BEGIN\n"
	"INSERT INTO SYS_FOREIGN VALUES(";
	static const char str2[] = ");\n";
	static const char str3[] =
	"INSERT INTO SYS_FOREIGN_COLS VALUES(";
	static const char str4[] =
	"COMMIT WORK;\n"
	"END;\n";
1280

1281 1282 1283
#ifdef UNIV_SYNC_DEBUG
	ut_ad(mutex_own(&(dict_sys->mutex)));
#endif /* UNIV_SYNC_DEBUG */
1284

unknown's avatar
unknown committed
1285
	if (NULL == dict_table_get_low("SYS_FOREIGN")) {
1286 1287
		fprintf(stderr,
     "InnoDB: table SYS_FOREIGN not found from internal data dictionary\n");
unknown's avatar
unknown committed
1288

1289 1290 1291 1292 1293 1294 1295 1296 1297 1298
		return(DB_ERROR);
	}

	foreign = UT_LIST_GET_FIRST(table->foreign_list);
loop:
	if (foreign == NULL) {

		return(DB_SUCCESS);
	}

unknown's avatar
unknown committed
1299 1300
	if (foreign->id == NULL) {
		/* Generate a new constraint id */
1301 1302
		ulint	namelen	= strlen(table->name);
		char*	id	= mem_heap_alloc(foreign->heap, namelen + 20);
unknown's avatar
unknown committed
1303
		/* no overflow if number < 1e13 */
unknown's avatar
unknown committed
1304
		sprintf(id, "%s_ibfk_%lu", table->name, (ulong) number++);
1305
		foreign->id = id;
unknown's avatar
unknown committed
1306
	}
1307

1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
	len = (sizeof str1) + (sizeof str2) + (sizeof str4) - 3
		+ 9/* ' and , chars */ + 10/* 32-bit integer */
		+ ut_strlenq(foreign->id, '\'') * (foreign->n_fields + 1)
		+ ut_strlenq(table->name, '\'')
		+ ut_strlenq(foreign->referenced_table_name, '\'');

	for (i = 0; i < foreign->n_fields; i++) {
		len += 9/* ' and , chars */ + 10/* 32-bit integer */
			+ (sizeof str3) + (sizeof str2) - 2
			+ ut_strlenq(foreign->foreign_col_names[i], '\'')
			+ ut_strlenq(foreign->referenced_col_names[i], '\'');
	}
unknown's avatar
unknown committed
1320

1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336
	sql = sqlend = mem_alloc(len + 1);

	/* INSERT INTO SYS_FOREIGN VALUES(...); */
	memcpy(sqlend, str1, (sizeof str1) - 1);
	sqlend += (sizeof str1) - 1;
	*sqlend++ = '\'';
	sqlend = ut_strcpyq(sqlend, '\'', foreign->id);
	*sqlend++ = '\'', *sqlend++ = ',', *sqlend++ = '\'';
	sqlend = ut_strcpyq(sqlend, '\'', table->name);
	*sqlend++ = '\'', *sqlend++ = ',', *sqlend++ = '\'';
	sqlend = ut_strcpyq(sqlend, '\'', foreign->referenced_table_name);
	*sqlend++ = '\'', *sqlend++ = ',';
	sqlend += sprintf(sqlend, "%010lu",
			foreign->n_fields + (foreign->type << 24));
	memcpy(sqlend, str2, (sizeof str2) - 1);
	sqlend += (sizeof str2) - 1;
1337 1338

	for (i = 0; i < foreign->n_fields; i++) {
1339 1340 1341 1342 1343 1344
		/* INSERT INTO SYS_FOREIGN_COLS VALUES(...); */
		memcpy(sqlend, str3, (sizeof str3) - 1);
		sqlend += (sizeof str3) - 1;
		*sqlend++ = '\'';
		sqlend = ut_strcpyq(sqlend, '\'', foreign->id);
		*sqlend++ = '\''; *sqlend++ = ',';
unknown's avatar
unknown committed
1345
		sqlend += sprintf(sqlend, "%010lu", (ulong) i);
1346 1347 1348 1349 1350 1351 1352 1353 1354
		*sqlend++ = ','; *sqlend++ = '\'';
		sqlend = ut_strcpyq(sqlend, '\'',
			foreign->foreign_col_names[i]);
		*sqlend++ = '\''; *sqlend++ = ','; *sqlend++ = '\'';
		sqlend = ut_strcpyq(sqlend, '\'',
			foreign->referenced_col_names[i]);
		*sqlend++ = '\'';
		memcpy(sqlend, str2, (sizeof str2) - 1);
		sqlend += (sizeof str2) - 1;
1355 1356
	}

1357 1358
	memcpy(sqlend, str4, sizeof str4);
	sqlend += sizeof str4;
1359

1360 1361 1362
	ut_a(sqlend == sql + len + 1);

	graph = pars_sql(sql);
1363 1364 1365

	ut_a(graph);

1366 1367
	mem_free(sql);

1368 1369 1370 1371 1372
	graph->trx = trx;
	trx->graph = NULL;

	graph->fork_type = QUE_FORK_MYSQL_INTERFACE;

unknown's avatar
unknown committed
1373
	ut_a(thr = que_fork_start_command(graph));
1374 1375 1376 1377 1378 1379 1380

	que_run_threads(thr);

	error = trx->error_state;

	que_graph_free(graph);

unknown's avatar
unknown committed
1381 1382
	if (error == DB_DUPLICATE_KEY) {
		mutex_enter(&dict_foreign_err_mutex);
1383 1384 1385 1386
		rewind(ef);
		ut_print_timestamp(ef);
		fputs(" Error in foreign key constraint creation for table ",
			ef);
1387
		ut_print_name(ef, trx, table->name);
1388
		fputs(".\nA foreign key constraint of name ", ef);
1389
		ut_print_name(ef, trx, foreign->id);
1390
		fputs("\nalready exists."
1391
			" (Note that internally InnoDB adds 'databasename/'\n"
1392 1393
			"in front of the user-defined constraint name).\n",
			ef);
1394 1395 1396 1397 1398 1399 1400 1401
		fputs("Note that InnoDB's FOREIGN KEY system tables store\n"
		      "constraint names as case-insensitive, with the\n"
		      "MySQL standard latin1_swedish_ci collation. If you\n"
		      "create tables or databases whose names differ only in\n"
		      "the character case, then collisions in constraint\n"
		      "names can occur. Workaround: name your constraints\n"
		      "explicitly with unique names.\n",
			ef);
unknown's avatar
unknown committed
1402 1403 1404 1405 1406 1407

		mutex_exit(&dict_foreign_err_mutex);

		return(error);
	}

1408
	if (error != DB_SUCCESS) {
unknown's avatar
unknown committed
1409
	        fprintf(stderr,
unknown's avatar
unknown committed
1410
			"InnoDB: Foreign key constraint creation failed:\n"
1411
			"InnoDB: internal error number %lu\n", (ulong) error);
unknown's avatar
unknown committed
1412

unknown's avatar
unknown committed
1413
		mutex_enter(&dict_foreign_err_mutex);
1414 1415 1416
		ut_print_timestamp(ef);
		fputs(" Internal error in foreign key constraint creation"
			" for table ", ef);
1417
		ut_print_name(ef, trx, table->name);
1418 1419
		fputs(".\n"
	"See the MySQL .err log in the datadir for more information.\n", ef);
unknown's avatar
unknown committed
1420 1421
		mutex_exit(&dict_foreign_err_mutex);

1422 1423 1424 1425 1426 1427 1428
		return(error);
	}
	
	foreign = UT_LIST_GET_NEXT(foreign_list, foreign);

	goto loop;
}