Skip to content

Commit

Permalink
Add get_database_product() private method to ODBC backend.
Browse files Browse the repository at this point in the history
This method allows to determine what kind of database are we actually
connected to. It is private as it's not clear whether it's a good idea to
expose it (although it might, in keeping with SOCI philosophy of letting to
use the underlying database API directly if needed), but it is needed to
implement the sequence functions in the ODBC backend, so do add it.

Signed-off-by: Vadim Zeitlin <vz-soci@zeitlins.org>
  • Loading branch information
vadz committed Jul 10, 2012
1 parent 74702ee commit c3afdf2
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
36 changes: 35 additions & 1 deletion src/backends/odbc/session.cpp
Expand Up @@ -12,7 +12,7 @@ using namespace soci;
using namespace soci::details;

odbc_session_backend::odbc_session_backend(std::string const & connectString)
: henv_(0), hdbc_(0)
: henv_(0), hdbc_(0), product_(prod_uninitialized)
{
SQLRETURN rc;

Expand Down Expand Up @@ -145,3 +145,37 @@ odbc_blob_backend * odbc_session_backend::make_blob_backend()
{
return new odbc_blob_backend(*this);
}

odbc_session_backend::database_product
odbc_session_backend::get_database_product()
{
// Cache the product type, it's not going to change during our life time.
if (product_ != prod_uninitialized)
return product_;

char product_name[1024];
SQLSMALLINT len = sizeof(product_name);
SQLRETURN rc = SQLGetInfo(hdbc_, SQL_DBMS_NAME, product_name, len, &len);
if (is_odbc_error(rc))
{
throw odbc_soci_error(SQL_HANDLE_DBC, henv_,
"SQLGetInfo(SQL_DBMS_NAME)");
}

if (strcmp(product_name, "Firebird") == 0)
product_ = prod_firebird;
else if (strcmp(product_name, "Microsoft SQL Server") == 0)
product_ = prod_mssql;
else if (strcmp(product_name, "MySQL") == 0)
product_ = prod_mysql;
else if (strcmp(product_name, "Oracle") == 0)
product_ = prod_oracle;
else if (strcmp(product_name, "PostgreSQL") == 0)
product_ = prod_postgresql;
else if (strcmp(product_name, "SQLite") == 0)
product_ = prod_sqlite;
else
product_ = prod_unknown;

return product_;
}
18 changes: 18 additions & 0 deletions src/backends/odbc/soci-odbc.h
Expand Up @@ -253,8 +253,26 @@ struct odbc_session_backend : details::session_backend
virtual odbc_rowid_backend * make_rowid_backend();
virtual odbc_blob_backend * make_blob_backend();

enum database_product
{
prod_uninitialized, // Never returned by get_database_product().
prod_firebird,
prod_mssql,
prod_mysql,
prod_oracle,
prod_postgresql,
prod_sqlite,
prod_unknown = -1
};

// Determine the type of the database we're connected to.
database_product get_database_product();


SQLHENV henv_;
SQLHDBC hdbc_;

database_product product_;
};

class SOCI_ODBC_DECL odbc_soci_error : public soci_error
Expand Down

0 comments on commit c3afdf2

Please sign in to comment.