Connection string is stored in db context + gather for db name and host.
authorJiří Filipovič <fila@ics.muni.cz>
Fri, 24 Feb 2012 14:28:03 +0000 (14:28 +0000)
committerJiří Filipovič <fila@ics.muni.cz>
Fri, 24 Feb 2012 14:28:03 +0000 (14:28 +0000)
org.glite.lbjp-common.db/interface/db-int.h
org.glite.lbjp-common.db/interface/db.h
org.glite.lbjp-common.db/src/db.c

index 37e314f..e774621 100644 (file)
@@ -31,6 +31,7 @@ struct glite_lbu_DBContext_s {
        } err;
        int caps;
        char *log_category;
+       char *connection_string;
 };
 typedef struct glite_lbu_DBContext_s glite_lbu_DBContext_t;
 
index 7172f4f..031dda3 100644 (file)
@@ -329,6 +329,30 @@ time_t glite_lbu_DBToTime(glite_lbu_DBContext ctx, const char *str);
 double glite_lbu_DBToTimestamp(glite_lbu_DBContext ctx, const char *str);
 
 
+/**
+ * Get the connection string into the database
+ *
+ * \return newly allocated connection string
+ * */
+char *glite_lbu_DBGetConnectionString(glite_lbu_DBContext ctx);
+
+
+/**
+ * Get the hostname of the database
+ *
+ * \return newly allocated host
+* */
+char *glite_lbu_DBGetHost(glite_lbu_DBContext ctx);
+
+
+/**
+ * Get the name of the database
+ *
+ * \return newly allocated name
+* */
+char *glite_lbu_DBGetName(glite_lbu_DBContext ctx);
+
+
 /* Generic helper time convert functions. */
 void glite_lbu_TimeToStr(time_t t, char **str);
 void glite_lbu_TimestampToStr(double t, char **str);
index d4a1760..1bdc5ba 100644 (file)
@@ -219,6 +219,7 @@ int glite_lbu_InitDBContext(glite_lbu_DBContext *ctx, int backend, char *log_cat
                (*ctx)->backend = backend;
                (*ctx)->log_category = log_category;
        }
+
        return ret;
 }
 
@@ -234,6 +235,7 @@ void glite_lbu_FreeDBContext(glite_lbu_DBContext ctx) {
 
 int glite_lbu_DBConnect(glite_lbu_DBContext ctx, const char *cs) {
        if (!VALID(ctx->backend)) return EINVAL;
+       ctx->connection_string = strdup(cs);
        return backends[ctx->backend]->connect(ctx, cs);
 }
 
@@ -357,6 +359,63 @@ double glite_lbu_DBToTimestamp(glite_lbu_DBContext ctx, const char *str) {
        return backends[ctx->backend]->DBToTimestamp(str);
 }
 
+char *glite_lbu_DBGetConnectionString(glite_lbu_DBContext ctx) {
+       return strdup(ctx->connection_string);
+}
+
+char *glite_lbu_DBGetHost(glite_lbu_DBContext ctx) {
+       //XXX this is same for msql and pg, move it into backends, when some difference occurs
+
+       char *host, *buf, *slash, *at, *colon;
+       if (! ctx->connection_string)
+               return NULL;
+
+       buf = strdup(ctx->connection_string);
+       slash = strchr(buf,'/');
+        at = strrchr(buf,'@');
+        colon = strrchr(buf,':');
+
+       if (!slash || !at || !colon){
+               //XXX should never happen when DB is opened
+               free(buf);
+               return NULL;
+       }
+
+       *slash = *at = *colon = 0;
+
+       host = strdup(at+1);
+       free(buf);
+       
+       return host;
+}
+
+char *glite_lbu_DBGetName(glite_lbu_DBContext ctx) {
+        //XXX this is same for msql and pg, move it into backends, when some difference occurs
+
+        char *name, *buf, *slash, *at, *colon;
+        if (! ctx->connection_string)
+                return NULL;
+
+        buf = strdup(ctx->connection_string);
+        slash = strchr(buf,'/');
+        at = strrchr(buf,'@');
+        colon = strrchr(buf,':');
+
+        if (!slash || !at || !colon){
+                //XXX should never happen when DB is opened
+                free(buf);
+                return NULL;
+        }
+
+        *slash = *at = *colon = 0;
+
+        name = strdup(colon+1);
+        free(buf);
+
+        return name;
+}
+
+
 
 #define STATUS(CTX) ((CTX)->err.code)
 #define CLR_ERR(CTX) glite_lbu_DBClearError((CTX))