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.

1230 lines
35 KiB

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