presage  0.9.2~beta
databaseConnector.h
Go to the documentation of this file.
1 
2 /******************************************************
3  * Presage, an extensible predictive text entry system
4  * ---------------------------------------------------
5  *
6  * Copyright (C) 2008 Matteo Vescovi <matteo.vescovi@yahoo.co.uk>
7 
8  This program is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License along
19  with this program; if not, write to the Free Software Foundation, Inc.,
20  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  *
22  **********(*)*/
23 
24 
25 #ifndef DATABASECONNECTOR_H
26 #define DATABASECONNECTOR_H
27 
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31 
32 #include "../../core/logger.h"
33 
34 #include <map>
35 #include <vector>
36 #include <string>
37 
38 typedef std::vector<std::string> Ngram;
39 typedef std::vector<Ngram> NgramTable;
40 
45 public:
46  DatabaseConnector(const std::string database_name,
47  const size_t cardinality,
48  const bool read_write);
49  DatabaseConnector(const std::string database_name,
50  const size_t cardinality,
51  const bool read_write,
52  const std::string& log_level);
53  virtual ~DatabaseConnector();
54 
57  void createNgramTable(const size_t cardinality) const;
58  void createUnigramTable() const { createNgramTable(1); }
59  void createBigramTable() const { createNgramTable(2); }
60  void createTrigramTable() const { createNgramTable(3); }
61 
64  int getUnigramCountsSum() const;
65 
68  int getNgramCount(const Ngram ngram) const;
69 
74  const char** filter,
75  const int count_threshold,
76  int limit = -1) const;
77 
83  int incrementNgramCount(const Ngram ngram) const;
84 
87  void insertNgram(const Ngram ngram, const int count) const;
88 
91  void updateNgram(const Ngram ngram, const int count) const;
92 
95  void removeNgram(const Ngram ngram) const;
96 
100  virtual void beginTransaction() const;
101 
105  virtual void endTransaction() const;
106 
110  virtual void rollbackTransaction() const;
111 
112 protected:
113  // Following functions to be overridden by derived classes.
114  virtual void openDatabase() = 0;
115  virtual void closeDatabase() = 0;
116  virtual NgramTable executeSql(const std::string query) const = 0;
117 
118  std::string get_database_filename () const;
119  std::string set_database_filename (const std::string& filename);
120 
121  void set_cardinality (const size_t cardinality);
122  size_t get_cardinality () const;
123 
124  void set_read_write_mode (const bool read_write);
125  bool get_read_write_mode () const;
126 
128 
129 private:
133  std::string buildSelectLikeClause(const int cardinality) const;
134 
137  std::string buildWhereClause(const Ngram ngram) const;
138 
144  std::string buildWhereLikeClause(const Ngram ngram,
145  const char** filter,
146  const int count_threshold) const;
147 
150  std::string buildValuesClause(const Ngram ngram, const int count) const;
151 
154  std::string sanitizeString(const std::string) const;
155 
158  int extractFirstInteger(const NgramTable&) const;
159 
160 
161  std::string expand_variables (std::string filename) const;
162  void substitute_variable_in_string (const std::string& variable_name, std::string& filepath) const;
163 
164  std::string database_filename;
165  size_t cardinality;
167 
168 };
169 
170 #endif // DATABASECONNECTOR_H
bool get_read_write_mode() const
size_t get_cardinality() const
void createBigramTable() const
std::string set_database_filename(const std::string &filename)
int getNgramCount(const Ngram ngram) const
void insertNgram(const Ngram ngram, const int count) const
std::string expand_variables(std::string filename) const
void set_read_write_mode(const bool read_write)
int getUnigramCountsSum() const
virtual void openDatabase()=0
NgramTable getNgramLikeTable(const Ngram ngram, const char **filter, const int count_threshold, int limit=-1) const
void set_cardinality(const size_t cardinality)
std::string database_filename
DatabaseConnector(const std::string database_name, const size_t cardinality, const bool read_write)
void createUnigramTable() const
void removeNgram(const Ngram ngram) const
std::string buildValuesClause(const Ngram ngram, const int count) const
int extractFirstInteger(const NgramTable &) const
virtual NgramTable executeSql(const std::string query) const =0
std::vector< Ngram > NgramTable
void createTrigramTable() const
int incrementNgramCount(const Ngram ngram) const
virtual void beginTransaction() const
void updateNgram(const Ngram ngram, const int count) const
virtual void rollbackTransaction() const
std::string buildWhereClause(const Ngram ngram) const
void createNgramTable(const size_t cardinality) const
std::vector< std::string > Ngram
std::string buildWhereLikeClause(const Ngram ngram, const char **filter, const int count_threshold) const
virtual void endTransaction() const
std::string buildSelectLikeClause(const int cardinality) const
std::string sanitizeString(const std::string) const
Logger< char > logger
void substitute_variable_in_string(const std::string &variable_name, std::string &filepath) const
std::string get_database_filename() const
Definition: ngram.h:33
virtual void closeDatabase()=0