ca143481fc6b30ee5a7431449a83d2e7cab00694
[shibboleth/sp.git] / odbc-store / odbc-store.cpp
1 /*
2  *  Copyright 2001-2007 Internet2
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /**
18  * odbc-store.cpp
19  *
20  * Storage Service using ODBC
21  */
22
23 #if defined (_MSC_VER) || defined(__BORLANDC__)
24 # include "config_win32.h"
25 #else
26 # include "config.h"
27 #endif
28
29 #ifdef WIN32
30 # define _CRT_NONSTDC_NO_DEPRECATE 1
31 # define _CRT_SECURE_NO_DEPRECATE 1
32 #endif
33
34 #ifdef WIN32
35 # define ODBCSTORE_EXPORTS __declspec(dllexport)
36 #else
37 # define ODBCSTORE_EXPORTS
38 #endif
39
40 #include <xercesc/util/XMLUniDefs.hpp>
41 #include <xmltooling/logging.h>
42 #include <xmltooling/XMLToolingConfig.h>
43 #include <xmltooling/util/NDC.h>
44 #include <xmltooling/util/StorageService.h>
45 #include <xmltooling/util/Threads.h>
46 #include <xmltooling/util/XMLHelper.h>
47
48 #include <sql.h>
49 #include <sqlext.h>
50
51 using namespace xmltooling::logging;
52 using namespace xmltooling;
53 using namespace xercesc;
54 using namespace std;
55
56 #define PLUGIN_VER_MAJOR 1
57 #define PLUGIN_VER_MINOR 0
58
59 #define LONGDATA_BUFLEN 16384
60
61 #define COLSIZE_CONTEXT 255
62 #define COLSIZE_ID 255
63 #define COLSIZE_STRING_VALUE 255
64
65 #define STRING_TABLE "strings"
66 #define TEXT_TABLE "texts"
67
68 /* table definitions
69 CREATE TABLE version (
70     major tinyint NOT NULL,
71     minor tinyint NOT NULL
72     )
73
74 CREATE TABLE strings (
75     context varchar(255) not null,
76     id varchar(255) not null,
77     expires datetime not null,
78     version smallint not null,
79     value varchar(255) not null,
80     PRIMARY KEY (context, id)
81     )
82
83 CREATE TABLE texts (
84     context varchar(255) not null,
85     id varchar(255) not null,
86     expires datetime not null,
87     version smallint not null,
88     value text not null,
89     PRIMARY KEY (context, id)
90     )
91 */
92
93 namespace {
94     static const XMLCh cleanupInterval[] =  UNICODE_LITERAL_15(c,l,e,a,n,u,p,I,n,t,e,r,v,a,l);
95     static const XMLCh ConnectionString[] = UNICODE_LITERAL_16(C,o,n,n,e,c,t,i,o,n,S,t,r,i,n,g);
96
97     // RAII for ODBC handles
98     struct ODBCConn {
99         ODBCConn(SQLHDBC conn) : handle(conn) {}
100         ~ODBCConn() {
101             SQLRETURN sr = SQLEndTran(SQL_HANDLE_DBC, handle, SQL_COMMIT);
102             SQLDisconnect(handle);
103             SQLFreeHandle(SQL_HANDLE_DBC,handle);
104             if (!SQL_SUCCEEDED(sr))
105                 throw IOException("Failed to commit connection.");
106         }
107         operator SQLHDBC() {return handle;}
108         SQLHDBC handle;
109     };
110
111     class ODBCStorageService : public StorageService
112     {
113     public:
114         ODBCStorageService(const DOMElement* e);
115         virtual ~ODBCStorageService();
116
117         bool createString(const char* context, const char* key, const char* value, time_t expiration) {
118             return createRow(STRING_TABLE, context, key, value, expiration);
119         }
120         int readString(const char* context, const char* key, string* pvalue=NULL, time_t* pexpiration=NULL, int version=0) {
121             return readRow(STRING_TABLE, context, key, pvalue, pexpiration, version, false);
122         }
123         int updateString(const char* context, const char* key, const char* value=NULL, time_t expiration=0, int version=0) {
124             return updateRow(STRING_TABLE, context, key, value, expiration, version);
125         }
126         bool deleteString(const char* context, const char* key) {
127             return deleteRow(STRING_TABLE, context, key);
128         }
129
130         bool createText(const char* context, const char* key, const char* value, time_t expiration) {
131             return createRow(TEXT_TABLE, context, key, value, expiration);
132         }
133         int readText(const char* context, const char* key, string* pvalue=NULL, time_t* pexpiration=NULL, int version=0) {
134             return readRow(TEXT_TABLE, context, key, pvalue, pexpiration, version, true);
135         }
136         int updateText(const char* context, const char* key, const char* value=NULL, time_t expiration=0, int version=0) {
137             return updateRow(TEXT_TABLE, context, key, value, expiration, version);
138         }
139         bool deleteText(const char* context, const char* key) {
140             return deleteRow(TEXT_TABLE, context, key);
141         }
142
143         void reap(const char* context) {
144             reap(STRING_TABLE, context);
145             reap(TEXT_TABLE, context);
146         }
147
148         void updateContext(const char* context, time_t expiration) {
149             updateContext(STRING_TABLE, context, expiration);
150             updateContext(TEXT_TABLE, context, expiration);
151         }
152
153         void deleteContext(const char* context) {
154             deleteContext(STRING_TABLE, context);
155             deleteContext(TEXT_TABLE, context);
156         }
157          
158
159     private:
160         bool createRow(const char *table, const char* context, const char* key, const char* value, time_t expiration);
161         int readRow(const char *table, const char* context, const char* key, string* pvalue, time_t* pexpiration, int version, bool text);
162         int updateRow(const char *table, const char* context, const char* key, const char* value, time_t expiration, int version);
163         bool deleteRow(const char *table, const char* context, const char* key);
164
165         void reap(const char* table, const char* context);
166         void updateContext(const char* table, const char* context, time_t expiration);
167         void deleteContext(const char* table, const char* context);
168
169         SQLHDBC getHDBC();
170         SQLHSTMT getHSTMT(SQLHDBC);
171         pair<int,int> getVersion(SQLHDBC);
172         bool log_error(SQLHANDLE handle, SQLSMALLINT htype, const char* checkfor=NULL);
173
174         static void* cleanup_fn(void*); 
175         void cleanup();
176
177         Category& m_log;
178         int m_cleanupInterval;
179         CondWait* shutdown_wait;
180         Thread* cleanup_thread;
181         bool shutdown;
182
183         SQLHENV m_henv;
184         string m_connstring;
185     };
186
187     StorageService* ODBCStorageServiceFactory(const DOMElement* const & e)
188     {
189         return new ODBCStorageService(e);
190     }
191
192     // convert SQL timestamp to time_t 
193     time_t timeFromTimestamp(SQL_TIMESTAMP_STRUCT expires)
194     {
195         time_t ret;
196         struct tm t;
197         t.tm_sec=expires.second;
198         t.tm_min=expires.minute;
199         t.tm_hour=expires.hour;
200         t.tm_mday=expires.day;
201         t.tm_mon=expires.month-1;
202         t.tm_year=expires.year-1900;
203         t.tm_isdst=0;
204 #if defined(HAVE_TIMEGM)
205         ret = timegm(&t);
206 #else
207         ret = mktime(&t) - timezone;
208 #endif
209         return (ret);
210     }
211
212     // conver time_t to SQL string
213     void timestampFromTime(time_t t, char* ret)
214     {
215 #ifdef HAVE_GMTIME_R
216         struct tm res;
217         struct tm* ptime=gmtime_r(&t,&res);
218 #else
219         struct tm* ptime=gmtime(&t);
220 #endif
221         strftime(ret,32,"{ts '%Y-%m-%d %H:%M:%S'}",ptime);
222     }
223
224     // make a string safe for SQL command
225     // result to be free'd only if it isn't the input
226     static char *makeSafeSQL(const char *src)
227     {
228        int ns = 0;
229        int nc = 0;
230        char *s;
231     
232        // see if any conversion needed
233        for (s=(char*)src; *s; nc++,s++) if (*s=='\'') ns++;
234        if (ns==0) return ((char*)src);
235     
236        char *safe = new char[(nc+2*ns+1)];
237        for (s=safe; *src; src++) {
238            if (*src=='\'') *s++ = '\'';
239            *s++ = (char)*src;
240        }
241        *s = '\0';
242        return (safe);
243     }
244
245     void freeSafeSQL(char *safe, const char *src)
246     {
247         if (safe!=src)
248             delete[](safe);
249     }
250 };
251
252 ODBCStorageService::ODBCStorageService(const DOMElement* e) : m_log(Category::getInstance("XMLTooling.StorageService")),
253    m_cleanupInterval(900), shutdown_wait(NULL), cleanup_thread(NULL), shutdown(false), m_henv(SQL_NULL_HANDLE)
254 {
255 #ifdef _DEBUG
256     xmltooling::NDC ndc("ODBCStorageService");
257 #endif
258
259     const XMLCh* tag=e ? e->getAttributeNS(NULL,cleanupInterval) : NULL;
260     if (tag && *tag)
261         m_cleanupInterval = XMLString::parseInt(tag);
262     if (!m_cleanupInterval)
263         m_cleanupInterval = 900;
264
265     if (m_henv == SQL_NULL_HANDLE) {
266         // Enable connection pooling.
267         SQLSetEnvAttr(SQL_NULL_HANDLE, SQL_ATTR_CONNECTION_POOLING, (void*)SQL_CP_ONE_PER_HENV, 0);
268
269         // Allocate the environment.
270         if (!SQL_SUCCEEDED(SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &m_henv)))
271             throw XMLToolingException("ODBC failed to initialize.");
272
273         // Specify ODBC 3.x
274         SQLSetEnvAttr(m_henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
275
276         m_log.info("ODBC initialized");
277     }
278
279     // Grab connection string from the configuration.
280     e = e ? XMLHelper::getFirstChildElement(e,ConnectionString) : NULL;
281     if (!e || !e->hasChildNodes()) {
282         SQLFreeHandle(SQL_HANDLE_ENV, m_henv);
283         throw XMLToolingException("ODBC StorageService requires ConnectionString element in configuration.");
284     }
285     auto_ptr_char arg(e->getFirstChild()->getNodeValue());
286     m_connstring=arg.get();
287
288     // Connect and check version.
289     ODBCConn conn(getHDBC());
290     pair<int,int> v=getVersion(conn);
291
292     // Make sure we've got the right version.
293     if (v.first != PLUGIN_VER_MAJOR) {
294         SQLFreeHandle(SQL_HANDLE_ENV, m_henv);
295         m_log.crit("unknown database version: %d.%d", v.first, v.second);
296         throw XMLToolingException("Unknown database version for ODBC StorageService.");
297     }
298
299     // Initialize the cleanup thread
300     shutdown_wait = CondWait::create();
301     cleanup_thread = Thread::create(&cleanup_fn, (void*)this);
302 }
303
304 ODBCStorageService::~ODBCStorageService()
305 {
306     shutdown = true;
307     shutdown_wait->signal();
308     cleanup_thread->join(NULL);
309     delete shutdown_wait;
310     if (m_henv != SQL_NULL_HANDLE)
311         SQLFreeHandle(SQL_HANDLE_ENV, m_henv);
312 }
313
314 bool ODBCStorageService::log_error(SQLHANDLE handle, SQLSMALLINT htype, const char* checkfor)
315 {
316     SQLSMALLINT  i = 0;
317     SQLINTEGER   native;
318     SQLCHAR      state[7];
319     SQLCHAR      text[256];
320     SQLSMALLINT  len;
321     SQLRETURN    ret;
322
323     bool res = false;
324     do {
325         ret = SQLGetDiagRec(htype, handle, ++i, state, &native, text, sizeof(text), &len);
326         if (SQL_SUCCEEDED(ret)) {
327             m_log.error("ODBC Error: %s:%ld:%ld:%s", state, i, native, text);
328             if (checkfor && !strcmp(checkfor, (const char*)state))
329                 res = true;
330         }
331     } while(SQL_SUCCEEDED(ret));
332     return res;
333 }
334
335 SQLHDBC ODBCStorageService::getHDBC()
336 {
337 #ifdef _DEBUG
338     xmltooling::NDC ndc("getHDBC");
339 #endif
340
341     // Get a handle.
342     SQLHDBC handle;
343     SQLRETURN sr=SQLAllocHandle(SQL_HANDLE_DBC, m_henv, &handle);
344     if (!SQL_SUCCEEDED(sr)) {
345         m_log.error("failed to allocate connection handle");
346         log_error(m_henv, SQL_HANDLE_ENV);
347         throw IOException("ODBC StorageService failed to allocate a connection handle.");
348     }
349
350     sr=SQLDriverConnect(handle,NULL,(SQLCHAR*)m_connstring.c_str(),m_connstring.length(),NULL,0,NULL,SQL_DRIVER_NOPROMPT);
351     if (!SQL_SUCCEEDED(sr)) {
352         m_log.error("failed to connect to database");
353         log_error(handle, SQL_HANDLE_DBC);
354         throw IOException("ODBC StorageService failed to connect to database.");
355     }
356
357     sr = SQLSetConnectAttr(handle, SQL_ATTR_AUTOCOMMIT, SQL_AUTOCOMMIT_OFF, NULL);
358     if (!SQL_SUCCEEDED(sr))
359         throw IOException("ODBC StorageService failed to disable auto-commit mode.");
360     sr = SQLSetConnectAttr(handle, SQL_ATTR_TXN_ISOLATION, (SQLPOINTER)SQL_TXN_SERIALIZABLE, NULL);
361     if (!SQL_SUCCEEDED(sr))
362         throw IOException("ODBC StorageService failed to enable transaction isolation.");
363
364     return handle;
365 }
366
367 SQLHSTMT ODBCStorageService::getHSTMT(SQLHDBC conn)
368 {
369     SQLHSTMT hstmt;
370     SQLRETURN sr=SQLAllocHandle(SQL_HANDLE_STMT,conn,&hstmt);
371     if (!SQL_SUCCEEDED(sr)) {
372         m_log.error("failed to allocate statement handle");
373         log_error(conn, SQL_HANDLE_DBC);
374         throw IOException("ODBC StorageService failed to allocate a statement handle.");
375     }
376     return hstmt;
377 }
378
379 pair<int,int> ODBCStorageService::getVersion(SQLHDBC conn)
380 {
381     // Grab the version number from the database.
382     SQLHSTMT stmt = getHSTMT(conn);
383     
384     SQLRETURN sr=SQLExecDirect(stmt, (SQLCHAR*)"SELECT major,minor FROM version", SQL_NTS);
385     if (!SQL_SUCCEEDED(sr)) {
386         m_log.error("failed to read version from database");
387         log_error(stmt, SQL_HANDLE_STMT);
388         throw IOException("ODBC StorageService failed to read version from database.");
389     }
390
391     SQLINTEGER major;
392     SQLINTEGER minor;
393     SQLBindCol(stmt,1,SQL_C_SLONG,&major,0,NULL);
394     SQLBindCol(stmt,2,SQL_C_SLONG,&minor,0,NULL);
395
396     if ((sr=SQLFetch(stmt)) != SQL_NO_DATA)
397         return pair<int,int>(major,minor);
398
399     m_log.error("no rows returned in version query");
400     throw IOException("ODBC StorageService failed to read version from database.");
401 }
402
403 bool ODBCStorageService::createRow(const char* table, const char* context, const char* key, const char* value, time_t expiration)
404 {
405 #ifdef _DEBUG
406     xmltooling::NDC ndc("createRow");
407 #endif
408
409     char timebuf[32];
410     timestampFromTime(expiration, timebuf);
411
412     // Get statement handle.
413     ODBCConn conn(getHDBC());
414     SQLHSTMT stmt = getHSTMT(conn);
415
416     // Prepare and exectute insert statement.
417     //char *scontext = makeSafeSQL(context);
418     //char *skey = makeSafeSQL(key);
419     //char *svalue = makeSafeSQL(value);
420     string q  = string("INSERT INTO ") + table + " VALUES (?,?," + timebuf + ",1,?)";
421
422     SQLRETURN sr = SQLPrepare(stmt, (SQLCHAR*)q.c_str(), SQL_NTS);
423     if (!SQL_SUCCEEDED(sr)) {
424         m_log.error("SQLPrepare failed (t=%s, c=%s, k=%s)", table, context, key);
425         log_error(stmt, SQL_HANDLE_STMT);
426         throw IOException("ODBC StorageService failed to insert record.");
427     }
428     m_log.debug("SQLPrepare() succeded. SQL: %s", q.c_str());
429
430     SQLINTEGER b_ind = SQL_NTS;
431     sr = SQLBindParam(stmt, 1, SQL_C_CHAR, SQL_VARCHAR, 0, 0, const_cast<char*>(context), &b_ind);
432     if (!SQL_SUCCEEDED(sr)) {
433         m_log.error("SQLBindParam failed (context = %s)", context);
434         log_error(stmt, SQL_HANDLE_STMT);
435         throw IOException("ODBC StorageService failed to insert record.");
436     }
437     m_log.debug("SQLBindParam succeded (context = %s)", context);
438
439     sr = SQLBindParam(stmt, 2, SQL_C_CHAR, SQL_VARCHAR, 0, 0, const_cast<char*>(key), &b_ind);
440     if (!SQL_SUCCEEDED(sr)) {
441         m_log.error("SQLBindParam failed (key = %s)", key);
442         log_error(stmt, SQL_HANDLE_STMT);
443         throw IOException("ODBC StorageService failed to insert record.");
444     }
445     m_log.debug("SQLBindParam succeded (key = %s)", key);
446
447     sr = SQLBindParam(stmt, 3, SQL_C_CHAR, (strcmp(table, TEXT_TABLE)==0 ? SQL_LONGVARCHAR : SQL_VARCHAR), 0, 0, const_cast<char*>(value), &b_ind);
448     if (!SQL_SUCCEEDED(sr)) {
449         m_log.error("SQLBindParam failed (value = %s)", value);
450         log_error(stmt, SQL_HANDLE_STMT);
451         throw IOException("ODBC StorageService failed to insert record.");
452     }
453     m_log.debug("SQLBindParam succeded (value = %s)", value);
454     
455     //freeSafeSQL(scontext, context);
456     //freeSafeSQL(skey, key);
457     //freeSafeSQL(svalue, value);
458     //m_log.debug("SQL: %s", q.c_str());
459
460     sr=SQLExecute(stmt);
461     if (!SQL_SUCCEEDED(sr)) {
462         m_log.error("insert record failed (t=%s, c=%s, k=%s)", table, context, key);
463         if (log_error(stmt, SQL_HANDLE_STMT, "23000"))
464             return false;   // supposedly integrity violation?
465         throw IOException("ODBC StorageService failed to insert record.");
466     }
467     return true;
468 }
469
470 int ODBCStorageService::readRow(
471     const char *table, const char* context, const char* key, string* pvalue, time_t* pexpiration, int version, bool text
472     )
473 {
474 #ifdef _DEBUG
475     xmltooling::NDC ndc("readRow");
476 #endif
477
478     // Get statement handle.
479     ODBCConn conn(getHDBC());
480     SQLHSTMT stmt = getHSTMT(conn);
481
482     // Prepare and exectute select statement.
483     char timebuf[32];
484     timestampFromTime(time(NULL), timebuf);
485     char *scontext = makeSafeSQL(context);
486     char *skey = makeSafeSQL(key);
487     ostringstream q;
488     q << "SELECT version";
489     if (pexpiration)
490         q << ",expires";
491     if (pvalue)
492         q << ",CASE version WHEN " << version << " THEN NULL ELSE value END";
493     q << " FROM " << table << " WHERE context='" << scontext << "' AND id='" << skey << "' AND expires > " << timebuf;
494     freeSafeSQL(scontext, context);
495     freeSafeSQL(skey, key);
496     if (m_log.isDebugEnabled())
497         m_log.debug("SQL: %s", q.str().c_str());
498
499     SQLRETURN sr=SQLExecDirect(stmt, (SQLCHAR*)q.str().c_str(), SQL_NTS);
500     if (!SQL_SUCCEEDED(sr)) {
501         m_log.error("error searching for (t=%s, c=%s, k=%s)", table, context, key);
502         log_error(stmt, SQL_HANDLE_STMT);
503         throw IOException("ODBC StorageService search failed.");
504     }
505
506     SQLSMALLINT ver;
507     SQL_TIMESTAMP_STRUCT expiration;
508
509     SQLBindCol(stmt,1,SQL_C_SSHORT,&ver,0,NULL);
510     if (pexpiration)
511         SQLBindCol(stmt,2,SQL_C_TYPE_TIMESTAMP,&expiration,0,NULL);
512
513     if ((sr=SQLFetch(stmt)) == SQL_NO_DATA)
514         return 0;
515
516     if (pexpiration)
517         *pexpiration = timeFromTimestamp(expiration);
518
519     if (version == ver)
520         return version; // nothing's changed, so just echo back the version
521
522     if (pvalue) {
523         SQLINTEGER len;
524         SQLCHAR buf[LONGDATA_BUFLEN];
525         while ((sr=SQLGetData(stmt,pexpiration ? 3 : 2,SQL_C_CHAR,buf,sizeof(buf),&len)) != SQL_NO_DATA) {
526             if (!SQL_SUCCEEDED(sr)) {
527                 m_log.error("error while reading text field from result set");
528                 log_error(stmt, SQL_HANDLE_STMT);
529                 throw IOException("ODBC StorageService search failed to read data from result set.");
530             }
531             pvalue->append((char*)buf);
532         }
533     }
534     
535     return ver;
536 }
537
538 int ODBCStorageService::updateRow(const char *table, const char* context, const char* key, const char* value, time_t expiration, int version)
539 {
540 #ifdef _DEBUG
541     xmltooling::NDC ndc("updateRow");
542 #endif
543
544     if (!value && !expiration)
545         throw IOException("ODBC StorageService given invalid update instructions.");
546
547     // Get statement handle.
548     ODBCConn conn(getHDBC());
549     SQLHSTMT stmt = getHSTMT(conn);
550
551     // First, fetch the current version for later, which also ensures the record still exists.
552     char timebuf[32];
553     timestampFromTime(time(NULL), timebuf);
554     char *scontext = makeSafeSQL(context);
555     char *skey = makeSafeSQL(key);
556     string q("SELECT version FROM ");
557     q = q + table + " WHERE context='" + scontext + "' AND id='" + key + "' AND expires > " + timebuf;
558
559     m_log.debug("SQL: %s", q.c_str());
560
561     SQLRETURN sr=SQLExecDirect(stmt, (SQLCHAR*)q.c_str(), SQL_NTS);
562     if (!SQL_SUCCEEDED(sr)) {
563         freeSafeSQL(scontext, context);
564         freeSafeSQL(skey, key);
565         m_log.error("error searching for (t=%s, c=%s, k=%s)", table, context, key);
566         log_error(stmt, SQL_HANDLE_STMT);
567         throw IOException("ODBC StorageService search failed.");
568     }
569
570     SQLSMALLINT ver;
571     SQLBindCol(stmt,1,SQL_C_SSHORT,&ver,0,NULL);
572     if ((sr=SQLFetch(stmt)) == SQL_NO_DATA) {
573         freeSafeSQL(scontext, context);
574         freeSafeSQL(skey, key);
575         return 0;
576     }
577
578     // Check version?
579     if (version > 0 && version != ver) {
580         freeSafeSQL(scontext, context);
581         freeSafeSQL(skey, key);
582         return -1;
583     }
584
585     SQLFreeHandle(SQL_HANDLE_STMT, stmt);
586     stmt = getHSTMT(conn);
587
588     // Prepare and exectute update statement.
589     q = string("UPDATE ") + table + " SET ";
590
591     if (value)
592         q = q + "value=?, version=version+1";
593
594     if (expiration) {
595         timestampFromTime(expiration, timebuf);
596         if (value)
597             q += ',';
598         q = q + "expires = " + timebuf;
599     }
600
601     q = q + " WHERE context='" + scontext + "' AND id='" + key + "'";
602     freeSafeSQL(scontext, context);
603     freeSafeSQL(skey, key);
604
605     sr = SQLPrepare(stmt, (SQLCHAR*)q.c_str(), SQL_NTS);
606     if (!SQL_SUCCEEDED(sr)) {
607         m_log.error("update of record failed (t=%s, c=%s, k=%s", table, context, key);
608         log_error(stmt, SQL_HANDLE_STMT);
609         throw IOException("ODBC StorageService failed to update record.");
610     }
611     m_log.debug("SQLPrepare() succeded. SQL: %s", q.c_str());
612
613     SQLINTEGER b_ind = SQL_NTS;
614     if (value) {
615         sr = SQLBindParam(stmt, 1, SQL_C_CHAR, (strcmp(table, TEXT_TABLE)==0 ? SQL_LONGVARCHAR : SQL_VARCHAR), 0, 0, const_cast<char*>(value), &b_ind);
616         if (!SQL_SUCCEEDED(sr)) {
617             m_log.error("SQLBindParam failed (context = %s)", context);
618             log_error(stmt, SQL_HANDLE_STMT);
619             throw IOException("ODBC StorageService failed to update record.");
620         }
621         m_log.debug("SQLBindParam succeded (context = %s)", context);
622     }
623
624     sr=SQLExecute(stmt);
625     if (sr==SQL_NO_DATA)
626         return 0;   // went missing?
627     else if (!SQL_SUCCEEDED(sr)) {
628         m_log.error("update of record failed (t=%s, c=%s, k=%s", table, context, key);
629         log_error(stmt, SQL_HANDLE_STMT);
630         throw IOException("ODBC StorageService failed to update record.");
631     }
632
633     return ver + 1;
634 }
635
636 bool ODBCStorageService::deleteRow(const char *table, const char *context, const char* key)
637 {
638 #ifdef _DEBUG
639     xmltooling::NDC ndc("deleteRow");
640 #endif
641
642     // Get statement handle.
643     ODBCConn conn(getHDBC());
644     SQLHSTMT stmt = getHSTMT(conn);
645
646     // Prepare and execute delete statement.
647     char *scontext = makeSafeSQL(context);
648     char *skey = makeSafeSQL(key);
649     string q = string("DELETE FROM ") + table + " WHERE context='" + scontext + "' AND id='" + skey + "'";
650     freeSafeSQL(scontext, context);
651     freeSafeSQL(skey, key);
652     m_log.debug("SQL: %s", q.c_str());
653
654     SQLRETURN sr=SQLExecDirect(stmt, (SQLCHAR*)q.c_str(), SQL_NTS);
655      if (sr==SQL_NO_DATA)
656         return false;
657     else if (!SQL_SUCCEEDED(sr)) {
658         m_log.error("error deleting record (t=%s, c=%s, k=%s)", table, context, key);
659         log_error(stmt, SQL_HANDLE_STMT);
660         throw IOException("ODBC StorageService failed to delete record.");
661     }
662
663     return true;
664 }
665
666
667 void ODBCStorageService::cleanup()
668 {
669 #ifdef _DEBUG
670     xmltooling::NDC ndc("cleanup");
671 #endif
672
673     Mutex* mutex = Mutex::create();
674
675     mutex->lock();
676
677     m_log.info("cleanup thread started... running every %d secs", m_cleanupInterval);
678
679     while (!shutdown) {
680         shutdown_wait->timedwait(mutex, m_cleanupInterval);
681         if (shutdown)
682             break;
683         try {
684             reap(NULL);
685         }
686         catch (exception& ex) {
687             m_log.error("cleanup thread swallowed exception: %s", ex.what());
688         }
689     }
690
691     m_log.info("cleanup thread exiting...");
692
693     mutex->unlock();
694     delete mutex;
695     Thread::exit(NULL);
696 }
697
698 void* ODBCStorageService::cleanup_fn(void* cache_p)
699 {
700   ODBCStorageService* cache = (ODBCStorageService*)cache_p;
701
702 #ifndef WIN32
703   // First, let's block all signals
704   Thread::mask_all_signals();
705 #endif
706
707   // Now run the cleanup process.
708   cache->cleanup();
709   return NULL;
710 }
711
712 void ODBCStorageService::updateContext(const char *table, const char* context, time_t expiration)
713 {
714 #ifdef _DEBUG
715     xmltooling::NDC ndc("updateContext");
716 #endif
717
718     // Get statement handle.
719     ODBCConn conn(getHDBC());
720     SQLHSTMT stmt = getHSTMT(conn);
721
722     char timebuf[32];
723     timestampFromTime(expiration, timebuf);
724
725     char nowbuf[32];
726     timestampFromTime(time(NULL), nowbuf);
727
728     char *scontext = makeSafeSQL(context);
729     string q("UPDATE ");
730     q = q + table + " SET expires = " + timebuf + " WHERE context='" + scontext + "' AND expires > " + nowbuf;
731     freeSafeSQL(scontext, context);
732
733     m_log.debug("SQL: %s", q.c_str());
734
735     SQLRETURN sr=SQLExecDirect(stmt, (SQLCHAR*)q.c_str(), SQL_NTS);
736     if ((sr!=SQL_NO_DATA) && !SQL_SUCCEEDED(sr)) {
737         m_log.error("error updating records (t=%s, c=%s)", table, context ? context : "all");
738         log_error(stmt, SQL_HANDLE_STMT);
739         throw IOException("ODBC StorageService failed to update context expiration.");
740     }
741 }
742
743 void ODBCStorageService::reap(const char *table, const char* context)
744 {
745 #ifdef _DEBUG
746     xmltooling::NDC ndc("reap");
747 #endif
748
749     // Get statement handle.
750     ODBCConn conn(getHDBC());
751     SQLHSTMT stmt = getHSTMT(conn);
752
753     // Prepare and execute delete statement.
754     char nowbuf[32];
755     timestampFromTime(time(NULL), nowbuf);
756     string q;
757     if (context) {
758         char *scontext = makeSafeSQL(context);
759         q = string("DELETE FROM ") + table + " WHERE context='" + scontext + "' AND expires <= " + nowbuf;
760         freeSafeSQL(scontext, context);
761     }
762     else {
763         q = string("DELETE FROM ") + table + " WHERE expires <= " + nowbuf;
764     }
765     m_log.debug("SQL: %s", q.c_str());
766
767     SQLRETURN sr=SQLExecDirect(stmt, (SQLCHAR*)q.c_str(), SQL_NTS);
768     if ((sr!=SQL_NO_DATA) && !SQL_SUCCEEDED(sr)) {
769         m_log.error("error expiring records (t=%s, c=%s)", table, context ? context : "all");
770         log_error(stmt, SQL_HANDLE_STMT);
771         throw IOException("ODBC StorageService failed to purge expired records.");
772     }
773 }
774
775 void ODBCStorageService::deleteContext(const char *table, const char* context)
776 {
777 #ifdef _DEBUG
778     xmltooling::NDC ndc("deleteContext");
779 #endif
780
781     // Get statement handle.
782     ODBCConn conn(getHDBC());
783     SQLHSTMT stmt = getHSTMT(conn);
784
785     // Prepare and execute delete statement.
786     char *scontext = makeSafeSQL(context);
787     string q = string("DELETE FROM ") + table + " WHERE context='" + scontext + "'";
788     freeSafeSQL(scontext, context);
789     m_log.debug("SQL: %s", q.c_str());
790
791     SQLRETURN sr=SQLExecDirect(stmt, (SQLCHAR*)q.c_str(), SQL_NTS);
792     if ((sr!=SQL_NO_DATA) && !SQL_SUCCEEDED(sr)) {
793         m_log.error("error deleting context (t=%s, c=%s)", table, context);
794         log_error(stmt, SQL_HANDLE_STMT);
795         throw IOException("ODBC StorageService failed to delete context.");
796     }
797 }
798
799 extern "C" int ODBCSTORE_EXPORTS xmltooling_extension_init(void*)
800 {
801     // Register this SS type
802     XMLToolingConfig::getConfig().StorageServiceManager.registerFactory("ODBC", ODBCStorageServiceFactory);
803     return 0;
804 }
805
806 extern "C" void ODBCSTORE_EXPORTS xmltooling_extension_term()
807 {
808     XMLToolingConfig::getConfig().StorageServiceManager.deregisterFactory("ODBC");
809 }