bool ReportingTable::max(const char* paramName, unsigned newVal) { char cmd[200]; sprintf(cmd,"UPDATE REPORTING SET VALUE=MAX(VALUE,%u), UPDATETIME=%ld WHERE NAME=\"%s\"", newVal, time(NULL), paramName); if (!sqlite3_command(mDB,cmd)) { gLogEarly(LOG_CRIT|mFacility, "cannot maximize reporting parameter %s, error message: %s", paramName, sqlite3_errmsg(mDB)); return false; } return true; }
sqlite3* SMSCBConnectDatabase(const char* path, sqlite3 **DB) { int rc = sqlite3_open(path,DB); if (rc) { LOG(EMERG) << "Cannot open SMSCB database on path " << path << ": " << sqlite3_errmsg(*DB); sqlite3_close(*DB); *DB = NULL; return NULL; } if (!sqlite3_command(*DB,createSMSCBTable)) { LOG(EMERG) << "Cannot create SMSCB table"; return NULL; } // Set high-concurrency WAL mode. if (!sqlite3_command(*DB,enableWAL)) { LOG(EMERG) << "Cannot enable WAL mode on database at " << path << ", error message: " << sqlite3_errmsg(*DB); } return *DB; }
bool ReportingTable::clear() { char cmd[200]; sprintf(cmd,"UPDATE REPORTING SET VALUE=0, UPDATETIME=0, CLEAREDTIME=%ld", time(NULL)); if (!sqlite3_command(mDB,cmd)) { gLogEarly(LOG_CRIT|mFacility, "cannot clear reporting table, error message: %s", sqlite3_errmsg(mDB)); return false; } return true; }
bool ReportingTable::clear(const char* paramName) { char cmd[200]; sprintf(cmd,"UPDATE REPORTING SET VALUE=0, UPDATETIME=0, CLEAREDTIME=%ld WHERE NAME=\"%s\"", time(NULL), paramName); if (!sqlite3_command(mDB,cmd)) { gLogEarly(LOG_CRIT|mFacility, "cannot clear reporting parameter %s, error message: %s", paramName, sqlite3_errmsg(mDB)); return false; } return true; }
ReportingTable::ReportingTable(const char* filename) { gLogEarly(LOG_INFO | mFacility, "opening reporting table from path %s", filename); // Connect to the database. int rc = sqlite3_open(filename,&mDB); if (rc) { gLogEarly(LOG_EMERG | mFacility, "cannot open reporting database at %s, error message: %s", filename, sqlite3_errmsg(mDB)); sqlite3_close(mDB); mDB = NULL; return; } // Create the table, if needed. if (!sqlite3_command(mDB,createReportingTable)) { gLogEarly(LOG_EMERG | mFacility, "cannot create reporting table in database at %s, error message: %s", filename, sqlite3_errmsg(mDB)); } // Set high-concurrency WAL mode. if (!sqlite3_command(mDB,enableWAL)) { gLogEarly(LOG_EMERG | mFacility, "Cannot enable WAL mode on database at %s, error message: %s", filename, sqlite3_errmsg(mDB)); } // Start the commit thread mBatchCommitter.start((void*(*)(void*))reportingBatchCommitter,NULL); }
bool ReportingTable::create(const char* paramName) { // add this report name to the batch map mLock.lock(); if (mBatch.find(paramName) == mBatch.end()) { mBatch[paramName] = 0; } mLock.unlock(); // and to the database char cmd[200]; sprintf(cmd,"INSERT OR IGNORE INTO REPORTING (NAME,CLEAREDTIME) VALUES (\"%s\",%ld)", paramName, time(NULL)); if (!sqlite3_command(mDB,cmd)) { gLogEarly(LOG_CRIT|mFacility, "cannot create reporting parameter %s, error message: %s", paramName, sqlite3_errmsg(mDB)); return false; } return true; }
bool ReportingTable::commit() { ReportBatch oustanding; ReportBatch::iterator mp; unsigned oustandingCount = 0; // copy out to free up access to mBatch as quickly as possible mLock.lock(); mp = mBatch.begin(); while (mp != mBatch.end()) { if (mp->second > 0) { oustanding[mp->first] = mp->second; mBatch[mp->first] = 0; oustandingCount++; } mp++; } mLock.unlock(); // now actually write them into the db if needed if (oustandingCount > 0) { Timeval timer; char cmd[200]; // TODO : could wrap this in a BEGIN; COMMIT; pair to transactionize these X UPDATEs mp = oustanding.begin(); while (mp != oustanding.end()) { sprintf(cmd,"UPDATE REPORTING SET VALUE=VALUE+%u, UPDATETIME=%ld WHERE NAME=\"%s\"", mp->second, time(NULL), mp->first.c_str()); if (!sqlite3_command(mDB,cmd)) { LOG(CRIT) << "could not increment reporting parameter " << mp->first << ", error message: " << sqlite3_errmsg(mDB); } mp++; } LOG(INFO) << "wrote " << oustandingCount << " entries in " << timer.elapsed() << "ms"; } return true; }
void sqlite3_connection::executenonquery(const wchar_t *sql) { if(!this->db) throw database_error("database is not open"); sqlite3_command(*this, sql).executenonquery(); }
void sqlite3_connection::executenonquery(const wchar_t *sql) { check_db_open(); sqlite3_command(*this, sql).executenonquery(); }
double sqlite3_connection::executedouble(const wchar_t *sql) { check_db_open(); return sqlite3_command(*this, sql).executedouble(); }
void sqlite3_connection::executenonquery(const std::wstring &sql) { if(!this->m_db) throw database_error("database is not open"); sqlite3_command(*this, sql).executenonquery(); }
std::string sqlite3_connection::executestring(const char *sql) { check_db_open(); return sqlite3_command(*this, sql).executestring(); }
long long sqlite3_connection::executeint64(const std::string &sql) { check_db_open(); return sqlite3_command(*this, sql).executeint64(); }
int sqlite3_connection::executeint(const char *sql) { check_db_open(); return sqlite3_command(*this, sql).executeint(); }
void sqlite3_connection::executenonquery(const std::string &sql) { check_db_open(); sqlite3_command(*this, sql).executenonquery(); }
const char * sqlite3_connection::executenonquery(const char *sql) { check_db_open(); sqlite3_command(*this, sql).executenonquery(); return sql; }
std::wstring sqlite3_connection::executestring16(const std::wstring &sql) { check_db_open(); return sqlite3_command(*this, sql).executestring16(); }
std::string sqlite3_connection::executeblob(const std::wstring &sql) { if(!this->m_db) throw database_error("database is not open"); return sqlite3_command(*this, sql).executeblob(); }
long long sqlite3_connection::executeint64(const wchar_t *sql) { if(!this->db) throw database_error("database is not open"); return sqlite3_command(*this, sql).executeint64(); }
long long sqlite3_connection::executeint64(const wchar_t *sql) { check_db_open(); return sqlite3_command(*this, sql).executeint64(); }
double sqlite3_connection::executedouble(char const * sql) { if(!this->m_db) throw database_error("database is not open"); return sqlite3_command(*this, sql).executedouble(); }
double sqlite3_connection::executedouble(const std::string &sql) { if(!this->db) throw database_error("database is not open"); return sqlite3_command(*this, sql).executedouble(); }
std::wstring sqlite3_connection::executestring16(const wchar_t *sql) { if(!this->db) throw database_error("database is not open"); return sqlite3_command(*this, sql).executestring16(); }
double sqlite3_connection::executedouble(const std::string &sql) { check_db_open(); return sqlite3_command(*this, sql).executedouble(); }