FormatSQL.cpp 2.9 KB

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