databaseConnector.h
Go to the documentation of this file.
/******************************************************
* Presage, an extensible predictive text entry system
* ---------------------------------------------------
*
* Copyright (C) 2008 Matteo Vescovi <matteo.vescovi@yahoo.co.uk>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
**********(*)*/
#ifndef DATABASECONNECTOR_H
#define DATABASECONNECTOR_H
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "../../core/logger.h"
#include <map>
#include <vector>
#include <string>
typedef std::vector<std::string> Ngram;
typedef std::vector<Ngram> NgramTable;
class DatabaseConnector {
public:
DatabaseConnector(const std::string database_name,
const size_t cardinality,
const bool read_write);
DatabaseConnector(const std::string database_name,
const size_t cardinality,
const bool read_write,
const std::string& log_level);
virtual ~DatabaseConnector();
void createNgramTable(const size_t cardinality) const;
void createUnigramTable() const { createNgramTable(1); }
void createBigramTable() const { createNgramTable(2); }
void createTrigramTable() const { createNgramTable(3); }
int getUnigramCountsSum() const;
int getNgramCount(const Ngram ngram) const;
NgramTable getNgramLikeTable(const Ngram ngram, int limit = -1) const;
NgramTable getNgramLikeTableFiltered(const Ngram ngram, const char** filter, int limit = -1) const;
int incrementNgramCount(const Ngram ngram) const;
void insertNgram(const Ngram ngram, const int count) const;
void updateNgram(const Ngram ngram, const int count) const;
void removeNgram(const Ngram ngram) const;
virtual void beginTransaction() const;
virtual void endTransaction() const;
virtual void rollbackTransaction() const;
protected:
// Following functions to be overridden by derived classes.
virtual void openDatabase() = 0;
virtual void closeDatabase() = 0;
virtual NgramTable executeSql(const std::string query) const = 0;
std::string get_database_filename () const;
std::string set_database_filename (const std::string& filename);
void set_cardinality (const size_t cardinality);
size_t get_cardinality () const;
void set_read_write_mode (const bool read_write);
bool get_read_write_mode () const;
Logger<char> logger;
private:
std::string buildSelectLikeClause(const int cardinality) const;
std::string buildWhereClause(const Ngram ngram) const;
std::string buildWhereLikeClause(const Ngram ngram) const;
std::string buildWhereLikeClauseFiltered(const Ngram ngram,const char** filter) const;
std::string buildValuesClause(const Ngram ngram, const int count) const;
std::string sanitizeString(const std::string) const;
int extractFirstInteger(const NgramTable&) const;
std::string expand_variables (std::string filename) const;
void substitute_variable_in_string (const std::string& variable_name, std::string& filepath) const;
std::string database_filename;
size_t cardinality;
bool read_write_mode;
};
#endif // DATABASECONNECTOR_H
DatabaseConnector::substitute_variable_in_string
void substitute_variable_in_string(const std::string &variable_name, std::string &filepath) const
Definition: databaseConnector.cpp:408
DatabaseConnector::getNgramCount
int getNgramCount(const Ngram ngram) const
Definition: databaseConnector.cpp:104
DatabaseConnector::insertNgram
void insertNgram(const Ngram ngram, const int count) const
Definition: databaseConnector.cpp:180
DatabaseConnector::set_database_filename
std::string set_database_filename(const std::string &filename)
Definition: databaseConnector.cpp:355
DatabaseConnector::get_read_write_mode
bool get_read_write_mode() const
Definition: databaseConnector.cpp:464
DatabaseConnector::read_write_mode
bool read_write_mode
Definition: databaseConnector.h:165
NgramTable
std::vector< Ngram > NgramTable
Definition: databaseConnector.h:38
DatabaseConnector::get_cardinality
size_t get_cardinality() const
Definition: databaseConnector.cpp:454
Ngram
Definition: ngram.h:32
DatabaseConnector::set_read_write_mode
void set_read_write_mode(const bool read_write)
Definition: databaseConnector.cpp:459
DatabaseConnector::getUnigramCountsSum
int getUnigramCountsSum() const
Definition: databaseConnector.cpp:87
DatabaseConnector::createBigramTable
void createBigramTable() const
Definition: databaseConnector.h:58
DatabaseConnector::expand_variables
std::string expand_variables(std::string filename) const
Definition: databaseConnector.cpp:377
DatabaseConnector::database_filename
std::string database_filename
Definition: databaseConnector.h:163
DatabaseConnector::removeNgram
void removeNgram(const Ngram ngram) const
Definition: databaseConnector.cpp:177
DatabaseConnector::buildWhereLikeClauseFiltered
std::string buildWhereLikeClauseFiltered(const Ngram ngram, const char **filter) const
Definition: databaseConnector.cpp:234
DatabaseConnector::buildValuesClause
std::string buildValuesClause(const Ngram ngram, const int count) const
Definition: databaseConnector.cpp:280
DatabaseConnector::getNgramLikeTable
NgramTable getNgramLikeTable(const Ngram ngram, int limit=-1) const
Definition: databaseConnector.cpp:124
DatabaseConnector::extractFirstInteger
int extractFirstInteger(const NgramTable &) const
Definition: databaseConnector.cpp:308
DatabaseConnector::set_cardinality
void set_cardinality(const size_t cardinality)
Definition: databaseConnector.cpp:449
DatabaseConnector::cardinality
size_t cardinality
Definition: databaseConnector.h:164
DatabaseConnector::createTrigramTable
void createTrigramTable() const
Definition: databaseConnector.h:59
DatabaseConnector::createUnigramTable
void createUnigramTable() const
Definition: databaseConnector.h:57
DatabaseConnector::openDatabase
virtual void openDatabase()=0
DatabaseConnector::incrementNgramCount
int incrementNgramCount(const Ngram ngram) const
Definition: databaseConnector.cpp:156
DatabaseConnector::DatabaseConnector
DatabaseConnector(const std::string database_name, const size_t cardinality, const bool read_write)
Definition: databaseConnector.cpp:34
Ngram
std::vector< std::string > Ngram
Definition: databaseConnector.h:37
DatabaseConnector
Definition: databaseConnector.h:43
DatabaseConnector::endTransaction
virtual void endTransaction() const
Definition: databaseConnector.cpp:340
DatabaseConnector::executeSql
virtual NgramTable executeSql(const std::string query) const =0
DatabaseConnector::logger
Logger< char > logger
Definition: databaseConnector.h:126
DatabaseConnector::beginTransaction
virtual void beginTransaction() const
Definition: databaseConnector.cpp:335
DatabaseConnector::updateNgram
void updateNgram(const Ngram ngram, const int count) const
Definition: databaseConnector.cpp:191
DatabaseConnector::~DatabaseConnector
virtual ~DatabaseConnector()
Definition: databaseConnector.cpp:55
DatabaseConnector::createNgramTable
void createNgramTable(const size_t cardinality) const
Definition: databaseConnector.cpp:58
Logger< char >
DatabaseConnector::buildSelectLikeClause
std::string buildSelectLikeClause(const int cardinality) const
Definition: databaseConnector.cpp:264
DatabaseConnector::closeDatabase
virtual void closeDatabase()=0
DatabaseConnector::rollbackTransaction
virtual void rollbackTransaction() const
Definition: databaseConnector.cpp:345
DatabaseConnector::buildWhereClause
std::string buildWhereClause(const Ngram ngram) const
Definition: databaseConnector.cpp:202
DatabaseConnector::buildWhereLikeClause
std::string buildWhereLikeClause(const Ngram ngram) const
Definition: databaseConnector.cpp:219
DatabaseConnector::getNgramLikeTableFiltered
NgramTable getNgramLikeTableFiltered(const Ngram ngram, const char **filter, int limit=-1) const
Definition: databaseConnector.cpp:140
DatabaseConnector::sanitizeString
std::string sanitizeString(const std::string) const
Definition: databaseConnector.cpp:294
DatabaseConnector::get_database_filename
std::string get_database_filename() const
Definition: databaseConnector.cpp:350