nanodbc
A small C++ wrapper for the native C ODBC API
Loading...
Searching...
No Matches
nanodbc.h
Go to the documentation of this file.
1
2
76
77#ifndef NANODBC_NANODBC_H
78#define NANODBC_NANODBC_H
79
80#include <cstddef>
81#include <cstdint>
82#include <exception>
83#include <functional>
84#include <iterator>
85#include <list>
86#include <memory>
87#include <stdexcept>
88#include <string>
89#include <type_traits>
90#include <utility>
91#include <vector>
92
93#if __cplusplus >= 201703L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)
94#define NANODBC_HAS_STD_STRING_VIEW
95#include <optional>
96#define NANODBC_HAS_STD_OPTIONAL
97#include <variant>
98#define NANODBC_HAS_STD_VARIANT
99#endif
100
107namespace nanodbc
108{
109
110// clang-format off
111// .d8888b. .d888 d8b 888 d8b
112// d88P Y88b d88P" Y8P 888 Y8P
113// 888 888 888 888
114// 888 .d88b. 88888b. 888888 888 .d88b. 888 888 888d888 8888b. 888888 888 .d88b. 88888b.
115// 888 d88""88b 888 "88b 888 888 d88P"88b 888 888 888P" "88b 888 888 d88""88b 888 "88b
116// 888 888 888 888 888 888 888 888 888 888 888 888 888 .d888888 888 888 888 888 888 888
117// Y88b d88P Y88..88P 888 888 888 888 Y88b 888 Y88b 888 888 888 888 Y88b. 888 Y88..88P 888 888
118// "Y8888P" "Y88P" 888 888 888 888 "Y88888 "Y88888 888 "Y888888 "Y888 888 "Y88P" 888 888
119// 888
120// Y8b d88P
121// "Y88P"
122// MARK: Configuration -
123// clang-format on
124
129#ifdef DOXYGEN
130
136#define NANODBC_THROW_NO_SOURCE_LOCATION 1
137
152#define NANODBC_ASSERT(expression) assert(expression)
153
154#endif
156
157// You must explicitly request Unicode support by defining NANODBC_ENABLE_UNICODE at compile time.
158#ifndef DOXYGEN
159#ifdef NANODBC_ENABLE_UNICODE
160#ifdef NANODBC_USE_IODBC_WIDE_STRINGS
161#define NANODBC_TEXT(s) U##s
162typedef std::u32string string;
163#ifdef NANODBC_HAS_STD_STRING_VIEW
164typedef std::u32string_view string_view;
165#endif
166#else
167#ifdef _MSC_VER
168typedef std::wstring string;
169#ifdef NANODBC_HAS_STD_STRING_VIEW
170typedef std::wstring_view string_view;
171#endif
172#define NANODBC_TEXT(s) L##s
173#else
174typedef std::u16string string;
175#ifdef NANODBC_HAS_STD_STRING_VIEW
176typedef std::u16string_view string_view;
177#endif
178#define NANODBC_TEXT(s) u##s
179#endif
180#endif
181#else
182typedef std::string string;
183#ifdef NANODBC_HAS_STD_STRING_VIEW
184typedef std::string_view string_view;
185#endif
186#define NANODBC_TEXT(s) s
187#endif
188
189#ifdef NANODBC_USE_IODBC_WIDE_STRINGS
190typedef std::u32string wide_string;
191#ifdef NANODBC_HAS_STD_STRING_VIEW
192typedef std::u32string_view wide_string_view;
193#endif
194#else
195#ifdef _MSC_VER
196typedef std::wstring wide_string;
197#ifdef NANODBC_HAS_STD_STRING_VIEW
198typedef std::wstring_view wide_string_view;
199#endif
200#else
201typedef std::u16string wide_string;
202#ifdef NANODBC_HAS_STD_STRING_VIEW
203typedef std::u16string_view wide_string_view;
204#endif
205#endif
206#endif
207
208typedef wide_string::value_type wide_char_t;
209
210#if defined(_WIN64)
211// LLP64 machine: Windows
212typedef std::int64_t null_type;
213#elif !defined(_WIN64) && defined(__LP64__)
214// LP64 machine: OS X or Linux
215typedef long null_type;
216#else
217// 32-bit machine
218typedef long null_type;
219#endif
220#else
230#define NANODBC_TEXT(s) s
231
236typedef unspecified - type string;
238typedef unspecified - type null_type;
239#endif
240
243#define NANODBC_DEPRECATED [[deprecated]]
244
245// forward declare
246#ifndef NANODBC_DISABLE_MSSQL_TVP
247class table_valued_parameter;
248#endif
249class statement;
250class connection;
251class transaction;
252class catalog;
253class result;
254
255// clang-format off
256// 8888888888 888 888 888 888 d8b
257// 888 888 888 888 888 Y8P
258// 888 888 888 888 888
259// 8888888 888d888 888d888 .d88b. 888d888 8888888888 8888b. 88888b. .d88888 888 888 88888b. .d88b.
260// 888 888P" 888P" d88""88b 888P" 888 888 "88b 888 "88b d88" 888 888 888 888 "88b d88P"88b
261// 888 888 888 888 888 888 888 888 .d888888 888 888 888 888 888 888 888 888 888 888
262// 888 888 888 Y88..88P 888 888 888 888 888 888 888 Y88b 888 888 888 888 888 Y88b 888
263// 8888888888 888 888 "Y88P" 888 888 888 "Y888888 888 888 "Y88888 888 888 888 888 "Y88888
264// 888
265// Y8b d88P
266// "Y88P"
267// MARK: Error Handling -
268// clang-format on
269
279
282class type_incompatible_error : public std::runtime_error
283{
284public:
286
288 char const* what() const noexcept override;
289};
290
293class null_access_error : public std::runtime_error
294{
295public:
297
299 char const* what() const noexcept override;
300};
301
304class index_range_error : public std::runtime_error
305{
306public:
308
310 char const* what() const noexcept override;
311};
312
315class programming_error : public std::runtime_error
316{
317public:
320 explicit programming_error(std::string const& info);
321
323 char const* what() const noexcept override;
324};
325
328class database_error : public std::runtime_error
329{
330public:
335 database_error(void* handle, short handle_type, std::string const& info = "");
336
338 char const* what() const noexcept override;
339
341 long native() const noexcept;
342
344 std::string const& state() const noexcept;
345
346private:
347 long native_error;
348 std::string sql_state;
349 std::string message;
350};
351
353
354// clang-format off
355// 888 888 888 d8b 888 d8b 888 d8b
356// 888 888 888 Y8P 888 Y8P 888 Y8P
357// 888 888 888 888 888
358// 888 888 888888 888 888 888 888888 888 .d88b. .d8888b
359// 888 888 888 888 888 888 888 888 d8P Y8b 88K
360// 888 888 888 888 888 888 888 888 88888888 "Y8888b.
361// Y88b. .d88P Y88b. 888 888 888 Y88b. 888 Y8b. X88
362// "Y88888P" "Y888 888 888 888 "Y888 888 "Y8888 88888P'
363// MARK: Utilities -
364// clang-format on
365
370
374{
377
379 batch_ops() noexcept
380 : parameter_array_length(-1L)
381 , rowset_size(-1L) {};
382
385 batch_ops(const long all_length) noexcept
386 : parameter_array_length(all_length)
387 , rowset_size(all_length) {};
388};
389
391struct date
392{
393 std::int16_t year;
394 std::int16_t month;
395 std::int16_t day;
396};
397
399struct time
400{
401 std::int16_t hour;
402 std::int16_t min;
403 std::int16_t sec;
404};
405
408{
409 std::int16_t year;
410 std::int16_t month;
411 std::int16_t day;
412 std::int16_t hour;
413 std::int16_t min;
414 std::int16_t sec;
415 std::int32_t fract;
416};
417
420{
422 std::int16_t offset_hour;
423 std::int16_t offset_minute;
424};
425
426#ifdef NANODBC_HAS_STD_VARIANT
431// `SQLSetConnectAttr`, or `SQLSetStmtAttr`. The second is the StringLength,
432// and the third is used to inform the ValuePtr argument. This argument,
438class attribute
439{
440public:
441#ifdef NANODBC_ENABLE_UNICODE
442 typedef std::variant<std::vector<uint8_t>, string, std::string, std::intptr_t, std::uintptr_t>
443 variant;
444#else
445 typedef std::variant<std::vector<uint8_t>, string, std::intptr_t, std::uintptr_t> variant;
446#endif
447 attribute() = delete;
448 attribute& operator=(attribute const&) = delete;
449
451 attribute(attribute const& other) noexcept;
452
457 attribute(long const& attribute, long const& string_length, variant const& resource) noexcept;
458
459protected:
461 void extractValuePtr();
462
463 long attribute_;
464 long string_length_;
465 variant resource_;
466 void* value_ptr_;
467};
468#else
476{
477public:
482 attribute(long const& attribute, long const& string_length, std::uintptr_t value_ptr) noexcept
483 : attribute_(attribute)
484 , string_length_(string_length)
485 , value_ptr_((void*)value_ptr) {};
486
487protected:
491};
492#endif
493
496template <typename T>
497using is_string = std::integral_constant<
498 bool,
499 std::is_same<typename std::decay<T>::type, std::string>::value ||
500 std::is_same<typename std::decay<T>::type, wide_string>::value
501#ifdef NANODBC_HAS_STD_STRING_VIEW
502 || std::is_same<typename std::decay<T>::type, std::string_view>::value ||
503 std::is_same<typename std::decay<T>::type, wide_string_view>::value
504#endif
505 >;
506
509template <typename T>
510using is_character = std::integral_constant<
511 bool,
512 std::is_same<typename std::decay<T>::type, std::string::value_type>::value ||
513 std::is_same<typename std::decay<T>::type, wide_char_t>::value>;
514
516template <typename T>
517using enable_if_string = typename std::enable_if<is_string<T>::value>::type;
518
520template <typename T>
521using enable_if_character = typename std::enable_if<is_character<T>::value>::type;
522
524
529
530// clang-format off
531// 88888888888 888 d8b
532// 888 888 Y8P
533// 888 888
534// 888 888d888 8888b. 88888b. .d8888b 8888b. .d8888b 888888 888 .d88b. 88888b.
535// 888 888P" "88b 888 "88b 88K "88b d88P" 888 888 d88""88b 888 "88b
536// 888 888 .d888888 888 888 "Y8888b. .d888888 888 888 888 888 888 888 888
537// 888 888 888 888 888 888 X88 888 888 Y88b. Y88b. 888 Y88..88P 888 888
538// 888 888 "Y888888 888 888 88888P' "Y888888 "Y8888P "Y888 888 "Y88P" 888 888
539// MARK: Transaction -
540// clang-format on
541
546{
547public:
551 explicit transaction(const class connection& conn);
552
554 transaction(const transaction& rhs) noexcept;
555
557 transaction(transaction&& rhs) noexcept;
558
561
563 void swap(transaction& rhs) noexcept;
564
566 ~transaction() noexcept;
567
570 void commit();
571
573 void rollback() noexcept;
574
576 class connection& connection() noexcept;
577
579 const class connection& connection() const noexcept;
580
582 operator class connection &() noexcept;
583
585 operator const class connection &() const noexcept;
586
587private:
588 class transaction_impl;
589 friend class nanodbc::connection;
590
591private:
592 std::shared_ptr<transaction_impl> impl_;
593};
594
595// clang-format off
596// 888b d888 .d8888b. .d8888b. .d88888b. 888 88888888888 888 888 8888888b.
597// 8888b d8888 d88P Y88b d88P Y88b d88P" "Y88b 888 888 888 888 888 Y88b
598// 88888b.d88888 Y88b. Y88b. 888 888 888 888 888 888 888 888
599// 888Y88888P888 "Y888b. "Y888b. 888 888 888 888 Y88b d88P 888 d88P
600// 888 Y888P 888 "Y88b. "Y88b. 888 888 888 888 Y88b d88P 8888888P"
601// 888 Y8P 888 "888 "888 888 Y8b 888 888 888888 888 Y88o88P 888
602// 888 " 888 Y88b d88P Y88b d88P Y88b.Y8b88P 888 888 Y888P 888
603// 888 888 "Y8888P" "Y8888P" "Y888888" 88888888 888 Y8P 888
604// Y8b
605// MARK: MSSQL - TVP (Table Valued Parameters) -
606// clang-format on
607#ifndef NANODBC_DISABLE_MSSQL_TVP
610{
611public:
614
617
620
623 table_valued_parameter(statement& stmt, short param_index, size_t row_count);
624
627
637 void open(statement& stmt, short param_index, std::size_t row_count);
638
641 void close();
642
662
665 template <class T>
666 void bind(short param_index, T const* values, std::size_t batch_size);
667
670 template <class T>
671 void bind(short param_index, T const* values, std::size_t batch_size, T const* null_sentry);
672
675 template <class T>
676 void bind(short param_index, T const* values, std::size_t batch_size, bool const* nulls);
677
680 void bind(short param_index, std::vector<std::vector<uint8_t>> const& values);
681
684 void
685 bind(short param_index, std::vector<std::vector<uint8_t>> const& values, bool const* nulls);
686
689 void bind(
690 short param_index,
691 std::vector<std::vector<uint8_t>> const& values,
692 uint8_t const* null_sentry);
693
695
715
718 template <class T, typename = enable_if_character<T>>
719 void bind_strings(
720 short param_index,
721 T const* values,
722 std::size_t value_size,
723 std::size_t batch_size);
724
731 template <class T, typename = enable_if_string<T>>
732 void bind_strings(short param_index, std::vector<T> const& values);
733
736 template <
737 std::size_t BatchSize,
738 std::size_t ValueSize,
739 class T,
740 typename = enable_if_character<T>>
741 void bind_strings(short param_index, T const (&values)[BatchSize][ValueSize])
742 {
743 auto param_values = reinterpret_cast<T const*>(values);
744 bind_strings(param_index, param_values, ValueSize, BatchSize);
745 }
746
749 template <class T, typename = enable_if_character<T>>
751 short param_index,
752 T const* values,
753 std::size_t value_size,
754 std::size_t batch_size,
755 T const* null_sentry);
756
759 template <class T, typename = enable_if_string<T>>
761 short param_index,
762 std::vector<T> const& values,
763 typename T::value_type const* null_sentry);
764
767 template <
768 std::size_t BatchSize,
769 std::size_t ValueSize,
770 class T,
771 typename = enable_if_character<T>>
772 void
773 bind_strings(short param_index, T const (&values)[BatchSize][ValueSize], T const* null_sentry)
774 {
775 auto param_values = reinterpret_cast<T const*>(values);
776 bind_strings(param_index, param_values, ValueSize, BatchSize, nullptr, null_sentry);
777 }
778
781 template <class T, typename = enable_if_character<T>>
783 short param_index,
784 T const* values,
785 std::size_t value_size,
786 std::size_t batch_size,
787 bool const* nulls);
788
791 template <class T, typename = enable_if_string<T>>
792 void bind_strings(short param_index, std::vector<T> const& values, bool const* nulls);
793
796 template <
797 std::size_t BatchSize,
798 std::size_t ValueSize,
799 class T,
800 typename = enable_if_character<T>>
801 void bind_strings(short param_index, T const (&values)[BatchSize][ValueSize], bool const* nulls)
802 {
803 auto param_values = reinterpret_cast<T const*>(values);
804 bind_strings(param_index, param_values, ValueSize, BatchSize, nulls);
805 }
806
808
812 void bind_null(short param_index);
813
825 const std::vector<short>& idx,
826 const std::vector<short>& type,
827 const std::vector<unsigned long>& size,
828 const std::vector<short>& scale);
829
831 short parameters() const noexcept;
832
834 unsigned long parameter_size(short param_index) const;
835
837 short parameter_scale(short param_index) const;
838
840 short parameter_type(short param_index) const;
841
842private:
843 class table_valued_parameter_impl;
844 friend class statement;
845
846private:
847 std::shared_ptr<table_valued_parameter_impl> impl_;
848};
849#endif // NANODBC_DISABLE_MSSQL_TVP
850
851// clang-format off
852// .d8888b. 888 888 888
853// d88P Y88b 888 888 888
854// Y88b. 888 888 888
855// "Y888b. 888888 8888b. 888888 .d88b. 88888b.d88b. .d88b. 88888b. 888888
856// "Y88b. 888 "88b 888 d8P Y8b 888 "888 "88b d8P Y8b 888 "88b 888
857// "888 888 .d888888 888 88888888 888 888 888 88888888 888 888 888
858// Y88b d88P Y88b. 888 888 Y88b. Y8b. 888 888 888 Y8b. 888 888 Y88b.
859// "Y8888P" "Y888 "Y888888 "Y888 "Y8888 888 888 888 "Y8888 888 888 "Y888
860// MARK: Statement -
861// clang-format on
862
865{
866
867private:
868 class statement_impl;
869
870#ifdef NANODBC_HAS_STD_VARIANT
871public:
872 class attribute : public nanodbc::attribute
873 {
874 public:
875 attribute(attribute const& other) noexcept
876 : nanodbc::attribute(other) {};
877 attribute(
878 long const& attribute,
879 long const& string_length,
880 variant const& resource) noexcept
881 : nanodbc::attribute(attribute, string_length, resource) {};
882
883 private:
884 friend class nanodbc::statement::statement_impl;
885 };
886#else
887private:
888 class attribute : public nanodbc::attribute
889 {
890 public:
891 attribute(
892 long const& attribute,
893 long const& string_length,
894 std::uintptr_t value_ptr) noexcept
895 : nanodbc::attribute(attribute, string_length, value_ptr) {};
896
897 private:
898 friend class nanodbc::statement::statement_impl;
899 };
900#endif
901
902public:
913
914public:
918
922 explicit statement(class connection& conn);
923
929 explicit statement(class connection& conn, std::list<attribute> const& attributes);
930
936 statement(class connection& conn, string const& query, long timeout = 0);
937
939 statement(const statement& rhs) noexcept;
940
942 statement(statement&& rhs) noexcept;
943
946
948 void swap(statement& rhs) noexcept;
949
952 ~statement() noexcept;
953
957 void open(class connection& conn);
958
960 bool open() const noexcept;
961
963 bool connected() const noexcept;
964
966 class connection& connection() noexcept;
967
969 const class connection& connection() const noexcept;
970
972 void* native_statement_handle() const noexcept;
973
975 void close();
976
979 void cancel();
980
987 void prepare(class connection& conn, string const& query, long timeout = 0);
988
996 void prepare(string const& query, long timeout = 0);
997
1000 void timeout(long timeout = 0);
1001
1012 class result execute_direct(
1013 class connection& conn,
1014 string const& query,
1015 long batch_operations = 1,
1016 long timeout = 0);
1017
1028 class result execute_direct(
1029 class connection& conn,
1030 string const& query,
1031 batch_ops const& array_sizes,
1032 long timeout = 0);
1033
1034#if !defined(NANODBC_DISABLE_ASYNC)
1051 bool async_prepare(string const& query, void* event_handle, long timeout = 0);
1052
1065
1086 class connection& conn,
1087 void* event_handle,
1088 string const& query,
1089 long batch_operations = 1,
1090 long timeout = 0);
1091
1109 bool async_execute(void* event_handle, long batch_operations = 1, long timeout = 0);
1110
1124 class result complete_execute(long batch_operations = 1);
1125
1127 void enable_async(void* event_handle);
1128
1130 void disable_async() const;
1131#endif
1132
1143 class connection& conn,
1144 string const& query,
1145 long batch_operations = 1,
1146 long timeout = 0);
1147
1156 class result execute(long batch_operations = 1, long timeout = 0);
1157
1165 void just_execute(long batch_operations = 1, long timeout = 0);
1166
1174 class result procedure_columns(
1175 string const& catalog,
1176 string const& schema,
1177 string const& procedure,
1178 string const& column);
1179
1182 long affected_rows() const;
1183
1186 short columns() const;
1187
1189 void reset_parameters() noexcept;
1190
1193 short parameters() const;
1194
1196 unsigned long parameter_size(short param_index) const;
1197
1199 short parameter_scale(short param_index) const;
1200
1202 short parameter_type(short param_index) const;
1203
1226
1239 template <class T>
1240 void bind(short param_index, T const* value, param_direction direction = PARAM_IN);
1241
1260
1263 template <class T>
1264 void bind(
1265 short param_index,
1266 T const* values,
1267 std::size_t batch_size,
1268 param_direction direction = PARAM_IN);
1269
1272 template <class T>
1273 void bind(
1274 short param_index,
1275 T const* values,
1276 std::size_t batch_size,
1277 T const* null_sentry,
1278 param_direction direction = PARAM_IN);
1279
1282 template <class T>
1283 void bind(
1284 short param_index,
1285 T const* values,
1286 std::size_t batch_size,
1287 bool const* nulls,
1288 param_direction direction = PARAM_IN);
1289
1292 void bind(
1293 short param_index,
1294 std::vector<std::vector<uint8_t>> const& values,
1295 param_direction direction = PARAM_IN);
1296
1299 void bind(
1300 short param_index,
1301 std::vector<std::vector<uint8_t>> const& values,
1302 bool const* nulls,
1303 param_direction direction = PARAM_IN);
1304
1307 void bind(
1308 short param_index,
1309 std::vector<std::vector<uint8_t>> const& values,
1310 uint8_t const* null_sentry,
1311 param_direction direction = PARAM_IN);
1312
1314
1335
1338 template <class T, typename = enable_if_character<T>>
1339 void bind_strings(
1340 short param_index,
1341 T const* values,
1342 std::size_t value_size,
1343 std::size_t batch_size,
1344 param_direction direction = PARAM_IN);
1345
1352 template <class T, typename = enable_if_string<T>>
1353 void bind_strings(
1354 short param_index,
1355 std::vector<T> const& values,
1356 param_direction direction = PARAM_IN);
1357
1360 template <
1361 std::size_t BatchSize,
1362 std::size_t ValueSize,
1363 class T,
1364 typename = enable_if_character<T>>
1365 void bind_strings(
1366 short param_index,
1367 T const (&values)[BatchSize][ValueSize],
1368 param_direction direction = PARAM_IN)
1369 {
1370 auto param_values = reinterpret_cast<T const*>(values);
1371 bind_strings(param_index, param_values, ValueSize, BatchSize, direction);
1372 }
1373
1376 template <class T, typename = enable_if_character<T>>
1378 short param_index,
1379 T const* values,
1380 std::size_t value_size,
1381 std::size_t batch_size,
1382 T const* null_sentry,
1383 param_direction direction = PARAM_IN);
1384
1387 template <class T, typename = enable_if_string<T>>
1389 short param_index,
1390 std::vector<T> const& values,
1391 typename T::value_type const* null_sentry,
1392 param_direction direction = PARAM_IN);
1393
1396 template <
1397 std::size_t BatchSize,
1398 std::size_t ValueSize,
1399 class T,
1400 typename = enable_if_character<T>>
1402 short param_index,
1403 T const (&values)[BatchSize][ValueSize],
1404 T const* null_sentry,
1405 param_direction direction = PARAM_IN)
1406 {
1407 auto param_values = reinterpret_cast<T const*>(values);
1408 bind_strings(param_index, param_values, ValueSize, BatchSize, null_sentry, direction);
1409 }
1410
1413 template <class T, typename = enable_if_character<T>>
1415 short param_index,
1416 T const* values,
1417 std::size_t value_size,
1418 std::size_t batch_size,
1419 bool const* nulls,
1420 param_direction direction = PARAM_IN);
1421
1424 template <class T, typename = enable_if_string<T>>
1426 short param_index,
1427 std::vector<T> const& values,
1428 bool const* nulls,
1429 param_direction direction = PARAM_IN);
1430
1433 template <
1434 std::size_t BatchSize,
1435 std::size_t ValueSize,
1436 class T,
1437 typename = enable_if_character<T>>
1439 short param_index,
1440 T const (&values)[BatchSize][ValueSize],
1441 bool const* nulls,
1442 param_direction direction = PARAM_IN)
1443 {
1444 auto param_values = reinterpret_cast<T const*>(values);
1445 bind_strings(param_index, param_values, ValueSize, BatchSize, nulls, direction);
1446 }
1447
1449
1461 void bind_null(short param_index, std::size_t batch_size = 1);
1462
1464
1483 const std::vector<short>& idx,
1484 const std::vector<short>& type,
1485 const std::vector<unsigned long>& size,
1486 const std::vector<short>& scale);
1487
1488private:
1489 typedef std::function<bool(std::size_t)> null_predicate_type;
1490 friend class nanodbc::result;
1491#ifndef NANODBC_DISABLE_MSSQL_TVP
1492 friend class nanodbc::table_valued_parameter::table_valued_parameter_impl;
1493#endif
1494
1495private:
1496 std::shared_ptr<statement_impl> impl_;
1497};
1498
1499// clang-format off
1500// .d8888b. 888 d8b
1501// d88P Y88b 888 Y8P
1502// 888 888 888
1503// 888 .d88b. 88888b. 88888b. .d88b. .d8888b 888888 888 .d88b. 88888b.
1504// 888 d88""88b 888 "88b 888 "88b d8P Y8b d88P" 888 888 d88""88b 888 "88b
1505// 888 888 888 888 888 888 888 888 88888888 888 888 888 888 888 888 888
1506// Y88b d88P Y88..88P 888 888 888 888 Y8b. Y88b. Y88b. 888 Y88..88P 888 888
1507// "Y8888P" "Y88P" 888 888 888 888 "Y8888 "Y8888P "Y888 888 "Y88P" 888 888
1508// MARK: Connection -
1509// clang-format on
1510
1513{
1514
1515private:
1516 class connection_impl;
1517 friend class nanodbc::transaction::transaction_impl;
1518
1519#ifdef NANODBC_HAS_STD_VARIANT
1520public:
1521 class attribute : public nanodbc::attribute
1522 {
1523 public:
1524 attribute(attribute const& other) noexcept
1525 : nanodbc::attribute(other) {};
1526 attribute(
1527 long const& attribute,
1528 long const& string_length,
1529 variant const& resource) noexcept
1530 : nanodbc::attribute(attribute, string_length, resource) {};
1531
1532 private:
1533 friend class nanodbc::connection::connection_impl;
1534 };
1535#else
1536private:
1537 class attribute : public nanodbc::attribute
1538 {
1539 public:
1540 attribute(
1541 long const& attribute,
1542 long const& string_length,
1543 std::uintptr_t value_ptr) noexcept
1544 : nanodbc::attribute(attribute, string_length, value_ptr) {};
1545
1546 private:
1547 friend class nanodbc::connection::connection_impl;
1548 };
1549#endif
1550public:
1553
1555 connection(const connection& rhs) noexcept;
1556
1558 connection(connection&& rhs) noexcept;
1559
1562
1564 void swap(connection&) noexcept;
1565
1576 connection(string const& dsn, string const& user, string const& pass, long timeout = 0);
1577
1587 explicit connection(string const& connection_string, long timeout = 0);
1588
1589#ifdef NANODBC_HAS_STD_VARIANT
1602
1603 connection(
1604 string const& dsn,
1605 string const& user,
1606 string const& pass,
1607 std::list<attribute> const& attributes);
1618 connection(string const& connection_string, std::list<attribute> const& attributes);
1619#endif
1624 ~connection() noexcept;
1625
1634 void allocate();
1635
1638 void deallocate();
1639
1647 void connect(string const& dsn, string const& user, string const& pass, long timeout = 0);
1648
1654 void connect(string const& connection_string, long timeout = 0);
1655
1656#ifdef NANODBC_HAS_STD_VARIANT
1665 void connect(
1666 string const& dsn,
1667 string const& user,
1668 string const& pass,
1669 std::list<attribute> const& attributes);
1670
1677 void connect(string const& connection_string, std::list<attribute> const& attributes);
1678#endif
1679#if !defined(NANODBC_DISABLE_ASYNC)
1698 string const& dsn,
1699 string const& user,
1700 string const& pass,
1701 void* event_handle,
1702 long timeout = 0);
1703
1719 bool async_connect(string const& connection_string, void* event_handle, long timeout = 0);
1720
1726#endif
1727
1729 bool connected() const noexcept;
1730
1732 void disconnect();
1733
1735 std::size_t transactions() const noexcept;
1736
1738 void* native_dbc_handle() const noexcept;
1739
1741 void* native_env_handle() const noexcept;
1742
1746 template <class T>
1747 T get_info(short info_type) const;
1748
1752 string dbms_name() const;
1753
1757 string dbms_version() const;
1758
1761 string driver_name() const;
1762
1765 string driver_version() const;
1766
1769 string database_name() const;
1770
1773 string catalog_name() const;
1774
1775private:
1776 std::size_t ref_transaction() noexcept;
1777 std::size_t unref_transaction() noexcept;
1778 bool rollback() const noexcept;
1779 void rollback(bool onoff) noexcept;
1780
1781private:
1782 std::shared_ptr<connection_impl> impl_;
1783};
1784
1785// clang-format off
1786// 8888888b. 888 888
1787// 888 Y88b 888 888
1788// 888 888 888 888
1789// 888 d88P .d88b. .d8888b 888 888 888 888888
1790// 8888888P" d8P Y8b 88K 888 888 888 888
1791// 888 T88b 88888888 "Y8888b. 888 888 888 888
1792// 888 T88b Y8b. X88 Y88b 888 888 Y88b.
1793// 888 T88b "Y8888 88888P' "Y88888 888 "Y888
1794// MARK: Result -
1795// clang-format on
1796
1797class catalog;
1798class variant_row_cached_result;
1799
1805{
1806public:
1808 result() noexcept;
1809
1811 ~result() noexcept;
1812
1814 result(const result& rhs) noexcept;
1815
1817 result(result&& rhs) noexcept;
1818
1820 result& operator=(result rhs) noexcept;
1821
1823 void swap(result& rhs) noexcept;
1824
1826 void* native_statement_handle() const noexcept;
1827
1829 long rowset_size() const noexcept;
1830
1833 long affected_rows() const;
1834
1843 bool has_affected_rows() const;
1844
1846 long rows() const noexcept;
1847
1850 short columns() const;
1851
1855 bool first();
1856
1860 bool last();
1861
1865 bool next();
1866
1867#if !defined(NANODBC_DISABLE_ASYNC)
1872 bool async_next(void* event_handle);
1873
1878#endif
1879
1883 bool prior();
1884
1888 bool move(long row);
1889
1893 bool skip(long rows);
1894
1896 unsigned long position() const;
1897
1899 bool at_end() const noexcept;
1900
1906 void unbind();
1907
1915 void unbind(string const& column_name);
1916
1932 void unbind(short column);
1933
1950
1960 template <class T>
1961 void get_ref(short column, T& result) const;
1962
1974 template <class T>
1975 void get_ref(short column, T const& fallback, T& result) const;
1976
1985 template <class T>
1986 void get_ref(string const& column_name, T& result) const;
1987
1998 template <class T>
1999 void get_ref(string const& column_name, T const& fallback, T& result) const;
2000
2009 template <class T>
2010 T get(short column) const;
2011
2022 template <class T>
2023 T get(short column, T const& fallback) const;
2024
2032 template <class T>
2033 T get(string const& column_name) const;
2034
2044 template <class T>
2045 T get(string const& column_name, T const& fallback) const;
2046
2048
2070 bool is_null(short column) const;
2071
2079 bool is_null(string const& column_name) const;
2080
2091 bool is_bound(short column) const;
2092
2099 bool is_bound(string const& column_name) const;
2100
2106 short column(string const& column_name) const;
2107
2113 string column_name(short column) const;
2114
2120 long column_size(short column) const;
2121
2123 long column_size(string const& column_name) const;
2124
2133 int column_decimal_digits(short column) const;
2134
2136 int column_decimal_digits(string const& column_name) const;
2137
2139 int column_datatype(short column) const;
2140
2142 int column_datatype(string const& column_name) const;
2143
2151 string column_datatype_name(short column) const;
2152
2160 string column_datatype_name(string const& column_name) const;
2161
2163 int column_c_datatype(short column) const;
2164
2166 int column_c_datatype(string const& column_name) const;
2167
2175 bool column_unsigned(short column) const;
2176
2179 bool column_unsigned(string const& column_name) const;
2180
2182 bool next_result();
2183
2185 explicit operator bool() const noexcept;
2186
2187private:
2188 result(statement statement, long rowset_size);
2189
2190private:
2191 class result_impl;
2192 friend class nanodbc::statement::statement_impl;
2193 friend class nanodbc::catalog;
2194#ifdef _MSC_VER
2195 friend class nanodbc::variant_row_cached_result;
2196#endif
2197
2198private:
2199 std::shared_ptr<result_impl> impl_;
2200};
2201
2204{
2205public:
2206 typedef std::input_iterator_tag iterator_category;
2208 typedef result* pointer;
2210 typedef std::ptrdiff_t difference_type;
2211
2213 result_iterator() = default;
2214
2217 : result_(r)
2218 {
2219 ++(*this);
2220 }
2221
2223 reference operator*() noexcept { return result_; }
2224
2227 {
2228 if (!result_)
2229 throw std::runtime_error("result is empty");
2230 return &(operator*());
2231 }
2232
2235 {
2236 try
2237 {
2238 if (!result_.next())
2239 result_ = result();
2240 }
2241 catch (...)
2242 {
2243 result_ = result();
2244 }
2245 return *this;
2246 }
2247
2253 void operator++(int) { ++(*this); }
2254
2256 bool operator==(result_iterator const& rhs) const noexcept
2257 {
2258 if (result_ && rhs.result_)
2259 return result_.native_statement_handle() == rhs.result_.native_statement_handle();
2260 else
2261 return !result_ && !rhs.result_;
2262 }
2263
2265 bool operator!=(result_iterator const& rhs) const noexcept { return !(*this == rhs); }
2266
2267private:
2268 result result_;
2269};
2270
2273{
2274 return result_iterator(r);
2275}
2276
2283inline result_iterator end(result& /*r*/) noexcept
2284{
2285 return {};
2286}
2287
2288// clang-format off
2289// 8888888b. d8b 888
2290// 888 "Y88b Y8P 888
2291// 888 888 888
2292// 888 888 .d88b. .d8888b .d8888b 888d888 888 88888b. 888888 .d88b. 888d888 .d8888b
2293// 888 888 d8P Y8b 88K d88P" 888P" 888 888 "88b 888 d88""88b 888P" 88K
2294// 888 888 88888888 "Y8888b. 888 888 888 888 888 888 888 888 888 "Y8888b.
2295// 888 .d88P Y8b. X88 Y88b. 888 888 888 d88P Y88b. Y88..88P 888 X88
2296// 8888888P" "Y8888 88888P' "Y8888P 888 888 88888P" "Y888 "Y88P" 888 88888P'
2297// 888
2298// 888
2299// 888
2300// MARK: Descriptors -
2301// clang-format on
2302
2309{
2310public:
2313
2326
2328 auto alloc_type() const -> short;
2329
2331 auto count() const noexcept -> short;
2332
2333 // Descriptor record fields (records) accessors
2334
2336 auto auto_unique_value(short record) const -> bool;
2337
2339 auto base_column_name(short record) const -> string;
2340
2342 auto base_table_name(short record) const -> string;
2343
2345 auto case_sensitive(short record) const -> bool;
2346
2348 auto catalog_name(short record) const -> string;
2349
2351 auto concise_type(short record) const -> short;
2352
2354 auto display_size(short record) const -> std::int64_t;
2355
2357 auto fixed_prec_scale(short record) const -> short;
2358
2360 auto label(short record) const -> string;
2361
2363 auto length(short record) const -> std::uint64_t;
2364
2366 auto local_type_name(short record) const -> string;
2367
2369 auto name(short record) const -> string;
2370
2374 auto nullable(short record) const -> short;
2375
2377 auto num_prec_radix(short record) const -> short;
2378
2380 auto octet_length(short record) const -> std::int64_t;
2381
2383 auto precision(short record) const -> short;
2384
2386 auto rowver(short record) const -> short;
2387
2389 auto scale(short record) const -> short;
2390
2392 auto schema_name(short record) const -> string;
2393
2398 auto searchable(short record) const -> short;
2399
2401 auto table_name(short record) const -> string;
2402
2404 auto type(short record) const -> short;
2405
2407 auto type_name(short record) const -> string;
2408
2410 auto unnamed(short record) const -> bool;
2411
2413 auto unsigned_(short record) const -> bool;
2414
2419 auto updatable(short record) const -> short;
2420
2421private:
2422 // Convenience wrapper for SQLGetDescrField accesor.
2423 struct sql_get_descr_field
2424 {
2425 sql_get_descr_field(
2427 short record,
2428 std::uint16_t field_identifier) noexcept;
2429 operator std::int64_t() const;
2430 operator std::uint64_t() const;
2431 operator string() const;
2432
2434 short record_;
2435 std::uint16_t field_identifier_;
2436 };
2437 friend sql_get_descr_field;
2438
2439 void initialize_descriptor();
2440 void throw_if_record_is_out_of_range(short record) const;
2441
2442 void* statement_handle_{nullptr};
2443 short statement_columns_count_{0};
2444 void* descriptor_handle_{nullptr};
2445 short descriptor_records_count_{0};
2446};
2447
2448// clang-format off
2449//
2450// .d8888b. 888 888
2451// d88P Y88b 888 888
2452// 888 888 888 888
2453// 888 8888b. 888888 8888b. 888 .d88b. .d88b.
2454// 888 "88b 888 "88b 888 d88""88b d88P"88b
2455// 888 888 .d888888 888 .d888888 888 888 888 888 888
2456// Y88b d88P 888 888 Y88b. 888 888 888 Y88..88P Y88b 888
2457// "Y8888P" "Y888888 "Y888 "Y888888 888 "Y88P" "Y88888
2458// 888
2459// Y8b d88P
2460// "Y88P"
2461// MARK: Catalog -
2462// clang-format on
2463
2470{
2471public:
2474 {
2475 public:
2476 bool next();
2477 string table_catalog() const;
2478 string table_schema() const;
2479 string table_name() const;
2480 string table_type() const;
2481 string table_remarks() const;
2482
2483 private:
2484 friend class nanodbc::catalog;
2485 explicit tables(result& find_result) noexcept;
2486 result result_;
2487 };
2488
2491 {
2492 public:
2493 bool next();
2494 string table_catalog() const;
2495 string table_schema() const;
2496 string table_name() const;
2497 string column_name() const;
2498 short data_type() const;
2499 string type_name() const;
2500 long column_size() const;
2501 long buffer_length() const;
2502 short decimal_digits() const;
2504 short nullable() const;
2505 string remarks() const;
2506 string column_default() const;
2507 short sql_data_type() const;
2508 short sql_datetime_subtype() const;
2509 long char_octet_length() const;
2510
2514 long ordinal_position() const;
2515
2521 string is_nullable() const;
2522
2523 private:
2524 friend class nanodbc::catalog;
2525 explicit columns(result& find_result) noexcept;
2526 result result_;
2527 };
2528
2531 {
2532 public:
2533 bool next();
2534 string table_catalog() const;
2535 string table_schema() const;
2536 string table_name() const;
2537 string column_name() const;
2538
2541 short column_number() const;
2542
2546 string primary_key_name() const;
2547
2548 private:
2549 friend class nanodbc::catalog;
2550 explicit primary_keys(result& find_result) noexcept;
2551 result result_;
2552 };
2553
2556 {
2557 public:
2558 bool next();
2559 string table_catalog() const;
2560 string table_schema() const;
2561 string table_name() const;
2562 string grantor() const;
2563 string grantee() const;
2564 string privilege() const;
2566 string is_grantable() const;
2567
2568 private:
2569 friend class nanodbc::catalog;
2570 explicit table_privileges(result& find_result) noexcept;
2571 result result_;
2572 };
2573
2576 {
2577 public:
2578 bool next();
2579 string procedure_catalog() const;
2580 string procedure_schema() const;
2581 string procedure_name() const;
2582 string procedure_remarks() const;
2583 short procedure_type() const;
2584
2585 private:
2586 friend class nanodbc::catalog;
2587 explicit procedures(result& find_result) noexcept;
2588 result result_;
2589 };
2590
2593 {
2594 public:
2595 bool next();
2596 string procedure_catalog() const;
2597 string procedure_schema() const;
2598 string procedure_name() const;
2599 string column_name() const;
2600 short column_type() const;
2601 short data_type() const;
2602 string type_name() const;
2603 long column_size() const;
2604 long buffer_length() const;
2605 short decimal_digits() const;
2607 short nullable() const;
2608 string remarks() const;
2609 string column_default() const;
2610 short sql_data_type() const;
2611 short sql_datetime_subtype() const;
2612 long char_octet_length() const;
2613
2617 long ordinal_position() const;
2618
2624 string is_nullable() const;
2625
2626 private:
2627 friend class nanodbc::catalog;
2628 explicit procedure_columns(result& find_result) noexcept;
2629 result result_;
2630 };
2631
2633 explicit catalog(connection& conn) noexcept;
2634
2645 string const& table = string(),
2646 string const& type = string(),
2647 string const& schema = string(),
2648 string const& catalog = string());
2649
2664 string const& catalog,
2665 string const& table = string(),
2666 string const& schema = string());
2667
2678 string const& column = string(),
2679 string const& table = string(),
2680 string const& schema = string(),
2681 string const& catalog = string());
2682
2692 string const& table,
2693 string const& schema = string(),
2694 string const& catalog = string());
2695
2705
2707 string const& procedure = string(),
2708 string const& schema = string(),
2709 string const& catalog = string());
2710
2722 string const& column = string(),
2723 string const& procedure = string(),
2724 string const& schema = string(),
2725 string const& catalog = string());
2726
2730 std::list<string> list_catalogs();
2731
2735 std::list<string> list_schemas();
2736
2741 std::list<string> list_table_types();
2742
2743private:
2744 connection conn_;
2745};
2746
2748
2749// clang-format off
2750// 8888888888 8888888888 888 d8b
2751// 888 888 888 Y8P
2752// 888 888 888
2753// 8888888 888d888 .d88b. .d88b. 8888888 888 888 88888b. .d8888b 888888 888 .d88b. 88888b. .d8888b
2754// 888 888P" d8P Y8b d8P Y8b 888 888 888 888 "88b d88P" 888 888 d88""88b 888 "88b 88K
2755// 888 888 88888888 88888888 888 888 888 888 888 888 888 888 888 888 888 888 "Y8888b.
2756// 888 888 Y8b. Y8b. 888 Y88b 888 888 888 Y88b. Y88b. 888 Y88..88P 888 888 X88
2757// 888 888 "Y8888 "Y8888 888 "Y88888 888 888 "Y8888P "Y888 888 "Y88P" 888 888 88888P'
2758// MARK: Free Functions -
2759// clang-format on
2760
2765
2768{
2775
2777 std::list<attribute> attributes;
2778};
2779
2786
2788std::list<driver> list_drivers();
2789
2791std::list<datasource> list_datasources();
2792
2803result execute(connection& conn, string const& query, long batch_operations = 1, long timeout = 0);
2804
2814 connection& conn,
2815 string const& query,
2816 long batch_operations = 1,
2817 long timeout = 0);
2818
2827result execute(statement& stmt, long batch_operations = 1);
2828
2836void just_execute(statement& stmt, long batch_operations = 1);
2837
2846result transact(statement& stmt, long batch_operations);
2847
2855void just_transact(statement& stmt, long batch_operations);
2856
2866void prepare(statement& stmt, string const& query, long timeout = 0);
2867
2869
2870} // namespace nanodbc
2871
2872#endif
A class representing a connection or a statement attribute.
Definition nanodbc.h:476
long string_length_
The StringLength argument of the ODBC call.
Definition nanodbc.h:489
void * value_ptr_
The ValuePtr argument of the ODBC call.
Definition nanodbc.h:490
long attribute_
The Attribute argument of the ODBC call.
Definition nanodbc.h:488
attribute(long const &attribute, long const &string_length, std::uintptr_t value_ptr) noexcept
Creates an attribute from the three arguments the ODBC call takes.
Definition nanodbc.h:482
Result set for a list of columns in one or more tables.
Definition nanodbc.h:2491
string remarks() const
Fetch column remarks.
short sql_data_type() const
Fetch column's SQL data type.
string table_catalog() const
Fetch table catalog.
short numeric_precision_radix() const
Fetch numeric precission.
string column_default() const
Fetch column's default.
string table_schema() const
Fetch table schema.
string type_name() const
Fetch column type name.
short sql_datetime_subtype() const
Fetch datetime subtype of column.
bool next()
Move to the next result in the result set.
short nullable() const
True iff column is nullable.
string column_name() const
Fetch column name.
string table_name() const
Fetch table name.
long column_size() const
Fetch column size.
short decimal_digits() const
Fetch decimal digits.
long char_octet_length() const
Fetch char octet length.
long ordinal_position() const
Ordinal position of the column in the table.
string is_nullable() const
Fetch column is-nullable information.
long buffer_length() const
Fetch buffer length.
short data_type() const
Fetch column data type.
Result set for a list of columns that compose the primary key of a single table.
Definition nanodbc.h:2531
bool next()
Move to the next result in the result set.
string column_name() const
Fetch column name.
short column_number() const
Column sequence number in the key (starting with 1).
string table_name() const
Fetch table name.
string primary_key_name() const
Primary key name.
string table_schema() const
Fetch table schema.
string table_catalog() const
Fetch table catalog.
Result set for a list of procedures in the data source.
Definition nanodbc.h:2593
string column_default() const
Fetch column's default.
string type_name() const
Fetch column type name.
long ordinal_position() const
Ordinal position of the column in the table.
bool next()
Move to the next result in the result set.
short decimal_digits() const
Fetch decimal digits.
long column_size() const
Fetch column size.
short sql_datetime_subtype() const
Fetch datetime subtype of column.
string remarks() const
Fetch column remarks.
long char_octet_length() const
Fetch char octet length.
string procedure_schema() const
Fetch procedure schema.
string procedure_catalog() const
Fetch procedure catalog.
short sql_data_type() const
Fetch column's SQL data type.
short column_type() const
Fetch column type.
short numeric_precision_radix() const
Fetch numeric precission.
string is_nullable() const
Fetch column is-nullable information.
long buffer_length() const
Fetch buffer length.
short nullable() const
True iff column is nullable.
string column_name() const
Fetch column name.
string procedure_name() const
Fetch procedure name.
short data_type() const
Fetch column data type.
Result set for a list of procedures in the data source.
Definition nanodbc.h:2576
string procedure_name() const
Fetch procedure name.
short procedure_type() const
Fetch procedure type.
string procedure_schema() const
Fetch procedure schema.
string procedure_catalog() const
Fetch procedure catalog.
string procedure_remarks() const
Fetch procedure remarks.
bool next()
Move to the next result in the result set.
Result set for a list of tables and the privileges associated with each table.
Definition nanodbc.h:2556
string privilege() const
Fetch the table privilege.
string grantor() const
Fetch name of user who granted the privilege.
bool next()
Move to the next result in the result set.
string table_schema() const
Fetch table schema.
string table_catalog() const
Fetch table catalog.
string table_name() const
Fetch table name.
string grantee() const
Fetch name of user whom the privilege was granted.
string is_grantable() const
Fetch indicator whether the grantee is permitted to grant the privilege to other users.
Result set for a list of tables in the data source.
Definition nanodbc.h:2474
string table_name() const
Fetch table name.
string table_catalog() const
Fetch table catalog.
bool next()
Move to the next result in the result set.
string table_remarks() const
Fetch table remarks.
string table_type() const
Fetch table type.
string table_schema() const
Fetch table schema.
A resource for get catalog information from connected data source.
Definition nanodbc.h:2470
std::list< string > list_schemas()
Returns names of all schemas available in connected data source.
std::list< string > list_catalogs()
Returns names of all catalogs (or databases) available in connected data source.
catalog(connection &conn) noexcept
Creates catalog operating on database accessible through the specified connection.
catalog::procedures find_procedures(string const &procedure=string(), string const &schema=string(), string const &catalog=string())
Creates result set with catalog, schema, procedure, and procedure types.
std::list< string > list_table_types()
Returns all available table types in the connected data source.
catalog::table_privileges find_table_privileges(string const &catalog, string const &table=string(), string const &schema=string())
Creates result set with tables and the privileges associated with each table.
catalog::tables find_tables(string const &table=string(), string const &type=string(), string const &schema=string(), string const &catalog=string())
Creates result set with catalogs, schemas, tables, or table types.
catalog::columns find_columns(string const &column=string(), string const &table=string(), string const &schema=string(), string const &catalog=string())
Creates result set with columns in one or more tables.
catalog::primary_keys find_primary_keys(string const &table, string const &schema=string(), string const &catalog=string())
Creates result set with columns that compose the primary key of a single table.
catalog::procedure_columns find_procedure_columns(string const &column=string(), string const &procedure=string(), string const &schema=string(), string const &catalog=string())
Creates result set with columns in one or more procedures.
Manages and encapsulates ODBC resources such as the connection and environment handles.
Definition nanodbc.h:1513
~connection() noexcept
Automatically disconnects from the database and frees all associated resources.
bool async_connect(string const &connection_string, void *event_handle, long timeout=0)
Initiate an asynchronous connection operation using the given connection string.
connection(const connection &rhs) noexcept
Copy constructor.
bool connected() const noexcept
Returns true if connected to the database.
void async_complete()
Completes a previously initiated asynchronous connection operation.
connection(connection &&rhs) noexcept
Move constructor.
connection(string const &dsn, string const &user, string const &pass, long timeout=0)
Create new connection object and immediately connect to the given data source.
connection & operator=(connection rhs) noexcept
Assignment.
connection()
Create new connection object, initially not connected.
bool async_connect(string const &dsn, string const &user, string const &pass, void *event_handle, long timeout=0)
Initiate an asynchronous connection operation to the given data source.
void swap(connection &) noexcept
Member swap.
connection(string const &connection_string, long timeout=0)
Create new connection object and immediately connect using the given connection string.
General database error.
Definition nanodbc.h:329
char const * what() const noexcept override
Returns the full message, including the driver's own text.
database_error(void *handle, short handle_type, std::string const &info="")
Creates runtime_error with message about last ODBC error.
Provides access to metadata in the Implementation Row Descriptor (IRD) implicitly allocated for a pre...
Definition nanodbc.h:2309
implementation_row_descriptor(statement const &statement)
Initializes IRD access from prepared or executed statement.
implementation_row_descriptor(result const &result)
Initializes IRD access from statement of executed result set.
auto alloc_type() const -> short
Value of the header field SQL_DESC_ALLOC_AUTO.
Index out of range.
Definition nanodbc.h:305
char const * what() const noexcept override
Returns the message describing the out of range index.
Accessed null data.
Definition nanodbc.h:294
char const * what() const noexcept override
Returns the message describing the null access.
Programming logic error.
Definition nanodbc.h:316
programming_error(std::string const &info)
Creates a programming error with the given message.
char const * what() const noexcept override
Returns the message describing the error.
Single pass input iterator that accesses successive rows in the attached result set.
Definition nanodbc.h:2204
result_iterator()=default
Default iterator; an empty result set.
bool operator!=(result_iterator const &rhs) const noexcept
Iterators are not equal if they have different native statemnt handles.
Definition nanodbc.h:2265
result * pointer
Pointer to iteration values.
Definition nanodbc.h:2208
pointer operator->()
Access through dereference.
Definition nanodbc.h:2226
result value_type
Values returned by iterator access.
Definition nanodbc.h:2207
void operator++(int)
Iteration.
Definition nanodbc.h:2253
bool operator==(result_iterator const &rhs) const noexcept
Iterators are equal if they a tied to the same native statemnt handle, or both empty.
Definition nanodbc.h:2256
result_iterator & operator++()
Iteration.
Definition nanodbc.h:2234
reference operator*() noexcept
Dereference.
Definition nanodbc.h:2223
result & reference
Reference to iteration values.
Definition nanodbc.h:2209
result_iterator(result &r)
Create result iterator for a given result set.
Definition nanodbc.h:2216
std::ptrdiff_t difference_type
Iterator difference.
Definition nanodbc.h:2210
std::input_iterator_tag iterator_category
Category of iterator.
Definition nanodbc.h:2206
A resource for managing result sets from statement execution.
Definition nanodbc.h:1805
bool complete_next()
Completes a previously-initiated async fetch for next row in the current result set.
unsigned long position() const
Returns the row position in the current result set.
bool move(long row)
Moves to and fetches the specified row in the current result set.
bool async_next(void *event_handle)
Initiates an asynchronous fetch of the next row in the current result set.
bool at_end() const noexcept
Returns true if there are no more results in the current result set.
bool skip(long rows)
Skips a number of rows and then fetches the resulting row in the current result set.
result() noexcept
Empty result set.
bool prior()
Fetches the prior row in the current result set.
Represents a statement on the database.
Definition nanodbc.h:865
statement(class connection &conn, std::list< attribute > const &attributes)
Constructs a statement object and associates it to the given connection, applying the given ODBC stat...
statement & operator=(statement rhs) noexcept
Assignment.
void just_execute_direct(class connection &conn, string const &query, long batch_operations=1, long timeout=0)
Execute the previously prepared query now without constructing result object.
void complete_prepare()
Completes a previously initiated asynchronous query preparation.
param_direction
Provides support for retrieving output/return parameters.
Definition nanodbc.h:906
@ PARAM_OUT
Binding an output parameter.
Definition nanodbc.h:908
@ PARAM_INOUT
Binding an input/output parameter.
Definition nanodbc.h:909
@ PARAM_IN
Binding an input parameter.
Definition nanodbc.h:907
bool async_prepare(string const &query, void *event_handle, long timeout=0)
Prepare the given statement, in asynchronous mode.
short columns() const
Returns the number of columns in a result set.
void disable_async() const
undocumented - for internal use only (used from result_impl)
bool async_execute_direct(class connection &conn, void *event_handle, string const &query, long batch_operations=1, long timeout=0)
Opens, prepares, and executes query directly on the given connection, in async mode.
void describe_parameters(const std::vector< short > &idx, const std::vector< short > &type, const std::vector< unsigned long > &size, const std::vector< short > &scale)
Sets descriptions for parameters in the prepared statement.
statement(statement &&rhs) noexcept
Move constructor.
~statement() noexcept
Closes the statement.
statement(class connection &conn)
Constructs a statement object and associates it to the given connection.
void swap(statement &rhs) noexcept
Member swap.
bool async_execute(void *event_handle, long batch_operations=1, long timeout=0)
Execute the previously prepared query now, in asynchronous mode.
statement()
Creates a new un-prepared statement.
statement(const statement &rhs) noexcept
Copy constructor.
void enable_async(void *event_handle)
undocumented - for internal use only (used from result_impl)
statement(class connection &conn, string const &query, long timeout=0)
Constructs and prepares a statement using the given connection and query.
void reset_parameters() noexcept
Resets all currently bound parameters.
void just_execute(long batch_operations=1, long timeout=0)
Execute the previously prepared query now without constructing result object.
long affected_rows() const
Returns rows affected by the request or -1 if affected rows is not available.
Support for table-valued parameter.
Definition nanodbc.h:610
table_valued_parameter(statement &stmt, short param_index, size_t row_count)
Creates a table-valued parameter and opens it on the given statement.
~table_valued_parameter() noexcept
Closes the parameter, if it is still open.
void bind_null(short param_index)
Binds a null value to the given column of every row.
table_valued_parameter(const table_valued_parameter &rhs) noexcept
Copy constructor.
short parameters() const noexcept
Returns the number of columns in the table valued parameter.
table_valued_parameter(table_valued_parameter &&rhs) noexcept
Move constructor.
table_valued_parameter()
Creates a table-valued parameter that is not yet open.
void describe_parameters(const std::vector< short > &idx, const std::vector< short > &type, const std::vector< unsigned long > &size, const std::vector< short > &scale)
Sets descriptions for columns of the table-valued parameter.
A resource for managing transaction commits and rollbacks.
Definition nanodbc.h:546
~transaction() noexcept
If this transaction has not been committed, it will rollback any modifying ops.
transaction(const class connection &conn)
Begin a transaction on the given connection object.
transaction(transaction &&rhs) noexcept
Move constructor.
transaction(const transaction &rhs) noexcept
Copy constructor.
transaction & operator=(transaction rhs) noexcept
Assignment.
void swap(transaction &rhs) noexcept
Member swap.
Type incompatible.
Definition nanodbc.h:283
char const * what() const noexcept override
Returns the message describing the incompatibility.
void bind_strings(short param_index, T const (&values)[BatchSize][ValueSize], bool const *nulls, param_direction direction=PARAM_IN)
Binds multiple string values.
Definition nanodbc.h:1438
void bind_strings(short param_index, T const (&values)[BatchSize][ValueSize], bool const *nulls)
Binds multiple string values.
Definition nanodbc.h:801
void bind_strings(short param_index, T const *values, std::size_t value_size, std::size_t batch_size, T const *null_sentry)
Binds multiple string values.
void bind_strings(short param_index, std::vector< T > const &values, bool const *nulls)
Binds multiple string values.
void bind_strings(short param_index, std::vector< T > const &values, typename T::value_type const *null_sentry)
Binds multiple string values.
void bind_strings(short param_index, std::vector< T > const &values, bool const *nulls, param_direction direction=PARAM_IN)
Binds multiple string values.
void bind_strings(short param_index, T const (&values)[BatchSize][ValueSize], T const *null_sentry)
Binds multiple string values.
Definition nanodbc.h:773
void bind_strings(short param_index, T const *values, std::size_t value_size, std::size_t batch_size, T const *null_sentry, param_direction direction=PARAM_IN)
Binds multiple string values.
void bind_strings(short param_index, T const *values, std::size_t value_size, std::size_t batch_size, bool const *nulls)
Binds multiple string values.
void bind_strings(short param_index, std::vector< T > const &values, typename T::value_type const *null_sentry, param_direction direction=PARAM_IN)
Binds multiple string values.
void bind_strings(short param_index, T const *values, std::size_t value_size, std::size_t batch_size, bool const *nulls, param_direction direction=PARAM_IN)
Binds multiple string values.
void bind_strings(short param_index, T const (&values)[BatchSize][ValueSize], T const *null_sentry, param_direction direction=PARAM_IN)
Binds multiple string values.
Definition nanodbc.h:1401
void bind_null(short param_index, std::size_t batch_size=1)
Binds null values to the parameter placeholder number in the prepared statement.
result_iterator end(result &) noexcept
Returns an iterator to the end of a result set.
Definition nanodbc.h:2283
result_iterator begin(result &r)
Returns an iterator to the beginning of the given result set.
Definition nanodbc.h:2272
void prepare(statement &stmt, string const &query, long timeout=0)
Prepares the given statement to execute on it associated connection.
result execute(connection &conn, string const &query, long batch_operations=1, long timeout=0)
Immediately opens, prepares, and executes the given query directly on the given connection.
void just_transact(statement &stmt, long batch_operations)
Execute the previously prepared query now and without creating result object.
result transact(statement &stmt, long batch_operations)
Execute the previously prepared query now.
std::list< driver > list_drivers()
Returns a list of ODBC drivers on your system.
std::list< datasource > list_datasources()
Returns a list of ODBC data sources on your system.
void just_execute(connection &conn, string const &query, long batch_operations=1, long timeout=0)
Opens, prepares, and executes query directly without creating result object.
typename std::enable_if< is_string< T >::value >::type enable_if_string
Enables an overload only for the string types nanodbc binds as strings.
Definition nanodbc.h:517
typename std::enable_if< is_character< T >::value >::type enable_if_character
Enables an overload only for the character types nanodbc binds as strings.
Definition nanodbc.h:521
std::integral_constant< bool, std::is_same< typename std::decay< T >::type, std::string >::value||std::is_same< typename std::decay< T >::type, wide_string >::value > is_string
A type trait for testing if a type is a std::basic_string compatible with the current nanodbc configu...
Definition nanodbc.h:505
std::integral_constant< bool, std::is_same< typename std::decay< T >::type, std::string::value_type >::value||std::is_same< typename std::decay< T >::type, wide_char_t >::value > is_character
A type trait for testing if a type is a character compatible with the current nanodbc configuration.
Definition nanodbc.h:513
The entirety of nanodbc can be found within this one namespace.
Definition nanodbc.h:108
unspecified type string
string will be std::u16string or std::32string if NANODBC_ENABLE_UNICODE defined.
Definition nanodbc.h:236
unspecified type null_type
null_type will be int64_t for 64-bit compilations, otherwise long.
Definition nanodbc.h:238
A type capturing parameter array length as well as number of rows in a rowset of a result.
Definition nanodbc.h:374
batch_ops() noexcept
Creates lengths of -1, meaning neither has been chosen.
Definition nanodbc.h:379
batch_ops(const long all_length) noexcept
Creates both lengths with the same value.
Definition nanodbc.h:385
long rowset_size
Number of rows fetched into a rowset at a time.
Definition nanodbc.h:376
long parameter_array_length
Number of parameter values bound per execution.
Definition nanodbc.h:375
A data source name registered with the driver manager.
Definition nanodbc.h:2782
nanodbc::string driver
Driver description.
Definition nanodbc.h:2784
nanodbc::string name
DSN name.
Definition nanodbc.h:2783
A type for representing date data.
Definition nanodbc.h:392
std::int16_t month
Month of the year [1-12].
Definition nanodbc.h:394
std::int16_t day
Day of the month [1-31].
Definition nanodbc.h:395
std::int16_t year
Year [0-inf).
Definition nanodbc.h:393
Driver attributes.
Definition nanodbc.h:2771
nanodbc::string keyword
Driver keyword attribute.
Definition nanodbc.h:2772
nanodbc::string value
Driver attribute value.
Definition nanodbc.h:2773
Information on a configured ODBC driver.
Definition nanodbc.h:2768
nanodbc::string name
Driver name.
Definition nanodbc.h:2776
std::list< attribute > attributes
List of driver attributes.
Definition nanodbc.h:2777
A type for representing time data.
Definition nanodbc.h:400
std::int16_t hour
Hours since midnight [0-23].
Definition nanodbc.h:401
std::int16_t min
Minutes after the hour [0-59].
Definition nanodbc.h:402
std::int16_t sec
Seconds after the minute.
Definition nanodbc.h:403
A type for representing timestamp data.
Definition nanodbc.h:408
std::int16_t hour
Hours since midnight [0-23].
Definition nanodbc.h:412
std::int16_t sec
Seconds after the minute.
Definition nanodbc.h:414
std::int16_t day
Day of the month [1-31].
Definition nanodbc.h:411
std::int16_t year
Year [0-inf).
Definition nanodbc.h:409
std::int16_t min
Minutes after the hour [0-59].
Definition nanodbc.h:413
std::int32_t fract
Fractional seconds.
Definition nanodbc.h:415
std::int16_t month
Month of the year [1-12].
Definition nanodbc.h:410
A type for representing timestamp+offset data.
Definition nanodbc.h:420
std::int16_t offset_minute
Minutes part of time zome offset.
Definition nanodbc.h:423
timestamp stamp
Date and time, in the offset's local terms.
Definition nanodbc.h:421
std::int16_t offset_hour
Whole hour part of time zone offset.
Definition nanodbc.h:422