Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make (push) Successful in 10s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake (push) Successful in 12s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Meson (push) Successful in 12s
45 lines
1.4 KiB
C
45 lines
1.4 KiB
C
#include "database.h"
|
|
#include "sql.h"
|
|
#include "user.h"
|
|
|
|
User *db_get_user_by_id(const int id) {
|
|
User *user = nullptr;
|
|
pthread_mutex_lock(&db_mutex);
|
|
if (!db) {
|
|
fprintf(stderr, "Database not initialized\n");
|
|
pthread_mutex_unlock(&db_mutex);
|
|
return nullptr;
|
|
}
|
|
sqlite3_stmt *stmt;
|
|
int rc = sqlite3_prepare_v2(db, SELECT_USER_BY_ID_SQL, -1, &stmt, nullptr);
|
|
if (rc != SQLITE_OK) {
|
|
fprintf(stderr, "Cannot prepare statement: %s\n", sqlite3_errmsg(db));
|
|
pthread_mutex_unlock(&db_mutex);
|
|
return nullptr;
|
|
}
|
|
|
|
sqlite3_bind_int(stmt, 1, id);
|
|
|
|
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
|
|
int id = sqlite3_column_int(stmt, 0);
|
|
const unsigned char *db_username = sqlite3_column_text(stmt, 1);
|
|
const unsigned char *db_email = sqlite3_column_text(stmt, 2);
|
|
const unsigned char *db_password = sqlite3_column_text(stmt, 3);
|
|
const unsigned char *db_group = sqlite3_column_text(stmt, 4);
|
|
int db_is_superuser = sqlite3_column_int(stmt, 5);
|
|
user = new_user(id, (char *)db_username, (char *)db_password,
|
|
(char *)db_email, (char *)db_group, db_is_superuser);
|
|
}
|
|
|
|
if (rc != SQLITE_DONE) {
|
|
fprintf(stderr, "Error reading data: %s\n", sqlite3_errmsg(db));
|
|
free_user(user);
|
|
sqlite3_finalize(stmt);
|
|
pthread_mutex_unlock(&db_mutex);
|
|
return nullptr;
|
|
}
|
|
|
|
pthread_mutex_unlock(&db_mutex);
|
|
return user;
|
|
}
|