PageRenderTime 76ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/contrib/mysql++/query.cpp

http://github.com/martinisoft/listbotto
C++ | 645 lines | 461 code | 85 blank | 99 comment | 131 complexity | 695f2d798fe38668d37c4059ad064274 MD5 | raw file
  1. /***********************************************************************
  2. query.cpp - Implements the Query class.
  3. Copyright (c) 1998 by Kevin Atkinson, (c) 1999-2001 by MySQL AB, and
  4. (c) 2004-2008 by Educational Technology Resources, Inc. Others may
  5. also hold copyrights on code in this file. See the CREDITS.txt file
  6. in the top directory of the distribution for details.
  7. This file is part of MySQL++.
  8. MySQL++ is free software; you can redistribute it and/or modify it
  9. under the terms of the GNU Lesser General Public License as published
  10. by the Free Software Foundation; either version 2.1 of the License, or
  11. (at your option) any later version.
  12. MySQL++ is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  15. License for more details.
  16. You should have received a copy of the GNU Lesser General Public
  17. License along with MySQL++; if not, write to the Free Software
  18. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
  19. USA
  20. ***********************************************************************/
  21. #include "query.h"
  22. #include "autoflag.h"
  23. #include "dbdriver.h"
  24. #include "connection.h"
  25. namespace mysqlpp {
  26. Query::Query(Connection* c, bool te, const char* qstr) :
  27. #if defined(MYSQLPP_HAVE_STD__NOINIT)
  28. // prevents a double-init memory leak in native VC++ RTL (not STLport!)
  29. std::ostream(std::_Noinit),
  30. #else
  31. std::ostream(0),
  32. #endif
  33. OptionalExceptions(te),
  34. template_defaults(this),
  35. conn_(c),
  36. copacetic_(true)
  37. {
  38. // Set up our internal IOStreams string buffer
  39. init(&sbuffer_);
  40. // Insert passed query string into our string buffer, if given
  41. if (qstr) {
  42. sbuffer_.str(qstr);
  43. seekp(0, std::ios::end); // allow more insertions at end
  44. }
  45. // Override any global locale setting; we want to use the classic C
  46. // locale so we don't get weird things like thousands separators in
  47. // integers inserted into the query stream.
  48. imbue(std::locale::classic());
  49. }
  50. Query::Query(const Query& q) :
  51. #if defined(MYSQLPP_HAVE_STD__NOINIT)
  52. // ditto above
  53. std::ostream(std::_Noinit),
  54. #else
  55. std::ostream(0),
  56. #endif
  57. OptionalExceptions(q.throw_exceptions()),
  58. template_defaults(q.template_defaults),
  59. conn_(q.conn_),
  60. copacetic_(q.copacetic_)
  61. {
  62. // We don't copy stream buffer or template query stuff from the other
  63. // Query on purpose. This isn't a copy ctor so much as a way to
  64. // ensure that "Query q(conn.query());" works correctly.
  65. init(&sbuffer_);
  66. // See above for reason we override locale for Query streams.
  67. imbue(std::locale::classic());
  68. }
  69. ulonglong
  70. Query::affected_rows()
  71. {
  72. return conn_->driver()->affected_rows();
  73. }
  74. int
  75. Query::errnum() const
  76. {
  77. return conn_->errnum();
  78. }
  79. const char*
  80. Query::error() const
  81. {
  82. return conn_->error();
  83. }
  84. size_t
  85. Query::escape_string(std::string* ps, const char* original,
  86. size_t length) const
  87. {
  88. if (ps == 0) {
  89. // Can't do any real work!
  90. return 0;
  91. }
  92. else if (original == 0) {
  93. // ps must point to the original data as well as to the
  94. // receiving string, so get the pointer and the length from it.
  95. original = ps->data();
  96. length = ps->length();
  97. }
  98. else if (length == 0) {
  99. // We got a pointer to a C++ string just for holding the result
  100. // and also a C string pointing to the original, so find the
  101. // length of the original.
  102. length = strlen(original);
  103. }
  104. char* escaped = new char[length * 2 + 1];
  105. length = escape_string(escaped, original, length);
  106. ps->assign(escaped, length);
  107. delete[] escaped;
  108. return length;
  109. }
  110. size_t
  111. Query::escape_string(char* escaped, const char* original,
  112. size_t length) const
  113. {
  114. if (conn_ && *conn_) {
  115. // Normal case
  116. return conn_->driver()->escape_string(escaped, original, length);
  117. }
  118. else {
  119. // Should only happen in test/test_manip.cpp, since it doesn't
  120. // want to open a DB connection just to test the manipulators.
  121. return DBDriver::escape_string_no_conn(escaped, original, length);
  122. }
  123. }
  124. bool
  125. Query::exec(const std::string& str)
  126. {
  127. if ((copacetic_ = conn_->driver()->execute(str.data(),
  128. static_cast<unsigned long>(str.length()))) == true) {
  129. if (parse_elems_.size() == 0) {
  130. // Not a template query, so auto-reset
  131. reset();
  132. }
  133. return true;
  134. }
  135. else if (throw_exceptions()) {
  136. throw BadQuery(error(), errnum());
  137. }
  138. else {
  139. return false;
  140. }
  141. }
  142. SimpleResult
  143. Query::execute(SQLQueryParms& p)
  144. {
  145. AutoFlag<> af(template_defaults.processing_);
  146. return execute(str(p));
  147. }
  148. SimpleResult
  149. Query::execute(const SQLTypeAdapter& s)
  150. {
  151. if ((parse_elems_.size() == 2) && !template_defaults.processing_) {
  152. // We're a template query and this isn't a recursive call, so
  153. // take s to be a lone parameter for the query. We will come
  154. // back in here with a completed query, but the processing_
  155. // flag will be set, allowing us to avoid an infinite loop.
  156. AutoFlag<> af(template_defaults.processing_);
  157. return execute(SQLQueryParms() << s);
  158. }
  159. else {
  160. // Take s to be the entire query string
  161. return execute(s.data(), s.length());
  162. }
  163. }
  164. SimpleResult
  165. Query::execute(const char* str, size_t len)
  166. {
  167. if ((copacetic_ = conn_->driver()->execute(str, len)) == true) {
  168. if (parse_elems_.size() == 0) {
  169. // Not a template query, so auto-reset
  170. reset();
  171. }
  172. return SimpleResult(conn_, insert_id(), affected_rows(), info());
  173. }
  174. else if (throw_exceptions()) {
  175. throw BadQuery(error(), errnum());
  176. }
  177. else {
  178. return SimpleResult();
  179. }
  180. }
  181. std::string
  182. Query::info()
  183. {
  184. return conn_->driver()->query_info();
  185. }
  186. ulonglong
  187. Query::insert_id()
  188. {
  189. return conn_->driver()->insert_id();
  190. }
  191. bool
  192. Query::more_results()
  193. {
  194. return conn_->driver()->more_results();
  195. }
  196. Query&
  197. Query::operator=(const Query& rhs)
  198. {
  199. set_exceptions(rhs.throw_exceptions());
  200. template_defaults = rhs.template_defaults;
  201. conn_ = rhs.conn_;
  202. copacetic_ = rhs.copacetic_;
  203. return *this;
  204. }
  205. Query::operator void*() const
  206. {
  207. return *conn_ && copacetic_ ? const_cast<Query*>(this) : 0;
  208. }
  209. void
  210. Query::parse()
  211. {
  212. std::string str = "";
  213. char num[4];
  214. std::string name;
  215. char* s = new char[sbuffer_.str().size() + 1];
  216. memcpy(s, sbuffer_.str().data(), sbuffer_.str().size());
  217. s[sbuffer_.str().size()] = '\0';
  218. const char* s0 = s;
  219. while (*s) {
  220. if (*s == '%') {
  221. // Following might be a template parameter declaration...
  222. s++;
  223. if (*s == '%') {
  224. // Doubled percent sign, so insert literal percent sign.
  225. str += *s++;
  226. }
  227. else if (isdigit(*s)) {
  228. // Number following percent sign, so it signifies a
  229. // positional parameter. First step: find position
  230. // value, up to 3 digits long.
  231. num[0] = *s;
  232. s++;
  233. if (isdigit(*s)) {
  234. num[1] = *s;
  235. num[2] = 0;
  236. s++;
  237. if (isdigit(*s)) {
  238. num[2] = *s;
  239. num[3] = 0;
  240. s++;
  241. }
  242. else {
  243. num[2] = 0;
  244. }
  245. }
  246. else {
  247. num[1] = 0;
  248. }
  249. signed char n = atoi(num);
  250. // Look for option character following position value.
  251. char option = ' ';
  252. if (*s == 'q' || *s == 'Q') {
  253. option = *s++;
  254. }
  255. // Is it a named parameter?
  256. if (*s == ':') {
  257. // Save all alphanumeric and underscore characters
  258. // following colon as parameter name.
  259. s++;
  260. for (/* */; isalnum(*s) || *s == '_'; ++s) {
  261. name += *s;
  262. }
  263. // Eat trailing colon, if it's present.
  264. if (*s == ':') {
  265. s++;
  266. }
  267. // Update maps that translate parameter name to
  268. // number and vice versa.
  269. if (n >= static_cast<short>(parsed_names_.size())) {
  270. parsed_names_.insert(parsed_names_.end(),
  271. static_cast<std::vector<std::string>::size_type>(
  272. n + 1) - parsed_names_.size(),
  273. std::string());
  274. }
  275. parsed_names_[n] = name;
  276. parsed_nums_[name] = n;
  277. }
  278. // Finished parsing parameter; save it.
  279. parse_elems_.push_back(SQLParseElement(str, option, n));
  280. str = "";
  281. name = "";
  282. }
  283. else {
  284. // Insert literal percent sign, because sign didn't
  285. // precede a valid parameter string; this allows users
  286. // to play a little fast and loose with the rules,
  287. // avoiding a double percent sign here.
  288. str += '%';
  289. }
  290. }
  291. else {
  292. // Regular character, so just copy it.
  293. str += *s++;
  294. }
  295. }
  296. parse_elems_.push_back(SQLParseElement(str, ' ', -1));
  297. delete[] s0;
  298. }
  299. SQLTypeAdapter*
  300. Query::pprepare(char option, SQLTypeAdapter& S, bool replace)
  301. {
  302. if (S.is_processed()) {
  303. return &S;
  304. }
  305. if (option == 'q') {
  306. std::string temp(S.quote_q() ? "'" : "", S.quote_q() ? 1 : 0);
  307. if (S.escape_q()) {
  308. char *escaped = new char[S.size() * 2 + 1];
  309. size_t len = conn_->driver()->escape_string(escaped,
  310. S.data(), static_cast<unsigned long>(S.size()));
  311. temp.append(escaped, len);
  312. delete[] escaped;
  313. }
  314. else {
  315. temp.append(S.data(), S.length());
  316. }
  317. if (S.quote_q()) temp.append("'", 1);
  318. SQLTypeAdapter* ss = new SQLTypeAdapter(temp);
  319. if (replace) {
  320. S = *ss;
  321. S.set_processed();
  322. delete ss;
  323. return &S;
  324. }
  325. else {
  326. return ss;
  327. }
  328. }
  329. else if (option == 'Q' && S.quote_q()) {
  330. std::string temp("'", 1);
  331. temp.append(S.data(), S.length());
  332. temp.append("'", 1);
  333. SQLTypeAdapter *ss = new SQLTypeAdapter(temp);
  334. if (replace) {
  335. S = *ss;
  336. S.set_processed();
  337. delete ss;
  338. return &S;
  339. }
  340. else {
  341. return ss;
  342. }
  343. }
  344. else {
  345. if (replace) {
  346. S.set_processed();
  347. }
  348. return &S;
  349. }
  350. }
  351. void
  352. Query::proc(SQLQueryParms& p)
  353. {
  354. sbuffer_.str("");
  355. for (std::vector<SQLParseElement>::iterator i = parse_elems_.begin();
  356. i != parse_elems_.end(); ++i) {
  357. MYSQLPP_QUERY_THISPTR << i->before;
  358. int num = i->num;
  359. if (num >= 0) {
  360. SQLQueryParms* c;
  361. if (size_t(num) < p.size()) {
  362. c = &p;
  363. }
  364. else if (size_t(num) < template_defaults.size()) {
  365. c = &template_defaults;
  366. }
  367. else {
  368. *this << " ERROR";
  369. throw BadParamCount(
  370. "Not enough parameters to fill the template.");
  371. }
  372. SQLTypeAdapter& param = (*c)[num];
  373. SQLTypeAdapter* ss = pprepare(i->option, param, c->bound());
  374. MYSQLPP_QUERY_THISPTR << *ss;
  375. if (ss != &param) {
  376. // pprepare() returned a new string object instead of
  377. // updating param in place, so we need to delete it.
  378. delete ss;
  379. }
  380. }
  381. }
  382. }
  383. void
  384. Query::reset()
  385. {
  386. seekp(0);
  387. clear();
  388. sbuffer_.str("");
  389. parse_elems_.clear();
  390. template_defaults.clear();
  391. }
  392. StoreQueryResult
  393. Query::store(SQLQueryParms& p)
  394. {
  395. AutoFlag<> af(template_defaults.processing_);
  396. return store(str(p));
  397. }
  398. StoreQueryResult
  399. Query::store(const SQLTypeAdapter& s)
  400. {
  401. if ((parse_elems_.size() == 2) && !template_defaults.processing_) {
  402. // We're a template query and this isn't a recursive call, so
  403. // take s to be a lone parameter for the query. We will come
  404. // back in here with a completed query, but the processing_
  405. // flag will be set, allowing us to avoid an infinite loop.
  406. AutoFlag<> af(template_defaults.processing_);
  407. return store(SQLQueryParms() << s);
  408. }
  409. else {
  410. // Take s to be the entire query string
  411. return store(s.data(), s.length());
  412. }
  413. }
  414. StoreQueryResult
  415. Query::store(const char* str, size_t len)
  416. {
  417. MYSQL_RES* res = 0;
  418. if ((copacetic_ = conn_->driver()->execute(str, len)) == true) {
  419. res = conn_->driver()->store_result();
  420. }
  421. if (res) {
  422. if (parse_elems_.size() == 0) {
  423. // Not a template query, so auto-reset
  424. reset();
  425. }
  426. return StoreQueryResult(res, conn_->driver(), throw_exceptions());
  427. }
  428. else {
  429. // Either result set is empty, or there was a problem executing
  430. // the query or storing its results. Since it's not an error to
  431. // use store() with queries that never return results (INSERT,
  432. // DELETE, CREATE, ALTER...) we need to figure out which case
  433. // this is. (You might use store() instead of execute() for
  434. // such queries when the query strings come from "outside".)
  435. if (copacetic_ = (conn_->errnum() == 0)) {
  436. if (parse_elems_.size() == 0) {
  437. // Not a template query, so auto-reset
  438. reset();
  439. }
  440. return StoreQueryResult();
  441. }
  442. else if (throw_exceptions()) {
  443. throw BadQuery(error(), errnum());
  444. }
  445. else {
  446. return StoreQueryResult();
  447. }
  448. }
  449. }
  450. StoreQueryResult
  451. Query::store_next()
  452. {
  453. #if MYSQL_VERSION_ID > 41000 // only in MySQL v4.1 +
  454. DBDriver::nr_code rc = conn_->driver()->next_result();
  455. if (rc == DBDriver::nr_more_results) {
  456. // There are more results, so return next result set.
  457. MYSQL_RES* res = conn_->driver()->store_result();
  458. if (res) {
  459. return StoreQueryResult(res, conn_->driver(),
  460. throw_exceptions());
  461. }
  462. else {
  463. // Result set is null, but throw an exception only i it is
  464. // null because of some error. If not, it's just an empty
  465. // result set, which is harmless. We return an empty result
  466. // set if exceptions are disabled, as well.
  467. if (conn_->errnum() && throw_exceptions()) {
  468. throw BadQuery(error(), errnum());
  469. }
  470. else {
  471. return StoreQueryResult();
  472. }
  473. }
  474. }
  475. else if (throw_exceptions()) {
  476. if (rc == DBDriver::nr_error) {
  477. throw BadQuery(error(), errnum());
  478. }
  479. else if (conn_->errnum()) {
  480. throw BadQuery(error(), errnum());
  481. }
  482. else {
  483. return StoreQueryResult(); // normal end-of-result-sets case
  484. }
  485. }
  486. else {
  487. return StoreQueryResult();
  488. }
  489. #else
  490. return store();
  491. #endif // MySQL v4.1+
  492. }
  493. std::string
  494. Query::str(SQLQueryParms& p)
  495. {
  496. if (!parse_elems_.empty()) {
  497. proc(p);
  498. }
  499. return sbuffer_.str();
  500. }
  501. UseQueryResult
  502. Query::use(SQLQueryParms& p)
  503. {
  504. AutoFlag<> af(template_defaults.processing_);
  505. return use(str(p));
  506. }
  507. UseQueryResult
  508. Query::use(const SQLTypeAdapter& s)
  509. {
  510. if ((parse_elems_.size() == 2) && !template_defaults.processing_) {
  511. // We're a template query and this isn't a recursive call, so
  512. // take s to be a lone parameter for the query. We will come
  513. // back in here with a completed query, but the processing_
  514. // flag will be set, allowing us to avoid an infinite loop.
  515. AutoFlag<> af(template_defaults.processing_);
  516. return use(SQLQueryParms() << s);
  517. }
  518. else {
  519. // Take s to be the entire query string
  520. return use(s.data(), s.length());
  521. }
  522. }
  523. UseQueryResult
  524. Query::use(const char* str, size_t len)
  525. {
  526. MYSQL_RES* res = 0;
  527. if ((copacetic_ = conn_->driver()->execute(str, len)) == true) {
  528. res = conn_->driver()->use_result();
  529. }
  530. if (res) {
  531. if (parse_elems_.size() == 0) {
  532. // Not a template query, so auto-reset
  533. reset();
  534. }
  535. return UseQueryResult(res, conn_->driver(), throw_exceptions());
  536. }
  537. else {
  538. // See comments in store() above for why we distinguish between
  539. // empty result sets and actual error returns here.
  540. if (copacetic_ = (conn_->errnum() == 0)) {
  541. if (parse_elems_.size() == 0) {
  542. // Not a template query, so auto-reset
  543. reset();
  544. }
  545. return UseQueryResult();
  546. }
  547. else if (throw_exceptions()) {
  548. throw BadQuery(error(), errnum());
  549. }
  550. else {
  551. return UseQueryResult();
  552. }
  553. }
  554. }
  555. } // end namespace mysqlpp