[go: up one dir, main page]

File: tosqlparse.cpp

package info (click to toggle)
tora 1.3.4-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 8,632 kB
  • ctags: 7,487
  • sloc: cpp: 68,518; perl: 1,475; ansic: 291; sh: 173; makefile: 51
file content (1144 lines) | stat: -rw-r--r-- 28,673 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
//***************************************************************************
/*
 * TOra - An Oracle Toolkit for DBA's and developers
 * Copyright (C) 2000-2001,2001 Underscore AB
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation;  only version 2 of
 * the License is valid for this program.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 *      As a special exception, you have permission to link this program
 *      with the Oracle Client libraries and distribute executables, as long
 *      as you follow the requirements of the GNU GPL in regard to all of the
 *      software in the executable aside from Oracle client libraries.
 *
 *      Specifically you are not permitted to link this program with the
 *      Qt/UNIX, Qt/Windows or Qt Non Commercial products of TrollTech.
 *      And you are not permitted to distribute binaries compiled against
 *      these libraries without written consent from Underscore AB. Observe
 *      that this does not disallow linking to the Qt Free Edition.
 *
 * All trademarks belong to their respective owners.
 *
 ****************************************************************************/

#include "utils.h"

#include "tohighlightedtext.h"
#include "tosqlparse.h"

#ifdef TOPARSE_DEBUG

#include <stdio.h>

bool toMonolithic(void)
{
  return false;
}

void printStatement(toSQLParse::statement &stat,int level)
{
  for (int i=0;i<level;i++)
    printf(" ");

  switch(stat.Type) {
  case toSQLParse::statement::Block:
    printf("Block:");
    break;
  case toSQLParse::statement::Statement:
    printf("Statement:");
    break;
  case toSQLParse::statement::List:
    printf("List:");
    break;
  case toSQLParse::statement::Keyword:
    printf("Keyword:");
    break;
  case toSQLParse::statement::Token:
    printf("Token:");
    break;
  case toSQLParse::statement::Raw:
    printf("Raw:");
    break;
  }
  printf("%s\n",(const char *)stat.String);
  if (!stat.Comment.isNull()) {
    for (int i=0;i<level;i++)
      printf(" ");
    printf("Comment:%s\n",(const char *)stat.Comment);
  }
  for(std::list<toSQLParse::statement>::iterator i=stat.subTokens().begin();
      i!=stat.subTokens().end();
      i++)
    printStatement(*i,level+1);
}

