summaryrefslogtreecommitdiff
path: root/util/db_sql_codegen/buildpt.c
blob: 7cfeaf81daec9d6764d0841f919be1a708b32613 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
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
311
312
313
314
315
316
317
318
319
320
321
322
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
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
592
593
594
595
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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
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
858
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
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
/*
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 1996, 2015 Oracle and/or its affiliates.  All rights reserved.
 *
 */

/*
 * The functions in this compilation unit are those related to
 * building the schema model during the parsing of a SQL source file.
 * Most of the functions that have name beginning with "sqlite3..."
 * are invoked by the sqlite parser as it recognizes bits of syntax.
 */

#include <ctype.h>

#include "db_sql_codegen.h"

DB_SCHEMA the_schema;
PARSE_PROGRESS the_parse_progress;
int maxbinsz; /* keeps track of the largest binary field */

/*
 * Extract a name from sqlite token structure.  Returns allocated memory.
 */

static char *
name_from_token(t, pParse)
	Token *t;
	Parse *pParse;
{
	char *s;

	if (t == NULL || t->n <= 0) {
		sqlite3ErrorMsg(pParse,
		    "Extracting name from a null or empty token");
		return NULL;
	}

	s = calloc(1, t->n + 1);
	if (s == NULL) {
		sqlite3ErrorMsg(pParse, "Malloc failed");
		return NULL;
	}

	memcpy(s, (char*)t->z, t->n);
	sqlite3Dequote(s);

	return s;
}

/*
 * Allocate, populate, and return memory for an ENTITY struct.  If
 * there is a malloc failure, set an error message in the Parse
 * object and return NULL.
 */
static ENTITY *
make_entity(name, pParse)
	char *name;
	Parse *pParse;
{
	ENTITY *entity;

	entity = calloc(1, sizeof(ENTITY));
	if (entity == NULL) {
		sqlite3ErrorMsg(pParse, "Malloc failed");
		return NULL;
	}

	entity->name = name;
	entity->line_number = line_number;
	entity->dbtype = "DB_BTREE";
	entity->transactional = the_schema.environment.transactional;
	return entity;
}

/*
 * Allocate, populate, and return memory for an ATTRIBUTE struct.  If
 * there is a malloc failure, set an error message in the Parse
 * object and return NULL.
 */
static ATTRIBUTE *
make_attribute(name, pParse)
	char *name;
	Parse *pParse;
{
	ATTRIBUTE *attribute;

	attribute = calloc(1, sizeof(ATTRIBUTE));
	if (attribute == NULL) {
		sqlite3ErrorMsg(pParse, "Malloc failed");
		return NULL;
	}

	attribute->name = name;
	return attribute;
}

/*
 * Allocate, populate, and return memory for an ATTR_TYPE struct.  If
 * there is a malloc failure, set an error message in the Parse
 * object and return NULL.
 */
static ATTR_TYPE *
make_attrtype(token, ctype, dimension, is_array, is_string, pParse)
	char *token;
	char *ctype;
	int dimension;
	int is_array;
	int is_string;
	Parse *pParse;
{
	ATTR_TYPE *type;

	type = calloc(1, sizeof(ATTR_TYPE));
	if (type == NULL) {
		sqlite3ErrorMsg(pParse, "Malloc failed");
		return NULL;
	}

	type->sql_token = token;
	type->c_type = ctype;
	type->array_dim = dimension;
	type->is_array = is_array;
	type->is_string = is_string;
	return type;
}

/*
 * Allocate, populate, and return memory for an DB_INDEX struct.  If
 * there is a malloc failure, set the error message in the Parse
 * object and return NULL.
 */
static DB_INDEX *
make_index(name, primary, attribute, pParse)
	char *name;
	ENTITY *primary;
	ATTRIBUTE *attribute;
	Parse *pParse;
{
	DB_INDEX *idx;

	idx = calloc(1, sizeof(DB_INDEX));
	if (idx == NULL) {
		sqlite3ErrorMsg(pParse, "Malloc failed");
		return NULL;
	}

	idx->name = name;
	idx->primary = primary;
	idx->attribute = attribute;
	idx->line_number = line_number;
	idx->dbtype = "DB_BTREE";

	return idx;
}

static void
add_entity(entity)
	ENTITY *entity;
{
	if (the_schema.entities_tail == NULL)
		the_schema.entities_tail = the_schema.entities_head = entity;
	else {
		the_schema.entities_tail->next = entity;
		the_schema.entities_tail = entity;
	}
}

