gnunet_pq_lib.h 19 KB

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