int main(int argc,char **argv) {
  QString res="
CREATE or REPLACE Procedure TEST_SPR
(
    IN_TICKET_NUM   IN  VARCHAR2
)
IS

BEGIN

BEGIN

for cur_rec in (select emp_id from employees) loop

	update employees set emp_id = emp_id + 1
	where emp_id = cur_rec.emp_id;
	commit;
end loop;
	
END;
END TEST_SPR;
"
#if 1
"


SELECT owner,
       OBJECT,
       TYPE FROM v$access
 WHERE sid=:f1<char[101]>
 ORDER BY owner,
	  OBJECT,
	  TYPE;
DECLARE
grade CHAR(1);
appraisal VARCHAR2(20);
BEGIN
appraisal := 
CASE grade
WHEN 'A' THEN 'Excellent'
WHEN 'B' THEN 'Very Good'
WHEN 'C' THEN 'Good'
WHEN 'D' THEN 'Fair'
WHEN 'F' THEN 'Poor'
ELSE 'No such grade'
END;
END;

CREATE TABLE ess.EssCalLog (
        CalID		CHAR(5) NOT NULL,		-- Calender type
	SeqID		NUMBER(8) NOT NULL,
	ActStt		CHAR(1) NOT NULL
		CONSTRAINT EssCalLog_CK_ActStt CHECK (ActStt IN ('A','D') ),
	LogRun		CHAR(1) NOT NULL		-- Should runs of this type be logged
		CONSTRAINT EssCalLog_CK_LogRun CHECK (LogRun IN ('Y','N') ),
	PrcID		NUMBER(8) NOT NULL
		CONSTRAINT EssCalDay_FK_PrcID REFERENCES ess.EssPrc(PrcID),
	Dsc		VARCHAR2(4000) NOT NULL,	-- Description of this type
	CONSTRAINT EssCal_PK PRIMARY KEY (CalID,SeqID)
		USING INDEX TABLESPACE Index02 -- A Comment
);
-- Another comment

CREATE OR REPLACE procedure spTuxGetAccData (oRet                        OUT  NUMBER,
					     oNumSwt                     OUT  NUMBER)
IS
  vYear  CHAR(4);
BEGIN
    <<label>>
    DECLARE
      oTrdStt NUMBER;
    BEGIN
      oTrdStt := 0;
    END;

    EXCEPTION
        WHEN VALUE_ERROR THEN
	    oRet := 3;
	WHEN NO_DATA_FOUND THEN
	    oRet := 2;
	WHEN OTHERS THEN
	    oRet := 1;
END;
CREATE OR REPLACE procedure spTuxGetAccData as
  vYear  CHAR(4);
begin
  null;
end;
-------------------------------------------------------------------
--    EssCal, Current calendar view

CREATE VIEW ess.EssCal AS
        SELECT CalID,
	       LogRun,
	       PrcID,
	       Dsc
	  FROM ess.EssCalLog a
	 WHERE SeqID = (SELECT MAX(aa.SeqID) FROM EssCalLog aa WHERE aa.CalID = a.CalID)
	   AND ActStt = 'A';

    /* A little comment
     */
    SELECT /*+
FULL(a)
*/ a.TskCod TskCod -- Test comment
      ,a.CreEdt CreEdt,
       a.TspActOprID /* One comment OprID */ , -- Another comment
       COUNT(1) Tot,
       COUNT(a.TspActOprID) Lft,
       b.TraCod TraCod,
       SUM(b.FinAmt) FinAmt,
       TraCod
  FROM EssTsk a,EssTra b
 WHERE ((a.TspActOprID = 'Test') OR a.TspActOprID IS NULL)
   AND DECODE(a.TspActOprID,NULL,NULL,a.TskID) = b.TskID(+)
 GROUP BY a.TskCod,a.CreEdt,a.TspActOprID,b.TraCod
HAVING COUNT(a.TspActOprID) > 0;
SELECT a.Sid \"-Id\",
       a.Serial# \"-Serial#\",
       a.SchemaName \"Schema\",
       a.Status \"Status\",
       a.Server \"Server\",
       a.OsUser \"Osuser\",
       a.Machine \"Machine\",
       a.Program \"Program\",
       a.Type \"Type\",
       a.Module \"Module\",
       a.Action \"Action\",
       a.Client_Info \"Client Info\",
       b.Block_Gets \"-Block Gets\",
       b.Consistent_Gets \"-Consistent Gets\",
       b.Physical_Reads \"-Physical Reads\",
       b.Block_Changes \"-Block Changes\",
       b.Consistent_Changes \"-Consistent Changes\",
       c.Value*10 \"-CPU (ms)\",
       a.Process \"-Process\",
       a.SQL_Address||':'||SQL_Hash_Value \" SQL Address\",
       a.Prev_SQL_Addr||':'||Prev_Hash_Value \" Prev SQl Address\"
  FROM v$session a,
       v$sess_io b,
       v$sesstat c
 WHERE a.sid = b.sid(+)
   AND a.sid = c.sid(+) AND (c.statistic# = 12 OR c.statistic# IS NULL)
 ORDER BY a.Sid;
select a.TskCod TskCod,
       count(1) Tot
  from (select * from EssTsk where PrsID >= '1940') ,EssTra b
 where decode(a.TspActOprID,NULL,NULL,a.PrsID)+5 = b.PrsID(+)
 group by a.TskCod,a.CreEdt,a.TspActOprID,b.TraCod
having count(a.TspActOprID) > 0;

CREATE OR REPLACE procedure spTuxGetAccData (oRet OUT  NUMBER)
AS
  vYear  CHAR(4);
BEGIN
    DECLARE
      oTrdStt NUMBER;
    BEGIN
      oTrdStt := 0;
    END;
    EXCEPTION
        WHEN VALUE_ERROR THEN
	    oRet := 3;
END;"
#endif
  ;
#if 0

  QApplication test(argc,argv);
  toMarkedText text(NULL);
  text.setText(res);

  int pos;
  int line;

  printf("\nForward string without comments\n");
  pos=0;
  for (QString ret=toSQLParse::getToken(res,pos,true,false);
       !ret.isNull();
       ret=toSQLParse::getToken(res,pos,true,false)) {
    printf("Token:%s\n",(const char *)ret);
  }

  printf("\nForward editor without comments\n");
  line=0;
  pos=0;
  for (QString ret=toSQLParse::getToken(&text,line,pos,true,false);
       !ret.isNull();
       ret=toSQLParse::getToken(&text,line,pos,true,false)) {
    printf("Token:%s\n",(const char *)ret);
  }

  printf("\nForward string with comments\n");
  pos=0;
  for (QString ret=toSQLParse::getToken(res,pos,true,true);
       !ret.isNull();
       ret=toSQLParse::getToken(res,pos,true,true)) {
    printf("Token:%s\n",(const char *)ret);
  }

  printf("\nForward editor with comments\n");
  line=0;
  pos=0;
  for (QString ret=toSQLParse::getToken(&text,line,pos,true,true);
       !ret.isNull();
       ret=toSQLParse::getToken(&text,line,pos,true,true)) {
    printf("Token:%s\n",(const char *)ret);
  }

  printf("\nBackward string without comments (Shouldn't work with --)\n");
  pos=res.length();
  for (QString ret=toSQLParse::getToken(res,pos,false,false);
       !ret.isNull();
       ret=toSQLParse::getToken(res,pos,false,false)) {
    printf("Token:%s\n",(const char *)ret);
  }

  printf("\nBackward editor without comments\n");
  line=text.numLines()-1;
  pos=text.textLine(line).length();
  for (QString ret=toSQLParse::getToken(&text,line,pos,false,false);
       !ret.isNull();
       ret=toSQLParse::getToken(&text,line,pos,false,false)) {
    printf("Token:%s\n",(const char *)ret);
  }

  printf("\nBackward string with comments (Shouldn't work with --)\n");
  pos=res.length();
  for (QString ret=toSQLParse::getToken(res,pos,false,true);
       !ret.isNull();
       ret=toSQLParse::getToken(res,pos,false,true)) {
    printf("Token:%s\n",(const char *)ret);
  }

  printf("\nBackward editor with comments\n");
  line=text.numLines()-1;
  pos=text.textLine(line).length();
  for (QString ret=toSQLParse::getToken(&text,line,pos,false,true);
       !ret.isNull();
       ret=toSQLParse::getToken(&text,line,pos,false,true)) {
    printf("Token:%s\n",(const char *)ret);
  }

#endif

  std::list<toSQLParse::statement> stat=toSQLParse::parse(res);

  for(std::list<toSQLParse::statement>::iterator i=stat.begin();i!=stat.end();i++) {
    printStatement(*i,1);
  }

  printf("%s\n",(const char *)toSQLParse::indent(res));

  return 0;
}

#endif

toSQLParse::statement::statement(type ntype,const QString &token,int cline)
  : Type(ntype),String(token),Line(cline)
{
  SubTokens=NULL;
}

std::list<toSQLParse::statement> &toSQLParse::statement::subTokens(void)
{
  if (!SubTokens)
    SubTokens=new std::list<statement>;
  return *SubTokens;
}

toSQLParse::statement::~statement()
{
  delete SubTokens;
}

toSQLParse::statement::statement(const statement &stat)
{
  Type=stat.Type;
  String=stat.String;
  Comment=stat.Comment;
  if (stat.SubTokens) {
    SubTokens=new std::list<statement>;
    (*SubTokens)=(*stat.SubTokens);
  } else
    SubTokens=NULL;
}

const toSQLParse::statement &toSQLParse::statement::operator = (const statement &stat)
{
  Type=stat.Type;
  String=stat.String;
  Comment=stat.Comment;
  delete SubTokens;
  if (stat.SubTokens) {
    SubTokens=new std::list<statement>;
    (*SubTokens)=(*stat.SubTokens);
  } else
    SubTokens=NULL;
  return *this;
}

static char *Operators[]={":=",
			  "=>",
			  "||",
			  "**",
			  "<<",
			  ">>",
			  "..",
			  "<>",
			  "!=",
			  "~=",
			  "^=",
			  "<=",
			  ">=",
			  NULL};

QString toSQLParse::getToken(const QString &line,int &cline,int &pos,bool forward,bool comments)
{
  QChar c;
  QChar nc;
  QChar endString;

  enum {
    space,
    any,
    identifier,
    string,
    comment,
    label,
    bindOpen,
    bindClose
  } state=space;

  QString token;

  int inc=forward?1:-1;

  while((forward&&pos<int(line.length()))||(!forward&&pos>=1)) {
    if (!forward)
      pos--;
    c=line[pos];
    if (c=='\n')
      cline++;
    if ((forward&&pos<int(line.length()-1))||(!forward&&pos>0))
      nc=line[pos+inc];
    else
      nc='\n';
    if (state==space) {
      if(forward&&c=='-'&&nc=='-') {
	int spos=pos;
	if (forward)
	  for (pos++;pos<int(line.length())&&line[pos]!='\n';pos++)
	    ;
	if (comments)
	  return line.mid(spos,pos-spos);
	continue;
      }
      if(c=='/'&&nc=='*')
	state=comment;
      else if((forward&&c=='<'&&nc=='<')||
	      (!forward&&c=='>'&&nc=='>'))
	state=label;
      else if(!c.isSpace())
	state=any;
    }
    
    if (forward)
      pos++;
    
    if (state!=space) {
      if(forward)
	token+=c;
      else
	token.prepend(c);
      switch(state) {
      case comment:
	if(c=='*'&&nc=='/') {
	  if(forward)
	    token+=nc;
	  else
	    token.prepend(nc);
	  pos+=inc;
	  if (comments)
	    return token;
	  else {
	    state=space;
	    token=QString::null;
	  }
	}
	break;
      case label:
	if((forward&&c=='>'&&nc=='>')||
	   (!forward&&c=='<'&&nc=='<')) {
	  if(forward)
	    token+=nc;
	  else
	    token.prepend(nc);
	  pos+=inc;
	  return token;
	}
	break;
      case space:
	break;
      case bindOpen:
	if(!toIsIdent(nc)) {
	  if (nc=='<')
	    state=bindClose;
	  else
	    return token;
	}
	break;
      case bindClose:
	if (c=='>')
	  return token;
	break;
      case any:
	if (c==':'&&toIsIdent(nc)) {
	  state=bindOpen;
	} else if (toIsIdent(c)) {
	  if (!toIsIdent(nc))
	    return token;
	  state=identifier;
	} else if (c=='\''||c=='\"') {
	  endString=c;
	  state=string;
	} else {
	  for (int i=0;Operators[i];i++) {
	    if ((forward&&c==Operators[i][0]&&nc==Operators[i][1])||
		(!forward&&nc==Operators[i][0]&&c==Operators[i][1])) {
	      if(forward)
		token+=nc;
	      else
		token.prepend(nc);
	      pos+=inc;
	      break;
	    }
	  }
	  return token;
	}
	break;
      case identifier:
	if (!toIsIdent(nc))
	  return token;
	break;
      case string:
	if (c==endString)
	  return token;
	break;
      }
    }
  }
  return token;
}

QString toSQLParse::getToken(toMarkedText *text,int &curLine,int &pos,bool forward,
			     bool comments)
{
  bool first=true;
  while(curLine<int(text->numLines())&&curLine>=0) {
    QString line=text->textLine(curLine);
    if (!first) {
      if (forward)
	pos=0;
      else
	pos=line.length();
    }
    QString ret=getToken(line,pos,forward,true);
    if (!ret.isNull()) {
      if (forward) {
	QString end;
	if (ret.startsWith("/*")&&
	    (ret.at(ret.length()-2)!='*'||
	     ret.at(ret.length()-1)!='/')) {
	  end="*/";
	} else if (ret.startsWith("'")&&(ret.length()<2||ret.at(ret.length()-1)!='\'')) {
	  end="'";
	} else if (ret.startsWith("\"")&&(ret.length()<2||ret.at(ret.length()-1)!='\"')) {
	  end="\"";
	}
	if (!end.isNull()) {
	  for (curLine++;
	       curLine<int(text->numLines())&&(pos=text->textLine(curLine).find(end))<0;
	       curLine++)
	    ret+="\n"+text->textLine(curLine);
	  if (curLine<int(text->numLines())) {
	    ret+="\n"+text->textLine(curLine).mid(0,pos+2);
	    pos+=2;
	  }
	}
      } else {
	QString end;
	if (ret.length()>=2&&
	    ret.at(ret.length()-2)=='*'&&
	    ret.at(ret.length()-1)=='/'&&
	    !ret.startsWith("/*")) {
	  end="/*";
	} else if ((ret.length()>=1&&ret.at(ret.length()-1)=='\'')&&
		   (ret.length()<2||ret[0]!='\'')) {
	  end="\'";
	} else if ((ret.length()>=1&&ret.at(ret.length()-1)=='\"')&&
		   (ret.length()<2||ret.at(0)!='\"')) {
	  end="\"";
	}
	if (!end.isNull()) {
	  for (curLine--;
	       curLine>=0&&(pos=text->textLine(curLine).findRev(end))<0;
	       curLine--)
	    ret.prepend(text->textLine(curLine)+"\n");
	  if (curLine>=0) {
	    QString str=text->textLine(curLine);
	    ret.prepend(str.mid(pos,str.length()-pos)+"\n");
	  }
	}
      }
      if (comments||(!ret.startsWith("/*")&&!ret.startsWith("--")))
	return ret;
      else {
	first=true;
	continue;
      }
    }
    curLine+=(forward?1:-1);
    first=false;
  }
  return QString::null;
}

toSQLParse::statement toSQLParse::parseStatement(const QString &str,int &cline,
						 int &pos,bool declare)
{
  statement ret(statement::Statement);

  toSyntaxAnalyzer &syntax=toSyntaxAnalyzer::defaultAnalyzer();

  QString first;
  bool nokey=false;
  bool block=false;
  for (QString token=getToken(str,cline,pos,true,true);
       !token.isNull();
       token=getToken(str,cline,pos,true,true)) {
    QString upp=token.upper();
#ifdef TOPARSE_DEBUG
    printf("%s\n",(const char*)token);
#endif
    if (first.isNull()&&!token.startsWith("/*")&&!token.startsWith("--"))
      first=upp;

    if (upp=="PROCEDURE"||upp=="FUNCTION"||upp=="PACKAGE")
      block=true;

    if (first!="END"&&((first=="IF"&&upp=="THEN")||
		       upp=="LOOP"||
		       upp=="DECLARE"||
		       (block&&upp=="AS")||
		       (block&&upp=="IS")||
		       (!declare&&upp=="BEGIN"))) {
      statement blk(statement::Block);
      ret.subTokens().insert(ret.subTokens().end(),statement(statement::Keyword,token,cline));
      blk.subTokens().insert(blk.subTokens().end(),ret);
      statement cur(statement::Statement);
      bool dcl=(upp=="DECLARE"||upp=="IS"||upp=="AS");
      do {
	cur=parseStatement(str,cline,pos,dcl);
	if (cur.Type==statement::List)
	  toStatusMessage("Unbalanced parenthesis (Too many ')')");
	blk.subTokens().insert(blk.subTokens().end(),cur);
	if (cur.subTokens().begin()!=cur.subTokens().end()&&
	    (*(cur.subTokens().begin())).String.upper()=="BEGIN")
	  dcl=false;
      } while(cur.subTokens().begin()!=cur.subTokens().end()&&
	      (*cur.subTokens().begin()).String.upper()!="END");
      return blk;
    } else if (upp=="THEN"||upp=="BEGIN"||upp=="EXCEPTION") {
      ret.subTokens().insert(ret.subTokens().end(),statement(statement::Keyword,token,cline));
      return ret;
    } else if (upp==","||(syntax.reservedWord(upp)&&upp!="NOT"&&upp!="IS"&&upp!="LIKE"&&upp!="IN"&&upp!="BETWEEN"&&upp!="ASC"&&upp!="DESC"&&upp!="NULL")&&!nokey) {
      ret.subTokens().insert(ret.subTokens().end(),statement(statement::Keyword,token,cline));
      nokey=false;
    } else if (upp=="(") {
      ret.subTokens().insert(ret.subTokens().end(),statement(statement::Token,token,cline));
      statement lst=parseStatement(str,cline,pos,false);
      statement t=toPop(lst.subTokens());
      if (lst.Type!=statement::List)
	toStatusMessage("Unbalanced parenthesis (Too many '(')");
      nokey=false;
      if (first=="CREATE"&&!block) {
	statement end=parseStatement(str,cline,pos,false);
	statement blk(statement::Block);
	blk.subTokens().insert(blk.subTokens().end(),ret);
	blk.subTokens().insert(blk.subTokens().end(),lst);
	end.subTokens().insert(end.subTokens().begin(),t);
	blk.subTokens().insert(blk.subTokens().end(),end);
	return blk;
      } else {
	ret.subTokens().insert(ret.subTokens().end(),lst);
	ret.subTokens().insert(ret.subTokens().end(),t);
      }
    } else if (upp==")") {
      ret.Type=statement::List;
      ret.subTokens().insert(ret.subTokens().end(),statement(statement::Token,token,cline));
      return ret;
    } else if (upp==";") {
      ret.subTokens().insert(ret.subTokens().end(),statement(statement::Token,token,cline));
      return ret;
    } else if (upp.startsWith("/*+")||upp.startsWith("--+")) {
      QString com=token;
      if (com.startsWith("--+"))
	com="/*+ "+com.mid(3)+" */";
      ret.subTokens().insert(ret.subTokens().end(),statement(statement::Token,
							   com.simplifyWhiteSpace(),cline));
    } else if (upp.startsWith("/*")||upp.startsWith("--")) {
      if (ret.subTokens().begin()==ret.subTokens().end()) {
	if (ret.Comment.isNull())
	  ret.Comment=token;
	else
	  ret.Comment+="\n"+token;
      } else {
	QString &com=(*ret.subTokens().rbegin()).Comment;
	if (com.isEmpty())
	  com=token;
	else
	  com+="\n"+token;
      }
    } else {
      ret.subTokens().insert(ret.subTokens().end(),statement(statement::Token,token,cline));
      nokey=(token==".");
    }
    if (upp=="AS"||upp=="IS")
      first="INVALID";
  }
  return ret;
}

std::list<toSQLParse::statement> toSQLParse::parse(const QString &str)
{
  int pos=0;
  std::list<toSQLParse::statement> ret;
  statement cur(statement::Statement);
  int cline=0;
  for(cur=parseStatement(str,cline,pos,false);
      cur.subTokens().begin()!=cur.subTokens().end();
      cur=parseStatement(str,cline,pos,false)) {
    if (cur.Type==statement::List)
      toStatusMessage("Unbalanced parenthesis (Too many ')')");
    ret.insert(ret.end(),cur);
  }
  if (pos<int(str.length()))
    ret.insert(ret.end(),statement(statement::Raw,
				   str.mid(pos,str.length()-pos),cline));
  return ret;
}

#define TABSTOP 8

int toSQLParse::countIndent(const QString &txt,int &chars)
{
  int level=0;
  while(txt[chars].isSpace()&&chars<int(txt.length())) {
    char c=txt[chars];
    if (c=='\n')
      level=0;
    else if (c==' ')
      level++;
    else if (c=='\t')
      level=(level/TABSTOP+1)*TABSTOP;
    chars++;
  }
  return level;
}

toSQLParse::settings toSQLParse::Settings={true,
					   false,
					   false,
					   false,
					   true,
					   true,
					   true,
					   4,
					   60};

QString toSQLParse::indentString(int level)
{
  QString ret;
  if (Settings.ExpandSpaces) {
    for(int i=0;i<level/8;i++)
      ret+="\t";
    for(int j=0;j<level%8;j++)
      ret+=" ";
  } else
    for(int j=0;j<level;j++)
      ret+=" ";
  return ret;
}

static int CurrentColumn(const QString &txt)
{
  int pos=txt.findRev("\n");
  if (pos<0)
    pos=0;
  else
    pos++;

  int level=0;
  while(pos<int(txt.length())) {
    char c=txt[pos];
    if (c=='\n')
      level=0;
    else if (c=='\t')
      level=(level/TABSTOP+1)*TABSTOP;
    else
      level++;
    pos++;
  }
  return level;

}

static QString IndentComment(int level,int current,const QString &comment,bool endNl)
{
  bool nl=true;
  QString ret;
  if (comment.length()) {
    if (level<=current&&(level||current)) {
      ret+="\n";
      current=0;
    }
    for (unsigned int i=0;i<comment.length();i++) {
      if (!nl||!comment.at(i).isSpace()) {
	if (nl) {
	  if (current==0)
	    ret+=toSQLParse::indentString(level);
	  else {
	    while(current<level) {
	      ret+=" ";
	      current++;
	    }
	  }
	  if (comment.at(i)=="*") {
	    ret+=" ";
	    current++;
	  }
	  nl=false;
	}
	ret+=comment.at(i);
	if (comment.at(i)=='\n') {
	  current=0;
	  nl=true;
	} else
	  nl=false;
      }
    }
    if (!nl)
      ret+="\n";
  } else if (endNl) {
    ret="\n";
  }
  return ret;
}

static QString AddComment(const QString &old,const QString &comment)
{
  QString ret=old;
  if (!ret.isEmpty()&&!comment.isEmpty())
    ret+="\n";
  ret+=comment;
  return ret;
}

QString toSQLParse::indentStatement(statement &stat,int level)
{
  QString ret;
  toSyntaxAnalyzer &syntax=toSyntaxAnalyzer::defaultAnalyzer();

  switch(stat.Type) {
  default:
    throw QString("Internal error in toSQLParse, should never get here");
    break;
  case statement::Block:
    {
      ret=IndentComment(level,0,stat.Comment,false);
      int exc=0;
      for(std::list<toSQLParse::statement>::iterator i=stat.subTokens().begin();
	  i!=stat.subTokens().end();
	  i++) {
	int add=0;
	std::list<toSQLParse::statement>::iterator j=i;
	j++;
	if (i!=stat.subTokens().begin()&&
	    j!=stat.subTokens().end())
	  add=Settings.IndentLevel;
	else
	  exc=0;

	QString t;
	if ((*i).subTokens().begin()!=(*i).subTokens().
	  end())
	  t=(*(*i).subTokens().begin()).String.upper();
	if (t=="BEGIN"||t=="WHEN")
	  add=0;
	if ((*i).Type==statement::List)
	  ret+=indentString(level+add+exc);
	ret+=indentStatement(*i,level+add+exc);
	if ((*i).Type==statement::List) {
	  int i;
	  for (i=ret.length()-1;i>=0&&ret[i].isSpace();i--)
	    ;
	  ret=ret.mid(0,max(i+1,0));
	  ret+="\n";
	  ret+=indentString(level+exc);
	}
	if (t=="EXCEPTION")
	  exc=Settings.IndentLevel*2;
      }
      if (Settings.EndBlockNewline&&level!=0)
	ret+="\n";
    }
    break;
  case statement::List:
  case statement::Statement:
    int maxlev=0;
    int maxlevorig=0;
    bool useMaxLev=false;
    bool any=true;
    int current;
    bool first;
    bool noKeyBreak=false;
    bool lineList=false;
    QString comment;
    if (stat.Type==statement::Statement) {
      ret=IndentComment(level,0,stat.Comment,false);
      useMaxLev=true;
      first=true;
      current=0;
    } else {
      for(std::list<toSQLParse::statement>::iterator i=stat.subTokens().begin();
	  i!=stat.subTokens().end();) {
	if ((*i).Type!=statement::Keyword)
	  noKeyBreak=true;
	else
	  useMaxLev=true;
	break;
      }
      current=level;
      first=true;
    }
    if (useMaxLev) {
      int count=0;
      for(std::list<toSQLParse::statement>::iterator i=stat.subTokens().begin();
	  i!=stat.subTokens().end();
	  i++) {
	if (any) {
	  QString upp=(*i).String.upper();
	  if ((*i).Type==statement::Keyword&&upp!="LOOP"&&upp!="THEN"&&upp!="AS"&&upp!="IS") {
	    if (int((*i).String.length())+1>maxlev)
	      maxlev=(*i).String.length()+1;
	    count++;
	    any=false;
	  } else if (i==stat.subTokens().begin()) {
	    noKeyBreak=true;
	    break;
	  }
	} else if ((*i).Type==statement::Token)
	  any=true;
	if ((*i).Type==statement::List)
	  count++;
      }
      if (count<=1&&maxlev>0)
	maxlev--;
      maxlevorig=maxlev;
      any=true;
    }

    for(std::list<toSQLParse::statement>::iterator i=stat.subTokens().begin();
	i!=stat.subTokens().end();
	i++) {
      comment=AddComment(comment,(*i).Comment);
      QString upp=(*i).String.upper();
#ifdef TOPARSE_DEBUG
      printf("%s\n",(const char*)(*i).String);
#endif
      if ((*i).Type==statement::List) {
	if (Settings.OperatorSpace) {
	  ret+=" ";
	  current++;
	}
	QString t=indentStatement(*i,current);
	if (t.find("\n")>=0)
	  current=CurrentColumn(t);
	else
	  current+=CurrentColumn(t);
	ret+=t;
	any=true;
      } else if ((*i).String==",") {
	if (Settings.CommaBefore) {
	  ret+=IndentComment(Settings.CommentColumn,current,comment,true);
	  comment=QString::null;
	  ret+=indentString(level+maxlev-(Settings.OperatorSpace?2:1));
	  ret+=",";
	} else {
	  ret+=",";
	  ret+=IndentComment(Settings.CommentColumn,current+1,comment,true);
	  comment=QString::null;
	  ret+=indentString(level+maxlev);
	}
	current=level+maxlev;
	any=false;
	lineList=true;
      } else if ((*i).Type==statement::Keyword&&(upp=="LOOP"||upp=="THEN"||upp=="AS"||upp=="IS")) {
	if (!Settings.BlockOpenLine) {
	  if (ret.length()>0) {
	    if (toIsIdent(ret.at(ret.length()-1))||
		ret.at(ret.length()-1)=='\"'||
		ret.at(ret.length()-1)=='\''||
		Settings.OperatorSpace) {
	      ret+=" ";
	      current++;
	    }
	  }
	  ret+=Settings.KeywordUpper?(*i).String.upper():(*i).String;
	  current+=(*i).String.length();
	} else {
	  ret+=IndentComment(Settings.CommentColumn,current,comment,true);
	  comment=QString::null;
	  ret+=indentString(level);
	  ret+=Settings.KeywordUpper?(*i).String.upper():(*i).String;
	  current=level+(*i).String.length();
	}
	any=false;
      } else if (any&&(*i).Type==statement::Keyword&&!noKeyBreak) {
	if (first)
	  first=false;
	else {
	  ret+=IndentComment(Settings.CommentColumn,current,comment,true);
	  current=0;
	  comment=QString::null;
	}
	if (current==0) {
	  ret+=indentString(level);
	  current=level;
	} else
	  while(current<level) {
	    ret+=" ";
	    current++;
	  }
	maxlev=maxlevorig;
	QString word=Settings.KeywordUpper?(*i).String.upper():(*i).String;
	if (ret.length()) {
	  ret+=QString("%1").arg(word,
				 Settings.RightSeparator?maxlev-1:1-maxlev);
	  current=level+max(int(word.length()),maxlev-1);
	} else {
	  ret+=word;
	  current=level+word.length();
	}
	any=false;
	lineList=false;
      } else {
	QString t=(*i).String;
	bool add=false;
	if ((*i).Type==statement::Keyword) {
	  if (!lineList&&
	      !any&&
	      (*i).Type==statement::Keyword&&
	      !noKeyBreak&&
	      upp=="BY")
	    add=true;
	} else {
	  any=true;
	}
	if (syntax.reservedWord(upp)&&Settings.KeywordUpper)
	  t=upp;

	int extra;
	if (first) {
	  first=false;
	  any=false;
	  extra=0;
	} else {
	  if (ret.length()>0&&
	      !ret.at(ret.length()-1).isSpace()&&
	      (Settings.OperatorSpace||((toIsIdent(t[0])||
					 t[0]=='\"'||t[0]=='\'')&&
					(toIsIdent(ret.at(ret.length()-1))||
					 ret.at(ret.length()-1)=='\"'||
					 ret.at(ret.length()-1)=='\'')
					)
	       )
	      ) {
	    if (t!=";"&&
		t!="."&&
		ret.at(ret.length()-1)!='.'&&
		current!=0) {
	      current++;
	      ret+=" ";
	    }
	  } else if (ret.length()>2&&ret.at(ret.length()-2)=='*'&&ret.at(ret.length()-1)=='/') {
	    current++;
	    ret+=" ";
	  }
	  extra=maxlev;
	}
	if (current<level+maxlev) {
	  if (current==0)
	    ret+=indentString(level+maxlev);
	  else
	    while(current<level+maxlev) {
	      ret+=" ";
	      current++;
	    }
	  current=level+maxlev;
	}
	ret+=t;
	current+=t.length();
	if(t.startsWith("<<")) {
	  ret+="\n";
	  current=0;
	}

	if (add)
	  maxlev+=t.length()+1;
      }
    }
    if (stat.Type==statement::Statement) {
      ret+=IndentComment(Settings.CommentColumn,current,comment,true);
      comment=QString::null;
      if (Settings.EndBlockNewline&&
	  level==0&&
	  stat.subTokens().begin()!=stat.subTokens().end()&&
	  (*stat.subTokens().rbegin()).String==";")
	ret+="\n";
    } else if (!comment.isEmpty()) {
      ret+=IndentComment(Settings.CommentColumn,current,comment,true);
      comment=QString::null;
      ret+=indentString(level-(Settings.OperatorSpace?2:1));
    }
    break;
  }
  return ret;
}

QString toSQLParse::indent(const QString &str)
{
  std::list<toSQLParse::statement> blk=parse(str);
  int pos=0;
  int level=countIndent(str,pos);

  QString ret;
  for(std::list<toSQLParse::statement>::iterator i=blk.begin();
      i!=blk.end();
      i++) {
    ret+=indentStatement(*i,level);
  }
  pos=ret.length();
  while(pos>0&&ret[pos-1].isSpace()) {
    pos--;
  }
  return ret.mid(0,pos)+"\n";
}