FormatSQL.cpp 3.0 KB

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