Improve:sql handle time.

This commit is contained in:
Fancy code 2024-07-13 22:37:25 +08:00
parent a9f698bf8b
commit ce3e00d4d7
2 changed files with 54 additions and 16 deletions

View File

@ -41,6 +41,7 @@ enum class FileStatus
}; };
constexpr unsigned long UNDEFINE_SERIAL_NUMBER = -1; constexpr unsigned long UNDEFINE_SERIAL_NUMBER = -1;
constexpr unsigned long UNDEFINE_FILE_SIZE = 0; constexpr unsigned long UNDEFINE_FILE_SIZE = 0;
constexpr unsigned long UNDEFINE_FILE_DURATION = 0;
constexpr time_t UNDEFINE_CREATE_TIME = -1; constexpr time_t UNDEFINE_CREATE_TIME = -1;
typedef struct sync_file_info typedef struct sync_file_info
{ {

View File

@ -120,24 +120,61 @@ bool SqliteHandle::SyncFile(const SyncFileInfo &info)
LogError("Serial number is undefine.\n"); LogError("Serial number is undefine.\n");
return false; return false;
} }
if (UpdateCreateTime(mDb, info.mSerialNumber, info.mCreateTime_s) == false) { std::stringstream sqlStream;
return false; std::string comma = " ";
} sqlStream << "UPDATE " FILES_TABLE " SET ";
if (UpdateFileName(mDb, info.mSerialNumber, info.mFileName) == false) { if (UNDEFINE_CREATE_TIME != info.mCreateTime_s) {
return false; sqlStream << comma << CREATE_TIME " = '" << info.mCreateTime_s << "'";
} comma = ", ";
if (UpdateFileSize(mDb, info.mSerialNumber, info.mFileSize) == false) { }
return false; if (info.mFileName.empty() == false) {
} sqlStream << comma << FILE_PATH " = '" << info.mFileName << "'";
if (UpdateFileStatus(mDb, info.mSerialNumber, info.mStatus) == false) { comma = ", ";
return false; }
} if (UNDEFINE_FILE_SIZE != info.mFileSize) {
if (UpdateFileType(mDb, info.mSerialNumber, info.mType) == false) { sqlStream << comma << FILE_SIZE " = '" << info.mFileSize << "'";
return false; comma = ", ";
} }
if (UpdateFileDuration(mDb, info.mSerialNumber, info.mFileDuration) == false) { if (FileStatus::END != info.mStatus) {
sqlStream << comma << FILE_STATUS " = '" << ConvertFileStatusToString(info.mStatus) << "'";
comma = ", ";
}
if (FileCreateType::END != info.mType) {
sqlStream << comma << FILE_TYPE " = '" << ConvertFileTypeToString(info.mType) << "'";
comma = ", ";
}
if (UNDEFINE_FILE_DURATION != info.mFileDuration) {
sqlStream << comma << FILE_DURATION " = '" << info.mFileDuration << "'";
comma = ", ";
}
sqlStream << " WHERE " TABLE_KEY " = " << info.mSerialNumber << ";";
LogInfo("Sql: %s\n", sqlStream.str().c_str());
char *errMsg = nullptr;
int rc = SQLITE_UNDEFINE;
rc = sqlite3_exec(mDb, sqlStream.str().c_str(), nullptr, nullptr, &errMsg);
if (SQLITE_OK != rc) {
LogError("SQL error: %s\n", errMsg);
sqlite3_free(errMsg);
return false; return false;
} }
// if (UpdateCreateTime(mDb, info.mSerialNumber, info.mCreateTime_s) == false) {
// return false;
// }
// if (UpdateFileName(mDb, info.mSerialNumber, info.mFileName) == false) {
// return false;
// }
// if (UpdateFileSize(mDb, info.mSerialNumber, info.mFileSize) == false) {
// return false;
// }
// if (UpdateFileStatus(mDb, info.mSerialNumber, info.mStatus) == false) {
// return false;
// }
// if (UpdateFileType(mDb, info.mSerialNumber, info.mType) == false) {
// return false;
// }
// if (UpdateFileDuration(mDb, info.mSerialNumber, info.mFileDuration) == false) {
// return false;
// }
return true; return true;
} }
bool SqliteHandle::SearchFiles(const std::vector<FileCreateType> &types, std::vector<SyncFileInfo> &info) bool SqliteHandle::SearchFiles(const std::vector<FileCreateType> &types, std::vector<SyncFileInfo> &info)