presage
0.8.7
|
00001 00002 /****************************************************** 00003 * Presage, an extensible predictive text entry system 00004 * --------------------------------------------------- 00005 * 00006 * Copyright (C) 2008 Matteo Vescovi <matteo.vescovi@yahoo.co.uk> 00007 00008 This program is free software; you can redistribute it and/or modify 00009 it under the terms of the GNU General Public License as published by 00010 the Free Software Foundation; either version 2 of the License, or 00011 (at your option) any later version. 00012 00013 This program is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 GNU General Public License for more details. 00017 00018 You should have received a copy of the GNU General Public License along 00019 with this program; if not, write to the Free Software Foundation, Inc., 00020 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 00021 * 00022 **********(*)*/ 00023 00024 00025 #ifndef DATABASECONNECTOR_H 00026 #define DATABASECONNECTOR_H 00027 00028 #ifdef HAVE_CONFIG_H 00029 #include "config.h" 00030 #endif 00031 00032 #include "../../core/logger.h" 00033 00034 #include <map> 00035 #include <vector> 00036 #include <string> 00037 00038 typedef std::vector<std::string> Ngram; 00039 typedef std::vector<Ngram> NgramTable; 00040 00044 class DatabaseConnector { 00045 public: 00046 DatabaseConnector(); 00047 DatabaseConnector(const std::string& log_level); 00048 virtual ~DatabaseConnector(); 00049 00052 void createNgramTable(const int n) const; 00053 void createUnigramTable() const { createNgramTable(1); } 00054 void createBigramTable() const { createNgramTable(2); } 00055 void createTrigramTable() const { createNgramTable(3); } 00056 00059 int getUnigramCountsSum() const; 00060 00063 int getNgramCount(const Ngram ngram) const; 00064 00067 NgramTable getNgramLikeTable(const Ngram ngram, int limit = -1) const; 00068 00071 NgramTable getNgramLikeTableFiltered(const Ngram ngram, const char** filter, int limit = -1) const; 00072 00078 int incrementNgramCount(const Ngram ngram) const; 00079 00082 void insertNgram(const Ngram ngram, const int count) const; 00083 00086 void updateNgram(const Ngram ngram, const int count) const; 00087 00090 void removeNgram(const Ngram ngram) const; 00091 00095 virtual void beginTransaction() const; 00096 00100 virtual void endTransaction() const; 00101 00105 virtual void rollbackTransaction() const; 00106 00107 protected: 00108 // Following functions to be overridden by derived classes. 00109 virtual void openDatabase() = 0; 00110 virtual void closeDatabase() = 0; 00111 virtual NgramTable executeSql(const std::string query) const = 0; 00112 00113 Logger<char> logger; 00114 00115 private: 00119 std::string buildSelectLikeClause(const int cardinality) const; 00120 00123 std::string buildWhereClause(const Ngram ngram) const; 00124 00128 std::string buildWhereLikeClause(const Ngram ngram) const; 00129 00132 std::string buildWhereLikeClauseFiltered(const Ngram ngram,const char** filter) const; 00133 00136 std::string buildValuesClause(const Ngram ngram, const int count) const; 00137 00140 std::string sanitizeString(const std::string) const; 00141 00144 int extractFirstInteger(const NgramTable&) const; 00145 00146 }; 00147 00148 #endif // DATABASECONNECTOR_H