77#ifndef NANODBC_NANODBC_H
78#define NANODBC_NANODBC_H
100#define NANODBC_HAS_STD_STRING_VIEW
101#define NANODBC_HAS_STD_OPTIONAL
102#define NANODBC_HAS_STD_VARIANT
103#define NANODBC_HAS_STD_ANY
140#define NANODBC_THROW_NO_SOURCE_LOCATION 1
156#define NANODBC_ASSERT(expression) assert(expression)
163#if defined(NANODBC_ENABLE_UNICODE) && defined(NANODBC_ENABLE_UTF8)
164#error "NANODBC_ENABLE_UNICODE and NANODBC_ENABLE_UTF8 are alternatives; define one"
167#ifdef NANODBC_ENABLE_UNICODE
168#ifdef NANODBC_USE_IODBC_WIDE_STRINGS
169#define NANODBC_TEXT(s) U##s
170using string = std::u32string;
171using string_view = std::u32string_view;
174using string = std::wstring;
175using string_view = std::wstring_view;
176#define NANODBC_TEXT(s) L##s
178using string = std::u16string;
179using string_view = std::u16string_view;
180#define NANODBC_TEXT(s) u##s
184using string = std::string;
185using string_view = std::string_view;
186#define NANODBC_TEXT(s) s
189#ifdef NANODBC_USE_IODBC_WIDE_STRINGS
190using wide_string = std::u32string;
191using wide_string_view = std::u32string_view;
194using wide_string = std::wstring;
195using wide_string_view = std::wstring_view;
197using wide_string = std::u16string;
198using wide_string_view = std::u16string_view;
202using wide_char_t = wide_string::value_type;
207#elif !defined(_WIN64) && defined(__LP64__)
224#define NANODBC_TEXT(s) s
237#define NANODBC_DEPRECATED [[deprecated]]
240#ifndef NANODBC_DISABLE_MSSQL_TVP
241class table_valued_parameter;
282 char const*
what() const noexcept override;
293 char const*
what() const noexcept override;
304 char const*
what() const noexcept override;
317 char const*
what() const noexcept override;
332 char const*
what() const noexcept override;
335 long native() const noexcept;
338 std::
string const& state() const noexcept;
342 std::
string sql_state;
369 long parameter_array_length{-1};
370 long rowset_size{-1};
378 : parameter_array_length(all_length)
379 , rowset_size(all_length) {};
432#ifdef NANODBC_ENABLE_UNICODE
434 std::variant<std::vector<uint8_t>,
string, std::string, std::intptr_t, std::uintptr_t>;
436 using variant = std::variant<std::vector<uint8_t>,
string, std::intptr_t, std::uintptr_t>;
467 std::is_same_v<std::decay_t<T>, std::string> || std::is_same_v<std::decay_t<T>, wide_string> ||
468 std::is_same_v<std::decay_t<T>, std::string_view> ||
469 std::is_same_v<std::decay_t<T>, wide_string_view>>;
481 std::is_same_v<std::decay_t<T>, std::string> || std::is_same_v<std::decay_t<T>, wide_string>>;
491 std::is_same_v<std::decay_t<T>, std::string::value_type> ||
492 std::is_same_v<std::decay_t<T>, wide_char_t>>;
518std::unique_ptr<bool[]>
519split_optional(std::vector<std::optional<T>>
const& values, std::vector<T>& column)
521 auto nulls = std::make_unique<bool[]>(values.size());
522 column.reserve(values.size());
524 for (
auto const& value : values)
526 nulls[i++] = !value.has_value();
527 column.push_back(value ? *value : T());
590 class transaction_impl;
594 std::shared_ptr<transaction_impl> impl_;
609#ifndef NANODBC_DISABLE_MSSQL_TVP
664 void bind(
short param_index, T
const* values, std::size_t batch_size);
669 void bind(
short param_index, T
const* values, std::size_t batch_size, T
const* null_sentry);
674 void bind(
short param_index, T
const* values, std::size_t batch_size,
bool const* nulls);
678 void bind(
short param_index, std::vector<std::vector<uint8_t>>
const& values);
683 bind(
short param_index, std::vector<std::vector<uint8_t>>
const& values,
bool const* nulls);
694 std::vector<std::vector<uint8_t>>
const& values,
695 uint8_t
const* null_sentry);
701 std::vector<std::vector<uint8_t>>
const& values,
702 std::vector<uint8_t>
const& null_sentry);
728 template <
class T,
typename = enable_if_
character<T>>
732 std::size_t value_size,
733 std::size_t batch_size);
741 template <
class T,
typename = enable_if_
string<T>>
747 std::size_t BatchSize,
748 std::size_t ValueSize,
751 void bind_strings(
short param_index, T
const (&values)[BatchSize][ValueSize])
753 auto param_values =
reinterpret_cast<T const*
>(values);
754 bind_strings(param_index, param_values, ValueSize, BatchSize);
759 template <
class T,
typename = enable_if_
character<T>>
763 std::size_t value_size,
764 std::size_t batch_size,
765 T
const* null_sentry);
769 template <
class T,
typename = enable_if_
string<T>>
772 std::vector<T>
const& values,
773 typename T::value_type
const* null_sentry);
778 std::size_t BatchSize,
779 std::size_t ValueSize,
783 bind_strings(
short param_index, T
const (&values)[BatchSize][ValueSize], T
const* null_sentry)
785 auto param_values =
reinterpret_cast<T const*
>(values);
786 bind_strings(param_index, param_values, ValueSize, BatchSize,
nullptr, null_sentry);
791 template <
class T,
typename = enable_if_
character<T>>
795 std::size_t value_size,
796 std::size_t batch_size,
801 template <
class T,
typename = enable_if_
string<T>>
802 void bind_strings(
short param_index, std::vector<T>
const& values,
bool const* nulls);
807 std::size_t BatchSize,
808 std::size_t ValueSize,
811 void bind_strings(
short param_index, T
const (&values)[BatchSize][ValueSize],
bool const* nulls)
813 auto param_values =
reinterpret_cast<T const*
>(values);
814 bind_strings(param_index, param_values, ValueSize, BatchSize, nulls);
835 const std::vector<short>& idx,
836 const std::vector<short>& type,
837 const std::vector<unsigned long>& size,
838 const std::vector<short>& scale);
844 unsigned long parameter_size(
short param_index) const;
847 short parameter_scale(
short param_index) const;
850 short parameter_type(
short param_index) const;
853 class table_valued_parameter_impl;
857 std::shared_ptr<table_valued_parameter_impl> impl_;
878 class statement_impl;
886 long const& string_length,
887 variant
const& resource) noexcept
891 friend class nanodbc::statement::statement_impl;
941 [[nodiscard]]
bool open() const noexcept;
944 [[nodiscard]]
bool connected() const noexcept;
953 [[nodiscard]]
void* native_statement_handle() const noexcept;
968 void prepare(class
connection& conn,
string const& query,
long timeout = 0);
977 void prepare(
string const& query,
long timeout = 0);
981 void timeout(
long timeout = 0);
996 long batch_operations = 1,
1011 string const& query,
1015#if !defined(NANODBC_DISABLE_ASYNC)
1069 string const& query,
1070 long batch_operations = 1,
1090 bool async_execute(
void* event_handle,
long batch_operations = 1,
long timeout = 0);
1105 class result complete_execute(long batch_operations = 1);
1125 string const& query,
1126 long batch_operations = 1,
1173 string const& schema,
1174 string const& procedure,
1175 string const& column);
1190 short parameters() const;
1193 unsigned long parameter_size(
short param_index) const;
1196 short parameter_scale(
short param_index) const;
1199 short parameter_type(
short param_index) const;
1212 bool parameter_is_null(
short param_index, std::
size_t batch_index = 0) const;
1271 bind(
short param_index, std::optional<T> const& value,
param_direction direction = PARAM_IN)
1273 if (!value.has_value())
1275 bind_null(param_index, 1);
1278 if constexpr (is_owning_string_v<T>)
1279 bind(param_index, value->c_str(), direction);
1281 bind(param_index, &*value, direction);
1309 std::size_t batch_size,
1318 std::size_t batch_size,
1319 T
const* null_sentry,
1328 std::size_t batch_size,
1336 std::vector<std::vector<uint8_t>>
const& values,
1343 std::vector<std::vector<uint8_t>>
const& values,
1356 std::vector<std::vector<uint8_t>>
const& values,
1357 uint8_t
const* null_sentry,
1364 std::vector<std::vector<uint8_t>>
const& values,
1365 std::vector<uint8_t>
const& null_sentry,
1383 std::vector<T>
const& values,
1406 std::vector<std::optional<T>>
const& values,
1409 std::vector<T> column;
1410 auto const nulls = detail::split_optional(values, column);
1411 bind(param_index, column, nulls.get(), direction);
1439 template <
class T,
typename = enable_if_
character<T>>
1443 std::size_t value_size,
1444 std::size_t batch_size,
1453 template <
class T,
typename = enable_if_
string<T>>
1456 std::vector<T>
const& values,
1462 std::size_t BatchSize,
1463 std::size_t ValueSize,
1468 T
const (&values)[BatchSize][ValueSize],
1471 auto param_values =
reinterpret_cast<T const*
>(values);
1472 bind_strings(param_index, param_values, ValueSize, BatchSize, direction);
1477 template <
class T,
typename = enable_if_
character<T>>
1481 std::size_t value_size,
1482 std::size_t batch_size,
1483 T
const* null_sentry,
1488 template <
class T,
typename = enable_if_
string<T>>
1491 std::vector<T>
const& values,
1492 typename T::value_type
const* null_sentry,
1498 std::size_t BatchSize,
1499 std::size_t ValueSize,
1504 T
const (&values)[BatchSize][ValueSize],
1505 T
const* null_sentry,
1508 auto param_values =
reinterpret_cast<T const*
>(values);
1509 bind_strings(param_index, param_values, ValueSize, BatchSize, null_sentry, direction);
1514 template <
class T,
typename = enable_if_
character<T>>
1518 std::size_t value_size,
1519 std::size_t batch_size,
1525 template <
class T,
typename = enable_if_
string<T>>
1528 std::vector<T>
const& values,
1535 std::size_t BatchSize,
1536 std::size_t ValueSize,
1541 T
const (&values)[BatchSize][ValueSize],
1545 auto param_values =
reinterpret_cast<T const*
>(values);
1546 bind_strings(param_index, param_values, ValueSize, BatchSize, nulls, direction);
1566 template <
class T,
typename = enable_if_
string<T>>
1569 std::vector<std::optional<T>>
const& values,
1572 std::vector<T> column;
1573 auto const nulls = detail::split_optional(values, column);
1574 bind_strings(param_index, column, nulls.get(), direction);
1590 void bind_null(
short param_index, std::size_t batch_size = 1);
1612 const std::vector<short>& idx,
1613 const std::vector<short>& type,
1614 const std::vector<unsigned long>& size,
1615 const std::vector<short>& scale);
1618 using null_predicate_type = std::function<bool(std::size_t)>;
1620#ifndef NANODBC_DISABLE_MSSQL_TVP
1621 friend class nanodbc::table_valued_parameter::table_valued_parameter_impl;
1625 std::shared_ptr<statement_impl> impl_;
1645 class connection_impl;
1646 friend class nanodbc::transaction::transaction_impl;
1654 long const& string_length,
1655 variant
const& resource) noexcept
1659 friend class nanodbc::connection::connection_impl;
1679 connection(
string const& dsn,
string const& user,
string const& pass,
long timeout = 0);
1690 explicit connection(
string const& connection_string,
long timeout = 0);
1709 std::list<attribute>
const& attributes);
1720 connection(
string const& connection_string, std::list<attribute>
const& attributes);
1743 void connect(
string const& dsn,
string const& user,
string const& pass,
long timeout = 0);
1750 void connect(
string const& connection_string,
long timeout = 0);
1764 std::list<attribute>
const& attributes);
1772 void connect(
string const& connection_string, std::list<attribute>
const& attributes);
1791#if !defined(NANODBC_DISABLE_ASYNC)
1831 bool async_connect(
string const& connection_string,
void* event_handle,
long timeout = 0);
1851 [[nodiscard]] std::
size_t transactions() const noexcept;
1854 [[nodiscard]]
void* native_dbc_handle() const noexcept;
1857 [[nodiscard]]
void* native_env_handle() const noexcept;
1863 T get_info(
short info_type) const;
1868 string dbms_name() const;
1873 string dbms_version() const;
1877 string driver_name() const;
1881 string driver_version() const;
1885 string database_name() const;
1889 string catalog_name() const;
1892 std::
size_t ref_transaction() noexcept;
1893 std::
size_t unref_transaction() noexcept;
1894 bool rollback() const noexcept;
1895 void rollback(
bool onoff) noexcept;
1896 bool restore_autocommit() const noexcept;
1897 void restore_autocommit(
bool onoff) noexcept;
1900 std::shared_ptr<connection_impl> impl_;
1916class variant_row_cached_result;
1935 [[nodiscard]]
long rowset_size() const noexcept;
1939 [[nodiscard]]
long affected_rows() const;
1949 bool has_affected_rows() const;
1952 [[nodiscard]]
long rows() const noexcept;
1956 [[nodiscard]]
short columns() const;
1961 [[nodiscard]]
bool first();
1966 [[nodiscard]]
bool last();
1971 [[nodiscard]]
bool next();
1973#if !defined(NANODBC_DISABLE_ASYNC)
2003 [[nodiscard]]
bool skip(
long rows);
2025 void unbind(
string const& column_name);
2042 void unbind(
short column);
2085 void get_ref(
short column, T const& fallback, T&
result) const;
2096 void get_ref(
string const& column_name, T&
result) const;
2109 void get_ref(
string const& column_name, T const& fallback, T&
result) const;
2124 [[nodiscard]] T get(
short column) const;
2137 [[nodiscard]] T get(
short column, T const& fallback) const;
2159 [[nodiscard]] T get_as(
short column) const;
2164 [[nodiscard]] T get_as(
string const& column_name) const;
2174 [[nodiscard]] T get(
string const& column_name) const;
2186 [[nodiscard]] T get(
string const& column_name, T const& fallback) const;
2210 std::function<
void(T const&)> const& callback,
2211 std::
size_t chunk_size = 65536) const;
2217 string const& column_name,
2218 std::function<
void(T const&)> const& callback,
2219 std::
size_t chunk_size = 65536) const;
2242 [[nodiscard]]
bool is_null(
short column) const;
2251 [[nodiscard]]
bool is_null(
string const& column_name) const;
2263 bool is_bound(
short column) const;
2271 bool is_bound(
string const& column_name) const;
2278 [[nodiscard]]
short column(
string const& column_name) const;
2285 [[nodiscard]]
string column_name(
short column) const;
2292 [[nodiscard]]
long column_size(
short column) const;
2295 [[nodiscard]]
long column_size(
string const& column_name) const;
2305 int column_decimal_digits(
short column) const;
2308 int column_decimal_digits(
string const& column_name) const;
2311 [[nodiscard]]
int column_datatype(
short column) const;
2314 [[nodiscard]]
int column_datatype(
string const& column_name) const;
2323 string column_datatype_name(
short column) const;
2332 string column_datatype_name(
string const& column_name) const;
2335 int column_c_datatype(
short column) const;
2338 int column_c_datatype(
string const& column_name) const;
2347 bool column_unsigned(
short column) const;
2351 bool column_unsigned(
string const& column_name) const;
2357 [[nodiscard]] explicit operator
bool() const noexcept;
2366 friend class nanodbc::variant_row_cached_result;
2370 std::shared_ptr<result_impl> impl_;
2400 throw std::runtime_error(
"result is empty");
2401 return &operator*();
2409 if (!result_.next())
2429 if (result_ && rhs.result_)
2430 return result_.native_statement_handle() == rhs.result_.native_statement_handle();
2432 return !result_ && !rhs.result_;
2502 auto count() const noexcept ->
short;
2507 auto auto_unique_value(
short record) const ->
bool;
2510 auto base_column_name(
short record) const ->
string;
2513 auto base_table_name(
short record) const ->
string;
2516 auto case_sensitive(
short record) const ->
bool;
2519 auto catalog_name(
short record) const ->
string;
2522 auto concise_type(
short record) const ->
short;
2525 auto display_size(
short record) const -> std::int64_t;
2528 auto fixed_prec_scale(
short record) const ->
short;
2531 auto label(
short record) const ->
string;
2534 auto length(
short record) const -> std::uint64_t;
2537 auto local_type_name(
short record) const ->
string;
2540 auto name(
short record) const ->
string;
2545 auto nullable(
short record) const ->
short;
2548 auto num_prec_radix(
short record) const ->
short;
2551 auto octet_length(
short record) const -> std::int64_t;
2554 auto precision(
short record) const ->
short;
2557 auto rowver(
short record) const ->
short;
2560 auto scale(
short record) const ->
short;
2563 auto schema_name(
short record) const ->
string;
2569 auto searchable(
short record) const ->
short;
2572 auto table_name(
short record) const ->
string;
2575 auto type(
short record) const ->
short;
2578 auto type_name(
short record) const ->
string;
2581 auto unnamed(
short record) const ->
bool;
2584 auto unsigned_(
short record) const ->
bool;
2590 auto updatable(
short record) const ->
short;
2594 struct sql_get_descr_field
2596 sql_get_descr_field(
2599 std::uint16_t field_identifier)
noexcept;
2600 operator std::int64_t()
const;
2601 operator std::uint64_t()
const;
2606 std::uint16_t field_identifier_;
2608 friend sql_get_descr_field;
2610 void initialize_descriptor();
2611 void throw_if_record_is_out_of_range(
short record)
const;
2613 void* statement_handle_{
nullptr};
2614 short statement_columns_count_{0};
2615 void* descriptor_handle_{
nullptr};
2616 short descriptor_records_count_{0};
2816 string const& table = {},
2817 string const& type = {},
2818 string const& schema = {},
2819 string const& catalog = {});
2836 string const& table = {},
2837 string const& schema = {});
2849 string const& column = {},
2850 string const& table = {},
2851 string const& schema = {},
2852 string const& catalog = {});
2862 catalog::primary_keys
2876 string const& procedure = {},
2877 string const& schema = {},
2878 string const& catalog = {});
2891 string const& column = {},
2892 string const& procedure = {},
2893 string const& schema = {},
2894 string const& catalog = {});
2934struct hold_column_value;
2937struct hold_column_value<std::any>
2939 static std::any from(std::any value)
noexcept {
return value; }
2942template <
class Alternative,
class Variant>
2943inline bool hold_alternative(Variant& variant, std::any
const& value)
2945 if (
auto const* held = std::any_cast<Alternative>(&value))
2953template <
class... Ts>
2954struct hold_column_value<std::variant<Ts...>>
2956 static std::variant<Ts...> from(std::any
const& value)
2958 std::variant<Ts...> variant;
2959 if (!value.has_value())
2963 bool const held = (hold_alternative<Ts>(variant, value) || ...);
2968 throw type_incompatible_error();
2977T result::get_as(
short column)
const
2979 return detail::hold_column_value<T>::from(get<std::any>(column));
2983T result::get_as(
string const& column_name)
const
2985 return get_as<T>(this->column(column_name));
3045 string const& query,
3046 long batch_operations = 1,
3054template <
class Row,
class Member,
class Class>
3061template <
class Row,
class Accessor>
3062auto read_field(Row
const& row, Accessor
const& accessor) ->
decltype(accessor(row))
3064 return accessor(row);
3068template <
class Row,
class Accessor>
3071 using type = std::decay_t<
3072 decltype(
read_field(std::declval<Row const&>(), std::declval<Accessor const&>()))>;
3102template <
class T,
bool IsOptional = is_optional_v<T>>
3111 using type = std::conditional_t<
3112 is_owning_string_v<typename T::value_type>,
3117template <
class Rows,
class Accessor>
3122 Accessor
const& accessor,
3125 using value_type =
typename field_type<typename Rows::value_type, Accessor>::type;
3126 std::vector<value_type> column;
3127 column.reserve(rows.size());
3128 for (
auto const& row : rows)
3130 stmt.
bind(param_index, column);
3133template <
class Rows,
class Accessor>
3138 Accessor
const& accessor,
3141 using value_type =
typename field_type<typename Rows::value_type, Accessor>::type;
3142 std::vector<value_type> column;
3143 column.reserve(rows.size());
3144 for (
auto const& row : rows)
3145 column.push_back(read_field(row, accessor));
3151template <
class Rows,
class Accessor,
class Column>
3152std::unique_ptr<bool[]> gather_optional(Rows
const& rows, Accessor
const& accessor, Column& column)
3154 auto nulls = std::make_unique<bool[]>(rows.size());
3156 for (
auto const& row : rows)
3158 auto const& value = read_field(row, accessor);
3159 nulls[i++] = !value.has_value();
3160 column.push_back(value ? *value : typename Column::value_type());
3165template <
class Rows,
class Accessor>
3170 Accessor
const& accessor,
3171 bind_as_optional_value)
3173 using optional_type =
typename field_type<typename Rows::value_type, Accessor>::type;
3174 std::vector<typename optional_type::value_type> column;
3175 column.reserve(rows.size());
3176 auto const nulls = gather_optional(rows, accessor, column);
3177 stmt.bind(param_index, column, nulls.get());
3180template <
class Rows,
class Accessor>
3185 Accessor
const& accessor,
3186 bind_as_optional_string)
3188 using optional_type =
typename field_type<typename Rows::value_type, Accessor>::type;
3189 std::vector<typename optional_type::value_type> column;
3190 column.reserve(rows.size());
3191 auto const nulls = gather_optional(rows, accessor, column);
3192 stmt.bind_strings(param_index, column, nulls.get());
3195template <
class Rows,
class Accessor>
3196void bind_one(statement& stmt,
short param_index, Rows
const& rows, Accessor
const& accessor)
3198 using value_type =
typename field_type<typename Rows::value_type, Accessor>::type;
3199 bind_column(stmt, param_index, rows, accessor,
typename bind_kind<value_type>::type());
3234template <
class Rows,
class... Accessors>
3237 short param_index = 0;
3240 (detail::bind_one(stmt, param_index++, rows, accessors), ...);
A class representing a connection or a statement attribute.
Definition nanodbc.h:430
attribute(attribute const &other) noexcept
Copy constructor.
void extractValuePtr() noexcept
Points value_ptr_ at the resource, according to which type it holds.
attribute(long const &attribute, long const &string_length, variant resource) noexcept
Creates an attribute from the three arguments the ODBC call takes.
Result set for a list of columns in one or more tables.
Definition nanodbc.h:2662
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:2702
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:2764
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:2747
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:2727
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:2645
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:2641
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::columns find_columns(string const &column={}, string const &table={}, string const &schema={}, string const &catalog={})
Creates result set with columns in one or more tables.
std::list< string > list_table_types()
Returns all available table types in the connected data source.
catalog::procedure_columns find_procedure_columns(string const &column={}, string const &procedure={}, string const &schema={}, string const &catalog={})
Creates result set with columns in one or more procedures.
catalog::procedures find_procedures(string const &procedure={}, string const &schema={}, string const &catalog={})
Creates result set with catalog, schema, procedure, and procedure types.
catalog::tables find_tables(string const &table={}, string const &type={}, string const &schema={}, string const &catalog={})
Creates result set with catalogs, schemas, tables, or table types.
catalog::primary_keys find_primary_keys(string const &table, string const &schema={}, string const &catalog={})
Creates result set with columns that compose the primary key of a single table.
catalog::table_privileges find_table_privileges(string const &catalog, string const &table={}, string const &schema={})
Creates result set with tables and the privileges associated with each table.
Definition nanodbc.h:1650
Manages and encapsulates ODBC resources such as the connection and environment handles.
Definition nanodbc.h:1642
void connect(string const &connection_string, std::list< attribute > const &attributes)
Set the connection attributes passed by the user, and connect to the given connection string.
connection(string const &dsn, string const &user, string const &pass, std::list< attribute > const &attributes)
Create new connection object, set the connection attributes passed as arguments and connect to the gi...
bool async_connect(string const &connection_string, void *event_handle, long timeout=0)
Initiate an asynchronous connection operation using the given connection string.
bool connected() const noexcept
Returns true if connected to the database.
void async_complete()
Completes a previously initiated asynchronous connection operation.
void connect(string const &dsn, string const &user, string const &pass, std::list< attribute > const &attributes)
Set the connection attributes passed by the user, and connect to the given data source.
void deallocate()
Release environment and connection handles.
void connect(string const &connection_string, long timeout=0)
Connect using the given connection string.
connection(string const &connection_string, std::list< attribute > const &attributes)
Create new connection object, set the connection attributes passed as arguments and connect to the gi...
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()
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.
string browse_connect(string const &connection_string, bool &more_wanted)
Asks the driver what it needs in order to connect.
void swap(connection &) noexcept
Member swap.
void allocate()
Allocate environment and connection handles.
void connect(string const &dsn, string const &user, string const &pass, long timeout=0)
Connect to the given data source.
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:323
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:2480
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:299
char const * what() const noexcept override
Returns the message describing the out of range index.
Accessed null data.
Definition nanodbc.h:288
char const * what() const noexcept override
Returns the message describing the null access.
Programming logic error.
Definition nanodbc.h:310
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:2375
reference operator*() const noexcept
Dereference.
Definition nanodbc.h:2394
pointer operator->() const
Access through dereference.
Definition nanodbc.h:2397
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:2436
std::ptrdiff_t difference_type
Iterator difference.
Definition nanodbc.h:2381
std::input_iterator_tag iterator_category
Category of iterator.
Definition nanodbc.h:2377
void operator++(int)
Iteration.
Definition nanodbc.h:2424
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:2427
result_iterator & operator++()
Iteration.
Definition nanodbc.h:2405
result_iterator(result &r)
Create result iterator for a given result set.
Definition nanodbc.h:2387
A resource for managing result sets from statement execution.
Definition nanodbc.h:1923
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.
result()=default
Empty result set.
bool move(long row)
Moves to and fetches the specified row in the current result set.
void * native_statement_handle() const noexcept
Returns the native ODBC statement handle.
bool async_next(void *event_handle)
Initiates an asynchronous fetch of the next row in the current result set.
void swap(result &rhs) noexcept
Member swap.
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.
bool prior()
Fetches the prior row in the current result set.
Represents a statement on the database.
Definition nanodbc.h:875
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...
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.
bool open() const noexcept
Returns true if connection is open.
void complete_prepare()
Completes a previously initiated asynchronous query preparation.
void open(class connection &conn)
Creates a statement for the given connection.
param_direction
Provides support for retrieving output/return parameters.
Definition nanodbc.h:898
@ PARAM_OUT
Binding an output parameter.
Definition nanodbc.h:900
@ PARAM_INOUT
Binding an input/output parameter.
Definition nanodbc.h:901
@ PARAM_IN
Binding an input parameter.
Definition nanodbc.h:899
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(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.
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:612
table_valued_parameter(statement &stmt, short param_index, size_t row_count)
Creates a table-valued parameter and opens it on the given statement.
void open(statement &stmt, short param_index, std::size_t row_count)
Opens the parameter on a statement, ready for values to be bound to it.
void bind_null(short param_index)
Binds a null value to the given column of every row.
void close()
Finishes the parameter, so the statement can be executed or bound further.
short parameters() const noexcept
Returns the number of columns in the table valued parameter.
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:560
void rollback() noexcept
Marks this transaction for rollback.
transaction(const class connection &conn)
Begin a transaction on the given connection object.
void commit()
Commits transaction immediately.
void swap(transaction &rhs) noexcept
Member swap.
Type incompatible.
Definition nanodbc.h:277
char const * what() const noexcept override
Returns the message describing the incompatibility.
void bind(short param_index, T const *values, std::size_t batch_size, T const *null_sentry)
Binds multiple values.
void bind(short param_index, std::vector< std::vector< uint8_t > > const &values, std::vector< uint8_t > const &null_sentry)
Binds multiple values, those equal to the sentry, in size and bytes, as null.
void bind(short param_index, std::vector< std::vector< uint8_t > > const &values)
Binds multiple values.
void bind(short param_index, std::vector< std::vector< uint8_t > > const &values, bool const *nulls)
Binds multiple values.
void bind(short param_index, T const *values, std::size_t batch_size, param_direction direction=PARAM_IN)
Binds multiple values.
void bind(short param_index, T const *values, std::size_t batch_size, bool const *nulls)
Binds multiple values.
NANODBC_DEPRECATED void bind(short param_index, std::vector< std::vector< uint8_t > > const &values, uint8_t const *null_sentry, param_direction direction=PARAM_IN)
Binds multiple values.
void bind(short param_index, std::vector< std::vector< uint8_t > > const &values, bool const *nulls, param_direction direction=PARAM_IN)
Binds multiple values.
void bind(short param_index, std::vector< std::optional< T > > const &values, param_direction direction=PARAM_IN)
Binds a batch of values, any of which may be absent and bound as null.
Definition nanodbc.h:1404
void bind(short param_index, T const *values, std::size_t batch_size)
Binds multiple values.
void bind(short param_index, std::vector< T > const &values, param_direction direction=PARAM_IN)
Binds multiple values, holding a copy of them.
void bind(short param_index, T const *values, std::size_t batch_size, bool const *nulls, param_direction direction=PARAM_IN)
Binds multiple values.
void bind(short param_index, std::vector< std::vector< uint8_t > > const &values, param_direction direction=PARAM_IN)
Binds multiple values.
void bind(short param_index, std::vector< std::vector< uint8_t > > const &values, std::vector< uint8_t > const &null_sentry, param_direction direction=PARAM_IN)
Binds multiple values, those equal to the sentry, in size and bytes, as null.
void bind(short param_index, T const *values, std::size_t batch_size, T const *null_sentry, param_direction direction=PARAM_IN)
Binds multiple values.
void bind(short param_index, std::vector< T > const &values, bool const *nulls, param_direction direction=PARAM_IN)
Binds multiple values, holding a copy of them.
NANODBC_DEPRECATED void bind(short param_index, std::vector< std::vector< uint8_t > > const &values, uint8_t const *null_sentry)
Binds multiple values.
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:1539
void bind_strings(short param_index, T const (&values)[BatchSize][ValueSize], bool const *nulls)
Binds multiple string values.
Definition nanodbc.h:811
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< std::optional< T > > const &values, param_direction direction=PARAM_IN)
Binds a batch of strings, any of which may be absent and bound as null.
Definition nanodbc.h:1567
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])
Binds multiple string values.
Definition nanodbc.h:751
void bind_strings(short param_index, T const *values, std::size_t value_size, std::size_t batch_size, 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:783
void bind_strings(short param_index, T const (&values)[BatchSize][ValueSize], param_direction direction=PARAM_IN)
Binds multiple string values.
Definition nanodbc.h:1466
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)
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:1502
void bind_strings(short param_index, std::vector< T > const &values, param_direction direction=PARAM_IN)
Binds multiple string values.
void bind_strings(short param_index, std::vector< T > const &values)
Binds multiple string values.
void bind_null(short param_index, std::size_t batch_size=1)
Binds null values to the parameter placeholder number in the prepared statement.
void bind(short param_index, T const *value, param_direction direction=PARAM_IN)
Binds given value to given 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:2454
result_iterator begin(result &r)
Returns an iterator to the beginning of the given result set.
Definition nanodbc.h:2443
void prepare(statement &stmt, string const &query, long timeout=0)
Prepares the given statement to execute on it associated connection.
void bind_rows(statement &stmt, Rows const &rows, Accessors const &... accessors)
Binds a range of rows, a parameter at a time.
Definition nanodbc.h:3235
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.
std::enable_if_t< is_character_v< T > > enable_if_character
Enables an overload only for the character types nanodbc binds as strings.
Definition nanodbc.h:504
constexpr bool is_owning_string_v
Whether a type is a string owning the characters it holds.
Definition nanodbc.h:485
std::bool_constant< std::is_same_v< std::decay_t< T >, std::string >||std::is_same_v< std::decay_t< T >, wide_string > > is_owning_string
A type trait for testing if a type is a string owning the characters it holds.
Definition nanodbc.h:481
constexpr bool is_string_v
Whether a type is a string compatible with the current nanodbc configuration.
Definition nanodbc.h:473
std::bool_constant< std::is_same_v< std::decay_t< T >, std::string >||std::is_same_v< std::decay_t< T >, wide_string >||std::is_same_v< std::decay_t< T >, std::string_view >||std::is_same_v< std::decay_t< T >, wide_string_view > > is_string
A type trait for testing if a type is a std::basic_string compatible with the current nanodbc configu...
Definition nanodbc.h:469
constexpr bool is_character_v
Whether a type is a character compatible with the current nanodbc configuration.
Definition nanodbc.h:496
std::enable_if_t< is_string_v< T > > enable_if_string
Enables an overload only for the string types nanodbc binds as strings.
Definition nanodbc.h:500
std::bool_constant< std::is_same_v< std::decay_t< T >, std::string::value_type >||std::is_same_v< std::decay_t< T >, wide_char_t > > is_character
A type trait for testing if a type is a character compatible with the current nanodbc configuration.
Definition nanodbc.h:492
Member const & read_field(Row const &row, Member Class::*field)
Reads a member through a pointer to it.
Definition nanodbc.h:3055
std::unique_ptr< bool[]> split_optional(std::vector< std::optional< T > > const &values, std::vector< T > &column)
Splits optionals into the values themselves and flags saying which are null.
Definition nanodbc.h:519
The entirety of nanodbc can be found within this one namespace.
Definition nanodbc.h:112
unspecified type string
string will be std::u16string or std::32string if NANODBC_ENABLE_UNICODE defined.
Definition nanodbc.h:230
unspecified type null_type
null_type will be int64_t for 64-bit compilations, otherwise long.
Definition nanodbc.h:232
#define NANODBC_DEPRECATED
Marks a declaration as deprecated.
Definition nanodbc.h:237
A type capturing parameter array length as well as number of rows in a rowset of a result.
Definition nanodbc.h:368
batch_ops()=default
Creates lengths of -1, meaning neither has been chosen.
batch_ops(const long all_length) noexcept
Creates both lengths with the same value.
Definition nanodbc.h:377
A data source name registered with the driver manager.
Definition nanodbc.h:3012
nanodbc::string driver
Driver description.
Definition nanodbc.h:3014
nanodbc::string name
DSN name.
Definition nanodbc.h:3013
A type for representing date data.
Definition nanodbc.h:384
std::int16_t month
Month of the year [1-12].
Definition nanodbc.h:386
std::int16_t day
Day of the month [1-31].
Definition nanodbc.h:387
std::int16_t year
Year [0-inf).
Definition nanodbc.h:385
Definition nanodbc.h:3099
Definition nanodbc.h:3096
Definition nanodbc.h:3093
Definition nanodbc.h:3090
Definition nanodbc.h:3104
The type an accessor yields for a row, stripped of reference and const.
Definition nanodbc.h:3070
Definition nanodbc.h:3077
Driver attributes.
Definition nanodbc.h:3001
nanodbc::string keyword
Driver keyword attribute.
Definition nanodbc.h:3002
nanodbc::string value
Driver attribute value.
Definition nanodbc.h:3003
Information on a configured ODBC driver.
Definition nanodbc.h:2998
nanodbc::string name
Driver name.
Definition nanodbc.h:3006
std::list< attribute > attributes
List of driver attributes.
Definition nanodbc.h:3007
A type for representing time data.
Definition nanodbc.h:392
std::int16_t hour
Hours since midnight [0-23].
Definition nanodbc.h:393
std::int16_t min
Minutes after the hour [0-59].
Definition nanodbc.h:394
std::int16_t sec
Seconds after the minute.
Definition nanodbc.h:395
A type for representing timestamp data.
Definition nanodbc.h:400
std::int16_t hour
Hours since midnight [0-23].
Definition nanodbc.h:404
std::int16_t sec
Seconds after the minute.
Definition nanodbc.h:406
std::int16_t day
Day of the month [1-31].
Definition nanodbc.h:403
std::int16_t year
Year [0-inf).
Definition nanodbc.h:401
std::int16_t min
Minutes after the hour [0-59].
Definition nanodbc.h:405
std::int32_t fract
Fractional seconds.
Definition nanodbc.h:407
std::int16_t month
Month of the year [1-12].
Definition nanodbc.h:402
A type for representing timestamp+offset data.
Definition nanodbc.h:412
std::int16_t offset_minute
Minutes part of time zome offset.
Definition nanodbc.h:415
timestamp stamp
Date and time, in the offset's local terms.
Definition nanodbc.h:413
std::int16_t offset_hour
Whole hour part of time zone offset.
Definition nanodbc.h:414