DatabaseConnector Class Referenceabstract

`#include <databaseConnector.h>`

Inheritance diagram for DatabaseConnector:
Collaboration diagram for DatabaseConnector:

## 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, 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 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
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

## 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 43 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 335 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 264 of file databaseConnector.cpp.

References cardinality.

Referenced by getNgramLikeTable(), and getNgramLikeTableFiltered().

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 280 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 202 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

private

Returns a string containing an SQL WHERE clause built for the ngram, where the last comparison is a LIKE clauses instead of = clause.

Definition at line 219 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:

## buildWhereLikeClauseFiltered()

std::string DatabaseConnector::buildWhereLikeClauseFiltered | ( | const Ngram | ngram,
|  | const char ** | filter
| ) |  | const

private

Same as buildWhereLikeClause but considers also a filter on the subsequent letter

Definition at line 234 of file databaseConnector.cpp.

References sanitizeString().

Referenced by getNgramLikeTableFiltered().

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

## closeDatabase()

virtual void DatabaseConnector::closeDatabase | ( |  | ) |

protected pure virtual

Implemented in SqliteDatabaseConnector.

## createBigramTable()

void DatabaseConnector::createBigramTable | ( |  | ) | const

inline

Definition at line 58 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(), 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 59 of file databaseConnector.h.

References createNgramTable().

Here is the call graph for this function:

## createUnigramTable()

void DatabaseConnector::createUnigramTable | ( |  | ) | const

inline

Definition at line 57 of file databaseConnector.h.

## endTransaction()

void DatabaseConnector::endTransaction | ( |  | ) | const

virtual

Marks the end of an SQL transaction.

Definition at line 340 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

protected pure virtual

Implemented in SqliteDatabaseConnector.

Referenced by beginTransaction(), createNgramTable(), endTransaction(), getNgramCount(), getNgramLikeTable(), getNgramLikeTableFiltered(), getUnigramCountsSum(), insertNgram(), rollbackTransaction(), and updateNgram().

Here is the caller graph for this function:

## expand_variables()

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

private

Definition at line 377 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 308 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 454 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 350 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 464 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,
|  | int | limit = `-1`
| ) |  | const

Returns a table of ngrams matching the specified ngram-like query.

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:

## getNgramLikeTableFiltered()

NgramTable DatabaseConnector::getNgramLikeTableFiltered | ( | const Ngram | ngram,
|  | const char ** | filter,
|  | int | limit = `-1`
| ) |  | const

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

Definition at line 140 of file databaseConnector.cpp.

References buildSelectLikeClause(), buildWhereLikeClauseFiltered(), 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 156 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 180 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 | ( |  | ) |

protected pure virtual

Implemented in SqliteDatabaseConnector.

## removeNgram()

void DatabaseConnector::removeNgram | ( | const Ngram | ngram | ) | const

Removes the ngram from the database

Definition at line 177 of file databaseConnector.cpp.

## rollbackTransaction()

void DatabaseConnector::rollbackTransaction | ( |  | ) | const

virtual

Rolls back an SQL transaction.

Definition at line 345 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 294 of file databaseConnector.cpp.

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

Here is the caller graph for this function:

## set_cardinality()

void DatabaseConnector::set_cardinality | ( | const size_t | cardinality | ) |

protected

Definition at line 449 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 355 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 459 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 408 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 191 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

Definition at line 164 of file databaseConnector.h.

Referenced by buildSelectLikeClause(), DatabaseConnector(), get_cardinality(), SqliteDatabaseConnector::openDatabase(), and set_cardinality().

## database_filename

std::string DatabaseConnector::database_filename

private

Definition at line 163 of file databaseConnector.h.

Referenced by get_database_filename(), and set_database_filename().

## logger

Logger<char> DatabaseConnector::logger

protected

Definition at line 126 of file databaseConnector.h.

Referenced by SqliteDatabaseConnector::executeSql(), extractFirstInteger(), getNgramCount(), getUnigramCountsSum(), incrementNgramCount(), and SqliteDatabaseConnector::openDatabase().

## read_write_mode

bool DatabaseConnector::read_write_mode

private

Definition at line 165 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:

- /build/presage-hvZ4Ld/presage-0.9.1/src/lib/predictors/dbconnector/databaseConnector.h
- /build/presage-hvZ4Ld/presage-0.9.1/src/lib/predictors/dbconnector/databaseConnector.cpp