wldb.h

00001 /***************************************************************************
00002  *   Copyright (C) 2004 by Michael Moritz                                  *
00003  *   mimo@restoel.net                                                      *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 #ifndef __WLDB_H
00021 #define __WLDB_H
00022 
00026 #include <exception>
00027 #include <string>
00028 #include <syslog.h>
00029 #include "triplet.h"
00030 #include "db.h"
00031 #include "wlmodule.h"
00032 #include "dbdefs.h"
00033 #include "pmatcher.h"
00034 
00035 class WLDB
00036 {
00037 public:
00038         WLDB(class DB& db, 
00039                 bool initmode,
00040                 const WLStaticDef& staticDef) throw(std::exception)
00041         : _db(db),
00042           _staticDef(staticDef)
00043         {
00044                 if(initmode) {
00045                         _db.createTable(_staticDef);
00046                 }
00047         }
00048         bool check(const class Triplet& triplet) throw(std::exception) {
00049                 switch(_staticDef._compareMode) {
00050                         case WlModule::CM_exactMatch:
00051                                 return _db.exactMatch( "wl",_staticDef,triplet );
00052                         case WlModule::CM_patternMatch:
00053                                 return patternMatch( "wl", triplet );
00054                         default:
00055                                 return false;
00056                 }
00057         }       
00058         ~WLDB() {};
00059 protected:
00061         bool patternMatch(const std::string logprefix, const Triplet& triplet)
00062         {
00063                 dbi_result result = _db.executeSQL(_staticDef._searchTable);
00064                 if(!result) //table does not exist, error has been logged
00065                         return false;
00066                 // this is required in older versions of libdbi -- otherwise no fieldnames
00067                 unsigned numrows = dbi_result_get_numrows(result);
00068                 if(numrows == 0) {
00069                         dbi_result_free(result);
00070                         return false;
00071                 }
00072                 dbi_result_next_row(result);
00073                 unsigned numfields = dbi_result_get_numfields(result);
00074                 //cout << "numfields: " << numfields << endl;
00075                 unsigned commentIdx = numfields;
00076                 unsigned primaryIdx = numfields;
00077                 for(unsigned i=0;i < numfields;i++) {
00078                         const char* colname = dbi_result_get_field_name(result,i+1);
00079                         //cout << i << ": " << colname << endl;
00080                         if(strcmp(colname,_staticDef._primaryKey) == 0)
00081                                 primaryIdx = i;
00082                         else 
00083                                 if(strcmp(colname,_staticDef._commentField) == 0)
00084                                         commentIdx = i;
00085 //                      else
00086 //                              _heading.insert(Heading::value_type(colname,i));
00087                 }
00088                 if((primaryIdx >= numfields) || (commentIdx >= numfields)) { 
00089                         //this means the table layout has changed--check dbdefs.cpp
00090                         syslog(LOG_WARNING,"WlDb no primary/comment idx in table %s",_staticDef._tableName);
00091                         dbi_result_free(result);
00092                         return false;
00093                 }
00094                 PatternMatcher pmatcher(triplet);
00095                 for(unsigned i=0;i < numrows;i++) {
00096                         const char *pattern = dbi_result_get_string_idx(result,primaryIdx+1);
00097 //                      std::cout << "pattern: " << pattern << std::endl;
00098                         if(pmatcher.match(pattern)) {
00099                                 const char *comment = dbi_result_get_string_idx(result,commentIdx+1);
00100                                 syslog(LOG_INFO,"%s %s: %s -> %s, %s: %s",
00101                                         logprefix.c_str(),_staticDef._tableName,triplet.getSender().c_str(),
00102                                         triplet.getRecipient().c_str(),triplet.getClientAddress().c_str(),comment);
00103                                 dbi_result_free(result);
00104                                 return true;
00105                         }
00106                         if(dbi_result_next_row(result) == 0)
00107                                 break;
00108                 }
00109                 dbi_result_free(result);
00110                 return false;   
00111         }
00112 private:
00113         class DB& _db;  
00114         const WLStaticDef& _staticDef;
00115 };
00116 
00117 #endif

Generated on Tue Jul 24 16:36:54 2007 for gps by  doxygen 1.5.1