Source code of Windows XP (NT5)
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.

1293 lines
36 KiB

  1. /*++
  2. Copyright (C) 1997-2001 Microsoft Corporation
  3. Module Name:
  4. WQLSCAN.CPP
  5. Abstract:
  6. WQL Prefix Scanner
  7. This module implements a specially cased shift-reduce parser to
  8. parse out selected columns, JOINed tables and aliases, while ignoring
  9. the rest of the query.
  10. History:
  11. raymcc 17-Oct-97 SMS extensions.
  12. --*/
  13. #include "precomp.h"
  14. #include <stdio.h>
  15. #include <flexarry.h>
  16. #include <wqllex.h>
  17. #include <wqlnode.h>
  18. #include <wqlscan.h>
  19. inline wchar_t *Macro_CloneLPWSTR(LPCWSTR src)
  20. {
  21. if (!src)
  22. return 0;
  23. wchar_t *dest = new wchar_t[wcslen(src) + 1];
  24. if (!dest)
  25. return 0;
  26. return wcscpy(dest, src);
  27. }
  28. #define trace(x) printf x
  29. class CTokenArray : public CFlexArray
  30. {
  31. public:
  32. ~CTokenArray() { Empty(); }
  33. void Empty()
  34. {
  35. for (int i = 0; i < Size(); i++) delete PWSLexToken(GetAt(i));
  36. CFlexArray::Empty();
  37. }
  38. };
  39. //***************************************************************************
  40. //
  41. // CWQLScanner::CWQLScanner
  42. //
  43. // Constructor
  44. //
  45. // Parameters:
  46. // <pSrc> A source from which to lex from.
  47. //
  48. //***************************************************************************
  49. CWQLScanner::CWQLScanner(CGenLexSource *pSrc)
  50. {
  51. m_pLexer = new CGenLexer(WQL_LexTable, pSrc);
  52. m_nLine = 0;
  53. m_pTokenText = 0;
  54. m_nCurrentToken = 0;
  55. m_bCount = FALSE;
  56. }
  57. //***************************************************************************
  58. //
  59. // CWQLScanner::~CWQLScanner
  60. //
  61. //***************************************************************************
  62. CWQLScanner::~CWQLScanner()
  63. {
  64. delete m_pLexer;
  65. ClearTokens();
  66. ClearTableRefs();
  67. ClearPropRefs();
  68. }
  69. //***************************************************************************
  70. //
  71. //***************************************************************************
  72. BOOL CWQLScanner::GetReferencedAliases(CWStringArray &aAliases)
  73. {
  74. for (int i = 0; i < m_aTableRefs.Size(); i++)
  75. {
  76. WSTableRef *pTRef = (WSTableRef *) m_aTableRefs[i];
  77. aAliases.Add(pTRef->m_pszAlias);
  78. }
  79. return TRUE;
  80. }
  81. //***************************************************************************
  82. //
  83. //***************************************************************************
  84. BOOL CWQLScanner::GetReferencedTables(CWStringArray &aClasses)
  85. {
  86. for (int i = 0; i < m_aTableRefs.Size(); i++)
  87. {
  88. WSTableRef *pTRef = (WSTableRef *) m_aTableRefs[i];
  89. aClasses.Add(pTRef->m_pszTable);
  90. }
  91. return TRUE;
  92. }
  93. //***************************************************************************
  94. //
  95. //***************************************************************************
  96. void CWQLScanner::ClearTokens()
  97. {
  98. for (int i = 0; i < m_aTokens.Size(); i++)
  99. delete (WSLexToken *) m_aTokens[i];
  100. }
  101. //***************************************************************************
  102. //
  103. //***************************************************************************
  104. void CWQLScanner::ClearPropRefs()
  105. {
  106. for (int i = 0; i < m_aPropRefs.Size(); i++)
  107. delete (SWQLColRef *) m_aPropRefs[i];
  108. }
  109. //***************************************************************************
  110. //
  111. //***************************************************************************
  112. void CWQLScanner::ClearTableRefs()
  113. {
  114. for (int i = 0; i < m_aTableRefs.Size(); i++)
  115. delete (WSTableRef *) m_aTableRefs[i];
  116. m_aTableRefs.Empty();
  117. }
  118. //***************************************************************************
  119. //
  120. // Next()
  121. //
  122. // Advances to the next token and recognizes keywords, etc.
  123. //
  124. //***************************************************************************
  125. struct WqlKeyword
  126. {
  127. LPWSTR m_pKeyword;
  128. int m_nTokenCode;
  129. };
  130. static WqlKeyword KeyWords[] = // Keep this alphabetized for binary search
  131. {
  132. L"ALL", WQL_TOK_ALL,
  133. L"AND", WQL_TOK_AND,
  134. L"AS", WQL_TOK_AS,
  135. L"BETWEEN", WQL_TOK_BETWEEN,
  136. L"BY", WQL_TOK_BY,
  137. L"COUNT", WQL_TOK_COUNT,
  138. L"DATEPART", WQL_TOK_DATEPART,
  139. L"DISTINCT", WQL_TOK_DISTINCT,
  140. L"FIRSTROW", WQL_TOK_FIRSTROW,
  141. L"FROM", WQL_TOK_FROM,
  142. L"FULL", WQL_TOK_FULL,
  143. L"GROUP", WQL_TOK_GROUP,
  144. L"HAVING", WQL_TOK_HAVING,
  145. L"IN", WQL_TOK_IN,
  146. L"INNER", WQL_TOK_INNER,
  147. L"IS", WQL_TOK_IS,
  148. L"ISA", WQL_TOK_ISA,
  149. L"ISNULL", WQL_TOK_ISNULL,
  150. L"JOIN", WQL_TOK_JOIN,
  151. L"LEFT", WQL_TOK_LEFT,
  152. L"LIKE", WQL_TOK_LIKE,
  153. L"LOWER", WQL_TOK_LOWER,
  154. L"NOT", WQL_TOK_NOT,
  155. L"NULL", WQL_TOK_NULL,
  156. L"ON", WQL_TOK_ON,
  157. L"OR", WQL_TOK_OR,
  158. L"ORDER", WQL_TOK_ORDER,
  159. L"OUTER", WQL_TOK_OUTER,
  160. L"QUALIFIER", WQL_TOK_QUALIFIER,
  161. L"RIGHT", WQL_TOK_RIGHT,
  162. L"SELECT", WQL_TOK_SELECT,
  163. L"UNION", WQL_TOK_UNION,
  164. L"UPPER", WQL_TOK_UPPER,
  165. L"WHERE", WQL_TOK_WHERE
  166. };
  167. const int NumKeywords = sizeof(KeyWords)/sizeof(WqlKeyword);
  168. BOOL CWQLScanner::Next()
  169. {
  170. if (!m_pLexer)
  171. return FALSE;
  172. m_nCurrentToken = m_pLexer->NextToken();
  173. if (m_nCurrentToken == WQL_TOK_ERROR)
  174. return FALSE;
  175. m_nLine = m_pLexer->GetLineNum();
  176. m_pTokenText = m_pLexer->GetTokenText();
  177. if (m_nCurrentToken == WQL_TOK_EOF)
  178. m_pTokenText = L"<end of file>";
  179. // Keyword check. Do a binary search
  180. // on the keyword table.
  181. // =================================
  182. if (m_nCurrentToken == WQL_TOK_IDENT)
  183. {
  184. int l = 0, u = NumKeywords - 1;
  185. while (l <= u)
  186. {
  187. int m = (l + u) / 2;
  188. if (_wcsicmp(m_pTokenText, KeyWords[m].m_pKeyword) < 0)
  189. u = m - 1;
  190. else if (_wcsicmp(m_pTokenText, KeyWords[m].m_pKeyword) > 0)
  191. l = m + 1;
  192. else // Match
  193. {
  194. m_nCurrentToken = KeyWords[m].m_nTokenCode;
  195. break;
  196. }
  197. }
  198. }
  199. return TRUE;
  200. }
  201. //***************************************************************************
  202. //
  203. // CWQLScanner::ExtractNext
  204. //
  205. //***************************************************************************
  206. PWSLexToken CWQLScanner::ExtractNext()
  207. {
  208. if (m_aTokens.Size() == 0)
  209. return NULL;
  210. PWSLexToken pTok = PWSLexToken(m_aTokens[0]);
  211. m_aTokens.RemoveAt(0);
  212. return pTok;
  213. }
  214. //***************************************************************************
  215. //
  216. // CWQLScanner::Pushback
  217. //
  218. //***************************************************************************
  219. void CWQLScanner::Pushback(PWSLexToken pPushbackTok)
  220. {
  221. m_aTokens.InsertAt(0, pPushbackTok);
  222. }
  223. //***************************************************************************
  224. //
  225. // Shift-reduce parser entry.
  226. //
  227. //***************************************************************************
  228. int CWQLScanner::Parse()
  229. {
  230. int nRes = SYNTAX_ERROR;
  231. if (m_pLexer == NULL)
  232. return FAILED;
  233. m_pLexer->Reset();
  234. if (!Next())
  235. return LEXICAL_ERROR;
  236. // Completely tokenize the entire query and build a parse-stack.
  237. // =============================================================
  238. if (m_nCurrentToken == WQL_TOK_SELECT)
  239. {
  240. while (1)
  241. {
  242. WSLexToken *pTok = new WSLexToken;
  243. if (!pTok)
  244. return FAILED;
  245. pTok->m_nToken = m_nCurrentToken;
  246. pTok->m_pszTokenText = Macro_CloneLPWSTR(m_pTokenText);
  247. m_aTokens.Add(pTok);
  248. if (m_nCurrentToken == WQL_TOK_EOF)
  249. break;
  250. if (!Next())
  251. return LEXICAL_ERROR;
  252. }
  253. }
  254. else
  255. return SYNTAX_ERROR;
  256. // Reduce by extracting the select type keywords if possible.
  257. // ==========================================================
  258. nRes = ExtractSelectType();
  259. if (nRes)
  260. return nRes;
  261. // Eliminate all tokens from WHERE onwards.
  262. // ========================================
  263. StripWhereClause();
  264. // Reduce by extracting the select list.
  265. // =====================================
  266. if (!m_bCount)
  267. {
  268. nRes = SelectList();
  269. if (nRes != 0)
  270. return nRes;
  271. }
  272. else
  273. {
  274. // Strip everything until the FROM keyword is encountered.
  275. // =======================================================
  276. WSLexToken *pTok = ExtractNext();
  277. while (pTok)
  278. {
  279. if (pTok->m_nToken == WQL_TOK_FROM)
  280. {
  281. Pushback(pTok);
  282. break;
  283. }
  284. // Bug #46728: the count(*) clause
  285. // can be the only element of the select clause.
  286. else if (!wcscmp(pTok->m_pszTokenText, L","))
  287. {
  288. delete pTok;
  289. return SYNTAX_ERROR;
  290. }
  291. delete pTok;
  292. pTok = ExtractNext();
  293. }
  294. if (pTok == 0)
  295. return SYNTAX_ERROR;
  296. }
  297. // Extract tables/aliases from JOIN clauses.
  298. // =========================================
  299. if (ReduceSql89Joins() != TRUE)
  300. {
  301. ClearTableRefs();
  302. if (ReduceSql92Joins() != TRUE)
  303. return SYNTAX_ERROR;
  304. }
  305. // Post process select clause to determine if
  306. // columns are tables or aliases.
  307. // ==========================================
  308. for (int i = 0; i < m_aPropRefs.Size(); i++)
  309. {
  310. SWQLColRef *pCRef = (SWQLColRef *) m_aPropRefs[i];
  311. if (pCRef->m_pTableRef != 0)
  312. {
  313. LPWSTR pTbl = AliasToTable(pCRef->m_pTableRef);
  314. if (pTbl == 0)
  315. continue;
  316. if (_wcsicmp(pTbl, pCRef->m_pTableRef) == 0)
  317. pCRef->m_dwFlags |= WQL_FLAG_TABLE;
  318. else
  319. pCRef->m_dwFlags |= WQL_FLAG_ALIAS;
  320. }
  321. }
  322. if (m_aTableRefs.Size() == 0)
  323. return SYNTAX_ERROR;
  324. return SUCCESS;
  325. }
  326. //***************************************************************************
  327. //
  328. // CWQLScanner::StripWhereClause
  329. //
  330. // If present, removes the WHERE or ORDER BY clause. Because
  331. // of SQL Syntax, stripping the first of {ORDER BY, WHERE} will automatically
  332. // get rid of the other.
  333. //
  334. //***************************************************************************
  335. BOOL CWQLScanner::StripWhereClause()
  336. {
  337. for (int i = 0; i < m_aTokens.Size(); i++)
  338. {
  339. WSLexToken *pCurrent = (WSLexToken *) m_aTokens[i];
  340. // If a WHERE token is found, we have something to strip.
  341. // ======================================================
  342. if (pCurrent->m_nToken == WQL_TOK_WHERE ||
  343. pCurrent->m_nToken == WQL_TOK_ORDER)
  344. {
  345. int nNumTokensToRemove = m_aTokens.Size() - i - 1;
  346. for (int i2 = 0; i2 < nNumTokensToRemove; i2++)
  347. {
  348. delete PWSLexToken(m_aTokens[i]);
  349. m_aTokens.RemoveAt(i);
  350. }
  351. return TRUE;
  352. }
  353. }
  354. return FALSE;
  355. }
  356. //***************************************************************************
  357. //
  358. // CWQLScanner::ExtractSelectType
  359. //
  360. // Examines the prefix to reduce the query by eliminating the SELECT
  361. // and select-type keywords, such as ALL, DISTINCT, FIRSTROW, COUNT
  362. //
  363. // If COUNT is used, move past the open-close parentheses.
  364. //
  365. //***************************************************************************
  366. int CWQLScanner::ExtractSelectType()
  367. {
  368. // Verify that SELECT is the first token.
  369. // ======================================
  370. WSLexToken *pFront = ExtractNext();
  371. if (pFront == 0 || pFront->m_nToken == WQL_TOK_EOF)
  372. {
  373. delete pFront;
  374. return SYNTAX_ERROR;
  375. }
  376. if (pFront->m_nToken != WQL_TOK_SELECT)
  377. {
  378. delete pFront;
  379. return SYNTAX_ERROR;
  380. }
  381. delete pFront;
  382. // Check for possible select-type and extract it.
  383. // ==============================================
  384. pFront = ExtractNext();
  385. if (pFront == 0)
  386. return SYNTAX_ERROR;
  387. if (pFront->m_nToken == WQL_TOK_COUNT)
  388. {
  389. delete pFront;
  390. m_bCount = TRUE;
  391. }
  392. else if (pFront->m_nToken == WQL_TOK_ALL ||
  393. pFront->m_nToken == WQL_TOK_DISTINCT ||
  394. pFront->m_nToken == WQL_TOK_FIRSTROW
  395. )
  396. delete pFront;
  397. else
  398. Pushback(pFront);
  399. return SUCCESS;
  400. }
  401. //***************************************************************************
  402. //
  403. // CWQLScanner::SelectList
  404. //
  405. // Extracts all tokens up to the FROM keyword and builds a list
  406. // of selected properties/columns. FROM is left on the parse-stack on exit.
  407. //
  408. //***************************************************************************
  409. int CWQLScanner::SelectList()
  410. {
  411. // If the first token is FROM, then we have a SELECT FROM <rest>
  412. // which is the same as SELECT * FROM <rest>. We simply
  413. // alter the parse-stack and let the following loop handle it.
  414. // =============================================================
  415. WSLexToken *pTok = ExtractNext();
  416. if (pTok->m_nToken == WQL_TOK_FROM)
  417. {
  418. WSLexToken *pAsterisk = new WSLexToken;
  419. if (pAsterisk == NULL)
  420. return FAILED;
  421. pAsterisk->m_nToken = WQL_TOK_ASTERISK;
  422. pAsterisk->m_pszTokenText = Macro_CloneLPWSTR(L"*");
  423. Pushback(pTok);
  424. Pushback(pAsterisk);
  425. }
  426. else
  427. Pushback(pTok);
  428. // Otherwise, some kind of column selection is present.
  429. // ====================================================
  430. BOOL bTerminate = FALSE;
  431. while (!bTerminate)
  432. {
  433. pTok = ExtractNext();
  434. if (pTok == 0)
  435. return SYNTAX_ERROR;
  436. // We must begin at a legal token.
  437. // ===============================
  438. if (pTok->m_nToken != WQL_TOK_EOF)
  439. {
  440. CTokenArray Tokens;
  441. Tokens.Add(pTok);
  442. while (1)
  443. {
  444. pTok = ExtractNext();
  445. if (pTok == 0 || pTok->m_nToken == WQL_TOK_EOF)
  446. {
  447. delete pTok;
  448. return SYNTAX_ERROR;
  449. }
  450. if (pTok->m_nToken == WQL_TOK_FROM)
  451. {
  452. Pushback(pTok);
  453. bTerminate = TRUE;
  454. break;
  455. }
  456. else if (pTok->m_nToken == WQL_TOK_COMMA)
  457. {
  458. delete pTok;
  459. break;
  460. }
  461. else
  462. Tokens.Add(pTok);
  463. }
  464. SWQLColRef *pColRef = new SWQLColRef;
  465. if (pColRef == 0)
  466. return FAILED;
  467. BOOL bRes = BuildSWQLColRef(Tokens, *pColRef);
  468. if (bRes)
  469. m_aPropRefs.Add(pColRef);
  470. else
  471. {
  472. delete pColRef;
  473. return SYNTAX_ERROR;
  474. }
  475. }
  476. // Else an illegal token, such as WQL_TOK_EOF.
  477. // ===========================================
  478. else
  479. {
  480. delete pTok;
  481. return SYNTAX_ERROR;
  482. }
  483. }
  484. return SUCCESS;
  485. }
  486. //***************************************************************************
  487. //
  488. // CWQLScanner::ReduceSql89Joins
  489. //
  490. // Attempts to reduce the FROM clause, assuming it is based on SQL-89
  491. // join syntax or else a simple unary select.
  492. //
  493. // The supported forms are:
  494. //
  495. // FROM x
  496. // FROM x, y
  497. // FROM x as x1, y as y1
  498. // FROM x x1, y y1
  499. //
  500. // If incompatible tokens are encountered, the entire function
  501. // returns FALSE and the results are ignored, and the parse-stack
  502. // is unaffected, in essence, allowing backtracking to try the SQL-92
  503. // syntax branch instead.
  504. //
  505. //***************************************************************************
  506. BOOL CWQLScanner::ReduceSql89Joins()
  507. {
  508. int i = 0;
  509. // Parse the FROM keyword.
  510. // =======================
  511. WSLexToken *pCurr = (WSLexToken *) m_aTokens[i++];
  512. if (pCurr->m_nToken != WQL_TOK_FROM)
  513. return FALSE;
  514. pCurr = (WSLexToken *) m_aTokens[i++];
  515. while (1)
  516. {
  517. if (pCurr->m_nToken != WQL_TOK_IDENT)
  518. return FALSE;
  519. // If here, we are looking at the beginnings of a table ref.
  520. // =========================================================
  521. WSTableRef *pTRef = new WSTableRef;
  522. if (pTRef == 0)
  523. return FAILED;
  524. pTRef->m_pszTable = Macro_CloneLPWSTR(pCurr->m_pszTokenText);
  525. pTRef->m_pszAlias = Macro_CloneLPWSTR(pCurr->m_pszTokenText);
  526. m_aTableRefs.Add(pTRef);
  527. // Attempt to recognize an alias.
  528. // ==============================
  529. pCurr = (WSLexToken *) m_aTokens[i++];
  530. if (pCurr == WQL_TOK_EOF || pCurr->m_nToken == WQL_TOK_UNION)
  531. break;
  532. if (pCurr->m_nToken == WQL_TOK_AS)
  533. pCurr = (WSLexToken *) m_aTokens[i++];
  534. if (pCurr->m_nToken == WQL_TOK_COMMA)
  535. {
  536. pCurr = (WSLexToken *) m_aTokens[i++];
  537. continue;
  538. }
  539. if (pCurr->m_nToken == WQL_TOK_EOF || pCurr->m_nToken == WQL_TOK_UNION)
  540. break;
  541. if (pCurr->m_nToken != WQL_TOK_IDENT)
  542. return FALSE;
  543. delete [] pTRef->m_pszAlias;
  544. pTRef->m_pszAlias = Macro_CloneLPWSTR(pCurr->m_pszTokenText);
  545. // We have completely parsed a table reference.
  546. // Now we move on to the next one.
  547. // ============================================
  548. pCurr = (WSLexToken *) m_aTokens[i++];
  549. if (pCurr->m_nToken == WQL_TOK_EOF || pCurr->m_nToken == WQL_TOK_UNION)
  550. break;
  551. if (pCurr->m_nToken != WQL_TOK_COMMA)
  552. return FALSE;
  553. pCurr = (WSLexToken *) m_aTokens[i++];
  554. }
  555. if (m_aTableRefs.Size())
  556. return TRUE;
  557. return FALSE;
  558. }
  559. //***************************************************************************
  560. //
  561. // CWQLScanner::ReduceSql92Joins
  562. //
  563. // This scans SQL-92 JOIN syntax looking for table aliases. See the
  564. // algorithm at the end of this file.
  565. //
  566. //***************************************************************************
  567. BOOL CWQLScanner::ReduceSql92Joins()
  568. {
  569. WSLexToken *pCurrent = 0, *pRover = 0, *pRight = 0, *pLeft;
  570. int nNumTokens = m_aTokens.Size();
  571. DWORD dwNumJoins = 0;
  572. int iCurrBase = 0;
  573. for (int i = 0; i < nNumTokens; i++)
  574. {
  575. pCurrent = (WSLexToken *) m_aTokens[i];
  576. // If a JOIN token is found, we have a candidate.
  577. // ==============================================
  578. if (pCurrent->m_nToken == WQL_TOK_JOIN)
  579. {
  580. dwNumJoins++;
  581. // Analyze right-context.
  582. // ======================
  583. if (i + 1 < nNumTokens)
  584. pRover = PWSLexToken(m_aTokens[i + 1]);
  585. else
  586. pRover = NULL;
  587. if (pRover && pRover->m_nToken == WQL_TOK_IDENT)
  588. {
  589. // Check for aliased table by checking for
  590. // AS or two juxtaposed idents.
  591. // =======================================
  592. if (i + 2 < nNumTokens)
  593. pRight = PWSLexToken(m_aTokens[i + 2]);
  594. else
  595. pRight = NULL;
  596. if (pRight && pRight->m_nToken == WQL_TOK_AS)
  597. {
  598. if (i + 3 < nNumTokens)
  599. pRight = PWSLexToken(m_aTokens[i + 3]);
  600. else
  601. pRight = NULL;
  602. }
  603. if (pRight && pRight->m_nToken == WQL_TOK_IDENT)
  604. {
  605. WSTableRef *pTRef = new WSTableRef;
  606. if (pTRef == 0)
  607. return FAILED;
  608. pTRef->m_pszAlias = Macro_CloneLPWSTR(pRight->m_pszTokenText);
  609. pTRef->m_pszTable = Macro_CloneLPWSTR(pRover->m_pszTokenText);
  610. m_aTableRefs.Add(pTRef);
  611. }
  612. else // An alias wasn't used, just a simple table ref.
  613. {
  614. WSTableRef *pTRef = new WSTableRef;
  615. if (pTRef == 0)
  616. return FAILED;
  617. pTRef->m_pszAlias = Macro_CloneLPWSTR(pRover->m_pszTokenText);
  618. pTRef->m_pszTable = Macro_CloneLPWSTR(pRover->m_pszTokenText);
  619. m_aTableRefs.Add(pTRef);
  620. }
  621. // discontinue analysis of right-context.
  622. }
  623. // Analyze left-context.
  624. // =====================
  625. int nLeft = i - 1;
  626. if (nLeft >= 0)
  627. pRover = PWSLexToken(m_aTokens[nLeft--]);
  628. else
  629. continue; // No point in continuing
  630. // Verify the ANSI join syntax.
  631. if (nLeft)
  632. {
  633. int iTemp = nLeft;
  634. WSLexToken *pTemp = pRover;
  635. bool bInner = false;
  636. bool bDir = false;
  637. bool bOuter = false;
  638. bool bFail = false;
  639. bool bIdent = false;
  640. while (iTemp >= iCurrBase)
  641. {
  642. if (pTemp->m_nToken == WQL_TOK_INNER)
  643. {
  644. if (bOuter || bIdent || bInner)
  645. bFail = TRUE;
  646. bInner = true;
  647. }
  648. else if (pTemp->m_nToken == WQL_TOK_OUTER)
  649. {
  650. if (bInner || bIdent || bOuter)
  651. bFail = TRUE;
  652. bOuter = true;
  653. }
  654. else if (pTemp->m_nToken == WQL_TOK_FULL ||
  655. pTemp->m_nToken == WQL_TOK_LEFT ||
  656. pTemp->m_nToken == WQL_TOK_RIGHT
  657. )
  658. {
  659. if (bDir || bIdent)
  660. bFail = TRUE;
  661. bDir = true;
  662. }
  663. else
  664. bIdent = TRUE;
  665. // We are trying to enforce correct ANSI-92 joins
  666. // even though we don't support them ourselves:
  667. // OK: LEFT OUTER JOIN
  668. // OUTER LEFT JOIN
  669. // LEFT JOIN
  670. // INNER JOIN
  671. // NOT: LEFT LEFT JOIN
  672. // LEFT INNER JOIN
  673. // LEFT RIGHT JOIN
  674. // OUTER INNER JOIN
  675. // OUTER LEFT OUTER JOIN
  676. // OUTER GARBAGE LEFT JOIN
  677. // (no right side)
  678. if ((bDir && bInner) || bFail)
  679. return FALSE;
  680. pTemp = PWSLexToken(m_aTokens[iTemp--]);
  681. }
  682. }
  683. // Skip past potential JOIN modifiers : INNER, OUTER,
  684. // FULL, LEFT, RIGHT
  685. // ==================================================
  686. if (pRover->m_nToken == WQL_TOK_INNER ||
  687. pRover->m_nToken == WQL_TOK_OUTER ||
  688. pRover->m_nToken == WQL_TOK_FULL ||
  689. pRover->m_nToken == WQL_TOK_LEFT ||
  690. pRover->m_nToken == WQL_TOK_RIGHT
  691. )
  692. {
  693. if (nLeft >= 0)
  694. pRover = PWSLexToken(m_aTokens[nLeft--]);
  695. else
  696. pRover = 0;
  697. }
  698. if (pRover->m_nToken == WQL_TOK_INNER ||
  699. pRover->m_nToken == WQL_TOK_OUTER ||
  700. pRover->m_nToken == WQL_TOK_FULL ||
  701. pRover->m_nToken == WQL_TOK_LEFT ||
  702. pRover->m_nToken == WQL_TOK_RIGHT
  703. )
  704. {
  705. if (nLeft >= 0)
  706. pRover = PWSLexToken(m_aTokens[nLeft--]);
  707. else
  708. pRover = 0;
  709. }
  710. // Now we look to see if the roving pointer is pointing
  711. // to an ident.
  712. // ====================================================
  713. if (pRover && pRover->m_nToken != WQL_TOK_IDENT)
  714. {
  715. // No chance that we are looking at an aliased
  716. // table in a JOIN clause.
  717. // ===========================================
  718. continue;
  719. }
  720. iCurrBase = i;
  721. // If here, we are now possibliy looking at the second half
  722. // of an alias, the 'alias' name proper. We mark this
  723. // by leaving pRover alone and continue to move into the
  724. // left context with a different pointer.
  725. // ========================================================
  726. if (nLeft >= 0)
  727. pLeft = PWSLexToken(m_aTokens[nLeft--]);
  728. else
  729. pLeft = 0;
  730. if (pLeft && pLeft->m_nToken == WQL_TOK_AS)
  731. {
  732. if (nLeft >= 0)
  733. pLeft = PWSLexToken(m_aTokens[nLeft--]);
  734. else
  735. pLeft = 0;
  736. }
  737. // The critical test. Are we at an ident?
  738. // =======================================
  739. if (pLeft && pLeft->m_nToken == WQL_TOK_IDENT)
  740. {
  741. WSTableRef *pTRef = new WSTableRef;
  742. if (pTRef == 0)
  743. return FAILED;
  744. pTRef->m_pszAlias = Macro_CloneLPWSTR(pRover->m_pszTokenText);
  745. pTRef->m_pszTable = Macro_CloneLPWSTR(pLeft->m_pszTokenText);
  746. m_aTableRefs.Add(pTRef);
  747. }
  748. else if (pLeft && pLeft->m_nToken == WQL_TOK_FROM)
  749. {
  750. WSTableRef *pTRef = new WSTableRef;
  751. if (pTRef == 0)
  752. return FAILED;
  753. pTRef->m_pszAlias = Macro_CloneLPWSTR(pRover->m_pszTokenText);
  754. pTRef->m_pszTable = Macro_CloneLPWSTR(pRover->m_pszTokenText);
  755. m_aTableRefs.Add(pTRef);
  756. if (nLeft >= 0)
  757. {
  758. pLeft = PWSLexToken(m_aTokens[nLeft--]);
  759. if (pLeft && pLeft->m_nToken == WQL_TOK_FROM)
  760. return FALSE;
  761. }
  762. }
  763. }
  764. // Find next JOIN occurrence
  765. }
  766. // Make sure there are two sides to every join reference.
  767. if (dwNumJoins+1 != (DWORD)m_aTableRefs.Size())
  768. return FALSE;
  769. return TRUE;
  770. }
  771. //***************************************************************************
  772. //
  773. //***************************************************************************
  774. void CWQLScanner::Dump()
  775. {
  776. WSLexToken *pCurrent = 0;
  777. printf("---Token Stream----\n");
  778. for (int i = 0; i < m_aTokens.Size(); i++)
  779. {
  780. pCurrent = (WSLexToken *) m_aTokens[i];
  781. printf("Token %d <%S>\n", pCurrent->m_nToken, pCurrent->m_pszTokenText);
  782. }
  783. printf("---Table Refs---\n");
  784. for (i = 0; i < m_aTableRefs.Size(); i++)
  785. {
  786. WSTableRef *pTRef = (WSTableRef *) m_aTableRefs[i];
  787. printf("Table = %S Alias = %S\n", pTRef->m_pszTable, pTRef->m_pszAlias);
  788. }
  789. if (!m_bCount)
  790. {
  791. printf("---Select List---\n");
  792. for (i = 0; i < m_aPropRefs.Size(); i++)
  793. {
  794. SWQLColRef *pCRef = (SWQLColRef *) m_aPropRefs[i];
  795. pCRef->DebugDump();
  796. }
  797. }
  798. else
  799. printf(" -> COUNT query\n");
  800. printf("\n\n---<end of dump>---\n\n");
  801. }
  802. /*---------------------------------------------------------------------------
  803. Algorithm for detecting aliased tables in SQL-92 join syntax.
  804. The JOIN keyword must appear.
  805. It may appear in several contexts which are not
  806. relevant to the aliasing problem, such as the following:
  807. select distinct t1a.name, t2a.id, t3.value from
  808. (t1 as t1a join t2 as t2a on t1a.name = t2a.name)
  809. join
  810. (t1 as t1b join t3 on t1b.id = t3.id and (t3.id = t1b.id or t1b.id = t3.id))
  811. on
  812. t1a.id = t3.id
  813. where a = b and c = d
  814. where the middle join is against anonymous result sets.
  815. When analyzing the JOIN, we can easily parse the right-context. Either
  816. an identifier follows (possibly further followed by AS),and an optional
  817. identifier if the JOIN is aliased. Otherwise, we hit ON immediately, or
  818. a parenthesis.
  819. The problem is the left-context of the JOIN token.
  820. For an alias to occur, an identifier must appear immediately to
  821. the left of the JOIN.
  822. id JOIN id2 as id3 ON ...
  823. ^
  824. If here, there is a chance we are looking at the left hand side of a
  825. SQL92 join, a table reference. However, we might be looking at the end of
  826. an ON clause which ends in an identifier:
  827. idx = id JOIN id2 as id3 ON...
  828. ^
  829. To disambiguate, we have to do further analysis of left context.
  830. Consider the follow left-context possibilities:
  831. (1) t1 AS id JOIN id2 as id3 ON
  832. ^
  833. (2) t1 id JOIN id2 as id3 ON
  834. ^
  835. (3) <keyword (except AS)> id JOIN id2 as id3 ON
  836. ^
  837. (4) on x <rel op> id JOIN id2 as id3 ON
  838. ^
  839. Once we have identified <id>, we have to consider the above cases.
  840. (1) Case 1 is easy. An AS clearly tells us we have an alias
  841. and we know how to get at the table and alias names.
  842. (2) Case 2 is easy. Two juxtaposed identifiers to the left always
  843. indicates an alias.
  844. In all other cases, like (3) and (4), etc., the table is not
  845. aliased anyway. Therefore, we only have to determine whether we
  846. are looking at an unaliased table name or the trailing end of
  847. another construct like an ON clause. This is easy. Only the
  848. FROM keyword can precede <id> if <id> is a simple table name.
  849. ---------------------------------------------------------------------------
  850. */
  851. //***************************************************************************
  852. //
  853. // SWQLQualifiedName::Empty
  854. //
  855. //***************************************************************************
  856. void SWQLQualifiedName::Empty()
  857. {
  858. for (int i = 0; i < m_aFields.Size(); i++)
  859. delete (SWQLQualifiedNameField *) m_aFields[i];
  860. }
  861. //***************************************************************************
  862. //
  863. // CWQLScanner::BuildSWQLColRef
  864. //
  865. //***************************************************************************
  866. BOOL CWQLScanner::BuildSWQLColRef(
  867. IN CFlexArray &aTokens,
  868. IN OUT SWQLColRef &ColRef // Empty on entry
  869. )
  870. {
  871. if (aTokens.Size() == 0)
  872. return FALSE;
  873. int nCurrent = 0;
  874. WSLexToken *pTok = PWSLexToken(aTokens[nCurrent++]);
  875. // Initial state: single asterisk or else prop name.
  876. // =================================================
  877. if (pTok->m_nToken == WQL_TOK_ASTERISK && aTokens.Size() == 1)
  878. {
  879. ColRef.m_pColName = Macro_CloneLPWSTR(L"*");
  880. ColRef.m_dwFlags = WQL_FLAG_ASTERISK;
  881. ColRef.m_pQName = new SWQLQualifiedName;
  882. if (ColRef.m_pQName == 0)
  883. return FALSE;
  884. SWQLQualifiedNameField *pField = new SWQLQualifiedNameField;
  885. if (pField == 0)
  886. return FALSE;
  887. pField->m_pName = Macro_CloneLPWSTR(L"*");
  888. ColRef.m_pQName->Add(pField);
  889. return TRUE;
  890. }
  891. // If not an identifier, we have an error.
  892. // =======================================
  893. else if (pTok->m_nToken == WQL_TOK_EOF)
  894. return FALSE;
  895. // If here, we have an identifier.
  896. // ===============================
  897. ColRef.m_pQName = new SWQLQualifiedName;
  898. if (ColRef.m_pQName == NULL)
  899. return FALSE;
  900. SWQLQualifiedNameField *pField = new SWQLQualifiedNameField;
  901. if (pField == 0)
  902. return FALSE;
  903. pField->m_pName = Macro_CloneLPWSTR(pTok->m_pszTokenText);
  904. ColRef.m_pQName->Add(pField);
  905. // Subsequent states.
  906. // ==================
  907. while (1)
  908. {
  909. if (nCurrent == aTokens.Size())
  910. break;
  911. pTok = PWSLexToken(aTokens[nCurrent++]);
  912. if (pTok->m_nToken == WQL_TOK_DOT)
  913. {
  914. pField = new SWQLQualifiedNameField;
  915. if (pField == 0)
  916. return FALSE;
  917. ColRef.m_pQName->Add(pField);
  918. if (nCurrent == aTokens.Size())
  919. return FALSE;
  920. pTok = PWSLexToken(aTokens[nCurrent++]);
  921. if (pTok->m_nToken != WQL_TOK_IDENT &&
  922. pTok->m_nToken != WQL_TOK_ASTERISK
  923. )
  924. return FALSE;
  925. pField->m_pName = Macro_CloneLPWSTR(pTok->m_pszTokenText);
  926. }
  927. else if (pTok->m_nToken == WQL_TOK_OPEN_BRACKET)
  928. {
  929. return FALSE; // Not supported at present!
  930. }
  931. else // illegal token
  932. return FALSE;
  933. }
  934. // Post-process. If the name is not complex, then we
  935. // can fill out fields of ColRef.
  936. // ==================================================
  937. if (ColRef.m_pQName->GetNumNames() == 2)
  938. {
  939. ColRef.m_pTableRef = Macro_CloneLPWSTR(ColRef.m_pQName->GetName(0));
  940. ColRef.m_pColName = Macro_CloneLPWSTR(ColRef.m_pQName->GetName(1));
  941. if (_wcsicmp(ColRef.m_pColName, L"NULL") == 0)
  942. ColRef.m_dwFlags |= WQL_FLAG_NULL;
  943. }
  944. else if (ColRef.m_pQName->GetNumNames() == 1)
  945. {
  946. LPWSTR pName = ColRef.m_pQName->GetName(0);
  947. ColRef.m_pColName = Macro_CloneLPWSTR(pName);
  948. if (_wcsicmp(ColRef.m_pColName, L"NULL") == 0)
  949. ColRef.m_dwFlags |= WQL_FLAG_NULL;
  950. }
  951. else
  952. {
  953. ColRef.m_pTableRef = Macro_CloneLPWSTR(ColRef.m_pQName->GetName(0));
  954. ColRef.m_dwFlags = WQL_FLAG_COMPLEX_NAME;
  955. }
  956. return TRUE;
  957. }
  958. //***************************************************************************
  959. //
  960. //***************************************************************************
  961. void SWQLColRef::DebugDump()
  962. {
  963. printf(" ---SWQLColRef---\n");
  964. printf(" Col Name = %S\n", m_pColName);
  965. printf(" Table = %S\n", m_pTableRef);
  966. printf(" Array Index = %d\n", m_dwArrayIndex);
  967. printf(" Flags = 0x%X ", m_dwFlags);
  968. if (m_dwFlags & WQL_FLAG_ALIAS)
  969. printf("WQL_FLAG_ALIAS ");
  970. if (m_dwFlags & WQL_FLAG_TABLE)
  971. printf("WQL_FLAG_TABLE ");
  972. if (m_dwFlags & WQL_FLAG_COLUMN)
  973. printf("WQL_FLAG_COLUMN ");
  974. if (m_dwFlags & WQL_FLAG_ASTERISK)
  975. printf("WQL_FLAG_ASTERISK ");
  976. if (m_dwFlags & WQL_FLAG_NULL)
  977. printf("WQL_FLAG_NULL ");
  978. if (m_dwFlags & WQL_FLAG_FUNCTIONIZED)
  979. printf("WQL_FLAG_FUNCTIONIZED ");
  980. if (m_dwFlags & WQL_FLAG_COMPLEX_NAME)
  981. printf("WQL_FLAG_COMPLEX_NAME ");
  982. if (m_dwFlags & WQL_FLAG_ARRAY_REF)
  983. printf(" WQL_FLAG_ARRAY_REF");
  984. if (m_dwFlags & WQL_FLAG_UPPER)
  985. printf(" WQL_FLAG_UPPER");
  986. if (m_dwFlags & WQL_FLAG_LOWER)
  987. printf(" WQL_FLAG_LOWER");
  988. if (m_dwFlags & WQL_FLAG_SORT_ASC)
  989. printf(" WQL_FLAG_SORT_ASC");
  990. if (m_dwFlags & WQL_FLAG_SORT_DESC)
  991. printf(" WQL_FLAG_SORT_DESC");
  992. printf("\n");
  993. printf(" ---\n\n");
  994. }
  995. //***************************************************************************
  996. //
  997. // SWQLQualifiedName::operator =
  998. //
  999. //***************************************************************************
  1000. // done
  1001. SWQLQualifiedName & SWQLQualifiedName::operator = (SWQLQualifiedName &Src)
  1002. {
  1003. Empty();
  1004. for (int i = 0; i < Src.m_aFields.Size(); i++)
  1005. {
  1006. SWQLQualifiedNameField *pQN = new SWQLQualifiedNameField;
  1007. if (pQN == 0)
  1008. return *this; // Short-circuit due to out-of-mem
  1009. *pQN = *(SWQLQualifiedNameField *) Src.m_aFields[i];
  1010. m_aFields.Add(pQN);
  1011. }
  1012. return *this;
  1013. }
  1014. //***************************************************************************
  1015. //
  1016. // SWQLQualifiedNameField::operator =
  1017. //
  1018. //***************************************************************************
  1019. // done
  1020. SWQLQualifiedNameField &
  1021. SWQLQualifiedNameField::operator =(SWQLQualifiedNameField &Src)
  1022. {
  1023. Empty();
  1024. m_bArrayRef = Src.m_bArrayRef;
  1025. m_pName = Macro_CloneLPWSTR(Src.m_pName);
  1026. m_dwArrayIndex = Src.m_dwArrayIndex;
  1027. return *this;
  1028. }
  1029. const LPWSTR CWQLScanner::AliasToTable(LPWSTR pszAlias)
  1030. {
  1031. if (pszAlias == 0)
  1032. return 0;
  1033. for (int i = 0; i < m_aTableRefs.Size(); i++)
  1034. {
  1035. WSTableRef *pTRef = (WSTableRef *) m_aTableRefs[i];
  1036. if (_wcsicmp(pszAlias, pTRef->m_pszAlias) == 0)
  1037. return pTRef->m_pszTable;
  1038. if (_wcsicmp(pszAlias, pTRef->m_pszTable) == 0)
  1039. return pTRef->m_pszTable;
  1040. }
  1041. return 0;
  1042. }
  1043.