FormatSQL.cpp 2.4 KB

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