ODBC is associated mostly with Windows platform but there exists also a Linux implementation of this API (http://www.unixodbc.org/). I was interested in answering the questions :
- How it runs on 64 bit Linux platform.
- Is it possible to develop multi-platform (Windows/Linux) C++ application having access to any data via ODBC driver provided.
To answer that questions I had to develop a small wrapper around ODBC API. ODBC API is very complicated, I can hardly imagine any program littered with sequences:
SQLAllocHandle - SQLSetEnvAttr - SQLConnect - SQLExecDirect - SQLFetch ....
To keep stuff as simple as possible I implemented only columns returned as string. I avoided problem with different data types and complicated C++ - SQL data types conversion. There is also nothing about transactions, stored procedures, code pages, binding parameters etc. But I believe that this interface can be easily extended to meet more demanding requirements.
Solution, simple wrapper around C ODBC
ODBCDrivers.h
- getODBCDrivers : retrieves list of ODBC drivers installed.
- getODBCDataSources : retrieves list of ODBC data sources.
- class ODBCConnection
- method open: opens connection
- method execute : run SQL statements
- method close: closes connection
In several places it was necessary to put statement like:
#ifdef WIN32But test cases (look beneath) does not include anything like that, it is hidden behind facade (wrapper).
....
#endif
Test program
To test that interfaces and answer the questions set up above I created a program as a C++ unit test based on googletest framework.
The test cases are very simple:
Verify number of ODBC drivers available on the host.
testdrivers.cpp
Verify number of ODBC data sources available.
testdatasources.cpp
Main test
- Create table
- Add some tuples having columns like SYMBOL{number} and NAME{number}
- Run some tests and verifications.
Execute the test
I run the test on the following platforms:
Linux 64 bit (RedHat)
- Test program compiled as 64 bit application
- Database: Oracle 11g
- ODBC driver: EasySoft. I was unable to run Oracle ODBC driver.
- Test program compiled as 64 bit application (using MS Visual Studio 2008)
- Database: MySql
- ODBC driver: MySql
- Test program compiled as 32 bit application (using MS Visual Studio 2008 Express)
- Database: DB2
- ODBC driver: DB2
- Test program compiled as 32 bit application
- Database: Progress
- ODBC driver: Progress
So the answer is positive. It runs as expected on 64 bit linux. Also it is possible to have portable C++ code accessing database via ODBC API. This example is very simple but I dare say that this conclusion can be applied also for more serious applications.
Brak komentarzy:
Prześlij komentarz