static void
add_index(idx)
	DB_INDEX *idx;
{
	if (the_schema.indexes_tail == NULL)
		the_schema.indexes_tail = the_schema.indexes_head = idx;
	else {
		the_schema.indexes_tail->next = idx;
		the_schema.indexes_tail = idx;
	}
}

static void
add_attribute(entity, attr)
	ENTITY *entity;
	ATTRIBUTE * attr;
{
	if (entity->attributes_tail == NULL) {
		entity->attributes_head = entity->attributes_tail = attr;
	} else {
		entity->attributes_tail->next = attr;
		entity->attributes_tail = attr;
	}
}

static ENTITY *
get_current_entity()
{
	/*
	 * The entity under construction is always at the tail of the
	 *  entity list
	 */
	ENTITY *entity = the_schema.entities_tail;
	assert(entity);

	return entity;
}

static ATTRIBUTE *
get_current_attribute()
{
	ENTITY *entity;
	ATTRIBUTE *attr;

	/*
	 * The attribute under construction is always at the tail of
	 * the attribute list belonging to the entity at the tail of
	 * the entity list.
	 */
	entity = get_current_entity();
	attr = entity->attributes_tail;
	assert(attr);
	return attr;
}

static ENTITY *
get_entity_by_name(sought_name)
char *sought_name;
{
	ENTITY *e;

	for (e = the_schema.entities_head; e; e = e->next)
		if (strcasecmp(sought_name, e->name) == 0)
			return e;

	return NULL;
}

static ATTRIBUTE *
get_attribute_by_name(in_entity, sought_name)
ENTITY * in_entity;
char *sought_name;
{
	ATTRIBUTE *a;
	for (a = in_entity->attributes_head; a; a = a->next)
		if (strcasecmp(sought_name, a->name) == 0)
			return a;

	return NULL;
}

static DB_INDEX *
get_index_by_name(sought_name)
char *sought_name;
{
	DB_INDEX *idx;

	for (idx = the_schema.indexes_head; idx; idx = idx->next)
		if (strcasecmp(sought_name, idx->name) == 0)
			return idx;

	return NULL;
}

void
sqlite3BeginParse(Parse *pParse, int explainFlag)
{
	COMPQUIET(pParse, NULL);
	COMPQUIET(explainFlag, 0);
	/* no-op */
}

void
bdb_create_database(Token *name, Parse *pParse) {
	if (the_schema.environment.name != NULL)
		sqlite3ErrorMsg(pParse,
		    "Encountered two CREATE DATABASE statements; only one \
is allowed");

	if ((the_schema.environment.name =
	    name_from_token(name, pParse)) == NULL)
		return;

	the_schema.environment.line_number = line_number;
	the_schema.environment.cache_size = 0;
	the_schema.environment.transactional = txnflag;

	the_parse_progress.last_event = PE_ENVIRONMENT;
}

void
sqlite3StartTable(pParse, pName1, pName2, isTemp, isView, isVirtual, noErr)
	Parse *pParse;   /* Parser context */
	Token *pName1;   /* First part of the name of the table or view */
	Token *pName2;   /* Second part of the name of the table or view */
	int isTemp;      /* True if this is a TEMP table */
	int isView;      /* True if this is a VIEW */
	int isVirtual;   /* True if this is a VIRTUAL table */
	int noErr;       /* Do nothing if table already exists */
{
	char *name, *name2;
	ENTITY *entity;
	DB_INDEX *idx;

	COMPQUIET(isTemp, 0);
	COMPQUIET(isView, 0);
	COMPQUIET(isVirtual, 0);
	COMPQUIET(noErr, 0);

	if (the_schema.environment.name == NULL) {
		sqlite3ErrorMsg(pParse,
		    "Please specify CREATE DATABASE before CREATE TABLE");
		return;
	}

	if ((name = name_from_token(pName1, pParse)) == NULL)
		return;

	/*
	 * We don't allow the second pName, for now.  Note for future
	 * reference: if pName2 is set, then pName1 is the database
	 * name, and pname2 is the table name.
	 */
	name2 = NULL;

	if (! (pName2 == NULL || pName2->n == 0) ) {
		name2 = name_from_token(pName2, pParse);
		sqlite3ErrorMsg(pParse,
		    "The table name must be simple: %s.%s",
		    name, name2);
		goto free_allocated_on_error;
	}

	if ((entity = get_entity_by_name(name)) != NULL) {
		sqlite3ErrorMsg(pParse,
		    "Found two declarations of a table named %s, at lines \
%d and %d",
		    name, entity->line_number, line_number);
		goto free_allocated_on_error;
	}

	if ((idx = get_index_by_name(name)) != NULL) {
		sqlite3ErrorMsg(pParse,
		    "The entity named %s on line %d has the same name as \
the index at line %d.  This is not allowed.",
		    name, line_number, idx->line_number);
		goto free_allocated_on_error;
	}

	if ((entity = make_entity(name, pParse)) == NULL)
		goto free_allocated_on_error;

	the_parse_progress.last_event = PE_ENTITY;
	the_parse_progress.last_entity = entity;
	add_entity(entity);

	return;

free_allocated_on_error:
	if (name != NULL) free(name);
	if (name2 != NULL) free(name2);
}

