#include #include "../shared/MiscSupport.h" #include "MiscSQL.h" const std::string sqlTrue = "1"; const std::string sqlFalse = "0"; std::string sqlAnd(std::vector< std::string > const &expressionList) { std::vector< std::string > interesting; for (std::vector< std::string >::const_iterator it = expressionList.begin(); it != expressionList.end(); it++) { std::string value = *it; if (value == sqlFalse) { return sqlFalse; } else if (value != sqlTrue) { interesting.push_back(value); } } switch (interesting.size()) { case 0: return sqlTrue; case 1: return interesting[0]; default: return "(" + implode(") AND (", interesting.begin(), interesting.end()) + ")"; } } std::string sqlOr(std::vector< std::string > const &expressionList) { std::vector< std::string > interesting; for (std::vector< std::string >::const_iterator it = expressionList.begin(); it != expressionList.end(); it++) { std::string value = *it; if (value == sqlTrue) { return sqlTrue; } else if (value != sqlFalse) { interesting.push_back(value); } } switch (interesting.size()) { case 0: return sqlFalse; case 1: return interesting[0]; default: return "(" + implode(") OR (", interesting.begin(), interesting.end()) + ")"; } } std::string sqlIn(std::string left, std::set< std::string > const &right) { switch (right.size()) { case 0: { return sqlFalse; } case 1: { return "(" + left + ") = (" + *right.begin() + ")"; } default: { return left + " IN (" + implode(", ", right.begin(), right.end()) + ")"; } } } std::string sqlUnion(std::vector< std::string > const &queries) { assert(!queries.empty()); // There is no good default to return in this case. if (queries.size() == 1) return *queries.begin(); std::string result; for (std::vector< std::string >::const_iterator it = queries.begin(); it != queries.end(); it++) { if (!result.empty()) result += " UNION "; result += '('; result += *it; result += ')'; } return result; } std::string timeTToMysql(time_t time) { static const std::string ZERO = "0000-00-00 00:00:00"; if (time <= 0) return ZERO; struct tm brokenDown; localtime_r(&time, &brokenDown); char buffer[25]; const size_t result = strftime(buffer, 20, "%Y-%m-%d %H:%M:%S", &brokenDown); if (result != 19) return ZERO; return buffer; } std::string dateToMysql(time_t date, bool round) { static const std::string ZERO = "0000-00-00"; if (date <= 0) return ZERO; if (round) // So 11:00pm on January 26 and 1:00am on January 27 would both appear as // "2014-01-27". date += 12*60*60; struct tm brokenDown; localtime_r(&date, &brokenDown); char buffer[25]; const size_t result = strftime(buffer, 11, "%Y-%m-%d", &brokenDown); if (result != 10) return ZERO; return buffer; } std::string secondsToMysql(int seconds, std::string date) { if ((seconds < 0) || (seconds >= 60 * 60 * 24)) { // This shouldn't happen! seconds = 0; } if (date.length() >= 10) { date = date.substr(0, 10) + ' '; } else { date = ""; } char buffer[10]; snprintf(buffer, 9, "%02d:%02d:%02d", seconds/3600, (seconds/60)%60, seconds%60); buffer[9] = '\0'; return date + buffer; } int mysqlToSeconds(std::string mysqlTime) { std::string::size_type length = mysqlTime.length(); if (length < 8) { // This shouldn't happen! return 0; } int hours = strtolDefault(mysqlTime.substr(length - 8, 2), -1); int minutes = strtolDefault(mysqlTime.substr(length - 5, 2), -1); int seconds = strtolDefault(mysqlTime.substr(length - 2, 2), -1); if ((hours < 0) || (minutes < 0) || (seconds < 0)) { // This shouldn't happen! return 0; } return hours * 3600 + minutes * 60 + seconds; } time_t mysqlToTimeT(std::string const &mysql) { bool dateOnly; if (mysql.size() == 10) dateOnly = true; else if (mysql.size() == 19) dateOnly = false; else return 0; if (mysql[4] != '-') return 0; if (mysql[7] != '-') return 0; const int year = strtolDefault(mysql.substr(0, 4), -1); if (year < 1970) return 0; const int month = strtolDefault(mysql.substr(5, 2), -1); if ((month < 1) || (month > 12)) return 0; const int day = strtolDefault(mysql.substr(8, 2), -1); if ((day < 1) || (day > 31)) return 0; tm brokenDownTime; brokenDownTime.tm_year = year - 1900; brokenDownTime.tm_mon = month - 1; brokenDownTime.tm_mday = day; if (dateOnly) { brokenDownTime.tm_hour = 0; brokenDownTime.tm_min = 0; brokenDownTime.tm_sec = 0; } else { if (mysql[10] != ' ') return 0; if (mysql[13] != ':') return 0; if (mysql[16] != ':') return 0; const int hour = strtolDefault(mysql.substr(11, 2), -1); if ((hour < 0) || (hour > 23)) return 0; const int minute = strtolDefault(mysql.substr(14, 2), -1); if ((minute < 0) || (minute > 59)) return 0; const int second = strtolDefault(mysql.substr(17, 2), -1); if ((second < 0) || (second > 61)) return 0; brokenDownTime.tm_hour = hour; brokenDownTime.tm_min = minute; brokenDownTime.tm_sec = second; } brokenDownTime.tm_isdst = -1; time_t result = mktime(&brokenDownTime); if (result == (time_t)-1) return 0; return result; }