FormatSQL.cpp 2.8 KB

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