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.

1333 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(BOOL bRemove)
  207. {
  208. if (m_aTokens.Size() == 0)
  209. return NULL;
  210. PWSLexToken pTok = PWSLexToken(m_aTokens[0]);
  211. if (bRemove)
  212. m_aTokens.RemoveAt(0);
  213. return pTok;
  214. }
  215. //***************************************************************************
  216. //
  217. // CWQLScanner::Pushback
  218. //
  219. //***************************************************************************
  220. int CWQLScanner::Pushback(PWSLexToken pPushbackTok)
  221. {
  222. return m_aTokens.InsertAt(0, pPushbackTok);
  223. }
  224. //***************************************************************************
  225. //
  226. // Shift-reduce parser entry.
  227. //
  228. //***************************************************************************
  229. int CWQLScanner::Parse()
  230. {
  231. int nRes = SYNTAX_ERROR;
  232. if (m_pLexer == NULL)
  233. return FAILED;
  234. m_pLexer->Reset();
  235. if (!Next())
  236. return LEXICAL_ERROR;
  237. // Completely tokenize the entire query and build a parse-stack.
  238. // =============================================================
  239. if (m_nCurrentToken == WQL_TOK_SELECT)
  240. {
  241. while (1)
  242. {
  243. WSLexToken *pTok = new WSLexToken;
  244. if (!pTok)
  245. return FAILED;
  246. pTok->m_nToken = m_nCurrentToken;
  247. pTok->m_pszTokenText = Macro_CloneLPWSTR(m_pTokenText);
  248. if (!pTok->m_pszTokenText)
  249. return FAILED;
  250. if (m_aTokens.Add(pTok))
  251. {
  252. delete pTok;
  253. return FAILED;
  254. }
  255. if (m_nCurrentToken == WQL_TOK_EOF)
  256. break;
  257. if (!Next())
  258. return LEXICAL_ERROR;
  259. }
  260. }
  261. else
  262. return SYNTAX_ERROR;
  263. // Reduce by extracting the select type keywords if possible.
  264. // ==========================================================
  265. nRes = ExtractSelectType();
  266. if (nRes)
  267. return nRes;
  268. // Eliminate all tokens from WHERE onwards.
  269. // ========================================
  270. StripWhereClause();
  271. // Reduce by extracting the select list.
  272. // =====================================
  273. if (!m_bCount)
  274. {
  275. nRes = SelectList();
  276. if (nRes != 0)
  277. return nRes;
  278. }
  279. else
  280. {
  281. // Strip everything until the FROM keyword is encountered.
  282. // =======================================================
  283. WSLexToken *pTok = ExtractNext(FALSE);
  284. if (pTok->m_nToken != WQL_TOK_OPEN_PAREN)
  285. {
  286. nRes = SelectList();
  287. if (nRes )
  288. return nRes;
  289. }
  290. else
  291. {
  292. pTok = ExtractNext();
  293. while (pTok)
  294. {
  295. if (pTok->m_nToken == WQL_TOK_FROM)
  296. {
  297. if (Pushback(pTok))
  298. {
  299. delete pTok;
  300. return FAILED;
  301. }
  302. break;
  303. }
  304. // Bug #46728: the count(*) clause
  305. // can be the only element of the select clause.
  306. else if (!wcscmp(pTok->m_pszTokenText, L","))
  307. {
  308. delete pTok;
  309. return SYNTAX_ERROR;
  310. }
  311. delete pTok;
  312. pTok = ExtractNext();
  313. }
  314. if (pTok == 0)
  315. return SYNTAX_ERROR;
  316. }
  317. }
  318. // Extract tables/aliases from JOIN clauses.
  319. // =========================================
  320. if (ReduceSql89Joins() != TRUE)
  321. {
  322. ClearTableRefs();
  323. if (ReduceSql92Joins() != TRUE)
  324. return SYNTAX_ERROR;
  325. }
  326. // Post process select clause to determine if
  327. // columns are tables or aliases.
  328. // ==========================================
  329. for (int i = 0; i < m_aPropRefs.Size(); i++)
  330. {
  331. SWQLColRef *pCRef = (SWQLColRef *) m_aPropRefs[i];
  332. if (pCRef->m_pTableRef != 0)
  333. {
  334. LPWSTR pTbl = AliasToTable(pCRef->m_pTableRef);
  335. if (pTbl == 0)
  336. continue;
  337. if (_wcsicmp(pTbl, pCRef->m_pTableRef) == 0)
  338. pCRef->m_dwFlags |= WQL_FLAG_TABLE;
  339. else
  340. pCRef->m_dwFlags |= WQL_FLAG_ALIAS;
  341. }
  342. }
  343. if (m_aTableRefs.Size() == 0)
  344. return SYNTAX_ERROR;
  345. return SUCCESS;
  346. }
  347. //***************************************************************************
  348. //
  349. // CWQLScanner::StripWhereClause
  350. //
  351. // If present, removes the WHERE or ORDER BY clause. Because
  352. // of SQL Syntax, stripping the first of {ORDER BY, WHERE} will automatically
  353. // get rid of the other.
  354. //
  355. //***************************************************************************
  356. BOOL CWQLScanner::StripWhereClause()
  357. {
  358. for (int i = 0; i < m_aTokens.Size(); i++)
  359. {
  360. WSLexToken *pCurrent = (WSLexToken *) m_aTokens[i];
  361. // If a WHERE token is found, we have something to strip.
  362. // ======================================================
  363. if (pCurrent->m_nToken == WQL_TOK_WHERE ||
  364. pCurrent->m_nToken == WQL_TOK_ORDER)
  365. {
  366. int nNumTokensToRemove = m_aTokens.Size() - i - 1;
  367. for (int i2 = 0; i2 < nNumTokensToRemove; i2++)
  368. {
  369. delete PWSLexToken(m_aTokens[i]);
  370. m_aTokens.RemoveAt(i);
  371. }
  372. return TRUE;
  373. }
  374. }
  375. return FALSE;
  376. }
  377. //***************************************************************************
  378. //
  379. // CWQLScanner::ExtractSelectType
  380. //
  381. // Examines the prefix to reduce the query by eliminating the SELECT
  382. // and select-type keywords, such as ALL, DISTINCT, FIRSTROW, COUNT
  383. //
  384. // If COUNT is used, move past the open-close parentheses.
  385. //
  386. //***************************************************************************
  387. int CWQLScanner::ExtractSelectType()
  388. {
  389. // Verify that SELECT is the first token.
  390. // ======================================
  391. WSLexToken *pFront = ExtractNext();
  392. if (pFront == 0 || pFront->m_nToken == WQL_TOK_EOF)
  393. {
  394. delete pFront;
  395. return SYNTAX_ERROR;
  396. }
  397. if (pFront->m_nToken != WQL_TOK_SELECT)
  398. {
  399. delete pFront;
  400. return SYNTAX_ERROR;
  401. }
  402. delete pFront;
  403. // Check for possible select-type and extract it.
  404. // ==============================================
  405. pFront = ExtractNext();
  406. if (pFront == 0)
  407. return SYNTAX_ERROR;
  408. if (pFront->m_nToken == WQL_TOK_COUNT)
  409. {
  410. delete pFront;
  411. m_bCount = TRUE;
  412. }
  413. else if (pFront->m_nToken == WQL_TOK_ALL ||
  414. pFront->m_nToken == WQL_TOK_DISTINCT ||
  415. pFront->m_nToken == WQL_TOK_FIRSTROW
  416. )
  417. delete pFront;
  418. else
  419. {
  420. if (Pushback(pFront))
  421. {
  422. delete pFront;
  423. return FAILED;
  424. }
  425. }
  426. return SUCCESS;
  427. }
  428. //***************************************************************************
  429. //
  430. // CWQLScanner::SelectList
  431. //
  432. // Extracts all tokens up to the FROM keyword and builds a list
  433. // of selected properties/columns. FROM is left on the parse-stack on exit.
  434. //
  435. //***************************************************************************
  436. int CWQLScanner::SelectList()
  437. {
  438. // If the first token is FROM, then we have a SELECT FROM <rest>
  439. // which is the same as SELECT * FROM <rest>. We simply
  440. // alter the parse-stack and let the following loop handle it.
  441. // =============================================================
  442. WSLexToken *pTok = ExtractNext();
  443. if (!pTok)
  444. return SYNTAX_ERROR;
  445. if (pTok->m_nToken == WQL_TOK_FROM)
  446. {
  447. WSLexToken *pAsterisk = new WSLexToken;
  448. if (pAsterisk == NULL)
  449. return FAILED;
  450. pAsterisk->m_nToken = WQL_TOK_ASTERISK;
  451. pAsterisk->m_pszTokenText = Macro_CloneLPWSTR(L"*");
  452. if (!pAsterisk->m_pszTokenText)
  453. return FAILED;
  454. if (Pushback(pTok))
  455. {
  456. delete pTok;
  457. delete pAsterisk;
  458. return FAILED;
  459. }
  460. if (Pushback(pAsterisk))
  461. {
  462. delete pAsterisk;
  463. return FAILED;
  464. }
  465. }
  466. else
  467. {
  468. if (Pushback(pTok))
  469. {
  470. delete pTok;
  471. return FAILED;
  472. }
  473. }
  474. // Otherwise, some kind of column selection is present.
  475. // ====================================================
  476. BOOL bTerminate = FALSE;
  477. while (!bTerminate)
  478. {
  479. pTok = ExtractNext();
  480. if (pTok == 0)
  481. return SYNTAX_ERROR;
  482. // We must begin at a legal token.
  483. // ===============================
  484. if (pTok->m_nToken != WQL_TOK_EOF)
  485. {
  486. CTokenArray Tokens;
  487. if (Tokens.Add(pTok))
  488. {
  489. delete pTok;
  490. return FAILED;
  491. }
  492. while (1)
  493. {
  494. pTok = ExtractNext();
  495. if (pTok == 0 || pTok->m_nToken == WQL_TOK_EOF)
  496. {
  497. delete pTok;
  498. return SYNTAX_ERROR;
  499. }
  500. if (pTok->m_nToken == WQL_TOK_FROM)
  501. {
  502. if (Pushback(pTok))
  503. {
  504. delete pTok;
  505. return FAILED;
  506. }
  507. bTerminate = TRUE;
  508. break;
  509. }
  510. else if (pTok->m_nToken == WQL_TOK_COMMA)
  511. {
  512. delete pTok;
  513. break;
  514. }
  515. else
  516. {
  517. if (Tokens.Add(pTok))
  518. {
  519. delete pTok;
  520. return FAILED;
  521. }
  522. }
  523. }
  524. SWQLColRef *pColRef = new SWQLColRef;
  525. if (pColRef == 0)
  526. return FAILED;
  527. BOOL bRes = BuildSWQLColRef(Tokens, *pColRef);
  528. if (bRes)
  529. {
  530. if (m_aPropRefs.Add(pColRef))
  531. {
  532. delete pColRef;
  533. return FAILED;
  534. }
  535. }
  536. else
  537. {
  538. delete pColRef;
  539. return SYNTAX_ERROR;
  540. }
  541. }
  542. // Else an illegal token, such as WQL_TOK_EOF.
  543. // ===========================================
  544. else
  545. {
  546. delete pTok;
  547. return SYNTAX_ERROR;
  548. }
  549. }
  550. return SUCCESS;
  551. }
  552. //***************************************************************************
  553. //
  554. // CWQLScanner::ReduceSql89Joins
  555. //
  556. // Attempts to reduce the FROM clause, assuming it is based on SQL-89
  557. // join syntax or else a simple unary select.
  558. //
  559. // The supported forms are:
  560. //
  561. // FROM x
  562. // FROM x, y
  563. // FROM x as x1, y as y1
  564. // FROM x x1, y y1
  565. //
  566. // If incompatible tokens are encountered, the entire function
  567. // returns FALSE and the results are ignored, and the parse-stack
  568. // is unaffected, in essence, allowing backtracking to try the SQL-92
  569. // syntax branch instead.
  570. //
  571. //***************************************************************************
  572. BOOL CWQLScanner::ReduceSql89Joins()
  573. {
  574. int i = 0;
  575. // Parse the FROM keyword.
  576. // =======================
  577. WSLexToken *pCurr = (WSLexToken *) m_aTokens[i++];
  578. if (pCurr->m_nToken != WQL_TOK_FROM)
  579. return FALSE;
  580. pCurr = (WSLexToken *) m_aTokens[i++];
  581. while (1)
  582. {
  583. if (pCurr->m_nToken != WQL_TOK_IDENT)
  584. return FALSE;
  585. // If here, we are looking at the beginnings of a table ref.
  586. // =========================================================
  587. WSTableRef *pTRef = new WSTableRef;
  588. if (pTRef == 0)
  589. return FAILED;
  590. pTRef->m_pszTable = Macro_CloneLPWSTR(pCurr->m_pszTokenText);
  591. if (!pTRef->m_pszTable)
  592. return FAILED;
  593. pTRef->m_pszAlias = Macro_CloneLPWSTR(pCurr->m_pszTokenText);
  594. if (!pTRef->m_pszAlias)
  595. return FAILED;
  596. if (m_aTableRefs.Add(pTRef))
  597. {
  598. delete pTRef;
  599. return FAILED;
  600. }
  601. // Attempt to recognize an alias.
  602. // ==============================
  603. pCurr = (WSLexToken *) m_aTokens[i++];
  604. if (pCurr == WQL_TOK_EOF || pCurr->m_nToken == WQL_TOK_UNION)
  605. break;
  606. if (pCurr->m_nToken == WQL_TOK_AS)
  607. pCurr = (WSLexToken *) m_aTokens[i++];
  608. if (pCurr->m_nToken == WQL_TOK_COMMA)
  609. {
  610. pCurr = (WSLexToken *) m_aTokens[i++];
  611. continue;
  612. }
  613. if (pCurr->m_nToken == WQL_TOK_EOF || pCurr->m_nToken == WQL_TOK_UNION)
  614. break;
  615. if (pCurr->m_nToken != WQL_TOK_IDENT)
  616. return FALSE;
  617. delete [] pTRef->m_pszAlias;
  618. pTRef->m_pszAlias = Macro_CloneLPWSTR(pCurr->m_pszTokenText);
  619. if (!pTRef->m_pszAlias)
  620. return FALSE;
  621. // We have completely parsed a table reference.
  622. // Now we move on to the next one.
  623. // ============================================
  624. pCurr = (WSLexToken *) m_aTokens[i++];
  625. if (pCurr->m_nToken == WQL_TOK_EOF || pCurr->m_nToken == WQL_TOK_UNION)
  626. break;
  627. if (pCurr->m_nToken != WQL_TOK_COMMA)
  628. return FALSE;
  629. pCurr = (WSLexToken *) m_aTokens[i++];
  630. }
  631. if (m_aTableRefs.Size())
  632. return TRUE;
  633. return FALSE;
  634. }
  635. //***************************************************************************
  636. //
  637. // CWQLScanner::ReduceSql92Joins
  638. //
  639. // This scans SQL-92 JOIN syntax looking for table aliases. See the
  640. // algorithm at the end of this file.
  641. //
  642. //***************************************************************************
  643. BOOL CWQLScanner::ReduceSql92Joins()
  644. {
  645. WSLexToken *pCurrent = 0, *pRover = 0, *pRight = 0, *pLeft;
  646. int nNumTokens = m_aTokens.Size();
  647. DWORD dwNumJoins = 0;
  648. int iCurrBase = 0;
  649. for (int i = 0; i < nNumTokens; i++)
  650. {
  651. pCurrent = (WSLexToken *) m_aTokens[i];
  652. // If a JOIN token is found, we have a candidate.
  653. // ==============================================
  654. if (pCurrent->m_nToken == WQL_TOK_JOIN)
  655. {
  656. dwNumJoins++;
  657. // Analyze right-context.
  658. // ======================
  659. if (i + 1 < nNumTokens)
  660. pRover = PWSLexToken(m_aTokens[i + 1]);
  661. else
  662. pRover = NULL;
  663. if (pRover && pRover->m_nToken == WQL_TOK_IDENT)
  664. {
  665. // Check for aliased table by checking for
  666. // AS or two juxtaposed idents.
  667. // =======================================
  668. if (i + 2 < nNumTokens)
  669. pRight = PWSLexToken(m_aTokens[i + 2]);
  670. else
  671. pRight = NULL;
  672. if (pRight && pRight->m_nToken == WQL_TOK_AS)
  673. {
  674. if (i + 3 < nNumTokens)
  675. pRight = PWSLexToken(m_aTokens[i + 3]);
  676. else
  677. pRight = NULL;
  678. }
  679. if (pRight && pRight->m_nToken == WQL_TOK_IDENT)
  680. {
  681. WSTableRef *pTRef = new WSTableRef;
  682. if (pTRef == 0)
  683. return FAILED;
  684. pTRef->m_pszAlias = Macro_CloneLPWSTR(pRight->m_pszTokenText);
  685. if (!pTRef->m_pszAlias)
  686. return FAILED;
  687. pTRef->m_pszTable = Macro_CloneLPWSTR(pRover->m_pszTokenText);
  688. if (!pTRef->m_pszTable)
  689. return FAILED;
  690. if (m_aTableRefs.Add(pTRef))
  691. {
  692. delete pTRef;
  693. return FAILED;
  694. }
  695. }
  696. else // An alias wasn't used, just a simple table ref.
  697. {
  698. WSTableRef *pTRef = new WSTableRef;
  699. if (pTRef == 0)
  700. return FAILED;
  701. pTRef->m_pszAlias = Macro_CloneLPWSTR(pRover->m_pszTokenText);
  702. if (!pTRef->m_pszAlias)
  703. return FAILED;
  704. pTRef->m_pszTable = Macro_CloneLPWSTR(pRover->m_pszTokenText);
  705. if (!pTRef->m_pszTable)
  706. return FAILED;
  707. if (m_aTableRefs.Add(pTRef))
  708. {
  709. delete pTRef;
  710. return FAILED;
  711. }
  712. }
  713. // discontinue analysis of right-context.
  714. }
  715. // Analyze left-context.
  716. // =====================
  717. int nLeft = i - 1;
  718. if (nLeft >= 0)
  719. pRover = PWSLexToken(m_aTokens[nLeft--]);
  720. else
  721. continue; // No point in continuing
  722. // Verify the ANSI join syntax.
  723. if (nLeft)
  724. {
  725. int iTemp = nLeft;
  726. WSLexToken *pTemp = pRover;
  727. bool bInner = false;
  728. bool bDir = false;
  729. bool bOuter = false;
  730. bool bFail = false;
  731. bool bIdent = false;
  732. while (iTemp >= iCurrBase)
  733. {
  734. if (pTemp->m_nToken == WQL_TOK_INNER)
  735. {
  736. if (bOuter || bIdent || bInner)
  737. bFail = TRUE;
  738. bInner = true;
  739. }
  740. else if (pTemp->m_nToken == WQL_TOK_OUTER)
  741. {
  742. if (bInner || bIdent || bOuter)
  743. bFail = TRUE;
  744. bOuter = true;
  745. }
  746. else if (pTemp->m_nToken == WQL_TOK_FULL ||
  747. pTemp->m_nToken == WQL_TOK_LEFT ||
  748. pTemp->m_nToken == WQL_TOK_RIGHT
  749. )
  750. {
  751. if (bDir || bIdent)
  752. bFail = TRUE;
  753. bDir = true;
  754. }
  755. else
  756. bIdent = TRUE;
  757. // We are trying to enforce correct ANSI-92 joins
  758. // even though we don't support them ourselves:
  759. // OK: LEFT OUTER JOIN
  760. // OUTER LEFT JOIN
  761. // LEFT JOIN
  762. // INNER JOIN
  763. // NOT: LEFT LEFT JOIN
  764. // LEFT INNER JOIN
  765. // LEFT RIGHT JOIN
  766. // OUTER INNER JOIN
  767. // OUTER LEFT OUTER JOIN
  768. // OUTER GARBAGE LEFT JOIN
  769. // (no right side)
  770. if ((bDir && bInner) || bFail)
  771. return FALSE;
  772. pTemp = PWSLexToken(m_aTokens[iTemp--]);
  773. }
  774. }
  775. // Skip past potential JOIN modifiers : INNER, OUTER,
  776. // FULL, LEFT, RIGHT
  777. // ==================================================
  778. if (pRover->m_nToken == WQL_TOK_INNER ||
  779. pRover->m_nToken == WQL_TOK_OUTER ||
  780. pRover->m_nToken == WQL_TOK_FULL ||
  781. pRover->m_nToken == WQL_TOK_LEFT ||
  782. pRover->m_nToken == WQL_TOK_RIGHT
  783. )
  784. {
  785. if (nLeft >= 0)
  786. pRover = PWSLexToken(m_aTokens[nLeft--]);
  787. else
  788. pRover = 0;
  789. }
  790. if (pRover->m_nToken == WQL_TOK_INNER ||
  791. pRover->m_nToken == WQL_TOK_OUTER ||
  792. pRover->m_nToken == WQL_TOK_FULL ||
  793. pRover->m_nToken == WQL_TOK_LEFT ||
  794. pRover->m_nToken == WQL_TOK_RIGHT
  795. )
  796. {
  797. if (nLeft >= 0)
  798. pRover = PWSLexToken(m_aTokens[nLeft--]);
  799. else
  800. pRover = 0;
  801. }
  802. // Now we look to see if the roving pointer is pointing
  803. // to an ident.
  804. // ====================================================
  805. if (pRover && pRover->m_nToken != WQL_TOK_IDENT)
  806. {
  807. // No chance that we are looking at an aliased
  808. // table in a JOIN clause.
  809. // ===========================================
  810. continue;
  811. }
  812. iCurrBase = i;
  813. // If here, we are now possibliy looking at the second half
  814. // of an alias, the 'alias' name proper. We mark this
  815. // by leaving pRover alone and continue to move into the
  816. // left context with a different pointer.
  817. // ========================================================
  818. if (nLeft >= 0)
  819. pLeft = PWSLexToken(m_aTokens[nLeft--]);
  820. else
  821. pLeft = 0;
  822. if (pLeft && pLeft->m_nToken == WQL_TOK_AS)
  823. {
  824. if (nLeft >= 0)
  825. pLeft = PWSLexToken(m_aTokens[nLeft--]);
  826. else
  827. pLeft = 0;
  828. }
  829. // The critical test. Are we at an ident?
  830. // =======================================
  831. if (pLeft && pLeft->m_nToken == WQL_TOK_IDENT)
  832. {
  833. WSTableRef *pTRef = new WSTableRef;
  834. if (pTRef == 0)
  835. return FAILED;
  836. pTRef->m_pszAlias = Macro_CloneLPWSTR(pRover->m_pszTokenText);
  837. if (!pTRef->m_pszAlias)
  838. return FAILED;
  839. pTRef->m_pszTable = Macro_CloneLPWSTR(pLeft->m_pszTokenText);
  840. if (!pTRef->m_pszTable)
  841. return FAILED;
  842. if (m_aTableRefs.Add(pTRef))
  843. {
  844. delete pTRef;
  845. return FAILED;
  846. }
  847. }
  848. else if (pLeft && pLeft->m_nToken == WQL_TOK_FROM)
  849. {
  850. WSTableRef *pTRef = new WSTableRef;
  851. if (pTRef == 0)
  852. return FAILED;
  853. pTRef->m_pszAlias = Macro_CloneLPWSTR(pRover->m_pszTokenText);
  854. if (!pTRef->m_pszAlias)
  855. return FAILED;
  856. pTRef->m_pszTable = Macro_CloneLPWSTR(pRover->m_pszTokenText);
  857. if (!pTRef->m_pszTable)
  858. return FAILED;
  859. if (m_aTableRefs.Add(pTRef))
  860. {
  861. delete pTRef;
  862. return FAILED;
  863. }
  864. if (nLeft >= 0)
  865. {
  866. pLeft = PWSLexToken(m_aTokens[nLeft--]);
  867. if (pLeft && pLeft->m_nToken == WQL_TOK_FROM)
  868. return FALSE;
  869. }
  870. }
  871. }
  872. // Find next JOIN occurrence
  873. }
  874. // Make sure there are two sides to every join reference.
  875. if (dwNumJoins+1 != (DWORD)m_aTableRefs.Size())
  876. return FALSE;
  877. return TRUE;
  878. }
  879. //***************************************************************************
  880. //
  881. //***************************************************************************
  882. void CWQLScanner::Dump()
  883. {
  884. WSLexToken *pCurrent = 0;
  885. printf("---Token Stream----\n");
  886. for (int i = 0; i < m_aTokens.Size(); i++)
  887. {
  888. pCurrent = (WSLexToken *) m_aTokens[i];
  889. printf("Token %d <%S>\n", pCurrent->m_nToken, pCurrent->m_pszTokenText);
  890. }
  891. printf("---Table Refs---\n");
  892. for (i = 0; i < m_aTableRefs.Size(); i++)
  893. {
  894. WSTableRef *pTRef = (WSTableRef *) m_aTableRefs[i];
  895. printf("Table = %S Alias = %S\n", pTRef->m_pszTable, pTRef->m_pszAlias);
  896. }
  897. if (!m_bCount)
  898. {
  899. printf("---Select List---\n");
  900. for (i = 0; i < m_aPropRefs.Size(); i++)
  901. {
  902. SWQLColRef *pCRef = (SWQLColRef *) m_aPropRefs[i];
  903. pCRef->DebugDump();
  904. }
  905. }
  906. else
  907. printf(" -> COUNT query\n");
  908. printf("\n\n---<end of dump>---\n\n");
  909. }
  910. /*---------------------------------------------------------------------------
  911. Algorithm for detecting aliased tables in SQL-92 join syntax.
  912. The JOIN keyword must appear.
  913. It may appear in several contexts which are not
  914. relevant to the aliasing problem, such as the following:
  915. select distinct t1a.name, t2a.id, t3.value from
  916. (t1 as t1a join t2 as t2a on t1a.name = t2a.name)
  917. join
  918. (t1 as t1b join t3 on t1b.id = t3.id and (t3.id = t1b.id or t1b.id = t3.id))
  919. on
  920. t1a.id = t3.id
  921. where a = b and c = d
  922. where the middle join is against anonymous result sets.
  923. When analyzing the JOIN, we can easily parse the right-context. Either
  924. an identifier follows (possibly further followed by AS),and an optional
  925. identifier if the JOIN is aliased. Otherwise, we hit ON immediately, or
  926. a parenthesis.
  927. The problem is the left-context of the JOIN token.
  928. For an alias to occur, an identifier must appear immediately to
  929. the left of the JOIN.
  930. id JOIN id2 as id3 ON ...
  931. ^
  932. If here, there is a chance we are looking at the left hand side of a
  933. SQL92 join, a table reference. However, we might be looking at the end of
  934. an ON clause which ends in an identifier:
  935. idx = id JOIN id2 as id3 ON...
  936. ^
  937. To disambiguate, we have to do further analysis of left context.
  938. Consider the follow left-context possibilities:
  939. (1) t1 AS id JOIN id2 as id3 ON
  940. ^
  941. (2) t1 id JOIN id2 as id3 ON
  942. ^
  943. (3) <keyword (except AS)> id JOIN id2 as id3 ON
  944. ^
  945. (4) on x <rel op> id JOIN id2 as id3 ON
  946. ^
  947. Once we have identified <id>, we have to consider the above cases.
  948. (1) Case 1 is easy. An AS clearly tells us we have an alias
  949. and we know how to get at the table and alias names.
  950. (2) Case 2 is easy. Two juxtaposed identifiers to the left always
  951. indicates an alias.
  952. In all other cases, like (3) and (4), etc., the table is not
  953. aliased anyway. Therefore, we only have to determine whether we
  954. are looking at an unaliased table name or the trailing end of
  955. another construct like an ON clause. This is easy. Only the
  956. FROM keyword can precede <id> if <id> is a simple table name.
  957. ---------------------------------------------------------------------------
  958. */
  959. //***************************************************************************
  960. //
  961. // CWQLScanner::BuildSWQLColRef
  962. //
  963. //***************************************************************************
  964. BOOL CWQLScanner::BuildSWQLColRef(
  965. IN CFlexArray &aTokens,
  966. IN OUT SWQLColRef &ColRef // Empty on entry
  967. )
  968. {
  969. if (aTokens.Size() == 0)
  970. return FALSE;
  971. int nCurrent = 0;
  972. WSLexToken *pTok = PWSLexToken(aTokens[nCurrent++]);
  973. // Initial state: single asterisk or else prop name.
  974. // =================================================
  975. if (pTok->m_nToken == WQL_TOK_ASTERISK && aTokens.Size() == 1)
  976. {
  977. ColRef.m_pColName = Macro_CloneLPWSTR(L"*");
  978. if (!ColRef.m_pColName)
  979. return FALSE;
  980. ColRef.m_dwFlags = WQL_FLAG_ASTERISK;
  981. ColRef.m_pQName = new SWQLQualifiedName;
  982. if (ColRef.m_pQName == 0)
  983. return FALSE;
  984. SWQLQualifiedNameField *pField = new SWQLQualifiedNameField;
  985. if (pField == 0)
  986. return FALSE;
  987. pField->m_pName = Macro_CloneLPWSTR(L"*");
  988. if (!pField->m_pName)
  989. return FALSE;
  990. if (ColRef.m_pQName->Add(pField))
  991. {
  992. delete pField;
  993. return FALSE;
  994. }
  995. return TRUE;
  996. }
  997. // If not an identifier, we have an error.
  998. // =======================================
  999. else if (pTok->m_nToken == WQL_TOK_EOF)
  1000. return FALSE;
  1001. // If here, we have an identifier.
  1002. // ===============================
  1003. ColRef.m_pQName = new SWQLQualifiedName;
  1004. if (ColRef.m_pQName == NULL)
  1005. return FALSE;
  1006. SWQLQualifiedNameField *pField = new SWQLQualifiedNameField;
  1007. if (pField == 0)
  1008. return FALSE;
  1009. pField->m_pName = Macro_CloneLPWSTR(pTok->m_pszTokenText);
  1010. if (!pField->m_pName)
  1011. return FALSE;
  1012. if (ColRef.m_pQName->Add(pField))
  1013. {
  1014. delete pField;
  1015. return FALSE;
  1016. }
  1017. // Subsequent states.
  1018. // ==================
  1019. while (1)
  1020. {
  1021. if (nCurrent == aTokens.Size())
  1022. break;
  1023. pTok = PWSLexToken(aTokens[nCurrent++]);
  1024. if (pTok->m_nToken == WQL_TOK_DOT)
  1025. {
  1026. pField = new SWQLQualifiedNameField;
  1027. if (pField == 0)
  1028. return FALSE;
  1029. if (ColRef.m_pQName->Add(pField))
  1030. {
  1031. delete pField;
  1032. return FALSE;
  1033. }
  1034. if (nCurrent == aTokens.Size())
  1035. return FALSE;
  1036. pTok = PWSLexToken(aTokens[nCurrent++]);
  1037. if (pTok->m_nToken != WQL_TOK_IDENT &&
  1038. pTok->m_nToken != WQL_TOK_ASTERISK
  1039. )
  1040. return FALSE;
  1041. pField->m_pName = Macro_CloneLPWSTR(pTok->m_pszTokenText);
  1042. if (!pField->m_pName)
  1043. return FALSE;
  1044. }
  1045. else if (pTok->m_nToken == WQL_TOK_OPEN_BRACKET)
  1046. {
  1047. return FALSE; // Not supported at present!
  1048. }
  1049. else // illegal token
  1050. return FALSE;
  1051. }
  1052. // Post-process. If the name is not complex, then we
  1053. // can fill out fields of ColRef.
  1054. // ==================================================
  1055. if (ColRef.m_pQName->GetNumNames() == 2)
  1056. {
  1057. ColRef.m_pTableRef = Macro_CloneLPWSTR(ColRef.m_pQName->GetName(0));
  1058. if (!ColRef.m_pTableRef)
  1059. return FALSE;
  1060. ColRef.m_pColName = Macro_CloneLPWSTR(ColRef.m_pQName->GetName(1));
  1061. if (!ColRef.m_pColName)
  1062. return FALSE;
  1063. if (_wcsicmp(ColRef.m_pColName, L"NULL") == 0)
  1064. ColRef.m_dwFlags |= WQL_FLAG_NULL;
  1065. }
  1066. else if (ColRef.m_pQName->GetNumNames() == 1)
  1067. {
  1068. LPWSTR pName = ColRef.m_pQName->GetName(0);
  1069. ColRef.m_pColName = Macro_CloneLPWSTR(pName);
  1070. if (!ColRef.m_pColName)
  1071. return FALSE;
  1072. if (0 == _wcsicmp(ColRef.m_pColName, L"NULL"))
  1073. ColRef.m_dwFlags |= WQL_FLAG_NULL;
  1074. }
  1075. else
  1076. {
  1077. ColRef.m_pTableRef = Macro_CloneLPWSTR(ColRef.m_pQName->GetName(0));
  1078. if (!ColRef.m_pTableRef)
  1079. return FALSE;
  1080. ColRef.m_dwFlags = WQL_FLAG_COMPLEX_NAME;
  1081. }
  1082. return TRUE;
  1083. }
  1084. const LPWSTR CWQLScanner::AliasToTable(LPWSTR pszAlias)
  1085. {
  1086. if (pszAlias == 0)
  1087. return 0;
  1088. for (int i = 0; i < m_aTableRefs.Size(); i++)
  1089. {
  1090. WSTableRef *pTRef = (WSTableRef *) m_aTableRefs[i];
  1091. if (_wcsicmp(pszAlias, pTRef->m_pszAlias) == 0)
  1092. return pTRef->m_pszTable;
  1093. if (_wcsicmp(pszAlias, pTRef->m_pszTable) == 0)
  1094. return pTRef->m_pszTable;
  1095. }
  1096. return 0;
  1097. }
  1098.