frontend.h 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (C) 2010-2011 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
  4. //
  5. // Distributed under:
  6. //
  7. // the Boost Software License, Version 1.0.
  8. // (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. // or (at your opinion) under:
  12. //
  13. // The MIT License
  14. // (See accompanying file MIT.txt or a copy at
  15. // http://www.opensource.org/licenses/mit-license.php)
  16. //
  17. ///////////////////////////////////////////////////////////////////////////////
  18. #ifndef CPPDB_FRONTEND_H
  19. #define CPPDB_FRONTEND_H
  20. #include <cppdb/defs.h>
  21. #include <cppdb/errors.h>
  22. #include <cppdb/ref_ptr.h>
  23. // Borland errors about unknown pool-type without this include.
  24. #ifdef __BORLANDC__
  25. #include <cppdb/backend.h>
  26. #endif
  27. #include <iosfwd>
  28. #include <ctime>
  29. #include <string>
  30. #include <memory>
  31. #include <typeinfo>
  32. ///
  33. /// The namespace of all data related to the cppdb api
  34. ///
  35. namespace cppdb {
  36. class result;
  37. class statement;
  38. class session;
  39. class connection_info;
  40. class connection_specific_data;
  41. ///
  42. /// Get CppDB Version String. It consists of "A.B.C", where A
  43. /// is a major version number, B is a minor version number and
  44. /// C is patch version
  45. ///
  46. CPPDB_API char const *version_string();
  47. ///
  48. /// Return CppDB version as a number as a sum A * 10000 + B * 100 + C
  49. /// where A is a major version number, B is a minor version number and
  50. /// C is patch version
  51. ///
  52. CPPDB_API int version_number();
  53. // Borland needs pool.h, but not this forward declaration.
  54. #ifndef __BORLANDC__
  55. namespace backend {
  56. class result;
  57. class statement;
  58. class connection;
  59. }
  60. #endif
  61. ///
  62. /// Null value marker
  63. ///
  64. typedef enum {
  65. null_value, ///< The value is null value
  66. not_null_value ///< The valus is not a null value
  67. } null_tag_type;
  68. /// \cond INTERNAL
  69. namespace tags {
  70. template<typename T>
  71. struct into_tag {
  72. T &value;
  73. null_tag_type &tag;
  74. into_tag(T &v, null_tag_type &t) : value(v),tag(t) {}
  75. };
  76. template<typename T>
  77. struct use_tag {
  78. T value;
  79. null_tag_type tag;
  80. use_tag(T v,null_tag_type t) : value(v),tag(t) {}
  81. };
  82. } // tags
  83. /// \endcond
  84. ///
  85. /// \brief Create a pair of value and tag for fetching a value from row.
  86. ///
  87. /// The fetched
  88. /// value will be stored in \a value if the column is not null and the flag
  89. /// if the value is null or not saved in \a tag
  90. ///
  91. template<typename T>
  92. tags::into_tag<T> into(T &value,null_tag_type &tag)
  93. {
  94. return tags::into_tag<T>(value,tag);
  95. }
  96. ///
  97. /// \brief Create a pair of a string value and tag for storing it to DB
  98. ///
  99. inline tags::use_tag<std::string const &> use(std::string const &v,null_tag_type tag)
  100. {
  101. return tags::use_tag<std::string const &>(v,tag);
  102. }
  103. ///
  104. /// \brief Create a pair of a string value and tag for storing it to DB
  105. ///
  106. inline tags::use_tag<char const *> use(char const *v,null_tag_type tag)
  107. {
  108. return tags::use_tag<char const *>(v,tag);
  109. }
  110. ///
  111. /// \brief Create a pair of value and tag for storing it to DB
  112. ///
  113. template<typename T>
  114. tags::use_tag<T> use(T value,null_tag_type tag)
  115. {
  116. return tags::use_tag<T>(value,tag);
  117. }
  118. /// \cond INTERNAL
  119. namespace details {
  120. template<typename Object>
  121. class functor {
  122. public:
  123. functor(functor const &other) :
  124. functor_(other.functor_),
  125. wrapper_(other.wrapper_)
  126. {
  127. }
  128. functor const &operator=(functor const &other)
  129. {
  130. functor_ = other.functor_;
  131. wrapper_ = other.wrapper_;
  132. return *this;
  133. }
  134. functor(void (*func)(Object &))
  135. {
  136. functor_ = reinterpret_cast<void *>(reinterpret_cast<size_t>(func));
  137. wrapper_ = &functor::call_func;
  138. }
  139. template<typename RealFunctor>
  140. functor(RealFunctor const &f)
  141. {
  142. // The usual casts are not enough for all compilers
  143. functor_ = reinterpret_cast<void const *>(&f);
  144. wrapper_ = &functor<Object>::template call_it<RealFunctor>;
  145. }
  146. void operator()(Object &p) const
  147. {
  148. wrapper_(functor_,p);
  149. }
  150. private:
  151. static void call_func(void const *pointer,Object &parameter)
  152. {
  153. typedef void function_type(Object &);
  154. function_type *f = reinterpret_cast<function_type *>(reinterpret_cast<size_t>((pointer)));
  155. f(parameter);
  156. }
  157. template<typename Functor>
  158. static void call_it(void const *pointer,Object &parameter)
  159. {
  160. Functor const *f_ptr = reinterpret_cast<Functor const *>(pointer);
  161. Functor const &f=*f_ptr;
  162. f(parameter);
  163. }
  164. void const *functor_;
  165. void (*wrapper_)(void const *,Object &);
  166. };
  167. } // details
  168. /// \endcond
  169. #ifdef CPPDB_DOXYGEN
  170. ///
  171. /// Special object that can be constructed from generic function like object \a f.
  172. ///
  173. /// So once_functor(f) can be created if f can be used like f(s) where s is \ref cppdb::session
  174. ///
  175. typedef unspecified_class_type once_functor;
  176. #else
  177. typedef details::functor<session> once_functor;
  178. #endif
  179. ///
  180. /// \brief This object represents query result.
  181. ///
  182. /// This object and it is generally created by statement::query() call, default constructor
  183. /// is provided for consistency, but access to any member function with exception of empty() would
  184. /// throw an exception.
  185. ///
  186. class CPPDB_API result {
  187. public:
  188. ///
  189. /// Create an empty result, it is not useful except for having default constructor
  190. ///
  191. result();
  192. ///
  193. /// Destroys the result, note, if the result of statement is not destroyed, it would
  194. /// not be returned to statements cache.
  195. ///
  196. ~result();
  197. ///
  198. /// Copy result, note it only keeps the reference to actual object so copy is just
  199. /// copy of the reference
  200. ///
  201. result(result const &);
  202. ///
  203. /// Assign result, note it only keeps the reference to actual object so assignment is just
  204. /// copy of the reference
  205. ///
  206. result const &operator=(result const &);
  207. ///
  208. /// Return the number of columns in the result
  209. ///
  210. int cols();
  211. ///
  212. /// Move forward to next row, returns false if no more rows available.
  213. ///
  214. /// Notes:
  215. ///
  216. /// - You should call next() at least once before you use fetch() functions
  217. /// - You must not call fetch() functions if next() returned false, it would cause empty_row_access exception.
  218. ///
  219. bool next();
  220. ///
  221. /// Convert column name \a n to its index, throws invalid_column if the name is not valid.
  222. ///
  223. int index(std::string const &n);
  224. ///
  225. /// Convert column name \a n to its index, returns -1 if the name is not valid.
  226. ///
  227. int find_column(std::string const &name);
  228. ///
  229. /// Convert column index to column name, throws invalid_column if col is not in range 0<= col < cols()
  230. ///
  231. std::string name(int col);
  232. ///
  233. /// Return true if the column number \a col (starting from 0) has NULL value
  234. ///
  235. bool is_null(int col);
  236. ///
  237. /// Return true if the column named \a n has NULL value
  238. ///
  239. bool is_null(std::string const &n);
  240. ///
  241. /// Clears the result, no further use of the result should be done until it is assigned again with a new statement result.
  242. ///
  243. /// It is useful when you want to release all data and return the statement to cache
  244. ///
  245. void clear();
  246. ///
  247. /// Reset current column index, so fetch without column index can be used once again
  248. ///
  249. void rewind_column();
  250. ///
  251. /// Check if the current row is empty, it is in 3 cases:
  252. ///
  253. /// -# Empty result
  254. /// -# next() wasn't called first time
  255. /// -# next() returned false;
  256. ///
  257. bool empty();
  258. ///
  259. /// Fetch a value from column \a col (starting from 0) into \a v. Returns false
  260. /// if the value in NULL and \a v is not updated, otherwise returns true.
  261. ///
  262. /// If the data type is not same it tries to cast the data, if casting fails or the
  263. /// data is out of the type range, throws bad_value_cast().
  264. ///
  265. bool fetch(int col,short &v);
  266. ///
  267. /// \copydoc fetch(int,short&)
  268. ///
  269. bool fetch(int col,unsigned short &v);
  270. ///
  271. /// \copydoc fetch(int,short&)
  272. ///
  273. bool fetch(int col,int &v);
  274. ///
  275. /// \copydoc fetch(int,short&)
  276. ///
  277. bool fetch(int col,unsigned &v);
  278. ///
  279. /// \copydoc fetch(int,short&)
  280. ///
  281. bool fetch(int col,long &v);
  282. ///
  283. /// \copydoc fetch(int,short&)
  284. ///
  285. bool fetch(int col,unsigned long &v);
  286. ///
  287. /// \copydoc fetch(int,short&)
  288. ///
  289. bool fetch(int col,long long &v);
  290. ///
  291. /// \copydoc fetch(int,short&)
  292. ///
  293. bool fetch(int col,unsigned long long &v);
  294. ///
  295. /// \copydoc fetch(int,short&)
  296. ///
  297. bool fetch(int col,float &v);
  298. ///
  299. /// \copydoc fetch(int,short&)
  300. ///
  301. bool fetch(int col,double &v);
  302. ///
  303. /// \copydoc fetch(int,short&)
  304. ///
  305. bool fetch(int col,long double &v);
  306. ///
  307. /// Fetch a textual value from column \a col (starting from 0) into \a v. Returns false
  308. /// if the value in NULL and \a v is not updated, otherwise returns true.
  309. ///
  310. /// If the data type is not same, if possible it converts it into textual representation.
  311. ///
  312. bool fetch(int col,std::string &v);
  313. ///
  314. /// \copydoc fetch(int,short&)
  315. ///
  316. bool fetch(int col,std::tm &v);
  317. ///
  318. /// Fetch a binary large object value from column \a col (starting from 0) into a stream \a v. Returns false
  319. /// if the value in NULL and \a v is not updated, otherwise returns true.
  320. ///
  321. /// If the data type is not blob, it may throw bad_value_cast()
  322. ///
  323. bool fetch(int col,std::ostream &v);
  324. ///
  325. /// Fetch a value from column named \a n into \a v. Returns false
  326. /// if the value in NULL and \a v is not updated, otherwise returns true.
  327. ///
  328. /// If the data type is not same it tries to cast the data, if casting fails or the
  329. /// data is out of the type range, throws bad_value_cast().
  330. ///
  331. /// If the \a n value is invalid throws invalid_column exception
  332. ///
  333. bool fetch(std::string const &n,short &v);
  334. ///
  335. /// \copydoc fetch(std::string const &,short&)
  336. ///
  337. bool fetch(std::string const &n,unsigned short &v);
  338. ///
  339. /// \copydoc fetch(std::string const &,short&)
  340. ///
  341. bool fetch(std::string const &n,int &v);
  342. ///
  343. /// \copydoc fetch(std::string const &,short&)
  344. ///
  345. bool fetch(std::string const &n,unsigned &v);
  346. ///
  347. /// \copydoc fetch(std::string const &,short&)
  348. ///
  349. bool fetch(std::string const &n,long &v);
  350. ///
  351. /// \copydoc fetch(std::string const &,short&)
  352. ///
  353. bool fetch(std::string const &n,unsigned long &v);
  354. ///
  355. /// \copydoc fetch(std::string const &,short&)
  356. ///
  357. bool fetch(std::string const &n,long long &v);
  358. ///
  359. /// \copydoc fetch(std::string const &,short&)
  360. ///
  361. bool fetch(std::string const &n,unsigned long long &v);
  362. ///
  363. /// \copydoc fetch(std::string const &,short&)
  364. ///
  365. bool fetch(std::string const &n,float &v);
  366. ///
  367. /// \copydoc fetch(std::string const &,short&)
  368. ///
  369. bool fetch(std::string const &n,double &v);
  370. ///
  371. /// \copydoc fetch(std::string const &,short&)
  372. ///
  373. bool fetch(std::string const &n,long double &v);
  374. ///
  375. /// Fetch a textual value from column named \a n into \a v. Returns false
  376. /// if the value in NULL and \a v is not updated, otherwise returns true.
  377. ///
  378. /// If the data type is not same, if possible it converts it into textual representation. If
  379. /// the \a n value is invalid throws invalid_column exception
  380. ///
  381. bool fetch(std::string const &n,std::string &v);
  382. ///
  383. /// \copydoc fetch(std::string const &,short&)
  384. ///
  385. bool fetch(std::string const &n,std::tm &v);
  386. ///
  387. /// Fetch a binary large object value from column named \a name into a stream \a v. Returns false
  388. /// if the value in NULL and \a v is not updated, otherwise returns true.
  389. ///
  390. /// If the data type is not blob, it may throw bad_value_cast(). If
  391. /// the \a n value is invalid throws invalid_column exception
  392. ///
  393. bool fetch(std::string const &n,std::ostream &v);
  394. ///
  395. /// Fetch a value from the next column in the row starting from the first one. Returns false
  396. /// if the value in NULL and \a v is not updated, otherwise returns true.
  397. ///
  398. /// If the data type is not same it tries to cast the data, if casting fails or the
  399. /// data is out of the type range, throws bad_value_cast().
  400. ///
  401. /// If fetch was called more times then cols() it throws invalid_column exception, to use
  402. /// it once again from the beginning on the same row call rewind_column() member function.
  403. /// It is not required to call rewind_column() after calling next() as column index is reset
  404. /// automatically.
  405. ///
  406. bool fetch(short &v);
  407. /// \copydoc fetch(short&)
  408. bool fetch(unsigned short &v);
  409. /// \copydoc fetch(short&)
  410. bool fetch(int &v);
  411. /// \copydoc fetch(short&)
  412. bool fetch(unsigned &v);
  413. /// \copydoc fetch(short&)
  414. bool fetch(long &v);
  415. /// \copydoc fetch(short&)
  416. bool fetch(unsigned long &v);
  417. /// \copydoc fetch(short&)
  418. bool fetch(long long &v);
  419. /// \copydoc fetch(short&)
  420. bool fetch(unsigned long long &v);
  421. /// \copydoc fetch(short&)
  422. bool fetch(float &v);
  423. /// \copydoc fetch(short&)
  424. bool fetch(double &v);
  425. /// \copydoc fetch(short&)
  426. bool fetch(long double &v);
  427. ///
  428. /// Fetch a textual value from the next column in the row starting from the first one. Returns false
  429. /// if the value in NULL and \a v is not updated, otherwise returns true.
  430. ///
  431. /// If the data type is not same, if possible it converts it into textual representation.
  432. ///
  433. /// If fetch was called more times then cols() it throws invalid_column exception, to use
  434. /// it once again from the beginning on the same row call rewind_column() member function.
  435. /// It is not required to call rewind_column() after calling next() as column index is reset
  436. /// automatically.
  437. ///
  438. bool fetch(std::string &v);
  439. /// \copydoc fetch(short&)
  440. bool fetch(std::tm &v);
  441. ///
  442. /// Fetch a blob value from the next column in the row starting from the first one into stream \a v. Returns false
  443. /// if the value in NULL and \a v is not updated, otherwise returns true.
  444. ///
  445. /// If the data type is not blob, it may throw bad_value_cast().
  446. ///
  447. /// If fetch was called more times then cols() it throws invalid_column exception, to use
  448. /// it once again from the beginning on the same row call rewind_column() member function.
  449. /// It is not required to call rewind_column() after calling next() as column index is reset
  450. /// automatically.
  451. ///
  452. bool fetch(std::ostream &v);
  453. ///
  454. /// Get a value of type \a T from column named \a name. If the column
  455. /// is null throws null_value_fetch(), if the column \a name is invalid throws invalid_column,
  456. /// if the column value cannot be converted to type T (see fetch functions) it throws bad_value_cast.
  457. ///
  458. template<typename T>
  459. T get(std::string const &name)
  460. {
  461. T v=T();
  462. if(!fetch(name,v))
  463. throw null_value_fetch();
  464. return v;
  465. }
  466. ///
  467. /// Get a value of type \a T from column named \a name. If the column
  468. /// is null returns \a def, if the column \a name is invalid throws invalid_column,
  469. /// if the column value cannot be converted to type T (see fetch functions) it throws bad_value_cast.
  470. ///
  471. template<typename T>
  472. T get(std::string const &name, T const &def)
  473. {
  474. T v=T();
  475. if(!fetch(name,v))
  476. return def;
  477. return v;
  478. }
  479. ///
  480. /// Get a value of type \a T from column \a col (starting from 0). If the column
  481. /// is null throws null_value_fetch(), if the column index is invalid throws invalid_column,
  482. /// if the column value cannot be converted to type T (see fetch functions) it throws bad_value_cast.
  483. ///
  484. template<typename T>
  485. T get(int col)
  486. {
  487. T v=T();
  488. if(!fetch(col,v))
  489. throw null_value_fetch();
  490. return v;
  491. }
  492. ///
  493. /// Get a value of type \a T from column \a col (starting from 0). If the column
  494. /// is null returns \a def, if the column index is invalid throws invalid_column,
  495. /// if the column value cannot be converted to type T (see fetch functions) it throws bad_value_cast.
  496. ///
  497. template<typename T>
  498. T get(int col, T const &def)
  499. {
  500. T v=T();
  501. if(!fetch(col,v))
  502. return def;
  503. return v;
  504. }
  505. ///
  506. /// Syntactic sugar, used together with into() function.
  507. ///
  508. /// res << into(x,y) is same as
  509. ///
  510. /// \code
  511. /// y = res.fetch(x) ? not_null_value : null_value
  512. /// \endcode
  513. ///
  514. template<typename T>
  515. result &operator>>(tags::into_tag<T> ref)
  516. {
  517. if(fetch(ref.value))
  518. ref.tag = not_null_value;
  519. else
  520. ref.tag = null_value;
  521. return *this;
  522. }
  523. ///
  524. /// Syntactic sugar, same as fetch(\a value)
  525. ///
  526. template<typename T>
  527. result &operator>>(T &value)
  528. {
  529. fetch(value);
  530. return *this;
  531. }
  532. private:
  533. result( ref_ptr<backend::result> res,
  534. ref_ptr<backend::statement> stat,
  535. ref_ptr<backend::connection> conn);
  536. void check();
  537. friend class statement;
  538. struct data;
  539. std::unique_ptr<data> d;
  540. bool eof_;
  541. bool fetched_;
  542. int current_col_;
  543. ref_ptr<backend::result> res_;
  544. ref_ptr<backend::statement> stat_;
  545. ref_ptr<backend::connection> conn_;
  546. };
  547. ///
  548. /// \brief This class represents a prepared (or ordinary) statement that can be executed.
  549. ///
  550. /// This object is usually created via session::prepare() function.
  551. ///
  552. class CPPDB_API statement {
  553. public:
  554. ///
  555. /// Default constructor, provided for convenience, access to any member function
  556. /// of empty statement will cause an exception being thrown.
  557. ///
  558. statement();
  559. ///
  560. /// Destructor, it releases prepared statement and if the statements cache is enabled
  561. /// it returns it into the cache.
  562. ///
  563. /// Note: if result object created by this statement is alive, the underlying backned::statement would
  564. /// be on hold until result object is destroyed and only then the statement would be put back
  565. /// into cache.
  566. ///
  567. ~statement();
  568. ///
  569. /// Copy statement.
  570. ///
  571. /// Please note it copies only the reference to underlying statement object, so copies
  572. /// of same statement represent same object and it is strongly not recommended to access the underlying
  573. /// backend::statement by two different statement objects.
  574. ///
  575. statement(statement const &);
  576. ///
  577. /// Assign a statement.
  578. ///
  579. /// Please note it copies only the reference to underlying statement object, so copies
  580. /// of same statement represent same object and it is strongly not recommended to access the underlying
  581. /// backend::statement by two different statement objects.
  582. ///
  583. statement const &operator=(statement const &);
  584. ///
  585. /// Reset the statement - remove all bindings and return it into initial state so query() or exec()
  586. /// functions can be called once again.
  587. ///
  588. /// You must use it if you use the same statement multiple times.
  589. ///
  590. /// Note, it is different from clear() where the statement is fully released and access to
  591. /// it would throw an exception
  592. ///
  593. void reset();
  594. ///
  595. /// Clear the statement, removes it, any access to statement object would throw an exception till it would
  596. /// be assigned once again
  597. ///
  598. void clear();
  599. ///
  600. /// Check if the statement is empty, it is empty when created with default constructor or when cleared
  601. /// with clear() member function.
  602. ///
  603. bool empty() const;
  604. ///
  605. /// Bind a value \a v to the next placeholder (starting from the first) marked with '?' marker in the query.
  606. ///
  607. /// If number of calls is higher then the number placeholders is the statement it
  608. /// may throw invalid_placeholder exception.
  609. ///
  610. /// If placeholder was not binded the behavior is undefined and may vary between different backends.
  611. ///
  612. statement &bind(int v);
  613. /// \copydoc bind(int)
  614. statement &bind(unsigned v);
  615. /// \copydoc bind(int)
  616. statement &bind(long v);
  617. /// \copydoc bind(int)
  618. statement &bind(unsigned long v);
  619. /// \copydoc bind(int)
  620. statement &bind(long long v);
  621. /// \copydoc bind(int)
  622. statement &bind(unsigned long long v);
  623. /// \copydoc bind(int)
  624. statement &bind(double v);
  625. /// \copydoc bind(int)
  626. statement &bind(long double v);
  627. ///
  628. /// Bind a string value \a v to the next placeholder marked with '?' marker in the query.
  629. ///
  630. /// Note: the reference to the string MUST remain valid until the statement is queried or executed!
  631. ///
  632. /// If number of calls is higher then the number placeholders is the statement it
  633. /// may throw invalid_placeholder exception.
  634. ///
  635. /// If placeholder was not binded the behavior is undefined and may vary between different backends.
  636. ///
  637. statement &bind(std::string const &v);
  638. ///
  639. /// Bind a null terminated string value \a s to the next placeholder marked with '?' marker in the query.
  640. ///
  641. /// Note: the reference to the string MUST remain valid until the statement is queried or executed!
  642. ///
  643. /// If number of calls is higher then the number placeholders is the statement it
  644. /// may throw invalid_placeholder exception.
  645. ///
  646. /// If placeholder was not binded the behavior is undefined and may vary between different backends.
  647. ///
  648. statement &bind(char const *s);
  649. ///
  650. /// Bind a string value in range [\a b, \a e ) to the next placeholder marked with '?' marker in the query.
  651. ///
  652. /// Note: the reference to the string MUST remain valid until the statement is queried or executed!
  653. ///
  654. /// If number of calls is higher then the number placeholders is the statement it
  655. /// may throw invalid_placeholder exception.
  656. ///
  657. /// If placeholder was not binded the behavior is undefined and may vary between different backends.
  658. ///
  659. statement &bind(char const *b,char const *e);
  660. /// \copydoc bind(int)
  661. statement &bind(std::tm const &v);
  662. ///
  663. /// Bind a BLOB value \a v to the next placeholder marked with '?' marker in the query.
  664. ///
  665. /// Note: the reference to the stream MUST remain valid until the statement is queried or executed!
  666. ///
  667. /// If number of calls is higher then the number placeholders is the statement it
  668. /// may throw invalid_placeholder exception.
  669. ///
  670. ///
  671. /// If placeholder was not binded the behavior is undefined and may vary between different backends.
  672. ///
  673. statement &bind(std::istream &v);
  674. ///
  675. /// Bind a NULL value to the next placeholder marked with '?' marker in the query.
  676. ///
  677. /// If number of calls is higher then the number placeholders is the statement it
  678. /// may throw invalid_placeholder exception.
  679. ///
  680. /// If placeholder was not binded the behavior is undefined and may vary between different backends.
  681. ///
  682. statement &bind_null();
  683. // Without the following statement &operator<<(T v) errors for tags::use_tag<T> as T.
  684. #ifdef __BORLANDC__
  685. template<typename T>
  686. statement &bind(tags::use_tag<T> const &val)
  687. {
  688. if(val.tag == null_value)
  689. return bind_null();
  690. else
  691. return bind(val.value);
  692. }
  693. #endif
  694. ///
  695. /// Bind a value \a v to the placeholder number \a col (starting from 1) marked with '?' marker in the query.
  696. ///
  697. /// If \a cols is invalid (less then 1 or higher then the number of the placeholders is the statement) it
  698. /// may throw invalid_placeholder exception.
  699. ///
  700. /// If placeholder was not binded the behavior is undefined and may vary between different backends.
  701. ///
  702. void bind(int col,int v);
  703. /// \copydoc bind(int,int)
  704. void bind(int col,unsigned v);
  705. /// \copydoc bind(int,int)
  706. void bind(int col,long v);
  707. /// \copydoc bind(int,int)
  708. void bind(int col,unsigned long v);
  709. /// \copydoc bind(int,int)
  710. void bind(int col,long long v);
  711. /// \copydoc bind(int,int)
  712. void bind(int col,unsigned long long v);
  713. /// \copydoc bind(int,int)
  714. void bind(int col,double v);
  715. /// \copydoc bind(int,int)
  716. void bind(int col,long double v);
  717. ///
  718. /// Bind a string value \a v to the placeholder number \a col (starting from 1) marked with '?' marker in the query.
  719. ///
  720. /// Note: the reference to the string MUST remain valid until the statement is queried or executed!
  721. ///
  722. /// If \a cols is invalid (less then 1 or higher then the number of the placeholders is the statement) it
  723. /// may throw invalid_placeholder exception.
  724. ///
  725. /// If placeholder was not binded the behavior is undefined and may vary between different backends.
  726. ///
  727. void bind(int col,std::string const &v);
  728. ///
  729. /// Bind a null terminated string value \a s to the placeholder number \a col (starting from 1) marked with '?' marker in the query.
  730. ///
  731. /// Note: the reference to the string MUST remain valid until the statement is queried or executed!
  732. ///
  733. /// If \a cols is invalid (less then 1 or higher then the number of the placeholders is the statement) it
  734. /// may throw invalid_placeholder exception.
  735. ///
  736. /// If placeholder was not binded the behavior is undefined and may vary between different backends.
  737. ///
  738. void bind(int col,char const *s);
  739. ///
  740. /// Bind a string value in range [\a b, \a e ) to the placeholder number \a col (starting from 1) marked with '?' marker in the query.
  741. ///
  742. /// Note: the reference to the string MUST remain valid until the statement is queried or executed!
  743. ///
  744. /// If \a cols is invalid (less then 1 or higher then the number of the placeholders is the statement) it
  745. /// may throw invalid_placeholder exception.
  746. ///
  747. /// If placeholder was not binded the behavior is undefined and may vary between different backends.
  748. ///
  749. void bind(int col,char const *b,char const *e);
  750. /// \copydoc bind(int,int)
  751. void bind(int col,std::tm const &v);
  752. ///
  753. /// Bind a BLOB value \a v to the placeholder number \a col (starting from 1) marked with '?' marker in the query.
  754. ///
  755. /// Note: the reference to the stream MUST remain valid until the statement is queried or executed!
  756. ///
  757. /// If \a cols is invalid (less then 1 or higher then the number of the placeholders is the statement) it
  758. /// may throw invalid_placeholder exception.
  759. ///
  760. /// If placeholder was not binded the behavior is undefined and may vary between different backends.
  761. ///
  762. void bind(int col,std::istream &v);
  763. ///
  764. /// Bind a NULL value to the placeholder number \a col (starting from 1) marked with '?' marker in the query.
  765. ///
  766. /// If \a cols is invalid (less then 1 or higher then the number of the placeholders is the statement) it
  767. /// may throw invalid_placeholder exception.
  768. ///
  769. /// If placeholder was not binded the behavior is undefined and may vary between different backends.
  770. ///
  771. void bind_null(int col);
  772. ///
  773. /// Get last insert id from the last executed statement, note, it is the same as sequence_last("").
  774. ///
  775. /// Some backends requires explicit sequence name so you should use sequence_last("sequence_name") in such
  776. /// case.
  777. ///
  778. /// If the statement is actually query, the behavior is undefined and may vary between backends.
  779. ///
  780. long long last_insert_id();
  781. ///
  782. /// Get last created sequence value from the last executed statement.
  783. ///
  784. /// If the backend does not support named sequences but rather supports "auto increment" columns (like MySQL, Sqlite3),
  785. /// the \a seq parameter is ignored.
  786. ///
  787. ///
  788. /// If the statement is actually query, the behavior is undefined and may vary between backends.
  789. ///
  790. long long sequence_last(std::string const &seq);
  791. ///
  792. /// Get the number of affected rows by the last statement,
  793. ///
  794. ///
  795. /// If the statement is actually query, the behavior is undefined and may vary between backends.
  796. ///
  797. unsigned long long affected();
  798. ///
  799. /// Fetch a single row from the query. Unlike query(), you should not call result::next()
  800. /// function as it is already called. You may check if the data was fetched using result::empty()
  801. /// function.
  802. ///
  803. /// If the result set consists of more then one row it throws multiple_rows_query exception, however some backends
  804. /// may ignore this.
  805. ///
  806. /// If the statement is not query statement (like SELECT) it would likely
  807. /// throw an exception, however the behavior may vary between backends that may ignore this error.
  808. ///
  809. result row();
  810. ///
  811. /// Fetch a result of the query, if the statement is not query statement (like SELECT) it would likely
  812. /// throw an exception, however the behavior may vary between backends that may ignore this error.
  813. ///
  814. result query();
  815. ///
  816. /// Same as query() - syntactic sugar
  817. ///
  818. operator result();
  819. ///
  820. /// Execute a statement, of the statement is actually SELECT like operator, it throws cppdb_error exception,
  821. /// however the behavior may vary between backends that may ignore this error.
  822. ///
  823. void exec();
  824. ///
  825. /// Same as bind(v);
  826. ///
  827. statement &operator<<(std::string const &v);
  828. ///
  829. /// Same as bind(s);
  830. ///
  831. statement &operator<<(char const *s);
  832. ///
  833. /// Same as bind(v);
  834. ///
  835. statement &operator<<(std::tm const &v);
  836. ///
  837. /// Same as bind(v);
  838. ///
  839. statement &operator<<(std::istream &v);
  840. ///
  841. /// Apply manipulator on the statement, same as manipulator(*this).
  842. ///
  843. statement &operator<<(void (*manipulator)(statement &st));
  844. ///
  845. /// Apply manipulator on the statement, same as manipulator(*this).
  846. ///
  847. result operator<<(result (*manipulator)(statement &st));
  848. ///
  849. /// Used together with use() function.
  850. ///
  851. /// The call st<<use(x,tag) is same as
  852. ///
  853. /// \code
  854. /// (tag == null_value) ? st.bind_null() : st.bind(x)
  855. /// \endcode
  856. ///
  857. template<typename T>
  858. statement &operator<<(tags::use_tag<T> const &val)
  859. {
  860. if(val.tag == null_value)
  861. return bind_null();
  862. else
  863. return bind(val.value);
  864. }
  865. ///
  866. /// Same as bind(v);
  867. ///
  868. template<typename T>
  869. statement &operator<<(T v)
  870. {
  871. return bind(v);
  872. }
  873. private:
  874. statement(ref_ptr<backend::statement> stat,ref_ptr<backend::connection> conn);
  875. friend class session;
  876. int placeholder_;
  877. ref_ptr<backend::statement> stat_;
  878. ref_ptr<backend::connection> conn_;
  879. struct data;
  880. std::unique_ptr<data> d;
  881. };
  882. ///
  883. /// \brief Manipulator that causes statement execution. Used as:
  884. ///
  885. /// \code
  886. /// sql << "delete from test" << cppdb::exec;
  887. /// \endcode
  888. ///
  889. inline void exec(statement &st)
  890. {
  891. st.exec();
  892. }
  893. ///
  894. /// \brief Manipulator that binds null value. Used as:
  895. ///
  896. /// \code
  897. /// sql << "insert into foo values(?,?,?)" << x << cppdb::null << y << cppdb::exec;
  898. /// \endcode
  899. ///
  900. inline void null(statement &st)
  901. {
  902. st.bind_null();
  903. }
  904. ///
  905. /// \brief Manipulator that fetches a single row. Used as:
  906. ///
  907. /// \code
  908. /// cppdb::result r = sql << "SELECT name where uid=?" << id << cppdb::row;
  909. /// if(!r.empty()) {
  910. /// ...
  911. /// }
  912. /// \endcode
  913. ///
  914. /// Or:
  915. ///
  916. /// \code
  917. /// sql << "SELECT name where uid=?" << id << cppdb::row >> name;
  918. /// \endcode
  919. ///
  920. /// Which would throw empty_row_access exception on attempt to fetch name if the result is empty.
  921. ///
  922. inline result row(statement &st)
  923. {
  924. return st.row();
  925. }
  926. ///
  927. /// \brief SQL session object that represents a single connection and is the gateway to SQL database
  928. ///
  929. /// It is the main class that is used for access to the DB, it uses various singleton classes to
  930. /// load drivers open connections and cache them.
  931. ///
  932. class CPPDB_API session {
  933. public:
  934. ///
  935. /// Create an empty session object, it should not be used until it is opened with calling open() function.
  936. ///
  937. session();
  938. ///
  939. /// Copy a session object, note - it copies only the reference to the underlying connection, so you should
  940. /// be very careful when you do it.
  941. ///
  942. session(session const &);
  943. ///
  944. /// Assign a session object, note - it copies only the reference to the underlying connection, so you should
  945. /// be very careful when you do it.
  946. ///
  947. session const &operator=(session const &);
  948. ///
  949. /// Destroys the session object, if connection pool is used it returns the object to connection pool.
  950. ///
  951. /// Note: the connection would not be returned to the pool until all statement and result objects
  952. /// created using this session are not destroyed.
  953. ///
  954. ~session();
  955. ///
  956. /// Create a session using a parsed connection string \a ci
  957. ///
  958. /// \copydetails cppdb::parse_connection_string(std::string const&,std::string&,std::map<std::string,std::string>&);
  959. ///
  960. session(connection_info const &ci);
  961. ///
  962. /// Create a session using a connection string \a cs.
  963. ///
  964. /// \copydetails cppdb::parse_connection_string(std::string const&,std::string&,std::map<std::string,std::string>&);
  965. ///
  966. session(std::string const &cs);
  967. ///
  968. /// Create a session using a parsed connection string \a ci and call \a f if
  969. /// a \ref once() was not called yet.
  970. ///
  971. /// It is useful for setting session specific options for new
  972. /// connection, not reused from the pool one.
  973. ///
  974. /// Requirements: \ref once_functor is an object that can be created from generic
  975. /// function like object func, such that func(*this) is valid expression
  976. ///
  977. /// \copydetails cppdb::parse_connection_string(std::string const&,std::string&,std::map<std::string,std::string>&);
  978. ///
  979. session(connection_info const &ci,once_functor const &f);
  980. ///
  981. /// Create a session using a connection string \a cs and call \a f if
  982. /// a \ref once() was not called yet.
  983. ///
  984. /// It is useful for setting session specific options for new
  985. /// connection, not reused from the pool one.
  986. ///
  987. /// Requirements: \ref once_functor is an object that can be created from generic
  988. /// function like object func, such that func(*this) is valid expression
  989. ///
  990. /// \copydetails cppdb::parse_connection_string(std::string const&,std::string&,std::map<std::string,std::string>&);
  991. ///
  992. session(std::string const &cs,once_functor const &f);
  993. ///
  994. /// Create a session using a pointer to backend::connection and call \a f if
  995. /// a \ref once() was not called yet.
  996. ///
  997. /// It is useful for setting session specific options for new
  998. /// connection, not reused from the pool one.
  999. ///
  1000. /// Requirements: \ref once_functor is an object that can be created from generic
  1001. /// function like object func, such that func(*this) is valid expression
  1002. ///
  1003. ///
  1004. session(ref_ptr<backend::connection> conn,once_functor const &f);
  1005. ///
  1006. /// Create a session using a pointer to backend::connection.
  1007. ///
  1008. session(ref_ptr<backend::connection> conn);
  1009. ///
  1010. /// Open a session using a connection_info object - parsed connection string \a ci.
  1011. ///
  1012. void open(connection_info const &ci);
  1013. ///
  1014. /// Open a session using a connection string \a cs.
  1015. ///
  1016. /// \copydetails cppdb::parse_connection_string(std::string const&,std::string&,std::map<std::string,std::string>&);
  1017. ///
  1018. void open(std::string const &cs);
  1019. ///
  1020. /// Close current connection, note, if connection pooling is used the connection is not actually become closed but
  1021. /// rather recycled for future use.
  1022. ///
  1023. void close();
  1024. ///
  1025. /// Check if the session was opened.
  1026. ///
  1027. bool is_open();
  1028. ///
  1029. /// Create a new statement, by default is creates prepared statement - create_prepared_statement() unless \@use_prepared connection string
  1030. /// property is set to off, then it uses normal statements by calling create_statement()
  1031. ///
  1032. /// This is the most convenient function to create statements with.
  1033. ///
  1034. statement prepare(std::string const &query);
  1035. ///
  1036. /// Syntactic sugar, same as prepare(q)
  1037. ///
  1038. statement operator<<(std::string const &q);
  1039. ///
  1040. /// Syntactic sugar, same as prepare(s)
  1041. ///
  1042. statement operator<<(char const *s);
  1043. ///
  1044. /// Create ordinary statement it generally unprepared statement and it is never cached. It should
  1045. /// be used when such statement is executed rarely or very customized.
  1046. ///
  1047. statement create_statement(std::string const &q);
  1048. ///
  1049. /// Create prepared statement that will be cached for next calls.
  1050. ///
  1051. statement create_prepared_statement(std::string const &q);
  1052. ///
  1053. /// Create prepared statement however don't use statements cache and for it. Useful for creation
  1054. /// of custom or rarely executed statements that should be executed several times at this point in program.
  1055. ///
  1056. statement create_prepared_uncached_statement(std::string const &q);
  1057. ///
  1058. /// Remove all statements from the cache.
  1059. ///
  1060. void clear_cache();
  1061. ///
  1062. /// Clear connections pool associated with this session's connection.
  1063. ///
  1064. /// Automatically calls clear_cache();
  1065. ///
  1066. void clear_pool();
  1067. ///
  1068. /// Begin a transaction. Don't use it directly for RAII reasons. Use transaction class instead.
  1069. ///
  1070. void begin();
  1071. ///
  1072. /// Commit a transaction. Don't use it directly for RAII reasons. Use transaction class instead.
  1073. ///
  1074. void commit();
  1075. ///
  1076. /// Rollback a transaction. Don't use it directly for RAII reasons. Use transaction class instead.
  1077. ///
  1078. void rollback();
  1079. ///
  1080. /// Escape a string in range [\a b,\a e) for inclusion in SQL statement. It does not add quotation marks at beginning and end.
  1081. /// It is designed to be used with text, don't use it with generic binary data.
  1082. ///
  1083. /// Some backends (odbc) may not support this.
  1084. ///
  1085. std::string escape(char const *b,char const *e);
  1086. ///
  1087. /// Escape a NULL terminated string \a s for inclusion in SQL statement. It does not add quotation marks at beginning and end.
  1088. /// It is designed to be used with text, don't use it with generic binary data.
  1089. ///
  1090. /// Some backends (odbc) may not support this.
  1091. ///
  1092. std::string escape(char const *s);
  1093. ///
  1094. /// Escape a string \a s for inclusion in SQL statement. It does not add quotation marks at beginning and end.
  1095. /// It is designed to be used with text, don't use it with generic binary data.
  1096. ///
  1097. /// Some backends (odbc) may not support this.
  1098. ///
  1099. std::string escape(std::string const &s);
  1100. ///
  1101. /// Get the driver name, as mysql, postgresql, odbc, sqlite3.
  1102. ///
  1103. std::string driver();
  1104. ///
  1105. /// Get an SQL engine name, it may be not the same as driver name for multiple engine drivers like odbc.
  1106. ///
  1107. std::string engine();
  1108. ///
  1109. /// Check if this session's connection can be recycled for reuse in a pool.
  1110. ///
  1111. /// If an exception is thrown during operation on DB this flag is reset
  1112. /// to false by the front-end classes result, statement, session.
  1113. ///
  1114. /// Default is true
  1115. ///
  1116. bool recyclable();
  1117. ///
  1118. /// Set recyclable state of the session. If some problem occurs on connection
  1119. /// that prevents its reuse it should be called with false parameter.
  1120. ///
  1121. void recyclable(bool value);
  1122. ///
  1123. /// Returns true of session specific initialization is done, otherwise returns false
  1124. ///
  1125. bool once_called();
  1126. ///
  1127. /// Set flag to true if session specific initialization is done, otherwise set it to false
  1128. ///
  1129. void once_called(bool state);
  1130. ///
  1131. /// Call the functional \a f on the connection only once. If the connection
  1132. /// created first time then f would be called. If the connection is created
  1133. /// from connection pool and thus setup functor was called, f would not be called.
  1134. ///
  1135. /// Requirements: \ref once_functor is an object that can be created from generic
  1136. /// function like object func, such that func(*this) is valid expression
  1137. ///
  1138. void once(once_functor const &f);
  1139. ///
  1140. /// Get connection specific object by its type \a t, returns 0 if not installed yet
  1141. ///
  1142. connection_specific_data *get_specific(std::type_info const &t);
  1143. ///
  1144. /// Transfers ownership on the connection specific object of type \a t, returns 0 if not installed yet
  1145. ///
  1146. connection_specific_data *release_specific(std::type_info const &t);
  1147. ///
  1148. /// Deletes connection specific object of type \a t, and sets a new one \a p (if not NULL)
  1149. ///
  1150. void reset_specific(std::type_info const &t,connection_specific_data *p=0);
  1151. ///
  1152. /// Get connection specific object by its type \a T, returns 0 if not installed yet
  1153. ///
  1154. template<typename T>
  1155. T *get_specific()
  1156. {
  1157. return static_cast<T*>(get_specific(typeid(T)));
  1158. }
  1159. ///
  1160. /// Transfers ownership on the connection specific object of type \a T, returns 0 if not installed yet
  1161. ///
  1162. template<typename T>
  1163. T *release_specific()
  1164. {
  1165. return static_cast<T*>(release_specific(typeid(T)));
  1166. }
  1167. ///
  1168. /// Deletes connection specific object of type \a T, and sets a new one \a p (if not NULL)
  1169. ///
  1170. template<typename T>
  1171. void reset_specific(T *p=0)
  1172. {
  1173. reset_specific(typeid(T),p);
  1174. }
  1175. private:
  1176. struct data;
  1177. std::unique_ptr<data> d;
  1178. ref_ptr<backend::connection> conn_;
  1179. };
  1180. ///
  1181. /// \brief The transaction guard
  1182. ///
  1183. /// This class is RAII transaction guard that causes automatic transaction rollback on stack unwind, unless
  1184. /// the transaction is committed
  1185. ///
  1186. class CPPDB_API transaction {
  1187. transaction(transaction const &);
  1188. void operator=(transaction const &);
  1189. public:
  1190. ///
  1191. /// Begin a transaction on session \a s, calls s.begin()
  1192. ///
  1193. transaction(session &s);
  1194. ///
  1195. /// If the transaction wasn't committed or rolled back calls session::rollback() for the session it was created with.
  1196. ///
  1197. ~transaction();
  1198. ///
  1199. /// Commit a transaction on the session. Calls session::commit() for the session it was created with.
  1200. ///
  1201. void commit();
  1202. ///
  1203. /// Rollback a transaction on the session. Calls session::rollback() for the session it was created with.
  1204. ///
  1205. void rollback();
  1206. private:
  1207. struct data;
  1208. session *s_;
  1209. bool commited_;
  1210. std::unique_ptr<data> d;
  1211. };
  1212. } // cppdb
  1213. #endif