nanodbc.h 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822
  1. /// \file nanodbc.h The entirety of nanodbc can be found within this file and nanodbc.cpp.
  2. /// \mainpage
  3. ///
  4. /// \section synopsis Synopsis
  5. /// This library provides a wrapper API for the native ODBC API. It aims to do everything ODBC does,
  6. /// but with a \b much nicer interface. Anything it doesn't (yet) do can be done by retrieving the
  7. /// native ODBC handles and dropping down to straight ODBC C API code.
  8. /// For more propaganda, please see the <a href="http://lexicalunit.github.com/nanodbc/">project
  9. /// homepage</a>.
  10. ///
  11. /// \section toc Table of Contents
  12. /// - \ref license "License"
  13. /// - \ref credits "Credits"
  14. /// - Source level documentation:
  15. /// - \ref nanodbc "nanodbc namespace"
  16. /// - \ref exceptions
  17. /// - \ref utility
  18. /// - \ref mainc
  19. /// - \ref mainf
  20. /// - \ref binding
  21. /// - \ref bind_multi
  22. /// - \ref bind_strings
  23. ///
  24. /// \section license License
  25. /// <div class="license">
  26. /// Copyright (C) 2013 lexicalunit <[email protected]>
  27. ///
  28. /// The MIT License
  29. ///
  30. /// Permission is hereby granted, free of charge, to any person obtaining a copy
  31. /// of this software and associated documentation files (the "Software"), to deal
  32. /// in the Software without restriction, including without limitation the rights
  33. /// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  34. /// copies of the Software, and to permit persons to whom the Software is
  35. /// furnished to do so, subject to the following conditions:
  36. ///
  37. /// The above copyright notice and this permission notice shall be included in
  38. /// all copies or substantial portions of the Software.
  39. ///
  40. /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  41. /// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  42. /// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  43. /// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  44. /// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  45. /// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  46. /// THE SOFTWARE.
  47. /// </div>
  48. ///
  49. /// \section credits Credits
  50. /// <div class="license">
  51. /// Much of the code in this file was originally derived from TinyODBC.
  52. /// TinyODBC is hosted at http://code.google.com/p/tiodbc/
  53. /// Copyright (C) 2008 SqUe [email protected]
  54. /// License: The MIT License
  55. ///
  56. /// The idea for using RAII for transactions was inspired by SimpleDB: C++ ODBC database API,
  57. /// however the code in nanodbc is original and not derived from SimpleDB. Therefore
  58. /// the LGPL license under which SimpleDB is distributed does NOT apply to nanodbc.
  59. /// SimpleDB is hosted at http://simpledb.sourceforge.net
  60. /// Copyright (C) 2006 Eminence Technology Pty Ltd
  61. /// Copyright (C) 2008-2010,2012 Russell Kliese [email protected]
  62. /// License: GNU Lesser General Public version 2.1
  63. ///
  64. /// Some improvements and features are based on The Python ODBC Library.
  65. /// The Python ODBC Library is hosted at http://code.google.com/p/pyodbc/
  66. /// License: The MIT License
  67. ///
  68. /// Implementation of column binding inspired by Nick E. Geht's source code posted to on CodeGuru.
  69. /// GSODBC hosted at http://www.codeguru.com/mfc_database/gsodbc.html
  70. /// Copyright (C) 2002 Nick E. Geht
  71. /// License: Perpetual license to reproduce, distribute, adapt, perform, display, and sublicense.
  72. /// See http://www.codeguru.com/submission-guidelines.php for details.
  73. /// </div>
  74. #ifndef NANODBC_H
  75. #define NANODBC_H
  76. #include <cstddef>
  77. #include <functional>
  78. #include <list>
  79. #include <memory>
  80. #include <stdexcept>
  81. #include <string>
  82. #include <vector>
  83. #ifndef __clang__
  84. #include <cstdint>
  85. #endif
  86. /// \brief The entirety of nanodbc can be found within this one namespace.
  87. ///
  88. /// \note This library does not make any exception safety guarantees, but should work just fine with
  89. /// a threading enabled ODBC driver. If you want to use nanodbc objects in threads I recommend
  90. /// each thread keep their own connection to the database. Otherwise you must synchronize any
  91. /// access to nanodbc objects.
  92. namespace nanodbc
  93. {
  94. // clang-format off
  95. // .d8888b. .d888 d8b 888 d8b
  96. // d88P Y88b d88P" Y8P 888 Y8P
  97. // 888 888 888 888
  98. // 888 .d88b. 88888b. 888888 888 .d88b. 888 888 888d888 8888b. 888888 888 .d88b. 88888b.
  99. // 888 d88""88b 888 "88b 888 888 d88P"88b 888 888 888P" "88b 888 888 d88""88b 888 "88b
  100. // 888 888 888 888 888 888 888 888 888 888 888 888 888 .d888888 888 888 888 888 888 888
  101. // Y88b d88P Y88..88P 888 888 888 888 Y88b 888 Y88b 888 888 888 888 Y88b. 888 Y88..88P 888 888
  102. // "Y8888P" "Y88P" 888 888 888 888 "Y88888 "Y88888 888 "Y888888 "Y888 888 "Y88P" 888 888
  103. // 888
  104. // Y8b d88P
  105. // "Y88P"
  106. // MARK: Configuration -
  107. // clang-format on
  108. /// \addtogroup macros Macros
  109. /// \brief Macros that nanodbc uses, can be overriden by users.
  110. ///
  111. /// @{
  112. #ifdef DOXYGEN
  113. /// \def NANODBC_ASSERT(expression)
  114. /// \brief Assertion.
  115. ///
  116. /// By default, nanodbc uses C \c assert() for internal assertions.
  117. /// User can override it by defining \c NANODBC_ASSERT(expr) macro
  118. /// in the nanodbc.h file and customizing it as desired,
  119. /// before building the library.
  120. ///
  121. /// \code{.cpp}
  122. /// #ifdef _DEBUG
  123. /// #include <crtdbg.h>
  124. /// #define NANODBC_ASSERT _ASSERTE
  125. /// #endif
  126. /// \endcode
  127. #define NANODBC_ASSERT(expression) assert(expression)
  128. #endif
  129. /// @}
  130. // You must explicitly request Unicode support by defining NANODBC_ENABLE_UNICODE at compile time.
  131. #ifndef DOXYGEN
  132. #ifdef NANODBC_ENABLE_UNICODE
  133. #ifdef NANODBC_USE_IODBC_WIDE_STRINGS
  134. #define NANODBC_TEXT(s) U##s
  135. typedef std::u32string string;
  136. #else
  137. #ifdef _MSC_VER
  138. typedef std::wstring string;
  139. #define NANODBC_TEXT(s) L##s
  140. #else
  141. typedef std::u16string string;
  142. #define NANODBC_TEXT(s) u##s
  143. #endif
  144. #endif
  145. #else
  146. typedef std::string string;
  147. #define NANODBC_TEXT(s) s
  148. #endif
  149. #if defined(_WIN64)
  150. // LLP64 machine: Windows
  151. typedef std::int64_t null_type;
  152. #elif !defined(_WIN64) && defined(__LP64__)
  153. // LP64 machine: OS X or Linux
  154. typedef long null_type;
  155. #else
  156. // 32-bit machine
  157. typedef long null_type;
  158. #endif
  159. #else
  160. /// \def NANODBC_TEXT(s)
  161. /// \brief Creates a string literal of the type corresponding to `nanodbc::string`.
  162. ///
  163. /// By default, the macro maps to an unprefixed string literal.
  164. /// If building with options NANODBC_ENABLE_UNICODE=ON and
  165. /// NANODBC_USE_IODBC_WIDE_STRINGS=ON specified, then it prefixes a literal with U"...".
  166. /// If only NANODBC_ENABLE_UNICODE=ON is specified, then:
  167. /// * If building with Visual Studio, then the macro prefixes a literal with L"...".
  168. /// * Otherwise, it prefixes a literal with u"...".
  169. #define NANODBC_TEXT(s) s
  170. /// \c string will be \c std::u16string or \c std::32string if \c NANODBC_ENABLE_UNICODE
  171. /// defined.
  172. ///
  173. /// Otherwise it will be \c std::string.
  174. typedef unspecified - type string;
  175. /// \c null_type will be \c int64_t for 64-bit compilations, otherwise \c long.
  176. typedef unspecified - type null_type;
  177. #endif
  178. #if defined(_MSC_VER) && _MSC_VER <= 1800
  179. // These versions of Visual C++ do not yet support \c noexcept or \c std::move.
  180. #define NANODBC_NOEXCEPT
  181. #define NANODBC_NO_MOVE_CTOR
  182. #else
  183. #define NANODBC_NOEXCEPT noexcept
  184. #endif
  185. #if __cplusplus >= 201402L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201402L)
  186. // [[deprecated]] is only available in C++14
  187. #define NANODBC_DEPRECATED [[deprecated]]
  188. #else
  189. #ifdef __GNUC__
  190. #define NANODBC_DEPRECATED __attribute__((deprecated))
  191. #elif defined(_MSC_VER)
  192. #define NANODBC_DEPRECATED __declspec(deprecated)
  193. #else
  194. #define NANODBC_DEPRECATED
  195. #endif
  196. #endif
  197. // clang-format off
  198. // 8888888888 888 888 888 888 d8b
  199. // 888 888 888 888 888 Y8P
  200. // 888 888 888 888 888
  201. // 8888888 888d888 888d888 .d88b. 888d888 8888888888 8888b. 88888b. .d88888 888 888 88888b. .d88b.
  202. // 888 888P" 888P" d88""88b 888P" 888 888 "88b 888 "88b d88" 888 888 888 888 "88b d88P"88b
  203. // 888 888 888 888 888 888 888 888 .d888888 888 888 888 888 888 888 888 888 888 888
  204. // 888 888 888 Y88..88P 888 888 888 888 888 888 888 Y88b 888 888 888 888 888 Y88b 888
  205. // 8888888888 888 888 "Y88P" 888 888 888 "Y888888 888 888 "Y88888 888 888 888 888 "Y88888
  206. // 888
  207. // Y8b d88P
  208. // "Y88P"
  209. // MARK: Error Handling -
  210. // clang-format on
  211. /// \addtogroup exceptions Exception types
  212. /// \brief Possible error conditions.
  213. ///
  214. /// Specific errors such as \c type_incompatible_error, \c null_access_error, and
  215. /// \c index_range_error can arise from improper use of the nanodbc library. The general
  216. /// \c database_error is for all other situations in which the ODBC driver or C API reports an error
  217. /// condition. The explanatory string for database_error will, if possible, contain a diagnostic
  218. /// message obtained from \c SQLGetDiagRec().
  219. /// @{
  220. /// \brief Type incompatible.
  221. /// \see exceptions
  222. class type_incompatible_error : public std::runtime_error
  223. {
  224. public:
  225. type_incompatible_error();
  226. const char* what() const NANODBC_NOEXCEPT;
  227. };
  228. /// \brief Accessed null data.
  229. /// \see exceptions
  230. class null_access_error : public std::runtime_error
  231. {
  232. public:
  233. null_access_error();
  234. const char* what() const NANODBC_NOEXCEPT;
  235. };
  236. /// \brief Index out of range.
  237. /// \see exceptions
  238. class index_range_error : public std::runtime_error
  239. {
  240. public:
  241. index_range_error();
  242. const char* what() const NANODBC_NOEXCEPT;
  243. };
  244. /// \brief Programming logic error.
  245. /// \see exceptions
  246. class programming_error : public std::runtime_error
  247. {
  248. public:
  249. explicit programming_error(const std::string& info);
  250. const char* what() const NANODBC_NOEXCEPT;
  251. };
  252. /// \brief General database error.
  253. /// \see exceptions
  254. class database_error : public std::runtime_error
  255. {
  256. public:
  257. /// \brief Creates runtime_error with message about last ODBC error.
  258. /// \param handle The native ODBC statement or connection handle.
  259. /// \param handle_type The native ODBC handle type code for the given handle.
  260. /// \param info Additional info that will be appended to the beginning of the error message.
  261. database_error(void* handle, short handle_type, const std::string& info = "");
  262. const char* what() const NANODBC_NOEXCEPT;
  263. const long native() const NANODBC_NOEXCEPT;
  264. const std::string state() const NANODBC_NOEXCEPT;
  265. private:
  266. long native_error;
  267. std::string sql_state;
  268. std::string message;
  269. };
  270. /// @}
  271. // clang-format off
  272. // 888 888 888 d8b 888 d8b 888 d8b
  273. // 888 888 888 Y8P 888 Y8P 888 Y8P
  274. // 888 888 888 888 888
  275. // 888 888 888888 888 888 888 888888 888 .d88b. .d8888b
  276. // 888 888 888 888 888 888 888 888 d8P Y8b 88K
  277. // 888 888 888 888 888 888 888 888 88888888 "Y8888b.
  278. // Y88b. .d88P Y88b. 888 888 888 Y88b. 888 Y8b. X88
  279. // "Y88888P" "Y888 888 888 888 "Y888 888 "Y8888 88888P'
  280. // MARK: Utilities -
  281. // clang-format on
  282. /// \addtogroup utility Utilities
  283. /// \brief Additional nanodbc utility classes and functions.
  284. ///
  285. /// \{
  286. /// \brief A type for representing date data.
  287. struct date
  288. {
  289. std::int16_t year; ///< Year [0-inf).
  290. std::int16_t month; ///< Month of the year [1-12].
  291. std::int16_t day; ///< Day of the month [1-31].
  292. };
  293. /// \brief A type for representing time data.
  294. struct time
  295. {
  296. std::int16_t hour; ///< Hours since midnight [0-23].
  297. std::int16_t min; ///< Minutes after the hour [0-59].
  298. std::int16_t sec; ///< Seconds after the minute.
  299. };
  300. /// \brief A type for representing timestamp data.
  301. struct timestamp
  302. {
  303. std::int16_t year; ///< Year [0-inf).
  304. std::int16_t month; ///< Month of the year [1-12].
  305. std::int16_t day; ///< Day of the month [1-31].
  306. std::int16_t hour; ///< Hours since midnight [0-23].
  307. std::int16_t min; ///< Minutes after the hour [0-59].
  308. std::int16_t sec; ///< Seconds after the minute.
  309. std::int32_t fract; ///< Fractional seconds.
  310. };
  311. /// \}
  312. /// \addtogroup mainc Main classes
  313. /// \brief Main nanodbc classes.
  314. ///
  315. /// @{
  316. // clang-format off
  317. // 88888888888 888 d8b
  318. // 888 888 Y8P
  319. // 888 888
  320. // 888 888d888 8888b. 88888b. .d8888b 8888b. .d8888b 888888 888 .d88b. 88888b.
  321. // 888 888P" "88b 888 "88b 88K "88b d88P" 888 888 d88""88b 888 "88b
  322. // 888 888 .d888888 888 888 "Y8888b. .d888888 888 888 888 888 888 888 888
  323. // 888 888 888 888 888 888 X88 888 888 Y88b. Y88b. 888 Y88..88P 888 888
  324. // 888 888 "Y888888 888 888 88888P' "Y888888 "Y8888P "Y888 888 "Y88P" 888 888
  325. // MARK: Transaction -
  326. // clang-format on
  327. /// \brief A resource for managing transaction commits and rollbacks.
  328. /// \attention You will want to use transactions if you are doing batch operations because it will
  329. /// prevent auto commits from occurring after each individual operation is executed.
  330. class transaction
  331. {
  332. public:
  333. /// \brief Begin a transaction on the given connection object.
  334. /// \post Operations that modify the database must now be committed before taking effect.
  335. /// \throws database_error
  336. explicit transaction(const class connection& conn);
  337. /// Copy constructor.
  338. transaction(const transaction& rhs);
  339. #ifndef NANODBC_NO_MOVE_CTOR
  340. /// Move constructor.
  341. transaction(transaction&& rhs) NANODBC_NOEXCEPT;
  342. #endif
  343. /// Assignment.
  344. transaction& operator=(transaction rhs);
  345. /// Member swap.
  346. void swap(transaction& rhs) NANODBC_NOEXCEPT;
  347. /// \brief If this transaction has not been committed, will will rollback any modifying ops.
  348. ~transaction() NANODBC_NOEXCEPT;
  349. /// \brief Commits transaction immediately.
  350. /// \throws database_error
  351. void commit();
  352. /// \brief Marks this transaction for rollback.
  353. void rollback() NANODBC_NOEXCEPT;
  354. /// Returns the connection object.
  355. class connection& connection();
  356. /// Returns the connection object.
  357. const class connection& connection() const;
  358. /// Returns the connection object.
  359. operator class connection&();
  360. /// Returns the connection object.
  361. operator const class connection&() const;
  362. private:
  363. class transaction_impl;
  364. friend class nanodbc::connection;
  365. private:
  366. std::shared_ptr<transaction_impl> impl_;
  367. };
  368. // clang-format off
  369. // .d8888b. 888 888 888
  370. // d88P Y88b 888 888 888
  371. // Y88b. 888 888 888
  372. // "Y888b. 888888 8888b. 888888 .d88b. 88888b.d88b. .d88b. 88888b. 888888
  373. // "Y88b. 888 "88b 888 d8P Y8b 888 "888 "88b d8P Y8b 888 "88b 888
  374. // "888 888 .d888888 888 88888888 888 888 888 88888888 888 888 888
  375. // Y88b d88P Y88b. 888 888 Y88b. Y8b. 888 888 888 Y8b. 888 888 Y88b.
  376. // "Y8888P" "Y888 "Y888888 "Y888 "Y8888 888 888 888 "Y8888 888 888 "Y888
  377. // MARK: Statement -
  378. // clang-format on
  379. /// \brief Represents a statement on the database.
  380. class statement
  381. {
  382. public:
  383. /// \brief Provides support for retrieving output/return parameters.
  384. /// \see binding
  385. enum param_direction
  386. {
  387. PARAM_IN, ///< Binding an input parameter.
  388. PARAM_OUT, ///< Binding an output parameter.
  389. PARAM_INOUT, ///< Binding an input/output parameter.
  390. PARAM_RETURN ///< Binding a return parameter.
  391. };
  392. public:
  393. /// \brief Creates a new un-prepared statement.
  394. /// \see execute(), just_execute(), execute_direct(), just_execute_direct(), open(), prepare()
  395. statement();
  396. /// \brief Constructs a statement object and associates it to the given connection.
  397. /// \param conn The connection to use.
  398. /// \see open(), prepare()
  399. explicit statement(class connection& conn);
  400. /// \brief Constructs and prepares a statement using the given connection and query.
  401. /// \param conn The connection to use.
  402. /// \param query The SQL query statement.
  403. /// \param timeout The number in seconds before query timeout. Default: 0 meaning no timeout.
  404. /// \see execute(), just_execute(), execute_direct(), just_execute_direct(), open(), prepare()
  405. statement(class connection& conn, const string& query, long timeout = 0);
  406. /// \brief Copy constructor.
  407. statement(const statement& rhs);
  408. #ifndef NANODBC_NO_MOVE_CTOR
  409. /// \brief Move constructor.
  410. statement(statement&& rhs) NANODBC_NOEXCEPT;
  411. #endif
  412. /// \brief Assignment.
  413. statement& operator=(statement rhs);
  414. /// \brief Member swap.
  415. void swap(statement& rhs) NANODBC_NOEXCEPT;
  416. /// \brief Closes the statement.
  417. /// \see close()
  418. ~statement() NANODBC_NOEXCEPT;
  419. /// \brief Creates a statement for the given connection.
  420. /// \param conn The connection where the statement will be executed.
  421. /// \throws database_error
  422. void open(class connection& conn);
  423. /// \brief Returns true if connection is open.
  424. bool open() const;
  425. /// \brief Returns true if connected to the database.
  426. bool connected() const;
  427. /// \brief Returns the associated connection object if any.
  428. class connection& connection();
  429. /// \brief Returns the associated connection object if any.
  430. const class connection& connection() const;
  431. /// \brief Returns the native ODBC statement handle.
  432. void* native_statement_handle() const;
  433. /// \brief Closes the statement and frees all associated resources.
  434. void close();
  435. /// \brief Cancels execution of the statement.
  436. /// \throws database_error
  437. void cancel();
  438. /// \brief Opens and prepares the given statement to execute on the given connection.
  439. /// \param conn The connection where the statement will be executed.
  440. /// \param query The SQL query that will be executed.
  441. /// \param timeout The number in seconds before query timeout. Default 0 meaning no timeout.
  442. /// \see open()
  443. /// \throws database_error
  444. void prepare(class connection& conn, const string& query, long timeout = 0);
  445. /// \brief Prepares the given statement to execute its associated connection.
  446. /// \note If the statement is not open throws programming_error.
  447. /// \param query The SQL query that will be executed.
  448. /// \param timeout The number in seconds before query timeout. Default 0 meaning no timeout.
  449. /// \see open()
  450. /// \throws database_error, programming_error
  451. void prepare(const string& query, long timeout = 0);
  452. /// \brief Sets the number in seconds before query timeout. Default is 0 indicating no timeout.
  453. /// \throws database_error
  454. void timeout(long timeout = 0);
  455. /// \brief Opens, prepares, and executes the given query directly on the given connection.
  456. /// \param conn The connection where the statement will be executed.
  457. /// \param query The SQL query that will be executed.
  458. /// \param batch_operations Numbers of rows to fetch per rowset, or the number of batch
  459. /// parameters to process.
  460. /// \param timeout The number in seconds before query timeout. Default 0 meaning no timeout.
  461. /// \return A result set object.
  462. /// \attention You will want to use transactions if you are doing batch operations because it
  463. /// will prevent auto commits occurring after each individual operation is executed.
  464. /// \see open(), prepare(), execute(), result, transaction
  465. class result execute_direct(
  466. class connection& conn,
  467. const string& query,
  468. long batch_operations = 1,
  469. long timeout = 0);
  470. #if !defined(NANODBC_DISABLE_ASYNC)
  471. /// \brief Prepare the given statement, in asynchronous mode.
  472. /// \note If the statement is not open throws programming_error.
  473. ///
  474. /// This method will only be available if nanodbc is built against ODBC headers and library that
  475. /// supports asynchronous mode. Such that the identifiers `SQL_ATTR_ASYNC_STMT_EVENT` and
  476. /// `SQLCompleteAsync` are extant. Otherwise this method will be defined, but not implemented.
  477. ///
  478. /// Asynchronous features can be disabled entirely by defining `NANODBC_DISABLE_ASYNC` when
  479. /// building nanodbc.
  480. ///
  481. /// \param event_handle The event handle the caller will wait before calling complete_prepare.
  482. /// \param query The SQL query that will be prepared.
  483. /// \param timeout The number in seconds before query timeout. Default 0 meaning no timeout.
  484. /// \throws database_error
  485. /// \return Boolean: true if the event handle needs to be awaited, false is result is ready now.
  486. /// \see complete_prepare()
  487. bool async_prepare(const string& query, void* event_handle, long timeout = 0);
  488. /// \brief Completes a previously initiated asynchronous query preparation.
  489. ///
  490. /// This method will only be available if nanodbc is built against ODBC headers and library that
  491. /// supports asynchronous mode. Such that the identifiers `SQL_ATTR_ASYNC_STMT_EVENT` and
  492. /// `SQLCompleteAsync` are extant. Otherwise this method will be defined, but not implemented.
  493. ///
  494. /// Asynchronous features can be disabled entirely by defining `NANODBC_DISABLE_ASYNC` when
  495. /// building nanodbc.
  496. ///
  497. /// \throws database_error
  498. /// \see async_prepare()
  499. void complete_prepare();
  500. /// \brief Opens, prepares, and executes query directly on the given connection, in async mode.
  501. ///
  502. /// This method will only be available if nanodbc is built against ODBC headers and library that
  503. /// supports asynchronous mode. Such that the identifiers `SQL_ATTR_ASYNC_STMT_EVENT` and
  504. /// `SQLCompleteAsync` are extant. Otherwise this method will be defined, but not implemented.
  505. ///
  506. /// Asynchronous features can be disabled entirely by defining `NANODBC_DISABLE_ASYNC` when
  507. /// building nanodbc.
  508. ///
  509. /// \param conn The connection where the statement will be executed.
  510. /// \param event_handle The event handle the caller will wait before calling complete_execute.
  511. /// \param query The SQL query that will be executed.
  512. /// \param batch_operations Rows to fetch per rowset or number of batch parameters to process.
  513. /// \param timeout The number in seconds before query timeout. Default 0 meaning no timeout.
  514. /// \throws database_error
  515. /// \return Boolean: true if event handle needs to be awaited, false if result ready now.
  516. /// \attention You will want to use transactions if you are doing batch operations because it
  517. /// will prevent auto commits after each individual operation is executed.
  518. /// \see complete_execute(), open(), prepare(), execute(), result, transaction
  519. bool async_execute_direct(
  520. class connection& conn,
  521. void* event_handle,
  522. const string& query,
  523. long batch_operations = 1,
  524. long timeout = 0);
  525. /// \brief Execute the previously prepared query now, in asynchronous mode.
  526. ///
  527. /// This method will only be available if nanodbc is built against ODBC headers and library that
  528. /// supports asynchronous mode. Such that the identifiers `SQL_ATTR_ASYNC_STMT_EVENT` and
  529. /// `SQLCompleteAsync` are extant. Otherwise this method will be defined, but not implemented.
  530. ///
  531. /// Asynchronous features can be disabled entirely by defining `NANODBC_DISABLE_ASYNC` when
  532. /// building nanodbc.
  533. ///
  534. /// \param event_handle The event handle the caller will wait before calling complete_execute.
  535. /// \param batch_operations Rows to fetch per rowset or number of batch parameters to process.
  536. /// \param timeout The number in seconds before query timeout. Default 0 meaning no timeout.
  537. /// \throws database_error
  538. /// \return Boolean: true if event handle needs to be awaited, false if result is ready now.
  539. /// \attention You will want to use transactions if you are doing batch operations because it
  540. /// will prevent auto commits after each individual operation is executed.
  541. /// \see complete_execute(), open(), prepare(), result, transaction
  542. bool async_execute(void* event_handle, long batch_operations = 1, long timeout = 0);
  543. /// \brief Completes a previously initiated asynchronous query execution, returning the result.
  544. ///
  545. /// This method will only be available if nanodbc is built against ODBC headers and library that
  546. /// supports asynchronous mode. Such that the identifiers `SQL_ATTR_ASYNC_STMT_EVENT` and
  547. /// `SQLCompleteAsync` are extant. Otherwise this method will be defined, but not implemented.
  548. ///
  549. /// Asynchronous features can be disabled entirely by defining `NANODBC_DISABLE_ASYNC` when
  550. /// building nanodbc.
  551. ///
  552. /// \throws database_error
  553. /// \return A result set object.
  554. /// \param batch_operations Rows to fetch per rowset or number of batch parameters to process.
  555. /// \see async_execute(), async_execute_direct()
  556. class result complete_execute(long batch_operations = 1);
  557. /// \brief Completes a previously initiated asynchronous query execution, returning the result.
  558. ///
  559. /// \deprecated Use complete_execute instead.
  560. NANODBC_DEPRECATED class result async_complete(long batch_operations = 1);
  561. /// undocumented - for internal use only (used from result_impl)
  562. void enable_async(void* event_handle);
  563. /// undocumented - for internal use only (used from result_impl)
  564. void disable_async() const;
  565. #endif
  566. /// \brief Execute the previously prepared query now without constructing result object.
  567. /// \param conn The connection where the statement will be executed.
  568. /// \param query The SQL query that will be executed.
  569. /// \param batch_operations Rows to fetch per rowset, or number of batch parameters to process.
  570. /// \param timeout Seconds before query timeout. Default is 0 indicating no timeout.
  571. /// \throws database_error
  572. /// \return A result set object.
  573. /// \attention You will want to use transactions if you are doing batch operations because it
  574. /// will prevent auto commits after each individual operation is executed.
  575. /// \see open(), prepare(), execute(), execute_direct(), result, transaction
  576. void just_execute_direct(
  577. class connection& conn,
  578. const string& query,
  579. long batch_operations = 1,
  580. long timeout = 0);
  581. /// \brief Execute the previously prepared query now.
  582. /// \param batch_operations Rows to fetch per rowset, or number of batch parameters to process.
  583. /// \param timeout The number in seconds before query timeout. Default 0 meaning no timeout.
  584. /// \throws database_error
  585. /// \return A result set object.
  586. /// \attention You will want to use transactions if you are doing batch operations because it
  587. /// will prevent auto commits after each individual operation is executed.
  588. /// \see open(), prepare(), result, transaction
  589. class result execute(long batch_operations = 1, long timeout = 0);
  590. /// \brief Execute the previously prepared query now without constructing result object.
  591. /// \param batch_operations Rows to fetch per rowset, or number of batch parameters to process.
  592. /// \param timeout The number in seconds before query timeout. Default 0 meaning no timeout.
  593. /// \throws database_error
  594. /// \return A result set object.
  595. /// \attention You will want to use transactions if you are doing batch operations because it
  596. /// will prevent auto commits after each individual operation is executed.
  597. /// \see open(), prepare(), execute(), result, transaction
  598. void just_execute(long batch_operations = 1, long timeout = 0);
  599. /// \brief Returns the input and output paramters of the specified stored procedure.
  600. /// \param catalog The catalog name of the procedure.
  601. /// \param schema Pattern to use for schema names.
  602. /// \param procedure The name of the procedure.
  603. /// \param column Pattern to use for column names.
  604. /// \throws database_error
  605. /// \return A result set object.
  606. class result procedure_columns(
  607. const string& catalog,
  608. const string& schema,
  609. const string& procedure,
  610. const string& column);
  611. /// \brief Returns rows affected by the request or -1 if affected rows is not available.
  612. /// \throws database_error
  613. long affected_rows() const;
  614. /// \brief Returns the number of columns in a result set.
  615. /// \throws database_error
  616. short columns() const;
  617. /// \brief Resets all currently bound parameters.
  618. void reset_parameters() NANODBC_NOEXCEPT;
  619. /// \brief Returns the number of parameters in the statement.
  620. /// \throws database_error
  621. short parameters() const;
  622. /// \brief Returns parameter size for indicated parameter placeholder in a prepared statement.
  623. unsigned long parameter_size(short param_index) const;
  624. /// \addtogroup binding Binding parameters
  625. /// \brief These functions are used to bind values to ODBC parameters.
  626. ///
  627. /// @{
  628. /// \brief Binds given value to given parameter placeholder number in the prepared statement.
  629. ///
  630. /// If your prepared SQL query has any ? placeholders, this is how you bind values to them.
  631. /// Placeholder numbers count from left to right and are 0-indexed.
  632. ///
  633. /// It is NOT possible to use these functions for batch operations as number of elements is not
  634. /// specified here.
  635. ///
  636. /// \param param_index Zero-based index of parameter marker (placeholder position).
  637. /// \param value Value to substitute into placeholder.
  638. /// \param direction ODBC parameter direction.
  639. /// \throws database_error
  640. template <class T>
  641. void bind(short param_index, T const* value, param_direction direction = PARAM_IN);
  642. /// \addtogroup bind_multi Binding multiple non-string values
  643. /// \brief Binds given values to given parameter placeholder number in the prepared statement.
  644. ///
  645. /// If your prepared SQL query has any parameter markers, ? (question mark) placeholders,
  646. /// this is how you bind values to them.
  647. /// Parameter markers are numbered using Zero-based index from left to right.
  648. ///
  649. /// It is possible to use these functions for batch operations.
  650. ///
  651. /// \param param_index Zero-based index of parameter marker (placeholder position).
  652. /// \param values Values to substitute into placeholder.
  653. /// \param batch_size The number of values being bound.
  654. /// \param null_sentry Value which should represent a null value.
  655. /// \param nulls Flags for values that should be set to a null value.
  656. /// \param param_direciton ODBC parameter direction.
  657. /// \throws database_error
  658. ///
  659. /// @{
  660. /// \brief Binds multiple values.
  661. /// \see bind_multi
  662. template <class T>
  663. void bind(
  664. short param_index,
  665. T const* values,
  666. std::size_t batch_size,
  667. param_direction direction = PARAM_IN);
  668. /// \brief Binds multiple values.
  669. /// \see bind_multi
  670. template <class T>
  671. void bind(
  672. short param_index,
  673. T const* values,
  674. std::size_t batch_size,
  675. T const* null_sentry,
  676. param_direction direction = PARAM_IN);
  677. /// \brief Binds multiple values.
  678. /// \see bind_multi
  679. template <class T>
  680. void bind(
  681. short param_index,
  682. T const* values,
  683. std::size_t batch_size,
  684. bool const* nulls,
  685. param_direction direction = PARAM_IN);
  686. /// \brief Binds multiple values.
  687. /// \see bind_multi
  688. void bind(
  689. short param_index,
  690. std::vector<std::vector<uint8_t>> const& values,
  691. param_direction direction = PARAM_IN);
  692. /// \brief Binds multiple values.
  693. /// \see bind_multi
  694. void bind(
  695. short param_index,
  696. std::vector<std::vector<uint8_t>> const& values,
  697. bool const* nulls,
  698. param_direction direction = PARAM_IN);
  699. /// \brief Binds multiple values.
  700. /// \see bind_multi
  701. void bind(
  702. short param_index,
  703. std::vector<std::vector<uint8_t>> const& values,
  704. uint8_t const* null_sentry,
  705. param_direction direction = PARAM_IN);
  706. /// @}
  707. /// \addtogroup bind_strings Binding multiple string values
  708. /// \brief Binds given string values to parameter marker in prepared statement.
  709. ///
  710. /// If your prepared SQL query has any parameter markers, ? (question mark) placeholders,
  711. /// this is how you bind values to them.
  712. /// Parameter markers are numbered using Zero-based index from left to right.
  713. ///
  714. /// It is possible to use these functions for batch operations.
  715. ///
  716. /// \param param_index Zero-based index of parameter marker (placeholder position).
  717. /// \param values Array of values to substitute into parameter placeholders.
  718. /// \param value_size Maximum length of string value in array.
  719. /// \param batch_size Number of string values to bind. Otherwise template parameter BatchSize is
  720. /// taken as the number of values.
  721. /// \param null_sentry Value which should represent a null value.
  722. /// \param nulls Flags for values that should be set to a null value.
  723. /// \param param_direciton ODBC parameter direction.
  724. /// \throws database_error
  725. ///
  726. /// @{
  727. /// \brief Binds multiple string values.
  728. /// \see bind_strings
  729. void bind_strings(
  730. short param_index,
  731. string::value_type const* values,
  732. std::size_t value_size,
  733. std::size_t batch_size,
  734. param_direction direction = PARAM_IN);
  735. /// \brief Binds multiple string values.
  736. ///
  737. /// Size of the values vector indicates number of values to bind.
  738. /// Longest string in the array determines maximum length of individual value.
  739. ///
  740. /// \see bind_strings
  741. void bind_strings(
  742. short param_index,
  743. std::vector<string> const& values,
  744. param_direction direction = PARAM_IN);
  745. /// \brief Binds multiple string values.
  746. /// \see bind_strings
  747. template <std::size_t BatchSize, std::size_t ValueSize>
  748. void bind_strings(
  749. short param_index,
  750. string::value_type const (&values)[BatchSize][ValueSize],
  751. param_direction direction = PARAM_IN)
  752. {
  753. auto param_values = reinterpret_cast<string::value_type const*>(values);
  754. bind_strings(param_index, param_values, ValueSize, BatchSize, direction);
  755. }
  756. /// \brief Binds multiple string values.
  757. /// \see bind_strings
  758. void bind_strings(
  759. short param_index,
  760. string::value_type const* values,
  761. std::size_t value_size,
  762. std::size_t batch_size,
  763. string::value_type const* null_sentry,
  764. param_direction direction = PARAM_IN);
  765. /// \brief Binds multiple string values.
  766. /// \see bind_strings
  767. void bind_strings(
  768. short param_index,
  769. std::vector<string> const& values,
  770. string::value_type const* null_sentry,
  771. param_direction direction = PARAM_IN);
  772. /// \brief Binds multiple string values.
  773. /// \see bind_strings
  774. template <std::size_t BatchSize, std::size_t ValueSize>
  775. void bind_strings(
  776. short param_index,
  777. string::value_type const (&values)[BatchSize][ValueSize],
  778. string::value_type const* null_sentry,
  779. param_direction direction = PARAM_IN)
  780. {
  781. auto param_values = reinterpret_cast<string::value_type const*>(values);
  782. bind_strings(param_index, param_values, ValueSize, BatchSize, null_sentry, direction);
  783. }
  784. /// \brief Binds multiple string values.
  785. /// \see bind_strings
  786. void bind_strings(
  787. short param_index,
  788. string::value_type const* values,
  789. std::size_t value_size,
  790. std::size_t batch_size,
  791. bool const* nulls,
  792. param_direction direction = PARAM_IN);
  793. /// \brief Binds multiple string values.
  794. /// \see bind_strings
  795. void bind_strings(
  796. short param_index,
  797. std::vector<string> const& values,
  798. bool const* nulls,
  799. param_direction direction = PARAM_IN);
  800. /// \brief Binds multiple string values.
  801. /// \see bind_strings
  802. template <std::size_t BatchSize, std::size_t ValueSize>
  803. void bind_strings(
  804. short param_index,
  805. string::value_type const (&values)[BatchSize][ValueSize],
  806. bool const* nulls,
  807. param_direction direction = PARAM_IN)
  808. {
  809. auto param_values = reinterpret_cast<string::value_type const*>(values);
  810. bind_strings(param_index, param_values, ValueSize, BatchSize, nulls, direction);
  811. }
  812. /// @}
  813. /// \brief Binds null values to the parameter placeholder number in the prepared statement.
  814. ///
  815. /// If your prepared SQL query has any parameter markers, ? (question mark) placeholders,
  816. /// this is how you bind values to them.
  817. /// Parameter markers are numbered using Zero-based index from left to right.
  818. ///
  819. /// It is possible to use this function for batch operations.
  820. ///
  821. /// \param param_index Zero-based index of parameter marker (placeholder position).
  822. /// \param batch_size The number of elements being bound.
  823. /// \throws database_error
  824. void bind_null(short param_index, std::size_t batch_size = 1);
  825. /// @}
  826. private:
  827. typedef std::function<bool(std::size_t)> null_predicate_type;
  828. private:
  829. class statement_impl;
  830. friend class nanodbc::result;
  831. private:
  832. std::shared_ptr<statement_impl> impl_;
  833. };
  834. // clang-format off
  835. // .d8888b. 888 d8b
  836. // d88P Y88b 888 Y8P
  837. // 888 888 888
  838. // 888 .d88b. 88888b. 88888b. .d88b. .d8888b 888888 888 .d88b. 88888b.
  839. // 888 d88""88b 888 "88b 888 "88b d8P Y8b d88P" 888 888 d88""88b 888 "88b
  840. // 888 888 888 888 888 888 888 888 88888888 888 888 888 888 888 888 888
  841. // Y88b d88P Y88..88P 888 888 888 888 Y8b. Y88b. Y88b. 888 Y88..88P 888 888
  842. // "Y8888P" "Y88P" 888 888 888 888 "Y8888 "Y8888P "Y888 888 "Y88P" 888 888
  843. // MARK: Connection -
  844. // clang-format on
  845. /// \brief Manages and encapsulates ODBC resources such as the connection and environment handles.
  846. class connection
  847. {
  848. public:
  849. /// \brief Create new connection object, initially not connected.
  850. connection();
  851. /// Copy constructor.
  852. connection(const connection& rhs);
  853. #ifndef NANODBC_NO_MOVE_CTOR
  854. /// Move constructor.
  855. connection(connection&& rhs) NANODBC_NOEXCEPT;
  856. #endif
  857. /// Assignment.
  858. connection& operator=(connection rhs);
  859. /// Member swap.
  860. void swap(connection&) NANODBC_NOEXCEPT;
  861. /// \brief Create new connection object and immediately connect to the given data source.
  862. /// \param dsn The name of the data source.
  863. /// \param user The username for authenticating to the data source.
  864. /// \param pass The password for authenticating to the data source.
  865. /// \param timeout Seconds before connection timeout. Default 0 meaning no timeout.
  866. /// \throws database_error
  867. /// \see connected(), connect()
  868. connection(const string& dsn, const string& user, const string& pass, long timeout = 0);
  869. /// \brief Create new connection object and immediately connect using the given connection
  870. /// string.
  871. /// \param connection_string The connection string for establishing a connection.
  872. /// \param timeout Seconds before connection timeout. Default is 0 indicating no timeout.
  873. /// \throws database_error
  874. /// \see connected(), connect()
  875. connection(const string& connection_string, long timeout = 0);
  876. /// \brief Automatically disconnects from the database and frees all associated resources.
  877. ///
  878. /// Will not throw even if disconnecting causes some kind of error and raises an exception.
  879. /// If you explicitly need to know if disconnect() succeeds, call it directly.
  880. ~connection() NANODBC_NOEXCEPT;
  881. /// \brief Connect to the given data source.
  882. /// \param dsn The name of the data source.
  883. /// \param user The username for authenticating to the data source.
  884. /// \param pass The password for authenticating to the data source.
  885. /// \param timeout Seconds before connection timeout. Default is 0 indicating no timeout.
  886. /// \throws database_error
  887. /// \see connected()
  888. void connect(const string& dsn, const string& user, const string& pass, long timeout = 0);
  889. /// \brief Connect using the given connection string.
  890. /// \param connection_string The connection string for establishing a connection.
  891. /// \param timeout Seconds before connection timeout. Default is 0 indicating no timeout.
  892. /// \throws database_error
  893. /// \see connected()
  894. void connect(const string& connection_string, long timeout = 0);
  895. #if !defined(NANODBC_DISABLE_ASYNC)
  896. /// \brief Initiate an asynchronous connection operation to the given data source.
  897. ///
  898. /// This method will only be available if nanodbc is built against ODBC headers and library that
  899. /// supports asynchronous mode. Such that the identifiers `SQL_ATTR_ASYNC_DBC_EVENT` and
  900. /// `SQLCompleteAsync` are extant. Otherwise this method will be defined, but not implemented.
  901. ///
  902. /// Asynchronous features can be disabled entierly by defining `NANODBC_DISABLE_ASYNC` when
  903. /// building nanodbc.
  904. ///
  905. /// \param dsn The name of the data source.
  906. /// \param user The username for authenticating to the data source.
  907. /// \param pass The password for authenticating to the data source.
  908. /// \param event_handle The event handle the caller will wait before calling async_complete.
  909. /// \param timeout Seconds before connection timeout. Default is 0 indicating no timeout.
  910. /// \throws database_error
  911. /// \return Boolean: true if event handle needs to be awaited, false if connection is ready now.
  912. /// \see connected()
  913. bool async_connect(
  914. const string& dsn,
  915. const string& user,
  916. const string& pass,
  917. void* event_handle,
  918. long timeout = 0);
  919. /// \brief Initiate an asynchronous connection operation using the given connection string.
  920. ///
  921. /// This method will only be available if nanodbc is built against ODBC headers and library that
  922. /// supports asynchronous mode. Such that the identifiers `SQL_ATTR_ASYNC_DBC_EVENT` and
  923. /// `SQLCompleteAsync` are extant. Otherwise this method will be defined, but not implemented.
  924. ///
  925. /// Asynchronous features can be disabled entierly by defining `NANODBC_DISABLE_ASYNC` when
  926. /// building nanodbc.
  927. ///
  928. /// \param connection_string The connection string for establishing a connection.
  929. /// \param event_handle Event handle the caller will wait before calling async_complete.
  930. /// \param timeout Seconds before connection timeout. Default is 0 indicating no timeout.
  931. /// \throws database_error
  932. /// \return Boolean: true if event handle needs to be awaited, false if connection is ready now.
  933. /// \see connected()
  934. bool async_connect(const string& connection_string, void* event_handle, long timeout = 0);
  935. /// \brief Completes a previously initiated asynchronous connection operation.
  936. ///
  937. /// Asynchronous features can be disabled entierly by defining `NANODBC_DISABLE_ASYNC` when
  938. /// building nanodbc.
  939. void async_complete();
  940. #endif
  941. /// \brief Returns true if connected to the database.
  942. bool connected() const;
  943. /// \brief Disconnects from the database, but maintains environment and handle resources.
  944. void disconnect();
  945. /// \brief Returns the number of transactions currently held for this connection.
  946. std::size_t transactions() const;
  947. /// \brief Returns the native ODBC database connection handle.
  948. void* native_dbc_handle() const;
  949. /// \brief Returns the native ODBC environment handle.
  950. void* native_env_handle() const;
  951. /// \brief Returns information from the ODBC connection as a string or fixed-size value.
  952. /// The general information about the driver and data source associated
  953. /// with a connection is obtained using `SQLGetInfo` function.
  954. template <class T>
  955. T get_info(short info_type) const;
  956. /// \brief Returns name of the DBMS product.
  957. /// Returns the ODBC information type SQL_DBMS_NAME of the DBMS product
  958. /// accesssed by the driver via the current connection.
  959. string dbms_name() const;
  960. /// \brief Returns version of the DBMS product.
  961. /// Returns the ODBC information type SQL_DBMS_VER of the DBMS product
  962. /// accesssed by the driver via the current connection.
  963. string dbms_version() const;
  964. /// \brief Returns the name of the ODBC driver.
  965. /// \throws database_error
  966. string driver_name() const;
  967. /// \brief Returns the name of the currently connected database.
  968. /// Returns the current SQL_DATABASE_NAME information value associated with the connection.
  969. string database_name() const;
  970. /// \brief Returns the name of the current catalog.
  971. /// Returns the current setting of the connection attribute SQL_ATTR_CURRENT_CATALOG.
  972. string catalog_name() const;
  973. private:
  974. std::size_t ref_transaction();
  975. std::size_t unref_transaction();
  976. bool rollback() const;
  977. void rollback(bool onoff);
  978. private:
  979. class connection_impl;
  980. friend class nanodbc::transaction::transaction_impl;
  981. private:
  982. std::shared_ptr<connection_impl> impl_;
  983. };
  984. // clang-format off
  985. // 8888888b. 888 888
  986. // 888 Y88b 888 888
  987. // 888 888 888 888
  988. // 888 d88P .d88b. .d8888b 888 888 888 888888
  989. // 8888888P" d8P Y8b 88K 888 888 888 888
  990. // 888 T88b 88888888 "Y8888b. 888 888 888 888
  991. // 888 T88b Y8b. X88 Y88b 888 888 Y88b.
  992. // 888 T88b "Y8888 88888P' "Y88888 888 "Y888
  993. // MARK: Result -
  994. // clang-format on
  995. class catalog;
  996. /// \brief A resource for managing result sets from statement execution.
  997. ///
  998. /// \see statement::execute(), statement::execute_direct()
  999. /// \note result objects may be copied, however all copies will refer to the same result set.
  1000. class result
  1001. {
  1002. public:
  1003. /// \brief Empty result set.
  1004. result();
  1005. /// \brief Free result set.
  1006. ~result() NANODBC_NOEXCEPT;
  1007. /// \brief Copy constructor.
  1008. result(const result& rhs);
  1009. #ifndef NANODBC_NO_MOVE_CTOR
  1010. /// \brief Move constructor.
  1011. result(result&& rhs) NANODBC_NOEXCEPT;
  1012. #endif
  1013. /// \brief Assignment.
  1014. result& operator=(result rhs);
  1015. /// \brief Member swap.
  1016. void swap(result& rhs) NANODBC_NOEXCEPT;
  1017. /// \brief Returns the native ODBC statement handle.
  1018. void* native_statement_handle() const;
  1019. /// \brief The rowset size for this result set.
  1020. long rowset_size() const NANODBC_NOEXCEPT;
  1021. /// \brief Number of affected rows by the request or -1 if the affected rows is not available.
  1022. /// \throws database_error
  1023. long affected_rows() const;
  1024. /// \brief Rows in the current rowset or 0 if the number of rows is not available.
  1025. long rows() const NANODBC_NOEXCEPT;
  1026. /// \brief Returns the number of columns in a result set.
  1027. /// \throws database_error
  1028. short columns() const;
  1029. /// \brief Fetches the first row in the current result set.
  1030. /// \return true if there are more results or false otherwise.
  1031. /// \throws database_error
  1032. bool first();
  1033. /// \brief Fetches the last row in the current result set.
  1034. /// \return true if there are more results or false otherwise.
  1035. /// \throws database_error
  1036. bool last();
  1037. /// \brief Fetches the next row in the current result set.
  1038. /// \return true if there are more results or false otherwise.
  1039. /// \throws database_error
  1040. bool next();
  1041. #if !defined(NANODBC_DISABLE_ASYNC)
  1042. /// \brief Initiates an asynchronous fetch of the next row in the current result set.
  1043. /// \return true if the caller needs to wait for the event to be signalled, false if
  1044. /// complete_next() can be called immediately.
  1045. /// \throws database_error
  1046. bool async_next(void* event_handle);
  1047. /// \brief Completes a previously-initiated async fetch for next row in the current result set.
  1048. /// \return true if there are more results or false otherwise.
  1049. /// \throws database_error
  1050. bool complete_next();
  1051. #endif
  1052. /// \brief Fetches the prior row in the current result set.
  1053. /// \return true if there are more results or false otherwise.
  1054. /// \throws database_error
  1055. bool prior();
  1056. /// \brief Moves to and fetches the specified row in the current result set.
  1057. /// \return true if there are results or false otherwise.
  1058. /// \throws database_error
  1059. bool move(long row);
  1060. /// \brief Skips a number of rows and then fetches the resulting row in the current result set.
  1061. /// \return true if there are results or false otherwise.
  1062. /// \throws database_error
  1063. bool skip(long rows);
  1064. /// \brief Returns the row position in the current result set.
  1065. unsigned long position() const;
  1066. /// \brief Returns true if there are no more results in the current result set.
  1067. bool at_end() const NANODBC_NOEXCEPT;
  1068. /// \brief Gets data from the given column of the current rowset.
  1069. ///
  1070. /// Columns are numbered from left to right and 0-indexed.
  1071. /// \param column position.
  1072. /// \param result The column's value will be written to this parameter.
  1073. /// \throws database_error, index_range_error, type_incompatible_error, null_access_error
  1074. template <class T>
  1075. void get_ref(short column, T& result) const;
  1076. /// \brief Gets data from the given column of the current rowset.
  1077. ///
  1078. /// If the data is null, fallback is returned instead.
  1079. ///
  1080. /// Columns are numbered from left to right and 0-indexed.
  1081. /// \param column position.
  1082. /// \param fallback if value is null, return fallback instead.
  1083. /// \param result The column's value will be written to this parameter.
  1084. /// \throws database_error, index_range_error, type_incompatible_error
  1085. template <class T>
  1086. void get_ref(short column, const T& fallback, T& result) const;
  1087. /// \brief Gets data from the given column by name of the current rowset.
  1088. ///
  1089. /// \param column_name column's name.
  1090. /// \param result The column's value will be written to this parameter.
  1091. /// \throws database_error, index_range_error, type_incompatible_error, null_access_error
  1092. template <class T>
  1093. void get_ref(const string& column_name, T& result) const;
  1094. /// \brief Gets data from the given column by name of the current rowset.
  1095. ///
  1096. /// If the data is null, fallback is returned instead.
  1097. ///
  1098. /// \param column_name column's name.
  1099. /// \param fallback if value is null, return fallback instead.
  1100. /// \param result The column's value will be written to this parameter.
  1101. /// \throws database_error, index_range_error, type_incompatible_error
  1102. template <class T>
  1103. void get_ref(const string& column_name, const T& fallback, T& result) const;
  1104. /// \brief Gets data from the given column of the current rowset.
  1105. ///
  1106. /// Columns are numbered from left to right and 0-indexed.
  1107. /// \param column position.
  1108. /// \throws database_error, index_range_error, type_incompatible_error, null_access_error
  1109. template <class T>
  1110. T get(short column) const;
  1111. /// \brief Gets data from the given column of the current rowset.
  1112. ///
  1113. /// If the data is null, fallback is returned instead.
  1114. ///
  1115. /// Columns are numbered from left to right and 0-indexed.
  1116. /// \param column position.
  1117. /// \param fallback if value is null, return fallback instead.
  1118. /// \throws database_error, index_range_error, type_incompatible_error
  1119. template <class T>
  1120. T get(short column, const T& fallback) const;
  1121. /// \brief Gets data from the given column by name of the current rowset.
  1122. ///
  1123. /// \param column_name column's name.
  1124. /// \throws database_error, index_range_error, type_incompatible_error, null_access_error
  1125. template <class T>
  1126. T get(const string& column_name) const;
  1127. /// \brief Gets data from the given column by name of the current rowset.
  1128. ///
  1129. /// If the data is null, fallback is returned instead.
  1130. ///
  1131. /// \param column_name column's name.
  1132. /// \param fallback if value is null, return fallback instead.
  1133. /// \throws database_error, index_range_error, type_incompatible_error
  1134. template <class T>
  1135. T get(const string& column_name, const T& fallback) const;
  1136. /// \brief Returns true if and only if the given column of the current rowset is null.
  1137. ///
  1138. /// There is a bug/limitation in ODBC drivers for SQL Server (and possibly others)
  1139. /// which causes SQLBindCol() to never write SQL_NOT_NULL to the length/indicator
  1140. /// buffer unless you also bind the data column. nanodbc's is_null() will return
  1141. /// correct values for (n)varchar(max) columns when you ensure that SQLGetData()
  1142. /// has been called for that column (i.e. after get() or get_ref() is called).
  1143. ///
  1144. /// Columns are numbered from left to right and 0-indexed.
  1145. /// \see get(), get_ref()
  1146. /// \param column position.
  1147. /// \throws database_error, index_range_error
  1148. bool is_null(short column) const;
  1149. /// \brief Returns true if and only if the given column by name of the current rowset is null.
  1150. ///
  1151. /// See is_null(short column) for details on a bug/limitation of some ODBC drivers.
  1152. /// \see is_null()
  1153. /// \param column_name column's name.
  1154. /// \throws database_error, index_range_error
  1155. bool is_null(const string& column_name) const;
  1156. /// \brief Returns the column number of the specified column name.
  1157. ///
  1158. /// Columns are numbered from left to right and 0-indexed.
  1159. /// \param column_name column's name.
  1160. /// \throws index_range_error
  1161. short column(const string& column_name) const;
  1162. /// \brief Returns the name of the specified column.
  1163. ///
  1164. /// Columns are numbered from left to right and 0-indexed.
  1165. /// \param column position.
  1166. /// \throws index_range_error
  1167. string column_name(short column) const;
  1168. /// \brief Returns the size of the specified column.
  1169. ///
  1170. /// Columns are numbered from left to right and 0-indexed.
  1171. /// \param column position.
  1172. /// \throws index_range_error
  1173. long column_size(short column) const;
  1174. /// \brief Returns the size of the specified column by name.
  1175. long column_size(const string& column_name) const;
  1176. /// \brief Returns the number of decimal digits of the specified column.
  1177. ///
  1178. /// Applies to exact numeric types (scale), datetime and interval types (prcision).
  1179. /// If the number cannot be determined or is not applicable, drivers typically return 0.
  1180. ///
  1181. /// Columns are numbered from left to right and 0-indexed.
  1182. /// \param column position.
  1183. /// \throws index_range_error
  1184. int column_decimal_digits(short column) const;
  1185. /// \brief Returns the number of decimal digits of the specified column by name.
  1186. int column_decimal_digits(const string& column_name) const;
  1187. /// \brief Returns a identifying integer value representing the SQL type of this column.
  1188. int column_datatype(short column) const;
  1189. /// \brief Returns a identifying integer value representing the SQL type of this column by name.
  1190. int column_datatype(const string& column_name) const;
  1191. /// \brief Returns data source dependent data type name of this column.
  1192. ///
  1193. /// The function calls SQLCoLAttribute with the field attribute SQL_DESC_TYPE_NAME to
  1194. /// obtain the data type name.
  1195. /// If the type is unknown, an empty string is returned.
  1196. /// \note Unlike other column metadata functions (eg. column_datatype()),
  1197. /// this function cost is an extra ODBC API call.
  1198. string column_datatype_name(short column) const;
  1199. /// \brief Returns data source dependent data type name of this column by name.
  1200. ///
  1201. /// The function calls SQLCoLAttribute with the field attribute SQL_DESC_TYPE_NAME to
  1202. /// obtain the data type name.
  1203. /// If the type is unknown, an empty string is returned.
  1204. /// \note Unlike other column metadata functions (eg. column_datatype()),
  1205. /// this function cost is an extra ODBC API call.
  1206. string column_datatype_name(const string& column_name) const;
  1207. /// \brief Returns a identifying integer value representing the C type of this column.
  1208. int column_c_datatype(short column) const;
  1209. /// \brief Returns a identifying integer value representing the C type of this column by name.
  1210. int column_c_datatype(const string& column_name) const;
  1211. /// \brief Returns the next result, e.g. when stored procedure returns multiple result sets.
  1212. bool next_result();
  1213. /// \brief If and only if result object is valid, returns true.
  1214. explicit operator bool() const;
  1215. private:
  1216. result(statement statement, long rowset_size);
  1217. private:
  1218. class result_impl;
  1219. friend class nanodbc::statement::statement_impl;
  1220. friend class nanodbc::catalog;
  1221. private:
  1222. std::shared_ptr<result_impl> impl_;
  1223. };
  1224. /// \brief Single pass input iterator that accesses successive rows in the attached result set.
  1225. class result_iterator
  1226. {
  1227. public:
  1228. typedef std::input_iterator_tag iterator_category; ///< Category of iterator.
  1229. typedef result value_type; ///< Values returned by iterator access.
  1230. typedef result* pointer; ///< Pointer to iteration values.
  1231. typedef result& reference; ///< Reference to iteration values.
  1232. typedef std::ptrdiff_t difference_type; ///< Iterator difference.
  1233. /// Default iterator; an empty result set.
  1234. result_iterator() = default;
  1235. /// Create result iterator for a given result set.
  1236. explicit result_iterator(result& r)
  1237. : result_(r)
  1238. {
  1239. ++(*this);
  1240. }
  1241. /// Dereference.
  1242. reference operator*() { return result_; }
  1243. /// Access through dereference.
  1244. pointer operator->()
  1245. {
  1246. if (!result_)
  1247. throw std::runtime_error("result is empty");
  1248. return &(operator*());
  1249. }
  1250. /// Iteration.
  1251. result_iterator& operator++()
  1252. {
  1253. try
  1254. {
  1255. if (!result_.next())
  1256. result_ = result();
  1257. }
  1258. catch (...)
  1259. {
  1260. result_ = result();
  1261. }
  1262. return *this;
  1263. }
  1264. /// Iteration.
  1265. result_iterator operator++(int)
  1266. {
  1267. result_iterator tmp(*this);
  1268. ++(*this);
  1269. return tmp;
  1270. }
  1271. /// Iterators are equal if they a tied to the same native statemnt handle, or both empty.
  1272. bool operator==(result_iterator const& rhs) const
  1273. {
  1274. if (result_ && rhs.result_)
  1275. return result_.native_statement_handle() == rhs.result_.native_statement_handle();
  1276. else
  1277. return !result_ && !rhs.result_;
  1278. }
  1279. /// Iterators are not equal if they have different native statemnt handles.
  1280. bool operator!=(result_iterator const& rhs) const { return !(*this == rhs); }
  1281. private:
  1282. result result_;
  1283. };
  1284. /// \brief Returns an iterator to the beginning of the given result set.
  1285. inline result_iterator begin(result& r)
  1286. {
  1287. return result_iterator(r);
  1288. }
  1289. /// \brief Returns an iterator to the end of a result set.
  1290. ///
  1291. /// The default-constructed `nanodbc::result_iterator` is known as the end-of-result iterator.
  1292. /// When a valid `nanodbc::result_iterator` reaches the end of the underlying result set,
  1293. /// it becomes equal to the end-of-result iterator.
  1294. /// Dereferencing or incrementing it further is undefined.
  1295. inline result_iterator end(result& /*r*/)
  1296. {
  1297. return result_iterator();
  1298. }
  1299. // clang-format off
  1300. //
  1301. // .d8888b. 888 888
  1302. // d88P Y88b 888 888
  1303. // 888 888 888 888
  1304. // 888 8888b. 888888 8888b. 888 .d88b. .d88b.
  1305. // 888 "88b 888 "88b 888 d88""88b d88P"88b
  1306. // 888 888 .d888888 888 .d888888 888 888 888 888 888
  1307. // Y88b d88P 888 888 Y88b. 888 888 888 Y88..88P Y88b 888
  1308. // "Y8888P" "Y888888 "Y888 "Y888888 888 "Y88P" "Y88888
  1309. // 888
  1310. // Y8b d88P
  1311. // "Y88P"
  1312. // MARK: Catalog -
  1313. // clang-format on
  1314. /// \brief A resource for get catalog information from connected data source.
  1315. ///
  1316. /// Queries are performed using the Catalog Functions in ODBC.
  1317. /// All provided operations are convenient wrappers around the ODBC API
  1318. /// The original ODBC behaviour should not be affected by any added processing.
  1319. class catalog
  1320. {
  1321. public:
  1322. /// \brief Result set for a list of tables in the data source.
  1323. class tables
  1324. {
  1325. public:
  1326. bool next(); ///< Move to the next result in the result set.
  1327. string table_catalog() const; ///< Fetch table catalog.
  1328. string table_schema() const; ///< Fetch table schema.
  1329. string table_name() const; ///< Fetch table name.
  1330. string table_type() const; ///< Fetch table type.
  1331. string table_remarks() const; ///< Fetch table remarks.
  1332. private:
  1333. friend class nanodbc::catalog;
  1334. tables(result& find_result);
  1335. result result_;
  1336. };
  1337. /// \brief Result set for a list of columns in one or more tables.
  1338. class columns
  1339. {
  1340. public:
  1341. bool next(); ///< Move to the next result in the result set.
  1342. string table_catalog() const; ///< Fetch table catalog.
  1343. string table_schema() const; ///< Fetch table schema.
  1344. string table_name() const; ///< Fetch table name.
  1345. string column_name() const; ///< Fetch column name.
  1346. short data_type() const; ///< Fetch column data type.
  1347. string type_name() const; ///< Fetch column type name.
  1348. long column_size() const; ///< Fetch column size.
  1349. long buffer_length() const; ///< Fetch buffer length.
  1350. short decimal_digits() const; ///< Fetch decimal digits.
  1351. short numeric_precision_radix() const; ///< Fetch numeric precission.
  1352. short nullable() const; ///< True iff column is nullable.
  1353. string remarks() const; ///< Fetch column remarks.
  1354. string column_default() const; ///< Fetch column's default.
  1355. short sql_data_type() const; ///< Fetch column's SQL data type.
  1356. short sql_datetime_subtype() const; ///< Fetch datetime subtype of column.
  1357. long char_octet_length() const; ///< Fetch char octet length.
  1358. /// \brief Ordinal position of the column in the table.
  1359. /// The first column in the table is number 1.
  1360. /// Returns ORDINAL_POSITION column value in result set returned by SQLColumns.
  1361. long ordinal_position() const;
  1362. /// \brief Fetch column is-nullable information.
  1363. ///
  1364. /// \note MSDN: This column returns a zero-length string if nullability is unknown.
  1365. /// ISO rules are followed to determine nullability.
  1366. /// An ISO SQL-compliant DBMS cannot return an empty string.
  1367. string is_nullable() const;
  1368. private:
  1369. friend class nanodbc::catalog;
  1370. columns(result& find_result);
  1371. result result_;
  1372. };
  1373. /// \brief Result set for a list of columns that compose the primary key of a single table.
  1374. class primary_keys
  1375. {
  1376. public:
  1377. bool next(); ///< Move to the next result in the result set.
  1378. string table_catalog() const; ///< Fetch table catalog.
  1379. string table_schema() const; ///< Fetch table schema.
  1380. string table_name() const; ///< Fetch table name.
  1381. string column_name() const; ///< Fetch column name.
  1382. /// \brief Column sequence number in the key (starting with 1).
  1383. /// Returns valye of KEY_SEQ column in result set returned by SQLPrimaryKeys.
  1384. short column_number() const;
  1385. /// \brief Primary key name.
  1386. /// NULL if not applicable to the data source.
  1387. /// Returns valye of PK_NAME column in result set returned by SQLPrimaryKeys.
  1388. string primary_key_name() const;
  1389. private:
  1390. friend class nanodbc::catalog;
  1391. primary_keys(result& find_result);
  1392. result result_;
  1393. };
  1394. /// \brief Result set for a list of tables and the privileges associated with each table.
  1395. class table_privileges
  1396. {
  1397. public:
  1398. bool next(); ///< Move to the next result in the result set
  1399. string table_catalog() const; ///< Fetch table catalog.
  1400. string table_schema() const; ///< Fetch table schema.
  1401. string table_name() const; ///< Fetch table name.
  1402. string grantor() const; ///< Fetch name of user who granted the privilege.
  1403. string grantee() const; ///< Fetch name of user whom the privilege was granted.
  1404. string privilege() const; ///< Fetch the table privilege.
  1405. /// Fetch indicator whether the grantee is permitted to grant the privilege to other users.
  1406. string is_grantable() const;
  1407. private:
  1408. friend class nanodbc::catalog;
  1409. table_privileges(result& find_result);
  1410. result result_;
  1411. };
  1412. /// \brief Creates catalog operating on database accessible through the specified connection.
  1413. explicit catalog(connection& conn);
  1414. /// \brief Creates result set with catalogs, schemas, tables, or table types.
  1415. ///
  1416. /// Tables information is obtained by executing `SQLTable` function within
  1417. /// scope of the connected database accessible with the specified connection.
  1418. /// Since this function is implemented in terms of the `SQLTable`s, it returns
  1419. /// result set ordered by TABLE_TYPE, TABLE_CAT, TABLE_SCHEM, and TABLE_NAME.
  1420. ///
  1421. /// All arguments are treated as the Pattern Value Arguments.
  1422. /// Empty string argument is equivalent to passing the search pattern '%'.
  1423. catalog::tables find_tables(
  1424. const string& table = string(),
  1425. const string& type = string(),
  1426. const string& schema = string(),
  1427. const string& catalog = string());
  1428. /// \brief Creates result set with tables and the privileges associated with each table.
  1429. /// Tables information is obtained by executing `SQLTablePrivileges` function within
  1430. /// scope of the connected database accessible with the specified connection.
  1431. /// Since this function is implemented in terms of the `SQLTablePrivileges`s, it returns
  1432. /// result set ordered by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, PRIVILEGE, and GRANTEE.
  1433. ///
  1434. /// \param catalog The table catalog. It cannot contain a string search pattern.
  1435. /// \param schema String search pattern for schema names, treated as the Pattern Value
  1436. /// Arguments.
  1437. /// \param table String search pattern for table names, treated as the Pattern Value Arguments.
  1438. ///
  1439. /// \note Due to the fact catalog cannot is not the Pattern Value Argument,
  1440. /// order of parameters is different than in the other catalog look-up functions.
  1441. catalog::table_privileges find_table_privileges(
  1442. const string& catalog,
  1443. const string& table = string(),
  1444. const string& schema = string());
  1445. /// \brief Creates result set with columns in one or more tables.
  1446. ///
  1447. /// Columns information is obtained by executing `SQLColumns` function within
  1448. /// scope of the connected database accessible with the specified connection.
  1449. /// Since this function is implemented in terms of the `SQLColumns`, it returns
  1450. /// result set ordered by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, and ORDINAL_POSITION.
  1451. ///
  1452. /// All arguments are treated as the Pattern Value Arguments.
  1453. /// Empty string argument is equivalent to passing the search pattern '%'.
  1454. catalog::columns find_columns(
  1455. const string& column = string(),
  1456. const string& table = string(),
  1457. const string& schema = string(),
  1458. const string& catalog = string());
  1459. /// \brief Creates result set with columns that compose the primary key of a single table.
  1460. ///
  1461. /// Returns result set with column names that make up the primary key for a table.
  1462. /// The primary key information is obtained by executing `SQLPrimaryKey` function within
  1463. /// scope of the connected database accessible with the specified connection.
  1464. ///
  1465. /// All arguments are treated as the Pattern Value Arguments.
  1466. /// Empty string argument is equivalent to passing the search pattern '%'.
  1467. catalog::primary_keys find_primary_keys(
  1468. const string& table,
  1469. const string& schema = string(),
  1470. const string& catalog = string());
  1471. /// \brief Returns names of all catalogs (or databases) available in connected data source.
  1472. ///
  1473. /// Executes `SQLTable` function with `SQL_ALL_CATALOG` as catalog search pattern.
  1474. std::list<string> list_catalogs();
  1475. /// \brief Returns names of all schemas available in connected data source.
  1476. ///
  1477. /// Executes `SQLTable` function with `SQL_ALL_SCHEMAS` as schema search pattern.
  1478. std::list<string> list_schemas();
  1479. private:
  1480. connection conn_;
  1481. };
  1482. /// @}
  1483. // clang-format off
  1484. // 8888888888 8888888888 888 d8b
  1485. // 888 888 888 Y8P
  1486. // 888 888 888
  1487. // 8888888 888d888 .d88b. .d88b. 8888888 888 888 88888b. .d8888b 888888 888 .d88b. 88888b. .d8888b
  1488. // 888 888P" d8P Y8b d8P Y8b 888 888 888 888 "88b d88P" 888 888 d88""88b 888 "88b 88K
  1489. // 888 888 88888888 88888888 888 888 888 888 888 888 888 888 888 888 888 888 "Y8888b.
  1490. // 888 888 Y8b. Y8b. 888 Y88b 888 888 888 Y88b. Y88b. 888 Y88..88P 888 888 X88
  1491. // 888 888 "Y8888 "Y8888 888 "Y88888 888 888 "Y8888P "Y888 888 "Y88P" 888 888 88888P'
  1492. // MARK: Free Functions -
  1493. // clang-format on
  1494. /// \addtogroup mainf Free Functions
  1495. /// \brief Convenience functions.
  1496. ///
  1497. /// @{
  1498. /// \brief Information on a configured ODBC driver.
  1499. struct driver
  1500. {
  1501. /// \brief Driver attributes.
  1502. struct attribute
  1503. {
  1504. nanodbc::string keyword; ///< Driver keyword attribute.
  1505. nanodbc::string value; ///< Driver attribute value.
  1506. };
  1507. nanodbc::string name; ///< Driver name.
  1508. std::list<attribute> attributes; ///< List of driver attributes.
  1509. };
  1510. /// \brief Returns a list of ODBC drivers on your system.
  1511. std::list<driver> list_drivers();
  1512. /// \brief Immediately opens, prepares, and executes the given query directly on the given
  1513. /// connection.
  1514. /// \param conn The connection where the statement will be executed.
  1515. /// \param query The SQL query that will be executed.
  1516. /// \param batch_operations Numbers of rows to fetch per rowset, or the number of batch parameters
  1517. /// to process.
  1518. /// \param timeout The number in seconds before query timeout. Default is 0 indicating no timeout.
  1519. /// \return A result set object.
  1520. /// \attention You will want to use transactions if you are doing batch operations because it will
  1521. /// prevent auto commits from occurring after each individual operation is executed.
  1522. /// \see open(), prepare(), execute(), result, transaction
  1523. result execute(connection& conn, const string& query, long batch_operations = 1, long timeout = 0);
  1524. /// \brief Opens, prepares, and executes query directly without creating result object.
  1525. /// \param conn The connection where the statement will be executed.
  1526. /// \param query The SQL query that will be executed.
  1527. /// \param batch_operations Rows to fetch per rowset, or number of batch parameters to process.
  1528. /// \param timeout The number in seconds before query timeout. Default is 0 indicating no timeout.
  1529. /// \return A result set object.
  1530. /// \attention You will want to use transactions if you are doing batch operations because it will
  1531. /// prevent auto commits from occurring after each individual operation is executed.
  1532. /// \see open(), prepare(), execute(), result, transaction
  1533. void just_execute(
  1534. connection& conn,
  1535. const string& query,
  1536. long batch_operations = 1,
  1537. long timeout = 0);
  1538. /// \brief Execute the previously prepared query now.
  1539. /// \param stmt The prepared statement that will be executed.
  1540. /// \param batch_operations Rows to fetch per rowset, or the number of batch parameters to process.
  1541. /// \throws database_error
  1542. /// \return A result set object.
  1543. /// \attention You will want to use transactions if you are doing batch operations because it will
  1544. /// prevent auto commits from occurring after each individual operation is executed.
  1545. /// \see open(), prepare(), execute(), result
  1546. result execute(statement& stmt, long batch_operations = 1);
  1547. /// \brief Execute the previously prepared query now and without creating result object.
  1548. /// \param stmt The prepared statement that will be executed.
  1549. /// \param batch_operations Rows to fetch per rowset, or the number of batch parameters to process.
  1550. /// \throws database_error
  1551. /// \return A result set object.
  1552. /// \attention You will want to use transactions if you are doing batch operations because it will
  1553. /// prevent auto commits from occurring after each individual operation is executed.
  1554. /// \see open(), prepare(), execute(), result
  1555. void just_execute(statement& stmt, long batch_operations = 1);
  1556. /// \brief Execute the previously prepared query now.
  1557. ///
  1558. /// Executes within the context of a transaction object, commits directly after execution.
  1559. /// \param stmt The prepared statement that will be executed in batch.
  1560. /// \param batch_operations Rows to fetch per rowset, or the number of batch parameters to process.
  1561. /// \throws database_error
  1562. /// \return A result set object.
  1563. /// \see open(), prepare(), execute(), result, transaction
  1564. result transact(statement& stmt, long batch_operations);
  1565. /// \brief Execute the previously prepared query now and without creating result object.
  1566. ///
  1567. /// Executes within the context of a transaction object, commits directly after execution.
  1568. /// \param stmt The prepared statement that will be executed in batch.
  1569. /// \param batch_operations Rows to fetch per rowset, or the number of batch parameters to process.
  1570. /// \throws database_error
  1571. /// \return A result set object.
  1572. /// \see open(), prepare(), execute(), result, transaction
  1573. void just_transact(statement& stmt, long batch_operations);
  1574. /// \brief Prepares the given statement to execute on it associated connection.
  1575. ///
  1576. /// If the statement is not open throws programming_error.
  1577. /// \param stmt The prepared statement that will be executed in batch.
  1578. /// \param query The SQL query that will be executed.
  1579. /// \param timeout The number in seconds before query timeout. Default is 0 indicating no timeout.
  1580. /// \see open()
  1581. /// \throws database_error, programming_error
  1582. void prepare(statement& stmt, const string& query, long timeout = 0);
  1583. /// @}
  1584. } // namespace nanodbc
  1585. #endif