sq.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2017 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file sq/sq.c
  18. * @brief helper functions for Sqlite3 DB interactions
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_sq_lib.h"
  23. /**
  24. * Execute a prepared statement.
  25. *
  26. * @param db_conn database connection
  27. * @param params parameters to the statement
  28. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  29. */
  30. int
  31. GNUNET_SQ_bind (sqlite3_stmt *stmt,
  32. const struct GNUNET_SQ_QueryParam *params)
  33. {
  34. unsigned int j;
  35. j = 1;
  36. for (unsigned int i = 0; NULL != params[i].conv; i++)
  37. {
  38. if (GNUNET_OK !=
  39. params[i].conv (params[i].conv_cls,
  40. params[i].data,
  41. params[i].size,
  42. stmt,
  43. j))
  44. {
  45. GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
  46. "sq",
  47. _ ("Failure to bind %u-th SQL parameter\n"),
  48. i);
  49. if (SQLITE_OK !=
  50. sqlite3_reset (stmt))
  51. {
  52. GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
  53. "sq",
  54. _ ("Failure in sqlite3_reset (!)\n"));
  55. return GNUNET_SYSERR;
  56. }
  57. }
  58. GNUNET_assert (0 != params[i].num_params);
  59. j += params[i].num_params;
  60. }
  61. return GNUNET_OK;
  62. }
  63. /**
  64. * Extract results from a query result according to the given specification.
  65. *
  66. * @param result result to process
  67. * @param[in,out] rs result specification to extract for
  68. * @return
  69. * #GNUNET_OK if all results could be extracted
  70. * #GNUNET_SYSERR if a result was invalid (non-existing field)
  71. */
  72. int
  73. GNUNET_SQ_extract_result (sqlite3_stmt *result,
  74. struct GNUNET_SQ_ResultSpec *rs)
  75. {
  76. unsigned int j = 0;
  77. for (unsigned int i = 0; NULL != rs[i].conv; i++)
  78. {
  79. if (NULL == rs[i].result_size)
  80. rs[i].result_size = &rs[i].dst_size;
  81. if (GNUNET_OK !=
  82. rs[i].conv (rs[i].cls,
  83. result,
  84. j,
  85. rs[i].result_size,
  86. rs[i].dst))
  87. {
  88. for (unsigned int k = 0; k < i; k++)
  89. if (NULL != rs[k].cleaner)
  90. rs[k].cleaner (rs[k].cls);
  91. return GNUNET_SYSERR;
  92. }
  93. GNUNET_assert (0 != rs[i].num_params);
  94. j += rs[i].num_params;
  95. }
  96. return GNUNET_OK;
  97. }
  98. /**
  99. * Free all memory that was allocated in @a rs during
  100. * #GNUNET_SQ_extract_result().
  101. *
  102. * @param rs reult specification to clean up
  103. */
  104. void
  105. GNUNET_SQ_cleanup_result (struct GNUNET_SQ_ResultSpec *rs)
  106. {
  107. for (unsigned int i = 0; NULL != rs[i].conv; i++)
  108. if (NULL != rs[i].cleaner)
  109. rs[i].cleaner (rs[i].cls);
  110. }
  111. /**
  112. * Reset @a stmt and log error.
  113. *
  114. * @param dbh database handle
  115. * @param stmt statement to reset
  116. */
  117. void
  118. GNUNET_SQ_reset (sqlite3 *dbh,
  119. sqlite3_stmt *stmt)
  120. {
  121. if (SQLITE_OK !=
  122. sqlite3_reset (stmt))
  123. GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
  124. "sqlite",
  125. _ ("Failed to reset sqlite statement with error: %s\n"),
  126. sqlite3_errmsg (dbh));
  127. }
  128. /* end of sq.c */