1 /*
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 1996, 2013 Oracle and/or its affiliates.  All rights reserved.
5  *
6  */
7 
8 /*
9  * The functions in this compilation unit are those related to
10  * building the schema model during the parsing of a SQL source file.
11  * Most of the functions that have name beginning with "sqlite3..."
12  * are invoked by the sqlite parser as it recognizes bits of syntax.
13  */
14 
15 #include <ctype.h>
16 
17 #include "db_sql_codegen.h"
18 
19 DB_SCHEMA the_schema;
20 PARSE_PROGRESS the_parse_progress;
21 int maxbinsz; /* keeps track of the largest binary field */
22 
23 /*
24  * Extract a name from sqlite token structure.  Returns allocated memory.
25  */
26 
27 static char *
name_from_token(t,pParse)28 name_from_token(t, pParse)
29 	Token *t;
30 	Parse *pParse;
31 {
32 	char *s;
33 
34 	if (t == NULL || t->n <= 0) {
35 		sqlite3ErrorMsg(pParse,
36 		    "Extracting name from a null or empty token");
37 		return NULL;
38 	}
39 
40 	s = calloc(1, t->n + 1);
41 	if (s == NULL) {
42 		sqlite3ErrorMsg(pParse, "Malloc failed");
43 		return NULL;
44 	}
45 
46 	memcpy(s, (char*)t->z, t->n);
47 	sqlite3Dequote(s);
48 
49 	return s;
50 }
51 
52 /*
53  * Allocate, populate, and return memory for an ENTITY struct.  If
54  * there is a malloc failure, set an error message in the Parse
55  * object and return NULL.
56  */
57 static ENTITY *
make_entity(name,pParse)58 make_entity(name, pParse)
59 	char *name;
60 	Parse *pParse;
61 {
62 	ENTITY *entity;
63 
64 	entity = calloc(1, sizeof(ENTITY));
65 	if (entity == NULL) {
66 		sqlite3ErrorMsg(pParse, "Malloc failed");
67 		return NULL;
68 	}
69 
70 	entity->name = name;
71 	entity->line_number = line_number;
72 	entity->dbtype = "DB_BTREE";
73 	entity->transactional = the_schema.environment.transactional;
74 	return entity;
75 }
76 
77 /*
78  * Allocate, populate, and return memory for an ATTRIBUTE struct.  If
79  * there is a malloc failure, set an error message in the Parse
80  * object and return NULL.
81  */
82 static ATTRIBUTE *
make_attribute(name,pParse)83 make_attribute(name, pParse)
84 	char *name;
85 	Parse *pParse;
86 {
87 	ATTRIBUTE *attribute;
88 
89 	attribute = calloc(1, sizeof(ATTRIBUTE));
90 	if (attribute == NULL) {
91 		sqlite3ErrorMsg(pParse, "Malloc failed");
92 		return NULL;
93 	}
94 
95 	attribute->name = name;
96 	return attribute;
97 }
98 
99 /*
100  * Allocate, populate, and return memory for an ATTR_TYPE struct.  If
101  * there is a malloc failure, set an error message in the Parse
102  * object and return NULL.
103  */
104 static ATTR_TYPE *
make_attrtype(token,ctype,dimension,is_array,is_string,pParse)105 make_attrtype(token, ctype, dimension, is_array, is_string, pParse)
106 	char *token;
107 	char *ctype;
108 	int dimension;
109 	int is_array;
110 	int is_string;
111 	Parse *pParse;
112 {
113 	ATTR_TYPE *type;
114 
115 	type = calloc(1, sizeof(ATTR_TYPE));
116 	if (type == NULL) {
117 		sqlite3ErrorMsg(pParse, "Malloc failed");
118 		return NULL;
119 	}
120 
121 	type->sql_token = token;
122 	type->c_type = ctype;
123 	type->array_dim = dimension;
124 	type->is_array = is_array;
125 	type->is_string = is_string;
126 	return type;
127 }
128 
129 /*
130  * Allocate, populate, and return memory for an DB_INDEX struct.  If
131  * there is a malloc failure, set the error message in the Parse
132  * object and return NULL.
133  */
134 static DB_INDEX *
make_index(name,primary,attribute,pParse)135 make_index(name, primary, attribute, pParse)
136 	char *name;
137 	ENTITY *primary;
138 	ATTRIBUTE *attribute;
139 	Parse *pParse;
140 {
141 	DB_INDEX *idx;
142 
143 	idx = calloc(1, sizeof(DB_INDEX));
144 	if (idx == NULL) {
145 		sqlite3ErrorMsg(pParse, "Malloc failed");
146 		return NULL;
147 	}
148 
149 	idx->name = name;
150 	idx->primary = primary;
151 	idx->attribute = attribute;
152 	idx->line_number = line_number;
153 	idx->dbtype = "DB_BTREE";
154 
155 	return idx;
156 }
157 
158 static void
add_entity(entity)159 add_entity(entity)
160 	ENTITY *entity;
161 {
162 	if (the_schema.entities_tail == NULL)
163 		the_schema.entities_tail = the_schema.entities_head = entity;
164 	else {
165 		the_schema.entities_tail->next = entity;
166 		the_schema.entities_tail = entity;
167 	}
168 }
169 
170 static void
add_index(idx)171 add_index(idx)
172 	DB_INDEX *idx;
173 {
174 	if (the_schema.indexes_tail == NULL)
175 		the_schema.indexes_tail = the_schema.indexes_head = idx;
176 	else {
177 		the_schema.indexes_tail->next = idx;
178 		the_schema.indexes_tail = idx;
179 	}
180 }
181 
182 static void
add_attribute(entity,attr)183 add_attribute(entity, attr)
184 	ENTITY *entity;
185 	ATTRIBUTE * attr;
186 {
187 	if (entity->attributes_tail == NULL) {
188 		entity->attributes_head = entity->attributes_tail = attr;
189 	} else {
190 		entity->attributes_tail->next = attr;
191 		entity->attributes_tail = attr;
192 	}
193 }
194 
195 static ENTITY *
get_current_entity()196 get_current_entity()
197 {
198 	/*
199 	 * The entity under construction is always at the tail of the
200 	 *  entity list
201 	 */
202 	ENTITY *entity = the_schema.entities_tail;
203 	assert(entity);
204 
205 	return entity;
206 }
207 
208 static ATTRIBUTE *
get_current_attribute()209 get_current_attribute()
210 {
211 	ENTITY *entity;
212 	ATTRIBUTE *attr;
213 
214 	/*
215 	 * The attribute under construction is always at the tail of
216 	 * the attribute list belonging to the entity at the tail of
217 	 * the entity list.
218 	 */
219 	entity = get_current_entity();
220 	attr = entity->attributes_tail;
221 	assert(attr);
222 	return attr;
223 }
224 
225 static ENTITY *
get_entity_by_name(sought_name)226 get_entity_by_name(sought_name)
227 char *sought_name;
228 {
229 	ENTITY *e;
230 
231 	for (e = the_schema.entities_head; e; e = e->next)
232 		if (strcasecmp(sought_name, e->name) == 0)
233 			return e;
234 
235 	return NULL;
236 }
237 
238 static ATTRIBUTE *
get_attribute_by_name(in_entity,sought_name)239 get_attribute_by_name(in_entity, sought_name)
240 ENTITY * in_entity;
241 char *sought_name;
242 {
243 	ATTRIBUTE *a;
244 	for (a = in_entity->attributes_head; a; a = a->next)
245 		if (strcasecmp(sought_name, a->name) == 0)
246 			return a;
247 
248 	return NULL;
249 }
250 
251 static DB_INDEX *
get_index_by_name(sought_name)252 get_index_by_name(sought_name)
253 char *sought_name;
254 {
255 	DB_INDEX *idx;
256 
257 	for (idx = the_schema.indexes_head; idx; idx = idx->next)
258 		if (strcasecmp(sought_name, idx->name) == 0)
259 			return idx;
260 
261 	return NULL;
262 }
263 
264 void
sqlite3BeginParse(Parse * pParse,int explainFlag)265 sqlite3BeginParse(Parse *pParse, int explainFlag)
266 {
267 	COMPQUIET(pParse, NULL);
268 	COMPQUIET(explainFlag, 0);
269 	/* no-op */
270 }
271 
272 void
bdb_create_database(Token * name,Parse * pParse)273 bdb_create_database(Token *name, Parse *pParse) {
274 	if (the_schema.environment.name != NULL)
275 		sqlite3ErrorMsg(pParse,
276 		    "Encountered two CREATE DATABASE statements; only one \
277 is allowed");
278 
279 	if ((the_schema.environment.name =
280 	    name_from_token(name, pParse)) == NULL)
281 		return;
282 
283 	the_schema.environment.line_number = line_number;
284 	the_schema.environment.cache_size = 0;
285 	the_schema.environment.transactional = txnflag;
286 
287 	the_parse_progress.last_event = PE_ENVIRONMENT;
288 }
289 
290 void
sqlite3StartTable(pParse,pName1,pName2,isTemp,isView,isVirtual,noErr)291 sqlite3StartTable(pParse, pName1, pName2, isTemp, isView, isVirtual, noErr)
292 	Parse *pParse;   /* Parser context */
293 	Token *pName1;   /* First part of the name of the table or view */
294 	Token *pName2;   /* Second part of the name of the table or view */
295 	int isTemp;      /* True if this is a TEMP table */
296 	int isView;      /* True if this is a VIEW */
297 	int isVirtual;   /* True if this is a VIRTUAL table */
298 	int noErr;       /* Do nothing if table already exists */
299 {
300 	char *name, *name2;
301 	ENTITY *entity;
302 	DB_INDEX *idx;
303 
304 	COMPQUIET(isTemp, 0);
305 	COMPQUIET(isView, 0);
306 	COMPQUIET(isVirtual, 0);
307 	COMPQUIET(noErr, 0);
308 
309 	if (the_schema.environment.name == NULL) {
310 		sqlite3ErrorMsg(pParse,
311 		    "Please specify CREATE DATABASE before CREATE TABLE");
312 		return;
313 	}
314 
315 	if ((name = name_from_token(pName1, pParse)) == NULL)
316 		return;
317 
318 	/*
319 	 * We don't allow the second pName, for now.  Note for future
320 	 * reference: if pName2 is set, then pName1 is the database
321 	 * name, and pname2 is the table name.
322 	 */
323 	name2 = NULL;
324 
325 	if (! (pName2 == NULL || pName2->n == 0) ) {
326 		name2 = name_from_token(pName2, pParse);
327 		sqlite3ErrorMsg(pParse,
328 		    "The table name must be simple: %s.%s",
329 		    name, name2);
330 		goto free_allocated_on_error;
331 	}
332 
333 	if ((entity = get_entity_by_name(name)) != NULL) {
334 		sqlite3ErrorMsg(pParse,
335 		    "Found two declarations of a table named %s, at lines \
336 %d and %d",
337 		    name, entity->line_number, line_number);
338 		goto free_allocated_on_error;
339 	}
340 
341 	if ((idx = get_index_by_name(name)) != NULL) {
342 		sqlite3ErrorMsg(pParse,
343 		    "The entity named %s on line %d has the same name as \
344 the index at line %d.  This is not allowed.",
345 		    name, line_number, idx->line_number);
346 		goto free_allocated_on_error;
347 	}
348 
349 	if ((entity = make_entity(name, pParse)) == NULL)
350 		goto free_allocated_on_error;
351 
352 	the_parse_progress.last_event = PE_ENTITY;
353 	the_parse_progress.last_entity = entity;
354 	add_entity(entity);
355 
356 	return;
357 
358 free_allocated_on_error:
359 	if (name != NULL) free(name);
360 	if (name2 != NULL) free(name2);
361 }
362 
363 void
sqlite3AddColumn(Parse * pParse,Token * pName)364 sqlite3AddColumn(Parse *pParse, Token *pName)
365 {
366 	ENTITY *entity;
367 	ATTRIBUTE *attr;
368 	char *name;
369 
370 	if ((name = name_from_token(pName, pParse)) == NULL)
371 		return;
372 
373 	entity = get_current_entity();
374 
375 	if ((attr = get_attribute_by_name(entity, name)) != NULL) {
376 		sqlite3ErrorMsg(pParse,
377 		    "The table %s contains two columns with the same name %s; \
378 this is not allowed.",
379 		    entity->name, name);
380 		goto free_allocated_on_error;
381 	}
382 
383 	if ((attr = make_attribute(name, pParse)) == NULL)
384 		goto free_allocated_on_error;
385 
386 	the_parse_progress.last_event = PE_ATTRIBUTE;
387 	the_parse_progress.last_attribute = attr;
388 	add_attribute(entity, attr);
389 
390 	return;
391 
392 free_allocated_on_error:
393 	if (name != NULL) free(name);
394 }
395 
396 static void
delete_spaces(char * s)397 delete_spaces(char *s) {
398 	char *p;
399 
400 	p = s;
401 	while (*p) {
402 		if (!isspace(*p))
403 			*s++ = *p;
404 		p++;
405 	}
406 	*s = *p;
407 }
408 
409 /*
410  * This function maps SQL types into the closest equivalent
411  * available using plain C-language types.
412  */
413 static ATTR_TYPE *
map_sql_type(pParse,token)414 map_sql_type(pParse, token)
415 	Parse *pParse;
416 	char *token;
417 {
418 	int dimension, scale, is_array, is_string;
419 	size_t len;
420 	char *p, *q;
421 	ATTR_TYPE *type;
422 	char *t;
423 	char *ctype;
424 
425 	ctype = NULL;
426 	type = NULL;
427 	scale = 0;
428 	len = strlen(token) + 1;
429 
430 	/*
431 	 * Make a copy of the original token, so that we can modify it
432 	 * to remove spaces, and break it up into multiple strings by
433 	 * inserting null characters.
434 	 */
435 	t = malloc(len);
436 	if (t == NULL) {
437 		sqlite3ErrorMsg(pParse, "Malloc failed");
438 		return NULL;
439 	}
440 
441 	memcpy(t, token, len);
442 
443 	dimension = 0;
444 	is_array = 0;
445 	is_string = 0;
446 
447 	delete_spaces(t); /* Remove any spaces that t might contain. */
448 
449 	/*
450 	 * If t contains a parenthesis, then it is a SQL type that has
451 	 * either a dimension, such as VARCHAR(255); or a precision,
452 	 * and possibly a scale, such as NUMBER(15, 2).  We need to
453 	 * extract these values.  Sqlite's parser does not decompose
454 	 * these tokens for us.
455 	 */
456 
457 	if ((p = strchr(t, '(')) != NULL) {
458 		/*
459 		 * Split the token into two strings by replacing the
460 		 * parenthesis with a null character.  The pointer t
461 		 * is now a simple type name, and p points to the
462 		 * first parenthesized parameter.
463 		 */
464 		*p++ = '\0';
465 
466 		/*
467 		 * There should be a right paren somewhere.
468 		 * Actually, the parser probably guarantees this at least.
469 		 */
470 		if ((q = strchr(p, ')')) == NULL) {
471 			sqlite3ErrorMsg(pParse,
472 			    "Missing right parenthesis in type expression %s\n",
473 			    token);
474 			goto free_allocated_on_error;
475 		}
476 
477 		/*
478 		 * null the paren, to make the parameter list a
479 		 * null-terminated string
480 		 */
481 		*q = '\0';
482 
483 		/*
484 		 * Is there a comma in the parameter list? If so,
485 		 * we'll isolate the first parameter
486 		 */
487 		if ((q = strchr(p, ',')) != NULL)
488 			*q++ = '\0';  /* q now points to the second param */
489 
490 		dimension = atoi(p);
491 
492 		if (dimension == 0) {
493 			sqlite3ErrorMsg(pParse,
494 			    "Non-numeric or zero size parameter in type \
495 expression %s\n",
496 			    token);
497 			goto free_allocated_on_error;
498 		}
499 		/* If there was a second parameter, parse it */
500 		scale = 0;
501 		if (q != NULL && *q != '\0') {
502 			if (strchr(q, ',') == NULL && isdigit(*q)) {
503 				scale = atoi(q);
504 			} else {
505 				sqlite3ErrorMsg(pParse,
506 				    "Unexpected value for second parameter in \
507 type expression %s\n",
508 				    token);
509 				goto free_allocated_on_error;
510 			}
511 		}
512 	}
513 
514 	/* OK, now the simple mapping. */
515 
516 	q = t;
517 	while (*q) {
518 		*q = tolower(*q);
519 		q++;
520 	}
521 
522 	if (strcmp(t, "bin") == 0) {
523 		ctype = "char";
524 		is_array = 1;
525 	} else if (strcmp(t, "varbin") == 0) {
526 		ctype = "char";
527 		is_array = 1;
528 	} else if (strcmp(t, "char") == 0) {
529 		ctype = "char";
530 		is_array = 1;
531 		is_string = 1;
532 	} else if (strcmp(t, "varchar") == 0) {
533 		ctype = "char";
534 		is_array = 1;
535 		is_string = 1;
536 	} else if (strcmp(t, "varchar2") == 0) {
537 		ctype = "char";
538 		is_array = 1;
539 		is_string = 1;
540 	} else if (strcmp(t, "bit") == 0)
541 		ctype = "char";
542 	else if (strcmp(t, "tinyint") == 0)
543 		ctype = "char";
544 	else if (strcmp(t, "smallint") == 0)
545 		ctype = "short";
546 	else if (strcmp(t, "integer") == 0)
547 		ctype = "int";
548 	else if (strcmp(t, "int") == 0)
549 		ctype = "int";
550 	else if (strcmp(t, "bigint") == 0)
551 		ctype = "long";
552 	else if (strcmp(t, "real") == 0)
553 		ctype = "float";
554 	else if (strcmp(t, "double") == 0)
555 		ctype = "double";
556 	else if (strcmp(t, "float") == 0)
557 		ctype = "double";
558 	else if (strcmp(t, "decimal") == 0)
559 		ctype = "double";
560 	else if (strcmp(t, "numeric") == 0)
561 		ctype = "double";
562 	else if (strcmp(t, "number") == 0 ) {
563 		/*
564 		 * Oracle Number type: if scale is zero, it's an integer.
565 		 * Otherwise, call it a floating point
566 		 */
567 		if (scale == 0) {
568 			/* A 10 digit decimal needs a long */
569 			if (dimension < 9)
570 				ctype = "int";
571 			else
572 				ctype = "long";
573 		} else {
574 			if (dimension < 7 )
575 				ctype = "float";
576 			else
577 				ctype = "double";
578 		}
579 	}
580 	else {
581 		sqlite3ErrorMsg(pParse,
582 		    "Unsupported type %s\n",
583 		    token);
584 		goto free_allocated_on_error;
585 	}
586 
587 	if (is_array) {
588 		if (dimension < 1) {
589 			sqlite3ErrorMsg(pParse,
590 			    "Zero dimension not allowed for %s",
591 			    token);
592 			goto free_allocated_on_error;
593 		}
594 		if ((!is_string) && dimension > maxbinsz)
595 			maxbinsz = dimension;
596 	}
597 
598 	if (is_string && dimension < 2)
599 		fprintf(stderr,
600 		    "Warning: dimension of string \"%s %s\" \
601 is too small to hold a null-terminated string.",
602 		    get_current_attribute()->name, token);
603 
604 	type = make_attrtype(token, ctype, dimension, is_array,
605 	    is_string, pParse);
606 
607 free_allocated_on_error:
608 	free(t);
609 
610 	/* Type will be NULL unless make_attrtype was called, and succeeded. */
611 	return (type);
612 }
613 
614 void
sqlite3AddColumnType(pParse,pType)615 sqlite3AddColumnType(pParse, pType)
616 	Parse *pParse;
617 	Token *pType;
618 {
619 	char *type;
620 	ATTRIBUTE *attr;
621 
622 	if ((type = name_from_token(pType, pParse)) == NULL)
623 		return;
624 
625 	attr = get_current_attribute();
626 
627 	attr->type = map_sql_type(pParse, type);
628 }
629 
630 void
sqlite3AddPrimaryKey(pParse,pList,onError,autoInc,sortOrder)631 sqlite3AddPrimaryKey(pParse, pList, onError, autoInc, sortOrder)
632 	Parse *pParse;    /* Parsing context */
633 	ExprList *pList;  /* List of field names to be indexed */
634 	int onError;      /* What to do with a uniqueness conflict */
635 	int autoInc;      /* True if the AUTOINCREMENT keyword is present */
636 	int sortOrder;    /* SQLITE_SO_ASC or SQLITE_SO_DESC */
637 {
638 	ENTITY *entity;
639 	ATTRIBUTE *attr;
640 
641 	COMPQUIET(pParse, NULL);
642 	COMPQUIET(pList, NULL);
643 	COMPQUIET(onError, 0);
644 	COMPQUIET(autoInc, 0);
645 	COMPQUIET(sortOrder, 0);
646 
647 	entity = get_current_entity();
648 	attr = get_current_attribute();
649 	entity->primary_key = attr;
650 }
651 
652 /*
653  * Add a new element to the end of an expression list.  If pList is
654  * initially NULL, then create a new expression list.
655  */
656 ExprList *
sqlite3ExprListAppend(pParse,pList,pExpr,pName)657 sqlite3ExprListAppend(pParse, pList, pExpr, pName)
658 	Parse *pParse;          /* Parsing context */
659 	ExprList *pList;        /* List to which to append. Might be NULL */
660 	Expr *pExpr;            /* Expression to be appended */
661 	Token *pName;           /* AS keyword for the expression */
662 {
663 	if ( pList == NULL ) {
664 		pList = calloc(1, sizeof(ExprList));
665 		if (pList == NULL) {
666 			sqlite3ErrorMsg(pParse, "Malloc failed");
667 			return NULL;
668 		}
669 	}
670 	if (pList->nAlloc <= pList->nExpr) {
671 		struct ExprList_item *a;
672 		int n = pList->nAlloc*2 + 4;
673 		a = realloc(pList->a, n * sizeof(pList->a[0]));
674 		if ( a == NULL ) {
675 			sqlite3ErrorMsg(pParse, "Malloc failed");
676 			return NULL;
677 		}
678 		pList->a = a;
679 		pList->nAlloc = n;
680 	}
681 
682 	if ( pExpr || pName ) {
683 		struct ExprList_item *pItem = &pList->a[pList->nExpr++];
684 		memset(pItem, 0, sizeof(*pItem));
685 		if ((pItem->zName = name_from_token(pName, pParse)) == NULL) {
686 			pList->nExpr --; /* undo allocation of pItem */
687 			return pList;
688 		}
689 		pItem->pExpr = pExpr;
690 	}
691 	return pList;
692 }
693 
694 void
sqlite3ExprListCheckLength(pParse,pElist,zObject)695 sqlite3ExprListCheckLength(pParse, pElist, zObject)
696 	Parse *pParse;
697 	ExprList *pElist;
698 	const char *zObject;
699 {
700 	COMPQUIET(pParse, NULL);
701 	COMPQUIET(pElist, NULL);
702 	COMPQUIET(zObject, NULL);
703 	/* no-op */
704 }
705 
706 /*
707  * Append a new table name to the given SrcList.  Create a new SrcList
708  * if need be.  If pList is initially NULL, then create a new
709  * src list.
710  *
711  * We don't have a Parse object here, so we can't use name_from_token,
712  * or report an error via the usual Parse.rc mechanism.  Just emit a
713  * message on stderr if there is a problem.  Returning NULL from this
714  * function will cause the parser to fail, so any message we print
715  * here will be followed by the usual syntax error message.
716  */
717 
718 SrcList *
sqlite3SrcListAppend(db,pList,pTable,pDatabase)719 sqlite3SrcListAppend(db, pList, pTable, pDatabase)
720 	sqlite3 *db;        /* unused */
721 	SrcList *pList;     /* Append to this SrcList */
722 	Token *pTable;      /* Table to append */
723 	Token *pDatabase;   /* Database of the table, not used at this time */
724 {
725 	struct SrcList_item *pItem;
726 	char *table_name;
727 
728 	COMPQUIET(db, NULL);
729 	COMPQUIET(pDatabase, NULL);
730 
731 	table_name = NULL;
732 
733 	if (pList == NULL) {
734 		pList = calloc(1, sizeof(SrcList));
735 		if (pList == NULL) {
736 			fprintf(stderr, "Malloc failure\n");
737 			return NULL;
738 		}
739 
740 		pList->nAlloc = 1;
741 	}
742 
743 	if (pList->nSrc >= pList->nAlloc) {
744 		SrcList *pNew;
745 		pList->nAlloc *= 2;
746 		pNew = realloc(pList,
747 		    sizeof(*pList) + (pList->nAlloc-1)*sizeof(pList->a[0]));
748 		if (pNew == NULL) {
749 			fprintf(stderr, "Malloc failure\n");
750 			return NULL;
751 		}
752 
753 		pList = pNew;
754 	}
755 
756 	pItem = &pList->a[pList->nSrc];
757 	memset(pItem, 0, sizeof(pList->a[0]));
758 
759 	if (pTable == NULL || pTable->n <= 0) {
760 		fprintf(stderr,
761 		    "Extracting name from a null or empty token\n");
762 		return NULL;
763 	}
764 	table_name = calloc(1, pTable->n + 1);
765 	if (table_name == NULL) {
766 		fprintf(stderr, "Malloc failure\n");
767 		return NULL;
768 	}
769 
770 	memcpy(table_name, (char*)pTable->z, pTable->n);
771 	pItem->zName = table_name;
772 	pItem->zDatabase = NULL;
773 	pItem->iCursor = -1;
774 	pItem->isPopulated = 0;
775 	pList->nSrc++;
776 
777 	return pList;
778 }
779 
780 /*
781  * We parse, and allow, foreign key constraints, but currently do not
782  * use them.
783  */
784 void
sqlite3CreateForeignKey(pParse,pFromCol,pTo,pToCol,flags)785 sqlite3CreateForeignKey(pParse, pFromCol, pTo, pToCol, flags)
786 	Parse *pParse;       /* Parsing context */
787 	ExprList *pFromCol;  /* Columns in that reference other table */
788 	Token *pTo;          /* Name of the other table */
789 	ExprList *pToCol;    /* Columns in the other table */
790 	int flags;           /* Conflict resolution algorithms. */
791 {
792 	char * s;
793 
794 	COMPQUIET(flags, 0);
795 
796 	if (debug) {
797 		if ((s = name_from_token(pTo, pParse)) == NULL)
798 			return;
799 		fprintf(stderr, "Foreign Key Constraint not implemented: \
800 FromTable %s FromCol %s ToTable %s ToCol %s\n",
801 		    get_current_entity()->name, pFromCol->a->zName,
802 		    s, pToCol->a->zName);
803 		free(s);
804 	}
805 }
806 
807 void
sqlite3DeferForeignKey(Parse * pParse,int isDeferred)808 sqlite3DeferForeignKey(Parse *pParse, int isDeferred)
809 {
810 	COMPQUIET(pParse, NULL);
811 	COMPQUIET(isDeferred, 0);
812 	/* no-op */
813 }
814 
815 void
sqlite3CreateIndex(pParse,pName1,pName2,pTblName,pList,onError,pStart,pEnd,sortOrder,ifNotExist)816 sqlite3CreateIndex(pParse, pName1, pName2, pTblName, pList,
817     onError, pStart, pEnd, sortOrder, ifNotExist)
818 	Parse *pParse;     /* All information about this parse */
819 	Token *pName1;     /* First part of index name. May be NULL */
820 	Token *pName2;     /* Second part of index name. May be NULL */
821 	SrcList *pTblName; /* Table to index. Use pParse->pNewTable if 0 */
822 	ExprList *pList;   /* A list of columns to be indexed */
823 	int onError;       /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
824 	Token *pStart;     /* The CREATE token that begins this statement */
825 	Token *pEnd;       /* The ")" that closes the CREATE INDEX statement */
826 	int sortOrder;     /* Sort order of primary key when pList==NULL */
827 	int ifNotExist;    /* Omit error if index already exists */
828 {
829 	ENTITY *e, *extra_entity;
830 	ATTRIBUTE *a;
831 	DB_INDEX *idx;
832 	char *entity_name, *attribute_name, *index_name;
833 
834 	COMPQUIET(pName2, NULL);
835 	COMPQUIET(onError, 0);
836 	COMPQUIET(pStart, NULL);
837 	COMPQUIET(pEnd, NULL);
838 	COMPQUIET(sortOrder, 0);
839 	COMPQUIET(ifNotExist, 0);
840 
841 	entity_name = pTblName->a->zName;
842 	attribute_name = pList->a->zName;
843 	if ((index_name = name_from_token(pName1, pParse)) == NULL)
844 		return;
845 
846 	e = get_entity_by_name(entity_name);
847 	if (e == NULL) {
848 		sqlite3ErrorMsg(pParse, "Index %s names unknown table %s",
849 		    index_name, entity_name);
850 		goto free_allocated_on_error;
851 	}
852 
853 	a = get_attribute_by_name(e, attribute_name);
854 	if (a == NULL) {
855 		sqlite3ErrorMsg(pParse,
856 		    "Index %s names unknown column %s in table %s",
857 		    index_name, attribute_name, entity_name);
858 		goto free_allocated_on_error;;
859 	}
860 
861 	if ((idx = get_index_by_name(index_name)) != NULL) {
862 		sqlite3ErrorMsg(pParse,
863 		    "Found two declarations of an index named %s, \
864 at lines %d and %d",
865 		    index_name, idx->line_number, line_number);
866 		goto free_allocated_on_error;;
867 	}
868 
869 	if ((extra_entity = get_entity_by_name(index_name)) != NULL) {
870 		sqlite3ErrorMsg(pParse,
871 		    "The index named %s on line %d has the same name as the \
872 table at line %d.  This is not allowed.",
873 		    index_name, line_number,
874 		    extra_entity->line_number);
875 		goto free_allocated_on_error;
876 	}
877 
878 	if ((idx = make_index(index_name, e, a, pParse)) == NULL)
879 		goto free_allocated_on_error;
880 
881 	the_parse_progress.last_event = PE_INDEX;
882 	the_parse_progress.last_index = idx;
883 	add_index(idx);
884 
885 	return;
886 
887 free_allocated_on_error:
888 	if (index_name != NULL)
889 		free(index_name);
890 }
891 
sqlite3EndTable(pParse,pCons,pEnd,pSelect)892 void sqlite3EndTable(pParse, pCons, pEnd, pSelect)
893 	Parse *pParse;          /* Parse context */
894 	Token *pCons;           /* The ',' token after the last column defn. */
895 	Token *pEnd;            /* The final ')' token in the CREATE TABLE */
896 	Select *pSelect;        /* Select from a "CREATE ... AS SELECT" */
897 {
898 	COMPQUIET(pParse, NULL);
899 	COMPQUIET(pCons, NULL);
900 	COMPQUIET(pEnd, NULL);
901 	COMPQUIET(pSelect, NULL);
902 	the_parse_progress.last_event = PE_ENTITY;
903 }
904 
905 void
sqlite3FinishCoding(pParse)906 sqlite3FinishCoding(pParse)
907 	Parse *pParse;
908 {
909 	COMPQUIET(pParse, NULL);
910 	/* no-op */
911 }
912