Finalize trio and db common modules:
authorFrantišek Dvořák <valtri@civ.zcu.cz>
Tue, 2 Oct 2007 17:13:47 +0000 (17:13 +0000)
committerFrantišek Dvořák <valtri@civ.zcu.cz>
Tue, 2 Oct 2007 17:13:47 +0000 (17:13 +0000)
- merge fixes from lb.server
- build via ETICS

org.glite.lbjp-common.db/Makefile
org.glite.lbjp-common.db/examples/db_expire.c
org.glite.lbjp-common.db/examples/db_test.c
org.glite.lbjp-common.db/interface/db.h
org.glite.lbjp-common.db/project/version.properties [new file with mode: 0644]
org.glite.lbjp-common.db/src/db.c
org.glite.lbjp-common.trio/Makefile
org.glite.lbjp-common.trio/project/version.properties [new file with mode: 0644]

index e54bd9f..e1d093f 100644 (file)
@@ -4,7 +4,7 @@ builddir=build
 stagedir=.
 distdir=.
 globalprefix=glite
-lbutilsprefix=lb-utils
+lbutilsprefix=lbu
 package=glite-lb-utils-db
 version=0.2.0
 PREFIX=/opt/glite
@@ -20,13 +20,11 @@ nothrflavour=gcc32
 -include Makefile.inc
 -include ../project/version.properties
 
-version=${module.version}
-
 CC=gcc
 
 VPATH=${top_srcdir}/interface:${top_srcdir}/src:${top_srcdir}/examples
 
-DEBUG:=-g -O0 -Wall
+DEBUG:=-g -O0 -W -Wall
 
 CFLAGS:= \
        ${DEBUG} \
@@ -71,6 +69,8 @@ HDRS:=db.h
 LOBJS:=${OBJS:.o=.lo}
 LTESTOBJS:=${TESTOBJS:.o=.lo}
 
+default all: compile doc
+
 libglite_lbu_db.la: ${LOBJS}
        ${LINK} -o $@ $< ${EXT_LIBS}
 
@@ -86,8 +86,6 @@ db_test: db_test.lo libglite_lbu_dbtest.la
 db_expire: db_expire.lo libglite_lbu_dbtest.la
        ${LINK} -o $@ $+ ${EXT_LIBS}
 
-default all: compile
-
 compile: libglite_lbu_db.la
 
 check:
@@ -101,6 +99,8 @@ test_coverage:
 examples: db_test db_expire
 
 doc:
