FormatSQL.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // FormatSQL.cpp: implementation of the CFormatSQL class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "cp_main.h"
  6. #include "FormatSQL.h"
  7. #ifdef _DEBUG
  8. #undef THIS_FILE
  9. static char THIS_FILE[]=__FILE__;
  10. #define new DEBUG_NEW
  11. #endif
  12. //////////////////////////////////////////////////////////////////////
  13. // Construction/Destruction
  14. //////////////////////////////////////////////////////////////////////
  15. CFormatSQL::CFormatSQL()
  16. {
  17. }
  18. CFormatSQL::~CFormatSQL()
  19. {
  20. }
  21. void CFormatSQL::Parse(CString cs)
  22. {
  23. //Replace all single ' with a double '
  24. cs.Replace("'", "''");
  25. //Replace all "|" with a space
  26. cs.Replace("|", " ");
  27. cs.Replace("[", " ");
  28. cs.Replace("]", " ");
  29. int nLength = cs.GetLength();
  30. CString csCurrentWord;
  31. bool bInQuotes = false;
  32. eSpecialTypes eNotValue = eINVALID;
  33. eSpecialTypes eOrValue = eAND;
  34. for(int i = 0; i < nLength; i++)
  35. {
  36. switch(cs[i])
  37. {
  38. case '"':
  39. bInQuotes = !bInQuotes;
  40. break;
  41. case ' ':
  42. if(bInQuotes == false)
  43. {
  44. eSpecialTypes sp = ConvetToKey(csCurrentWord);
  45. switch(sp)
  46. {
  47. case eNOT:
  48. eNotValue = sp;
  49. break;
  50. case eOR:
  51. case eAND:
  52. eOrValue = sp;
  53. break;
  54. default:
  55. AddToSQL(csCurrentWord, eNotValue, eOrValue);
  56. }
  57. csCurrentWord = "";
  58. }
  59. else
  60. {
  61. csCurrentWord += cs[i];
  62. }
  63. break;
  64. default:
  65. csCurrentWord += cs[i];
  66. }
  67. }
  68. if(csCurrentWord.GetLength() > 0)
  69. AddToSQL(csCurrentWord, eNotValue, eOrValue);
  70. }
  71. CFormatSQL::eSpecialTypes CFormatSQL::ConvetToKey(CString cs)
  72. {
  73. cs.MakeUpper();
  74. if(cs == "NOT" ||
  75. cs == "!")
  76. {
  77. return eNOT;
  78. }
  79. else if(cs == "OR")
  80. {
  81. return eOR;
  82. }
  83. else if(cs == "AND")
  84. {
  85. return eAND;
  86. }
  87. return eINVALID;
  88. }
  89. CString CFormatSQL::GetKeyWordString(eSpecialTypes eKeyWord)
  90. {
  91. switch(eKeyWord)
  92. {
  93. case eNOT:
  94. return " NOT ";
  95. case eAND:
  96. return " AND ";
  97. case eOR:
  98. return " OR ";
  99. case eINVALID:
  100. return " ";
  101. }
  102. return " ";
  103. }
  104. bool CFormatSQL::AddToSQL(CString cs, eSpecialTypes &eNOTValue, eSpecialTypes &eORValue)
  105. {
  106. CString csThisSQL;
  107. cs.TrimLeft();
  108. cs.TrimRight();
  109. if(cs.Find("*") < 0 && cs.Find("?") < 0)
  110. {
  111. csThisSQL.Format("%s%sLIKE \'*%s*\'", m_csVariable, GetKeyWordString(eNOTValue), cs);
  112. }
  113. else
  114. {
  115. csThisSQL.Format("%s%sLIKE \'%s\'", m_csVariable, GetKeyWordString(eNOTValue), cs);
  116. }
  117. if(m_csWhere.GetLength() > 0)
  118. {
  119. m_csWhere += GetKeyWordString(eORValue) + csThisSQL;
  120. }
  121. else
  122. {
  123. m_csWhere = csThisSQL;
  124. }
  125. eNOTValue = eINVALID;
  126. eORValue = eAND;
  127. return true;
  128. }