From: Kern Sibbald Date: Sat, 10 Apr 2010 10:15:10 +0000 (+0200) Subject: Revert "Replace INGcheck with EXEC SQL WHENEVER SQLERROR GOTO construction used by... X-Git-Tag: Release-5.0.2~69 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=a83e8146220450213b59818c6725a51079583193;p=bacula%2Fbacula Revert "Replace INGcheck with EXEC SQL WHENEVER SQLERROR GOTO construction used by Ingres embedded SQL." This reverts commit f1f3bea96e1a984a4fadb319043e46b3b512c226. --- diff --git a/bacula/src/cats/ingres.c b/bacula/src/cats/ingres.c index af3428b774..8a7219e820 100755 --- a/bacula/src/cats/ingres.c +++ b/bacula/src/cats/ingres.c @@ -579,7 +579,7 @@ int my_ingres_query(B_DB *mdb, const char *query) Dmsg0(500,"my_ingres_query (non SELECT) starting...\n"); /* non SELECT */ mdb->num_rows = INGexec(mdb->db, new_query, mdb->transaction); - if (mdb->num_rows == -1) { + if (INGcheck()) { Dmsg0(500,"my_ingres_query (non SELECT) went wrong\n"); mdb->status = 1; } else { @@ -684,7 +684,7 @@ int my_ingres_insert_id(B_DB *mdb, const char *query, const char *table_name) * Ingres and things work. */ mdb->num_rows = INGexec(mdb->db, query, true); - if (mdb->num_rows == -1) { + if (INGcheck()) { return 0; } diff --git a/bacula/src/cats/myingres.sc b/bacula/src/cats/myingres.sc index f63484a76b..db629be8bd 100644 --- a/bacula/src/cats/myingres.sc +++ b/bacula/src/cats/myingres.sc @@ -47,6 +47,11 @@ EXEC SQL INCLUDE SQLDA; /* * ---Implementations--- */ +int INGcheck() +{ + return (sqlca.sqlcode < 0) ? sqlca.sqlcode : 0; +} + short INGgetCols(INGconn *conn, const char *query, bool transaction) { EXEC SQL BEGIN DECLARE SECTION; @@ -54,7 +59,7 @@ short INGgetCols(INGconn *conn, const char *query, bool transaction) char *stmt; EXEC SQL END DECLARE SECTION; - short number = -1; + short number = 1; IISQLDA *sqlda; sqlda = (IISQLDA *)malloc(IISQDA_HEAD_SIZE + (number * IISQDA_VAR_SIZE)); @@ -69,24 +74,28 @@ short INGgetCols(INGconn *conn, const char *query, bool transaction) */ sess_id = conn->session_id; EXEC SQL SET_SQL (SESSION = :sess_id); - - EXEC SQL WHENEVER SQLERROR GOTO bail_out; EXEC SQL PREPARE s1 from :stmt; - EXEC SQL DESCRIBE s1 into :sqlda; + if (INGcheck() < 0) { + number = -1; + goto bail_out; + } - EXEC SQL WHENEVER SQLERROR CONTINUE; + EXEC SQL DESCRIBE s1 into :sqlda; + if (INGcheck() < 0) { + number = -1; + goto bail_out; + } number = sqlda->sqld; +bail_out: /* * If we are not in a transaction we commit our work now. */ if (!transaction) { EXEC SQL COMMIT WORK; } - -bail_out: /* * Switch to no default session for this thread. */ @@ -113,10 +122,12 @@ static inline IISQLDA *INGgetDescriptor(short numCols, const char *query) stmt = bstrdup(query); EXEC SQL PREPARE s2 INTO :sqlda FROM :stmt; + + free(stmt); for (i = 0; i < sqlda->sqld; ++i) { /* - * Negative type indicates nullable columns, so an indicator + * Negative type indicates nullable coulumns, so an indicator * is allocated, otherwise it's null */ if (sqlda->sqlvar[i].sqltype > 0) { @@ -147,7 +158,6 @@ static inline IISQLDA *INGgetDescriptor(short numCols, const char *query) } } - free(stmt); return sqlda; } @@ -359,28 +369,29 @@ static inline ING_ROW *INGgetRowSpace(INGresult *ing_res) static inline int INGfetchAll(const char *query, INGresult *ing_res) { + int linecount = 0; ING_ROW *row; IISQLDA *desc; - int linecount = -1; + int check = -1; desc = ing_res->sqlda; - EXEC SQL WHENEVER SQLERROR GOTO bail_out; - EXEC SQL DECLARE c2 CURSOR FOR s2; + if ((check = INGcheck()) < 0) { + return check; + } + EXEC SQL OPEN c2; + if ((check = INGcheck()) < 0) { + return check; + } - EXEC SQL WHENEVER SQLERROR CONTINUE; - - linecount = 0; + /* for (linecount = 0; sqlca.sqlcode == 0; ++linecount) */ do { EXEC SQL FETCH c2 USING DESCRIPTOR :desc; - if (sqlca.sqlcode == 0 || sqlca.sqlcode == -40202) { - /* - * Allocate space for fetched row - */ - row = INGgetRowSpace(ing_res); + if ( (sqlca.sqlcode == 0) || (sqlca.sqlcode == -40202) ) { + row = INGgetRowSpace(ing_res); /* alloc space for fetched row */ /* * Initialize list when encountered first time @@ -390,10 +401,10 @@ static inline int INGfetchAll(const char *query, INGresult *ing_res) ing_res->first_row->next = NULL; ing_res->act_row = ing_res->first_row; } - ing_res->act_row->next = row; /* append row to old act_row */ ing_res->act_row = row; /* set row as act_row */ - row->row_number = linecount++; + row->row_number = linecount; + ++linecount; } } while ( (sqlca.sqlcode == 0) || (sqlca.sqlcode == -40202) ); @@ -401,8 +412,6 @@ static inline int INGfetchAll(const char *query, INGresult *ing_res) ing_res->status = ING_COMMAND_OK; ing_res->num_rows = linecount; - -bail_out: return linecount; } @@ -488,35 +497,40 @@ int INGexec(INGconn *conn, const char *query, bool transaction) char *stmt; EXEC SQL END DECLARE SECTION; - rowcount = -1; stmt = bstrdup(query); + rowcount = -1; /* * Switch to the correct default session for this thread. */ sess_id = conn->session_id; EXEC SQL SET_SQL (SESSION = :sess_id); + EXEC SQL EXECUTE IMMEDIATE :stmt; - EXEC SQL WHENEVER SQLERROR GOTO bail_out; + free(stmt); - EXEC SQL EXECUTE IMMEDIATE :stmt; - EXEC SQL INQUIRE_INGRES(:rowcount = ROWCOUNT); + if ((check = INGcheck()) < 0) { + rowcount = check; + goto bail_out; + } - EXEC SQL WHENEVER SQLERROR CONTINUE; + EXEC SQL INQUIRE_INGRES(:rowcount = ROWCOUNT); + if ((check = INGcheck()) < 0) { + rowcount = check; + goto bail_out; + } +bail_out: /* * If we are not in a transaction we commit our work now. */ if (!transaction) { EXEC SQL COMMIT WORK; } - -bail_out: /* * Switch to no default session for this thread. */ EXEC SQL SET_SQL (SESSION = NONE); - free(stmt); return rowcount; } @@ -606,23 +620,25 @@ void INGcommit(const INGconn *conn) INGconn *INGconnectDB(char *dbname, char *user, char *passwd, int session_id) { + INGconn *dbconn; + + if (dbname == NULL || strlen(dbname) == 0) { + return NULL; + } + + dbconn = (INGconn *)malloc(sizeof(INGconn)); + memset(dbconn, 0, sizeof(INGconn)); + EXEC SQL BEGIN DECLARE SECTION; char ingdbname[24]; char ingdbuser[32]; char ingdbpasswd[32]; int sess_id; EXEC SQL END DECLARE SECTION; - INGconn *dbconn; - - if (dbname == NULL || strlen(dbname) == 0) { - return NULL; - } sess_id = session_id; bstrncpy(ingdbname, dbname, sizeof(ingdbname)); - EXEC SQL WHENEVER SQLERROR GOTO bail_out; - if (user != NULL) { bstrncpy(ingdbuser, user, sizeof(ingdbuser)); if (passwd != NULL) { @@ -640,17 +656,15 @@ INGconn *INGconnectDB(char *dbname, char *user, char *passwd, int session_id) :ingdbname SESSION :sess_id; } + if (INGcheck() < 0) { + return NULL; + } - EXEC SQL WHENEVER SQLERROR CONTINUE; - - dbconn = (INGconn *)malloc(sizeof(INGconn)); - memset(dbconn, 0, sizeof(INGconn)); - bstrncpy(dbconn->dbname, ingdbname, sizeof(dbconn->dbname)); bstrncpy(dbconn->user, ingdbuser, sizeof(dbconn->user)); bstrncpy(dbconn->password, ingdbpasswd, sizeof(dbconn->password)); dbconn->session_id = sess_id; - dbconn->msg = (char *)malloc(257); + dbconn->msg = (char*)malloc(257); memset(dbconn->msg, 0, 257); /* @@ -658,7 +672,6 @@ INGconn *INGconnectDB(char *dbname, char *user, char *passwd, int session_id) */ EXEC SQL SET_SQL (SESSION = NONE); -bail_out: return dbconn; } @@ -683,8 +696,8 @@ char *INGerrorMessage(const INGconn *conn) char errbuf[256]; EXEC SQL END DECLARE SECTION; - EXEC SQL INQUIRE_INGRES (:errbuf = ERRORTEXT); - strncpy(conn->msg, errbuf, sizeof(conn->msg)); + EXEC SQL INQUIRE_INGRES(:errbuf = ERRORTEXT); + memcpy(conn->msg, &errbuf, 256); return conn->msg; } diff --git a/bacula/src/cats/myingres.sh b/bacula/src/cats/myingres.sh index 6d49814304..11629707b0 100644 --- a/bacula/src/cats/myingres.sh +++ b/bacula/src/cats/myingres.sh @@ -82,6 +82,7 @@ typedef struct ing_conn { /* ---Prototypes--- */ +int INGcheck(void); short INGgetCols(INGconn *conn, const char *query, bool transaction); char *INGgetvalue(INGresult *res, int row_number, int column_number); bool INGgetisnull(INGresult *res, int row_number, int column_number);