pq_prepare.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 pq/pq_prepare.c
  18. * @brief functions to connect to libpq (PostGres)
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "gnunet_pq_lib.h"
  24. /**
  25. * Create a `struct GNUNET_PQ_PreparedStatement`.
  26. *
  27. * @param name name of the statement
  28. * @param sql actual SQL statement
  29. * @param num_args number of arguments in the statement
  30. * @return initialized struct
  31. */
  32. struct GNUNET_PQ_PreparedStatement
  33. GNUNET_PQ_make_prepare (const char *name,
  34. const char *sql,
  35. unsigned int num_args)
  36. {
  37. struct GNUNET_PQ_PreparedStatement ps = {
  38. .name = name,
  39. .sql = sql,
  40. .num_arguments = num_args
  41. };
  42. return ps;
  43. }
  44. /**
  45. * Request creation of prepared statements @a ps from Postgres.
  46. *
  47. * @param connection connection to prepare the statements for
  48. * @param ps #GNUNET_PQ_PREPARED_STATEMENT_END-terminated array of prepared
  49. * statements.
  50. * @return #GNUNET_OK on success,
  51. * #GNUNET_SYSERR on error
  52. */
  53. int
  54. GNUNET_PQ_prepare_statements (PGconn *connection,
  55. const struct GNUNET_PQ_PreparedStatement *ps)
  56. {
  57. for (unsigned int i=0;NULL != ps[i].name;i++)
  58. {
  59. PGresult *ret;
  60. GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
  61. "pq",
  62. "Preparing SQL statement `%s' as `%s'\n",
  63. ps[i].sql,
  64. ps[i].name);
  65. ret = PQprepare (connection,
  66. ps[i].name,
  67. ps[i].sql,
  68. ps[i].num_arguments,
  69. NULL);
  70. if (PGRES_COMMAND_OK != PQresultStatus (ret))
  71. {
  72. GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
  73. "pq",
  74. _("PQprepare (`%s' as `%s') failed with error: %s\n"),
  75. ps[i].sql,
  76. ps[i].name,
  77. PQerrorMessage (connection));
  78. PQclear (ret);
  79. return GNUNET_SYSERR;
  80. }
  81. PQclear (ret);
  82. }
  83. return GNUNET_OK;
  84. }
  85. /* end of pq/pq_prepare.c */