-/*
- * Copyright 2001-2007 Internet2
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-/**
- * odbc-store.cpp
- *
- * Storage Service using ODBC
- */
-
-#if defined (_MSC_VER) || defined(__BORLANDC__)
-# include "config_win32.h"
-#else
-# include "config.h"
-#endif
-
-#ifdef WIN32
-# define _CRT_NONSTDC_NO_DEPRECATE 1
-# define _CRT_SECURE_NO_DEPRECATE 1
-#endif
-
-#ifdef WIN32
-# define ODBCSTORE_EXPORTS __declspec(dllexport)
-#else
-# define ODBCSTORE_EXPORTS
-#endif
-
-#include <xercesc/util/XMLUniDefs.hpp>
-#include <xmltooling/logging.h>
-#include <xmltooling/XMLToolingConfig.h>
-#include <xmltooling/util/NDC.h>
-#include <xmltooling/util/StorageService.h>
-#include <xmltooling/util/Threads.h>
-#include <xmltooling/util/XMLHelper.h>
-
-#include <sql.h>
-#include <sqlext.h>
-
-using namespace xmltooling::logging;
-using namespace xmltooling;
-using namespace xercesc;
-using namespace std;
-
-#define PLUGIN_VER_MAJOR 1
-#define PLUGIN_VER_MINOR 0
-
-#define LONGDATA_BUFLEN 16384
-
-#define COLSIZE_CONTEXT 255
-#define COLSIZE_ID 255
-#define COLSIZE_STRING_VALUE 255
-
-#define STRING_TABLE "strings"
-#define TEXT_TABLE "texts"
-
-/* tables definitions
-CREATE TABLE version (
- major tinyint NOT NULL,
- minor tinyint NOT NULL
- )
-
-CREATE TABLE strings (
- context varchar(255) not null,
- id varchar(255) not null,
- expires datetime not null,
- version smallint not null,
- value varchar(255) not null,
- PRIMARY KEY (context, id)
- )
-
-CREATE TABLE texts (
- context varchar(255) not null,
- id varchar(255) not null,
- expires datetime not null,
- version smallint not null,
- value text not null,
- PRIMARY KEY (context, id)
- )
-*/
-
-namespace {
- static const XMLCh cleanupInterval[] = UNICODE_LITERAL_15(c,l,e,a,n,u,p,I,n,t,e,r,v,a,l);
- static const XMLCh ConnectionString[] = UNICODE_LITERAL_16(C,o,n,n,e,c,t,i,o,n,S,t,r,i,n,g);
-
- // RAII for ODBC handles
- struct ODBCConn {
- ODBCConn(SQLHDBC conn) : handle(conn) {}
- ~ODBCConn() {
- SQLRETURN sr = SQLEndTran(SQL_HANDLE_DBC, handle, SQL_COMMIT);
- SQLDisconnect(handle);
- SQLFreeHandle(SQL_HANDLE_DBC,handle);
- if (!SQL_SUCCEEDED(sr))
- throw IOException("Failed to commit connection.");
- }
- operator SQLHDBC() {return handle;}
- SQLHDBC handle;
- };
-
- struct ODBCStatement {
- ODBCStatement(SQLHSTMT statement) : handle(statement) {}
- ~ODBCStatement() {SQLFreeHandle(SQL_HANDLE_STMT,handle);}
- operator SQLHSTMT() {return handle;}
- SQLHSTMT handle;
- };
-
- class ODBCStorageService : public StorageService
- {
- public:
- ODBCStorageService(const DOMElement* e);
- virtual ~ODBCStorageService();
-
- 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) {
- 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) {
- return updateRow(STRING_TABLE, context, key, value, expiration, version);
- }
- bool deleteString(const char* context, const char* key) {
- return deleteRow(STRING_TABLE, context, key);
- }
-
- 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) {
- 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) {
- return updateRow(TEXT_TABLE, context, key, value, expiration, version);
- }
- bool deleteText(const char* context, const char* key) {
- return deleteRow(TEXT_TABLE, context, key);
- }
-
- void reap(const char* context) {
- reap(STRING_TABLE, context);
- reap(TEXT_TABLE, context);
- }
-
- void updateContext(const char* context, time_t expiration) {
- updateContext(STRING_TABLE, context, expiration);
- updateContext(TEXT_TABLE, context, expiration);
- }
-
- void deleteContext(const char* context) {
- deleteContext(STRING_TABLE, context);
- deleteContext(TEXT_TABLE, context);
- }
-
-
- private:
- bool createRow(const char *table, const char* context, const char* key, const char* value, time_t expiration);
- int readRow(const char *table, const char* context, const char* key, string* pvalue, time_t* pexpiration, int version, bool text);
- int updateRow(const char *table, const char* context, const char* key, const char* value, time_t expiration, int version);
- bool deleteRow(const char *table, const char* context, const char* key);
-
- void reap(const char* table, const char* context);
- void updateContext(const char* table, const char* context, time_t expiration);
- void deleteContext(const char* table, const char* context);
-
- SQLHDBC getHDBC();
- SQLHSTMT getHSTMT(SQLHDBC);
- pair<int,int> getVersion(SQLHDBC);
- bool log_error(SQLHANDLE handle, SQLSMALLINT htype, const char* checkfor=NULL);
-
- static void* cleanup_fn(void*);
- void cleanup();
-
- Category& m_log;
- int m_cleanupInterval;
- CondWait* shutdown_wait;
- Thread* cleanup_thread;
- bool shutdown;
-
- SQLHENV m_henv;
- string m_connstring;
- };
-
- StorageService* ODBCStorageServiceFactory(const DOMElement* const & e)
- {
- return new ODBCStorageService(e);
- }
-
- // convert SQL timestamp to time_t
- time_t timeFromTimestamp(SQL_TIMESTAMP_STRUCT expires)
- {
- time_t ret;
- struct tm t;
- t.tm_sec=expires.second;
- t.tm_min=expires.minute;
- t.tm_hour=expires.hour;
- t.tm_mday=expires.day;
- t.tm_mon=expires.month-1;
- t.tm_year=expires.year-1900;
- t.tm_isdst=0;
-#if defined(HAVE_TIMEGM)
- ret = timegm(&t);
-#else
- ret = mktime(&t) - timezone;
-#endif
- return (ret);
- }
-
- // conver time_t to SQL string
- void timestampFromTime(time_t t, char* ret)
- {
-#ifdef HAVE_GMTIME_R
- struct tm res;
- struct tm* ptime=gmtime_r(&t,&res);
-#else
- struct tm* ptime=gmtime(&t);
-#endif
- strftime(ret,32,"{ts '%Y-%m-%d %H:%M:%S'}",ptime);
- }
-
- // make a string safe for SQL command
- // result to be free'd only if it isn't the input
- static char *makeSafeSQL(const char *src)
- {
- int ns = 0;
- int nc = 0;
- char *s;
-
- // see if any conversion needed
- for (s=(char*)src; *s; nc++,s++) if (*s=='\'') ns++;
- if (ns==0) return ((char*)src);
-
- char *safe = new char[(nc+2*ns+1)];
- for (s=safe; *src; src++) {
- if (*src=='\'') *s++ = '\'';
- *s++ = (char)*src;
- }
- *s = '\0';
- return (safe);
- }
-
- void freeSafeSQL(char *safe, const char *src)
- {
- if (safe!=src)
- delete[](safe);
- }
-};
-
-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)
-{
-#ifdef _DEBUG
- xmltooling::NDC ndc("ODBCStorageService");
-#endif
-
- const XMLCh* tag=e ? e->getAttributeNS(NULL,cleanupInterval) : NULL;
- if (tag && *tag)
- m_cleanupInterval = XMLString::parseInt(tag);
- if (!m_cleanupInterval)
- m_cleanupInterval = 900;
-
- if (m_henv == SQL_NULL_HANDLE) {
- // Enable connection pooling.
- SQLSetEnvAttr(SQL_NULL_HANDLE, SQL_ATTR_CONNECTION_POOLING, (void*)SQL_CP_ONE_PER_HENV, 0);
-
- // Allocate the environment.
- if (!SQL_SUCCEEDED(SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &m_henv)))
- throw XMLToolingException("ODBC failed to initialize.");
-
- // Specify ODBC 3.x
- SQLSetEnvAttr(m_henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
-
- m_log.info("ODBC initialized");
- }
-
- // Grab connection string from the configuration.
- e = e ? XMLHelper::getFirstChildElement(e,ConnectionString) : NULL;
- if (!e || !e->hasChildNodes()) {
- SQLFreeHandle(SQL_HANDLE_ENV, m_henv);
- throw XMLToolingException("ODBC StorageService requires ConnectionString element in configuration.");
- }
- auto_ptr_char arg(e->getFirstChild()->getNodeValue());
- m_connstring=arg.get();
-
- // Connect and check version.
- ODBCConn conn(getHDBC());
- pair<int,int> v=getVersion(conn);
-
- // Make sure we've got the right version.
- if (v.first != PLUGIN_VER_MAJOR) {
- SQLFreeHandle(SQL_HANDLE_ENV, m_henv);
- m_log.crit("unknown database version: %d.%d", v.first, v.second);
- throw XMLToolingException("Unknown database version for ODBC StorageService.");
- }
-
- // Initialize the cleanup thread
- shutdown_wait = CondWait::create();
- cleanup_thread = Thread::create(&cleanup_fn, (void*)this);
-}
-
-ODBCStorageService::~ODBCStorageService()
-{
- shutdown = true;
- shutdown_wait->signal();
- cleanup_thread->join(NULL);
- delete shutdown_wait;
- SQLFreeHandle(SQL_HANDLE_ENV, m_henv);
-}
-
-bool ODBCStorageService::log_error(SQLHANDLE handle, SQLSMALLINT htype, const char* checkfor)
-{
- SQLSMALLINT i = 0;
- SQLINTEGER native;
- SQLCHAR state[7];
- SQLCHAR text[256];
- SQLSMALLINT len;
- SQLRETURN ret;
-
- bool res = false;
- do {
- ret = SQLGetDiagRec(htype, handle, ++i, state, &native, text, sizeof(text), &len);
- if (SQL_SUCCEEDED(ret)) {
- m_log.error("ODBC Error: %s:%ld:%ld:%s", state, i, native, text);
- if (checkfor && !strcmp(checkfor, (const char*)state))
- res = true;
- }
- } while(SQL_SUCCEEDED(ret));
- return res;
-}
-
-SQLHDBC ODBCStorageService::getHDBC()
-{
-#ifdef _DEBUG
- xmltooling::NDC ndc("getHDBC");
-#endif
-
- // Get a handle.
- SQLHDBC handle;
- SQLRETURN sr=SQLAllocHandle(SQL_HANDLE_DBC, m_henv, &handle);
- if (!SQL_SUCCEEDED(sr)) {
- m_log.error("failed to allocate connection handle");
- log_error(m_henv, SQL_HANDLE_ENV);
- 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);
- 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_AUTOCOMMIT, SQL_AUTOCOMMIT_OFF, NULL);
- if (!SQL_SUCCEEDED(sr))
- throw IOException("ODBC StorageService failed to disable auto-commit mode.");
- sr = SQLSetConnectAttr(handle, SQL_ATTR_TXN_ISOLATION, (SQLPOINTER)SQL_TXN_SERIALIZABLE, NULL);
- if (!SQL_SUCCEEDED(sr))
- throw IOException("ODBC StorageService failed to enable transaction isolation.");
-
- return handle;
-}
-
-SQLHSTMT ODBCStorageService::getHSTMT(SQLHDBC conn)
-{
- SQLHSTMT hstmt;
- SQLRETURN sr=SQLAllocHandle(SQL_HANDLE_STMT,conn,&hstmt);
- if (!SQL_SUCCEEDED(sr)) {
- m_log.error("failed to allocate statement handle");
- log_error(conn, SQL_HANDLE_DBC);
- throw IOException("ODBC StorageService failed to allocate a statement handle.");
- }
- return hstmt;
-}
-
-pair<int,int> ODBCStorageService::getVersion(SQLHDBC conn)
-{
- // Grab the version number from the database.
- ODBCStatement stmt(getHSTMT(conn));
-
- SQLRETURN sr=SQLExecDirect(stmt, (SQLCHAR*)"SELECT major,minor FROM version", SQL_NTS);
- if (!SQL_SUCCEEDED(sr)) {
- m_log.error("failed to read version from database");
- log_error(stmt, SQL_HANDLE_STMT);
- throw IOException("ODBC StorageService failed to read version from database.");
- }
-
- SQLINTEGER major;
- SQLINTEGER minor;
- SQLBindCol(stmt,1,SQL_C_SLONG,&major,0,NULL);
- SQLBindCol(stmt,2,SQL_C_SLONG,&minor,0,NULL);
-
- if ((sr=SQLFetch(stmt)) != SQL_NO_DATA)
- return pair<int,int>(major,minor);
-
- m_log.error("no rows returned in version query");
- throw IOException("ODBC StorageService failed to read version from database.");
-}
-
-bool ODBCStorageService::createRow(const char *table, const char* context, const char* key, const char* value, time_t expiration)
-{
-#ifdef _DEBUG
- xmltooling::NDC ndc("createRow");
-#endif
-
- char timebuf[32];
- timestampFromTime(expiration, timebuf);
-
- // Get statement handle.
- ODBCConn conn(getHDBC());
- ODBCStatement stmt(getHSTMT(conn));
-
- // Prepare and exectute insert statement.
- char *scontext = makeSafeSQL(context);
- char *skey = makeSafeSQL(key);
- char *svalue = makeSafeSQL(value);
- string q = string("INSERT ") + table + " VALUES ('" + scontext + "','" + skey + "'," + timebuf + ",1,'" + svalue + "')";
- freeSafeSQL(scontext, context);
- freeSafeSQL(skey, key);
- freeSafeSQL(svalue, value);
- m_log.debug("SQL: %s", q.c_str());
-
- SQLRETURN sr=SQLExecDirect(stmt, (SQLCHAR*)q.c_str(), SQL_NTS);
- if (!SQL_SUCCEEDED(sr)) {
- m_log.error("insert record failed (t=%s, c=%s, k=%s)", table, context, key);
- if (log_error(stmt, SQL_HANDLE_STMT, "23000"))
- return false; // supposedly integrity violation?
- throw IOException("ODBC StorageService failed to insert record.");
- }
- return true;
-}
-
-int ODBCStorageService::readRow(
- const char *table, const char* context, const char* key, string* pvalue, time_t* pexpiration, int version, bool text
- )
-{
-#ifdef _DEBUG
- xmltooling::NDC ndc("readRow");
-#endif
-
- // Get statement handle.
- ODBCConn conn(getHDBC());
- ODBCStatement stmt(getHSTMT(conn));
-
- // Prepare and exectute select statement.
- char timebuf[32];
- timestampFromTime(time(NULL), timebuf);
- char *scontext = makeSafeSQL(context);
- char *skey = makeSafeSQL(key);
- ostringstream q;
- q << "SELECT version";
- if (pexpiration)
- q << ",expires";
- if (pvalue)
- q << ",CASE version WHEN " << version << " THEN NULL ELSE value END";
- q << " FROM " << table << " WHERE context='" << scontext << "' AND id='" << skey << "' AND expires > " << timebuf;
- freeSafeSQL(scontext, context);
- freeSafeSQL(skey, key);
- if (m_log.isDebugEnabled())
- m_log.debug("SQL: %s", q.str().c_str());
-
- SQLRETURN sr=SQLExecDirect(stmt, (SQLCHAR*)q.str().c_str(), SQL_NTS);
- if (!SQL_SUCCEEDED(sr)) {
- m_log.error("error searching for (t=%s, c=%s, k=%s)", table, context, key);
- log_error(stmt, SQL_HANDLE_STMT);
- throw IOException("ODBC StorageService search failed.");
- }
-
- SQLSMALLINT ver;
- SQL_TIMESTAMP_STRUCT expiration;
-
- SQLBindCol(stmt,1,SQL_C_SSHORT,&ver,0,NULL);
- if (pexpiration)
- SQLBindCol(stmt,2,SQL_C_TYPE_TIMESTAMP,&expiration,0,NULL);
-
- if ((sr=SQLFetch(stmt)) == SQL_NO_DATA)
- return 0;
-
- if (pexpiration)
- *pexpiration = timeFromTimestamp(expiration);
-
- if (version == ver)
- return version; // nothing's changed, so just echo back the version
-
- if (pvalue) {
- SQLINTEGER 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)) {
- m_log.error("error while reading text field from result set");
- log_error(stmt, SQL_HANDLE_STMT);
- throw IOException("ODBC StorageService search failed to read data from result set.");
- }
- pvalue->append((char*)buf);
- }
- }
-
- return ver;
-}
-
-int ODBCStorageService::updateRow(const char *table, const char* context, const char* key, const char* value, time_t expiration, int version)
-{
-#ifdef _DEBUG
- xmltooling::NDC ndc("updateRow");
-#endif
-
- if (!value && !expiration)
- throw IOException("ODBC StorageService given invalid update instructions.");
-
- // Get statement handle.
- ODBCConn conn(getHDBC());
- ODBCStatement stmt(getHSTMT(conn));
-
- // First, fetch the current version for later, which also ensures the record still exists.
- char timebuf[32];
- timestampFromTime(time(NULL), timebuf);
- char *scontext = makeSafeSQL(context);
- char *skey = makeSafeSQL(key);
- string q("SELECT version FROM ");
- q = q + table + " WHERE context='" + scontext + "' AND id='" + key + "' AND expires > " + timebuf;
-
- m_log.debug("SQL: %s", q.c_str());
-
- SQLRETURN sr=SQLExecDirect(stmt, (SQLCHAR*)q.c_str(), SQL_NTS);
- if (!SQL_SUCCEEDED(sr)) {
- freeSafeSQL(scontext, context);
- freeSafeSQL(skey, key);
- m_log.error("error searching for (t=%s, c=%s, k=%s)", table, context, key);
- log_error(stmt, SQL_HANDLE_STMT);
- throw IOException("ODBC StorageService search failed.");
- }
-
- SQLSMALLINT ver;
- SQLBindCol(stmt,1,SQL_C_SSHORT,&ver,0,NULL);
- if ((sr=SQLFetch(stmt)) == SQL_NO_DATA) {
- freeSafeSQL(scontext, context);
- freeSafeSQL(skey, key);
- return 0;
- }
-
- // Check version?
- if (version > 0 && version != ver) {
- freeSafeSQL(scontext, context);
- freeSafeSQL(skey, key);
- return -1;
- }
-
- // Prepare and exectute update statement.
- q = string("UPDATE ") + table + " SET ";
-
- if (value) {
- char *svalue = makeSafeSQL(value);
- q = q + "value='" + svalue + "'" + ",version=version+1";
- freeSafeSQL(svalue, value);
- }
-
- if (expiration) {
- timestampFromTime(expiration, timebuf);
- if (value)
- q += ',';
- q = q + "expires = " + timebuf;
- }
-
- q = q + " WHERE context='" + scontext + "' AND id='" + key + "'";
- freeSafeSQL(scontext, context);
- freeSafeSQL(skey, key);
-
- m_log.debug("SQL: %s", q.c_str());
- sr=SQLExecDirect(stmt, (SQLCHAR*)q.c_str(), SQL_NTS);
- if (sr==SQL_NO_DATA)
- return 0; // went missing?
- else if (!SQL_SUCCEEDED(sr)) {
- m_log.error("update of record failed (t=%s, c=%s, k=%s", table, context, key);
- log_error(stmt, SQL_HANDLE_STMT);
- throw IOException("ODBC StorageService failed to update record.");
- }
-
- return ver + 1;
-}
-
-bool ODBCStorageService::deleteRow(const char *table, const char *context, const char* key)
-{
-#ifdef _DEBUG
- xmltooling::NDC ndc("deleteRow");
-#endif
-
- // Get statement handle.
- ODBCConn conn(getHDBC());
- ODBCStatement stmt(getHSTMT(conn));
-
- // Prepare and execute delete statement.
- char *scontext = makeSafeSQL(context);
- char *skey = makeSafeSQL(key);
- string q = string("DELETE FROM ") + table + " WHERE context='" + scontext + "' AND id='" + skey + "'";
- freeSafeSQL(scontext, context);
- freeSafeSQL(skey, key);
- m_log.debug("SQL: %s", q.c_str());
-
- SQLRETURN sr=SQLExecDirect(stmt, (SQLCHAR*)q.c_str(), SQL_NTS);
- if (sr==SQL_NO_DATA)
- return false;
- else if (!SQL_SUCCEEDED(sr)) {
- m_log.error("error deleting record (t=%s, c=%s, k=%s)", table, context, key);
- log_error(stmt, SQL_HANDLE_STMT);
- throw IOException("ODBC StorageService failed to delete record.");
- }
-
- return true;
-}
-
-
-void ODBCStorageService::cleanup()
-{
-#ifdef _DEBUG
- xmltooling::NDC ndc("cleanup");
-#endif
-
- Mutex* mutex = Mutex::create();
-
- mutex->lock();
-
- m_log.info("cleanup thread started... running every %d secs", m_cleanupInterval);
-
- while (!shutdown) {
- shutdown_wait->timedwait(mutex, m_cleanupInterval);
- if (shutdown)
- break;
- try {
- reap(NULL);
- }
- catch (exception& ex) {
- m_log.error("cleanup thread swallowed exception: %s", ex.what());
- }
- }
-
- m_log.info("cleanup thread exiting...");
-
- mutex->unlock();
- delete mutex;
- Thread::exit(NULL);
-}
-
-void* ODBCStorageService::cleanup_fn(void* cache_p)
-{
- ODBCStorageService* cache = (ODBCStorageService*)cache_p;
-
-#ifndef WIN32
- // First, let's block all signals
- Thread::mask_all_signals();
-#endif
-
- // Now run the cleanup process.
- cache->cleanup();
- return NULL;
-}
-
-void ODBCStorageService::updateContext(const char *table, const char* context, time_t expiration)
-{
-#ifdef _DEBUG
- xmltooling::NDC ndc("updateContext");
-#endif
-
- // Get statement handle.
- ODBCConn conn(getHDBC());
- ODBCStatement stmt(getHSTMT(conn));
-
- char timebuf[32];
- timestampFromTime(expiration, timebuf);
-
- char nowbuf[32];
- timestampFromTime(time(NULL), nowbuf);
-
- char *scontext = makeSafeSQL(context);
- string q("UPDATE ");
- q = q + table + " SET expires = " + timebuf + " WHERE context='" + scontext + "' AND expires > " + nowbuf;
- freeSafeSQL(scontext, context);
-
- m_log.debug("SQL: %s", q.c_str());
-
- SQLRETURN sr=SQLExecDirect(stmt, (SQLCHAR*)q.c_str(), SQL_NTS);
- if ((sr!=SQL_NO_DATA) && !SQL_SUCCEEDED(sr)) {
- m_log.error("error updating records (t=%s, c=%s)", table, context ? context : "all");
- log_error(stmt, SQL_HANDLE_STMT);
- throw IOException("ODBC StorageService failed to update context expiration.");
- }
-}
-
-void ODBCStorageService::reap(const char *table, const char* context)
-{
-#ifdef _DEBUG
- xmltooling::NDC ndc("reap");
-#endif
-
- // Get statement handle.
- ODBCConn conn(getHDBC());
- ODBCStatement stmt(getHSTMT(conn));
-
- // Prepare and execute delete statement.
- char nowbuf[32];
- timestampFromTime(time(NULL), nowbuf);
- string q;
- if (context) {
- char *scontext = makeSafeSQL(context);
- q = string("DELETE FROM ") + table + " WHERE context='" + scontext + "' AND expires <= " + nowbuf;
- freeSafeSQL(scontext, context);
- }
- else {
- q = string("DELETE FROM ") + table + " WHERE expires <= " + nowbuf;
- }
- m_log.debug("SQL: %s", q.c_str());
-
- SQLRETURN sr=SQLExecDirect(stmt, (SQLCHAR*)q.c_str(), SQL_NTS);
- if ((sr!=SQL_NO_DATA) && !SQL_SUCCEEDED(sr)) {
- m_log.error("error expiring records (t=%s, c=%s)", table, context ? context : "all");
- log_error(stmt, SQL_HANDLE_STMT);
- throw IOException("ODBC StorageService failed to purge expired records.");
- }
-}
-
-void ODBCStorageService::deleteContext(const char *table, const char* context)
-{
-#ifdef _DEBUG
- xmltooling::NDC ndc("deleteContext");
-#endif
-
- // Get statement handle.
- ODBCConn conn(getHDBC());
- ODBCStatement stmt(getHSTMT(conn));
-
- // Prepare and execute delete statement.
- char *scontext = makeSafeSQL(context);
- string q = string("DELETE FROM ") + table + " WHERE context='" + scontext + "'";
- freeSafeSQL(scontext, context);
- m_log.debug("SQL: %s", q.c_str());
-
- SQLRETURN sr=SQLExecDirect(stmt, (SQLCHAR*)q.c_str(), SQL_NTS);
- if ((sr!=SQL_NO_DATA) && !SQL_SUCCEEDED(sr)) {
- m_log.error("error deleting context (t=%s, c=%s)", table, context);
- log_error(stmt, SQL_HANDLE_STMT);
- throw IOException("ODBC StorageService failed to delete context.");
- }
-}
-
-extern "C" int ODBCSTORE_EXPORTS xmltooling_extension_init(void*)
-{
- // Register this SS type
- XMLToolingConfig::getConfig().StorageServiceManager.registerFactory("ODBC", ODBCStorageServiceFactory);
- return 0;
-}
-
-extern "C" void ODBCSTORE_EXPORTS xmltooling_extension_term()
-{
- XMLToolingConfig::getConfig().StorageServiceManager.deregisterFactory("ODBC");
-}
+/**\r
+ * Licensed to the University Corporation for Advanced Internet\r
+ * Development, Inc. (UCAID) under one or more contributor license\r
+ * agreements. See the NOTICE file distributed with this work for\r
+ * additional information regarding copyright ownership.\r
+ *\r
+ * UCAID licenses this file to you under the Apache License,\r
+ * Version 2.0 (the "License"); you may not use this file except\r
+ * in compliance with the License. You may obtain a copy of the\r
+ * License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing,\r
+ * software distributed under the License is distributed on an\r
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,\r
+ * either express or implied. See the License for the specific\r
+ * language governing permissions and limitations under the License.\r
+ */\r
+\r
+/**\r
+ * odbc-store.cpp\r
+ *\r
+ * Storage Service using ODBC.\r
+ */\r
+\r
+#if defined (_MSC_VER) || defined(__BORLANDC__)\r
+# include "config_win32.h"\r
+#else\r
+# include "config.h"\r
+#endif\r
+\r
+#ifdef WIN32\r
+# define _CRT_NONSTDC_NO_DEPRECATE 1\r
+# define _CRT_SECURE_NO_DEPRECATE 1\r
+#endif\r
+\r
+#ifdef WIN32\r
+# define ODBCSTORE_EXPORTS __declspec(dllexport)\r
+#else\r
+# define ODBCSTORE_EXPORTS\r
+#endif\r
+\r
+#include <xmltooling/logging.h>\r
+#include <xmltooling/unicode.h>\r
+#include <xmltooling/XMLToolingConfig.h>\r
+#include <xmltooling/util/NDC.h>\r
+#include <xmltooling/util/StorageService.h>\r
+#include <xmltooling/util/Threads.h>\r
+#include <xmltooling/util/XMLHelper.h>\r
+#include <xercesc/util/XMLUniDefs.hpp>\r
+\r
+#include <sql.h>\r
+#include <sqlext.h>\r
+\r
+#include <boost/lexical_cast.hpp>\r
+#include <boost/algorithm/string.hpp>\r
+\r
+using namespace xmltooling::logging;\r
+using namespace xmltooling;\r
+using namespace xercesc;\r
+using namespace boost;\r
+using namespace std;\r
+\r
+#define PLUGIN_VER_MAJOR 1\r
+#define PLUGIN_VER_MINOR 1\r
+\r
+#define LONGDATA_BUFLEN 16384\r
+\r
+#define COLSIZE_CONTEXT 255\r
+#define COLSIZE_ID 255\r
+#define COLSIZE_STRING_VALUE 255\r
+\r
+#define STRING_TABLE "strings"\r
+#define TEXT_TABLE "texts"\r
+\r
+/* table definitions\r
+CREATE TABLE version (\r
+ major int NOT nullptr,\r
+ minor int NOT nullptr\r
+ )\r
+\r
+CREATE TABLE strings (\r
+ context varchar(255) not null,\r
+ id varchar(255) not null,\r
+ expires datetime not null,\r
+ version int not null,\r
+ value varchar(255) not null,\r
+ PRIMARY KEY (context, id)\r
+ )\r
+\r
+CREATE TABLE texts (\r
+ context varchar(255) not null,\r
+ id varchar(255) not null,\r
+ expires datetime not null,\r
+ version int not null,\r
+ value text not null,\r
+ PRIMARY KEY (context, id)\r
+ )\r
+*/\r
+\r
+namespace {\r
+ static const XMLCh cleanupInterval[] = UNICODE_LITERAL_15(c,l,e,a,n,u,p,I,n,t,e,r,v,a,l);\r
+ static const XMLCh isolationLevel[] = UNICODE_LITERAL_14(i,s,o,l,a,t,i,o,n,L,e,v,e,l);\r
+ static const XMLCh ConnectionString[] = UNICODE_LITERAL_16(C,o,n,n,e,c,t,i,o,n,S,t,r,i,n,g);\r
+ static const XMLCh RetryOnError[] = UNICODE_LITERAL_12(R,e,t,r,y,O,n,E,r,r,o,r);\r
+ static const XMLCh contextSize[] = UNICODE_LITERAL_11(c,o,n,t,e,x,t,S,i,z,e);\r
+ static const XMLCh keySize[] = UNICODE_LITERAL_7(k,e,y,S,i,z,e);\r
+ static const XMLCh stringSize[] = UNICODE_LITERAL_10(s,t,r,i,n,g,S,i,z,e);\r
+\r
+ // RAII for ODBC handles\r
+ struct ODBCConn {\r
+ ODBCConn(SQLHDBC conn) : handle(conn), autoCommit(true) {}\r
+ ~ODBCConn() {\r
+ if (handle != SQL_NULL_HDBC) {\r
+ SQLRETURN sr = SQL_SUCCESS;\r
+ if (!autoCommit)\r
+ sr = SQLSetConnectAttr(handle, SQL_ATTR_AUTOCOMMIT, (SQLPOINTER)SQL_AUTOCOMMIT_ON, 0);\r
+ SQLDisconnect(handle);\r
+ SQLFreeHandle(SQL_HANDLE_DBC, handle);\r
+ if (!SQL_SUCCEEDED(sr))\r
+ throw IOException("Failed to commit connection and return to auto-commit mode.");\r
+ }\r
+ }\r
+ operator SQLHDBC() {return handle;}\r
+ SQLHDBC handle;\r
+ bool autoCommit;\r
+ };\r
+\r
+ class ODBCStorageService : public StorageService\r
+ {\r
+ public:\r
+ ODBCStorageService(const DOMElement* e);\r
+ virtual ~ODBCStorageService();\r
+\r
+ const Capabilities& getCapabilities() const {\r
+ return m_caps;\r
+ }\r
+\r
+ bool createString(const char* context, const char* key, const char* value, time_t expiration) {\r
+ return createRow(STRING_TABLE, context, key, value, expiration);\r
+ }\r
+ int readString(const char* context, const char* key, string* pvalue=nullptr, time_t* pexpiration=nullptr, int version=0) {\r
+ return readRow(STRING_TABLE, context, key, pvalue, pexpiration, version);\r
+ }\r
+ int updateString(const char* context, const char* key, const char* value=nullptr, time_t expiration=0, int version=0) {\r
+ return updateRow(STRING_TABLE, context, key, value, expiration, version);\r
+ }\r
+ bool deleteString(const char* context, const char* key) {\r
+ return deleteRow(STRING_TABLE, context, key);\r
+ }\r
+\r
+ bool createText(const char* context, const char* key, const char* value, time_t expiration) {\r
+ return createRow(TEXT_TABLE, context, key, value, expiration);\r
+ }\r
+ int readText(const char* context, const char* key, string* pvalue=nullptr, time_t* pexpiration=nullptr, int version=0) {\r
+ return readRow(TEXT_TABLE, context, key, pvalue, pexpiration, version);\r
+ }\r
+ int updateText(const char* context, const char* key, const char* value=nullptr, time_t expiration=0, int version=0) {\r
+ return updateRow(TEXT_TABLE, context, key, value, expiration, version);\r
+ }\r
+ bool deleteText(const char* context, const char* key) {\r
+ return deleteRow(TEXT_TABLE, context, key);\r
+ }\r
+\r
+ void reap(const char* context) {\r
+ reap(STRING_TABLE, context);\r
+ reap(TEXT_TABLE, context);\r
+ }\r
+\r
+ void updateContext(const char* context, time_t expiration) {\r
+ updateContext(STRING_TABLE, context, expiration);\r
+ updateContext(TEXT_TABLE, context, expiration);\r
+ }\r
+\r
+ void deleteContext(const char* context) {\r
+ deleteContext(STRING_TABLE, context);\r
+ deleteContext(TEXT_TABLE, context);\r
+ }\r
+ \r
+\r
+ private:\r
+ bool createRow(const char *table, const char* context, const char* key, const char* value, time_t expiration);\r
+ int readRow(const char *table, const char* context, const char* key, string* pvalue, time_t* pexpiration, int version);\r
+ int updateRow(const char *table, const char* context, const char* key, const char* value, time_t expiration, int version);\r
+ bool deleteRow(const char *table, const char* context, const char* key);\r
+\r
+ void reap(const char* table, const char* context);\r
+ void updateContext(const char* table, const char* context, time_t expiration);\r
+ void deleteContext(const char* table, const char* context);\r
+\r
+ SQLHDBC getHDBC();\r
+ SQLHSTMT getHSTMT(SQLHDBC);\r
+ pair<SQLINTEGER,SQLINTEGER> getVersion(SQLHDBC);\r
+ pair<bool,bool> log_error(SQLHANDLE handle, SQLSMALLINT htype, const char* checkfor=nullptr);\r
+\r
+ static void* cleanup_fn(void*); \r
+ void cleanup();\r
+\r
+ Category& m_log;\r
+ Capabilities m_caps;\r
+ int m_cleanupInterval;\r
+ scoped_ptr<CondWait> shutdown_wait;\r
+ Thread* cleanup_thread;\r
+ bool shutdown;\r
+\r
+ SQLHENV m_henv;\r
+ string m_connstring;\r
+ long m_isolation;\r
+ bool m_wideVersion;\r
+ vector<SQLINTEGER> m_retries;\r
+ };\r
+\r
+ StorageService* ODBCStorageServiceFactory(const DOMElement* const & e)\r
+ {\r
+ return new ODBCStorageService(e);\r
+ }\r
+\r
+ // convert SQL timestamp to time_t \r
+ time_t timeFromTimestamp(SQL_TIMESTAMP_STRUCT expires)\r
+ {\r
+ time_t ret;\r
+ struct tm t;\r
+ t.tm_sec=expires.second;\r
+ t.tm_min=expires.minute;\r
+ t.tm_hour=expires.hour;\r
+ t.tm_mday=expires.day;\r
+ t.tm_mon=expires.month-1;\r
+ t.tm_year=expires.year-1900;\r
+ t.tm_isdst=0;\r
+#if defined(HAVE_TIMEGM)\r
+ ret = timegm(&t);\r
+#else\r
+ ret = mktime(&t) - timezone;\r
+#endif\r
+ return (ret);\r
+ }\r
+\r
+ // conver time_t to SQL string\r
+ void timestampFromTime(time_t t, char* ret)\r
+ {\r
+#ifdef HAVE_GMTIME_R\r
+ struct tm res;\r
+ struct tm* ptime=gmtime_r(&t,&res);\r
+#else\r
+ struct tm* ptime=gmtime(&t);\r
+#endif\r
+ strftime(ret,32,"{ts '%Y-%m-%d %H:%M:%S'}",ptime);\r
+ }\r
+\r
+ class SQLString {\r
+ const char* m_src;\r
+ string m_copy;\r
+ public:\r
+ SQLString(const char* src) : m_src(src) {\r
+ if (strchr(src, '\'')) {\r
+ m_copy = src;\r
+ replace_all(m_copy, "'", "''");\r
+ }\r
+ }\r
+\r
+ operator const char*() const {\r
+ return tostr();\r
+ }\r
+\r
+ const char* tostr() const {\r
+ return m_copy.empty() ? m_src : m_copy.c_str();\r
+ }\r
+ };\r
+};\r
+\r
+ODBCStorageService::ODBCStorageService(const DOMElement* e) : m_log(Category::getInstance("XMLTooling.StorageService")),\r
+ m_caps(XMLHelper::getAttrInt(e, 255, contextSize), XMLHelper::getAttrInt(e, 255, keySize), XMLHelper::getAttrInt(e, 255, stringSize)),\r
+ m_cleanupInterval(XMLHelper::getAttrInt(e, 900, cleanupInterval)),\r
+ cleanup_thread(nullptr), shutdown(false), m_henv(SQL_NULL_HENV), m_isolation(SQL_TXN_SERIALIZABLE), m_wideVersion(false)\r
+{\r
+#ifdef _DEBUG\r
+ xmltooling::NDC ndc("ODBCStorageService");\r
+#endif\r
+ string iso(XMLHelper::getAttrString(e, "SERIALIZABLE", isolationLevel));\r
+ if (iso == "SERIALIZABLE")\r
+ m_isolation = SQL_TXN_SERIALIZABLE;\r
+ else if (iso == "REPEATABLE_READ")\r
+ m_isolation = SQL_TXN_REPEATABLE_READ;\r
+ else if (iso == "READ_COMMITTED")\r
+ m_isolation = SQL_TXN_READ_COMMITTED;\r
+ else if (iso == "READ_UNCOMMITTED")\r
+ m_isolation = SQL_TXN_READ_UNCOMMITTED;\r
+ else\r
+ throw XMLToolingException("Unknown transaction isolationLevel property.");\r
+\r
+ if (m_henv == SQL_NULL_HENV) {\r
+ // Enable connection pooling.\r
+ SQLSetEnvAttr(SQL_NULL_HANDLE, SQL_ATTR_CONNECTION_POOLING, (void*)SQL_CP_ONE_PER_HENV, 0);\r
+\r
+ // Allocate the environment.\r
+ if (!SQL_SUCCEEDED(SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &m_henv)))\r
+ throw XMLToolingException("ODBC failed to initialize.");\r
+\r
+ // Specify ODBC 3.x\r
+ SQLSetEnvAttr(m_henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);\r
+\r
+ m_log.info("ODBC initialized");\r
+ }\r
+\r
+ // Grab connection string from the configuration.\r
+ e = e ? XMLHelper::getFirstChildElement(e, ConnectionString) : nullptr;\r
+ auto_ptr_char arg(e ? e->getTextContent() : nullptr);\r
+ if (!arg.get() || !*arg.get()) {\r
+ SQLFreeHandle(SQL_HANDLE_ENV, m_henv);\r
+ throw XMLToolingException("ODBC StorageService requires ConnectionString element in configuration.");\r
+ }\r
+ m_connstring = arg.get();\r
+\r
+ // Connect and check version.\r
+ ODBCConn conn(getHDBC());\r
+ pair<SQLINTEGER,SQLINTEGER> v = getVersion(conn);\r
+\r
+ // Make sure we've got the right version.\r
+ if (v.first != PLUGIN_VER_MAJOR) {\r
+ SQLFreeHandle(SQL_HANDLE_ENV, m_henv);\r
+ m_log.crit("unknown database version: %d.%d", v.first, v.second);\r
+ throw XMLToolingException("Unknown database version for ODBC StorageService.");\r
+ }\r
+ \r
+ if (v.first > 1 || v.second > 0) {\r
+ m_log.info("using 32-bit int type for version fields in tables");\r
+ m_wideVersion = true;\r
+ }\r
+\r
+ // Load any retry errors to check.\r
+ e = XMLHelper::getNextSiblingElement(e, RetryOnError);\r
+ while (e) {\r
+ if (e->hasChildNodes()) {\r
+ try {\r
+ int code = XMLString::parseInt(e->getTextContent());\r
+ m_retries.push_back(code);\r
+ m_log.info("will retry operations when native ODBC error (%d) is returned", code);\r
+ }\r
+ catch (XMLException&) {\r
+ m_log.error("skipping non-numeric ODBC retry code");\r
+ }\r
+ }\r
+ e = XMLHelper::getNextSiblingElement(e, RetryOnError);\r
+ }\r
+\r
+ if (m_cleanupInterval > 0) {\r
+ // Initialize the cleanup thread\r
+ shutdown_wait.reset(CondWait::create());\r
+ cleanup_thread = Thread::create(&cleanup_fn, (void*)this);\r
+ }\r
+ else {\r
+ m_log.info("no cleanup interval configured, no cleanup thread will be started");\r
+ }\r
+}\r
+\r
+ODBCStorageService::~ODBCStorageService()\r
+{\r
+ shutdown = true;\r
+ if (shutdown_wait.get()) {\r
+ shutdown_wait->signal();\r
+ }\r
+ if (cleanup_thread) {\r
+ cleanup_thread->join(nullptr);\r
+ }\r
+ if (m_henv != SQL_NULL_HANDLE) {\r
+ SQLFreeHandle(SQL_HANDLE_ENV, m_henv);\r
+ }\r
+}\r
+\r
+pair<bool,bool> ODBCStorageService::log_error(SQLHANDLE handle, SQLSMALLINT htype, const char* checkfor)\r
+{\r
+ SQLSMALLINT i = 0;\r
+ SQLINTEGER native;\r
+ SQLCHAR state[7];\r
+ SQLCHAR text[256];\r
+ SQLSMALLINT len;\r
+ SQLRETURN ret;\r
+\r
+ pair<bool,bool> res = make_pair(false,false);\r
+ do {\r
+ ret = SQLGetDiagRec(htype, handle, ++i, state, &native, text, sizeof(text), &len);\r
+ if (SQL_SUCCEEDED(ret)) {\r
+ m_log.error("ODBC Error: %s:%ld:%ld:%s", state, i, native, text);\r
+ for (vector<SQLINTEGER>::const_iterator n = m_retries.begin(); !res.first && n != m_retries.end(); ++n)\r
+ res.first = (*n == native);\r
+ if (checkfor && !strcmp(checkfor, (const char*)state))\r
+ res.second = true;\r
+ }\r
+ } while(SQL_SUCCEEDED(ret));\r
+ return res;\r
+}\r
+\r
+SQLHDBC ODBCStorageService::getHDBC()\r
+{\r
+#ifdef _DEBUG\r
+ xmltooling::NDC ndc("getHDBC");\r
+#endif\r
+\r
+ // Get a handle.\r
+ SQLHDBC handle = SQL_NULL_HDBC;\r
+ SQLRETURN sr = SQLAllocHandle(SQL_HANDLE_DBC, m_henv, &handle);\r
+ if (!SQL_SUCCEEDED(sr) || handle == SQL_NULL_HDBC) {\r
+ m_log.error("failed to allocate connection handle");\r
+ log_error(m_henv, SQL_HANDLE_ENV);\r
+ throw IOException("ODBC StorageService failed to allocate a connection handle.");\r
+ }\r
+\r
+ sr = SQLDriverConnect(handle,nullptr,(SQLCHAR*)m_connstring.c_str(),m_connstring.length(),nullptr,0,nullptr,SQL_DRIVER_NOPROMPT);\r
+ if (!SQL_SUCCEEDED(sr)) {\r
+ m_log.error("failed to connect to database");\r
+ log_error(handle, SQL_HANDLE_DBC);\r
+ SQLFreeHandle(SQL_HANDLE_DBC, handle);\r
+ throw IOException("ODBC StorageService failed to connect to database.");\r
+ }\r
+\r
+ sr = SQLSetConnectAttr(handle, SQL_ATTR_TXN_ISOLATION, (SQLPOINTER)m_isolation, 0);\r
+ if (!SQL_SUCCEEDED(sr)) {\r
+ SQLDisconnect(handle);\r
+ SQLFreeHandle(SQL_HANDLE_DBC, handle);\r
+ throw IOException("ODBC StorageService failed to set transaction isolation level.");\r
+ }\r
+\r
+ return handle;\r
+}\r
+\r
+SQLHSTMT ODBCStorageService::getHSTMT(SQLHDBC conn)\r
+{\r
+ SQLHSTMT hstmt = SQL_NULL_HSTMT;\r
+ SQLRETURN sr = SQLAllocHandle(SQL_HANDLE_STMT, conn, &hstmt);\r
+ if (!SQL_SUCCEEDED(sr) || hstmt == SQL_NULL_HSTMT) {\r
+ m_log.error("failed to allocate statement handle");\r
+ log_error(conn, SQL_HANDLE_DBC);\r
+ throw IOException("ODBC StorageService failed to allocate a statement handle.");\r
+ }\r
+ return hstmt;\r
+}\r
+\r
+pair<SQLINTEGER,SQLINTEGER> ODBCStorageService::getVersion(SQLHDBC conn)\r
+{\r
+ // Grab the version number from the database.\r
+ SQLHSTMT stmt = getHSTMT(conn);\r
+ \r
+ SQLRETURN sr = SQLExecDirect(stmt, (SQLCHAR*)"SELECT major,minor FROM version", SQL_NTS);\r
+ if (!SQL_SUCCEEDED(sr)) {\r
+ m_log.error("failed to read version from database");\r
+ log_error(stmt, SQL_HANDLE_STMT);\r
+ throw IOException("ODBC StorageService failed to read version from database.");\r
+ }\r
+\r
+ SQLINTEGER major;\r
+ SQLINTEGER minor;\r
+ SQLBindCol(stmt, 1, SQL_C_SLONG, &major, 0, nullptr);\r
+ SQLBindCol(stmt, 2, SQL_C_SLONG, &minor, 0, nullptr);\r
+\r
+ if ((sr = SQLFetch(stmt)) != SQL_NO_DATA)\r
+ return make_pair(major,minor);\r
+\r
+ m_log.error("no rows returned in version query");\r
+ throw IOException("ODBC StorageService failed to read version from database.");\r
+}\r
+\r
+bool ODBCStorageService::createRow(const char* table, const char* context, const char* key, const char* value, time_t expiration)\r
+{\r
+#ifdef _DEBUG\r
+ xmltooling::NDC ndc("createRow");\r
+#endif\r
+\r
+ char timebuf[32];\r
+ timestampFromTime(expiration, timebuf);\r
+\r
+ // Get statement handle.\r
+ ODBCConn conn(getHDBC());\r
+ SQLHSTMT stmt = getHSTMT(conn);\r
+\r
+ string q = string("INSERT INTO ") + table + " VALUES (?,?," + timebuf + ",1,?)";\r
+\r
+ SQLRETURN sr = SQLPrepare(stmt, (SQLCHAR*)q.c_str(), SQL_NTS);\r
+ if (!SQL_SUCCEEDED(sr)) {\r
+ m_log.error("SQLPrepare failed (t=%s, c=%s, k=%s)", table, context, key);\r
+ log_error(stmt, SQL_HANDLE_STMT);\r
+ throw IOException("ODBC StorageService failed to insert record.");\r
+ }\r
+ m_log.debug("SQLPrepare succeeded. SQL: %s", q.c_str());\r
+\r
+ SQLLEN b_ind = SQL_NTS;\r
+ sr = SQLBindParam(stmt, 1, SQL_C_CHAR, SQL_VARCHAR, 255, 0, const_cast<char*>(context), &b_ind);\r
+ if (!SQL_SUCCEEDED(sr)) {\r
+ m_log.error("SQLBindParam failed (context = %s)", context);\r
+ log_error(stmt, SQL_HANDLE_STMT);\r
+ throw IOException("ODBC StorageService failed to insert record.");\r
+ }\r
+ m_log.debug("SQLBindParam succeeded (context = %s)", context);\r
+\r
+ sr = SQLBindParam(stmt, 2, SQL_C_CHAR, SQL_VARCHAR, 255, 0, const_cast<char*>(key), &b_ind);\r
+ if (!SQL_SUCCEEDED(sr)) {\r
+ m_log.error("SQLBindParam failed (key = %s)", key);\r
+ log_error(stmt, SQL_HANDLE_STMT);\r
+ throw IOException("ODBC StorageService failed to insert record.");\r
+ }\r
+ m_log.debug("SQLBindParam succeeded (key = %s)", key);\r
+\r
+ if (strcmp(table, TEXT_TABLE)==0)\r
+ sr = SQLBindParam(stmt, 3, SQL_C_CHAR, SQL_LONGVARCHAR, strlen(value), 0, const_cast<char*>(value), &b_ind);\r
+ else\r
+ sr = SQLBindParam(stmt, 3, SQL_C_CHAR, SQL_VARCHAR, 255, 0, const_cast<char*>(value), &b_ind);\r
+ if (!SQL_SUCCEEDED(sr)) {\r
+ m_log.error("SQLBindParam failed (value = %s)", value);\r
+ log_error(stmt, SQL_HANDLE_STMT);\r
+ throw IOException("ODBC StorageService failed to insert record.");\r
+ }\r
+ m_log.debug("SQLBindParam succeeded (value = %s)", value);\r
+ \r
+ int attempts = 3;\r
+ pair<bool,bool> logres;\r
+ do {\r
+ logres = make_pair(false,false);\r
+ attempts--;\r
+ sr = SQLExecute(stmt);\r
+ if (SQL_SUCCEEDED(sr)) {\r
+ m_log.debug("SQLExecute of insert succeeded");\r
+ return true;\r
+ }\r
+ m_log.error("insert record failed (t=%s, c=%s, k=%s)", table, context, key);\r
+ logres = log_error(stmt, SQL_HANDLE_STMT, "23000");\r
+ if (logres.second) {\r
+ // Supposedly integrity violation.\r
+ // Try and delete any expired record still hanging around until the final attempt.\r
+ if (attempts > 0) {\r
+ reap(table, context);\r
+ logres.first = true; // force it to treat as a retryable error\r
+ continue;\r
+ }\r
+ return false;\r
+ }\r
+ } while (attempts && logres.first);\r
+\r
+ throw IOException("ODBC StorageService failed to insert record.");\r
+}\r
+\r
+int ODBCStorageService::readRow(const char *table, const char* context, const char* key, string* pvalue, time_t* pexpiration, int version)\r
+{\r
+#ifdef _DEBUG\r
+ xmltooling::NDC ndc("readRow");\r
+#endif\r
+\r
+ // Get statement handle.\r
+ ODBCConn conn(getHDBC());\r
+ SQLHSTMT stmt = getHSTMT(conn);\r
+\r
+ // Prepare and exectute select statement.\r
+ char timebuf[32];\r
+ timestampFromTime(time(nullptr), timebuf);\r
+ SQLString scontext(context);\r
+ SQLString skey(key);\r
+ string q("SELECT version");\r
+ if (pexpiration)\r
+ q += ",expires";\r
+ if (pvalue) {\r
+ pvalue->erase();\r
+ q = q + ",CASE version WHEN " + lexical_cast<string>(version) + " THEN null ELSE value END";\r
+ }\r
+ q = q + " FROM " + table + " WHERE context='" + scontext.tostr() + "' AND id='" + skey.tostr() + "' AND expires > " + timebuf;\r
+ if (m_log.isDebugEnabled())\r
+ m_log.debug("SQL: %s", q.c_str());\r
+\r
+ SQLRETURN sr=SQLExecDirect(stmt, (SQLCHAR*)q.c_str(), SQL_NTS);\r
+ if (!SQL_SUCCEEDED(sr)) {\r
+ m_log.error("error searching for (t=%s, c=%s, k=%s)", table, context, key);\r
+ log_error(stmt, SQL_HANDLE_STMT);\r
+ throw IOException("ODBC StorageService search failed.");\r
+ }\r
+\r
+ SQLSMALLINT ver;\r
+ SQLINTEGER widever;\r
+ SQL_TIMESTAMP_STRUCT expiration;\r
+\r
+ if (m_wideVersion)\r
+ SQLBindCol(stmt, 1, SQL_C_SLONG, &widever, 0, nullptr);\r
+ else\r
+ SQLBindCol(stmt, 1, SQL_C_SSHORT, &ver, 0, nullptr);\r
+ if (pexpiration)\r
+ SQLBindCol(stmt, 2, SQL_C_TYPE_TIMESTAMP, &expiration, 0, nullptr);\r
+\r
+ if ((sr = SQLFetch(stmt)) == SQL_NO_DATA) {\r
+ if (m_log.isDebugEnabled())\r
+ m_log.debug("search returned no data (t=%s, c=%s, k=%s)", table, context, key);\r
+ return 0;\r
+ }\r
+\r
+ if (pexpiration)\r
+ *pexpiration = timeFromTimestamp(expiration);\r
+\r
+ if (version == (m_wideVersion ? widever : ver)) {\r
+ if (m_log.isDebugEnabled())\r
+ m_log.debug("versioned search detected no change (t=%s, c=%s, k=%s)", table, context, key);\r
+ return version; // nothing's changed, so just echo back the version\r
+ }\r
+\r
+ if (pvalue) {\r
+ SQLLEN len;\r
+ SQLCHAR buf[LONGDATA_BUFLEN];\r
+ while ((sr = SQLGetData(stmt, (pexpiration ? 3 : 2), SQL_C_CHAR, buf, sizeof(buf), &len)) != SQL_NO_DATA) {\r
+ if (!SQL_SUCCEEDED(sr)) {\r
+ m_log.error("error while reading text field from result set");\r
+ log_error(stmt, SQL_HANDLE_STMT);\r
+ throw IOException("ODBC StorageService search failed to read data from result set.");\r
+ }\r
+ pvalue->append((char*)buf);\r
+ }\r
+ }\r
+ \r
+ return (m_wideVersion ? widever : ver);\r
+}\r
+\r
+int ODBCStorageService::updateRow(const char *table, const char* context, const char* key, const char* value, time_t expiration, int version)\r
+{\r
+#ifdef _DEBUG\r
+ xmltooling::NDC ndc("updateRow");\r
+#endif\r
+\r
+ if (!value && !expiration)\r
+ throw IOException("ODBC StorageService given invalid update instructions.");\r
+\r
+ // Get statement handle. Disable auto-commit mode to wrap select + update.\r
+ ODBCConn conn(getHDBC());\r
+ SQLRETURN sr = SQLSetConnectAttr(conn, SQL_ATTR_AUTOCOMMIT, SQL_AUTOCOMMIT_OFF, 0);\r
+ if (!SQL_SUCCEEDED(sr))\r
+ throw IOException("ODBC StorageService failed to disable auto-commit mode.");\r
+ conn.autoCommit = false;\r
+ SQLHSTMT stmt = getHSTMT(conn);\r
+\r
+ // First, fetch the current version for later, which also ensures the record still exists.\r
+ char timebuf[32];\r
+ timestampFromTime(time(nullptr), timebuf);\r
+ SQLString scontext(context);\r
+ SQLString skey(key);\r
+ string q("SELECT version FROM ");\r
+ q = q + table + " WHERE context='" + scontext.tostr() + "' AND id='" + skey.tostr() + "' AND expires > " + timebuf;\r
+\r
+ m_log.debug("SQL: %s", q.c_str());\r
+\r
+ sr = SQLExecDirect(stmt, (SQLCHAR*)q.c_str(), SQL_NTS);\r
+ if (!SQL_SUCCEEDED(sr)) {\r
+ m_log.error("error searching for (t=%s, c=%s, k=%s)", table, context, key);\r
+ log_error(stmt, SQL_HANDLE_STMT);\r
+ throw IOException("ODBC StorageService search failed.");\r
+ }\r
+\r
+ SQLSMALLINT ver;\r
+ SQLINTEGER widever;\r
+ if (m_wideVersion)\r
+ SQLBindCol(stmt, 1, SQL_C_SLONG, &widever, 0, nullptr);\r
+ else\r
+ SQLBindCol(stmt, 1, SQL_C_SSHORT, &ver, 0, nullptr);\r
+ if ((sr = SQLFetch(stmt)) == SQL_NO_DATA) {\r
+ return 0;\r
+ }\r
+\r
+ // Check version?\r
+ if (version > 0 && version != (m_wideVersion ? widever : ver)) {\r
+ return -1;\r
+ }\r
+ else if ((m_wideVersion && widever == INT_MAX) || (!m_wideVersion && ver == 32767)) {\r
+ m_log.error("record version overflow (t=%s, c=%s, k=%s)", table, context, key);\r
+ throw IOException("Version overflow, record in ODBC StorageService could not be updated.");\r
+ }\r
+\r
+ SQLFreeHandle(SQL_HANDLE_STMT, stmt);\r
+ stmt = getHSTMT(conn);\r
+\r
+ // Prepare and exectute update statement.\r
+ q = string("UPDATE ") + table + " SET ";\r
+\r
+ if (value)\r
+ q = q + "value=?, version=version+1";\r
+\r
+ if (expiration) {\r
+ timestampFromTime(expiration, timebuf);\r
+ if (value)\r
+ q += ',';\r
+ q = q + "expires = " + timebuf;\r
+ }\r
+\r
+ q = q + " WHERE context='" + scontext.tostr() + "' AND id='" + skey.tostr() + "'";\r
+\r
+ sr = SQLPrepare(stmt, (SQLCHAR*)q.c_str(), SQL_NTS);\r
+ if (!SQL_SUCCEEDED(sr)) {\r
+ m_log.error("update of record failed (t=%s, c=%s, k=%s", table, context, key);\r
+ log_error(stmt, SQL_HANDLE_STMT);\r
+ throw IOException("ODBC StorageService failed to update record.");\r
+ }\r
+ m_log.debug("SQLPrepare succeeded. SQL: %s", q.c_str());\r
+\r
+ SQLLEN b_ind = SQL_NTS;\r
+ if (value) {\r
+ if (strcmp(table, TEXT_TABLE)==0)\r
+ sr = SQLBindParam(stmt, 1, SQL_C_CHAR, SQL_LONGVARCHAR, strlen(value), 0, const_cast<char*>(value), &b_ind);\r
+ else\r
+ sr = SQLBindParam(stmt, 1, SQL_C_CHAR, SQL_VARCHAR, 255, 0, const_cast<char*>(value), &b_ind);\r
+ if (!SQL_SUCCEEDED(sr)) {\r
+ m_log.error("SQLBindParam failed (value = %s)", value);\r
+ log_error(stmt, SQL_HANDLE_STMT);\r
+ throw IOException("ODBC StorageService failed to update record.");\r
+ }\r
+ m_log.debug("SQLBindParam succeeded (value = %s)", value);\r
+ }\r
+\r
+ int attempts = 3;\r
+ pair<bool,bool> logres;\r
+ do {\r
+ logres = make_pair(false,false);\r
+ attempts--;\r
+ sr = SQLExecute(stmt);\r
+ if (sr == SQL_NO_DATA)\r
+ return 0; // went missing?\r
+ else if (SQL_SUCCEEDED(sr)) {\r
+ m_log.debug("SQLExecute of update succeeded");\r
+ return (m_wideVersion ? widever : ver) + 1;\r
+ }\r
+\r
+ m_log.error("update of record failed (t=%s, c=%s, k=%s)", table, context, key);\r
+ logres = log_error(stmt, SQL_HANDLE_STMT);\r
+ } while (attempts && logres.first);\r
+\r
+ throw IOException("ODBC StorageService failed to update record.");\r
+}\r
+\r
+bool ODBCStorageService::deleteRow(const char *table, const char *context, const char* key)\r
+{\r
+#ifdef _DEBUG\r
+ xmltooling::NDC ndc("deleteRow");\r
+#endif\r
+\r
+ // Get statement handle.\r
+ ODBCConn conn(getHDBC());\r
+ SQLHSTMT stmt = getHSTMT(conn);\r
+\r
+ // Prepare and execute delete statement.\r
+ SQLString scontext(context);\r
+ SQLString skey(key);\r
+ string q = string("DELETE FROM ") + table + " WHERE context='" + scontext.tostr() + "' AND id='" + skey.tostr() + "'";\r
+ m_log.debug("SQL: %s", q.c_str());\r
+\r
+ SQLRETURN sr = SQLExecDirect(stmt, (SQLCHAR*)q.c_str(), SQL_NTS);\r
+ if (sr == SQL_NO_DATA)\r
+ return false;\r
+ else if (!SQL_SUCCEEDED(sr)) {\r
+ m_log.error("error deleting record (t=%s, c=%s, k=%s)", table, context, key);\r
+ log_error(stmt, SQL_HANDLE_STMT);\r
+ throw IOException("ODBC StorageService failed to delete record.");\r
+ }\r
+\r
+ return true;\r
+}\r
+\r
+\r
+void ODBCStorageService::cleanup()\r
+{\r
+#ifdef _DEBUG\r
+ xmltooling::NDC ndc("cleanup");\r
+#endif\r
+\r
+ scoped_ptr<Mutex> mutex(Mutex::create());\r
+\r
+ mutex->lock();\r
+\r
+ m_log.info("cleanup thread started... running every %d secs", m_cleanupInterval);\r
+\r
+ while (!shutdown) {\r
+ shutdown_wait->timedwait(mutex.get(), m_cleanupInterval);\r
+ if (shutdown)\r
+ break;\r
+ try {\r
+ reap(nullptr);\r
+ }\r
+ catch (std::exception& ex) {\r
+ m_log.error("cleanup thread swallowed exception: %s", ex.what());\r
+ }\r
+ }\r
+\r
+ m_log.info("cleanup thread exiting...");\r
+\r
+ mutex->unlock();\r
+ Thread::exit(nullptr);\r
+}\r
+\r
+void* ODBCStorageService::cleanup_fn(void* cache_p)\r
+{\r
+ ODBCStorageService* cache = (ODBCStorageService*)cache_p;\r
+\r
+#ifndef WIN32\r
+ // First, let's block all signals\r
+ Thread::mask_all_signals();\r
+#endif\r
+\r
+ // Now run the cleanup process.\r
+ cache->cleanup();\r
+ return nullptr;\r
+}\r
+\r
+void ODBCStorageService::updateContext(const char *table, const char* context, time_t expiration)\r
+{\r
+#ifdef _DEBUG\r
+ xmltooling::NDC ndc("updateContext");\r
+#endif\r
+\r
+ // Get statement handle.\r
+ ODBCConn conn(getHDBC());\r
+ SQLHSTMT stmt = getHSTMT(conn);\r
+\r
+ char timebuf[32];\r
+ timestampFromTime(expiration, timebuf);\r
+\r
+ char nowbuf[32];\r
+ timestampFromTime(time(nullptr), nowbuf);\r
+\r
+ SQLString scontext(context);\r
+ string q = string("UPDATE ") + table + " SET expires = " + timebuf + " WHERE context='" + scontext.tostr() + "' AND expires > " + nowbuf;\r
+\r
+ m_log.debug("SQL: %s", q.c_str());\r
+\r
+ SQLRETURN sr = SQLExecDirect(stmt, (SQLCHAR*)q.c_str(), SQL_NTS);\r
+ if ((sr != SQL_NO_DATA) && !SQL_SUCCEEDED(sr)) {\r
+ m_log.error("error updating records (t=%s, c=%s)", table, context ? context : "all");\r
+ log_error(stmt, SQL_HANDLE_STMT);\r
+ throw IOException("ODBC StorageService failed to update context expiration.");\r
+ }\r
+}\r
+\r
+void ODBCStorageService::reap(const char *table, const char* context)\r
+{\r
+#ifdef _DEBUG\r
+ xmltooling::NDC ndc("reap");\r
+#endif\r
+\r
+ // Get statement handle.\r
+ ODBCConn conn(getHDBC());\r
+ SQLHSTMT stmt = getHSTMT(conn);\r
+\r
+ // Prepare and execute delete statement.\r
+ char nowbuf[32];\r
+ timestampFromTime(time(nullptr), nowbuf);\r
+ string q;\r
+ if (context) {\r
+ SQLString scontext(context);\r
+ q = string("DELETE FROM ") + table + " WHERE context='" + scontext.tostr() + "' AND expires <= " + nowbuf;\r
+ }\r
+ else {\r
+ q = string("DELETE FROM ") + table + " WHERE expires <= " + nowbuf;\r
+ }\r
+ m_log.debug("SQL: %s", q.c_str());\r
+\r
+ SQLRETURN sr = SQLExecDirect(stmt, (SQLCHAR*)q.c_str(), SQL_NTS);\r
+ if ((sr != SQL_NO_DATA) && !SQL_SUCCEEDED(sr)) {\r
+ m_log.error("error expiring records (t=%s, c=%s)", table, context ? context : "all");\r
+ log_error(stmt, SQL_HANDLE_STMT);\r
+ throw IOException("ODBC StorageService failed to purge expired records.");\r
+ }\r
+}\r
+\r
+void ODBCStorageService::deleteContext(const char *table, const char* context)\r
+{\r
+#ifdef _DEBUG\r
+ xmltooling::NDC ndc("deleteContext");\r
+#endif\r
+\r
+ // Get statement handle.\r
+ ODBCConn conn(getHDBC());\r
+ SQLHSTMT stmt = getHSTMT(conn);\r
+\r
+ // Prepare and execute delete statement.\r
+ SQLString scontext(context);\r
+ string q = string("DELETE FROM ") + table + " WHERE context='" + scontext.tostr() + "'";\r
+ m_log.debug("SQL: %s", q.c_str());\r
+\r
+ SQLRETURN sr = SQLExecDirect(stmt, (SQLCHAR*)q.c_str(), SQL_NTS);\r
+ if ((sr != SQL_NO_DATA) && !SQL_SUCCEEDED(sr)) {\r
+ m_log.error("error deleting context (t=%s, c=%s)", table, context);\r
+ log_error(stmt, SQL_HANDLE_STMT);\r
+ throw IOException("ODBC StorageService failed to delete context.");\r
+ }\r
+}\r
+\r
+extern "C" int ODBCSTORE_EXPORTS xmltooling_extension_init(void*)\r
+{\r
+ // Register this SS type\r
+ XMLToolingConfig::getConfig().StorageServiceManager.registerFactory("ODBC", ODBCStorageServiceFactory);\r
+ return 0;\r
+}\r
+\r
+extern "C" void ODBCSTORE_EXPORTS xmltooling_extension_term()\r
+{\r
+ XMLToolingConfig::getConfig().StorageServiceManager.deregisterFactory("ODBC");\r
+}\r