sq_exec.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2018 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_exec.c
  18. * @brief helper functions for executing SQL statements
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_sq_lib.h"
  23. /**
  24. * Create a `struct GNUNET_SQ_ExecuteStatement` where errors are fatal.
  25. *
  26. * @param sql actual SQL statement
  27. * @return initialized struct
  28. */
  29. struct GNUNET_SQ_ExecuteStatement
  30. GNUNET_SQ_make_execute (const char *sql)
  31. {
  32. struct GNUNET_SQ_ExecuteStatement es = {
  33. .sql = sql,
  34. .ignore_errors = GNUNET_NO
  35. };
  36. return es;
  37. }
  38. /**
  39. * Create a `struct GNUNET_SQ_ExecuteStatement` where errors should
  40. * be tolerated.
  41. *
  42. * @param sql actual SQL statement
  43. * @return initialized struct
  44. */
  45. struct GNUNET_SQ_ExecuteStatement
  46. GNUNET_SQ_make_try_execute (const char *sql)
  47. {
  48. struct GNUNET_SQ_ExecuteStatement es = {
  49. .sql = sql,
  50. .ignore_errors = GNUNET_YES
  51. };
  52. return es;
  53. }
  54. /**
  55. * Request execution of an array of statements @a es from Postgres.
  56. *
  57. * @param dbh database to execute the statements over
  58. * @param es #GNUNET_PQ_PREPARED_STATEMENT_END-terminated array of prepared
  59. * statements.
  60. * @return #GNUNET_OK on success (modulo statements where errors can be ignored)
  61. * #GNUNET_SYSERR on error
  62. */
  63. int
  64. GNUNET_SQ_exec_statements (sqlite3 *dbh,
  65. const struct GNUNET_SQ_ExecuteStatement *es)
  66. {
  67. for (unsigned int i=0;NULL != es[i].sql;i++)
  68. {
  69. char *emsg = NULL;
  70. if (SQLITE_OK !=
  71. sqlite3_exec (dbh,
  72. es[i].sql,
  73. NULL,
  74. NULL,
  75. &emsg))
  76. {
  77. if (es[i].ignore_errors)
  78. {
  79. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  80. "Failed to run SQL `%s': %s\n",
  81. es[i].sql,
  82. emsg);
  83. }
  84. else
  85. {
  86. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  87. "Failed to run SQL `%s': %s\n",
  88. es[i].sql,
  89. emsg);
  90. sqlite3_free (emsg);
  91. return GNUNET_SYSERR;
  92. }
  93. sqlite3_free (emsg);
  94. }
  95. }
  96. return GNUNET_OK;
  97. }
  98. /* end of sq_exec */