+       cp ${top_srcdir}/doc/*.dox .
+       echo "PROJECT_NUMBER = ${version}" >> C.dox
        doxygen C.dox
 
 stage: compile
@@ -119,14 +119,18 @@ distbin:
        save_dir=`pwd`; cd tmpbuilddir${stagedir} && tar -czf $$save_dir/${top_srcdir}/${distdir}/${package}-${version}_bin.tar.gz *; cd $$save_dir
        rm -rf tmpbuilddir
         
-install:
+install: all
        -mkdir -p ${PREFIX}/lib
+       -mkdir -p ${PREFIX}/share/doc/${package}-${version}
        -mkdir -p ${PREFIX}/include/${globalprefix}/${lbutilsprefix}
        ${INSTALL} -m 644 ${top_srcdir}/LICENSE ${PREFIX}/share/doc/${package}-${version}
+       cp -r C ${PREFIX}/share/doc/${package}-${version}
        ${INSTALL} -m 755 "libglite_lbu_db.la" "${PREFIX}/lib/libglite_lbu_db.la"; \
        ${INSTALL} -m 644 ${top_srcdir}/interface/${HDRS} ${PREFIX}/include/${globalprefix}/${lbutilsprefix}
 
 clean:
+       rm -rvf *.o *.lo .libs lib* *.c *.h *.dox C/ CPP/
+       rm -rvf log.xml project/ rpmbuild/ RPMS/ tgz/
 
 %.o %.lo: %.c
        ${COMPILE} -c $<
index 012a91b..8ca3c1b 100644 (file)
@@ -24,7 +24,8 @@
 
 
 static void print_blob(unsigned long len, char *blob) {
-       int i;
+       unsigned int i;
+
        for (i = 0; i < len; i++) printf("%02X ", blob[i]);
        printf("(='");
        for (i = 0; i < len; i++) printf("%c", blob[i]);
@@ -81,7 +82,7 @@ int main(int argn, char *argv[]) {
        do {
                user = "cicomexocitl.civ";
                dprintf(("executing '%s'...\n", user));
-               if (glite_lbu_ExecStmt(stmt, 1, GLITE_LBU_DB_TYPE_VARCHAR, user) == -1) goto failstmt;
+               if (glite_lbu_ExecPreparedStmt(stmt, 1, GLITE_LBU_DB_TYPE_VARCHAR, user) == -1) goto failstmt;
                dprintf(("fetching '%s'...\n", user));
                while ((nr = glite_lbu_FetchRow(stmt, 3, lens, res)) > 0) {
                        dprintf(("Result: n=%d, res=%p\n", nr, res));
index 02be92a..b86338e 100644 (file)
@@ -33,7 +33,8 @@
 
 
 static void print_blob(unsigned long len, char *blob) {
-       int i;
+       unsigned int i;
+
        for (i = 0; i < len; i++) printf("%02X ", blob[i]);
        printf("(='");
        for (i = 0; i < len; i++) printf("%c", blob[i]);
@@ -101,17 +102,17 @@ int main(int argn, char *argv[]) {
        dprintf(("prepare-insert...\n"));
        if (glite_lbu_PrepareStmt(ctx, INSERT_CMD, &stmt) != 0) goto failcon;
        dprintf(("execute 1. insert...\n"));
-       if (glite_lbu_ExecStmt(stmt, 3,
+       if (glite_lbu_ExecPreparedStmt(stmt, 3,
                               GLITE_LBU_DB_TYPE_INT, 2,
                               GLITE_LBU_DB_TYPE_VARCHAR, "cicomexocitl.civ",
                               GLITE_LBU_DB_TYPE_BLOB, blob1, sizeof(blob1) - 1) != 1) goto failstmt;
        dprintf(("execute 2. insert...\n"));
-       if (glite_lbu_ExecStmt(stmt, 3,
+       if (glite_lbu_ExecPreparedStmt(stmt, 3,
                               GLITE_LBU_DB_TYPE_INT, 3,
                               GLITE_LBU_DB_TYPE_VARCHAR, "tartarus",
                               GLITE_LBU_DB_TYPE_NULL) != 1) goto failstmt;
        dprintf(("execute 3. insert...\n"));
-       if (glite_lbu_ExecStmt(stmt, 3,
+       if (glite_lbu_ExecPreparedStmt(stmt, 3,
                               GLITE_LBU_DB_TYPE_INT, 4,
                               GLITE_LBU_DB_TYPE_VARCHAR, "harpia",
                               GLITE_LBU_DB_TYPE_BLOB, blob2, sizeof(blob2)) != 1) goto failstmt;
@@ -162,7 +163,7 @@ int main(int argn, char *argv[]) {
 
        user = "cicomexocitl.civ";
        dprintf(("executing '%s'...\n", user));
-       if (glite_lbu_ExecStmt(stmt, 1, GLITE_LBU_DB_TYPE_VARCHAR, user) == -1) goto failstmt;
+       if (glite_lbu_ExecPreparedStmt(stmt, 1, GLITE_LBU_DB_TYPE_VARCHAR, user) == -1) goto failstmt;
        dprintf(("fetching '%s'...\n", user));
        while ((nr = glite_lbu_FetchRow(stmt, 3, lens, res)) > 0) {
                dprintf(("Result: n=%d, res=%p\n", nr, res));
index 2818a4e..f28e2b8 100644 (file)
@@ -24,7 +24,7 @@ extern "C" {
  * SQL commands as single string. All values are incorporated in the SQL command strings. Proper escaping is required.
  * - enhanced:
  *
- * Prepared SQL commands with separated parameters, functions PrepareStmt() and ExecStmt(). All values are delivered in separated buffers. Its faster for multiple using and more secure.
+ * Prepared SQL commands with separated parameters, functions PrepareStmt() and ExecPreparedStmt(). All values are delivered in separated buffers. Its faster for multiple using and more secure.
  * @{
  */
 