void
sqlite3AddColumn(Parse *pParse, Token *pName)
{
	ENTITY *entity;
	ATTRIBUTE *attr;
	char *name;

	if ((name = name_from_token(pName, pParse)) == NULL)
		return;

	entity = get_current_entity();

	if ((attr = get_attribute_by_name(entity, name)) != NULL) {
		sqlite3ErrorMsg(pParse,
		    "The table %s contains two columns with the same name %s; \
this is not allowed.",
		    entity->name, name);
		goto free_allocated_on_error;
	}

	if ((attr = make_attribute(name, pParse)) == NULL)
		goto free_allocated_on_error;

	the_parse_progress.last_event = PE_ATTRIBUTE;
	the_parse_progress.last_attribute = attr;
	add_attribute(entity, attr);

	return;

free_allocated_on_error:
	if (name != NULL) free(name);
}

static void
delete_spaces(char *s) {
	char *p;

	p = s;
	while (*p) {
		if (!isspace(*p))
			*s++ = *p;
		p++;
	}
	*s = *p;
}

/*
 * This function maps SQL types into the closest equivalent
 * available using plain C-language types.
 */
static ATTR_TYPE *
map_sql_type(pParse, token)
	Parse *pParse;
	char *token;
{
	int dimension, scale, is_array, is_string;
	size_t len;
	char *p, *q;
	ATTR_TYPE *type;
	char *t;
	char *ctype;

	ctype = NULL;
	type = NULL;
	scale = 0;
	len = strlen(token) + 1;

	/*
	 * Make a copy of the original token, so that we can modify it
	 * to remove spaces, and break it up into multiple strings by
	 * inserting null characters.
	 */
	t = malloc(len);
	if (t == NULL) {
		sqlite3ErrorMsg(pParse, "Malloc failed");
		return NULL;
	}

	memcpy(t, token, len);

	dimension = 0;
	is_array = 0;
	is_string = 0;

	delete_spaces(t); /* Remove any spaces that t might contain. */

	/*
	 * If t contains a parenthesis, then it is a SQL type that has
	 * either a dimension, such as VARCHAR(255); or a precision,
	 * and possibly a scale, such as NUMBER(15, 2).  We need to
	 * extract these values.  Sqlite's parser does not decompose
	 * these tokens for us.
	 */

	if ((p = strchr(t, '(')) != NULL) {
		/*
		 * Split the token into two strings by replacing the
		 * parenthesis with a null character.  The pointer t
		 * is now a simple type name, and p points to the
		 * first parenthesized parameter.
		 */
		*p++ = '\0';

		/*
		 * There should be a right paren somewhere.
		 * Actually, the parser probably guarantees this at least.
		 */
		if ((q = strchr(p, ')')) == NULL) {
			sqlite3ErrorMsg(pParse,
			    "Missing right parenthesis in type expression %s\n",
			    token);
			goto free_allocated_on_error;
		}

		/*
		 * null the paren, to make the parameter list a
		 * null-terminated string
		 */
		*q = '\0';

		/*
		 * Is there a comma in the parameter list? If so,
		 * we'll isolate the first parameter
		 */
		if ((q = strchr(p, ',')) != NULL)
			*q++ = '\0';  /* q now points to the second param */

		dimension = atoi(p);

		if (dimension == 0) {
			sqlite3ErrorMsg(pParse,
			    "Non-numeric or zero size parameter in type \
expression %s\n",
			    token);
			goto free_allocated_on_error;
		}
		/* If there was a second parameter, parse it */
		scale = 0;
		if (q != NULL && *q != '\0') {
			if (strchr(q, ',') == NULL && isdigit(*q)) {
				scale = atoi(q);
			} else {
				sqlite3ErrorMsg(pParse,
				    "Unexpected value for second parameter in \
type expression %s\n",
				    token);
				goto free_allocated_on_error;
			}
		}
	}

	/* OK, now the simple mapping. */

	q = t;
	while (*q) {
		*q = tolower(*q);
		q++;
	}

	if (strcmp(t, "bin") == 0) {
		ctype = "char";
		is_array = 1;
	} else if (strcmp(t, "varbin") == 0) {
		ctype = "char";
		is_array = 1;
	} else if (strcmp(t, "char") == 0) {
		ctype = "char";
		is_array = 1;
		is_string = 1;
	} else if (strcmp(t, "varchar") == 0) {
		ctype = "char";
		is_array = 1;
		is_string = 1;
	} else if (strcmp(t, "varchar2") == 0) {
		ctype = "char";
		is_array = 1;
		is_string = 1;
	} else if (strcmp(t, "bit") == 0)
		ctype = "char";
	else if (strcmp(t, "tinyint") == 0)
		ctype = "char";
	else if (strcmp(t, "smallint") == 0)
		ctype = "short";
	else if (strcmp(t, "integer") == 0)
		ctype = "int";
	else if (strcmp(t, "int") == 0)
		ctype = "int";
	else if (strcmp(t, "bigint") == 0)
		ctype = "long";
	else if (strcmp(t, "real") == 0)
		ctype = "float";
	else if (strcmp(t, "double") == 0)
		ctype = "double";
	else if (strcmp(t, "float") == 0)
		ctype = "double";
	else if (strcmp(t, "decimal") == 0)
		ctype = "double";
	else if (strcmp(t, "numeric") == 0)
		ctype = "double";
	else if (strcmp(t, "number") == 0 ) {
		/*
		 * Oracle Number type: if scale is zero, it's an integer.
		 * Otherwise, call it a floating point
		 */
		if (scale == 0) {
			/* A 10 digit decimal needs a long */
			if (dimension < 9)
				ctype = "int";
			else
				ctype = "long";
		} else {
			if (dimension < 7 )
				ctype = "float";
			else
				ctype = "double";
		}
	}
	else {
		sqlite3ErrorMsg(pParse,
		    "Unsupported type %s\n",
		    token);
		goto free_allocated_on_error;
	}

	if (is_array) {
		if (dimension < 1) {
			sqlite3ErrorMsg(pParse,
			    "Zero dimension not allowed for %s",
			    token);
			goto free_allocated_on_error;
		}
		if ((!is_string) && dimension > maxbinsz)
			maxbinsz = dimension;
	}

	if (is_string && dimension < 2)
		fprintf(stderr,
		    "Warning: dimension of string \"%s %s\" \
is too small to hold a null-terminated string.",
		    get_current_attribute()->name, token);

	type = make_attrtype(token, ctype, dimension, is_array,
	    is_string, pParse);

free_allocated_on_error:
	free(t);

	/* Type will be NULL unless make_attrtype was called, and succeeded. */
	return (type);
}

