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_FILE_SIZE = 0;
constexpr unsigned long UNDEFINE_FILE_DURATION = 0;
constexpr time_t UNDEFINE_CREATE_TIME = -1;
typedef struct sync_file_info
{

View File

@ -120,24 +120,61 @@ bool SqliteHandle::SyncFile(const SyncFileInfo &info)
LogError("Serial number is undefine.\n");
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) {
std::stringstream sqlStream;
std::string comma = " ";
sqlStream << "UPDATE " FILES_TABLE " SET ";
if (UNDEFINE_CREATE_TIME != info.mCreateTime_s) {
sqlStream << comma << CREATE_TIME " = '" << info.mCreateTime_s << "'";
comma = ", ";
}
if (info.mFileName.empty() == false) {
sqlStream << comma << FILE_PATH " = '" << info.mFileName << "'";
comma = ", ";
}
if (UNDEFINE_FILE_SIZE != info.mFileSize) {
sqlStream << comma << FILE_SIZE " = '" << info.mFileSize << "'";
comma = ", ";
}
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;
}
// 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;
}
bool SqliteHandle::SearchFiles(const std::vector<FileCreateType> &types, std::vector<SyncFileInfo> &info)