From: Kern Sibbald Date: Sat, 10 Apr 2010 10:18:34 +0000 (+0200) Subject: Revert "Lets rewrite the query at a higher level then we see the new query in the... X-Git-Tag: Release-5.0.2~48 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=7c361bfd9f878c0e63502baec31d24b5fda36821;p=bacula%2Fbacula Revert "Lets rewrite the query at a higher level then we see the new query in the debugging and" This reverts commit c8fe384b0b90cd7c364d4709a1ad36ac6601186a. --- diff --git a/bacula/src/cats/ingres.c b/bacula/src/cats/ingres.c index abcb7860f1..e984b799af 100755 --- a/bacula/src/cats/ingres.c +++ b/bacula/src/cats/ingres.c @@ -527,14 +527,6 @@ void my_ingres_field_seek(B_DB *mdb, int field) */ int my_ingres_query(B_DB *mdb, const char *query) { - char *new_query; - - if (strstr(query, "LIMIT") != NULL) { - new_query = mdb->limit_filter->replace(query); - } else { - new_query = query; - } - Dmsg0(500, "my_ingres_query started\n"); // We are starting a new query. reset everything. mdb->num_rows = -1; @@ -548,17 +540,17 @@ int my_ingres_query(B_DB *mdb, const char *query) mdb->result = NULL; } - Dmsg1(500, "my_ingres_query starts with '%s'\n", new_query); + Dmsg1(500, "my_ingres_query starts with '%s'\n", query); /* TODO: differentiate between SELECTs and other queries */ - if ((cols = INGgetCols(new_query)) <= 0) { + if ((cols = INGgetCols(mdb, query)) <= 0) { if (cols < 0 ) { Dmsg0(500,"my_ingres_query: neg.columns: no DML stmt!\n"); } Dmsg0(500,"my_ingres_query (non SELECT) starting...\n"); /* non SELECT */ - mdb->num_rows = INGexec(mdb->db, new_query); + mdb->num_rows = INGexec(mdb, mdb->db, query); if (INGcheck()) { Dmsg0(500,"my_ingres_query (non SELECT) went wrong\n"); mdb->status = 1; @@ -569,9 +561,9 @@ int my_ingres_query(B_DB *mdb, const char *query) } else { /* SELECT */ Dmsg0(500,"my_ingres_query (SELECT) starting...\n"); - mdb->result = INGquery(mdb->db, new_query); + mdb->result = INGquery(mdb, mdb->db, query); if (mdb->result != NULL) { - Dmsg1(500, "we have a result\n", new_query); + Dmsg1(500, "we have a result\n", query); // how many fields in the set? mdb->num_fields = (int)INGnfields(mdb->result); @@ -637,7 +629,7 @@ int my_ingres_currval(B_DB *mdb, const char *table_name) Dmsg1(500, "my_ingres_currval invoked with '%s'\n", query); - result = INGquery(mdb->db, query); + result = INGquery(mdb, mdb->db, query); if (!result) { Dmsg1(50, "Query failed: %s\n", query); diff --git a/bacula/src/cats/myingres.c b/bacula/src/cats/myingres.c index e393b2ea79..29cf2f2623 100644 --- a/bacula/src/cats/myingres.c +++ b/bacula/src/cats/myingres.c @@ -26,8 +26,13 @@ * * Stefan Reddig, June 2009 */ +/* The following is necessary so that we do not include + * the dummy external definition of DB. + */ +#define __SQL_C /* indicate that this is sql.c */ #include "bacula.h" -/* # line 37 "myingres.sc" */ +#include "cats.h" +/* # line 43 "myingres.sc" */ #ifdef HAVE_INGRES #include #include @@ -45,65 +50,87 @@ int INGcheck() { return (sqlca.sqlcode < 0) ? sqlca.sqlcode : 0; } -short INGgetCols(const char *query) +short INGgetCols(B_DB *mdb, const char *query) { -/* # line 57 "myingres.sc" */ + bool stmt_free = false; +/* # line 64 "myingres.sc" */ char *stmt; -/* # line 59 "myingres.sc" */ +/* # line 66 "myingres.sc" */ short number = 1; IISQLDA *sqlda; sqlda = (IISQLDA *)malloc(IISQDA_HEAD_SIZE + (number * IISQDA_VAR_SIZE)); memset(sqlda, 0, (IISQDA_HEAD_SIZE + (number * IISQDA_VAR_SIZE))); sqlda->sqln = number; - stmt = bstrdup(query); -/* # line 71 "myingres.sc" */ /* prepare */ + /* + * See if we need to run this through the limit_filter. + */ + if (strstr(query, "LIMIT") != NULL) { + stmt = mdb->limit_filter->replace(query); + } else { + stmt = bstrdup(query); + stmt_free = true; + } +/* # line 86 "myingres.sc" */ /* prepare */ { IIsqInit(&sqlca); IIsqPrepare(0,(char *)"s1",(char *)0,0,stmt); } -/* # line 72 "myingres.sc" */ /* host code */ +/* # line 87 "myingres.sc" */ /* host code */ if (INGcheck() < 0) { number = -1; goto bail_out; } -/* # line 77 "myingres.sc" */ /* describe */ +/* # line 92 "myingres.sc" */ /* describe */ { IIsqInit(&sqlca); IIsqDescribe(0,(char *)"s1",sqlda,0); } -/* # line 78 "myingres.sc" */ /* host code */ +/* # line 93 "myingres.sc" */ /* host code */ if (INGcheck() < 0) { number = -1; goto bail_out; } number = sqlda->sqld; bail_out: - free(stmt); + if (stmt_free) { + free(stmt); + } free(sqlda); return number; } -static inline IISQLDA *INGgetDescriptor(short numCols, const char *query) +static inline IISQLDA *INGgetDescriptor(B_DB *mdb, short numCols, const char *query) { -/* # line 93 "myingres.sc" */ + bool stmt_free = false; +/* # line 111 "myingres.sc" */ char *stmt; -/* # line 95 "myingres.sc" */ +/* # line 113 "myingres.sc" */ int i; IISQLDA *sqlda; sqlda = (IISQLDA *)malloc(IISQDA_HEAD_SIZE + (numCols * IISQDA_VAR_SIZE)); memset(sqlda, 0, (IISQDA_HEAD_SIZE + (numCols * IISQDA_VAR_SIZE))); sqlda->sqln = numCols; - stmt = bstrdup(query); -/* # line 107 "myingres.sc" */ /* prepare */ + /* + * See if we need to run this through the limit_filter. + */ + if (strstr(query, "LIMIT") != NULL) { + stmt = mdb->limit_filter->replace(query); + } else { + stmt = bstrdup(query); + stmt_free = true; + } +/* # line 133 "myingres.sc" */ /* prepare */ { IIsqInit(&sqlca); IIsqPrepare(0,(char *)"s2",sqlda,0,stmt); } -/* # line 109 "myingres.sc" */ /* host code */ - free(stmt); +/* # line 135 "myingres.sc" */ /* host code */ + if (stmt_free) { + free(stmt); + } for (i = 0; i < sqlda->sqld; ++i) { /* * Negative type indicates nullable coulumns, so an indicator @@ -327,32 +354,32 @@ static inline int INGfetchAll(const char *query, INGresult *ing_res) IISQLDA *desc; int check = -1; desc = ing_res->sqlda; -/* # line 363 "myingres.sc" */ /* host code */ +/* # line 392 "myingres.sc" */ /* host code */ if ((check = INGcheck()) < 0) { return check; } -/* # line 367 "myingres.sc" */ /* open */ +/* # line 396 "myingres.sc" */ /* open */ { IIsqInit(&sqlca); - IIcsOpen((char *)"c2",10560,29663); + IIcsOpen((char *)"c2",4150,23546); IIwritio(0,(short *)0,1,32,0,(char *)"s2"); - IIcsQuery((char *)"c2",10560,29663); + IIcsQuery((char *)"c2",4150,23546); } -/* # line 368 "myingres.sc" */ /* host code */ +/* # line 397 "myingres.sc" */ /* host code */ if ((check = INGcheck()) < 0) { return check; } /* for (linecount = 0; sqlca.sqlcode == 0; ++linecount) */ do { -/* # line 374 "myingres.sc" */ /* fetch */ +/* # line 403 "myingres.sc" */ /* fetch */ { IIsqInit(&sqlca); - if (IIcsRetScroll((char *)"c2",10560,29663,-1,-1) != 0) { + if (IIcsRetScroll((char *)"c2",4150,23546,-1,-1) != 0) { IIcsDaGet(0,desc); IIcsERetrieve(); } /* IIcsRetrieve */ } -/* # line 376 "myingres.sc" */ /* host code */ +/* # line 405 "myingres.sc" */ /* host code */ if ( (sqlca.sqlcode == 0) || (sqlca.sqlcode == -40202) ) { row = INGgetRowSpace(ing_res); /* alloc space for fetched row */ /* @@ -369,12 +396,12 @@ static inline int INGfetchAll(const char *query, INGresult *ing_res) ++linecount; } } while ( (sqlca.sqlcode == 0) || (sqlca.sqlcode == -40202) ); -/* # line 394 "myingres.sc" */ /* close */ +/* # line 423 "myingres.sc" */ /* close */ { IIsqInit(&sqlca); - IIcsClose((char *)"c2",10560,29663); + IIcsClose((char *)"c2",4150,23546); } -/* # line 396 "myingres.sc" */ /* host code */ +/* # line 425 "myingres.sc" */ /* host code */ ing_res->status = ING_COMMAND_OK; ing_res->num_rows = linecount; return linecount; @@ -439,39 +466,50 @@ short INGftype(const INGresult *res, int column_number) { return res->fields[column_number].type; } -int INGexec(INGconn *conn, const char *query) +int INGexec(B_DB *mdb, INGconn *conn, const char *query) { + bool stmt_free = false; int check; -/* # line 477 "myingres.sc" */ +/* # line 507 "myingres.sc" */ int rowcount; char *stmt; -/* # line 480 "myingres.sc" */ +/* # line 510 "myingres.sc" */ - stmt = bstrdup(query); + /* + * See if we need to run this through the limit_filter. + */ + if (strstr(query, "LIMIT") != NULL) { + stmt = mdb->limit_filter->replace(query); + } else { + stmt = bstrdup(query); + stmt_free = true; + } rowcount = -1; -/* # line 485 "myingres.sc" */ /* execute */ +/* # line 523 "myingres.sc" */ /* execute */ { IIsqInit(&sqlca); IIsqExImmed(stmt); IIsyncup((char *)0,0); } -/* # line 487 "myingres.sc" */ /* host code */ - free(stmt); +/* # line 525 "myingres.sc" */ /* host code */ + if (stmt_free) { + free(stmt); + } if ((check = INGcheck()) < 0) { return check; } -/* # line 493 "myingres.sc" */ /* inquire_ingres */ +/* # line 533 "myingres.sc" */ /* inquire_ingres */ { IILQisInqSqlio((short *)0,1,30,sizeof(rowcount),&rowcount,8); } -/* # line 494 "myingres.sc" */ /* host code */ +/* # line 534 "myingres.sc" */ /* host code */ if ((check = INGcheck()) < 0) { return check; } return rowcount; } -INGresult *INGquery(INGconn *conn, const char *query) +INGresult *INGquery(B_DB *mdb, INGconn *conn, const char *query) { /* * TODO: error handling @@ -479,21 +517,25 @@ INGresult *INGquery(INGconn *conn, const char *query) IISQLDA *desc = NULL; INGresult *res = NULL; int rows = -1; - int cols = INGgetCols(query); - desc = INGgetDescriptor(cols, query); + int cols = INGgetCols(mdb, query); + desc = INGgetDescriptor(mdb, cols, query); if (!desc) { return NULL; } + res = INGgetINGresult(desc); if (!res) { return NULL; } + rows = INGfetchAll(query, res); + if (rows < 0) { INGfreeDescriptor(desc); INGfreeINGresult(res); return NULL; } + return res; } void INGclear(INGresult *res) @@ -512,14 +554,14 @@ INGconn *INGconnectDB(char *dbname, char *user, char *passwd) } dbconn = (INGconn *)malloc(sizeof(INGconn)); memset(dbconn, 0, sizeof(INGconn)); -/* # line 553 "myingres.sc" */ +/* # line 590 "myingres.sc" */ char ingdbname[24]; char ingdbuser[32]; char ingdbpasw[32]; char conn_name[32]; int sess_id; -/* # line 559 "myingres.sc" */ +/* # line 596 "myingres.sc" */ bstrncpy(ingdbname, dbname, sizeof(ingdbname)); if (user != NULL) { @@ -529,7 +571,7 @@ INGconn *INGconnectDB(char *dbname, char *user, char *passwd) } else { memset(ingdbpasw, 0, sizeof(ingdbpasw)); } -/* # line 570 "myingres.sc" */ /* connect */ +/* # line 607 "myingres.sc" */ /* connect */ { IIsqInit(&sqlca); IIsqUser(ingdbuser); @@ -537,26 +579,26 @@ INGconn *INGconnectDB(char *dbname, char *user, char *passwd) (char *)0, (char *)0, (char *)0, (char *)0, (char *)0, (char *)0, (char *)0, (char *)0, (char *)0, (char *)0); } -/* # line 574 "myingres.sc" */ /* host code */ +/* # line 611 "myingres.sc" */ /* host code */ } else { -/* # line 575 "myingres.sc" */ /* connect */ +/* # line 612 "myingres.sc" */ /* connect */ { IIsqInit(&sqlca); IIsqConnect(0,ingdbname,(char *)0, (char *)0, (char *)0, (char *)0, (char *)0, (char *)0, (char *)0, (char *)0, (char *)0, (char *)0, (char *)0, (char *)0, (char *)0); } -/* # line 576 "myingres.sc" */ /* host code */ +/* # line 613 "myingres.sc" */ /* host code */ } -/* # line 578 "myingres.sc" */ /* inquire_sql */ +/* # line 615 "myingres.sc" */ /* inquire_sql */ { IILQisInqSqlio((short *)0,1,32,31,conn_name,13); } -/* # line 579 "myingres.sc" */ /* inquire_sql */ +/* # line 616 "myingres.sc" */ /* inquire_sql */ { IILQisInqSqlio((short *)0,1,30,sizeof(sess_id),&sess_id,11); } -/* # line 581 "myingres.sc" */ /* host code */ +/* # line 618 "myingres.sc" */ /* host code */ bstrncpy(dbconn->dbname, ingdbname, sizeof(dbconn->dbname)); bstrncpy(dbconn->user, ingdbuser, sizeof(dbconn->user)); bstrncpy(dbconn->password, ingdbpasw, sizeof(dbconn->password)); @@ -571,12 +613,12 @@ void INGdisconnectDB(INGconn *dbconn) /* * TODO: check for any real use of dbconn: maybe whenn multithreaded? */ -/* # line 597 "myingres.sc" */ /* disconnect */ +/* # line 634 "myingres.sc" */ /* disconnect */ { IIsqInit(&sqlca); IIsqDisconnect(); } -/* # line 598 "myingres.sc" */ /* host code */ +/* # line 635 "myingres.sc" */ /* host code */ if (dbconn != NULL) { free(dbconn->msg); free(dbconn); @@ -584,16 +626,16 @@ void INGdisconnectDB(INGconn *dbconn) } char *INGerrorMessage(const INGconn *conn) { -/* # line 606 "myingres.sc" */ +/* # line 643 "myingres.sc" */ char errbuf[256]; -/* # line 608 "myingres.sc" */ +/* # line 645 "myingres.sc" */ -/* # line 610 "myingres.sc" */ /* inquire_ingres */ +/* # line 647 "myingres.sc" */ /* inquire_ingres */ { IILQisInqSqlio((short *)0,1,32,255,errbuf,63); } -/* # line 611 "myingres.sc" */ /* host code */ +/* # line 648 "myingres.sc" */ /* host code */ memcpy(conn->msg, &errbuf, 256); return conn->msg; } @@ -605,5 +647,5 @@ char *INGcmdTuples(INGresult *res) int INGputCopyEnd(INGconn *conn, const char *errormsg); int INGputCopyData(INGconn *conn, const char *buffer, int nbytes); */ -/* # line 625 "myingres.sc" */ +/* # line 662 "myingres.sc" */ #endif diff --git a/bacula/src/cats/myingres.h b/bacula/src/cats/myingres.h index 7f450651f1..45899b8c84 100644 --- a/bacula/src/cats/myingres.h +++ b/bacula/src/cats/myingres.h @@ -71,15 +71,15 @@ typedef struct ing_conn { } INGconn; /* ---Prototypes--- */ int INGcheck(void); -short INGgetCols(const char *stmt); +short INGgetCols(B_DB *mdb, const char *stmt); char *INGgetvalue(INGresult *res, int row_number, int column_number); bool INGgetisnull(INGresult *res, int row_number, int column_number); int INGntuples(const INGresult *res); int INGnfields(const INGresult *res); char *INGfname(const INGresult *res, int column_number); short INGftype(const INGresult *res, int column_number); -int INGexec(INGconn *db, const char *query); -INGresult *INGquery(INGconn *db, const char *query); +int INGexec(B_DB *mdb, INGconn *db, const char *query); +INGresult *INGquery(B_DB *mdb, INGconn *db, const char *query); void INGclear(INGresult *res); INGconn *INGconnectDB(char *dbname, char *user, char *passwd); void INGdisconnectDB(INGconn *dbconn); diff --git a/bacula/src/cats/myingres.sc b/bacula/src/cats/myingres.sc index 898a9a61fb..e065313f4f 100644 --- a/bacula/src/cats/myingres.sc +++ b/bacula/src/cats/myingres.sc @@ -32,7 +32,13 @@ * Stefan Reddig, June 2009 */ +/* The following is necessary so that we do not include + * the dummy external definition of DB. + */ +#define __SQL_C /* indicate that this is sql.c */ + #include "bacula.h" +#include "cats.h" #ifdef HAVE_INGRES EXEC SQL INCLUDE SQLCA; @@ -52,8 +58,9 @@ int INGcheck() return (sqlca.sqlcode < 0) ? sqlca.sqlcode : 0; } -short INGgetCols(const char *query) +short INGgetCols(B_DB *mdb, const char *query) { + bool stmt_free = false; EXEC SQL BEGIN DECLARE SECTION; char *stmt; EXEC SQL END DECLARE SECTION; @@ -66,7 +73,15 @@ short INGgetCols(const char *query) sqlda->sqln = number; - stmt = bstrdup(query); + /* + * See if we need to run this through the limit_filter. + */ + if (strstr(query, "LIMIT") != NULL) { + stmt = mdb->limit_filter->replace(query); + } else { + stmt = bstrdup(query); + stmt_free = true; + } EXEC SQL PREPARE s1 from :stmt; if (INGcheck() < 0) { @@ -83,13 +98,16 @@ short INGgetCols(const char *query) number = sqlda->sqld; bail_out: - free(stmt); + if (stmt_free) { + free(stmt); + } free(sqlda); return number; } -static inline IISQLDA *INGgetDescriptor(short numCols, const char *query) +static inline IISQLDA *INGgetDescriptor(B_DB *mdb, short numCols, const char *query) { + bool stmt_free = false; EXEC SQL BEGIN DECLARE SECTION; char *stmt; EXEC SQL END DECLARE SECTION; @@ -102,11 +120,21 @@ static inline IISQLDA *INGgetDescriptor(short numCols, const char *query) sqlda->sqln = numCols; - stmt = bstrdup(query); + /* + * See if we need to run this through the limit_filter. + */ + if (strstr(query, "LIMIT") != NULL) { + stmt = mdb->limit_filter->replace(query); + } else { + stmt = bstrdup(query); + stmt_free = true; + } EXEC SQL PREPARE s2 INTO :sqlda FROM :stmt; - free(stmt); + if (stmt_free) { + free(stmt); + } for (i = 0; i < sqlda->sqld; ++i) { /* @@ -471,20 +499,31 @@ short INGftype(const INGresult *res, int column_number) return res->fields[column_number].type; } -int INGexec(INGconn *conn, const char *query) +int INGexec(B_DB *mdb, INGconn *conn, const char *query) { + bool stmt_free = false; int check; EXEC SQL BEGIN DECLARE SECTION; int rowcount; char *stmt; EXEC SQL END DECLARE SECTION; - stmt = bstrdup(query); + /* + * See if we need to run this through the limit_filter. + */ + if (strstr(query, "LIMIT") != NULL) { + stmt = mdb->limit_filter->replace(query); + } else { + stmt = bstrdup(query); + stmt_free = true; + } rowcount = -1; EXEC SQL EXECUTE IMMEDIATE :stmt; - free(stmt); + if (stmt_free) { + free(stmt); + } if ((check = INGcheck()) < 0) { return check; @@ -498,7 +537,7 @@ int INGexec(INGconn *conn, const char *query) return rowcount; } -INGresult *INGquery(INGconn *conn, const char *query) +INGresult *INGquery(B_DB *mdb, INGconn *conn, const char *query) { /* * TODO: error handling @@ -506,9 +545,9 @@ INGresult *INGquery(INGconn *conn, const char *query) IISQLDA *desc = NULL; INGresult *res = NULL; int rows = -1; - int cols = INGgetCols(query); + int cols = INGgetCols(mdb, query); - desc = INGgetDescriptor(cols, query); + desc = INGgetDescriptor(mdb, cols, query); if (!desc) { return NULL; } diff --git a/bacula/src/cats/myingres.sh b/bacula/src/cats/myingres.sh index 88120e4d4d..f3a1a2da4a 100644 --- a/bacula/src/cats/myingres.sh +++ b/bacula/src/cats/myingres.sh @@ -84,15 +84,15 @@ typedef struct ing_conn { /* ---Prototypes--- */ int INGcheck(void); -short INGgetCols(const char *stmt); +short INGgetCols(B_DB *mdb, const char *stmt); char *INGgetvalue(INGresult *res, int row_number, int column_number); bool INGgetisnull(INGresult *res, int row_number, int column_number); int INGntuples(const INGresult *res); int INGnfields(const INGresult *res); char *INGfname(const INGresult *res, int column_number); short INGftype(const INGresult *res, int column_number); -int INGexec(INGconn *db, const char *query); -INGresult *INGquery(INGconn *db, const char *query); +int INGexec(B_DB *mdb, INGconn *db, const char *query); +INGresult *INGquery(B_DB *mdb, INGconn *db, const char *query); void INGclear(INGresult *res); INGconn *INGconnectDB(char *dbname, char *user, char *passwd); void INGdisconnectDB(INGconn *dbconn);