void
sqlite3AddColumnType(pParse, pType)
	Parse *pParse;
	Token *pType;
{
	char *type;
	ATTRIBUTE *attr;

	if ((type = name_from_token(pType, pParse)) == NULL)
		return;

	attr = get_current_attribute();

	attr->type = map_sql_type(pParse, type);
}

void
sqlite3AddPrimaryKey(pParse, pList, onError, autoInc, sortOrder)
	Parse *pParse;    /* Parsing context */
	ExprList *pList;  /* List of field names to be indexed */
	int onError;      /* What to do with a uniqueness conflict */
	int autoInc;      /* True if the AUTOINCREMENT keyword is present */
	int sortOrder;    /* SQLITE_SO_ASC or SQLITE_SO_DESC */
{
	ENTITY *entity;
	ATTRIBUTE *attr;

	COMPQUIET(pParse, NULL);
	COMPQUIET(pList, NULL);
	COMPQUIET(onError, 0);
	COMPQUIET(autoInc, 0);
	COMPQUIET(sortOrder, 0);

	entity = get_current_entity();
	attr = get_current_attribute();
	entity->primary_key = attr;
}

/*
 * Add a new element to the end of an expression list.  If pList is
 * initially NULL, then create a new expression list.
 */
ExprList *
sqlite3ExprListAppend(pParse, pList, pExpr, pName)
	Parse *pParse;          /* Parsing context */
	ExprList *pList;        /* List to which to append. Might be NULL */
	Expr *pExpr;            /* Expression to be appended */
	Token *pName;           /* AS keyword for the expression */
{
	if ( pList == NULL ) {
		pList = calloc(1, sizeof(ExprList));
		if (pList == NULL) {
			sqlite3ErrorMsg(pParse, "Malloc failed");
			return NULL;
		}
	}
	if (pList->nAlloc <= pList->nExpr) {
		struct ExprList_item *a;
		int n = pList->nAlloc*2 + 4;
		a = realloc(pList->a, n * sizeof(pList->a[0]));
		if ( a == NULL ) {
			sqlite3ErrorMsg(pParse, "Malloc failed");
			return NULL;
		}
		pList->a = a;
		pList->nAlloc = n;
	}

	if ( pExpr || pName ) {
		struct ExprList_item *pItem = &pList->a[pList->nExpr++];
		memset(pItem, 0, sizeof(*pItem));
		if ((pItem->zName = name_from_token(pName, pParse)) == NULL) {
			pList->nExpr --; /* undo allocation of pItem */
			return pList;
		}
		pItem->pExpr = pExpr;
	}
	return pList;
}

