Init
This commit is contained in:
172
userAuth.cpp
Normal file
172
userAuth.cpp
Normal file
@@ -0,0 +1,172 @@
|
||||
#include "userAuth.h"
|
||||
#include "tools.h"
|
||||
|
||||
const char *CREATE_USER_TABLE = "CREATE TABLE IF NOT EXISTS user_data ("
|
||||
"id INTEGER PRIMARY KEY AUTOINCREMENT,"
|
||||
"username TEXT NOT NULL,"
|
||||
"userpassword TEXT NOT NULL,"
|
||||
"created_time DATETIME)";
|
||||
|
||||
const char *REGISTER_USER = "INSERT INTO user_data (username, userpassword, "
|
||||
"created_time) VALUES (?1, ?2, ?3);";
|
||||
|
||||
const char *LOGIN_USER =
|
||||
"SELECT 1 FROM user_data WHERE username = ? AND userpassword = ?;";
|
||||
|
||||
const char *SELECT_USER =
|
||||
"SELECT id, username, userpassword, created_time FROM user_data "
|
||||
"ORDER BY id DESC;";
|
||||
|
||||
const char *UPDATE_USER =
|
||||
"UPDATE user_data SET username = ?1, userpassword = ?3 WHERE id = ?2";
|
||||
|
||||
const char *DELETE_USER = "DELETE FROM user_data WHERE id = ?1;";
|
||||
|
||||
void init_user_data(sqlite3 *db) {
|
||||
char *err_msg = nullptr;
|
||||
|
||||
int rc = sqlite3_exec(db, CREATE_USER_TABLE, nullptr, nullptr, &err_msg);
|
||||
if (rc != SQLITE_OK) {
|
||||
std::cerr << "创建表失败: " << err_msg << std::endl;
|
||||
sqlite3_free(err_msg);
|
||||
exit(1);
|
||||
};
|
||||
};
|
||||
|
||||
void register_user(sqlite3 *db, const string &username,
|
||||
const string &userpassword) {
|
||||
sqlite3_stmt *stmt;
|
||||
|
||||
int rc = sqlite3_prepare_v2(db, REGISTER_USER, -1, &stmt, nullptr);
|
||||
|
||||
if (rc != SQLITE_OK) {
|
||||
std::cerr << "准备语句失败: " << sqlite3_errmsg(db) << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
sqlite3_bind_text(stmt, 1, username.c_str(), -1, SQLITE_STATIC);
|
||||
sqlite3_bind_text(stmt, 2, userpassword.c_str(), -1, SQLITE_STATIC);
|
||||
|
||||
string timestamp = get_utc8_time();
|
||||
sqlite3_bind_text(stmt, 3, timestamp.c_str(), -1, SQLITE_STATIC);
|
||||
|
||||
rc = sqlite3_step(stmt);
|
||||
if (rc != SQLITE_DONE) {
|
||||
std::cerr << "执行失败" << sqlite3_errmsg(db) << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
sqlite3_finalize(stmt);
|
||||
};
|
||||
|
||||
bool login_user(sqlite3 *db, const std::string &username,
|
||||
const std::string &password) {
|
||||
sqlite3_stmt *stmt;
|
||||
bool authResult = false;
|
||||
|
||||
if (sqlite3_prepare_v2(db, LOGIN_USER, -1, &stmt, nullptr) != SQLITE_OK) {
|
||||
std::cerr << "准备语句失败: " << sqlite3_errmsg(db) << std::endl;
|
||||
sqlite3_close(db);
|
||||
return false;
|
||||
}
|
||||
|
||||
sqlite3_bind_text(stmt, 1, username.c_str(), -1, SQLITE_STATIC);
|
||||
sqlite3_bind_text(stmt, 2, password.c_str(), -1, SQLITE_STATIC);
|
||||
|
||||
// 执行查询
|
||||
if (sqlite3_step(stmt) == SQLITE_ROW) {
|
||||
authResult = true; // 找到匹配记录
|
||||
}
|
||||
|
||||
sqlite3_finalize(stmt);
|
||||
return authResult;
|
||||
}
|
||||
|
||||
vector<UserStruct> get_user_inputs(sqlite3 *db) {
|
||||
vector<UserStruct> results;
|
||||
sqlite3_stmt *stmt;
|
||||
|
||||
int rc = sqlite3_prepare_v2(db, SELECT_USER, -1, &stmt, nullptr);
|
||||
if (rc != SQLITE_OK) {
|
||||
std::cerr << "准备查询失败: " << sqlite3_errmsg(db) << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
|
||||
UserStruct record;
|
||||
record.id = sqlite3_column_int(stmt, 0);
|
||||
|
||||
const unsigned char *text1 = sqlite3_column_text(stmt, 1);
|
||||
record.username = std::string(reinterpret_cast<const char *>(text1));
|
||||
|
||||
const unsigned char *text2 = sqlite3_column_text(stmt, 2);
|
||||
record.userpassword = std::string(reinterpret_cast<const char *>(text2));
|
||||
|
||||
const unsigned char *time = sqlite3_column_text(stmt, 3);
|
||||
record.created_time = time ? reinterpret_cast<const char *>(time) : "N/A";
|
||||
|
||||
results.push_back(record);
|
||||
}
|
||||
|
||||
if (rc != SQLITE_DONE) {
|
||||
std::cerr << "查询错误失败: " << sqlite3_errmsg(db) << std::endl;
|
||||
}
|
||||
|
||||
sqlite3_finalize(stmt);
|
||||
return results;
|
||||
}
|
||||
|
||||
bool update_user(sqlite3 *db, int id, const string &new_username,
|
||||
const string &new_password) {
|
||||
sqlite3_stmt *stmt;
|
||||
|
||||
int rc = sqlite3_prepare_v2(db, UPDATE_USER, -1, &stmt, nullptr);
|
||||
if (rc != SQLITE_OK) {
|
||||
std::cerr << "准备更新语句失败: " << sqlite3_errmsg(db) << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
sqlite3_bind_text(stmt, 1, new_username.c_str(), -1, SQLITE_STATIC);
|
||||
sqlite3_bind_text(stmt, 3, new_password.c_str(), -1, SQLITE_STATIC);
|
||||
sqlite3_bind_int(stmt, 2, id);
|
||||
|
||||
rc = sqlite3_step(stmt);
|
||||
sqlite3_finalize(stmt);
|
||||
|
||||
if (rc != SQLITE_DONE) {
|
||||
std::cerr << "更新执行失败: " << sqlite3_errmsg(db) << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool delete_user(sqlite3 *db, int id) {
|
||||
sqlite3_stmt *stmt = nullptr;
|
||||
|
||||
int rc = sqlite3_prepare_v2(db, DELETE_USER, -1, &stmt, nullptr);
|
||||
if (rc != SQLITE_OK) {
|
||||
std::cerr << "准备删除语句失败: " << sqlite3_errmsg(db) << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
rc = sqlite3_bind_int(stmt, 1, id);
|
||||
|
||||
if (rc != SQLITE_OK) {
|
||||
std::cerr << "绑定参数失败: " << sqlite3_errmsg(db) << std::endl;
|
||||
sqlite3_finalize(stmt);
|
||||
return false;
|
||||
}
|
||||
|
||||
rc = sqlite3_step(stmt);
|
||||
const bool success = (rc == SQLITE_DONE);
|
||||
const char *errorMsg = sqlite3_errmsg(db);
|
||||
|
||||
sqlite3_finalize(stmt);
|
||||
|
||||
if (!success) {
|
||||
std::cerr << "删除执行失败: " << errorMsg << std::endl;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
Reference in New Issue
Block a user