Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @file database_user.c
- * @author Jens Pauwels <Jens@dptechnics.com>
- * @date 15 Jun 2018
- * @copyright DPTechnics
- * @brief user database actions.
- *
- * @section LICENSE
- *
- * Copyright (C) DPTechnics - All Rights Reserved
- *
- * Unauthorized copying of this file, via any medium is strictly prohibited
- * Proprietary and confidential.
- *
- * @section DESCRIPTION
- *
- * This file contains the temperature monitor sensor database
- * implementation.
- */
- #include <time.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <string.h>
- #include <stdbool.h>
- #include <sqlite3.h>
- #include <wembed/wembed.h>
- #include "../config/config.h"
- #include "../helper/helper.h"
- #include "../example/example_json_api.h"
- #include "database.h"
- static bool _database_get_user_handler(sqlite3_stmt *stmt, void **args) {
- struct user *user = (struct user*) *args;
- user->id = sqlite3_column_int(stmt, 0);
- helper_strncpy_safe(user->username = (const char*) sqlite3_column_text(stmt, 1), USERNAME_MAX_LEN);
- helper_strncpy_safe(user->password = (const char*) sqlite3_column_text(stmt, 2), PASSWORD_MAX_LEN);
- return true;
- };
- static bool _database_get_user_array_handler(sqlite3_stmt *stmt, void **args)
- {
- struct example_user_array *users = (struct example_user_array*) *args;
- /* Create space for a new sensor */
- users->count += 1;
- users->array = (struct users*) realloc(users->array, users->count * sizeof(struct user));
- if(users->array == NULL) {
- wembed_log(WEMBED_LOG_ERROR, "[DATABASE] Not enough memory to store sensors");
- return false;
- }
- struct user *user = sensors->array + sensors->count - 1;
- /* Read the values from this sensor */
- user->id = sqlite3_column_int(stmt, 0);
- helper_strncpy_safe(user->username = (const char*) sqlite3_column_text(stmt, 1), USERNAME_MAX_LEN);
- helper_strncpy_safe(user->password = (const char*) sqlite3_column_text(stmt, 2), PASSWORD_MAX_LEN);
- return true;
- }
- void database_get_user(struct user *user) {
- if(sqlite3_prepare_v2(_db, "SELECT "
- "id,"
- "username,"
- "password,"
- "FROM users", -1, &statement, 0) != SQLITE_OK) {
- wembed_log(WEMBED_LOG_ERROR, "[DATABASE] Could not prepare SQL statement: %s\n", sqlite3_errmsg(_db));
- return false;
- }
- /* Execute the statement */
- if(wembed_helper_database_execute_prepared_statement(statement, _database_get_user_handler, (void**) &user) != SQLITE_OK) {
- wembed_log(WEMBED_LOG_ERROR, "[DATABASE] Could not execute SQL statement to get the measurement: %s\n", sqlite3_errmsg(_db));
- sqlite3_finalize(statement);
- return false;
- }
- /* Cleanup */
- sqlite3_finalize(statement);
- return true;
- }
- bool database_insert_user(struct user *user) {
- /* Prepare the statement */
- sqlite3_stmt *statement;
- if(sqlite3_prepare_v2(_db, "INSERT INTO user (username, password) VALUES (?,?)", -1, &statement, 0) != SQLITE_OK) {
- wembed_log(WEMBED_LOG_ERROR, "[DATABASE] Could not prepare SQL statement: %s\n", sqlite3_errmsg(_db));
- return false;
- }
- /* Bind the username */
- if(sqlite3_bind_text(statement, 1, user->username, -1, NULL) != SQLITE_OK) {
- wembed_log(WEMBED_LOG_ERROR, "[DATABASE] Could not bind the user username: %s\n", sqlite3_errmsg(_db));
- sqlite3_finalize(statement);
- return false;
- }
- /* Bind the password */
- if(sqlite3_bind_text(statement, 2, user->password, -1, NULL) != SQLITE_OK) {
- wembed_log(WEMBED_LOG_ERROR, "[DATABASE] Could not bind the user password: %s\n", sqlite3_errmsg(_db));
- sqlite3_finalize(statement);
- return false;
- }
- /* Execute the statement */
- if(wembed_helper_database_execute_prepared_statement(statement, NULL, NULL) != SQLITE_OK) {
- wembed_log(WEMBED_LOG_ERROR, "[DATABASE] Could not execute SQL statement to insert the new user: %s\n", sqlite3_errmsg(_db));
- sqlite3_finalize(statement);
- return false;
- }
- /* Retrieve the inserted row ID */
- user->id = (int) sqlite3_last_insert_rowid(_db);
- /* Cleanup */
- sqlite3_finalize(statement);
- return true;
- }
- bool database_update_user(struct user *user) {
- sqlite3_stmt *statement;
- if(sqlite3_prepare_v2(_db, "UPDATE users SET username=?,password=? WHERE id=?", -1, &statement, 0) != SQLITE_OK) {
- wembed_log(WEMBED_LOG_ERROR, "Could not prepare SQL statement: %s\n", sqlite3_errmsg(_db));
- return false;
- }
- /* Bind the last_delta */
- if(sqlite3_bind_double(statement, 1, user->username) != SQLITE_OK) {
- wembed_log(WEMBED_LOG_ERROR, "[DATABASE] Could not bind the username: %s\n", sqlite3_errmsg(_db));
- sqlite3_finalize(statement);
- return false;
- }
- /* Bind the password */
- if(sqlite3_bind_int(statement, 2, user->password) != SQLITE_OK) {
- wembed_log(WEMBED_LOG_ERROR, "[DATABASE] Could not bind the password: %s\n", sqlite3_errmsg(_db));
- sqlite3_finalize(statement);
- return false;
- }
- /* Bind the id */
- if(sqlite3_bind_int(statement, 2, user->id) != SQLITE_OK) {
- wembed_log(WEMBED_LOG_ERROR, "[DATABASE] Could not bind the id: %s\n", sqlite3_errmsg(_db));
- sqlite3_finalize(statement);
- return false;
- }
- /* Execute the statement */
- if(wembed_helper_database_execute_prepared_statement(statement, NULL, NULL) != SQLITE_OK) {
- wembed_log(WEMBED_LOG_ERROR, "Could not execute SQL statement to update last_delta of sensor: %s\n", sqlite3_errmsg(_db));
- sqlite3_finalize(statement);
- return false;
- }
- /* Cleanup */
- sqlite3_finalize(statement);
- return true;
- }
- bool database_remove_user(int id) {
- /* Prepare the statement */
- sqlite3_stmt *statement;
- if(sqlite3_prepare_v2(_db, "DELETE FROM users WHERE id = ?", -1, &statement, 0) != SQLITE_OK) {
- wembed_log(WEMBED_LOG_ERROR, "Could not prepare SQL statement: %s\n", sqlite3_errmsg(_db));
- return false;
- }
- /* Bind the database id of the sensor */
- if(sqlite3_bind_int(statement, 1, id) != SQLITE_OK) {
- wembed_log(WEMBED_LOG_ERROR, "[DATABASE] Could not bind id of the user: %s\n", sqlite3_errmsg(_db));
- sqlite3_finalize(statement);
- return false;
- }
- /* Execute the statement */
- if(wembed_helper_database_execute_prepared_statement(statement, NULL, NULL) != SQLITE_OK) {
- wembed_log(WEMBED_LOG_ERROR, "Could not execute SQL statement to delete the user: %s\n", sqlite3_errmsg(_db));
- sqlite3_finalize(statement);
- return false;
- }
- /* Cleanup */
- sqlite3_finalize(statement);
- return true;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement