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