@@ -276,7 +276,7 @@ time_t glite_lbu_DBToTime(const char *str);
  * when insert string oversize size_limit or number of rows to be inserted
  * overcome record_limit, the real insert is triggered
  */
-int glite_lbu_bufferedInsertInit(glite_lbu_DBContext ctx, glite_lbu_bufInsert *bi, void *mysql, const char *table_name, long size_limit, long record_limit, const char * columns);
+int glite_lbu_bufferedInsertInit(glite_lbu_DBContext ctx, glite_lbu_bufInsert *bi, const char *table_name, long size_limit, long record_limit, const char * columns);
 
 
 /**
@@ -329,7 +329,7 @@ int glite_lbu_PrepareStmt(glite_lbu_DBContext ctx, const char *sql, glite_lbu_St
  *
  * \return              number of affected rows, -1 on error
  */
-int glite_lbu_ExecStmt(glite_lbu_Statement stmt, int n, ...);
+int glite_lbu_ExecPreparedStmt(glite_lbu_Statement stmt, int n, ...);
 
 
 /**
diff --git a/org.glite.lbjp-common.db/project/version.properties b/org.glite.lbjp-common.db/project/version.properties
new file mode 100644 (file)
index 0000000..32cfddd
--- /dev/null
@@ -0,0 +1,4 @@
+# $Id$
+# $Name$
+module.version=1.0.0
+module.age=1
index bce48b8..a3fab13 100644 (file)
@@ -41,7 +41,6 @@
 struct glite_lbu_DBContext_s {
        MYSQL *mysql;
        const char *cs;
-       int have_caps;
        int caps;
        struct {
                int code;
@@ -108,7 +107,7 @@ static int myerrstmt(glite_lbu_Statement stmt, const char *source, int line);
 static int myisokstmt(glite_lbu_Statement stmt, const char *source, int line, int *retry);
 static int db_connect(glite_lbu_DBContext ctx, const char *cs, MYSQL **mysql);
 static void db_close(MYSQL *mysql);
-static int transaction_test(glite_lbu_DBContext ctx, MYSQL *m2, int *have_transactions);
+static int transaction_test(glite_lbu_DBContext ctx);
 static int FetchRowSimple(glite_lbu_DBContext ctx, MYSQL_RES *result, unsigned long *lengths, char **results);
 static int FetchRowPrepared(glite_lbu_DBContext ctx, glite_lbu_Statement stmt, unsigned int n, unsigned long *lengths, char **results);
 void set_time(MYSQL_TIME *mtime, const time_t time);
@@ -158,12 +157,10 @@ void glite_lbu_DBClose(glite_lbu_DBContext ctx) {
 
 int glite_lbu_DBQueryCaps(glite_lbu_DBContext ctx) {
        MYSQL   *m = ctx->mysql;
-       MYSQL   *m2;
-       int     major,minor,sub,version,caps,have_transactions=0;
+       int     major,minor,sub,version,caps,origcaps;
        const char *ver_s;
 
-       if (ctx->have_caps) return ctx->caps;
-
+       origcaps = ctx->caps;
        caps = 0;
 
        ver_s = mysql_get_server_info(m);
@@ -175,17 +172,12 @@ int glite_lbu_DBQueryCaps(glite_lbu_DBContext ctx) {
        if (version >= GLITE_LBU_MYSQL_PREPARED_VERSION) caps |= GLITE_LBU_DB_CAP_PREPARED;
 
        CLR_ERR(ctx);
+       transaction_test(ctx);
+       if ((ctx->caps & GLITE_LBU_DB_CAP_TRANSACTIONS)) caps |= GLITE_LBU_DB_CAP_TRANSACTIONS;
 
-       if (db_connect(ctx, ctx->cs, &m2) == 0) {
-               transaction_test(ctx, m2, &have_transactions);
-               db_close(m2);
-       }
-       if (have_transactions) caps |= GLITE_LBU_DB_CAP_TRANSACTIONS;
-
-       if (STATUS(ctx) == 0) {
-               ctx->have_caps = 1;
-               return caps;
-       } else return -1;
+       ctx->caps = origcaps;
+       if (STATUS(ctx) == 0) return caps;
+       else return -1;
 }
 
 
@@ -264,7 +256,7 @@ int glite_lbu_QueryIndices(glite_lbu_DBContext ctx, const char *table, char ***k
                return STATUS(ctx);
 
        while ((ret = glite_lbu_FetchRow(stmt,sizeof(showcol)/sizeof(showcol[0]),NULL,showcol)) > 0) {
-               assert(ret <= sizeof showcol/sizeof showcol[0]);
+               assert(ret <= (int)(sizeof showcol/sizeof showcol[0]));
 
                if (!col_names) {
                        col_names = malloc(ret * sizeof col_names[0]);
@@ -367,6 +359,7 @@ int glite_lbu_ExecSQL(glite_lbu_DBContext ctx, const char *cmd, glite_lbu_Statem
                                        return -1;
                                        break;
                                case CR_SERVER_LOST:
+                               case CR_SERVER_GONE_ERROR:
                                        if (retry_nr <= 0) 
                                                do_reconnect = 1;
                                        break;
@@ -485,7 +478,7 @@ failed:
 }
 
 
-int glite_lbu_ExecStmt(glite_lbu_Statement stmt, int n, ...) {
+int glite_lbu_ExecPreparedStmt(glite_lbu_Statement stmt, int n, ...) {
        int i;
        va_list ap;
        glite_lbu_DBType type;
@@ -597,7 +590,7 @@ failed:
 }
 
 
-int glite_lbu_bufferedInsertInit(glite_lbu_DBContext ctx, glite_lbu_bufInsert *bi, void *mysql, const char *table_name, long size_limit, long record_limit, const char *columns)
+int glite_lbu_bufferedInsertInit(glite_lbu_DBContext ctx, glite_lbu_bufInsert *bi, const char *table_name, long size_limit, long record_limit, const char *columns)
 {
        *bi = calloc(1, sizeof(*bi));
        (*bi)->ctx = ctx;
@@ -749,6 +742,7 @@ static int myisokstmt(glite_lbu_Statement stmt, const char *source, int line, in
                        return -1;
                        break;
                case CR_SERVER_LOST:
+               case CR_SERVER_GONE_ERROR:
                        if (*retry > 0) {
                                (*retry)--;
                                return 0;
@@ -771,6 +765,9 @@ static int db_connect(glite_lbu_DBContext ctx, const char *cs, MYSQL **mysql) {
        char    *host,*user,*pw,*db; 
        char    *slash,*at,*colon;
        int      ret;
+#ifdef MYSQL_OPT_RECONNECT
+       my_bool reconnect = 1;
+#endif
 
        // needed for SQL result parameters
        assert(sizeof(int) >= sizeof(my_bool));
@@ -780,6 +777,10 @@ static int db_connect(glite_lbu_DBContext ctx, const char *cs, MYSQL **mysql) {
        if (!(*mysql = mysql_init(NULL))) return ERR(ctx, ENOMEM, NULL);
 
        mysql_options(*mysql, MYSQL_READ_DEFAULT_FILE, "my");
+#ifdef MYSQL_OPT_RECONNECT
+       /* XXX: may result in weird behaviour in the middle of transaction */
+       mysql_options(*mysql, MYSQL_OPT_RECONNECT, &reconnect);
+#endif
 
        host = user = pw = db = NULL;
 