void
sqlite3ExprListCheckLength(pParse, pElist, zObject)
	Parse *pParse;
	ExprList *pElist;
	const char *zObject;
{
	COMPQUIET(pParse, NULL);
	COMPQUIET(pElist, NULL);
	COMPQUIET(zObject, NULL);
	/* no-op */
}

/*
 * Append a new table name to the given SrcList.  Create a new SrcList
 * if need be.  If pList is initially NULL, then create a new
 * src list.
 *
 * We don't have a Parse object here, so we can't use name_from_token,
 * or report an error via the usual Parse.rc mechanism.  Just emit a
 * message on stderr if there is a problem.  Returning NULL from this
 * function will cause the parser to fail, so any message we print
 * here will be followed by the usual syntax error message.
 */

SrcList *
sqlite3SrcListAppend(db, pList, pTable, pDatabase)
	sqlite3 *db;        /* unused */
	SrcList *pList;     /* Append to this SrcList */
	Token *pTable;      /* Table to append */
	Token *pDatabase;   /* Database of the table, not used at this time */
{
	struct SrcList_item *pItem;
	char *table_name;

	COMPQUIET(db, NULL);
	COMPQUIET(pDatabase, NULL);

	table_name = NULL;

	if (pList == NULL) {
		pList = calloc(1, sizeof(SrcList));
		if (pList == NULL) {
			fprintf(stderr, "Malloc failure\n");
			return NULL;
		}

		pList->nAlloc = 1;
	}

	if (pList->nSrc >= pList->nAlloc) {
		SrcList *pNew;
		pList->nAlloc *= 2;
		pNew = realloc(pList,
		    sizeof(*pList) + (pList->nAlloc-1)*sizeof(pList->a[0]));
		if (pNew == NULL) {
			fprintf(stderr, "Malloc failure\n");
			return NULL;
		}

		pList = pNew;
	}

	pItem = &pList->a[pList->nSrc];
	memset(pItem, 0, sizeof(pList->a[0]));

	if (pTable == NULL || pTable->n <= 0) {
		fprintf(stderr,
		    "Extracting name from a null or empty token\n");
		return NULL;
	}
	table_name = calloc(1, pTable->n + 1);
	if (table_name == NULL) {
		fprintf(stderr, "Malloc failure\n");
		return NULL;
	}

	memcpy(table_name, (char*)pTable->z, pTable->n);
	pItem->zName = table_name;
	pItem->zDatabase = NULL;
	pItem->iCursor = -1;
	pItem->isPopulated = 0;
	pList->nSrc++;

	return pList;
}

/*
 * We parse, and allow, foreign key constraints, but currently do not
 * use them.
 */
