27 #include "../../core/utility.h" 35 const size_t cardinality,
36 const bool read_write)
37 : logger(
"DatabaseConnector", std::cerr)
45 const size_t cardinality,
46 const bool read_write,
47 const std::string& log_level)
48 : logger(
"DatabaseConnector", std::cerr, log_level)
61 std::stringstream query;
62 std::stringstream unique;
63 query <<
"CREATE TABLE";
67 query <<
" IF NOT EXISTS";
69 query <<
" _" << n <<
"_gram (";
70 for (
int i = n - 1; i >= 0; i--) {
72 unique <<
"word_" << i <<
", ";
73 query <<
"word_" << i <<
" TEXT, ";
76 query <<
"word TEXT, count INTEGER, UNIQUE(" << unique.str() <<
") );";
89 std::string query =
"SELECT SUM(count) FROM _1_gram;";
93 logger << DEBUG <<
"NgramTable:";
94 for (
size_t i = 0; i < result.size(); i++) {
95 for (
size_t j = 0; j < result[i].size(); j++) {
96 logger << DEBUG << result[i][j] <<
'\t';
106 std::stringstream query;
107 query <<
"SELECT count " 108 <<
"FROM _" << ngram.size() <<
"_gram" 113 logger << DEBUG <<
"NgramTable:";
114 for (
size_t i = 0; i < result.size(); i++) {
115 for (
size_t j = 0; j < result[i].size(); j++) {
116 logger << DEBUG << result[i][j] <<
'\t';
126 std::stringstream query;
128 <<
"FROM _" << ngram.size() <<
"_gram" 130 <<
" ORDER BY count DESC";
134 query <<
" LIMIT " << limit <<
';';
148 logger << DEBUG <<
"Updated ngram to " << count <<
endl;
166 std::stringstream query;
168 query <<
"INSERT INTO _" << ngram.size() <<
"_gram " 177 std::stringstream query;
179 query <<
"UPDATE _" << ngram.size() <<
"_gram " 180 <<
"SET count = " << count
188 std::stringstream where_clause;
189 where_clause <<
" WHERE";
190 for (
size_t i = 0; i < ngram.size(); i++) {
191 if (i < ngram.size() - 1) {
192 where_clause <<
" word_" << ngram.size() - i - 1 <<
" = '" 195 where_clause <<
" word = '" <<
sanitizeString(ngram[ngram.size() - 1]) <<
"'";
198 return where_clause.str();
203 const int count_threshold)
const 205 std::stringstream where_clause;
206 where_clause <<
" WHERE";
207 for (
size_t i = 0; i < ngram.size(); i++) {
208 if (i < ngram.size() - 1) {
209 where_clause <<
" word_" << ngram.size() - i - 1 <<
" = '" 213 where_clause <<
" word LIKE '" <<
sanitizeString(ngram[ngram.size() - 1]) <<
"%'";
215 std::string true_prefix =
sanitizeString(ngram[ngram.size() - 1]);
216 where_clause <<
" (";
217 for (
int j = 0; filter[j] != 0; j++) {
219 where_clause <<
" OR ";
221 where_clause <<
" word LIKE '" << true_prefix << filter[j] <<
"%'";
225 if (count_threshold > 0) {
226 where_clause <<
" AND count >= " << count_threshold;
230 return where_clause.str();
238 std::stringstream result;
241 result <<
"word_" << i <<
", ";
243 result <<
"word, count";
252 std::stringstream values_clause;
253 values_clause <<
"VALUES(";
254 for (
size_t i = 0; i < ngram.size(); i++) {
255 if (i < ngram.size() - 1) {
258 values_clause <<
"'" <<
sanitizeString(ngram[i]) <<
"', " << count <<
")";
261 return values_clause.str();
284 if (table.size() > 0) {
285 if (table[0].size() > 0) {
286 count = atoi(table[0][0].c_str());
290 logger << DEBUG <<
"table: ";
291 for (
size_t i = 0; i < table.size(); i++) {
292 for (
size_t j = 0; j < table[i].size(); j++) {
293 logger << DEBUG << table[i][j] <<
'\t';
298 return (count > 0 ? count : 0);
340 return prev_filename;
348 const std::string start_marker =
"${";
349 const std::string end_marker =
"}";
351 std::list<std::string> variables;
353 std::string::size_type pos_start = filepath.find (start_marker);
354 while (pos_start != std::string::npos)
356 std::string::size_type pos_end = filepath.find (end_marker, pos_start);
357 if (pos_end != std::string::npos) {
358 variables.push_back (filepath.substr(pos_start + start_marker.size(), pos_end - end_marker.size() - pos_start - 1));
361 pos_start = filepath.find (start_marker, pos_end);
364 for (std::list<std::string>::const_iterator it = variables.begin();
365 it != variables.end();
376 std::string variable_token =
"${" + variable_name +
"}";
378 for (std::string::size_type pos = filepath.find (variable_token);
379 pos != std::string::npos;
380 pos = filepath.find (variable_token, pos))
382 const char* value = getenv(variable_name.c_str());
385 filepath.replace (pos,
386 variable_token.size(),
392 if (variable_name ==
"HOME")
394 value = getenv(
"USERPROFILE");
397 filepath.replace (pos,
398 variable_token.size(),
407 filepath.replace (pos,
408 variable_token.size(),
bool get_read_write_mode() const
size_t get_cardinality() 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
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 removeNgram(const Ngram ngram) const
std::string buildValuesClause(const Ngram ngram, const int count) const
int extractFirstInteger(const NgramTable &) const
static void create_directory(const std::string &dir)
virtual NgramTable executeSql(const std::string query) const =0
std::vector< Ngram > NgramTable
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
virtual ~DatabaseConnector()
void createNgramTable(const size_t cardinality) const
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
void substitute_variable_in_string(const std::string &variable_name, std::string &filepath) const
static std::string dirname(const std::string &)
std::string get_database_filename() const
static bool is_directory_usable(const std::string &dir)
const Logger< _charT, _Traits > & endl(const Logger< _charT, _Traits > &lgr)