Add conditional fetch of values during read.
authorcantor <cantor@cb58f699-b61c-0410-a6fe-9272a202ed29>
Sat, 23 Jun 2007 01:50:08 +0000 (01:50 +0000)
committercantor <cantor@cb58f699-b61c-0410-a6fe-9272a202ed29>
Sat, 23 Jun 2007 01:50:08 +0000 (01:50 +0000)
Disconnect handles before freeing.

git-svn-id: https://svn.middleware.georgetown.edu/cpp-sp/trunk@2325 cb58f699-b61c-0410-a6fe-9272a202ed29

odbc-store/odbc-store.cpp

index 2693a9d..61f4314 100644 (file)
@@ -99,6 +99,7 @@ namespace {
         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.");
@@ -448,17 +449,19 @@ int ODBCStorageService::readRow(
     timestampFromTime(time(NULL), timebuf);
     char *scontext = makeSafeSQL(context);
     char *skey = makeSafeSQL(key);
-    string q("SELECT version");
+    ostringstream q;
+    q << "SELECT version";
     if (pexpiration)
-        q += ",expires";
+        q << ",expires";
     if (pvalue)
-        q += ",value";
-    q = q + " FROM " + table + " WHERE context='" + scontext + "' AND id='" + skey + "' AND expires > " + timebuf;
+        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);
-    m_log.debug("SQL: %s", q.c_str());
+    if (m_log.isDebugEnabled())
+        m_log.debug("SQL: %s", q.str().c_str());
 
-    SQLRETURN sr=SQLExecDirect(stmt, (SQLCHAR*)q.c_str(), SQL_NTS);
+    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);