aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorÖzgür Kesim <oec-taler@kesim.org>2023-07-28 15:18:18 +0200
committerÖzgür Kesim <oec-taler@kesim.org>2023-07-28 15:19:06 +0200
commit3e5ca0f158bd671e56690e034afcc26a1d10b00a (patch)
tree4744be3335f9f3769b735d66ca98cec29c0bff9c /src
parent526a111bd9dc0c83def348ebed63d527b7bbb4f4 (diff)
downloadgnunet-3e5ca0f158bd671e56690e034afcc26a1d10b00a.tar.gz
gnunet-3e5ca0f158bd671e56690e034afcc26a1d10b00a.zip
pq: added oid-by-name lookup API
NEWS: Added GNUNET_PQ_get_oid_by_name
Diffstat (limited to 'src')
-rw-r--r--src/include/gnunet_pq_lib.h35
-rw-r--r--src/pq/pq_connect.c66
-rw-r--r--src/pq/test_pq.c35
3 files changed, 135 insertions, 1 deletions
diff --git a/src/include/gnunet_pq_lib.h b/src/include/gnunet_pq_lib.h
index edb0519b0..3b960df70 100644
--- a/src/include/gnunet_pq_lib.h
+++ b/src/include/gnunet_pq_lib.h
@@ -28,6 +28,7 @@
28 28
29#include <libpq-fe.h> 29#include <libpq-fe.h>
30#include <stdint.h> 30#include <stdint.h>
31#include "gnunet_common.h"
31#include "gnunet_time_lib.h" 32#include "gnunet_time_lib.h"
32#include "gnunet_util_lib.h" 33#include "gnunet_util_lib.h"
33#include "gnunet_db_lib.h" 34#include "gnunet_db_lib.h"
@@ -198,7 +199,7 @@ enum GNUNET_PQ_DataTypes
198}; 199};
199 200
200/** 201/**
201 * Returns the oid for a given datatype 202 * Returns the oid for a given, basic datatype
202 * 203 *
203 * @param db The db-connection 204 * @param db The db-connection
204 * @param typ the Datatype 205 * @param typ the Datatype
@@ -210,6 +211,38 @@ GNUNET_PQ_get_oid (
210 enum GNUNET_PQ_DataTypes typ); 211 enum GNUNET_PQ_DataTypes typ);
211 212
212/** 213/**
214 * Returns the oid for a given datatype by name.
215 *
216 * @param db The db-connection
217 * @param name The name of the datatype
218 * @param[out] oid The OID of the datatype.
219 * @return GNUNET_OK when the datatype was found, GNUNET_SYSERR otherwise
220 */
221enum GNUNET_GenericReturnValue
222GNUNET_PQ_get_oid_by_name (
223 struct GNUNET_PQ_Context *db,
224 const char *name,
225 Oid *oid);
226
227
228/**
229 * The header for a postgresql array in binary format. note that this a
230 * simplified special case of the general structure (which contains pointers),
231 * as we only support one-dimensional arrays.
232 *
233 * Note that all values must be in network-byte-order.
234 */
235struct GNUNET_PQ_ArrayHeader_P
236{
237 uint32_t ndim; /* number of dimensions. we only support ndim = 1 */
238 uint32_t has_null;
239 uint32_t oid; /* oid of the elements */
240 uint32_t dim; /* size of the array */
241 uint32_t lbound; /* index value of first element in the db (default: 1). */
242} __attribute__((packed));
243
244
245/**
213 * Generate query parameter for an array of bool in host byte order. 246 * Generate query parameter for an array of bool in host byte order.
214 * 247 *
215 * @param num Number of elements in @a elements 248 * @param num Number of elements in @a elements
diff --git a/src/pq/pq_connect.c b/src/pq/pq_connect.c
index f30f989ea..ba272bce4 100644
--- a/src/pq/pq_connect.c
+++ b/src/pq/pq_connect.c
@@ -430,6 +430,45 @@ GNUNET_PQ_get_oid (
430} 430}
431 431
432 432
433enum GNUNET_GenericReturnValue
434GNUNET_PQ_get_oid_by_name (
435 struct GNUNET_PQ_Context *db,
436 const char *name,
437 Oid *oid)
438{
439 char *typname;
440 enum GNUNET_DB_QueryStatus qs;
441 struct GNUNET_PQ_QueryParam params[] = {
442 GNUNET_PQ_query_param_string (name),
443 GNUNET_PQ_query_param_end
444 };
445 struct GNUNET_PQ_ResultSpec rs[] = {
446 GNUNET_PQ_result_spec_string ("typname",
447 &typname),
448 GNUNET_PQ_result_spec_uint32 ("oid",
449 oid),
450 GNUNET_PQ_result_spec_end
451 };
452
453 GNUNET_assert (NULL != db);
454
455 /* Initialize to Oid(0) (= unknown) */
456 *oid = 0;
457
458 qs = GNUNET_PQ_eval_prepared_singleton_select (db,
459 "gnunet_pq_get_oid_by_name",
460 params,
461 rs);
462 if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != qs)
463 return GNUNET_SYSERR;
464
465 if (0 != strcasecmp (typname, name))
466 return GNUNET_SYSERR;
467
468 return GNUNET_OK;
469}
470
471
433void 472void
434GNUNET_PQ_reconnect (struct GNUNET_PQ_Context *db) 473GNUNET_PQ_reconnect (struct GNUNET_PQ_Context *db)
435{ 474{
@@ -530,6 +569,33 @@ GNUNET_PQ_reconnect (struct GNUNET_PQ_Context *db)
530 return; 569 return;
531 } 570 }
532 571
572 /* Prepare statement for OID lookup by name */
573 {
574 PGresult *res;
575
576 res = PQprepare (db->conn,
577 "gnunet_pq_get_oid_by_name",
578 "SELECT"
579 " typname, oid"
580 " FROM pg_type"
581 " WHERE typname = $1"
582 " LIMIT 1",
583 1,
584 NULL);
585 if (PGRES_COMMAND_OK != PQresultStatus (res))
586 {
587 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
588 "Failed to run SQL statement prepare OID lookups: %s/%s\n",
589 PQresultErrorMessage (res),
590 PQerrorMessage (db->conn));
591 PQclear (res);
592 PQfinish (db->conn);
593 db->conn = NULL;
594 return;
595 }
596 PQclear (res);
597 }
598
533 if (NULL != db->auto_suffix) 599 if (NULL != db->auto_suffix)
534 { 600 {
535 PGresult *res; 601 PGresult *res;
diff --git a/src/pq/test_pq.c b/src/pq/test_pq.c
index 69c986b6e..a20ff9ddd 100644
--- a/src/pq/test_pq.c
+++ b/src/pq/test_pq.c
@@ -554,6 +554,41 @@ main (int argc,
554 GNUNET_PQ_disconnect (db); 554 GNUNET_PQ_disconnect (db);
555 return ret; 555 return ret;
556 } 556 }
557
558 /* ensure oid lookup works */
559 {
560 enum GNUNET_GenericReturnValue ret;
561 Oid oid;
562
563 ret = GNUNET_PQ_get_oid_by_name (db, "int8", &oid);
564
565 if (GNUNET_OK != ret)
566 {
567 fprintf (stderr,
568 "Cannot lookup oid for int8: %s\n",
569 PQerrorMessage (db->conn));
570 GNUNET_break (0);
571 GNUNET_PQ_disconnect (db);
572 return 77; /* signal test was skipped */
573 }
574
575 PQexec (db->conn, "CREATE TYPE foo AS (foo int, bar int);");
576
577 ret = GNUNET_PQ_get_oid_by_name (db, "foo", &oid);
578 if (GNUNET_OK != ret)
579 {
580 fprintf (stderr,
581 "Cannot lookup oid for foo: %s\n",
582 PQerrorMessage (db->conn));
583 GNUNET_break (0);
584 GNUNET_PQ_disconnect (db);
585 return 77; /* signal test was skipped */
586 }
587
588 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
589 "got oid %d for type foo\n", oid);
590 }
591
557 GNUNET_SCHEDULER_run (&sched_tests, 592 GNUNET_SCHEDULER_run (&sched_tests,
558 NULL); 593 NULL);
559 if (0 != ret) 594 if (0 != ret)