A small C++ wrapper for the native C ODBC API | Requires C++14 since v2.12
A small C++ wrapper for the native C ODBC API | Requires C++14 since v2.12
![nanodbc-banner][nanodbc-banner]
A small C++ wrapper for the native C ODBC API. Please see the [online documentation][nanodbc] for user information, example usage, propaganda, and detailed source level documentation.
| Branch | Linux | Windows | Coverage |
|---|---|---|---|
main |
Coverage is measured by the coverage job in ci-linux.yml, which instruments the library with llvm-cov and runs the utility, SQLite, PostgreSQL and SQL Server suites. The job comments the figures on each pull request and prints the same report into its own summary, so the coverage change is visible before merging, and it publishes the figure the badge above reads to the badges branch. The lcov report is attached to each run as an artifact.
The native C API for working with ODBC is exorbitantly verbose, ridiculously complicated, and fantastically brittle. nanodbc addresses these frustrations! The goal for nanodbc is to make developers happy. Common tasks should be easy, requiring concise and simple code.
The [latest C++ standards][cpp-std] and [best practices][cpp-core] are enthusiastically incorporated to make the library as future-proof as possible. To accommodate users who can not use the latest and greatest, [semantic versioning][semver] and release notes will clarify required C++ features and/or standards for particular versions.
All complex objects in nanodbc follow the [pimpl (Pointer to IMPLementation)][pimpl] idiom to provide separation between interface and implementation, value semantics, and a clean nanodbc.h header file that includes nothing but standard C++ headers.
nanodbc wraps ODBC code, providing a simpler way to do the same thing. We try to be as featureful as possible, but I can't guarantee you'll never have to write supporting ODBC code. Personally, I have never had to do so.
Major features beyond what's already supported by ODBC are not within the scope of nanodbc. This is where the nano part of nanodbc becomes relevant: This library is as minimal as possible. That means no dependencies beyond standard C++ and typical ODBC headers and libraries to link against. No features unsupported by existing ODBC API calls.
Each release line sets a minimum C++ standard. Pick the nanodbc version whose minimum your project can meet; anything newer than the minimum also works.
| nanodbc version | Minimum C++ standard |
|---|---|
= 3.0 |
C++17 |
CI builds every supported compiler against C++17 and C++20, so both are exercised on each change.
nanodbc is intentionally small enough that you can drag and drop the header and implementation files into your project and run with it. For those that want it, I have also provided [CMake][cmake] files which build a library object, or build and run the included tests. The CMake files will also support out of source builds.
Tests use the [Catch2][catch] test framework, vendored under test/catch as its amalgamated distribution so that the tests build without network access. To build the nanodbc and the tests you will also need to have either [unixODBC] or [iODBC] installed and discoverable by CMake. This is easy on OS X where you can use [Homebrew][brew] to install unixODBC with brew install unixodbc, or use the system provided iODBC if you have OS X 10.9 or earlier.
The tests attempt to connect to a [SQLite][sqlite] database, so you will have to have that and a SQLite ODBC driver installed. At the time of this writing, there happens to be a nice [SQLite ODBC driver][sqliteodbc] available from Christian Werner's website, also available via Homebrew as sqliteodbc! The tests name the driver rather than a data source, so it has to be registered in odbcinst.ini as SQLite3 on *nix systems and as SQLite3 ODBC Driver on Windows systems. On OS X that section looks like the following, with the paths pointing at wherever the driver actually landed — under $(brew --prefix)/lib for a Homebrew install.
[SQLite3]
Description = SQLite3 ODBC Driver
Setup = /usr/local/lib/libsqlite3odbc.dylib
Driver = /usr/local/lib/libsqlite3odbc.dylib
Threading = 2The tests write their database into the working directory as nanodbc.db, and remove it again afterwards. Set NANODBC_TEST_CONNSTR_SQLITE to override the whole connection string, as the development container does; each suite reads its own NANODBC_TEST_CONNSTR_ variable, falling back to NANODBC_TEST_CONNSTR.
It's most convenient to create a build directory for an out of source build, but this isn't required. After you've used cmake to generate your Makefiles, make nanodbc will build the library. make tests will build the test programs and make test will run them. You can also install nanodbc to your system using make install.
If the tests fail, please don't hesitate to [report it][nanodbc-new-issue] by creating an issue with your detailed test log (prepend your make command with env CTEST_OUTPUT_ON_FAILURE=1 to enable verbose output please).
cd path/to/nanodbc/repository
mkdir build
cd build
cmake [Build Options] ..
make # builds the library, the tests and the examples
make nanodbc # builds the library alone
make tests # builds the tests
make test # runs the tests
make examples # builds all the example programs
make install # installs nanodbc.h and the libraryThe library is static unless -DBUILD_SHARED_LIBS=ON is given. Tests and examples are built by default only when nanodbc is the top level project; see Build Options.
The following build options are available via [CMake command-line option][cmake-docs] -D. If you are not using CMake to build nanodbc, you will need to set the corresponding -D compile define flags yourself.
All boolean options follow the CMake [OPTION][cmake-option] default value convention: if no initial value is provided, OFF is used.
Use the standard CMake option -DBUILD_SHARED_LIBS=ON to build nanodbc as shared library.
If you need to use the NANODBC_ENABLE_BOOST=ON option, you will have to configure your environment to use [Boost][boost].
| CMake Option | Possible Values | Details |
|---|---|---|
NANODBC_BUILD_EXAMPLES |
OFF or ON |
Build examples. On by default when nanodbc is the top level project. |
NANODBC_BUILD_TESTS |
OFF or ON |
Build tests. On by default when nanodbc is the top level project. |
NANODBC_DISABLE_ASYNC |
OFF or ON |
Disable all async features. The ODBC 3.8 async API is switched off automatically when the ODBC headers found at configure time do not declare it. |
NANODBC_DISABLE_MSSQL_TVP |
OFF or ON |
Do not use MSSQL table-valued parameters. |
NANODBC_ENABLE_BOOST |
OFF or ON |
Use Boost for Unicode string conversions (requires [Boost.Locale][boost-locale] and NANODBC_ENABLE_UNICODE=ON). Workaround to issue #24. |
NANODBC_ENABLE_COVERAGE |
OFF or ON |
Enable code coverage analysis. Requires tests to be built. |
NANODBC_ENABLE_UNICODE |
OFF or ON |
Enable Unicode support. nanodbc::string becomes std::u16string or std::u32string. |
NANODBC_ENABLE_WORKAROUND_NODATA |
OFF or ON |
Enable SQL_NO_DATA workaround to issue #43. |
NANODBC_FORCE_LIBCXX |
OFF or ON |
Force the use of libc++. On by default if the compiler supports it. |
NANODBC_FORCE_WARNINGS_AS_ERROR |
OFF or ON |
Treat compiler warnings as errors when building nanodbc. |
NANODBC_GENERATE_INSTALL |
OFF or ON |
Generate the install target. On by default when nanodbc is the top level project. |
NANODBC_OVERALLOCATE_CHAR |
OFF or ON |
Overallocate auto-bound n/var/char buffers to accommodate retrieving Unicode data in VARCHAR columns (requires NANODBC_ENABLE_UNICODE=ON) #219. |
NANODBC_ODBC_VERSION |
SQL_OV_ODBC3[...] |
Forces ODBC version to use. Default is SQL_OV_ODBC3_80 if available, otherwise SQL_OV_ODBC3. |
Under Windows sizeof(wchar_t) == sizeof(SQLWCHAR) == 2, yet on Unix systems sizeof(wchar_t) == 4. On unixODBC, sizeof(SQLWCHAR) == 2 while on iODBC, sizeof(SQLWCHAR) == sizeof(wchar_t) == 4. This leads to incompatible ABIs between applications and drivers. If buildi
No open issues yet, or sync has not completed.