@@ -827,60 +828,32 @@ static void db_close(MYSQL *mysql) {
 
 /*
  * test transactions capability:
- *
- * 1) with connection 1 create testing table test<pid>
- * 2) with connection 1 insert a value
- * 3) with connection 2 look for a value, transactions are for no error and
- *    no items found
- * 4) with connection 1 commit and drop  the table
  */
-static int transaction_test(glite_lbu_DBContext ctx, MYSQL *m2, int *have_transactions) {
-       MYSQL *m1;
-       char *desc, *cmd_create, *cmd_insert, *cmd_select, *cmd_drop;
+static int transaction_test(glite_lbu_DBContext ctx) {
+       glite_lbu_Statement stmt;
+       char *table[1] = { NULL }, *res[2] = { NULL, NULL }, *cmd = NULL;
        int retval;
-       int err;
-       pid_t pid;
 
-       ctx->caps |= GLITE_LBU_DB_CAP_TRANSACTIONS;
-       pid = getpid();
-       *have_transactions = 0;
+       ctx->caps &= ~GLITE_LBU_DB_CAP_TRANSACTIONS;
 
-       asprintf(&cmd_create, "CREATE TABLE test%d (item INT) ENGINE='innodb'", pid);
-       asprintf(&cmd_insert, "INSERT INTO test%d (item) VALUES (1)", pid);
-       asprintf(&cmd_select, "SELECT item FROM test%d", pid);
-       asprintf(&cmd_drop, "DROP TABLE test%d", pid);
-
-       m1 = ctx->mysql;
-       //glite_lbu_ExecSQL(ctx, cmd_drop, NULL);
-       if (glite_lbu_ExecSQL(ctx, cmd_create, NULL) != 0) goto err1;
-       if (glite_lbu_Transaction(ctx) != 0) goto err2;
-       if (glite_lbu_ExecSQL(ctx, cmd_insert, NULL) != 1) goto err2;
-
-       ctx->mysql = m2;
-       if ((retval = glite_lbu_ExecSQL(ctx, cmd_select, NULL)) == -1) goto err2;
+       if ((retval = glite_lbu_ExecSQL(ctx, "SHOW TABLES", &stmt)) <= 0 || glite_lbu_FetchRow(stmt, 1, NULL, table) < 0) goto quit;
+       glite_lbu_FreeStmt(&stmt);
 
-       ctx->mysql = m1;
-       if (glite_lbu_Commit(ctx) != 0) goto err2;
-       if (glite_lbu_ExecSQL(ctx, cmd_drop, NULL) != 0) goto err1;
+       trio_asprintf(&cmd, "SHOW CREATE TABLE %|Ss", table[0]);
+       if (glite_lbu_ExecSQL(ctx, cmd, &stmt) <= 0 || (retval = glite_lbu_FetchRow(stmt, 2, NULL, res)) < 0 ) goto quit;
+       if (retval != 2 || strcmp(res[0], table[0])) ERR(ctx, EIO, "unexpected show create result");
+       else ctx->caps |= GLITE_LBU_DB_CAP_TRANSACTIONS;
 
 #ifdef LBS_DB_PROFILE
        fprintf(stderr, "[%d] use_transactions = %d\n", getpid(), USE_TRANS(ctx));
 #endif
 
-       *have_transactions = retval == 0;
-       goto ok;
-err2:
-       err = ctx->err.code;
-       desc = ctx->err.desc;
-       glite_lbu_ExecSQL(ctx, cmd_drop, NULL);
-       ctx->err.code = err;
-       ctx->err.desc = desc;
-err1:
-ok:
-       free(cmd_create);
-       free(cmd_insert);
-       free(cmd_select);
-       free(cmd_drop);
+quit:
+       glite_lbu_FreeStmt(&stmt);
+       if (table[0]) free(table[0]);
+       if (res[0]) free(res[0]);
+       if (res[1]) free(res[1]);
+       if (cmd) free(cmd);
        return STATUS(ctx);
 }
 
@@ -890,7 +863,7 @@ ok:
  */
 static int FetchRowSimple(glite_lbu_DBContext ctx, MYSQL_RES *result, unsigned long *lengths, char **results) {
        MYSQL_ROW            row;
-       int                  nr, i;
+       unsigned int         nr, i;
        unsigned long       *len;
 
        CLR_ERR(ctx);
@@ -922,7 +895,8 @@ static int FetchRowSimple(glite_lbu_DBContext ctx, MYSQL_RES *result, unsigned l
  * prepared version of the fetch
  */
 static int FetchRowPrepared(glite_lbu_DBContext ctx, glite_lbu_Statement stmt, unsigned int n, unsigned long *lengths, char **results) {
-       int ret, retry, i;
+       int ret, retry;
+       unsigned int i;
        MYSQL_BIND *binds = NULL;
        unsigned long *lens = NULL;
 
index 96a8386..ff6a50b 100644 (file)
@@ -13,8 +13,6 @@ lbuprefix=lbu
 -include Makefile.inc
 -include ../project/version.properties
 
-version=${module.version}
-
 VPATH=${top_srcdir}/src:${top_srcdir}/test::${top_srcdir}/doc
 
 CC=gcc
@@ -97,18 +95,18 @@ distbin:
        save_dir=`pwd`; cd tmpbuilddir${stagedir} && tar -czf $$save_dir/${top_srcdir}/${distdir}/${package}-${version}_bin.tar.gz *; cd $$save_dir
        rm -rf tmpbuilddir
 
-install:
-       mkdir -p ${PREFIX}/lib
-       mkdir -p ${PREFIX}/share/doc/${package}-${version}
-       ${INSTALL} -m 644 ${LTLIB} ${PREFIX}/lib
+install: all
+       -mkdir -p ${PREFIX}/lib
+       -mkdir -p ${PREFIX}/share/doc/${package}-${version}
+       -mkdir -p ${PREFIX}/include/${globalprefix}/${lbutilsprefix}
+       -mkdir -p ${PREFIX}/include/${globalprefix}/${lbuprefix}
+       ${INSTALL} -m 644 ${LTLIB} ${STATICLIB} ${PREFIX}/lib
        ${INSTALL} -m 644 ${top_srcdir}/LICENSE ${PREFIX}/share/doc/${package}-${version}
-       if [ x${DOSTAGE} = xyes ]; then \
-               mkdir -p ${PREFIX}/include/${globalprefix}/${lbuprefix} ; \
-               (cd ${top_srcdir}/interface && install -m 644 ${HDRS} ${PREFIX}/include/${globalprefix}/${lbuprefix}) ; \
-               install -m 644 ${STATICLIB} ${PREFIX}/lib; \
-       fi
+       (cd ${top_srcdir}/interface && ${INSTALL} -m 644 ${HDRS} ${PREFIX}/include/${globalprefix}/${lbuprefix})
 
 clean:
-
+       rm -rvf *.o *.lo .libs lib* *.c *.h *.dox C/ CPP/
+       rm -rvf log.xml project/ rpmbuild/ RPMS/ tgz/
+       
 %.o: %.c
        ${COMPILE} ${CFLAGS} -c $<
diff --git a/org.glite.lbjp-common.trio/project/version.properties b/org.glite.lbjp-common.trio/project/version.properties
new file mode 100644 (file)
index 0000000..32cfddd
--- /dev/null
@@ -0,0 +1,4 @@
+# $Id$
+# $Name$
+module.version=1.0.0
+module.age=1