void
sqlite3CreateForeignKey(pParse, pFromCol, pTo, pToCol, flags)
	Parse *pParse;       /* Parsing context */
	ExprList *pFromCol;  /* Columns in that reference other table */
	Token *pTo;          /* Name of the other table */
	ExprList *pToCol;    /* Columns in the other table */
	int flags;           /* Conflict resolution algorithms. */
{
	char * s;

	COMPQUIET(flags, 0);

	if (debug) {
		if ((s = name_from_token(pTo, pParse)) == NULL)
			return;
		fprintf(stderr, "Foreign Key Constraint not implemented: \
FromTable %s FromCol %s ToTable %s ToCol %s\n",
		    get_current_entity()->name, pFromCol->a->zName,
		    s, pToCol->a->zName);
		free(s);
	}
}

void
sqlite3DeferForeignKey(Parse *pParse, int isDeferred)
{
	COMPQUIET(pParse, NULL);
	COMPQUIET(isDeferred, 0);
	/* no-op */
}

void
sqlite3CreateIndex(pParse, pName1, pName2, pTblName, pList,
    onError, pStart, pEnd, sortOrder, ifNotExist)
	Parse *pParse;     /* All information about this parse */
	Token *pName1;     /* First part of index name. May be NULL */
	Token *pName2;     /* Second part of index name. May be NULL */
	SrcList *pTblName; /* Table to index. Use pParse->pNewTable if 0 */
	ExprList *pList;   /* A list of columns to be indexed */
	int onError;       /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
	Token *pStart;     /* The CREATE token that begins this statement */
	Token *pEnd;       /* The ")" that closes the CREATE INDEX statement */
	int sortOrder;     /* Sort order of primary key when pList==NULL */
	int ifNotExist;    /* Omit error if index already exists */
{
	ENTITY *e, *extra_entity;
	ATTRIBUTE *a;
	DB_INDEX *idx;
	char *entity_name, *attribute_name, *index_name;

	COMPQUIET(pName2, NULL);
	COMPQUIET(onError, 0);
	COMPQUIET(pStart, NULL);
	COMPQUIET(pEnd, NULL);
	COMPQUIET(sortOrder, 0);
	COMPQUIET(ifNotExist, 0);

	entity_name = pTblName->a->zName;
	attribute_name = pList->a->zName;
	if ((index_name = name_from_token(pName1, pParse)) == NULL)
		return;

	e = get_entity_by_name(entity_name);
	if (e == NULL) {
		sqlite3ErrorMsg(pParse, "Index %s names unknown table %s",
		    index_name, entity_name);
		goto free_allocated_on_error;
	}

	a = get_attribute_by_name(e, attribute_name);
	if (a == NULL) {
		sqlite3ErrorMsg(pParse,
		    "Index %s names unknown column %s in table %s",
		    index_name, attribute_name, entity_name);
		goto free_allocated_on_error;;
	}

	if ((idx = get_index_by_name(index_name)) != NULL) {
		sqlite3ErrorMsg(pParse,
		    "Found two declarations of an index named %s, \
at lines %d and %d",
		    index_name, idx->line_number, line_number);
		goto free_allocated_on_error;;
	}

	if ((extra_entity = get_entity_by_name(index_name)) != NULL) {
		sqlite3ErrorMsg(pParse,
		    "The index named %s on line %d has the same name as the \
table at line %d.  This is not allowed.",
		    index_name, line_number,
		    extra_entity->line_number);
		goto free_allocated_on_error;
	}

	if ((idx = make_index(index_name, e, a, pParse)) == NULL)
		goto free_allocated_on_error;

	the_parse_progress.last_event = PE_INDEX;
	the_parse_progress.last_index = idx;
	add_index(idx);

	return;

free_allocated_on_error:
	if (index_name != NULL)
		free(index_name);
}

void sqlite3EndTable(pParse, pCons, pEnd, pSelect)
	Parse *pParse;          /* Parse context */
	Token *pCons;           /* The ',' token after the last column defn. */
	Token *pEnd;            /* The final ')' token in the CREATE TABLE */
	Select *pSelect;        /* Select from a "CREATE ... AS SELECT" */
{
	COMPQUIET(pParse, NULL);
	COMPQUIET(pCons, NULL);
	COMPQUIET(pEnd, NULL);
	COMPQUIET(pSelect, NULL);
	the_parse_progress.last_event = PE_ENTITY;
}

void
sqlite3FinishCoding(pParse)
	Parse *pParse;
{
	COMPQUIET(pParse, NULL);
	/* no-op */
}