FormatSQL.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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(_T("'"), _T("''"));
  25. //Replace all "|" with a space
  26. cs.Replace(_T("|"), _T(" "));
  27. cs.Replace(_T("["), _T(" "));
  28. cs.Replace(_T("]"), _T(" "));
  29. cs.Replace(_T("*"), _T("%"));
  30. int nLength = cs.GetLength();
  31. CString csCurrentWord;
  32. bool bInQuotes = false;
  33. eSpecialTypes eNotValue = eINVALID;
  34. eSpecialTypes eOrValue = eAND;
  35. for(int i = 0; i < nLength; i++)
  36. {
  37. switch(cs[i])
  38. {
  39. case '"':
  40. bInQuotes = !bInQuotes;
  41. break;
  42. case ' ':
  43. if(bInQuotes == false)
  44. {
  45. eSpecialTypes sp = ConvetToKey(csCurrentWord);
  46. switch(sp)
  47. {
  48. case eNOT:
  49. eNotValue = sp;
  50. break;
  51. case eOR:
  52. case eAND:
  53. eOrValue = sp;
  54. break;
  55. default:
  56. AddToSQL(csCurrentWord, eNotValue, eOrValue);
  57. }
  58. csCurrentWord = "";
  59. }
  60. else
  61. {
  62. csCurrentWord += cs[i];
  63. }
  64. break;
  65. default:
  66. csCurrentWord += cs[i];
  67. }
  68. }
  69. if(csCurrentWord.GetLength() > 0)
  70. AddToSQL(csCurrentWord, eNotValue, eOrValue);
  71. }
  72. CFormatSQL::eSpecialTypes CFormatSQL::ConvetToKey(CString cs)
  73. {
  74. cs.MakeUpper();
  75. if(cs == "NOT" ||
  76. cs == "!")
  77. {
  78. return eNOT;
  79. }
  80. else if(cs == "OR")
  81. {
  82. return eOR;
  83. }
  84. else if(cs == "AND")
  85. {
  86. return eAND;
  87. }
  88. return eINVALID;
  89. }
  90. CString CFormatSQL::GetKeyWordString(eSpecialTypes eKeyWord)
  91. {
  92. switch(eKeyWord)
  93. {
  94. case eNOT:
  95. return " NOT ";
  96. case eAND:
  97. return " AND ";
  98. case eOR:
  99. return " OR ";
  100. case eINVALID:
  101. return " ";
  102. }
  103. return " ";
  104. }
  105. bool CFormatSQL::AddToSQL(CString cs, eSpecialTypes &eNOTValue, eSpecialTypes &eORValue)
  106. {
  107. CString csThisSQL;
  108. cs.TrimLeft();
  109. cs.TrimRight();
  110. if(cs.Find(_T("%")) < 0 && cs.Find(_T("?")) < 0)
  111. {
  112. csThisSQL.Format(_T("%s%sLIKE \'%%%s%%\'"), m_csVariable, GetKeyWordString(eNOTValue), cs);
  113. }
  114. else
  115. {
  116. csThisSQL.Format(_T("%s%sLIKE \'%s%%\'"), m_csVariable, GetKeyWordString(eNOTValue), cs);
  117. }
  118. if(m_csWhere.GetLength() > 0)
  119. {
  120. m_csWhere += GetKeyWordString(eORValue) + csThisSQL;
  121. }
  122. else
  123. {
  124. m_csWhere = csThisSQL;
  125. }
  126. eNOTValue = eINVALID;
  127. eORValue = eAND;
  128. return true;
  129. }