X-Git-Url: http://www.project-moonshot.org/gitweb/?a=blobdiff_plain;f=odbc-store%2Fodbc-store.cpp;h=0e9c089e0722ce156493e42c5d9527d3a2a9fa35;hb=a62c416b6ecfc17d89853a8f14604249c94d2f60;hp=838cc4b9011a9ddb602ecf6fe75d4cde53965653;hpb=83ed106db17b49ac7e28bfd051aba8758ba2032f;p=shibboleth%2Fsp.git diff --git a/odbc-store/odbc-store.cpp b/odbc-store/odbc-store.cpp index 838cc4b..0e9c089 100644 --- a/odbc-store/odbc-store.cpp +++ b/odbc-store/odbc-store.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2001-2007 Internet2 + * Copyright 2001-2010 Internet2 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,7 +17,7 @@ /** * odbc-store.cpp * - * Storage Service using ODBC + * Storage Service using ODBC. */ #if defined (_MSC_VER) || defined(__BORLANDC__) @@ -39,6 +39,7 @@ #include #include +#include #include #include #include @@ -67,8 +68,8 @@ using namespace std; /* table definitions CREATE TABLE version ( - major tinyint NOT NULL, - minor tinyint NOT NULL + major int NOT nullptr, + minor int NOT nullptr ) CREATE TABLE strings ( @@ -102,7 +103,7 @@ namespace { ~ODBCConn() { SQLRETURN sr = SQL_SUCCESS; if (!autoCommit) - sr = SQLSetConnectAttr(handle, SQL_ATTR_AUTOCOMMIT, (SQLPOINTER)SQL_AUTOCOMMIT_ON, NULL); + sr = SQLSetConnectAttr(handle, SQL_ATTR_AUTOCOMMIT, (SQLPOINTER)SQL_AUTOCOMMIT_ON, 0); SQLDisconnect(handle); SQLFreeHandle(SQL_HANDLE_DBC,handle); if (!SQL_SUCCEEDED(sr)) @@ -122,10 +123,10 @@ namespace { bool createString(const char* context, const char* key, const char* value, time_t expiration) { return createRow(STRING_TABLE, context, key, value, expiration); } - int readString(const char* context, const char* key, string* pvalue=NULL, time_t* pexpiration=NULL, int version=0) { + int readString(const char* context, const char* key, string* pvalue=nullptr, time_t* pexpiration=nullptr, int version=0) { return readRow(STRING_TABLE, context, key, pvalue, pexpiration, version, false); } - int updateString(const char* context, const char* key, const char* value=NULL, time_t expiration=0, int version=0) { + int updateString(const char* context, const char* key, const char* value=nullptr, time_t expiration=0, int version=0) { return updateRow(STRING_TABLE, context, key, value, expiration, version); } bool deleteString(const char* context, const char* key) { @@ -135,10 +136,10 @@ namespace { bool createText(const char* context, const char* key, const char* value, time_t expiration) { return createRow(TEXT_TABLE, context, key, value, expiration); } - int readText(const char* context, const char* key, string* pvalue=NULL, time_t* pexpiration=NULL, int version=0) { + int readText(const char* context, const char* key, string* pvalue=nullptr, time_t* pexpiration=nullptr, int version=0) { return readRow(TEXT_TABLE, context, key, pvalue, pexpiration, version, true); } - int updateText(const char* context, const char* key, const char* value=NULL, time_t expiration=0, int version=0) { + int updateText(const char* context, const char* key, const char* value=nullptr, time_t expiration=0, int version=0) { return updateRow(TEXT_TABLE, context, key, value, expiration, version); } bool deleteText(const char* context, const char* key) { @@ -174,7 +175,7 @@ namespace { SQLHDBC getHDBC(); SQLHSTMT getHSTMT(SQLHDBC); pair getVersion(SQLHDBC); - pair log_error(SQLHANDLE handle, SQLSMALLINT htype, const char* checkfor=NULL); + pair log_error(SQLHANDLE handle, SQLSMALLINT htype, const char* checkfor=nullptr); static void* cleanup_fn(void*); void cleanup(); @@ -257,19 +258,19 @@ namespace { }; ODBCStorageService::ODBCStorageService(const DOMElement* e) : m_log(Category::getInstance("XMLTooling.StorageService")), - m_cleanupInterval(900), shutdown_wait(NULL), cleanup_thread(NULL), shutdown(false), m_henv(SQL_NULL_HANDLE), m_isolation(SQL_TXN_SERIALIZABLE) + m_cleanupInterval(900), shutdown_wait(nullptr), cleanup_thread(nullptr), shutdown(false), m_henv(SQL_NULL_HANDLE), m_isolation(SQL_TXN_SERIALIZABLE) { #ifdef _DEBUG xmltooling::NDC ndc("ODBCStorageService"); #endif - const XMLCh* tag=e ? e->getAttributeNS(NULL,cleanupInterval) : NULL; + const XMLCh* tag=e ? e->getAttributeNS(nullptr,cleanupInterval) : nullptr; if (tag && *tag) m_cleanupInterval = XMLString::parseInt(tag); if (!m_cleanupInterval) m_cleanupInterval = 900; - auto_ptr_char iso(e ? e->getAttributeNS(NULL,isolationLevel) : NULL); + auto_ptr_char iso(e ? e->getAttributeNS(nullptr,isolationLevel) : nullptr); if (iso.get() && *iso.get()) { if (!strcmp(iso.get(),"SERIALIZABLE")) m_isolation = SQL_TXN_SERIALIZABLE; @@ -298,7 +299,7 @@ ODBCStorageService::ODBCStorageService(const DOMElement* e) : m_log(Category::ge } // Grab connection string from the configuration. - e = e ? XMLHelper::getFirstChildElement(e,ConnectionString) : NULL; + e = e ? XMLHelper::getFirstChildElement(e,ConnectionString) : nullptr; if (!e || !e->hasChildNodes()) { SQLFreeHandle(SQL_HANDLE_ENV, m_henv); throw XMLToolingException("ODBC StorageService requires ConnectionString element in configuration."); @@ -336,7 +337,7 @@ ODBCStorageService::~ODBCStorageService() { shutdown = true; shutdown_wait->signal(); - cleanup_thread->join(NULL); + cleanup_thread->join(nullptr); delete shutdown_wait; if (m_henv != SQL_NULL_HANDLE) SQLFreeHandle(SQL_HANDLE_ENV, m_henv); @@ -380,14 +381,14 @@ SQLHDBC ODBCStorageService::getHDBC() throw IOException("ODBC StorageService failed to allocate a connection handle."); } - sr=SQLDriverConnect(handle,NULL,(SQLCHAR*)m_connstring.c_str(),m_connstring.length(),NULL,0,NULL,SQL_DRIVER_NOPROMPT); + sr=SQLDriverConnect(handle,nullptr,(SQLCHAR*)m_connstring.c_str(),m_connstring.length(),nullptr,0,nullptr,SQL_DRIVER_NOPROMPT); if (!SQL_SUCCEEDED(sr)) { m_log.error("failed to connect to database"); log_error(handle, SQL_HANDLE_DBC); throw IOException("ODBC StorageService failed to connect to database."); } - sr = SQLSetConnectAttr(handle, SQL_ATTR_TXN_ISOLATION, (SQLPOINTER)m_isolation, NULL); + sr = SQLSetConnectAttr(handle, SQL_ATTR_TXN_ISOLATION, (SQLPOINTER)m_isolation, 0); if (!SQL_SUCCEEDED(sr)) throw IOException("ODBC StorageService failed to set transaction isolation level."); @@ -420,8 +421,8 @@ pair ODBCStorageService::getVersion(SQLHDBC conn) SQLINTEGER major; SQLINTEGER minor; - SQLBindCol(stmt,1,SQL_C_SLONG,&major,0,NULL); - SQLBindCol(stmt,2,SQL_C_SLONG,&minor,0,NULL); + SQLBindCol(stmt,1,SQL_C_SLONG,&major,0,nullptr); + SQLBindCol(stmt,2,SQL_C_SLONG,&minor,0,nullptr); if ((sr=SQLFetch(stmt)) != SQL_NO_DATA) return pair(major,minor); @@ -455,16 +456,16 @@ bool ODBCStorageService::createRow(const char* table, const char* context, const log_error(stmt, SQL_HANDLE_STMT); throw IOException("ODBC StorageService failed to insert record."); } - m_log.debug("SQLPrepare succeded. SQL: %s", q.c_str()); + m_log.debug("SQLPrepare succeeded. SQL: %s", q.c_str()); - SQLINTEGER b_ind = SQL_NTS; + SQLLEN b_ind = SQL_NTS; sr = SQLBindParam(stmt, 1, SQL_C_CHAR, SQL_VARCHAR, 255, 0, const_cast(context), &b_ind); if (!SQL_SUCCEEDED(sr)) { m_log.error("SQLBindParam failed (context = %s)", context); log_error(stmt, SQL_HANDLE_STMT); throw IOException("ODBC StorageService failed to insert record."); } - m_log.debug("SQLBindParam succeded (context = %s)", context); + m_log.debug("SQLBindParam succeeded (context = %s)", context); sr = SQLBindParam(stmt, 2, SQL_C_CHAR, SQL_VARCHAR, 255, 0, const_cast(key), &b_ind); if (!SQL_SUCCEEDED(sr)) { @@ -472,7 +473,7 @@ bool ODBCStorageService::createRow(const char* table, const char* context, const log_error(stmt, SQL_HANDLE_STMT); throw IOException("ODBC StorageService failed to insert record."); } - m_log.debug("SQLBindParam succeded (key = %s)", key); + m_log.debug("SQLBindParam succeeded (key = %s)", key); if (strcmp(table, TEXT_TABLE)==0) sr = SQLBindParam(stmt, 3, SQL_C_CHAR, SQL_LONGVARCHAR, strlen(value), 0, const_cast(value), &b_ind); @@ -483,7 +484,7 @@ bool ODBCStorageService::createRow(const char* table, const char* context, const log_error(stmt, SQL_HANDLE_STMT); throw IOException("ODBC StorageService failed to insert record."); } - m_log.debug("SQLBindParam succeded (value = %s)", value); + m_log.debug("SQLBindParam succeeded (value = %s)", value); //freeSafeSQL(scontext, context); //freeSafeSQL(skey, key); @@ -523,7 +524,7 @@ int ODBCStorageService::readRow( // Prepare and exectute select statement. char timebuf[32]; - timestampFromTime(time(NULL), timebuf); + timestampFromTime(time(nullptr), timebuf); char *scontext = makeSafeSQL(context); char *skey = makeSafeSQL(key); ostringstream q; @@ -531,7 +532,7 @@ int ODBCStorageService::readRow( if (pexpiration) q << ",expires"; if (pvalue) - q << ",CASE version WHEN " << version << " THEN NULL ELSE value END"; + q << ",CASE version WHEN " << version << " THEN nullptr ELSE value END"; q << " FROM " << table << " WHERE context='" << scontext << "' AND id='" << skey << "' AND expires > " << timebuf; freeSafeSQL(scontext, context); freeSafeSQL(skey, key); @@ -548,9 +549,9 @@ int ODBCStorageService::readRow( SQLSMALLINT ver; SQL_TIMESTAMP_STRUCT expiration; - SQLBindCol(stmt,1,SQL_C_SSHORT,&ver,0,NULL); + SQLBindCol(stmt,1,SQL_C_SSHORT,&ver,0,nullptr); if (pexpiration) - SQLBindCol(stmt,2,SQL_C_TYPE_TIMESTAMP,&expiration,0,NULL); + SQLBindCol(stmt,2,SQL_C_TYPE_TIMESTAMP,&expiration,0,nullptr); if ((sr=SQLFetch(stmt)) == SQL_NO_DATA) return 0; @@ -562,7 +563,7 @@ int ODBCStorageService::readRow( return version; // nothing's changed, so just echo back the version if (pvalue) { - SQLINTEGER len; + SQLLEN len; SQLCHAR buf[LONGDATA_BUFLEN]; while ((sr=SQLGetData(stmt,pexpiration ? 3 : 2,SQL_C_CHAR,buf,sizeof(buf),&len)) != SQL_NO_DATA) { if (!SQL_SUCCEEDED(sr)) { @@ -588,7 +589,7 @@ int ODBCStorageService::updateRow(const char *table, const char* context, const // Get statement handle. Disable auto-commit mode to wrap select + update. ODBCConn conn(getHDBC()); - SQLRETURN sr = SQLSetConnectAttr(conn, SQL_ATTR_AUTOCOMMIT, SQL_AUTOCOMMIT_OFF, NULL); + SQLRETURN sr = SQLSetConnectAttr(conn, SQL_ATTR_AUTOCOMMIT, SQL_AUTOCOMMIT_OFF, 0); if (!SQL_SUCCEEDED(sr)) throw IOException("ODBC StorageService failed to disable auto-commit mode."); conn.autoCommit = false; @@ -596,7 +597,7 @@ int ODBCStorageService::updateRow(const char *table, const char* context, const // First, fetch the current version for later, which also ensures the record still exists. char timebuf[32]; - timestampFromTime(time(NULL), timebuf); + timestampFromTime(time(nullptr), timebuf); char *scontext = makeSafeSQL(context); char *skey = makeSafeSQL(key); string q("SELECT version FROM "); @@ -614,7 +615,7 @@ int ODBCStorageService::updateRow(const char *table, const char* context, const } SQLSMALLINT ver; - SQLBindCol(stmt,1,SQL_C_SSHORT,&ver,0,NULL); + SQLBindCol(stmt,1,SQL_C_SSHORT,&ver,0,nullptr); if ((sr=SQLFetch(stmt)) == SQL_NO_DATA) { freeSafeSQL(scontext, context); freeSafeSQL(skey, key); @@ -654,9 +655,9 @@ int ODBCStorageService::updateRow(const char *table, const char* context, const log_error(stmt, SQL_HANDLE_STMT); throw IOException("ODBC StorageService failed to update record."); } - m_log.debug("SQLPrepare succeded. SQL: %s", q.c_str()); + m_log.debug("SQLPrepare succeeded. SQL: %s", q.c_str()); - SQLINTEGER b_ind = SQL_NTS; + SQLLEN b_ind = SQL_NTS; if (value) { if (strcmp(table, TEXT_TABLE)==0) sr = SQLBindParam(stmt, 1, SQL_C_CHAR, SQL_LONGVARCHAR, strlen(value), 0, const_cast(value), &b_ind); @@ -667,7 +668,7 @@ int ODBCStorageService::updateRow(const char *table, const char* context, const log_error(stmt, SQL_HANDLE_STMT); throw IOException("ODBC StorageService failed to update record."); } - m_log.debug("SQLBindParam succeded (context = %s)", context); + m_log.debug("SQLBindParam succeeded (context = %s)", context); } int attempts = 3; @@ -738,7 +739,7 @@ void ODBCStorageService::cleanup() if (shutdown) break; try { - reap(NULL); + reap(nullptr); } catch (exception& ex) { m_log.error("cleanup thread swallowed exception: %s", ex.what()); @@ -749,7 +750,7 @@ void ODBCStorageService::cleanup() mutex->unlock(); delete mutex; - Thread::exit(NULL); + Thread::exit(nullptr); } void* ODBCStorageService::cleanup_fn(void* cache_p) @@ -763,7 +764,7 @@ void* ODBCStorageService::cleanup_fn(void* cache_p) // Now run the cleanup process. cache->cleanup(); - return NULL; + return nullptr; } void ODBCStorageService::updateContext(const char *table, const char* context, time_t expiration) @@ -780,7 +781,7 @@ void ODBCStorageService::updateContext(const char *table, const char* context, t timestampFromTime(expiration, timebuf); char nowbuf[32]; - timestampFromTime(time(NULL), nowbuf); + timestampFromTime(time(nullptr), nowbuf); char *scontext = makeSafeSQL(context); string q("UPDATE "); @@ -809,7 +810,7 @@ void ODBCStorageService::reap(const char *table, const char* context) // Prepare and execute delete statement. char nowbuf[32]; - timestampFromTime(time(NULL), nowbuf); + timestampFromTime(time(nullptr), nowbuf); string q; if (context) { char *scontext = makeSafeSQL(context);