presage
0.9.2~beta
|
#include <databaseConnector.h>
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 |
Provides the interface to database creation, updating and querying operations.
Definition at line 44 of file databaseConnector.h.
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().
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().
|
virtual |
Definition at line 55 of file databaseConnector.cpp.
|
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().
|
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().
|
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().
|
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().
|
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().
|
protectedpure virtual |
Implemented in SqliteDatabaseConnector.
|
inline |
Definition at line 59 of file databaseConnector.h.
References 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().
|
inline |
Definition at line 60 of file databaseConnector.h.
References createNgramTable().
|
inline |
Definition at line 58 of file databaseConnector.h.
References createNgramTable().
|
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().
|
protectedpure virtual |
Implemented in SqliteDatabaseConnector.
Referenced by beginTransaction(), createNgramTable(), endTransaction(), getNgramCount(), getNgramLikeTable(), getUnigramCountsSum(), insertNgram(), rollbackTransaction(), and updateNgram().
|
private |
Definition at line 343 of file databaseConnector.cpp.
References substitute_variable_in_string().
Referenced by set_database_filename().
|
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().
|
protected |
Definition at line 420 of file databaseConnector.cpp.
References cardinality.
Referenced by SqliteDatabaseConnector::openDatabase().
|
protected |
Definition at line 316 of file databaseConnector.cpp.
References database_filename.
Referenced by SqliteDatabaseConnector::executeSql(), and SqliteDatabaseConnector::openDatabase().
|
protected |
Definition at line 430 of file databaseConnector.cpp.
References read_write_mode.
Referenced by SqliteDatabaseConnector::openDatabase().
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().
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().
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().
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().
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().
|
protectedpure virtual |
Implemented in SqliteDatabaseConnector.
void DatabaseConnector::removeNgram | ( | const Ngram | ngram | ) | const |
Removes the ngram from the database
Definition at line 161 of file databaseConnector.cpp.
|
virtual |
Rolls back an SQL transaction.
Definition at line 311 of file databaseConnector.cpp.
References executeSql().
Referenced by SmoothedNgramPredictor::learn().
|
private |
Sanitizes ngram, guards against SQL code injection.
Definition at line 264 of file databaseConnector.cpp.
Referenced by buildValuesClause(), buildWhereClause(), and buildWhereLikeClause().
|
protected |
Definition at line 415 of file databaseConnector.cpp.
References cardinality.
Referenced by DatabaseConnector().
|
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().
|
protected |
Definition at line 425 of file databaseConnector.cpp.
References read_write_mode.
Referenced by DatabaseConnector().
|
private |
Definition at line 374 of file databaseConnector.cpp.
Referenced by expand_variables().
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().
|
private |
Definition at line 165 of file databaseConnector.h.
Referenced by buildSelectLikeClause(), DatabaseConnector(), get_cardinality(), SqliteDatabaseConnector::openDatabase(), and set_cardinality().
|
private |
Definition at line 164 of file databaseConnector.h.
Referenced by get_database_filename(), and set_database_filename().
|
protected |
Definition at line 127 of file databaseConnector.h.
Referenced by SqliteDatabaseConnector::executeSql(), extractFirstInteger(), getNgramCount(), getUnigramCountsSum(), incrementNgramCount(), and SqliteDatabaseConnector::openDatabase().
|
private |
Definition at line 166 of file databaseConnector.h.
Referenced by get_read_write_mode(), and set_read_write_mode().