A small C++ wrapper for the native C ODBC API. Please see the online documentation for user information, example usage, propaganda, and detailed source level documentation.
Note: The Coverity status uses the coverity_scan branch. When
masterhas had a significant amount of work pushed to it, merge those changes into
coverity_scanas well to keep the status up to date.
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 and best practices 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 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) idiom to
provide separation between interface and implementation, value semantics, and a clean
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. No features unsupported by existing ODBC API calls.
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 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 Catch test framework, and CMake will automatically fetch the latest version
of Catch for you at build time. To build 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
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 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 available from Christian Werner's website, also available via
sqliteobdc! The tests expect to find a data source named
sqlite on *nix systems and
SQLite3 ODBC Driver on Windows systems. For example, your
odbcinst.ini file on OS X must have a
section like the following.
[sqlite] Description = SQLite3 ODBC Driver Setup = /usr/lib/libsqlite3odbc-0.93.dylib Driver = /usr/lib/libsqlite3odbc-0.93.dylib Threading = 2
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 your shared
make check will build and run the tests. You can also install nanodbc to your system
If the tests fail, please don't hesitate to report it 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 # creates shared library make nanodbc # creates shared library make tests # builds the tests make test # runs the tests make check # builds and then runs tests make examples # builds all the example programs make install # installs nanodbc.h and shared library
The following build options are available via CMake command-line option
-D. If you
are not using CMake to build nanodbc, you will need to set the corresponding
-D compile define
All boolean options follow the 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.
|CMake Option||Possible Values||Details|
||Disable all async features. May resolve build issues in older ODBC versions.|
||Do not build examples.|
||Do not generate install target.|
||Do not use libc++, if available on the system.|
||Do not build tests.|
||Use Boost for Unicode string convertions (requires Boost.Locale). Workaround to issue #24.|
||Enable Unicode support.
||Forces ODBC version to use. Default is
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 building against iODBC and the build option
nanodbc::string will be
std::u32string. In ALL other cases it will be
Continuous integration tests run on Travis-CI. The build platform does not make available
a Unicode-enabled iODBC driver. As such there is no guarantee that tests will pass in entirety on a
system using iODBC. My recommendation is to use unixODBC. If you must use iODBC, consider
disabling unicode mode to avoid
clang-format handles all C++ code formatting for nanodbc. This utility is
brew-installable on OS X (
brew install clang-format) and is available on all major
platforms. See our
.clang-format configuration file for details on the style. The script
utility/style.sh formats all code in the repository automatically.
clang-format on a single file use the following.
clang-format -i /path/to/file
Please auto-format all code submitted in Pull Requests.
For example, to spin up a docker container suitable for testing and development of nanodbc:
cd /path/to/nanodbc docker build -t nanodbc . # Use container local nanodbc repository docker run -it nanodbc /bin/bash [email protected]:/# mkdir -p /opt/nanodbc/build && cd /opt/nanodbc-host/build # Alternatively, bind host repository as container volume docker run -v "$(pwd)":"/opt/$(basename $(pwd))-host" -it nanodbc /bin/bash [email protected]:/# mkdir -p /opt/nanodbc-host/build && cd /opt/nanodbc-host/build [email protected]:/opt/nanodbc-host/build# cmake .. [email protected]:/opt/nanodbc-host/build# make nanodbc
Or, spin up the complete multi-container environment with database services:
cd /path/to/nanodbc docker-compose build docker-compose up -d docker exec -it nanodbc /bin/bash
Or, to build and ssh into a vagrant VM (using VirtualBox for example) use:
cd /path/to/nanodbc vagrant up vagrant ssh [email protected]:~$ git clone https://github.com/nanodbc/nanodbc.git [email protected]:~$ mkdir -p nanodbc/build && cd nanodbc/build [email protected]:~$ CXX=g++-5 cmake .. [email protected]:~$ make nanodbc
One of important objectives is to maintain nanodbc covered with tests. New contributions submitted via Pull Requests must include corresponding tests. This is important to ensure the quality of new features.
The good news is that adding tests is easy!
The tests structure:
tests/base_test_fixture.hincludes a set of common test cases.
tests/<database>_test.cppis a source code for an independent test program that includes both, common and database-specific test cases.
To add new test case:
tests/base_test_fixture.hfile, add a new test case method to
tests/<database>_test.cppfile, copy and paste the
TEST_CASE_METHODboilerplate, updating name, tags, etc.
If a feature requires a database-specific test case for each database, then skip the
tests/base_test_fixture.h step and write a dedicated test case directly in
Once your local
master branch is ready for publishing
(i.e. semantic versioning), use the
utility/publish.sh script. This script
bumps the major, minor, or patch version, then updates the repository's
VERSION.txt file, adds a
"Preparing" commit, and creates git tags appropriately. For example to make a minor update you
Review files of CMake configuration, documentation and Sphinx configuration,
and update version number wherever necessary.
Important: Always update
CHANGELOG.mdwith information about new changes, bug fixes, and features when making a new release. Use the
./utility/changes.shscript to aid in your composition of this document. The publish script itself will attempt to verify that the changelog file has been properly updated.
To do this manually instead, use the following steps — for example a minor update from
echo "2.10.0" > VERSION.txt
git add VERSION.txt
git commit -m "Preparing 2.10.0 release."
git tag -f "v2.10.0"
git push -f origin "v2.10.0"
Next, switch to
gh-pages branch, build latest documentation, commit and push.
Finally, announce the new release to the public.
bind_*family of functions, reduce any duplication.
matesrand previous releases.