-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql.c
2112 lines (1934 loc) · 51.4 KB
/
sql.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
/*
* $Id: sql.c 703 2009-03-14 22:06:12Z dfishburn $
*
* Copyright (c) 2002-2003, Darren Hiebert
*
* This source code is released for free distribution under the terms of the
* GNU General Public License.
*
* This module contains functions for generating tags for PL/SQL language
* files.
*/
/*
* INCLUDE FILES
*/
#include "general.h" /* must always come first */
#include <ctype.h> /* to define isalpha () */
#include <setjmp.h>
#ifdef DEBUG
#include <stdio.h>
#endif
#include "debug.h"
#include "entry.h"
#include "keyword.h"
#include "parse.h"
#include "read.h"
#include "routines.h"
#include "vstring.h"
/*
* On-line "Oracle Database PL/SQL Language Reference":
* http://download.oracle.com/docs/cd/B28359_01/appdev.111/b28370/toc.htm
*
* Sample PL/SQL code is available from:
* http://www.orafaq.com/faqscrpt.htm#GENPLSQL
*
* On-line SQL Anywhere Documentation
* http://www.ianywhere.com/developer/product_manuals/sqlanywhere/index.html
*/
/*
* MACROS
*/
#define isType(token,t) (boolean) ((token)->type == (t))
#define isKeyword(token,k) (boolean) ((token)->keyword == (k))
/*
* DATA DECLARATIONS
*/
typedef enum eException { ExceptionNone, ExceptionEOF } exception_t;
/*
* Used to specify type of keyword.
*/
typedef enum eKeywordId {
KEYWORD_NONE = -1,
KEYWORD_is,
KEYWORD_begin,
KEYWORD_body,
KEYWORD_cursor,
KEYWORD_declare,
KEYWORD_end,
KEYWORD_function,
KEYWORD_if,
KEYWORD_loop,
KEYWORD_case,
KEYWORD_for,
KEYWORD_call,
KEYWORD_package,
KEYWORD_pragma,
KEYWORD_procedure,
KEYWORD_record,
KEYWORD_object,
KEYWORD_ref,
KEYWORD_rem,
KEYWORD_return,
KEYWORD_returns,
KEYWORD_subtype,
KEYWORD_table,
KEYWORD_trigger,
KEYWORD_type,
KEYWORD_index,
KEYWORD_event,
KEYWORD_publication,
KEYWORD_service,
KEYWORD_domain,
KEYWORD_datatype,
KEYWORD_result,
KEYWORD_url,
KEYWORD_internal,
KEYWORD_external,
KEYWORD_when,
KEYWORD_then,
KEYWORD_variable,
KEYWORD_exception,
KEYWORD_at,
KEYWORD_on,
KEYWORD_primary,
KEYWORD_references,
KEYWORD_unique,
KEYWORD_check,
KEYWORD_constraint,
KEYWORD_foreign,
KEYWORD_ml_table,
KEYWORD_ml_table_lang,
KEYWORD_ml_table_dnet,
KEYWORD_ml_table_java,
KEYWORD_ml_table_chk,
KEYWORD_ml_conn,
KEYWORD_ml_conn_lang,
KEYWORD_ml_conn_dnet,
KEYWORD_ml_conn_java,
KEYWORD_ml_conn_chk,
KEYWORD_local,
KEYWORD_temporary,
KEYWORD_drop,
KEYWORD_view,
KEYWORD_synonym,
KEYWORD_handler,
KEYWORD_comment,
KEYWORD_create,
KEYWORD_go
} keywordId;
/*
* Used to determine whether keyword is valid for the token language and
* what its ID is.
*/
typedef struct sKeywordDesc {
const char *name;
keywordId id;
} keywordDesc;
typedef enum eTokenType {
TOKEN_UNDEFINED,
TOKEN_BLOCK_LABEL_BEGIN,
TOKEN_BLOCK_LABEL_END,
TOKEN_CHARACTER,
TOKEN_CLOSE_PAREN,
TOKEN_SEMICOLON,
TOKEN_COMMA,
TOKEN_IDENTIFIER,
TOKEN_KEYWORD,
TOKEN_OPEN_PAREN,
TOKEN_OPERATOR,
TOKEN_OTHER,
TOKEN_STRING,
TOKEN_PERIOD,
TOKEN_OPEN_CURLY,
TOKEN_CLOSE_CURLY,
TOKEN_OPEN_SQUARE,
TOKEN_CLOSE_SQUARE,
TOKEN_TILDE,
TOKEN_FORWARD_SLASH
} tokenType;
typedef struct sTokenInfoSQL {
tokenType type;
keywordId keyword;
vString * string;
vString * scope;
int begin_end_nest_lvl;
unsigned long lineNumber;
fpos_t filePosition;
} tokenInfo;
/*
* DATA DEFINITIONS
*/
static langType Lang_sql;
static jmp_buf Exception;
typedef enum {
SQLTAG_CURSOR,
SQLTAG_PROTOTYPE,
SQLTAG_FUNCTION,
SQLTAG_FIELD,
SQLTAG_LOCAL_VARIABLE,
SQLTAG_BLOCK_LABEL,
SQLTAG_PACKAGE,
SQLTAG_PROCEDURE,
SQLTAG_RECORD,
SQLTAG_SUBTYPE,
SQLTAG_TABLE,
SQLTAG_TRIGGER,
SQLTAG_VARIABLE,
SQLTAG_INDEX,
SQLTAG_EVENT,
SQLTAG_PUBLICATION,
SQLTAG_SERVICE,
SQLTAG_DOMAIN,
SQLTAG_VIEW,
SQLTAG_SYNONYM,
SQLTAG_MLTABLE,
SQLTAG_MLCONN,
SQLTAG_COUNT
} sqlKind;
static kindOption SqlKinds [] = {
{ TRUE, 'c', "cursor", "cursors" },
{ FALSE, 'd', "prototype", "prototypes" },
{ TRUE, 'f', "function", "functions" },
{ TRUE, 'F', "field", "record fields" },
{ FALSE, 'l', "local", "local variables" },
{ TRUE, 'L', "label", "block label" },
{ TRUE, 'P', "package", "packages" },
{ TRUE, 'p', "procedure", "procedures" },
{ FALSE, 'r', "record", "records" },
{ TRUE, 's', "subtype", "subtypes" },
{ TRUE, 't', "table", "tables" },
{ TRUE, 'T', "trigger", "triggers" },
{ TRUE, 'v', "variable", "variables" },
{ TRUE, 'i', "index", "indexes" },
{ TRUE, 'e', "event", "events" },
{ TRUE, 'U', "publication", "publications" },
{ TRUE, 'R', "service", "services" },
{ TRUE, 'D', "domain", "domains" },
{ TRUE, 'V', "view", "views" },
{ TRUE, 'n', "synonym", "synonyms" },
{ TRUE, 'x', "mltable", "MobiLink Table Scripts" },
{ TRUE, 'y', "mlconn", "MobiLink Conn Scripts" }
};
static const keywordDesc SqlKeywordTable [] = {
/* keyword keyword ID */
{ "as", KEYWORD_is },
{ "is", KEYWORD_is },
{ "begin", KEYWORD_begin },
{ "body", KEYWORD_body },
{ "cursor", KEYWORD_cursor },
{ "declare", KEYWORD_declare },
{ "end", KEYWORD_end },
{ "function", KEYWORD_function },
{ "if", KEYWORD_if },
{ "loop", KEYWORD_loop },
{ "case", KEYWORD_case },
{ "for", KEYWORD_for },
{ "call", KEYWORD_call },
{ "package", KEYWORD_package },
{ "pragma", KEYWORD_pragma },
{ "procedure", KEYWORD_procedure },
{ "record", KEYWORD_record },
{ "object", KEYWORD_object },
{ "ref", KEYWORD_ref },
{ "rem", KEYWORD_rem },
{ "return", KEYWORD_return },
{ "returns", KEYWORD_returns },
{ "subtype", KEYWORD_subtype },
{ "table", KEYWORD_table },
{ "trigger", KEYWORD_trigger },
{ "type", KEYWORD_type },
{ "index", KEYWORD_index },
{ "event", KEYWORD_event },
{ "publication", KEYWORD_publication },
{ "service", KEYWORD_service },
{ "domain", KEYWORD_domain },
{ "datatype", KEYWORD_datatype },
{ "result", KEYWORD_result },
{ "url", KEYWORD_url },
{ "internal", KEYWORD_internal },
{ "external", KEYWORD_external },
{ "when", KEYWORD_when },
{ "then", KEYWORD_then },
{ "variable", KEYWORD_variable },
{ "exception", KEYWORD_exception },
{ "at", KEYWORD_at },
{ "on", KEYWORD_on },
{ "primary", KEYWORD_primary },
{ "references", KEYWORD_references },
{ "unique", KEYWORD_unique },
{ "check", KEYWORD_check },
{ "constraint", KEYWORD_constraint },
{ "foreign", KEYWORD_foreign },
{ "ml_add_table_script", KEYWORD_ml_table },
{ "ml_add_lang_table_script", KEYWORD_ml_table_lang },
{ "ml_add_dnet_table_script", KEYWORD_ml_table_dnet },
{ "ml_add_java_table_script", KEYWORD_ml_table_java },
{ "ml_add_lang_table_script_chk", KEYWORD_ml_table_chk },
{ "ml_add_connection_script", KEYWORD_ml_conn },
{ "ml_add_lang_connection_script", KEYWORD_ml_conn_lang },
{ "ml_add_dnet_connection_script", KEYWORD_ml_conn_dnet },
{ "ml_add_java_connection_script", KEYWORD_ml_conn_java },
{ "ml_add_lang_conn_script_chk", KEYWORD_ml_conn_chk },
{ "local", KEYWORD_local },
{ "temporary", KEYWORD_temporary },
{ "drop", KEYWORD_drop },
{ "view", KEYWORD_view },
{ "synonym", KEYWORD_synonym },
{ "handler", KEYWORD_handler },
{ "comment", KEYWORD_comment },
{ "create", KEYWORD_create },
{ "go", KEYWORD_go }
};
/*
* FUNCTION DECLARATIONS
*/
/* Recursive calls */
static void parseBlock (tokenInfo *const token, const boolean local);
static void parseKeywords (tokenInfo *const token);
static void parseSqlFile (tokenInfo *const token);
/*
* FUNCTION DEFINITIONS
*/
static boolean isIdentChar1 (const int c)
{
/*
* Other databases are less restrictive on the first character of
* an identifier.
* isIdentChar1 is used to identify the first character of an
* identifier, so we are removing some restrictions.
*/
return (boolean)
(isalpha (c) || c == '@' || c == '_' );
}
static boolean isIdentChar (const int c)
{
return (boolean)
(isalpha (c) || isdigit (c) || c == '$' ||
c == '@' || c == '_' || c == '#');
}
static boolean isCmdTerm (tokenInfo *const token)
{
DebugStatement (
debugPrintf (DEBUG_PARSE
, "\n isCmdTerm: token same tt:%d tk:%d\n"
, token->type
, token->keyword
);
);
/*
* Based on the various customer sites I have been at
* the most common command delimiters are
* ;
* ~
* /
* go
* This routine will check for any of these, more
* can easily be added by modifying readToken and
* either adding the character to:
* enum eTokenType
* enum eTokenType
*/
return ( isType (token, TOKEN_SEMICOLON) ||
isType (token, TOKEN_TILDE) ||
isType (token, TOKEN_FORWARD_SLASH) ||
isKeyword (token, KEYWORD_go)
);
}
static boolean isMatchedEnd(tokenInfo *const token, int nest_lvl)
{
boolean terminated = FALSE;
/*
* Since different forms of SQL allow the use of
* BEGIN
* ...
* END
* blocks, some statements may not be terminated using
* the standard delimiters:
* ;
* ~
* /
* go
* This routine will check to see if we encounter and END
* for the matching nest level of BEGIN ... END statements.
* If we find one, then we can assume, the statement was terminated
* since we have fallen through to the END statement of the BEGIN
* block.
*/
if ( nest_lvl > 0 && isKeyword (token, KEYWORD_end) )
{
if ( token->begin_end_nest_lvl == nest_lvl )
terminated = TRUE;
}
return terminated;
}
static void buildSqlKeywordHash (void)
{
const size_t count = sizeof (SqlKeywordTable) /
sizeof (SqlKeywordTable [0]);
size_t i;
for (i = 0 ; i < count ; ++i)
{
const keywordDesc* const p = &SqlKeywordTable [i];
addKeyword (p->name, Lang_sql, (int) p->id);
}
}
static tokenInfo *newToken (void)
{
tokenInfo *const token = xMalloc (1, tokenInfo);
token->type = TOKEN_UNDEFINED;
token->keyword = KEYWORD_NONE;
token->string = vStringNew ();
token->scope = vStringNew ();
token->begin_end_nest_lvl = 0;
token->lineNumber = getSourceLineNumber ();
token->filePosition = getInputFilePosition ();
return token;
}
static void deleteToken (tokenInfo *const token)
{
vStringDelete (token->string);
vStringDelete (token->scope);
eFree (token);
}
/*
* Tag generation functions
*/
static void makeConstTag (tokenInfo *const token, const sqlKind kind)
{
if (SqlKinds [kind].enabled)
{
const char *const name = vStringValue (token->string);
tagEntryInfo e;
initTagEntry (&e, name);
e.lineNumber = token->lineNumber;
e.filePosition = token->filePosition;
e.kindName = SqlKinds [kind].name;
e.kind = SqlKinds [kind].letter;
makeTagEntry (&e);
}
}
static void makeSqlTag (tokenInfo *const token, const sqlKind kind)
{
vString * fulltag;
if (SqlKinds [kind].enabled)
{
/*
* If a scope has been added to the token, change the token
* string to include the scope when making the tag.
*/
if ( vStringLength(token->scope) > 0 )
{
fulltag = vStringNew ();
vStringCopy(fulltag, token->scope);
vStringCatS (fulltag, ".");
vStringCatS (fulltag, vStringValue(token->string));
vStringTerminate(fulltag);
vStringCopy(token->string, fulltag);
vStringDelete (fulltag);
}
makeConstTag (token, kind);
}
}
/*
* Parsing functions
*/
static void parseString (vString *const string, const int delimiter)
{
boolean end = FALSE;
while (! end)
{
int c = fileGetc ();
if (c == EOF)
end = TRUE;
/*
else if (c == '\\')
{
c = fileGetc(); // This maybe a ' or ". //
vStringPut(string, c);
}
*/
else if (c == delimiter)
end = TRUE;
else
vStringPut (string, c);
}
vStringTerminate (string);
}
/* Read a C identifier beginning with "firstChar" and places it into "name".
*/
static void parseIdentifier (vString *const string, const int firstChar)
{
int c = firstChar;
Assert (isIdentChar1 (c));
do
{
vStringPut (string, c);
c = fileGetc ();
} while (isIdentChar (c));
vStringTerminate (string);
if (!isspace (c))
fileUngetc (c); /* unget non-identifier character */
}
static void readToken (tokenInfo *const token)
{
int c;
token->type = TOKEN_UNDEFINED;
token->keyword = KEYWORD_NONE;
vStringClear (token->string);
getNextChar:
do
{
c = fileGetc ();
token->lineNumber = getSourceLineNumber ();
token->filePosition = getInputFilePosition ();
/*
* Added " to the list of ignores, not sure what this
* might break but it gets by this issue:
* create table "t1" (...)
*
* Darren, the code passes all my tests for both
* Oracle and SQL Anywhere, but maybe you can tell me
* what this may effect.
*/
}
while (c == '\t' || c == ' ' || c == '\n');
switch (c)
{
case EOF: longjmp (Exception, (int)ExceptionEOF); break;
case '(': token->type = TOKEN_OPEN_PAREN; break;
case ')': token->type = TOKEN_CLOSE_PAREN; break;
case ';': token->type = TOKEN_SEMICOLON; break;
case '.': token->type = TOKEN_PERIOD; break;
case ',': token->type = TOKEN_COMMA; break;
case '{': token->type = TOKEN_OPEN_CURLY; break;
case '}': token->type = TOKEN_CLOSE_CURLY; break;
case '~': token->type = TOKEN_TILDE; break;
case '[': token->type = TOKEN_OPEN_SQUARE; break;
case ']': token->type = TOKEN_CLOSE_SQUARE; break;
case '\'':
case '"':
token->type = TOKEN_STRING;
parseString (token->string, c);
token->lineNumber = getSourceLineNumber ();
token->filePosition = getInputFilePosition ();
break;
case '-':
c = fileGetc ();
if (c == '-') /* -- is this the start of a comment? */
{
fileSkipToCharacter ('\n');
goto getNextChar;
}
else
{
if (!isspace (c))
fileUngetc (c);
token->type = TOKEN_OPERATOR;
}
break;
case '<':
case '>':
{
const int initial = c;
int d = fileGetc ();
if (d == initial)
{
if (initial == '<')
token->type = TOKEN_BLOCK_LABEL_BEGIN;
else
token->type = TOKEN_BLOCK_LABEL_END;
}
else
{
fileUngetc (d);
token->type = TOKEN_UNDEFINED;
}
break;
}
case '\\':
c = fileGetc ();
if (c != '\\' && c != '"' && c != '\'' && !isspace (c))
fileUngetc (c);
token->type = TOKEN_CHARACTER;
token->lineNumber = getSourceLineNumber ();
token->filePosition = getInputFilePosition ();
break;
case '/':
{
int d = fileGetc ();
if ( (d != '*') && /* is this the start of a comment? */
(d != '/') ) /* is a one line comment? */
{
token->type = TOKEN_FORWARD_SLASH;
fileUngetc (d);
}
else
{
if (d == '*')
{
do
{
fileSkipToCharacter ('*');
c = fileGetc ();
if (c == '/')
break;
else
fileUngetc (c);
} while (c != EOF && c != '\0');
goto getNextChar;
}
else if (d == '/') /* is this the start of a comment? */
{
fileSkipToCharacter ('\n');
goto getNextChar;
}
}
break;
}
default:
if (! isIdentChar1 (c))
token->type = TOKEN_UNDEFINED;
else
{
parseIdentifier (token->string, c);
token->lineNumber = getSourceLineNumber ();
token->filePosition = getInputFilePosition ();
token->keyword = analyzeToken (token->string, Lang_sql);
if (isKeyword (token, KEYWORD_rem))
{
vStringClear (token->string);
fileSkipToCharacter ('\n');
goto getNextChar;
}
else if (isKeyword (token, KEYWORD_NONE))
token->type = TOKEN_IDENTIFIER;
else
token->type = TOKEN_KEYWORD;
}
break;
}
}
/*
* Token parsing functions
*/
/*
* static void addContext (tokenInfo* const parent, const tokenInfo* const child)
* {
* if (vStringLength (parent->string) > 0)
* {
* vStringCatS (parent->string, ".");
* }
* vStringCatS (parent->string, vStringValue(child->string));
* vStringTerminate(parent->string);
* }
*/
static void addToScope (tokenInfo* const token, vString* const extra)
{
if (vStringLength (token->scope) > 0)
{
vStringCatS (token->scope, ".");
}
vStringCatS (token->scope, vStringValue(extra));
vStringTerminate(token->scope);
}
/*
* Scanning functions
*/
static void findToken (tokenInfo *const token, const tokenType type)
{
while (! isType (token, type))
{
readToken (token);
}
}
static void findCmdTerm (tokenInfo *const token, const boolean check_first)
{
int begin_end_nest_lvl = token->begin_end_nest_lvl;
if ( check_first )
{
if ( isCmdTerm(token) )
return;
}
do
{
readToken (token);
} while ( !isCmdTerm(token) && !isMatchedEnd(token, begin_end_nest_lvl) );
}
static void skipToMatched(tokenInfo *const token)
{
int nest_level = 0;
tokenType open_token;
tokenType close_token;
switch (token->type)
{
case TOKEN_OPEN_PAREN:
open_token = TOKEN_OPEN_PAREN;
close_token = TOKEN_CLOSE_PAREN;
break;
case TOKEN_OPEN_CURLY:
open_token = TOKEN_OPEN_CURLY;
close_token = TOKEN_CLOSE_CURLY;
break;
case TOKEN_OPEN_SQUARE:
open_token = TOKEN_OPEN_SQUARE;
close_token = TOKEN_CLOSE_SQUARE;
break;
default:
return;
}
/*
* This routine will skip to a matching closing token.
* It will also handle nested tokens like the (, ) below.
* ( name varchar(30), text binary(10) )
*/
if (isType (token, open_token))
{
nest_level++;
while (! (isType (token, close_token) && (nest_level == 0)))
{
readToken (token);
if (isType (token, open_token))
{
nest_level++;
}
if (isType (token, close_token))
{
if (nest_level > 0)
{
nest_level--;
}
}
}
readToken (token);
}
}
static void skipArgumentList (tokenInfo *const token)
{
/*
* Other databases can have arguments with fully declared
* datatypes:
* ( name varchar(30), text binary(10) )
* So we must check for nested open and closing parantheses
*/
if (isType (token, TOKEN_OPEN_PAREN)) /* arguments? */
{
skipToMatched (token);
}
}
static void parseSubProgram (tokenInfo *const token)
{
tokenInfo *const name = newToken ();
/*
* This must handle both prototypes and the body of
* the procedures.
*
* Prototype:
* FUNCTION func_name RETURN integer;
* PROCEDURE proc_name( parameters );
* Procedure
* FUNCTION GET_ML_USERNAME RETURN VARCHAR2
* IS
* BEGIN
* RETURN v_sync_user_id;
* END GET_ML_USERNAME;
*
* PROCEDURE proc_name( parameters )
* IS
* BEGIN
* END;
* CREATE PROCEDURE proc_name( parameters )
* EXTERNAL NAME ... ;
* CREATE PROCEDURE proc_name( parameters )
* BEGIN
* END;
*
* CREATE FUNCTION f_GetClassName(
* IN @object VARCHAR(128)
* ,IN @code VARCHAR(128)
* )
* RETURNS VARCHAR(200)
* DETERMINISTIC
* BEGIN
*
* IF( @object = 'user_state' ) THEN
* SET something = something;
* END IF;
*
* RETURN @name;
* END;
*/
const sqlKind kind = isKeyword (token, KEYWORD_function) ?
SQLTAG_FUNCTION : SQLTAG_PROCEDURE;
Assert (isKeyword (token, KEYWORD_function) ||
isKeyword (token, KEYWORD_procedure));
readToken (name);
readToken (token);
if (isType (token, TOKEN_PERIOD))
{
readToken (name);
readToken (token);
}
if (isType (token, TOKEN_OPEN_PAREN))
{
/* Reads to the next token after the TOKEN_CLOSE_PAREN */
skipArgumentList(token);
}
if (kind == SQLTAG_FUNCTION)
{
if (isKeyword (token, KEYWORD_return) || isKeyword (token, KEYWORD_returns))
{
/* Read datatype */
readToken (token);
/*
* Read token after which could be the
* command terminator if a prototype
* or an open parantheses
*/
readToken (token);
if (isType (token, TOKEN_OPEN_PAREN))
{
/* Reads to the next token after the TOKEN_CLOSE_PAREN */
skipArgumentList(token);
}
}
}
if( isCmdTerm (token) )
{
makeSqlTag (name, SQLTAG_PROTOTYPE);
}
else
{
while (!(isKeyword (token, KEYWORD_is) ||
isKeyword (token, KEYWORD_begin) ||
isKeyword (token, KEYWORD_at) ||
isKeyword (token, KEYWORD_internal) ||
isKeyword (token, KEYWORD_external) ||
isKeyword (token, KEYWORD_url) ||
isCmdTerm (token)
)
)
{
if ( isKeyword (token, KEYWORD_result) )
{
readToken (token);
if (isType (token, TOKEN_OPEN_PAREN))
{
/* Reads to the next token after the TOKEN_CLOSE_PAREN */
skipArgumentList(token);
}
} else {
readToken (token);
}
}
if (isKeyword (token, KEYWORD_at) ||
isKeyword (token, KEYWORD_url) ||
isKeyword (token, KEYWORD_internal) ||
isKeyword (token, KEYWORD_external) )
{
addToScope(token, name->string);
if (isType (name, TOKEN_IDENTIFIER) ||
isType (name, TOKEN_STRING) ||
!isKeyword (token, KEYWORD_NONE)
)
makeSqlTag (name, kind);
vStringClear (token->scope);
}
if (isKeyword (token, KEYWORD_is) ||
isKeyword (token, KEYWORD_begin) )
{
addToScope(token, name->string);
if (isType (name, TOKEN_IDENTIFIER) ||
isType (name, TOKEN_STRING) ||
!isKeyword (token, KEYWORD_NONE)
)
makeSqlTag (name, kind);
parseBlock (token, TRUE);
vStringClear (token->scope);
}
}
deleteToken (name);
}
static void parseRecord (tokenInfo *const token)
{
/*
* Make it a bit forgiving, this is called from
* multiple functions, parseTable, parseType
*/
if (!isType (token, TOKEN_OPEN_PAREN))
readToken (token);
Assert (isType (token, TOKEN_OPEN_PAREN));
do
{
if ( isType (token, TOKEN_COMMA) || isType (token, TOKEN_OPEN_PAREN) )
readToken (token);
/*
* Create table statements can end with various constraints
* which must be excluded from the SQLTAG_FIELD.
* create table t1 (
* c1 integer,
* c2 char(30),
* c3 numeric(10,5),
* c4 integer,
* constraint whatever,
* primary key(c1),
* foreign key (),
* check ()
* )
*/
if (! (isKeyword(token, KEYWORD_primary) ||
isKeyword(token, KEYWORD_references) ||
isKeyword(token, KEYWORD_unique) ||
isKeyword(token, KEYWORD_check) ||
isKeyword(token, KEYWORD_constraint) ||
isKeyword(token, KEYWORD_foreign) ) )
{
if (isType (token, TOKEN_IDENTIFIER) ||
isType (token, TOKEN_STRING))
makeSqlTag (token, SQLTAG_FIELD);
}
while (!(isType (token, TOKEN_COMMA) ||
isType (token, TOKEN_CLOSE_PAREN) ||
isType (token, TOKEN_OPEN_PAREN)
))
{
readToken (token);
/*
* A table structure can look like this:
* create table t1 (
* c1 integer,
* c2 char(30),
* c3 numeric(10,5),
* c4 integer
* )
* We can't just look for a COMMA or CLOSE_PAREN
* since that will not deal with the numeric(10,5)
* case. So we need to skip the argument list
* when we find an open paren.
*/
if (isType (token, TOKEN_OPEN_PAREN))
{
/* Reads to the next token after the TOKEN_CLOSE_PAREN */
skipArgumentList(token);
}
}
} while (! isType (token, TOKEN_CLOSE_PAREN));
}
static void parseType (tokenInfo *const token)
{
tokenInfo *const name = newToken ();
vString * saveScope = vStringNew ();
vStringCopy(saveScope, token->scope);
/* If a scope has been set, add it to the name */
addToScope (name, token->scope);
readToken (name);
if (isType (name, TOKEN_IDENTIFIER))
{
readToken (token);