gnunet_pq_lib.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2016, 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 include/gnunet_pq_lib.h
  18. * @brief helper functions for Postgres DB interactions
  19. * @author Christian Grothoff
  20. */
  21. #ifndef GNUNET_PQ_LIB_H
  22. #define GNUNET_PQ_LIB_H
  23. #include <libpq-fe.h>
  24. #include "gnunet_util_lib.h"
  25. #include "gnunet_db_lib.h"
  26. /* ************************* pq_query_helper.c functions ************************ */
  27. /**
  28. * Function called to convert input argument into SQL parameters.
  29. *
  30. * @param cls closure
  31. * @param data pointer to input argument
  32. * @param data_len number of bytes in @a data (if applicable)
  33. * @param[out] param_values SQL data to set
  34. * @param[out] param_lengths SQL length data to set
  35. * @param[out] param_formats SQL format data to set
  36. * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
  37. * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
  38. * @param scratch_length number of entries left in @a scratch
  39. * @return -1 on error, number of offsets used in @a scratch otherwise
  40. */
  41. typedef int
  42. (*GNUNET_PQ_QueryConverter) (void *cls,
  43. const void *data,
  44. size_t data_len,
  45. void *param_values[],
  46. int param_lengths[],
  47. int param_formats[],
  48. unsigned int param_length,
  49. void *scratch[],
  50. unsigned int scratch_length);
  51. /**
  52. * @brief Description of a DB query parameter.
  53. */
  54. struct GNUNET_PQ_QueryParam
  55. {
  56. /**
  57. * Function for how to handle this type of entry.
  58. */
  59. GNUNET_PQ_QueryConverter conv;
  60. /**
  61. * Closure for @e conv.
  62. */
  63. void *conv_cls;
  64. /**
  65. * Data or NULL.
  66. */
  67. const void *data;
  68. /**
  69. * Size of @e data
  70. */
  71. size_t size;
  72. /**
  73. * Number of parameters eaten by this operation.
  74. */
  75. unsigned int num_params;
  76. };
  77. /**
  78. * End of query parameter specification.
  79. */
  80. #define GNUNET_PQ_query_param_end \
  81. { \
  82. NULL, NULL, NULL, 0, 0 \
  83. }
  84. /**
  85. * Generate query parameter for a buffer @a ptr of
  86. * @a ptr_size bytes.
  87. *
  88. * @param ptr pointer to the query parameter to pass
  89. * @oaran ptr_size number of bytes in @a ptr
  90. */
  91. struct GNUNET_PQ_QueryParam
  92. GNUNET_PQ_query_param_fixed_size (const void *ptr, size_t ptr_size);
  93. /**
  94. * Generate query parameter for a string.
  95. *
  96. * @param ptr pointer to the string query parameter to pass
  97. */
  98. struct GNUNET_PQ_QueryParam
  99. GNUNET_PQ_query_param_string (const char *ptr);
  100. /**
  101. * Generate fixed-size query parameter with size determined
  102. * by variable type.
  103. *
  104. * @param x pointer to the query parameter to pass.
  105. */
  106. #define GNUNET_PQ_query_param_auto_from_type(x) \
  107. GNUNET_PQ_query_param_fixed_size ((x), sizeof(*(x)))
  108. /**
  109. * Generate query parameter for an RSA public key. The
  110. * database must contain a BLOB type in the respective position.
  111. *
  112. * @param x the query parameter to pass.
  113. */
  114. struct GNUNET_PQ_QueryParam
  115. GNUNET_PQ_query_param_rsa_public_key (
  116. const struct GNUNET_CRYPTO_RsaPublicKey *x);
  117. /**
  118. * Generate query parameter for an RSA signature. The
  119. * database must contain a BLOB type in the respective position.
  120. *
  121. * @param x the query parameter to pass
  122. */
  123. struct GNUNET_PQ_QueryParam
  124. GNUNET_PQ_query_param_rsa_signature (
  125. const struct GNUNET_CRYPTO_RsaSignature *x);
  126. /**
  127. * Generate query parameter for an absolute time value.
  128. * The database must store a 64-bit integer.
  129. *
  130. * @param x pointer to the query parameter to pass
  131. */
  132. struct GNUNET_PQ_QueryParam
  133. GNUNET_PQ_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x);
  134. /**
  135. * Generate query parameter for an absolute time value.
  136. * The database must store a 64-bit integer.
  137. *
  138. * @param x pointer to the query parameter to pass
  139. */
  140. struct GNUNET_PQ_QueryParam
  141. GNUNET_PQ_query_param_absolute_time_nbo (
  142. const struct GNUNET_TIME_AbsoluteNBO *x);
  143. /**
  144. * Generate query parameter for an uint16_t in host byte order.
  145. *
  146. * @param x pointer to the query parameter to pass
  147. */
  148. struct GNUNET_PQ_QueryParam
  149. GNUNET_PQ_query_param_uint16 (const uint16_t *x);
  150. /**
  151. * Generate query parameter for an uint32_t in host byte order.
  152. *
  153. * @param x pointer to the query parameter to pass
  154. */
  155. struct GNUNET_PQ_QueryParam
  156. GNUNET_PQ_query_param_uint32 (const uint32_t *x);
  157. /**
  158. * Generate query parameter for an uint16_t in host byte order.
  159. *
  160. * @param x pointer to the query parameter to pass
  161. */
  162. struct GNUNET_PQ_QueryParam
  163. GNUNET_PQ_query_param_uint64 (const uint64_t *x);
  164. /* ************************* pq_result_helper.c functions ************************ */
  165. /**
  166. * Extract data from a Postgres database @a result at row @a row.
  167. *
  168. * @param cls closure
  169. * @param result where to extract data from
  170. * @param int row to extract data from
  171. * @param fname name (or prefix) of the fields to extract from
  172. * @param[in,out] dst_size where to store size of result, may be NULL
  173. * @param[out] dst where to store the result
  174. * @return
  175. * #GNUNET_YES if all results could be extracted
  176. * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
  177. */
  178. typedef int
  179. (*GNUNET_PQ_ResultConverter) (void *cls,
  180. PGresult *result,
  181. int row,
  182. const char *fname,
  183. size_t *dst_size,
  184. void *dst);
  185. /**
  186. * Function called to clean up memory allocated
  187. * by a #GNUNET_PQ_ResultConverter.
  188. *
  189. * @param cls closure
  190. * @param rd result data to clean up
  191. */
  192. typedef void
  193. (*GNUNET_PQ_ResultCleanup) (void *cls,
  194. void *rd);
  195. /**
  196. * @brief Description of a DB result cell.
  197. */
  198. struct GNUNET_PQ_ResultSpec
  199. {
  200. /**
  201. * What is the format of the result?
  202. */
  203. GNUNET_PQ_ResultConverter conv;
  204. /**
  205. * Function to clean up result data, NULL if cleanup is
  206. * not necessary.
  207. */
  208. GNUNET_PQ_ResultCleanup cleaner;
  209. /**
  210. * Closure for @e conv and @e cleaner.
  211. */
  212. void *cls;
  213. /**
  214. * Destination for the data.
  215. */
  216. void *dst;
  217. /**
  218. * Allowed size for the data, 0 for variable-size
  219. * (in this case, the type of @e dst is a `void **`
  220. * and we need to allocate a buffer of the right size).
  221. */
  222. size_t dst_size;
  223. /**
  224. * Field name of the desired result.
  225. */
  226. const char *fname;
  227. /**
  228. * Where to store actual size of the result.
  229. */
  230. size_t *result_size;
  231. };
  232. /**
  233. * End of result parameter specification.
  234. *
  235. * @return array last entry for the result specification to use
  236. */
  237. #define GNUNET_PQ_result_spec_end \
  238. { \
  239. NULL, NULL, NULL, NULL, 0, NULL, NULL \
  240. }
  241. /**
  242. * Variable-size result expected.
  243. *
  244. * @param name name of the field in the table
  245. * @param[out] dst where to store the result, allocated
  246. * @param[out] sptr where to store the size of @a dst
  247. * @return array entry for the result specification to use
  248. */
  249. struct GNUNET_PQ_ResultSpec
  250. GNUNET_PQ_result_spec_variable_size (const char *name,
  251. void **dst,
  252. size_t *sptr);
  253. /**
  254. * Fixed-size result expected.
  255. *
  256. * @param name name of the field in the table
  257. * @param[out] dst where to store the result
  258. * @param dst_size number of bytes in @a dst
  259. * @return array entry for the result specification to use
  260. */
  261. struct GNUNET_PQ_ResultSpec
  262. GNUNET_PQ_result_spec_fixed_size (const char *name, void *dst, size_t dst_size);
  263. /**
  264. * We expect a fixed-size result, with size determined by the type of `* dst`
  265. *
  266. * @param name name of the field in the table
  267. * @param dst point to where to store the result, type fits expected result size
  268. * @return array entry for the result specification to use
  269. */
  270. #define GNUNET_PQ_result_spec_auto_from_type(name, dst) \
  271. GNUNET_PQ_result_spec_fixed_size (name, (dst), sizeof(*(dst)))
  272. /**
  273. * 0-terminated string expected.
  274. *
  275. * @param name name of the field in the table
  276. * @param[out] dst where to store the result, allocated
  277. * @return array entry for the result specification to use
  278. */
  279. struct GNUNET_PQ_ResultSpec
  280. GNUNET_PQ_result_spec_string (const char *name, char **dst);
  281. /**
  282. * RSA public key expected.
  283. *
  284. * @param name name of the field in the table
  285. * @param[out] rsa where to store the result
  286. * @return array entry for the result specification to use
  287. */
  288. struct GNUNET_PQ_ResultSpec
  289. GNUNET_PQ_result_spec_rsa_public_key (const char *name,
  290. struct GNUNET_CRYPTO_RsaPublicKey **rsa);
  291. /**
  292. * RSA signature expected.
  293. *
  294. * @param name name of the field in the table
  295. * @param[out] sig where to store the result;
  296. * @return array entry for the result specification to use
  297. */
  298. struct GNUNET_PQ_ResultSpec
  299. GNUNET_PQ_result_spec_rsa_signature (const char *name,
  300. struct GNUNET_CRYPTO_RsaSignature **sig);
  301. /**
  302. * Absolute time expected.
  303. *
  304. * @param name name of the field in the table
  305. * @param[out] at where to store the result
  306. * @return array entry for the result specification to use
  307. */
  308. struct GNUNET_PQ_ResultSpec
  309. GNUNET_PQ_result_spec_absolute_time (const char *name,
  310. struct GNUNET_TIME_Absolute *at);
  311. /**
  312. * Absolute time expected.
  313. *
  314. * @param name name of the field in the table
  315. * @param[out] at where to store the result
  316. * @return array entry for the result specification to use
  317. */
  318. struct GNUNET_PQ_ResultSpec
  319. GNUNET_PQ_result_spec_absolute_time_nbo (const char *name,
  320. struct GNUNET_TIME_AbsoluteNBO *at);
  321. /**
  322. * uint16_t expected.
  323. *
  324. * @param name name of the field in the table
  325. * @param[out] u16 where to store the result
  326. * @return array entry for the result specification to use
  327. */
  328. struct GNUNET_PQ_ResultSpec
  329. GNUNET_PQ_result_spec_uint16 (const char *name, uint16_t *u16);
  330. /**
  331. * uint32_t expected.
  332. *
  333. * @param name name of the field in the table
  334. * @param[out] u32 where to store the result
  335. * @return array entry for the result specification to use
  336. */
  337. struct GNUNET_PQ_ResultSpec
  338. GNUNET_PQ_result_spec_uint32 (const char *name, uint32_t *u32);
  339. /**
  340. * uint64_t expected.
  341. *
  342. * @param name name of the field in the table
  343. * @param[out] u64 where to store the result
  344. * @return array entry for the result specification to use
  345. */
  346. struct GNUNET_PQ_ResultSpec
  347. GNUNET_PQ_result_spec_uint64 (const char *name, uint64_t *u64);
  348. /* ************************* pq.c functions ************************ */
  349. /**
  350. * Postgres context.
  351. */
  352. struct GNUNET_PQ_Context;
  353. /**
  354. * Execute a prepared statement.
  355. *
  356. * @param db database context
  357. * @param name name of the prepared statement
  358. * @param params parameters to the statement
  359. * @return postgres result
  360. * @deprecated (should become an internal API)
  361. */
  362. PGresult *
  363. GNUNET_PQ_exec_prepared (struct GNUNET_PQ_Context *db,
  364. const char *name,
  365. const struct GNUNET_PQ_QueryParam *params);
  366. /**
  367. * Extract results from a query result according to the given specification.
  368. *
  369. * @param result result to process
  370. * @param[in,out] rs result specification to extract for
  371. * @param row row from the result to extract
  372. * @return
  373. * #GNUNET_YES if all results could be extracted
  374. * #GNUNET_SYSERR if a result was invalid (non-existing field)
  375. * @deprecated (should become an internal API)
  376. */
  377. int
  378. GNUNET_PQ_extract_result (PGresult *result,
  379. struct GNUNET_PQ_ResultSpec *rs,
  380. int row);
  381. /**
  382. * Free all memory that was allocated in @a rs during
  383. * #GNUNET_PQ_extract_result().
  384. *
  385. * @param rs reult specification to clean up
  386. */
  387. void
  388. GNUNET_PQ_cleanup_result (struct GNUNET_PQ_ResultSpec *rs);
  389. /* ******************** pq_eval.c functions ************** */
  390. /**
  391. * Check the @a result's error code to see what happened.
  392. * Also logs errors.
  393. *
  394. * @param db database to execute the statement in
  395. * @param statement_name name of the statement that created @a result
  396. * @param result result to check
  397. * @return status code from the result, mapping PQ status
  398. * codes to `enum GNUNET_DB_QueryStatus`. Never
  399. * returns positive values as this function does
  400. * not look at the result set.
  401. * @deprecated (low level, let's see if we can do with just the high-level functions)
  402. */
  403. enum GNUNET_DB_QueryStatus
  404. GNUNET_PQ_eval_result (struct GNUNET_PQ_Context *db,
  405. const char *statement_name,
  406. PGresult *result);
  407. /**
  408. * Execute a named prepared @a statement that is NOT a SELECT
  409. * statement in @a connnection using the given @a params. Returns the
  410. * resulting session state.
  411. *
  412. * @param db database to execute the statement with
  413. * @param statement_name name of the statement
  414. * @param params parameters to give to the statement (#GNUNET_PQ_query_param_end-terminated)
  415. * @return status code from the result, mapping PQ status
  416. * codes to `enum GNUNET_DB_QueryStatus`. If the
  417. * statement was a DELETE or UPDATE statement, the
  418. * number of affected rows is returned; if the
  419. * statment was an INSERT statement, and no row
  420. * was added due to a UNIQUE violation, we return
  421. * zero; if INSERT was successful, we return one.
  422. */
  423. enum GNUNET_DB_QueryStatus
  424. GNUNET_PQ_eval_prepared_non_select (struct GNUNET_PQ_Context *db,
  425. const char *statement_name,
  426. const struct GNUNET_PQ_QueryParam *params);
  427. /**
  428. * Function to be called with the results of a SELECT statement
  429. * that has returned @a num_results results.
  430. *
  431. * @param cls closure
  432. * @param result the postgres result
  433. * @param num_result the number of results in @a result
  434. */
  435. typedef void
  436. (*GNUNET_PQ_PostgresResultHandler) (void *cls,
  437. PGresult *result,
  438. unsigned int num_results);
  439. /**
  440. * Execute a named prepared @a statement that is a SELECT statement
  441. * which may return multiple results in @a connection using the given
  442. * @a params. Call @a rh with the results. Returns the query
  443. * status including the number of results given to @a rh (possibly zero).
  444. * @a rh will not have been called if the return value is negative.
  445. *
  446. * @param db database to execute the statement with
  447. * @param statement_name name of the statement
  448. * @param params parameters to give to the statement (#GNUNET_PQ_query_param_end-terminated)
  449. * @param rh function to call with the result set, NULL to ignore
  450. * @param rh_cls closure to pass to @a rh
  451. * @return status code from the result, mapping PQ status
  452. * codes to `enum GNUNET_DB_QueryStatus`.
  453. */
  454. enum GNUNET_DB_QueryStatus
  455. GNUNET_PQ_eval_prepared_multi_select (struct GNUNET_PQ_Context *db,
  456. const char *statement_name,
  457. const struct GNUNET_PQ_QueryParam *params,
  458. GNUNET_PQ_PostgresResultHandler rh,
  459. void *rh_cls);
  460. /**
  461. * Execute a named prepared @a statement that is a SELECT statement
  462. * which must return a single result in @a connection using the given
  463. * @a params. Stores the result (if any) in @a rs, which the caller
  464. * must then clean up using #GNUNET_PQ_cleanup_result() if the return
  465. * value was #GNUNET_DB_STATUS_SUCCESS_ONE_RESULT. Returns the
  466. * resulting session status.
  467. *
  468. * @param db database to execute the statement with
  469. * @param statement_name name of the statement
  470. * @param params parameters to give to the statement (#GNUNET_PQ_query_param_end-terminated)
  471. * @param[in,out] rs result specification to use for storing the result of the query
  472. * @return status code from the result, mapping PQ status
  473. * codes to `enum GNUNET_DB_QueryStatus`.
  474. */
  475. enum GNUNET_DB_QueryStatus
  476. GNUNET_PQ_eval_prepared_singleton_select (struct GNUNET_PQ_Context *db,
  477. const char *statement_name,
  478. const struct
  479. GNUNET_PQ_QueryParam *params,
  480. struct GNUNET_PQ_ResultSpec *rs);
  481. /* ******************** pq_prepare.c functions ************** */
  482. /**
  483. * Information needed to prepare a list of SQL statements using
  484. * #GNUNET_PQ_prepare_statements().
  485. */
  486. struct GNUNET_PQ_PreparedStatement
  487. {
  488. /**
  489. * Name of the statement.
  490. */
  491. const char *name;
  492. /**
  493. * Actual SQL statement.
  494. */
  495. const char *sql;
  496. /**
  497. * Number of arguments included in @e sql.
  498. */
  499. unsigned int num_arguments;
  500. };
  501. /**
  502. * Terminator for prepared statement list.
  503. */
  504. #define GNUNET_PQ_PREPARED_STATEMENT_END \
  505. { \
  506. NULL, NULL, 0 \
  507. }
  508. /**
  509. * Create a `struct GNUNET_PQ_PreparedStatement`.
  510. *
  511. * @param name name of the statement
  512. * @param sql actual SQL statement
  513. * @param num_args number of arguments in the statement
  514. * @return initialized struct
  515. */
  516. struct GNUNET_PQ_PreparedStatement
  517. GNUNET_PQ_make_prepare (const char *name,
  518. const char *sql,
  519. unsigned int num_args);
  520. /**
  521. * Request creation of prepared statements @a ps from Postgres.
  522. *
  523. * @param db database to prepare the statements for
  524. * @param ps #GNUNET_PQ_PREPARED_STATEMENT_END-terminated array of prepared
  525. * statements.
  526. * @return #GNUNET_OK on success,
  527. * #GNUNET_SYSERR on error
  528. */
  529. int
  530. GNUNET_PQ_prepare_statements (struct GNUNET_PQ_Context *db,
  531. const struct GNUNET_PQ_PreparedStatement *ps);
  532. /* ******************** pq_exec.c functions ************** */
  533. /**
  534. * Information needed to run a list of SQL statements using
  535. * #GNUNET_PQ_exec_statements().
  536. */
  537. struct GNUNET_PQ_ExecuteStatement
  538. {
  539. /**
  540. * Actual SQL statement.
  541. */
  542. const char *sql;
  543. /**
  544. * Should we ignore errors?
  545. */
  546. int ignore_errors;
  547. };
  548. /**
  549. * Terminator for executable statement list.
  550. */
  551. #define GNUNET_PQ_EXECUTE_STATEMENT_END \
  552. { \
  553. NULL, GNUNET_SYSERR \
  554. }
  555. /**
  556. * Create a `struct GNUNET_PQ_ExecuteStatement` where errors are fatal.
  557. *
  558. * @param sql actual SQL statement
  559. * @return initialized struct
  560. */
  561. struct GNUNET_PQ_ExecuteStatement
  562. GNUNET_PQ_make_execute (const char *sql);
  563. /**
  564. * Create a `struct GNUNET_PQ_ExecuteStatement` where errors should
  565. * be tolerated.
  566. *
  567. * @param sql actual SQL statement
  568. * @return initialized struct
  569. */
  570. struct GNUNET_PQ_ExecuteStatement
  571. GNUNET_PQ_make_try_execute (const char *sql);
  572. /**
  573. * Request execution of an array of statements @a es from Postgres.
  574. *
  575. * @param pq database to execute the statements in
  576. * @param es #GNUNET_PQ_PREPARED_STATEMENT_END-terminated array of prepared
  577. * statements.
  578. * @return #GNUNET_OK on success (modulo statements where errors can be ignored)
  579. * #GNUNET_SYSERR on error
  580. */
  581. int
  582. GNUNET_PQ_exec_statements (struct GNUNET_PQ_Context *db,
  583. const struct GNUNET_PQ_ExecuteStatement *es);
  584. /* ******************** pq_connect.c functions ************** */
  585. /**
  586. * Create a connection to the Postgres database using @a config_str
  587. * for the configuration. Initialize logging via GNUnet's log
  588. * routines and disable Postgres's logger. Also ensures that the
  589. * statements in @a es are executed whenever we (re)connect to the
  590. * database, and that the prepared statements in @a ps are "ready".
  591. * If statements in @es fail that were created with
  592. * #GNUNET_PQ_make_execute(), then the entire operation fails.
  593. *
  594. * @param config_str configuration to use
  595. * @param es #GNUNET_PQ_PREPARED_STATEMENT_END-terminated
  596. * array of statements to execute upon EACH connection, can be NULL
  597. * @param ps array of prepared statements to prepare, can be NULL
  598. * @return NULL on error
  599. */
  600. struct GNUNET_PQ_Context *
  601. GNUNET_PQ_connect (const char *config_str,
  602. const struct GNUNET_PQ_ExecuteStatement *es,
  603. const struct GNUNET_PQ_PreparedStatement *ps);
  604. /**
  605. * Connect to a postgres database using the configuration
  606. * option "CONFIG" in @a section. Also ensures that the
  607. * statements in @a es are executed whenever we (re)connect to the
  608. * database, and that the prepared statements in @a ps are "ready".
  609. *
  610. * @param cfg configuration
  611. * @param section configuration section to use to get Postgres configuration options
  612. * @param es #GNUNET_PQ_PREPARED_STATEMENT_END-terminated
  613. * array of statements to execute upon EACH connection, can be NULL
  614. * @param ps array of prepared statements to prepare, can be NULL
  615. * @return the postgres handle, NULL on error
  616. */
  617. struct GNUNET_PQ_Context *
  618. GNUNET_PQ_connect_with_cfg (const struct GNUNET_CONFIGURATION_Handle *cfg,
  619. const char *section,
  620. const struct GNUNET_PQ_ExecuteStatement *es,
  621. const struct GNUNET_PQ_PreparedStatement *ps);
  622. /**
  623. * Reinitialize the database @a db if the connection is down.
  624. *
  625. * @param db database connection to reinitialize
  626. */
  627. void
  628. GNUNET_PQ_reconnect_if_down (struct GNUNET_PQ_Context *db);
  629. /**
  630. * Reinitialize the database @a db.
  631. *
  632. * @param db database connection to reinitialize
  633. */
  634. void
  635. GNUNET_PQ_reconnect (struct GNUNET_PQ_Context *db);
  636. /**
  637. * Disconnect from the database, destroying the prepared statements
  638. * and releasing other associated resources.
  639. *
  640. * @param db database handle to disconnect (will be free'd)
  641. */
  642. void
  643. GNUNET_PQ_disconnect (struct GNUNET_PQ_Context *db);
  644. #endif /* GNUNET_PQ_LIB_H_ */
  645. /* end of include/gnunet_pq_lib.h */