Browse Source

modify GNUNET_PQ_connect_with_cfg to enable flexible loading of .sql files

Christian Grothoff 4 years ago
parent
commit
2fd320bc72

+ 1 - 1
contrib/build-common

@@ -1 +1 @@
-Subproject commit 1915a74bbb4cd2ae9bc541a382dfebc37064a2fd
+Subproject commit d81bbfabc2538932f631d3946bd6a9b95182b4f2

+ 1 - 0
src/datacache/plugin_datacache_postgres.c

@@ -123,6 +123,7 @@ init_connection (struct Plugin *plugin)
 
   plugin->dbh = GNUNET_PQ_connect_with_cfg (plugin->env->cfg,
                                             "datacache-postgres",
+                                            NULL,
                                             es,
                                             ps);
   if (NULL == plugin->dbh)

+ 1 - 0
src/datastore/plugin_datastore_postgres.c

@@ -172,6 +172,7 @@ init_connection (struct Plugin *plugin)
 
   plugin->dbh = GNUNET_PQ_connect_with_cfg (plugin->env->cfg,
                                             "datastore-postgres",
+                                            NULL,
                                             es,
                                             ps);
   if (NULL == plugin->dbh)

+ 5 - 0
src/include/gnunet_pq_lib.h

@@ -742,8 +742,12 @@ GNUNET_PQ_connect (const char *config_str,
  * statements in @a es are executed whenever we (re)connect to the
  * database, and that the prepared statements in @a ps are "ready".
  *
+ * The caller does not have to ensure that @a es and @a ps remain allocated
+ * and initialized in memory until #GNUNET_PQ_disconnect() is called, as a copy will be made.
+ *
  * @param cfg configuration
  * @param section configuration section to use to get Postgres configuration options
+ * @param load_path_suffix suffix to append to the SQL_DIR in the configuration
  * @param es #GNUNET_PQ_PREPARED_STATEMENT_END-terminated
  *            array of statements to execute upon EACH connection, can be NULL
  * @param ps array of prepared statements to prepare, can be NULL
@@ -752,6 +756,7 @@ GNUNET_PQ_connect (const char *config_str,
 struct GNUNET_PQ_Context *
 GNUNET_PQ_connect_with_cfg (const struct GNUNET_CONFIGURATION_Handle *cfg,
                             const char *section,
+                            const char *load_path,
                             const struct GNUNET_PQ_ExecuteStatement *es,
                             const struct GNUNET_PQ_PreparedStatement *ps);
 

+ 1 - 0
src/namecache/plugin_namecache_postgres.c

@@ -113,6 +113,7 @@ database_setup (struct Plugin *plugin)
 
     plugin->dbh = GNUNET_PQ_connect_with_cfg (plugin->cfg,
                                               "namecache-postgres",
+                                              NULL,
                                               es,
                                               ps);
   }

+ 1 - 0
src/namestore/plugin_namestore_postgres.c

@@ -158,6 +158,7 @@ database_setup (struct Plugin *plugin)
 
     plugin->dbh = GNUNET_PQ_connect_with_cfg (plugin->cfg,
                                               "namestore-postgres",
+                                              NULL,
                                               es,
                                               ps);
   }

+ 17 - 9
src/pq/pq_connect.c

@@ -267,12 +267,12 @@ GNUNET_PQ_reconnect (struct GNUNET_PQ_Context *db)
  * statements in @a es are executed whenever we (re)connect to the
  * database, and that the prepared statements in @a ps are "ready".
  *
- * The caller MUST ensure that @a es and @a ps remain allocated and
- * initialized in memory until #GNUNET_PQ_disconnect() is called,
- * as they may be needed repeatedly and no copy will be made.
+ * The caller does not have to ensure that @a es and @a ps remain allocated
+ * and initialized in memory until #GNUNET_PQ_disconnect() is called, as a copy will be made.
  *
  * @param cfg configuration
  * @param section configuration section to use to get Postgres configuration options
+ * @param load_path_suffix suffix to append to the SQL_DIR in the configuration
  * @param es #GNUNET_PQ_PREPARED_STATEMENT_END-terminated
  *            array of statements to execute upon EACH connection, can be NULL
  * @param ps array of prepared statements to prepare, can be NULL
@@ -281,12 +281,14 @@ GNUNET_PQ_reconnect (struct GNUNET_PQ_Context *db)
 struct GNUNET_PQ_Context *
 GNUNET_PQ_connect_with_cfg (const struct GNUNET_CONFIGURATION_Handle *cfg,
                             const char *section,
+                            const char *load_path_suffix,
                             const struct GNUNET_PQ_ExecuteStatement *es,
                             const struct GNUNET_PQ_PreparedStatement *ps)
 {
   struct GNUNET_PQ_Context *db;
   char *conninfo;
   char *load_path;
+  char *sp;
 
   if (GNUNET_OK !=
       GNUNET_CONFIGURATION_get_value_string (cfg,
@@ -294,17 +296,23 @@ GNUNET_PQ_connect_with_cfg (const struct GNUNET_CONFIGURATION_Handle *cfg,
                                              "CONFIG",
                                              &conninfo))
     conninfo = NULL;
-  if (GNUNET_OK !=
-      GNUNET_CONFIGURATION_get_value_string (cfg,
-                                             section,
-                                             "SQL_PATH",
-                                             &load_path))
-    load_path = NULL;
+  load_path = NULL;
+  sp = NULL;
+  if (GNUNET_OK ==
+      GNUNET_CONFIGURATION_get_value_filename (cfg,
+                                               section,
+                                               "SQL_DIR",
+                                               &sp))
+    GNUNET_asprintf (&load_path,
+                     "%s%s",
+                     sp,
+                     load_path_suffix);
   db = GNUNET_PQ_connect (conninfo == NULL ? "" : conninfo,
                           load_path,
                           es,
                           ps);
   GNUNET_free_non_null (load_path);
+  GNUNET_free_non_null (sp);
   GNUNET_free_non_null (conninfo);
   return db;
 }