|
28 | 28 | #include "wsjcpp_sql_builder.h" |
29 | 29 | #include <algorithm> |
30 | 30 |
|
| 31 | + |
| 32 | +// --------------------------------------------------------------------- |
| 33 | +// WsjcppSqlBuilderHelpers |
| 34 | + |
| 35 | +std::string WsjcppSqlBuilderHelpers::escapingStringValue(const std::string &sValue) { |
| 36 | + // escaping simbols NUL (ASCII 0), \n, \r, \, ', ", и Control-Z. |
| 37 | + std::string sResult; |
| 38 | + sResult.reserve(sValue.size() * 2); |
| 39 | + sResult.push_back('\''); |
| 40 | + for (int i = 0; i < sValue.size(); i++) { |
| 41 | + char c = sValue[i]; |
| 42 | + if (c == '\n') { |
| 43 | + sResult.push_back('\\'); |
| 44 | + sResult.push_back('n'); |
| 45 | + } else if (c == '\r') { |
| 46 | + sResult.push_back('\\'); |
| 47 | + sResult.push_back('r'); |
| 48 | + } else if (c == '\\' || c == '"') { |
| 49 | + sResult.push_back('\\'); |
| 50 | + sResult.push_back(c); |
| 51 | + } else if (c == '\'') { |
| 52 | + sResult.push_back('\''); |
| 53 | + sResult.push_back(c); |
| 54 | + } else if (c == 0) { |
| 55 | + sResult.push_back('\\'); |
| 56 | + sResult.push_back('0'); |
| 57 | + } else { |
| 58 | + sResult.push_back(c); |
| 59 | + } |
| 60 | + } |
| 61 | + sResult.push_back('\''); |
| 62 | + return sResult; |
| 63 | +} |
| 64 | + |
31 | 65 | // --------------------------------------------------------------------- |
32 | 66 | // WsjcppSqlQuery |
33 | 67 |
|
@@ -71,9 +105,9 @@ bool WsjcppSqlQuery::add(const std::string &sColumnName, |
71 | 105 | m_bValid = false; |
72 | 106 | } else if (m_nSqlType == WsjcppSqlBuilderType::INSERT) { |
73 | 107 | m_sSqlQuery0 += sColumnName + ", "; |
74 | | - m_sSqlQuery1 += prepareStringValue(sValue) + ", "; |
| 108 | + m_sSqlQuery1 += WsjcppSqlBuilderHelpers::escapingStringValue(sValue) + ", "; |
75 | 109 | } else if (m_nSqlType == WsjcppSqlBuilderType::UPDATE) { |
76 | | - m_sSqlQuery0 += sColumnName + " = " + prepareStringValue(sValue); |
| 110 | + m_sSqlQuery0 += sColumnName + " = " + WsjcppSqlBuilderHelpers::escapingStringValue(sValue); |
77 | 111 | } else { |
78 | 112 | m_sErrorMessage = "Unknown sql type"; |
79 | 113 | m_bValid = false; |
@@ -138,12 +172,12 @@ bool WsjcppSqlQuery::where(const std::string &sColumnName, |
138 | 172 | return false; |
139 | 173 | } |
140 | 174 | if (m_nSqlType == WsjcppSqlBuilderType::SELECT) { |
141 | | - m_sSqlQuery2 += sColumnName + " = " + prepareStringValue(sValue); |
| 175 | + m_sSqlQuery2 += sColumnName + " = " + WsjcppSqlBuilderHelpers::escapingStringValue(sValue); |
142 | 176 | } else if (m_nSqlType == WsjcppSqlBuilderType::INSERT) { |
143 | 177 | m_sErrorMessage = "where can be in insert"; |
144 | 178 | return false; |
145 | 179 | } else if (m_nSqlType == WsjcppSqlBuilderType::UPDATE) { |
146 | | - m_sSqlQuery1 += sColumnName + " = " + prepareStringValue(sValue); |
| 180 | + m_sSqlQuery1 += sColumnName + " = " + WsjcppSqlBuilderHelpers::escapingStringValue(sValue); |
147 | 181 | } |
148 | 182 |
|
149 | 183 | return true; |
@@ -239,36 +273,6 @@ bool WsjcppSqlQuery::checkName(const std::string &sColumnName) { |
239 | 273 | return true; |
240 | 274 | } |
241 | 275 |
|
242 | | -std::string WsjcppSqlQuery::prepareStringValue(const std::string &sValue) { |
243 | | - // escaping simbols NUL (ASCII 0), \n, \r, \, ', ", и Control-Z. |
244 | | - std::string sResult; |
245 | | - sResult.reserve(sValue.size() * 2); |
246 | | - sResult.push_back('\''); |
247 | | - for (int i = 0; i < sValue.size(); i++) { |
248 | | - char c = sValue[i]; |
249 | | - if (c == '\n') { |
250 | | - sResult.push_back('\\'); |
251 | | - sResult.push_back('n'); |
252 | | - } else if (c == '\r') { |
253 | | - sResult.push_back('\\'); |
254 | | - sResult.push_back('r'); |
255 | | - } else if (c == '\\' || c == '"') { |
256 | | - sResult.push_back('\\'); |
257 | | - sResult.push_back(c); |
258 | | - } else if (c == '\'') { |
259 | | - sResult.push_back('\''); |
260 | | - sResult.push_back(c); |
261 | | - } else if (c == 0) { |
262 | | - sResult.push_back('\\'); |
263 | | - sResult.push_back('0'); |
264 | | - } else { |
265 | | - sResult.push_back(c); |
266 | | - } |
267 | | - } |
268 | | - sResult.push_back('\''); |
269 | | - return sResult; |
270 | | -} |
271 | | - |
272 | 276 | // --------------------------------------------------------------------- |
273 | 277 | // WsjcppSqlBuilderSelect |
274 | 278 |
|
@@ -318,9 +322,33 @@ WsjcppSqlWhereCondition::WsjcppSqlWhereCondition( |
318 | 322 | const std::string &name, |
319 | 323 | WsjcppSqlWhereConditionType comparator, |
320 | 324 | const std::string &value |
321 | | -) |
322 | | - : WsjcppSqlWhereBase(WsjcppSqlWhereType::CONDITION), m_name(name), m_comparator(comparator), m_value(value) { |
| 325 | +) : WsjcppSqlWhereBase(WsjcppSqlWhereType::CONDITION), m_name(name), m_comparator(comparator) { |
| 326 | + // TODO in different databases different quotes, mssql have a column names in double quotes |
| 327 | + m_value = WsjcppSqlBuilderHelpers::escapingStringValue(value); |
| 328 | +} |
| 329 | + |
| 330 | +WsjcppSqlWhereCondition::WsjcppSqlWhereCondition( |
| 331 | + const std::string &name, |
| 332 | + WsjcppSqlWhereConditionType comparator, |
| 333 | + int value |
| 334 | +) : WsjcppSqlWhereBase(WsjcppSqlWhereType::CONDITION), m_name(name), m_comparator(comparator) { |
| 335 | + m_value = std::to_string(value); |
| 336 | +} |
323 | 337 |
|
| 338 | +WsjcppSqlWhereCondition::WsjcppSqlWhereCondition( |
| 339 | + const std::string &name, |
| 340 | + WsjcppSqlWhereConditionType comparator, |
| 341 | + double value |
| 342 | +) : WsjcppSqlWhereBase(WsjcppSqlWhereType::CONDITION), m_name(name), m_comparator(comparator) { |
| 343 | + m_value = std::to_string(value); |
| 344 | +} |
| 345 | + |
| 346 | +WsjcppSqlWhereCondition::WsjcppSqlWhereCondition( |
| 347 | + const std::string &name, |
| 348 | + WsjcppSqlWhereConditionType comparator, |
| 349 | + float value |
| 350 | +) : WsjcppSqlWhereBase(WsjcppSqlWhereType::CONDITION), m_name(name), m_comparator(comparator) { |
| 351 | + m_value = std::to_string(value); |
324 | 352 | } |
325 | 353 |
|
326 | 354 | const std::string &WsjcppSqlWhereCondition::name() { |
@@ -358,7 +386,7 @@ std::string WsjcppSqlWhereCondition::sql() { |
358 | 386 | ret += " unknwon_operator "; |
359 | 387 | break; |
360 | 388 | } |
361 | | - ret += "\"" + m_value + "\""; // TODO validate and escaping |
| 389 | + ret += m_value; |
362 | 390 | return ret; |
363 | 391 | } |
364 | 392 |
|
|
0 commit comments