Advertisement
Guest User

Untitled

a guest
Jun 15th, 2018
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.83 KB | None | 0 0
  1. /**
  2. * @file database_user.c
  3. * @author Jens Pauwels <Jens@dptechnics.com>
  4. * @date 15 Jun 2018
  5. * @copyright DPTechnics
  6. * @brief user database actions.
  7. *
  8. * @section LICENSE
  9. *
  10. * Copyright (C) DPTechnics - All Rights Reserved
  11. *
  12. * Unauthorized copying of this file, via any medium is strictly prohibited
  13. * Proprietary and confidential.
  14. *
  15. * @section DESCRIPTION
  16. *
  17. * This file contains the temperature monitor sensor database
  18. * implementation.
  19. */
  20.  
  21. #include <time.h>
  22. #include <fcntl.h>
  23. #include <stdio.h>
  24. #include <unistd.h>
  25. #include <string.h>
  26. #include <stdbool.h>
  27. #include <sqlite3.h>
  28. #include <wembed/wembed.h>
  29.  
  30. #include "../config/config.h"
  31. #include "../helper/helper.h"
  32. #include "../example/example_json_api.h"
  33. #include "database.h"
  34.  
  35.  
  36. static bool _database_get_user_handler(sqlite3_stmt *stmt, void **args) {
  37. struct user *user = (struct user*) *args;
  38.  
  39. user->id = sqlite3_column_int(stmt, 0);
  40. helper_strncpy_safe(user->username = (const char*) sqlite3_column_text(stmt, 1), USERNAME_MAX_LEN);
  41. helper_strncpy_safe(user->password = (const char*) sqlite3_column_text(stmt, 2), PASSWORD_MAX_LEN);
  42. return true;
  43. };
  44.  
  45. static bool _database_get_user_array_handler(sqlite3_stmt *stmt, void **args)
  46. {
  47. struct example_user_array *users = (struct example_user_array*) *args;
  48.  
  49. /* Create space for a new sensor */
  50. users->count += 1;
  51. users->array = (struct users*) realloc(users->array, users->count * sizeof(struct user));
  52.  
  53. if(users->array == NULL) {
  54. wembed_log(WEMBED_LOG_ERROR, "[DATABASE] Not enough memory to store sensors");
  55. return false;
  56. }
  57.  
  58. struct user *user = sensors->array + sensors->count - 1;
  59.  
  60. /* Read the values from this sensor */
  61. user->id = sqlite3_column_int(stmt, 0);
  62. helper_strncpy_safe(user->username = (const char*) sqlite3_column_text(stmt, 1), USERNAME_MAX_LEN);
  63. helper_strncpy_safe(user->password = (const char*) sqlite3_column_text(stmt, 2), PASSWORD_MAX_LEN);
  64. return true;
  65. }
  66.  
  67.  
  68.  
  69.  
  70. void database_get_user(struct user *user) {
  71.  
  72. if(sqlite3_prepare_v2(_db, "SELECT "
  73. "id,"
  74. "username,"
  75. "password,"
  76. "FROM users", -1, &statement, 0) != SQLITE_OK) {
  77. wembed_log(WEMBED_LOG_ERROR, "[DATABASE] Could not prepare SQL statement: %s\n", sqlite3_errmsg(_db));
  78. return false;
  79. }
  80.  
  81. /* Execute the statement */
  82. if(wembed_helper_database_execute_prepared_statement(statement, _database_get_user_handler, (void**) &user) != SQLITE_OK) {
  83. wembed_log(WEMBED_LOG_ERROR, "[DATABASE] Could not execute SQL statement to get the measurement: %s\n", sqlite3_errmsg(_db));
  84. sqlite3_finalize(statement);
  85. return false;
  86. }
  87.  
  88. /* Cleanup */
  89. sqlite3_finalize(statement);
  90. return true;
  91. }
  92.  
  93. bool database_insert_user(struct user *user) {
  94. /* Prepare the statement */
  95. sqlite3_stmt *statement;
  96.  
  97. if(sqlite3_prepare_v2(_db, "INSERT INTO user (username, password) VALUES (?,?)", -1, &statement, 0) != SQLITE_OK) {
  98. wembed_log(WEMBED_LOG_ERROR, "[DATABASE] Could not prepare SQL statement: %s\n", sqlite3_errmsg(_db));
  99. return false;
  100. }
  101.  
  102. /* Bind the username */
  103. if(sqlite3_bind_text(statement, 1, user->username, -1, NULL) != SQLITE_OK) {
  104. wembed_log(WEMBED_LOG_ERROR, "[DATABASE] Could not bind the user username: %s\n", sqlite3_errmsg(_db));
  105. sqlite3_finalize(statement);
  106. return false;
  107. }
  108.  
  109. /* Bind the password */
  110. if(sqlite3_bind_text(statement, 2, user->password, -1, NULL) != SQLITE_OK) {
  111. wembed_log(WEMBED_LOG_ERROR, "[DATABASE] Could not bind the user password: %s\n", sqlite3_errmsg(_db));
  112. sqlite3_finalize(statement);
  113. return false;
  114. }
  115.  
  116. /* Execute the statement */
  117. if(wembed_helper_database_execute_prepared_statement(statement, NULL, NULL) != SQLITE_OK) {
  118. wembed_log(WEMBED_LOG_ERROR, "[DATABASE] Could not execute SQL statement to insert the new user: %s\n", sqlite3_errmsg(_db));
  119. sqlite3_finalize(statement);
  120. return false;
  121. }
  122.  
  123. /* Retrieve the inserted row ID */
  124. user->id = (int) sqlite3_last_insert_rowid(_db);
  125.  
  126. /* Cleanup */
  127. sqlite3_finalize(statement);
  128. return true;
  129. }
  130.  
  131. bool database_update_user(struct user *user) {
  132.  
  133. sqlite3_stmt *statement;
  134. if(sqlite3_prepare_v2(_db, "UPDATE users SET username=?,password=? WHERE id=?", -1, &statement, 0) != SQLITE_OK) {
  135. wembed_log(WEMBED_LOG_ERROR, "Could not prepare SQL statement: %s\n", sqlite3_errmsg(_db));
  136. return false;
  137. }
  138.  
  139. /* Bind the last_delta */
  140. if(sqlite3_bind_double(statement, 1, user->username) != SQLITE_OK) {
  141. wembed_log(WEMBED_LOG_ERROR, "[DATABASE] Could not bind the username: %s\n", sqlite3_errmsg(_db));
  142. sqlite3_finalize(statement);
  143. return false;
  144. }
  145.  
  146. /* Bind the password */
  147. if(sqlite3_bind_int(statement, 2, user->password) != SQLITE_OK) {
  148. wembed_log(WEMBED_LOG_ERROR, "[DATABASE] Could not bind the password: %s\n", sqlite3_errmsg(_db));
  149. sqlite3_finalize(statement);
  150. return false;
  151. }
  152.  
  153. /* Bind the id */
  154. if(sqlite3_bind_int(statement, 2, user->id) != SQLITE_OK) {
  155. wembed_log(WEMBED_LOG_ERROR, "[DATABASE] Could not bind the id: %s\n", sqlite3_errmsg(_db));
  156. sqlite3_finalize(statement);
  157. return false;
  158. }
  159.  
  160. /* Execute the statement */
  161. if(wembed_helper_database_execute_prepared_statement(statement, NULL, NULL) != SQLITE_OK) {
  162. wembed_log(WEMBED_LOG_ERROR, "Could not execute SQL statement to update last_delta of sensor: %s\n", sqlite3_errmsg(_db));
  163. sqlite3_finalize(statement);
  164. return false;
  165. }
  166.  
  167. /* Cleanup */
  168. sqlite3_finalize(statement);
  169. return true;
  170. }
  171.  
  172. bool database_remove_user(int id) {
  173. /* Prepare the statement */
  174. sqlite3_stmt *statement;
  175. if(sqlite3_prepare_v2(_db, "DELETE FROM users WHERE id = ?", -1, &statement, 0) != SQLITE_OK) {
  176. wembed_log(WEMBED_LOG_ERROR, "Could not prepare SQL statement: %s\n", sqlite3_errmsg(_db));
  177. return false;
  178. }
  179.  
  180. /* Bind the database id of the sensor */
  181. if(sqlite3_bind_int(statement, 1, id) != SQLITE_OK) {
  182. wembed_log(WEMBED_LOG_ERROR, "[DATABASE] Could not bind id of the user: %s\n", sqlite3_errmsg(_db));
  183. sqlite3_finalize(statement);
  184. return false;
  185. }
  186.  
  187. /* Execute the statement */
  188. if(wembed_helper_database_execute_prepared_statement(statement, NULL, NULL) != SQLITE_OK) {
  189. wembed_log(WEMBED_LOG_ERROR, "Could not execute SQL statement to delete the user: %s\n", sqlite3_errmsg(_db));
  190. sqlite3_finalize(statement);
  191. return false;
  192. }
  193.  
  194. /* Cleanup */
  195. sqlite3_finalize(statement);
  196. return true;
  197. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement