Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

933 lines
43 KiB

  1. /*
  2. Copyright (c) 1995 Microsoft Corporation
  3. Module Name:
  4. sqlparse.cxx
  5. Abstract:
  6. Author:
  7. Felix Wong [t-FelixW] 16-Dec-1996
  8. ++*/
  9. #include "lexer.hxx"
  10. #include "sqltree.hxx"
  11. #include "sqlparse.hxx"
  12. //#define DEBUG_DUMPSTACK
  13. //#define DEBUG_DUMPRULE
  14. #if (defined(DEBUG_DUMPSTACK) || defined (DEBUG_DUMPRULE))
  15. #include "stdio.h"
  16. #endif
  17. #define BAIL_ON_FAILURE(hr) \
  18. if (FAILED(hr)) { \
  19. goto error; \
  20. }\
  21. // Action Table
  22. typedef struct _action{
  23. DWORD type;
  24. DWORD dwState;
  25. }action;
  26. // Rule Table
  27. typedef struct _rule{
  28. DWORD dwNumber;
  29. DWORD dwA;
  30. }rule;
  31. enum types {
  32. N,
  33. S,
  34. R,
  35. A
  36. };
  37. #define X 99
  38. action g_action[59][28] = {
  39. // ERROR ,EQ, STAR, LPARAN,RPARAN,INT, REAL, STR, UDN, COMMA, LT, GT, LE, GE, NE, SELECT,ALL, FROM, WHERE, BOOL, AND, OR, NOT, ORDER BY ASC DESC END
  40. /*00*/ { {N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{S,3 },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X } },
  41. /*01*/ { {N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{S,49},{N,X },{N,X },{N,X },{A,X } },
  42. /*02*/ { {N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X } },
  43. /*03*/ { {N,X },{N,X },{S,4 },{N,X },{N,X },{N,X },{N,X },{S,47},{S,5 },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{S,11},{R,5 },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X } },
  44. /*04*/ { {N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{R,6 },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X } },
  45. /*05*/ { {N,X },{R,10},{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{R,10},{R,10},{R,10},{R,10},{R,10},{R,10},{N,X },{N,X },{R,10},{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X } },
  46. /*06*/ { {N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{R,4 },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X } },
  47. /*07*/ { {N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{R,7 },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X } },
  48. /*08*/ { {N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{S,9 },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{R,8 },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X } },
  49. /*09*/ { {N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{S,47},{S,5 },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X } },
  50. /*10*/ { {N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{R,9 },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X } },
  51. /*11*/ { {N,X },{N,X },{S,4 },{N,X },{N,X },{N,X },{N,X },{S,47},{S,5 },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X } },
  52. /*12*/ { {N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{S,13},{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X } },
  53. /*13*/ { {N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{S,14},{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X } },
  54. /*14*/ { {N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{R,11},{N,X },{N,X },{N,X },{N,X },{R,11},{N,X },{N,X },{N,X },{R,11} },
  55. /*15*/ { {N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{S,16},{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{R,2 } },
  56. /*16*/ { {N,X },{N,X },{N,X },{N,X },{S,23},{N,X },{N,X },{S,47},{S,25},{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{S,21},{N,X },{N,X },{N,X },{N,X },{N,X } },
  57. /*17*/ { {N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{R,3 },{N,X },{N,X },{N,X },{R,3 } },
  58. /*18*/ { {N,X },{N,X },{N,X },{N,X },{R,12},{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{S,26},{N,X },{R,12},{N,X },{N,X },{N,X },{R,12} },
  59. /*19*/ { {N,X },{N,X },{N,X },{N,X },{R,14},{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{S,28},{R,14},{N,X },{R,14},{N,X },{N,X },{N,X },{R,14} },
  60. /*20*/ { {N,X },{N,X },{N,X },{N,X },{R,16},{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{R,16},{R,16},{S,21},{R,16},{N,X },{N,X },{N,X },{R,16} },
  61. /*21*/ { {N,X },{N,X },{N,X },{S,23},{N,X },{N,X },{N,X },{S,47},{S,25},{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X } },
  62. /*22*/ { {N,X },{N,X },{N,X },{N,X },{R,18},{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{R,18},{R,18},{N,X },{R,18},{N,X },{N,X },{N,X },{R,18} },
  63. /*23*/ { {N,X },{N,X },{N,X },{S,23},{N,X },{N,X },{N,X },{S,47},{S,25},{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X } },
  64. /*24*/ { {N,X },{S,38},{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{S,34},{S,35},{S,37},{S,36},{S,39},{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X } },
  65. /*25*/ { {N,X },{R,10},{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{R,10},{R,10},{R,10},{R,10},{R,10},{R,10},{N,X },{N,X },{R,10},{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X } },
  66. /*26*/ { {N,X },{N,X },{N,X },{S,23},{N,X },{N,X },{N,X },{S,47},{S,25},{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{S,21},{N,X },{N,X },{N,X },{N,X },{N,X } },
  67. /*27*/ { {N,X },{N,X },{N,X },{N,X },{R,13},{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{R,13},{N,X },{N,X },{N,X },{R,13} },
  68. /*28*/ { {N,X },{N,X },{N,X },{S,23},{N,X },{N,X },{N,X },{S,47},{S,25},{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{S,21},{N,X },{N,X },{N,X },{N,X },{N,X } },
  69. /*29*/ { {N,X },{N,X },{N,X },{N,X },{R,15},{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{R,15},{N,X },{R,15},{N,X },{N,X },{N,X },{R,15} },
  70. /*30*/ { {N,X },{N,X },{N,X },{N,X },{R,17},{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{R,17},{R,17},{N,X },{R,17},{N,X },{N,X },{N,X },{R,17} },
  71. /*31*/ { {N,X },{N,X },{N,X },{N,X },{S,32},{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X } },
  72. /*32*/ { {N,X },{N,X },{N,X },{N,X },{R,19},{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{R,19},{R,19},{N,X },{R,19},{N,X },{N,X },{N,X },{R,19} },
  73. /*33*/ { {N,X },{N,X },{N,X },{N,X },{N,X },{S,44},{S,45},{S,41},{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{S,43},{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X } },
  74. /*34*/ { {N,X },{N,X },{N,X },{N,X },{N,X },{R,21},{R,21},{R,21},{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{R,21},{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X } },
  75. /*35*/ { {N,X },{N,X },{N,X },{N,X },{N,X },{R,22},{R,22},{R,22},{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{R,22},{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X } },
  76. /*36*/ { {N,X },{N,X },{N,X },{N,X },{N,X },{R,23},{R,23},{R,23},{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{R,23},{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X } },
  77. /*37*/ { {N,X },{N,X },{N,X },{N,X },{N,X },{R,24},{R,24},{R,24},{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{R,24},{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X } },
  78. /*38*/ { {N,X },{N,X },{N,X },{N,X },{N,X },{R,25},{R,25},{R,25},{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{R,25},{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X } },
  79. /*39*/ { {N,X },{N,X },{N,X },{N,X },{N,X },{R,26},{R,26},{R,26},{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{R,26},{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X } },
  80. /*40*/ { {N,X },{N,X },{N,X },{N,X },{R,20},{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{R,20},{R,20},{N,X },{R,20},{N,X },{N,X },{N,X },{R,20} },
  81. /*41*/ { {N,X },{N,X },{N,X },{N,X },{R,27},{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{R,27},{R,27},{N,X },{R,27},{N,X },{N,X },{N,X },{R,27} },
  82. /*42*/ { {N,X },{N,X },{N,X },{N,X },{R,28},{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{R,28},{R,28},{N,X },{R,28},{N,X },{N,X },{N,X },{R,28} },
  83. /*43*/ { {N,X },{N,X },{N,X },{N,X },{R,29},{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{R,29},{R,29},{N,X },{R,29},{N,X },{N,X },{N,X },{R,29} },
  84. /*44*/ { {N,X },{N,X },{N,X },{N,X },{R,30},{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{R,30},{R,30},{N,X },{R,30},{N,X },{N,X },{N,X },{R,30} },
  85. /*45*/ { {N,X },{N,X },{N,X },{N,X },{R,31},{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{R,31},{R,31},{N,X },{R,31},{N,X },{N,X },{N,X },{R,31} },
  86. /*46*/ { {N,X },{N,X },{N,X },{N,X },{N,X}, {N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{R,5 },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X} },
  87. /*47*/ { {N,X },{R,32},{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{R,32},{R,32},{R,32},{R,32},{R,32},{R,32},{N,X },{N,X },{R,32},{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{R,32} },
  88. /*48*/ { {N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{A,X } },
  89. /*49*/ { {N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{S,50},{N,X },{N,X },{N,X } },
  90. /*50*/ { {N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{S,55},{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X } },
  91. /*51*/ { {N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{R,34} },
  92. /*52*/ { {N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{S,53},{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{R,35} },
  93. /*53*/ { {N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{S,55},{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X } },
  94. /*54*/ { {N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{R,36} },
  95. /*55*/ { {N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{R,37},{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{S,57},{S,58},{R,37} },
  96. /*56*/ { {N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{R,38},{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{R,38} },
  97. /*57*/ { {N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{R,39},{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{R,39} },
  98. /*58*/ { {N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{R,40},{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{N,X },{R,40} },
  99. };
  100. enum non_terminals {
  101. NONTERM_SEL_STMT,
  102. NONTERM_SEL_LIST_ALL,
  103. NONTERM_SEL_LIST,
  104. NONTERM_SEL_SUBLIST,
  105. NONTERM_COL_ID,
  106. NONTERM_TBL_ID,
  107. NONTERM_SRCH_COND,
  108. NONTERM_BOOL_TERM,
  109. NONTERM_BOOL_FACT,
  110. NONTERM_BOOL_PRIM,
  111. NONTERM_COMP,
  112. NONTERM_COMP_OP,
  113. NONTERM_LITERAL,
  114. NONTERM_NUM,
  115. NONTERM_STMT,
  116. NONTERM_ORDER_BY,
  117. NONTERM_SORT_SPEC_LIST,
  118. NONTERM_SORT_COL_ID,
  119. NONTERM_ORDER_SPEC
  120. };
  121. rule g_rule[] = {
  122. // 1)No. of non-terminals and terminals on the right hand side
  123. // 2)The Parent
  124. /*00*/ {0, 0, },
  125. /*01*/ {1, NONTERM_STMT },
  126. /*02*/ {4, NONTERM_SEL_STMT },
  127. /*03*/ {6, NONTERM_SEL_STMT },
  128. /*04*/ {1, NONTERM_SEL_LIST_ALL },
  129. /*05*/ {2, NONTERM_SEL_LIST_ALL },
  130. /*06*/ {1, NONTERM_SEL_LIST },
  131. /*07*/ {1, NONTERM_SEL_LIST },
  132. /*08*/ {1, NONTERM_SEL_SUBLIST },
  133. /*09*/ {3, NONTERM_SEL_SUBLIST },
  134. /*10*/ {1, NONTERM_COL_ID },
  135. /*11*/ {1, NONTERM_TBL_ID },
  136. /*12*/ {1, NONTERM_SRCH_COND },
  137. /*13*/ {3, NONTERM_SRCH_COND },
  138. /*14*/ {1, NONTERM_BOOL_TERM, },
  139. /*15*/ {3, NONTERM_BOOL_TERM },
  140. /*16*/ {1, NONTERM_BOOL_FACT },
  141. /*17*/ {2, NONTERM_BOOL_FACT },
  142. /*18*/ {1, NONTERM_BOOL_PRIM },
  143. /*19*/ {3, NONTERM_BOOL_PRIM },
  144. /*20*/ {3, NONTERM_COMP },
  145. /*21*/ {1, NONTERM_COMP_OP },
  146. /*22*/ {1, NONTERM_COMP_OP },
  147. /*23*/ {1, NONTERM_COMP_OP },
  148. /*24*/ {1, NONTERM_COMP_OP },
  149. /*25*/ {1, NONTERM_COMP_OP },
  150. /*26*/ {1, NONTERM_COMP_OP },
  151. /*27*/ {1, NONTERM_LITERAL },
  152. /*28*/ {1, NONTERM_LITERAL },
  153. /*29*/ {1, NONTERM_LITERAL },
  154. /*30*/ {1, NONTERM_NUM },
  155. /*31*/ {1, NONTERM_NUM },
  156. /*32*/ {1, NONTERM_COL_ID },
  157. /*33*/ {2, NONTERM_STMT },
  158. /*34*/ {3, NONTERM_ORDER_BY },
  159. /*35*/ {1, NONTERM_SORT_SPEC_LIST },
  160. /*36*/ {3, NONTERM_SORT_SPEC_LIST },
  161. /*37*/ {1, NONTERM_SORT_COL_ID },
  162. /*38*/ {2, NONTERM_SORT_COL_ID },
  163. /*39*/ {1, NONTERM_ORDER_SPEC },
  164. /*40*/ {1, NONTERM_ORDER_SPEC },
  165. };
  166. #ifdef DEBUG_DUMPRULE
  167. LPWSTR g_rgszRule[] = {
  168. /*00*/ L"",
  169. /*01*/ L"stmt -> sel_stmt",
  170. /*02*/ L"sel_stmt -> T_SELECT sel_list_all T_FROM tbl_id",
  171. /*03*/ L"sel_stmt -> T_SELECT sel_list_all T_FROM tbl_id T_WHERE srch_cond",
  172. /*04*/ L"sel_list_all -> sel_list",
  173. /*05*/ L"sel_list_all -> T_ALL sel_list",
  174. /*06*/ L"sel_list -> T_STAR",
  175. /*07*/ L" sel_list -> sel_sublist",
  176. /*08*/ L" sel_sublist -> col_id",
  177. /*09*/ L" sel_sublist -> col_id T_COMMA sel_sublist",
  178. /*10*/ L" col_id -> T_UD_NAME",
  179. /*11*/ L" tbl_id -> T_STRING",
  180. /*12*/ L" srch_cond -> bool_term",
  181. /*13*/ L" srch_cond -> bool_term T_OR srch_cond",
  182. /*14*/ L" bool_term -> bool_fact",
  183. /*15*/ L" bool_term -> bool_fact T_AND bool_term",
  184. /*16*/ L" bool_fact -> bool_prim",
  185. /*17*/ L" bool_fact -> T_NOT bool_prim",
  186. /*18*/ L" bool_prim -> comp",
  187. /*19*/ L" bool_prim -> T_LPARAN srch_cond T_RPARAN",
  188. /*20*/ L" comp -> col_id comp_op literal",
  189. /*21*/ L" comp_op -> T_LT",
  190. /*22*/ L" comp_op -> T_GT",
  191. /*23*/ L" comp_op -> T_GE",
  192. /*24*/ L" comp_op -> T_LE",
  193. /*25*/ L" comp_op -> T_EQ",
  194. /*26*/ L" comp_op -> T_NE",
  195. /*27*/ L" literal -> T_STRING",
  196. /*28*/ L" literal -> num",
  197. /*29*/ L" literal -> T_BOOL",
  198. /*30*/ L" num -> T_INT",
  199. /*31*/ L" num -> T_REAL",
  200. /*32*/ L" col_id -> T_STRING",
  201. /*33*/ L" stmt -> sel_stmt order_by_clause",
  202. /*34*/ L" order_by_clause ::= T_ORDER T_BY sort_spec_list",
  203. /*35*/ L" sort_spec_list ::= sort_col_id",
  204. /*36*/ L" sort_spec_list ::= sort_col_id T_COMMA sort_spec_list",
  205. /*37*/ L" sort_col_id ::= T_UD_NAME",
  206. /*38*/ L" sort_col_id ::= T_UD_NAME order_spec",
  207. /*39*/ L" order_spec ::= T_ASC",
  208. /*40*/ L" order_spec ::= T_DESC"
  209. };
  210. #endif
  211. DWORD g_goto[59][19] = {
  212. // Sel sel sel sel col tbl srch bool bool bool comp comp lit num stmt order sort sort order
  213. // stmt,lsta,lst, sub, id, id, cond,trm, fact,prim, ,op, , , by, spec, col , spec ,
  214. /*00*/ {1, X, X, X, X, X, X, X, X, X, X, X, X, X, 2, X, X, X, X, },
  215. /*01*/ {X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, 48, X, X, X, },
  216. /*02*/ {X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, },
  217. /*03*/ {X, 12, 6, 7, 8, X, X, X, X, X, X, X, X, X, X, X, X, X, X, },
  218. /*04*/ {X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, },
  219. /*05*/ {X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, },
  220. /*06*/ {X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, },
  221. /*07*/ {X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, },
  222. /*08*/ {X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, },
  223. /*09*/ {X, X, X, 10, 8, X, X, X, X, X, X, X, X, X, X, X, X, X, X, },
  224. /*10*/ {X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, },
  225. /*11*/ {X, X, 46, 7, 8, X, X, X, X, X, X, X, X, X, X, X, X, X, X, },
  226. /*12*/ {X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, },
  227. /*13*/ {X, X, X, X, X, 15, X, X, X, X, X, X, X, X, X, X, X, X, X, },
  228. /*14*/ {X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, },
  229. /*15*/ {X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, },
  230. /*16*/ {X, X, X, X, 24, X, 17, 18, 19, 20, 22, X, X, X, X, X, X, X, X, },
  231. /*17*/ {X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, },
  232. /*18*/ {X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, },
  233. /*19*/ {X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, },
  234. /*20*/ {X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, },
  235. /*21*/ {X, X, X, X, 24, X, X, X, X, 30, 22, X, X, X, X, X, X, X, X, },
  236. /*22*/ {X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, },
  237. /*23*/ {X, X, X, X, 24, X, 31, 18, 19, 20, 22, X, X, X, X, X, X, X, X, },
  238. /*24*/ {X, X, X, X, X, X, X, X, X, X, X, 33, X, X, X, X, X, X, X, },
  239. /*25*/ {X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, },
  240. /*26*/ {X, X, X, X, 24, X, 27, 18, 19, 20, 22, X, X, X, X, X, X, X, X, },
  241. /*27*/ {X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, },
  242. /*28*/ {X, X, X, X, 24, X, X, 29, 19, 20, 22, X, X, X, X, X, X, X, X, },
  243. /*29*/ {X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, },
  244. /*30*/ {X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, },
  245. /*31*/ {X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, },
  246. /*32*/ {X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, },
  247. /*33*/ {X, X, X, X, X, X, X, X, X, X, X, X, 40, 42, X, X, X, X, X, },
  248. /*34*/ {X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, },
  249. /*35*/ {X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, },
  250. /*36*/ {X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, },
  251. /*37*/ {X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, },
  252. /*38*/ {X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, },
  253. /*39*/ {X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, },
  254. /*40*/ {X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, },
  255. /*41*/ {X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, },
  256. /*42*/ {X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, },
  257. /*43*/ {X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, },
  258. /*44*/ {X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, },
  259. /*45*/ {X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, },
  260. /*46*/ {X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, },
  261. /*47*/ {X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, },
  262. /*48*/ {X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, },
  263. /*49*/ {X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, },
  264. /*50*/ {X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, 51, 52, X, },
  265. /*51*/ {X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, },
  266. /*52*/ {X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, },
  267. /*53*/ {X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, 54, 52, X, },
  268. /*54*/ {X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, },
  269. /*55*/ {X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, 56, },
  270. /*56*/ {X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, },
  271. /*57*/ {X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, },
  272. /*58*/ {X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, },
  273. };
  274. HRESULT SQLParse(
  275. LPWSTR szQuery,
  276. LPWSTR *ppszLocation,
  277. LPWSTR *ppszLDAPQuery,
  278. LPWSTR *ppszSelect,
  279. LPWSTR *ppszOrderList
  280. )
  281. {
  282. //
  283. // check input parameters
  284. //
  285. if (!ppszLocation || !ppszLDAPQuery || !ppszSelect || !ppszOrderList) {
  286. return (E_INVALIDARG);
  287. }
  288. *ppszLocation = *ppszLDAPQuery = *ppszSelect = *ppszOrderList = NULL;
  289. CStack Stack;
  290. CLexer Query(szQuery);
  291. LPWSTR lexeme;
  292. DWORD dwToken;
  293. DWORD dwState;
  294. HRESULT hr = E_ADS_INVALID_FILTER;
  295. CSyntaxNode *pSynNode;
  296. CSQLNode *pNode1 = NULL;
  297. CSQLNode *pNode2 = NULL;
  298. CSQLNode *pNode3 = NULL;
  299. CSQLString *pString = NULL, *pOrderString = NULL;
  300. // Push in State 0
  301. pSynNode = new CSyntaxNode;
  302. if (!pSynNode) {
  303. hr = E_OUTOFMEMORY;
  304. goto error;
  305. }
  306. Stack.Push(pSynNode);
  307. pSynNode = NULL;
  308. #ifdef DEBUG_DUMPSTACK
  309. Stack.Dump();
  310. #endif
  311. while (1) {
  312. // Getting information for this iteration, dwToken and dwState
  313. hr = Query.GetCurrentToken(
  314. &lexeme,
  315. &dwToken
  316. );
  317. BAIL_ON_FAILURE(hr);
  318. hr = Stack.Current(&pSynNode);
  319. BAIL_ON_FAILURE(hr);
  320. dwState = pSynNode->_dwState;
  321. pSynNode = NULL;
  322. // Analysing and processing the data
  323. if (g_action[dwState][dwToken].type == S) {
  324. pSynNode = new CSyntaxNode;
  325. if (!pSynNode) {
  326. hr = E_OUTOFMEMORY;
  327. goto error;
  328. }
  329. pSynNode->_dwState = g_action[dwState][dwToken].dwState;
  330. pSynNode->_dwToken = dwToken;
  331. switch (dwToken) {
  332. case TOKEN_STRING_LITERAL:
  333. case TOKEN_USER_DEFINED_NAME:
  334. case TOKEN_INTEGER_LITERAL:
  335. case TOKEN_REAL_LITERAL:
  336. case TOKEN_BOOLEAN_LITERAL:
  337. case TOKEN_ASC:
  338. case TOKEN_DESC:
  339. {
  340. LPWSTR szValue = AllocADsStr(lexeme);
  341. if (!szValue) {
  342. hr = E_OUTOFMEMORY;
  343. goto error;
  344. }
  345. pSynNode->SetNode(szValue);
  346. break;
  347. }
  348. }
  349. hr = Stack.Push(pSynNode);
  350. BAIL_ON_FAILURE(hr);
  351. pSynNode = NULL;
  352. hr = Query.GetNextToken(
  353. &lexeme,
  354. &dwToken
  355. );
  356. BAIL_ON_FAILURE(hr);
  357. #ifdef DUMP
  358. Stack.Dump();
  359. #endif
  360. }
  361. else if (g_action[dwState][dwToken].type == R) {
  362. DWORD dwRule = g_action[dwState][dwToken].dwState;
  363. DWORD dwNumber = g_rule[dwRule].dwNumber;
  364. #ifdef DEBUG_DUMPRULE
  365. wprintf(L"%s\n",g_rgszRule[dwRule]);
  366. #endif
  367. pSynNode = new CSyntaxNode;
  368. if (NULL == pSynNode)
  369. BAIL_ON_FAILURE(hr = E_OUTOFMEMORY);
  370. CSyntaxNode *pSynNodeRed, *pSynNodeRed2;
  371. switch (dwRule) {
  372. case 20:
  373. {
  374. DWORD dwType;
  375. hr = Stack.Pop(&pSynNodeRed);
  376. BAIL_ON_FAILURE(hr);
  377. hr = MakeLeaf(
  378. pSynNodeRed->_szValue,
  379. &pNode2
  380. );
  381. pSynNodeRed->_dwType = SNODE_NULL;
  382. delete pSynNodeRed;
  383. hr = Stack.Pop(&pSynNodeRed);
  384. BAIL_ON_FAILURE(hr);
  385. dwType = pSynNodeRed->_dwFilterType;
  386. pSynNodeRed->_dwType = SNODE_NULL;
  387. delete pSynNodeRed;
  388. hr = Stack.Pop(&pSynNodeRed);
  389. BAIL_ON_FAILURE(hr);
  390. hr = MakeLeaf(
  391. pSynNodeRed->_szValue,
  392. &pNode1
  393. );
  394. pSynNodeRed->_dwType = SNODE_NULL;
  395. delete pSynNodeRed;
  396. // New node
  397. hr = MakeNode(
  398. dwType,
  399. pNode1,
  400. pNode2,
  401. &pNode3
  402. );
  403. BAIL_ON_FAILURE(hr);
  404. pSynNode->SetNode(
  405. pNode3
  406. );
  407. pNode1 = NULL;
  408. pNode2 = NULL;
  409. pNode3 = NULL;
  410. break;
  411. }
  412. case 13:
  413. case 15:
  414. // Reduction of AND OR
  415. {
  416. CSQLNode *pNode1;
  417. CSQLNode *pNode2;
  418. CSQLNode *pNode3;
  419. DWORD dwType;
  420. hr = Stack.Pop(&pSynNodeRed);
  421. BAIL_ON_FAILURE(hr);
  422. pNode2 = pSynNodeRed->_pSQLNode;
  423. pSynNodeRed->_dwType = SNODE_NULL;
  424. delete pSynNodeRed;
  425. hr = Stack.Pop(&pSynNodeRed);
  426. BAIL_ON_FAILURE(hr);
  427. dwType = pSynNodeRed->_dwToken;
  428. pSynNodeRed->_dwType = SNODE_NULL;
  429. delete pSynNodeRed;
  430. hr = Stack.Pop(&pSynNodeRed);
  431. BAIL_ON_FAILURE(hr);
  432. pNode1 = pSynNodeRed->_pSQLNode;
  433. pSynNodeRed->_dwType = SNODE_NULL;
  434. delete pSynNodeRed;
  435. // New node
  436. hr = MakeNode(
  437. dwType,
  438. pNode1,
  439. pNode2,
  440. &pNode3
  441. );
  442. BAIL_ON_FAILURE(hr);
  443. pSynNode->SetNode(
  444. pNode3
  445. );
  446. pNode1 = NULL;
  447. pNode2 = NULL;
  448. pNode3 = NULL;
  449. break;
  450. }
  451. case 17: // Reduction of NOT
  452. {
  453. CSQLNode *pNode1;
  454. CSQLNode *pNode2;
  455. DWORD dwType;
  456. hr = Stack.Pop(&pSynNodeRed);
  457. BAIL_ON_FAILURE(hr);
  458. pNode1 = pSynNodeRed->_pSQLNode;
  459. pSynNodeRed->_dwType = SNODE_NULL;
  460. delete pSynNodeRed;
  461. hr = Stack.Pop(&pSynNodeRed);
  462. BAIL_ON_FAILURE(hr);
  463. dwType = pSynNodeRed->_dwToken;
  464. pSynNodeRed->_dwType = SNODE_NULL;
  465. delete pSynNodeRed;
  466. // New node
  467. hr = MakeNode(
  468. dwType,
  469. pNode1,
  470. NULL,
  471. &pNode2
  472. );
  473. BAIL_ON_FAILURE(hr);
  474. pSynNode->SetNode(
  475. pNode2
  476. );
  477. pNode1 = NULL;
  478. pNode2 = NULL;
  479. break;
  480. }
  481. case 3:
  482. // save entry
  483. {
  484. CSQLNode* pNode;
  485. hr = Stack.Pop(&pSynNodeRed);
  486. BAIL_ON_FAILURE(hr);
  487. pNode = pSynNodeRed->_pSQLNode;
  488. pSynNodeRed->_dwType = SNODE_NULL;
  489. delete pSynNodeRed;
  490. for (DWORD i = 0;i<dwNumber-1;i++)
  491. Stack.Pop();
  492. CSQLString *pString = new CSQLString;
  493. if (!pString) {
  494. hr = E_OUTOFMEMORY;
  495. goto error;
  496. }
  497. hr = pNode->GenerateLDAPString(
  498. pString
  499. );
  500. BAIL_ON_FAILURE(hr);
  501. delete pNode;
  502. *ppszLDAPQuery = AllocADsStr(pString->_szString);
  503. delete pString;
  504. if (!*ppszLDAPQuery) {
  505. hr = E_OUTOFMEMORY;
  506. goto error;
  507. }
  508. break;
  509. }
  510. case 11:
  511. {
  512. hr = Stack.Pop(&pSynNodeRed);
  513. BAIL_ON_FAILURE(hr);
  514. *ppszLocation = AllocADsStr(pSynNodeRed->_szValue);
  515. delete pSynNodeRed;
  516. break;
  517. }
  518. case 21:
  519. case 22:
  520. case 23:
  521. case 24:
  522. case 25:
  523. case 26:
  524. {
  525. hr = Stack.Pop(&pSynNodeRed);
  526. BAIL_ON_FAILURE(hr);
  527. pSynNode->SetNode(
  528. pSynNodeRed->_dwToken
  529. );
  530. pSynNodeRed->_dwType = SNODE_NULL;
  531. delete pSynNodeRed;
  532. break;
  533. }
  534. case 12:
  535. case 14:
  536. case 16:
  537. case 18:
  538. {
  539. // we propogate the last entry
  540. hr = Stack.Pop(&pSynNodeRed);
  541. BAIL_ON_FAILURE(hr);
  542. pSynNode->SetNode(
  543. pSynNodeRed->_pSQLNode
  544. );
  545. pSynNodeRed->_dwType = SNODE_NULL;
  546. delete pSynNodeRed;
  547. break;
  548. }
  549. case 10:
  550. case 27:
  551. case 29:
  552. case 30:
  553. case 31:
  554. case 28:
  555. case 32:
  556. case 37:
  557. case 39:
  558. case 40:
  559. {
  560. hr = Stack.Pop(&pSynNodeRed);
  561. BAIL_ON_FAILURE(hr);
  562. pSynNode->SetNode(
  563. pSynNodeRed->_szValue
  564. );
  565. pSynNodeRed->_dwType = SNODE_NULL;
  566. delete pSynNodeRed;
  567. break;
  568. }
  569. case 19:
  570. {
  571. hr = Stack.Pop();
  572. BAIL_ON_FAILURE(hr);
  573. hr = Stack.Pop(&pSynNodeRed);
  574. BAIL_ON_FAILURE(hr);
  575. pSynNode->SetNode(
  576. pSynNodeRed->_pSQLNode
  577. );
  578. pSynNodeRed->_dwType = SNODE_NULL;
  579. delete pSynNodeRed;
  580. hr = Stack.Pop();
  581. BAIL_ON_FAILURE(hr);
  582. break;
  583. }
  584. case 8:
  585. {
  586. pString = new CSQLString;
  587. if (NULL == pString)
  588. BAIL_ON_FAILURE(hr = E_OUTOFMEMORY);
  589. hr = Stack.Pop(&pSynNodeRed);
  590. BAIL_ON_FAILURE(hr);
  591. BAIL_ON_FAILURE(
  592. hr = pString->Append(pSynNodeRed->_szValue));
  593. delete pSynNodeRed;
  594. break;
  595. }
  596. case 9:
  597. {
  598. CSQLNode *pNode;
  599. hr = Stack.Pop();
  600. BAIL_ON_FAILURE(hr);
  601. hr = Stack.Pop();
  602. BAIL_ON_FAILURE(hr);
  603. hr = Stack.Pop(&pSynNodeRed);
  604. BAIL_ON_FAILURE(hr);
  605. BAIL_ON_FAILURE(
  606. hr = pString->Append(L","));
  607. BAIL_ON_FAILURE(
  608. hr = pString->Append(pSynNodeRed->_szValue));
  609. delete pSynNodeRed;
  610. break;
  611. }
  612. case 35:
  613. {
  614. pOrderString = new CSQLString;
  615. if (NULL == pOrderString)
  616. BAIL_ON_FAILURE(hr = E_OUTOFMEMORY);
  617. hr = Stack.Pop(&pSynNodeRed);
  618. BAIL_ON_FAILURE(hr);
  619. BAIL_ON_FAILURE(
  620. hr = pOrderString->Append(pSynNodeRed->_szValue));
  621. delete pSynNodeRed;
  622. break;
  623. }
  624. case 36:
  625. {
  626. CSQLNode *pNode;
  627. hr = Stack.Pop();
  628. BAIL_ON_FAILURE(hr);
  629. hr = Stack.Pop();
  630. BAIL_ON_FAILURE(hr);
  631. hr = Stack.Pop(&pSynNodeRed);
  632. BAIL_ON_FAILURE(hr);
  633. pOrderString->AppendAtBegin(L",");
  634. pOrderString->AppendAtBegin(pSynNodeRed->_szValue);
  635. delete pSynNodeRed;
  636. break;
  637. }
  638. case 38:
  639. {
  640. CSQLNode *pNode;
  641. LPWSTR pTemp = NULL;
  642. hr = Stack.Pop(&pSynNodeRed);
  643. BAIL_ON_FAILURE(hr);
  644. hr = Stack.Pop(&pSynNodeRed2);
  645. BAIL_ON_FAILURE(hr);
  646. pTemp = (LPWSTR) AllocADsMem((wcslen(pSynNodeRed2->_szValue) +
  647. wcslen(pSynNodeRed->_szValue) +
  648. wcslen(L" ") + 1) * sizeof(WCHAR));
  649. if (!pTemp) {
  650. BAIL_ON_FAILURE(hr = E_OUTOFMEMORY);
  651. }
  652. wsprintf(pTemp, L"%s %s",
  653. pSynNodeRed2->_szValue,
  654. pSynNodeRed->_szValue);
  655. pSynNode->SetNode( pTemp );
  656. //
  657. // In all other cases the _dwType of the deleted nodes is
  658. // set to SNODE_NULL before deletion because the data contained
  659. // in the node is transferred to the new node.
  660. // In this case, we have effectively copied the data (with the
  661. // wsprintf statement) and so we should not alter the _dwType of
  662. // the nodes before deletion because we do want the nodes to
  663. // clean up their data.
  664. //
  665. //pSynNodeRed->_dwType = SNODE_NULL;
  666. //pSynNodeRed2->_dwType = SNODE_NULL;
  667. delete pSynNodeRed;
  668. delete pSynNodeRed2;
  669. break;
  670. }
  671. default:
  672. {
  673. for (DWORD i = 0;i<dwNumber;i++)
  674. Stack.Pop();
  675. break;
  676. }
  677. }
  678. hr = Stack.Current(&pSynNodeRed);
  679. BAIL_ON_FAILURE(hr);
  680. dwState = pSynNodeRed->_dwState;
  681. DWORD A = g_rule[dwRule].dwA;
  682. pSynNode->_dwState = g_goto[dwState][A];
  683. pSynNode->_dwToken = A;
  684. hr = Stack.Push(pSynNode);
  685. BAIL_ON_FAILURE(hr);
  686. pSynNode = NULL;
  687. #ifdef DEBUG_DUMPSTACK
  688. Stack.Dump();
  689. #endif
  690. }
  691. else if (g_action[dwState][dwToken].type == A){
  692. if (pString == NULL) {
  693. *ppszSelect = AllocADsStr(L"*");
  694. }
  695. else {
  696. *ppszSelect = AllocADsStr(pString->_szString);
  697. }
  698. if (!*ppszSelect) {
  699. hr = E_OUTOFMEMORY;
  700. goto error;
  701. }
  702. delete pString;
  703. if (pOrderString) {
  704. *ppszOrderList = AllocADsStr(pOrderString->_szString);
  705. if (!*ppszOrderList) {
  706. hr = E_OUTOFMEMORY;
  707. goto error;
  708. }
  709. delete pOrderString;
  710. }
  711. return S_OK;
  712. }
  713. else {
  714. hr = E_ADS_INVALID_FILTER;
  715. goto error;
  716. }
  717. }
  718. error:
  719. if (pSynNode)
  720. delete pSynNode;
  721. if (pNode1)
  722. delete pNode1;
  723. if (pNode2)
  724. delete pNode2;
  725. if (pNode3)
  726. delete pNode3;
  727. if (pString)
  728. delete pString;
  729. if (pOrderString)
  730. delete pOrderString;
  731. if (*ppszSelect)
  732. {
  733. FreeADsMem(*ppszSelect);
  734. *ppszSelect = NULL;
  735. }
  736. if (*ppszLocation)
  737. {
  738. FreeADsMem(*ppszLocation);
  739. *ppszLocation = NULL;
  740. }
  741. if (*ppszLDAPQuery)
  742. {
  743. FreeADsMem(*ppszLDAPQuery);
  744. *ppszLDAPQuery = NULL;
  745. }
  746. if (*ppszOrderList)
  747. {
  748. FreeADsMem(*ppszOrderList);
  749. *ppszOrderList = NULL;
  750. }
  751. return hr;
  752. }
  753. CStack::CStack()
  754. {
  755. _dwStackIndex = 0;
  756. }
  757. CStack::~CStack()
  758. {
  759. DWORD dwIndex = _dwStackIndex;
  760. while (dwIndex > 0) {
  761. CSyntaxNode *pNode;
  762. pNode = _Stack[--dwIndex];
  763. delete pNode;
  764. }
  765. }
  766. #ifdef DEBUG_DUMPSTACK
  767. void CStack::Dump()
  768. {
  769. DWORD dwIndex = _dwStackIndex;
  770. printf("Stack:\n");
  771. while (dwIndex > 0) {
  772. CSyntaxNode *pNode;
  773. pNode = _Stack[--dwIndex];
  774. printf(
  775. "State=%5.0d, Token=%5.0d\n",
  776. pNode->_dwState,
  777. pNode->_dwToken
  778. );
  779. }
  780. }
  781. #endif
  782. HRESULT CStack::Push(CSyntaxNode* pNode)
  783. {
  784. if (_dwStackIndex < MAXVAL) {
  785. _Stack[_dwStackIndex++] = pNode;
  786. return S_OK;
  787. }
  788. else
  789. return E_FAIL;
  790. }
  791. HRESULT CStack::Pop(CSyntaxNode** ppNode)
  792. {
  793. if (_dwStackIndex > 0) {
  794. *ppNode = _Stack[--_dwStackIndex];
  795. return S_OK;
  796. }
  797. else {
  798. return E_FAIL;
  799. }
  800. }
  801. HRESULT CStack::Pop()
  802. {
  803. if (_dwStackIndex > 0) {
  804. CSyntaxNode *pNode;
  805. pNode = _Stack[--_dwStackIndex];
  806. delete pNode;
  807. return S_OK;
  808. }
  809. else {
  810. return E_FAIL;
  811. }
  812. }
  813. HRESULT CStack::Current(CSyntaxNode **ppNode)
  814. {
  815. if (_dwStackIndex > 0) {
  816. *ppNode = _Stack[_dwStackIndex-1];
  817. return S_OK;
  818. }
  819. else {
  820. return E_FAIL;
  821. }
  822. }
  823. CSyntaxNode::CSyntaxNode()
  824. {
  825. _dwType = SNODE_NULL;
  826. _dwToken = 0;
  827. _dwState = 0;
  828. _pSQLNode = 0;
  829. }
  830. CSyntaxNode::~CSyntaxNode()
  831. {
  832. switch (_dwType) {
  833. case SNODE_SZ:
  834. FreeADsStr(_szValue);
  835. break;
  836. case SNODE_SQLNODE:
  837. delete _pSQLNode;
  838. break;
  839. default:
  840. break;
  841. }
  842. }
  843. void CSyntaxNode::SetNode(
  844. CSQLNode *pSQLNode
  845. )
  846. {
  847. _pSQLNode = pSQLNode;
  848. _dwType = SNODE_SQLNODE;
  849. }
  850. void CSyntaxNode::SetNode(
  851. LPWSTR szValue
  852. )
  853. {
  854. _szValue = szValue;
  855. _dwType = SNODE_SZ;
  856. }
  857. void CSyntaxNode::SetNode(
  858. DWORD dwFilterType
  859. )
  860. {
  861. _dwFilterType = dwFilterType;
  862. _dwType = SNODE_FILTER;
  863. }