From: Jiří Filipovič Date: Fri, 24 Feb 2012 14:28:03 +0000 (+0000) Subject: Connection string is stored in db context + gather for db name and host. X-Git-Tag: gridsite-core_R_1_7_17~23 X-Git-Url: http://scientific.zcu.cz/git/?a=commitdiff_plain;h=16ff857f19678a64bcab7c1332a2b0fdc202c26e;p=jra1mw.git Connection string is stored in db context + gather for db name and host. --- diff --git a/org.glite.lbjp-common.db/interface/db-int.h b/org.glite.lbjp-common.db/interface/db-int.h index 37e314f..e774621 100644 --- a/org.glite.lbjp-common.db/interface/db-int.h +++ b/org.glite.lbjp-common.db/interface/db-int.h @@ -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; diff --git a/org.glite.lbjp-common.db/interface/db.h b/org.glite.lbjp-common.db/interface/db.h index 7172f4f..031dda3 100644 --- a/org.glite.lbjp-common.db/interface/db.h +++ b/org.glite.lbjp-common.db/interface/db.h @@ -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); diff --git a/org.glite.lbjp-common.db/src/db.c b/org.glite.lbjp-common.db/src/db.c index d4a1760..1bdc5ba 100644 --- a/org.glite.lbjp-common.db/src/db.c +++ b/org.glite.lbjp-common.db/src/db.c @@ -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))