presage  0.9.2~beta
Public Member Functions | Protected Member Functions | Protected Attributes | Private Member Functions | Private Attributes | List of all members
DatabaseConnector Class Referenceabstract

#include <databaseConnector.h>

Inheritance diagram for DatabaseConnector:
Inheritance graph
Collaboration diagram for DatabaseConnector:
Collaboration graph

Public Member Functions

 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
 
void createBigramTable () const
 
void createTrigramTable () const
 
int getUnigramCountsSum () const
 
int getNgramCount (const Ngram ngram) const
 
NgramTable getNgramLikeTable (const Ngram ngram, const char **filter, const int count_threshold, 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 Member Functions

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
 

Protected Attributes

Logger< char > logger
 

Private Member Functions

std::string buildSelectLikeClause (const int cardinality) const
 
std::string buildWhereClause (const Ngram ngram) const
 
std::string buildWhereLikeClause (const Ngram ngram, const char **filter, const int count_threshold) 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
 

Private Attributes

std::string database_filename
 
size_t cardinality
 
bool read_write_mode
 

Detailed Description

Provides the interface to database creation, updating and querying operations.

Definition at line 44 of file databaseConnector.h.

Constructor & Destructor Documentation

◆ DatabaseConnector() [1/2]

DatabaseConnector::DatabaseConnector ( const std::string  database_name,
const size_t  cardinality,
const bool  read_write 
)

Definition at line 34 of file databaseConnector.cpp.

References cardinality, set_cardinality(), set_database_filename(), and set_read_write_mode().

Here is the call graph for this function:

◆ DatabaseConnector() [2/2]

DatabaseConnector::DatabaseConnector ( const std::string  database_name,
const size_t  cardinality,
const bool  read_write,
const std::string &  log_level 
)

Definition at line 44 of file databaseConnector.cpp.

References cardinality, set_cardinality(), set_database_filename(), and set_read_write_mode().

Here is the call graph for this function:

◆ ~DatabaseConnector()

DatabaseConnector::~DatabaseConnector ( )
virtual

Definition at line 55 of file databaseConnector.cpp.

Member Function Documentation

◆ beginTransaction()

void DatabaseConnector::beginTransaction ( ) const
virtual

Marks the beginning of an SQL transaction.

Definition at line 301 of file databaseConnector.cpp.

References executeSql().

Referenced by SmoothedNgramPredictor::learn(), main(), and SmoothedNgramPredictor::predict().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ buildSelectLikeClause()

std::string DatabaseConnector::buildSelectLikeClause ( const int  cardinality) const
private

Returns a string containing the column specifiers for the SQL SELECT clause built for an ngram of specified cardinality.

Definition at line 234 of file databaseConnector.cpp.

References cardinality.

Referenced by getNgramLikeTable().

Here is the caller graph for this function:

◆ buildValuesClause()

std::string DatabaseConnector::buildValuesClause ( const Ngram  ngram,
const int  count 
) const
private

Returns a string containing an SQL VALUES clause built for the ngram.

Definition at line 250 of file databaseConnector.cpp.

References sanitizeString().

Referenced by insertNgram().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ buildWhereClause()

std::string DatabaseConnector::buildWhereClause ( const Ngram  ngram) const
private

Returns a string containing an SQL WHERE clause built for the ngram.

Definition at line 186 of file databaseConnector.cpp.

References sanitizeString().

Referenced by getNgramCount(), and updateNgram().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ buildWhereLikeClause()

std::string DatabaseConnector::buildWhereLikeClause ( const Ngram  ngram,
const char **  filter,
const int  count_threshold 
) const
private

Returns a string containing an SQL WHERE clause built for the ngram, where the last comparison is a LIKE clauses instead of = clause and also possibly contains a filter on the last word and a count threshold.

Definition at line 201 of file databaseConnector.cpp.

References sanitizeString().

Referenced by getNgramLikeTable().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ closeDatabase()

virtual void DatabaseConnector::closeDatabase ( )
protectedpure virtual

Implemented in SqliteDatabaseConnector.

◆ createBigramTable()

void DatabaseConnector::createBigramTable ( ) const
inline

Definition at line 59 of file databaseConnector.h.

References createNgramTable().

Here is the call graph for this function:

◆ createNgramTable()

void DatabaseConnector::createNgramTable ( const size_t  cardinality) const

Creates a table to store N-grams.

Definition at line 58 of file databaseConnector.cpp.

References executeSql().

Referenced by createBigramTable(), createTrigramTable(), createUnigramTable(), main(), and SqliteDatabaseConnector::openDatabase().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ createTrigramTable()

void DatabaseConnector::createTrigramTable ( ) const
inline

Definition at line 60 of file databaseConnector.h.

References createNgramTable().

Here is the call graph for this function:

◆ createUnigramTable()

void DatabaseConnector::createUnigramTable ( ) const
inline

Definition at line 58 of file databaseConnector.h.

References createNgramTable().

Here is the call graph for this function:

◆ endTransaction()

void DatabaseConnector::endTransaction ( ) const
virtual

Marks the end of an SQL transaction.

Definition at line 306 of file databaseConnector.cpp.

References executeSql().

Referenced by SmoothedNgramPredictor::learn(), main(), and SmoothedNgramPredictor::predict().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ executeSql()

virtual NgramTable DatabaseConnector::executeSql ( const std::string  query) const
protectedpure virtual

◆ expand_variables()

std::string DatabaseConnector::expand_variables ( std::string  filename) const
private

Definition at line 343 of file databaseConnector.cpp.

References substitute_variable_in_string().

Referenced by set_database_filename().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ extractFirstInteger()

int DatabaseConnector::extractFirstInteger ( const NgramTable table) const
private

Returns the first element of the ngramtable as an integer.

Definition at line 274 of file databaseConnector.cpp.

References endl(), and logger.

Referenced by getNgramCount(), and getUnigramCountsSum().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_cardinality()

size_t DatabaseConnector::get_cardinality ( ) const
protected

Definition at line 420 of file databaseConnector.cpp.

References cardinality.

Referenced by SqliteDatabaseConnector::openDatabase().

Here is the caller graph for this function:

◆ get_database_filename()

std::string DatabaseConnector::get_database_filename ( ) const
protected

Definition at line 316 of file databaseConnector.cpp.

References database_filename.

Referenced by SqliteDatabaseConnector::executeSql(), and SqliteDatabaseConnector::openDatabase().

Here is the caller graph for this function:

◆ get_read_write_mode()

bool DatabaseConnector::get_read_write_mode ( ) const
protected

Definition at line 430 of file databaseConnector.cpp.

References read_write_mode.

Referenced by SqliteDatabaseConnector::openDatabase().

Here is the caller graph for this function:

◆ getNgramCount()

int DatabaseConnector::getNgramCount ( const Ngram  ngram) const

Returns an integer equal to the specified ngram count.

Definition at line 104 of file databaseConnector.cpp.

References buildWhereClause(), endl(), executeSql(), extractFirstInteger(), and logger.

Referenced by SmoothedNgramPredictor::count(), incrementNgramCount(), SmoothedNgramPredictor::learn(), and main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getNgramLikeTable()

NgramTable DatabaseConnector::getNgramLikeTable ( const Ngram  ngram,
const char **  filter,
const int  count_threshold,
int  limit = -1 
) const

Returns a table of ngrams matching the specified ngram-like query, satisfying the given filter and count threshold.

Definition at line 124 of file databaseConnector.cpp.

References buildSelectLikeClause(), buildWhereLikeClause(), and executeSql().

Referenced by SmoothedNgramPredictor::predict().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getUnigramCountsSum()

int DatabaseConnector::getUnigramCountsSum ( ) const

Returns an integer equal to the sum of the counts of all unigrams.

Definition at line 87 of file databaseConnector.cpp.

References endl(), executeSql(), extractFirstInteger(), and logger.

Referenced by SmoothedNgramPredictor::count(), and SmoothedNgramPredictor::predict().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ incrementNgramCount()

int DatabaseConnector::incrementNgramCount ( const Ngram  ngram) const

Increments the specified ngram count and returns the updated count.

If the ngram does not yet exit in the database, it is created and its count is set to one.

Definition at line 140 of file databaseConnector.cpp.

References endl(), getNgramCount(), insertNgram(), logger, and updateNgram().

Referenced by SmoothedNgramPredictor::check_learn_consistency().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ insertNgram()

void DatabaseConnector::insertNgram ( const Ngram  ngram,
const int  count 
) const

Insert ngram into database and sets its count.

Definition at line 164 of file databaseConnector.cpp.

References buildValuesClause(), and executeSql().

Referenced by incrementNgramCount(), SmoothedNgramPredictor::learn(), and main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ openDatabase()

virtual void DatabaseConnector::openDatabase ( )
protectedpure virtual

Implemented in SqliteDatabaseConnector.

◆ removeNgram()

void DatabaseConnector::removeNgram ( const Ngram  ngram) const

Removes the ngram from the database

Definition at line 161 of file databaseConnector.cpp.

◆ rollbackTransaction()

void DatabaseConnector::rollbackTransaction ( ) const
virtual

Rolls back an SQL transaction.

Definition at line 311 of file databaseConnector.cpp.

References executeSql().

Referenced by SmoothedNgramPredictor::learn().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sanitizeString()

std::string DatabaseConnector::sanitizeString ( const std::string  str) const
private

Sanitizes ngram, guards against SQL code injection.

Definition at line 264 of file databaseConnector.cpp.

Referenced by buildValuesClause(), buildWhereClause(), and buildWhereLikeClause().

Here is the caller graph for this function:

◆ set_cardinality()

void DatabaseConnector::set_cardinality ( const size_t  cardinality)
protected

Definition at line 415 of file databaseConnector.cpp.

References cardinality.

Referenced by DatabaseConnector().

Here is the caller graph for this function:

◆ set_database_filename()

std::string DatabaseConnector::set_database_filename ( const std::string &  filename)
protected

Definition at line 321 of file databaseConnector.cpp.

References Utility::create_directory(), database_filename, Utility::dirname(), expand_variables(), and Utility::is_directory_usable().

Referenced by DatabaseConnector().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ set_read_write_mode()

void DatabaseConnector::set_read_write_mode ( const bool  read_write)
protected

Definition at line 425 of file databaseConnector.cpp.

References read_write_mode.

Referenced by DatabaseConnector().

Here is the caller graph for this function:

◆ substitute_variable_in_string()

void DatabaseConnector::substitute_variable_in_string ( const std::string &  variable_name,
std::string &  filepath 
) const
private

Definition at line 374 of file databaseConnector.cpp.

Referenced by expand_variables().

Here is the caller graph for this function:

◆ updateNgram()

void DatabaseConnector::updateNgram ( const Ngram  ngram,
const int  count 
) const

Updates ngram count.

Definition at line 175 of file databaseConnector.cpp.

References buildWhereClause(), and executeSql().

Referenced by incrementNgramCount(), SmoothedNgramPredictor::learn(), and main().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ cardinality

size_t DatabaseConnector::cardinality
private

◆ database_filename

std::string DatabaseConnector::database_filename
private

Definition at line 164 of file databaseConnector.h.

Referenced by get_database_filename(), and set_database_filename().

◆ logger

Logger<char> DatabaseConnector::logger
protected

◆ read_write_mode

bool DatabaseConnector::read_write_mode
private

Definition at line 166 of file databaseConnector.h.

Referenced by get_read_write_mode(), and set_read_write_mode().


The documentation for this class was generated from the following files: