-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathsql3parse_table.c
1562 lines (1237 loc) · 49.4 KB
/
sql3parse_table.c
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
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
995
996
997
998
999
1000
//
// sql3parse_table.c
//
// Created by Marco Bambini on 14/02/16.
//
#include "sql3parse_table.h"
#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable:4201) // nonstandard extension used: nameless struct/union
#endif
typedef enum {
// internals
TOK_EOF = 0, TOK_ERROR, TOK_IDENTIFIER, TOK_NUMBER, TOK_LITERAL, TOK_COMMENT,
// keywords
TOK_CREATE, TOK_TEMP, TOK_TABLE, TOK_IF, TOK_NOT, TOK_EXISTS,
TOK_WITHOUT, TOK_ROWID, TOK_STRICT, TOK_AS,
// separators
TOK_DOT, TOK_SEMICOLON, TOK_COMMA, TOK_OPEN_PARENTHESIS, TOK_CLOSED_PARENTHESIS,
// constraints
TOK_CONSTRAINT, TOK_PRIMARY, TOK_KEY, TOK_UNIQUE, TOK_CHECK, TOK_FOREIGN,
TOK_ON, TOK_CONFLICT, TOK_ROLLBACK, TOK_ABORT, TOK_FAIL, TOK_IGNORE, TOK_REPLACE,
TOK_COLLATE, TOK_ASC, TOK_DESC, TOK_AUTOINCREMENT,
// foreign key clause
TOK_REFERENCES, TOK_DELETE, TOK_UPDATE, TOK_SET, TOK_NULL, TOK_DEFAULT, TOK_CASCADE,
TOK_RESTRICT, TOK_NO, TOK_ACTION, TOK_MATCH, TOK_DEFERRABLE, TOK_INITIALLY,
TOK_DEFERRED, TOK_IMMEDIATE,
// alter
TOK_ALTER, TOK_RENAME, TOK_ADD, TOK_DROP, TOK_COLUMN, TOK_TO
} sql3token_t;
struct sql3string {
const char *ptr; // ptr to first byte of the string
size_t length; // string length
};
struct sql3foreignkey {
sql3string table; // foreign key table
size_t num_columns;
sql3string *column_name;
sql3fk_action on_delete;
sql3fk_action on_update;
sql3string match;
sql3fk_deftype deferrable;
};
struct sql3column {
sql3string name; // column name
sql3string type; // column type (can be NULL)
sql3string length; // column length (can be NULL)
sql3string constraint_name; // constraint name (can be NULL)
sql3string comment; // column comment (can be NULL)
bool is_primarykey; // primary key flag
bool is_autoincrement; // autoincrement flag (only if is_primarykey is true)
bool is_notnull; // not null flag
bool is_unique; // is unique flag
sql3order_clause pk_order; // primary key order
sql3conflict_clause pk_conflictclause; // primary key conflit clause
sql3conflict_clause notnull_conflictclause; // not null conflit clause
sql3conflict_clause unique_conflictclause; // unique conflit clause
sql3string check_expr; // check expression (can be NULL)
sql3string default_expr; // default expression (can be NULL)
sql3string collate_name; // collate name (can be NULL)
sql3foreignkey *foreignkey_clause; // foreign key clause (can be NULL)
};
struct sql3tableconstraint {
sql3constraint_type type; // table constraint type
sql3string name; // constraint name (can be NULL)
union {
// if type SQL3TABLECONSTRAINT_PRIMARYKEY or SQL3TABLECONSTRAINT_UNIQUE
struct {
size_t num_indexed; // number of indexed columns
sql3idxcolumn *indexed_columns; // array fo indexed columns
sql3conflict_clause conflict_clause; // conflict clause
};
// if type SQL3TABLECONSTRAINT_CHECK
sql3string check_expr; // check expression (always NULL in this version)
// if type SQL3TABLECONSTRAINT_FOREIGNKEY
struct {
size_t foreignkey_num; // number of columns defined in foreign key
sql3string *foreignkey_name; // column names in the foreign key
sql3foreignkey *foreignkey_clause; // foreign key clause (can be NULL)
};
};
};
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
struct sql3table {
sql3string name; // table name
sql3string schema; // schema name (can be NULL)
sql3string comment; // table comment (can be NULL)
bool is_temporary; // flag set if table is temporary
bool is_ifnotexists; // flag set if table is created with a IF NOT EXISTS clause
bool is_withoutrowid; // flag set if table is created with a WITHOUT ROWID clause
bool is_strict; // flag set if table is created with a STRICT clause
size_t num_columns; // number of columns defined in the table
sql3column **columns; // array of columns defined in the table
size_t num_constraint; // number of table constraint
sql3tableconstraint **constraints; // array of table constraints
sql3statement_type type; // statement type
sql3string current_name; // used in ALTER TABLE statement
sql3string new_name; // used in ALTER TABLE statement
};
struct sql3idxcolumn {
sql3string name; // column name
sql3string collate_name; // collate name (can be NULL)
sql3order_clause order; // order
};
typedef struct {
const char *buffer; // original sql
size_t size; // size of the input buffer
size_t offset; // offset inside the input buffer
sql3string identifier; // latest identifier found by the lexer
sql3string *comment; // ptr to comment struct contained in sql3table or sql3column
sql3table *table; // table definition
} sql3state;
static sql3string temp_identifier = {.ptr = "temp", .length = 4};
// MARK: - Macros -
#define IS_EOF (state->offset >= state->size)
#define PEEK (state->buffer[state->offset])
#define PEEK2 (state->buffer[state->offset+1])
#define NEXT (state->buffer[state->offset++])
#define SKIP_ONE ++state->offset;
#define CHECK_STR(s) if (!s.ptr) return NULL
#define CHECK_IDX(idx1,idx2) if (idx1>=idx2) return NULL
// MARK: - Public String Functions -
const char *sql3string_ptr (sql3string *s, size_t *length) {
if (length) *length = s->length;
return s->ptr;
}
const char *sql3string_cstring (sql3string *s) {
if (!s->ptr) return NULL;
char *ptr = (char *)SQL3MALLOC0(s->length+1);
if (!ptr) return NULL;
memcpy(ptr, s->ptr, s->length);
return (const char *)ptr;
}
// MARK: - Internal Utils -
static int str_nocasencmp(const char *s1, const char *s2, size_t n) {
while(n > 0 && tolower((unsigned char)*s1) == tolower((unsigned char)*s2)) {
if(*s1 == '\0') return 0;
s1++;
s2++;
n--;
}
if(n == 0) return 0;
return tolower((unsigned char)*s1) - tolower((unsigned char)*s2);
}
static bool symbol_is_space (sql3char c) {
return ((c == ' ') || (c == '\t') || (c == '\v') || (c == '\f'));
}
static bool symbol_is_newline (sql3char c) {
return ((c == '\n') || (c == '\r'));
}
static bool symbol_is_toskip (sql3char c) {
// skip whitespaces and newlines
return (symbol_is_space(c) || symbol_is_newline(c));
}
static bool symbol_is_comment (sql3char c, sql3state *state) {
if ((c == '-') && (PEEK2 == '-')) return true;
if ((c == '/') && (PEEK2 == '*')) return true;
return false;
}
static bool symbol_is_alpha (sql3char c) {
if (c == '_') return true;
return isalpha((int)c);
}
static bool symbol_is_identifier (sql3char c) {
// when called I am already sure first character is alpha so next valid characters are alpha, digit and _
return ((isalpha(c)) || (isdigit(c)) || (c == '_'));
}
static bool symbol_is_escape (sql3char c) {
// From Dr. Hipp
// An effort is made to use labels in single-quotes as string literals
// first, as that is the SQL standard. But if if the token does not make
// sense as a string literal, it falls back to being an identifier. This
// is an ugly hack. I originally put in the fall-back logic for
// compatibility with MySQL and I now see that was a mistake. But it is
// used a lot in legacy code, so I cannot take it out.
return ((c == '`') || (c == '\'') || (c == '"') || (c == '['));
}
static bool symbol_is_punctuation (sql3char c) {
return ((c == '.') || (c == ',') || (c == '(') || (c == ')') || (c == ';'));
}
static bool token_is_column_constraint (sql3token_t t) {
return ((t == TOK_CONSTRAINT) || (t == TOK_PRIMARY) || (t == TOK_NOT) || (t == TOK_UNIQUE) ||
(t == TOK_CHECK) || (t == TOK_DEFAULT) || (t == TOK_COLLATE) || (t == TOK_REFERENCES));
}
static bool token_is_table_constraint (sql3token_t t) {
return ((t == TOK_CONSTRAINT) || (t == TOK_PRIMARY) || (t == TOK_UNIQUE) ||
(t == TOK_CHECK) || (t == TOK_FOREIGN));
}
// MARK: - Internal Lexer -
static sql3token_t sql3lexer_keyword (const char *ptr, size_t length) {
// check if ptr is a reserved keyword
switch (length) {
case 2:
if (str_nocasencmp(ptr, "if", length) == 0) return TOK_IF;
if (str_nocasencmp(ptr, "on", length) == 0) return TOK_ON;
if (str_nocasencmp(ptr, "no", length) == 0) return TOK_NO;
if (str_nocasencmp(ptr, "as", length) == 0) return TOK_AS;
if (str_nocasencmp(ptr, "to", length) == 0) return TOK_TO;
break;
case 3:
if (str_nocasencmp(ptr, "not", length) == 0) return TOK_NOT;
if (str_nocasencmp(ptr, "key", length) == 0) return TOK_KEY;
if (str_nocasencmp(ptr, "asc", length) == 0) return TOK_ASC;
if (str_nocasencmp(ptr, "set", length) == 0) return TOK_SET;
if (str_nocasencmp(ptr, "add", length) == 0) return TOK_ADD;
break;
case 4:
if (str_nocasencmp(ptr, "temp", length) == 0) return TOK_TEMP;
if (str_nocasencmp(ptr, "desc", length) == 0) return TOK_DESC;
if (str_nocasencmp(ptr, "null", length) == 0) return TOK_NULL;
if (str_nocasencmp(ptr, "fail", length) == 0) return TOK_FAIL;
if (str_nocasencmp(ptr, "drop", length) == 0) return TOK_DROP;
break;
case 5:
if (str_nocasencmp(ptr, "table", length) == 0) return TOK_TABLE;
if (str_nocasencmp(ptr, "rowid", length) == 0) return TOK_ROWID;
if (str_nocasencmp(ptr, "check", length) == 0) return TOK_CHECK;
if (str_nocasencmp(ptr, "abort", length) == 0) return TOK_ABORT;
if (str_nocasencmp(ptr, "match", length) == 0) return TOK_MATCH;
if (str_nocasencmp(ptr, "alter", length) == 0) return TOK_ALTER;
break;
case 6:
if (str_nocasencmp(ptr, "create", length) == 0) return TOK_CREATE;
if (str_nocasencmp(ptr, "exists", length) == 0) return TOK_EXISTS;
if (str_nocasencmp(ptr, "unique", length) == 0) return TOK_UNIQUE;
if (str_nocasencmp(ptr, "ignore", length) == 0) return TOK_IGNORE;
if (str_nocasencmp(ptr, "delete", length) == 0) return TOK_DELETE;
if (str_nocasencmp(ptr, "update", length) == 0) return TOK_UPDATE;
if (str_nocasencmp(ptr, "action", length) == 0) return TOK_ACTION;
if (str_nocasencmp(ptr, "strict", length) == 0) return TOK_STRICT;
if (str_nocasencmp(ptr, "rename", length) == 0) return TOK_RENAME;
if (str_nocasencmp(ptr, "column", length) == 0) return TOK_COLUMN;
break;
case 7:
if (str_nocasencmp(ptr, "without", length) == 0) return TOK_WITHOUT;
if (str_nocasencmp(ptr, "primary", length) == 0) return TOK_PRIMARY;
if (str_nocasencmp(ptr, "default", length) == 0) return TOK_DEFAULT;
if (str_nocasencmp(ptr, "collate", length) == 0) return TOK_COLLATE;
if (str_nocasencmp(ptr, "replace", length) == 0) return TOK_REPLACE;
if (str_nocasencmp(ptr, "cascade", length) == 0) return TOK_CASCADE;
if (str_nocasencmp(ptr, "foreign", length) == 0) return TOK_FOREIGN;
break;
case 8:
if (str_nocasencmp(ptr, "conflict", length) == 0) return TOK_CONFLICT;
if (str_nocasencmp(ptr, "rollback", length) == 0) return TOK_ROLLBACK;
if (str_nocasencmp(ptr, "restrict", length) == 0) return TOK_RESTRICT;
if (str_nocasencmp(ptr, "deferred", length) == 0) return TOK_DEFERRED;
break;
case 9:
if (str_nocasencmp(ptr, "temporary", length) == 0) return TOK_TEMP;
if (str_nocasencmp(ptr, "initially", length) == 0) return TOK_INITIALLY;
if (str_nocasencmp(ptr, "immediate", length) == 0) return TOK_IMMEDIATE;
break;
case 10:
if (str_nocasencmp(ptr, "constraint", length) == 0) return TOK_CONSTRAINT;
if (str_nocasencmp(ptr, "references", length) == 0) return TOK_REFERENCES;
if (str_nocasencmp(ptr, "deferrable", length) == 0) return TOK_DEFERRABLE;
break;
case 13:
if (str_nocasencmp(ptr, "autoincrement", length) == 0) return TOK_AUTOINCREMENT;
break;
}
// no reserved keyword found
return TOK_IDENTIFIER;
}
sql3token_t sql3lexer_comment (sql3state *state) {
sql3char c1_start = NEXT;
sql3char c2_start = NEXT;
bool is_c_comment = ((c1_start == '/') && (c2_start == '*'));
size_t offset = state->offset;
const char *ptr = &state->buffer[offset];
while (1) {
sql3char c1 = NEXT;
// EOF case
if (c1 == 0) {
// SQL or C-style comments can be terminated by EOF
break;
}
// check for end-of-comment condition
if (is_c_comment) {
// c-style comments need two characters to check
sql3char c2 = PEEK;
if ((c1 == '*') && (c2 == '/')) {
(void) NEXT; // consume c2
break;
}
} else {
// -- comments are closed by newline
if (symbol_is_newline(c1)) break;
}
}
// setup current comment
if (state->comment) {
size_t length = state->offset - offset;
length -= (is_c_comment) ? 2 : 1; // remove */ or newline
state->comment->ptr = ptr;
state->comment->length = length;
//printf("Parsed comment: %.*s\n", (int)length, ptr);
}
return TOK_COMMENT;
}
sql3token_t sql3lexer_punctuation (sql3state *state) {
sql3char c = NEXT;
switch (c) {
case ',': return TOK_COMMA;
case '.': return TOK_DOT;
case '(': return TOK_OPEN_PARENTHESIS;
case ')': return TOK_CLOSED_PARENTHESIS;
case ';': return TOK_SEMICOLON;
}
return TOK_ERROR;
}
sql3token_t sql3lexer_alpha (sql3state *state) {
size_t offset = state->offset;
while (symbol_is_identifier(PEEK)) {
SKIP_ONE;
}
const char *ptr = &state->buffer[offset];
size_t length = state->offset - offset;
sql3token_t t = sql3lexer_keyword(ptr, length);
if (t != TOK_IDENTIFIER) return t;
// setup internal identifier
state->identifier.ptr = ptr;
state->identifier.length = length;
return TOK_IDENTIFIER;
}
sql3token_t sql3lexer_escape (sql3state *state) {
sql3char c, escaped = NEXT; // consume escaped char
if (escaped == '[') escaped = ']'; // mysql compatibility mode
// read until EOF or closing escape character
size_t offset = state->offset;
do {
c = NEXT;
} while ((c != 0) && (c != escaped));
const char *ptr = &state->buffer[offset];
size_t length = state->offset - (offset + 1);
// sanity check on closing escaped character
if (c != escaped) return TOK_ERROR;
// setup internal identifier
state->identifier.ptr = ptr;
state->identifier.length = length;
return TOK_IDENTIFIER;
}
static bool sql3lexer_checkskip (sql3state *state) {
sql3char c;
loop:
c = PEEK;
if (symbol_is_toskip(c)) {SKIP_ONE; goto loop;}
if (symbol_is_comment(c, state)) {if (sql3lexer_comment(state) != TOK_COMMENT) return false; goto loop;}
return true;
}
static sql3token_t sql3lexer_next (sql3state *state) {
loop:
if (IS_EOF) return TOK_EOF;
sql3char c = PEEK;
if (c == 0) return TOK_EOF;
if (symbol_is_toskip(c)) {SKIP_ONE; goto loop;}
if (symbol_is_comment(c, state)) {if (sql3lexer_comment(state) != TOK_COMMENT) return TOK_ERROR; goto loop;}
if (symbol_is_punctuation(c)) return sql3lexer_punctuation(state);
if (symbol_is_alpha(c)) return sql3lexer_alpha(state);
if (symbol_is_escape(c)) return sql3lexer_escape(state);
return TOK_ERROR;
}
static sql3token_t sql3lexer_peek (sql3state *state) {
// peek calls sql3lexer_next and reset its state after the call
size_t saved = state->offset;
sql3token_t token = sql3lexer_next(state);
state->offset = saved;
return token;
}
// MARK: - Internal Parser -
static sql3error_code sql3parse_optionalorder (sql3state *state, sql3order_clause *clause) {
sql3token_t token = sql3lexer_peek(state);
*clause = SQL3ORDER_NONE;
if ((token == TOK_ASC) || (token == TOK_DESC)) {
sql3lexer_next(state); // consume token
if (token == TOK_ASC) *clause = SQL3ORDER_ASC;
else *clause = SQL3ORDER_DESC;
}
return SQL3ERROR_NONE;
}
static sql3error_code sql3parse_optionalconflitclause (sql3state *state, sql3conflict_clause *conflict) {
sql3token_t token = sql3lexer_peek(state);
*conflict = SQL3CONFLICT_NONE;
if (token == TOK_ON) {
sql3lexer_next(state); // consume TOK_ON
token = sql3lexer_next(state);
if (token != TOK_CONFLICT) return SQL3ERROR_SYNTAX;
token = sql3lexer_next(state);
if (token == TOK_ROLLBACK) *conflict = SQL3CONFLICT_ROLLBACK;
else if (token == TOK_ABORT) *conflict = SQL3CONFLICT_ABORT;
else if (token == TOK_FAIL) *conflict = SQL3CONFLICT_FAIL;
else if (token == TOK_IGNORE) *conflict = SQL3CONFLICT_IGNORE;
else if (token == TOK_REPLACE) *conflict = SQL3CONFLICT_REPLACE;
else return SQL3ERROR_SYNTAX;
}
return SQL3ERROR_NONE;
}
static sql3foreignkey *sql3parse_foreignkey_clause (sql3state *state) {
sql3foreignkey *fk = SQL3MALLOC0(sizeof(sql3foreignkey));
if (!fk) return NULL;
// parse foreign table name
sql3token_t token = sql3lexer_next(state);
if (token != TOK_IDENTIFIER) goto error;
fk->table = state->identifier;
// check for optional columns part
if (sql3lexer_peek(state) == TOK_OPEN_PARENTHESIS) {
sql3lexer_next(state); // consume TOK_OPEN_PARENTHESIS
// parse column names
do {
// parse column-name
token = sql3lexer_next(state);
if (token != TOK_IDENTIFIER) goto error;
// add column name
++fk->num_columns;
fk->column_name = SQL3REALLOC(fk->column_name, sizeof(sql3string) * fk->num_columns);
if (!fk->column_name) goto error;
fk->column_name[fk->num_columns-1] = state->identifier;
token = sql3lexer_peek(state);
if (token == TOK_COMMA) sql3lexer_next(state); // consume TOK_COMMA
} while (token == TOK_COMMA);
// closed parenthesis is mandatory here
if (sql3lexer_next(state) != TOK_CLOSED_PARENTHESIS) goto error;
}
// check for optional part
fk_loop:
token = sql3lexer_peek(state);
if ((token == TOK_ON) || (token == TOK_MATCH) || (token == TOK_NOT) || (token == TOK_DEFERRABLE)) {
sql3lexer_next(state); // consume token
if (token == TOK_MATCH) {
token = sql3lexer_next(state);
if (token != TOK_IDENTIFIER) goto error;
fk->match = state->identifier;
goto fk_loop;
}
if (token == TOK_ON) {
token = sql3lexer_next(state);
if ((token != TOK_DELETE) && (token != TOK_UPDATE)) goto error;
bool isupdate = (token == TOK_UPDATE);
token = sql3lexer_next(state);
if (token == TOK_CASCADE) {
if (isupdate) fk->on_update = SQL3FKACTION_CASCADE;
else fk->on_delete = SQL3FKACTION_CASCADE;
} else if (token == TOK_RESTRICT) {
if (isupdate) fk->on_update = SQL3FKACTION_RESTRICT;
else fk->on_delete = SQL3FKACTION_RESTRICT;
} else if (token == TOK_SET) {
token = sql3lexer_next(state);
if ((token != TOK_NULL) && (token != TOK_DEFAULT)) goto error;
if (token == TOK_NULL) {
if (isupdate) fk->on_update = SQL3FKACTION_SETNULL;
else fk->on_delete = SQL3FKACTION_SETNULL;
} else {
// TOK_DEFAULT
if (isupdate) fk->on_update = SQL3FKACTION_SETDEFAULT;
else fk->on_delete = SQL3FKACTION_SETDEFAULT;
}
} else if (token == TOK_NO) {
if (sql3lexer_next(state) != TOK_ACTION) goto error;
if (isupdate) fk->on_update = SQL3FKACTION_NOACTION;
else fk->on_delete = SQL3FKACTION_NOACTION;
}
goto fk_loop;
}
bool isnot = false;
if (token == TOK_NOT) {
token = sql3lexer_next(state); // get next token
isnot = true;
}
if (token == TOK_DEFERRABLE) {
fk->deferrable = (isnot) ? SQL3DEFTYPE_NOTDEFERRABLE : SQL3DEFTYPE_DEFERRABLE;
if (sql3lexer_peek(state) == TOK_INITIALLY) {
sql3lexer_next(state); // consume TOK_INITIALLY
token = sql3lexer_next(state); // get next token
if (token == TOK_DEFERRED) {
fk->deferrable = (isnot) ? SQL3DEFTYPE_NOTDEFERRABLE_INITIALLY_DEFERRED : SQL3DEFTYPE_DEFERRABLE_INITIALLY_DEFERRED;
} else if (token == TOK_IMMEDIATE) {
fk->deferrable = (isnot) ? SQL3DEFTYPE_NOTDEFERRABLE_INITIALLY_IMMEDIATE : SQL3DEFTYPE_DEFERRABLE_INITIALLY_IMMEDIATE;
} else goto error;
}
goto fk_loop;
}
goto error;
}
return fk;
error:
if (fk) SQL3FREE(fk);
return NULL;
}
static sql3error_code sql3parse_table_options (sql3state *state) {
sql3token_t token = sql3lexer_peek(state);
while (1) {
if (token == TOK_WITHOUT) {
// WITHOUT ROWID table option
// consume WITHOUT
sql3lexer_next(state);
// ROWID is mandatory at this point
if (sql3lexer_next(state) != TOK_ROWID) return SQL3ERROR_SYNTAX;
// set without rowid flag
state->table->is_withoutrowid = true;
} else if (token == TOK_STRICT) {
// STRICT table option
// consume STRICT
sql3lexer_next(state);
// set without rowid flag
state->table->is_strict = true;
} else {
return SQL3ERROR_NONE;
}
// check for optional ,
token = sql3lexer_peek(state);
if (token != TOK_COMMA) break;
// consume COMMA and PEEK next token
sql3lexer_next(state);
token = sql3lexer_peek(state);
continue;
}
return SQL3ERROR_NONE;
}
static sql3tableconstraint *sql3parse_table_constraint (sql3state *state) {
sql3token_t token = sql3lexer_peek(state);
sql3tableconstraint *constraint = (sql3tableconstraint *)SQL3MALLOC0(sizeof(sql3tableconstraint));
if (!constraint) return NULL;
// optional constraint name
if (token == TOK_CONSTRAINT) {
sql3lexer_next(state); // consume token
token = sql3lexer_next(state);
if (token != TOK_IDENTIFIER) goto error;
constraint->name = state->identifier;
// peek next token
token = sql3lexer_peek(state);
// sanity check next token
if ((token != TOK_CHECK) && (token != TOK_PRIMARY) && (token != TOK_UNIQUE) && (token != TOK_FOREIGN)) goto error;
}
// check for others constraint
if (token == TOK_CHECK) {
token = sql3lexer_next(state); // consume token
constraint->type = SQL3TABLECONSTRAINT_CHECK;
// expressions are not supported in this version
goto error;
}
// same code to execute for PRIMARY KEY or UNIQUE constraint
else if ((token == TOK_PRIMARY) || (token == TOK_UNIQUE)) {
token = sql3lexer_next(state); // consume token
if (token == TOK_PRIMARY) {
if (sql3lexer_next(state) != TOK_KEY) goto error;
constraint->type = SQL3TABLECONSTRAINT_PRIMARYKEY;
} else constraint->type = SQL3TABLECONSTRAINT_UNIQUE;
if (sql3lexer_next(state) != TOK_OPEN_PARENTHESIS) goto error;
// get indexed column
do {
sql3idxcolumn column = {0};
// parse column-name
token = sql3lexer_next(state);
if (token != TOK_IDENTIFIER) goto error;
column.name = state->identifier;
if (sql3lexer_peek(state) == TOK_COLLATE) {
sql3lexer_next(state); // consume TOK_COLLATE
// parse collation-name
token = sql3lexer_next(state);
if (token != TOK_IDENTIFIER) goto error;
column.collate_name = state->identifier;
}
// parse optional order
if (sql3parse_optionalorder(state, &column.order) != SQL3ERROR_NONE) goto error;
// add indexed column
++constraint->num_indexed;
constraint->indexed_columns = SQL3REALLOC(constraint->indexed_columns, sizeof(sql3idxcolumn) * constraint->num_indexed);
if (!constraint->indexed_columns) goto error;
constraint->indexed_columns[constraint->num_indexed-1] = column;
token = sql3lexer_peek(state);
if (token == TOK_COMMA) sql3lexer_next(state); // consume TOK_COMMA
} while (token == TOK_COMMA);
if (sql3lexer_next(state) != TOK_CLOSED_PARENTHESIS) goto error;
if (sql3parse_optionalconflitclause(state, &constraint->conflict_clause) != SQL3ERROR_NONE) goto error;
}
// foreign key constraint
else if (token == TOK_FOREIGN) {
sql3lexer_next(state); // consume TOK_FOREIGN
if (sql3lexer_next(state) != TOK_KEY) goto error;
if (sql3lexer_next(state) != TOK_OPEN_PARENTHESIS) goto error;
constraint->type = SQL3TABLECONSTRAINT_FOREIGNKEY;
// get column names
do {
// parse column-name
token = sql3lexer_next(state);
if (token != TOK_IDENTIFIER) goto error;
// add column name
++constraint->foreignkey_num;
constraint->foreignkey_name = SQL3REALLOC(constraint->foreignkey_name, sizeof(sql3string) * constraint->foreignkey_num);
if (!constraint->foreignkey_name) goto error;
constraint->foreignkey_name[constraint->foreignkey_num-1] = state->identifier;
token = sql3lexer_peek(state);
if (token == TOK_COMMA) sql3lexer_next(state); // consume TOK_COMMA
} while (token == TOK_COMMA);
if (sql3lexer_next(state) != TOK_CLOSED_PARENTHESIS) goto error;
if (sql3lexer_next(state) != TOK_REFERENCES) goto error;
// parse foreign key clause
sql3foreignkey *fk = sql3parse_foreignkey_clause(state);
if (!fk) goto error;
constraint->foreignkey_clause = fk;
}
return constraint;
error:
if (constraint) SQL3FREE(constraint);
return NULL;
}
static sql3string sql3parse_literal (sql3state *state) {
// signed-number (+/-) => numeric-literal
// literal-value
// numeric-literal
// string-literal
// blob-literal (x/X'')
// NULL
// TRUE
// FALSE
// CURRENT_TIME
// CURRENT_DATE
// CURRENT_TIMESTAMP
sql3lexer_checkskip(state);
size_t offset = state->offset;
sql3char c = NEXT;
if (c == '\'' || c == '"') {
// parse string literal
sql3char escaped = c;
while (true) {
c = NEXT;
if (c == escaped) {
sql3char c2 = PEEK;
if (c2 != escaped) break;
(void) NEXT;
}
}
} else {
// parse everything else up until a space
while (true) {
c = PEEK;
if (c == ' ' || c == ',' || c == ')') break;
c = NEXT;
}
}
const char *ptr = &state->buffer[offset];
size_t length = state->offset - offset;
sql3string result = {ptr, length};
return result;
}
static sql3string sql3parse_expression (sql3state *state) {
// '(' expression ')'
sql3lexer_checkskip(state);
size_t offset = state->offset;
sql3char c = NEXT; // '('
uint32_t count = 1; // count number of '('
while (true) {
c = NEXT;
if (c == '(') ++count;
else if (c == ')') {
if (--count == 0) break;
}
}
const char *ptr = &state->buffer[offset];
size_t length = state->offset - offset;
sql3string result = {ptr, length};
return result;
}
static sql3error_code sql3parse_column_type (sql3state *state, sql3column *column) {
// column type is reported as a string
size_t offset = 0;
while (sql3lexer_peek(state) == TOK_IDENTIFIER) {
// consume identifier
sql3lexer_next(state);
// mark the beginning of the first identifier
if (offset == 0) offset = state->offset - state->identifier.length;
}
const char *ptr = &state->buffer[offset];
size_t length = state->offset - offset;
// setup internal identifier
column->type.ptr = ptr;
column->type.length = length;
// check for optional lenght
if (sql3lexer_peek(state) == TOK_OPEN_PARENTHESIS) {
sql3lexer_next(state); // consume '('
// mark start of string
offset = state->offset;
sql3char c;
do {
c = NEXT;
} while ((c != 0) && (c != ')'));
// sanity check on closing escaped character
if (c != ')') return SQL3ERROR_SYNTAX;
// don't include ')' in column lenght
ptr = &state->buffer[offset];
length = state->offset - (offset + 1);
column->length.ptr = ptr;
column->length.length = length;
}
return SQL3ERROR_NONE;
}
static sql3error_code sql3parse_column_constraints (sql3state *state, sql3column *column) {
while (token_is_column_constraint(sql3lexer_peek(state))) {
sql3token_t token = sql3lexer_next(state);
// optional constraint name
if (token == TOK_CONSTRAINT) {
token = sql3lexer_next(state);
if (token != TOK_IDENTIFIER) return SQL3ERROR_SYNTAX;
column->constraint_name = state->identifier;
token = sql3lexer_next(state);
}
switch (token) {
case TOK_PRIMARY:
token = sql3lexer_next(state);
if (token != TOK_KEY) return SQL3ERROR_SYNTAX;
column->is_primarykey = true;
if (sql3parse_optionalorder(state, &column->pk_order) != SQL3ERROR_NONE) return SQL3ERROR_SYNTAX;
if (sql3parse_optionalconflitclause(state, &column->pk_conflictclause) != SQL3ERROR_NONE) return SQL3ERROR_SYNTAX;
if (sql3lexer_peek(state) == TOK_AUTOINCREMENT) {
sql3lexer_next(state); // consume TOK_AUTOINCREMENT
column->is_autoincrement = true;
}
break;
case TOK_NOT:
token = sql3lexer_next(state);
if (token != TOK_NULL) return SQL3ERROR_SYNTAX;
column->is_notnull = true;
if (sql3parse_optionalconflitclause(state, &column->notnull_conflictclause) != SQL3ERROR_NONE) return SQL3ERROR_SYNTAX;
break;
case TOK_UNIQUE:
column->is_unique = true;
if (sql3parse_optionalconflitclause(state, &column->unique_conflictclause) != SQL3ERROR_NONE) return SQL3ERROR_SYNTAX;
break;
case TOK_CHECK:
column->check_expr = sql3parse_expression(state);
break;
case TOK_DEFAULT:
// signed-number (+/-) => numeric-literal
// literal-value
// numeric-literal
// string-literal
// blob-literal (x/X'')
// NULL
// TRUE
// FALSE
// CURRENT_TIME
// CURRENT_DATE
// CURRENT_TIMESTAMP
// '(' expression ')'
// expressions are not supported in this version
if (sql3lexer_peek(state) == TOK_OPEN_PARENTHESIS) column->default_expr = sql3parse_expression(state);
else column->default_expr = sql3parse_literal(state);
break;
case TOK_COLLATE:
token = sql3lexer_next(state);
if (token != TOK_IDENTIFIER) return SQL3ERROR_SYNTAX;
column->collate_name = state->identifier;
break;
case TOK_REFERENCES: {
sql3foreignkey *fk = sql3parse_foreignkey_clause(state);
if (!fk) return SQL3ERROR_SYNTAX;
column->foreignkey_clause = fk;
} break;
default:
return SQL3ERROR_SYNTAX;
}
}
return SQL3ERROR_NONE;
}
static sql3column *sql3parse_column (sql3state *state) {
sql3column *column = SQL3MALLOC0(sizeof(sql3column));
if (!column) return NULL;
// set column comment reference inside state context
state->comment = &column->comment;
// column name is mandatory
sql3token_t token = sql3lexer_next(state);
if (token != TOK_IDENTIFIER) goto error;
// copy column name
column->name = state->identifier;
// parse optional column type
if (sql3lexer_peek(state) == TOK_IDENTIFIER) {
if (sql3parse_column_type(state, column) != SQL3ERROR_NONE) goto error;
}
// check optional column constraints path
if (token_is_column_constraint(sql3lexer_peek(state))) {
if (sql3parse_column_constraints(state, column) != SQL3ERROR_NONE) goto error;
}
return column;
error:
if (column) SQL3FREE(column);
return NULL;
}
// MARK: -
static sql3error_code sql3parse_schema_identifier (sql3state *state) {
sql3table *table = state->table;
// at this point there should be an identifier
// only the optional dot will tell if it is a schema or a table name
sql3token_t token = sql3lexer_next(state);
// temp is a keyword but it is perfectly legal as a schema name
if (token != TOK_IDENTIFIER && token != TOK_TEMP) return SQL3ERROR_SYNTAX;
const char *identifier = (token == TOK_IDENTIFIER) ? sql3string_ptr(&state->identifier, NULL) : temp_identifier.ptr;
if (!identifier) return SQL3ERROR_SYNTAX;
// check for optional DOT (if any then identifier is a schema name)
if (sql3lexer_peek(state) == TOK_DOT) {
// consume DOT
sql3lexer_next(state);
// set schema name
table->schema = (token == TOK_IDENTIFIER) ? state->identifier : temp_identifier;
// parse table name
if (sql3lexer_next(state) != TOK_IDENTIFIER) return SQL3ERROR_SYNTAX;
identifier = sql3string_ptr(&state->identifier, NULL);
if (!identifier) return SQL3ERROR_SYNTAX;