00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "rodsClient.h"
00027 #include "icatHighLevelRoutines.h"
00028 #include "icatMidLevelRoutines.h"
00029 #include "icatLowLevel.h"
00030 #define LIMIT_AUDIT_ACCESS 1
00031
00032
00033
00034 #ifdef EXTENDED_ICAT
00035 #include "extendedICAT.h"
00036 #endif
00037
00038 int logSQLGenQuery=0;
00039
00040 void icatGeneralQuerySetup();
00041 int insertWhere(char *condition, int option);
00042
00043
00044 #define MINIMUM_COL_SIZE 50
00045
00046 extern icatSessionStruct *chlGetRcs();
00047
00048 #ifdef EXTENDED_ICAT
00049 #define MAX_LINKS_TABLES_OR_COLUMNS 500+EXTENDED_TABLES_AND_COLUMNS
00050 #else
00051 #define MAX_LINKS_TABLES_OR_COLUMNS 500
00052 #endif
00053
00054 #define MAX_TSQL 100
00055
00056 #define MAX_SQL_SIZE_GQ MAX_SQL_SIZE_GENERAL_QUERY // JMC - backport 4848
00057
00058 int firstCall=1;
00059
00060 char selectSQL[MAX_SQL_SIZE_GQ];
00061 int selectSQLInitFlag;
00062 int doUpperCase;
00063 char fromSQL[MAX_SQL_SIZE_GQ];
00064 char whereSQL[MAX_SQL_SIZE_GQ];
00065 char orderBySQL[MAX_SQL_SIZE_GQ];
00066 char groupBySQL[MAX_SQL_SIZE_GQ];
00067 int mightNeedGroupBy;
00068 int fromCount;
00069 char accessControlUserName[MAX_NAME_LEN];
00070 char accessControlZone[MAX_NAME_LEN];
00071 int accessControlPriv;
00072 int accessControlControlFlag=0;
00073
00074 struct tlinks {
00075 int table1;
00076 int table2;
00077 char connectingSQL[MAX_TSQL];
00078 } Links [MAX_LINKS_TABLES_OR_COLUMNS];
00079
00080 int nLinks;
00081
00082 struct tTables {
00083 char tableName[NAME_LEN];
00084 char tableAlias[MAX_TSQL];
00085 int cycler;
00086 int flag;
00087 char tableAbbrev[2];
00088 } Tables [MAX_LINKS_TABLES_OR_COLUMNS];
00089
00090 int nTables;
00091
00092 struct tColumns {
00093 int defineValue;
00094 char columnName[NAME_LEN];
00095 char tableName[NAME_LEN];
00096 } Columns [MAX_LINKS_TABLES_OR_COLUMNS];
00097
00098 int nColumns;
00099
00100 int nToFind;
00101
00102 char tableAbbrevs;
00103
00104 int debug=0;
00105 int debug2=0;
00106
00107
00108
00109
00110
00111
00112 int fkFindName(char *tableName) {
00113 int i;
00114 for (i=0;i<nTables;i++) {
00115 if (strcmp(Tables[i].tableName, tableName)==0) {
00116 return(i);
00117 }
00118 }
00119 rodsLog(LOG_ERROR, "fkFindName table %s unknown", tableName);
00120 return(0);
00121 }
00122
00123
00124
00125
00126
00127
00128 int
00129 sFklink(char *table1, char *table2, char *connectingSQL) {
00130 if (nLinks >= MAX_LINKS_TABLES_OR_COLUMNS) {
00131 rodsLog(LOG_ERROR, "fklink table full %d", CAT_TOO_MANY_TABLES );
00132 return( CAT_TOO_MANY_TABLES );
00133 }
00134 Links[nLinks].table1 = fkFindName(table1);
00135 Links[nLinks].table2 = fkFindName(table2);
00136 strncpy(Links[nLinks].connectingSQL, connectingSQL, MAX_TSQL);
00137 if (debug>1) printf("link %d is from %d to %d\n", nLinks,
00138 Links[nLinks].table1,
00139 Links[nLinks].table2);
00140 if (debug2) printf("T%2.2d L%2.2d T%2.2d\n",
00141 Links[nLinks].table1, nLinks,
00142 Links[nLinks].table2);
00143 nLinks++;
00144 return(0);
00145 }
00146
00147
00148
00149
00150 int
00151 sTableInit()
00152 {
00153 nLinks=0;
00154 nTables=0;
00155 nColumns=0;
00156 memset (Links, 0, sizeof (Links));
00157 memset (Tables, 0, sizeof (Tables));
00158 memset (Columns, 0, sizeof (Columns));
00159 return(0);
00160 }
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174 int
00175 sTable(char *tableName, char *tableAlias, int cycler) {
00176 if (nTables >= MAX_LINKS_TABLES_OR_COLUMNS) {
00177 rodsLog(LOG_ERROR, "sTable table full %d", CAT_TOO_MANY_TABLES );
00178 return( CAT_TOO_MANY_TABLES );
00179 }
00180 strncpy(Tables[nTables].tableName, tableName, NAME_LEN);
00181 strncpy(Tables[nTables].tableAlias, tableAlias, MAX_TSQL);
00182 Tables[nTables].cycler = cycler;
00183 if (debug>1) printf("table %d is %s\n", nTables, tableName);
00184 nTables++;
00185 return(0);
00186 }
00187
00188 int
00189 sColumn(int defineVal, char *tableName, char *columnName) {
00190 if (nColumns >= MAX_LINKS_TABLES_OR_COLUMNS) {
00191 rodsLog(LOG_ERROR, "sTable table full %d", CAT_TOO_MANY_TABLES );
00192 return( CAT_TOO_MANY_TABLES );
00193 }
00194 strncpy(Columns[nColumns].tableName, tableName, NAME_LEN);
00195 strncpy(Columns[nColumns].columnName, columnName, NAME_LEN);
00196 Columns[nColumns].defineValue = defineVal;
00197 if (debug>1) printf("column %d is %d %s %s\n",
00198 nColumns, defineVal, tableName, columnName);
00199 nColumns++;
00200 return(0);
00201 }
00202
00203
00204
00205 int
00206 sGetColumnInfo(int defineVal, char **tableName, char **columnName) {
00207 int i;
00208 for (i=0;i<nColumns;i++) {
00209 if (Columns[i].defineValue==defineVal) {
00210 *tableName=Columns[i].tableName;
00211 *columnName=Columns[i].columnName;
00212 return(0);
00213 }
00214 }
00215 return(CAT_INVALID_ARGUMENT);
00216 }
00217
00218
00219
00220
00221
00222
00223
00224
00225 int
00226 tablePresent(char *table, char *sqlText) {
00227 int tokens, blank;
00228 char *cp1, *cp2;
00229
00230 if (debug>1) printf("tablePresent table:%s:\n",table);
00231 if (debug>1) printf("tablePresent sqlText:%s:\n",sqlText);
00232
00233 if (strstr(sqlText, table)==NULL) {
00234 if (debug>1) printf("tablePresent return 0 (simple)\n");
00235 return(0);
00236 }
00237
00238 tokens=0;
00239 blank=1;
00240 cp1=table;
00241 for (;*cp1!='\0' && *cp1!=',';cp1++) {
00242 if (*cp1==' ') {
00243 if (blank==0) tokens++;
00244 blank=1;
00245 }
00246 else {
00247 blank=0;
00248 }
00249 }
00250 if (blank==0) tokens++;
00251
00252 if (debug>1) printf("tablePresent tokens=%d\n",tokens);
00253 if (tokens==2) {
00254 return(1);
00255 }
00256
00257
00258 blank=1;
00259 cp1=sqlText;
00260 for(;;) {
00261 cp2=strstr(cp1, table);
00262 if (cp2==NULL) {
00263 return(0);
00264 }
00265 tokens=0;
00266 for (;*cp2!='\0' && *cp2!=',';cp2++) {
00267 if (*cp2==' ') {
00268 if (blank==0) tokens++;
00269 blank=1;
00270 }
00271 else {
00272 blank=0;
00273 }
00274 }
00275 if (blank==0) tokens++;
00276 if (tokens==1) return(1);
00277 cp1=cp2;
00278 }
00279 }
00280
00281
00282
00283
00284 int
00285 tScan(int table, int link) {
00286 int thisKeep;
00287 int subKeep;
00288 int i;
00289
00290 if (debug>1) printf("%d tScan\n",table);
00291
00292 thisKeep = 0;
00293 if (Tables[table].flag==1) {
00294 thisKeep=1;
00295 Tables[table].flag=2;
00296 nToFind--;
00297 if (debug>1) printf("nToFind decremented, now=%d\n",nToFind);
00298 thisKeep=1;
00299 if (nToFind <= 0) return(thisKeep);
00300 }
00301 else {
00302 if (Tables[table].flag!=0) {
00303 if (debug>1) printf("%d returning flag=%d\n",table, Tables[table].flag);
00304 return(0);
00305 }
00306 }
00307 if (Tables[table].cycler==1) {
00308 if (debug>1) printf("%d returning cycler\n", table);
00309 return(thisKeep);
00310 }
00311
00312 Tables[table].flag=3;
00313
00314 for (i=0;i<nLinks; i++) {
00315 if (Links[i].table1 == table && link!=i ) {
00316 if (debug>1) printf("%d trying link %d forward\n", table, i);
00317 subKeep = tScan(Links[i].table2, i);
00318 if (debug>1) printf("subKeep %d, this table %d, link %d, table2 %d\n",
00319 subKeep, table, i, Links[i].table2);
00320 if (subKeep) {
00321 thisKeep=1;
00322 if (debug>1) printf("%d use link %d\n", table, i);
00323 if (strlen(whereSQL)>6) rstrcat(whereSQL, " AND ", MAX_SQL_SIZE_GQ);
00324 rstrcat(whereSQL, Links[i].connectingSQL, MAX_SQL_SIZE_GQ);
00325 rstrcat(whereSQL, " ", MAX_SQL_SIZE_GQ);
00326 if (tablePresent(Tables[Links[i].table2].tableAlias,fromSQL)==0){
00327 rstrcat(fromSQL, ", ", MAX_SQL_SIZE_GQ);
00328 rstrcat(fromSQL, Tables[Links[i].table2].tableAlias,
00329 MAX_SQL_SIZE_GQ);
00330 rstrcat(fromSQL, " ", MAX_SQL_SIZE_GQ);
00331 }
00332 if (tablePresent(Tables[Links[i].table1].tableAlias, fromSQL)==0){
00333 rstrcat(fromSQL, ", ", MAX_SQL_SIZE_GQ);
00334 rstrcat(fromSQL, Tables[Links[i].table1].tableAlias,
00335 MAX_SQL_SIZE_GQ);
00336 rstrcat(fromSQL, " ", MAX_SQL_SIZE_GQ);
00337 }
00338 if (debug>1) printf("added (2) to fromSQL: %s\n", fromSQL);
00339 if (nToFind <= 0) return(thisKeep);
00340 }
00341 }
00342 }
00343 for (i=0;i<nLinks; i++) {
00344 if (Links[i].table2 == table && link!=i ) {
00345 if (debug>1) printf("%d trying link %d backward\n", table, i);
00346 subKeep = tScan(Links[i].table1, i);
00347 if (debug>1) printf("subKeep %d, this table %d, link %d, table1 %d\n",
00348 subKeep, table, i, Links[i].table1);
00349 if (subKeep) {
00350 thisKeep=1;
00351 if (debug>1) printf("%d use link %d\n", table, i);
00352 if (strlen(whereSQL)>6) rstrcat(whereSQL, " AND ", MAX_SQL_SIZE_GQ);
00353 rstrcat(whereSQL, Links[i].connectingSQL, MAX_SQL_SIZE_GQ);
00354 rstrcat(whereSQL, " ", MAX_SQL_SIZE_GQ);
00355 if (tablePresent(Tables[Links[i].table2].tableAlias, fromSQL)==0){
00356 rstrcat(fromSQL, ", ", MAX_SQL_SIZE_GQ);
00357 rstrcat(fromSQL, Tables[Links[i].table2].tableAlias,
00358 MAX_SQL_SIZE_GQ);
00359 rstrcat(fromSQL, " ", MAX_SQL_SIZE_GQ);
00360 }
00361 if (tablePresent(Tables[Links[i].table1].tableAlias, fromSQL)==0){
00362 rstrcat(fromSQL, ", ", MAX_SQL_SIZE_GQ);
00363 rstrcat(fromSQL, Tables[Links[i].table1].tableAlias,
00364 MAX_SQL_SIZE_GQ);
00365 rstrcat(fromSQL, " ", MAX_SQL_SIZE_GQ);
00366 }
00367 if (debug>1) printf("added (3) to fromSQL: %s\n", fromSQL);
00368 if (nToFind <= 0) return(thisKeep);
00369 }
00370 }
00371 }
00372 if (debug>1) printf("%d returning %d\n",table, thisKeep);
00373 return(thisKeep);
00374 }
00375
00376
00377 int
00378 sTest(int i1, int i2) {
00379 int i;
00380 int keepVal;
00381
00382 if (firstCall) {
00383 icatGeneralQuerySetup();
00384 }
00385 firstCall=0;
00386
00387 for (i=0;i<nTables;i++) {
00388 Tables[i].flag=0;
00389 if (i==i1 || i==i2) Tables[i].flag=1;
00390 }
00391 nToFind=2;
00392 keepVal = tScan(i1, -1);
00393 if (keepVal!=1 || nToFind!=0) {
00394 printf("error failed to link %d to %d\n", i1, i2);
00395 }
00396 else {
00397 printf("SUCCESS linking %d to %d\n", i1, i2);
00398 }
00399 return(0);
00400 }
00401
00402 int sTest2(int i1, int i2, int i3) {
00403 int i;
00404 int keepVal;
00405
00406 if (firstCall) {
00407 icatGeneralQuerySetup();
00408 }
00409 firstCall=0;
00410
00411 for (i=0;i<nTables;i++) {
00412 Tables[i].flag=0;
00413 if (i==i1 || i==i2 || i==i3) Tables[i].flag=1;
00414 }
00415 nToFind=3;
00416 keepVal = tScan(i1, -1);
00417 if (keepVal!=1 || nToFind!=0) {
00418 printf("error failed to link %d, %d and %d\n", i1, i2, i3);
00419 }
00420 else {
00421 printf("SUCCESS linking %d, %d, %d\n", i1, i2, i3);
00422 }
00423 return(0);
00424 }
00425
00426
00427
00428
00429
00430 int
00431 tCycleChk(int table, int link, int thisTreeNum) {
00432 int thisKeep;
00433 int subKeep;
00434 int i;
00435
00436 if (debug>1) printf("%d tCycleChk\n",table);
00437
00438 thisKeep = 0;
00439
00440 if (Tables[table].flag != 0) {
00441 if (Tables[table].flag == thisTreeNum) {
00442 if (debug>1) printf("Found cycle at node %d\n", table);
00443 return(1);
00444 }
00445 }
00446 Tables[table].flag = thisTreeNum;
00447
00448 if (Tables[table].cycler==1) {
00449 if (debug>1) printf("%d returning cycler\n", table);
00450 return(thisKeep);
00451 }
00452
00453 for (i=0;i<nLinks; i++) {
00454 if (Links[i].table1 == table && link!=i ) {
00455 if (debug>1) printf("%d trying link %d forward\n", table, i);
00456 subKeep = tCycleChk(Links[i].table2, i, thisTreeNum);
00457 if (subKeep) {
00458 thisKeep=1;
00459 if (debug>1) printf("%d use link %d tree %d\n", table, i,
00460 thisTreeNum);
00461 return(thisKeep);
00462 }
00463 }
00464 }
00465 for (i=0;i<nLinks; i++) {
00466 if (Links[i].table2 == table && link!=i ) {
00467 if (debug>1) printf("%d trying link %d backward\n", table, i);
00468 subKeep = tCycleChk(Links[i].table1, i, thisTreeNum);
00469 if (subKeep) {
00470 thisKeep=1;
00471 if (debug>1) printf("%d use link %d\n", table, i);
00472 return(thisKeep);
00473 }
00474 }
00475 }
00476 if (debug>1) printf("%d returning %d\n",table, thisKeep);
00477 return(thisKeep);
00478 }
00479
00480
00481
00482
00483
00484
00485
00486
00487 int findCycles(int startTable)
00488 {
00489 int i, status;
00490 int treeNum;
00491
00492 if (firstCall) {
00493 icatGeneralQuerySetup();
00494 }
00495 firstCall=0;
00496
00497 for (i=0;i<nTables;i++) {
00498 Tables[i].flag=0;
00499 }
00500 treeNum=0;
00501
00502 if (startTable != 0) {
00503 if (startTable > nTables) return(CAT_INVALID_ARGUMENT);
00504 treeNum++;
00505 status = tCycleChk(startTable, -1, treeNum);
00506 if (debug>1) printf("tree %d status %d\n", treeNum, status);
00507 if (status) return(status);
00508 }
00509
00510 for (i=0;i<nTables;i++) {
00511 if (Tables[i].flag==0) {
00512 treeNum++;
00513 status = tCycleChk(i, -1, treeNum);
00514 if (debug>1) printf("tree %d status %d\n", treeNum, status);
00515 if (status) return(status);
00516 }
00517 }
00518 return(0);
00519 }
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529 int setTable(int column, int sel, int selectOption, int castOption) {
00530 int colIx;
00531 int i;
00532 int selectOptFlag;
00533
00534 colIx=-1;
00535 for (i=0;i<nColumns;i++) {
00536 if (Columns[i].defineValue == column) colIx=i;
00537 }
00538 if (colIx == -1) return(CAT_UNKNOWN_TABLE);
00539
00540 for (i=0; i<nTables;i++) {
00541 if (strcmp(Tables[i].tableName, Columns[colIx].tableName) == 0) {
00542 if (Tables[i].flag==0) {
00543 nToFind++;
00544 Tables[i].tableAbbrev[0]=tableAbbrevs;
00545 Tables[i].tableAbbrev[1]='\0';
00546 tableAbbrevs++;
00547 }
00548 Tables[i].flag=1;
00549 if (sel) {
00550 if (selectSQLInitFlag==0) rstrcat(selectSQL, ",", MAX_SQL_SIZE_GQ);
00551 selectSQLInitFlag=0;
00552
00553 selectOptFlag=0;
00554 if (selectOption != 0) {
00555 if (selectOption == SELECT_MIN) {
00556 rstrcat(selectSQL, "min(", MAX_SQL_SIZE_GQ);
00557 selectOptFlag=1;
00558 }
00559 if (selectOption == SELECT_MAX) {
00560 rstrcat(selectSQL, "max(", MAX_SQL_SIZE_GQ);
00561 selectOptFlag=1;
00562 }
00563 if (selectOption == SELECT_SUM) {
00564 rstrcat(selectSQL, "sum(", MAX_SQL_SIZE_GQ);
00565 selectOptFlag=1;
00566 }
00567 if (selectOption == SELECT_AVG) {
00568 rstrcat(selectSQL, "avg(", MAX_SQL_SIZE_GQ);
00569 selectOptFlag=1;
00570 }
00571 if (selectOption == SELECT_COUNT) {
00572 rstrcat(selectSQL, "count(", MAX_SQL_SIZE_GQ);
00573 selectOptFlag=1;
00574 }
00575 }
00576 rstrcat(selectSQL, Tables[i].tableName, MAX_SQL_SIZE_GQ);
00577 rstrcat(selectSQL, ".", MAX_SQL_SIZE_GQ);
00578 rstrcat(selectSQL, Columns[colIx].columnName, MAX_SQL_SIZE_GQ);
00579 rstrcat(selectSQL, " ", MAX_SQL_SIZE_GQ);
00580 if (selectOptFlag) {
00581 rstrcat(selectSQL, ") ", MAX_SQL_SIZE_GQ);
00582 mightNeedGroupBy=1;
00583 }
00584 else {
00585 if (strlen(groupBySQL)>10) rstrcat(groupBySQL,",",MAX_SQL_SIZE_GQ);
00586 rstrcat(groupBySQL, Tables[i].tableName, MAX_SQL_SIZE_GQ);
00587 rstrcat(groupBySQL, ".", MAX_SQL_SIZE_GQ);
00588 rstrcat(groupBySQL, Columns[colIx].columnName, MAX_SQL_SIZE_GQ);
00589 rstrcat(groupBySQL, " ", MAX_SQL_SIZE_GQ);
00590 }
00591
00592 if (tablePresent(Tables[i].tableAlias, fromSQL)==0){
00593 if (fromCount) {
00594 rstrcat(fromSQL, ", ", MAX_SQL_SIZE_GQ);
00595 }
00596 else {
00597 rstrcat(fromSQL, " ", MAX_SQL_SIZE_GQ);
00598 }
00599 fromCount++;
00600 rstrcat(fromSQL, Tables[i].tableAlias, MAX_SQL_SIZE_GQ);
00601 rstrcat(fromSQL, " ", MAX_SQL_SIZE_GQ);
00602 }
00603 if (debug>1) printf("added (1) to fromSQL: %s\n", fromSQL);
00604 }
00605 else {
00606
00607 if (strlen(whereSQL)>6) rstrcat(whereSQL, " AND ", MAX_SQL_SIZE_GQ);
00608 if (castOption==1) {
00609 rstrcat(whereSQL, "cast (", MAX_SQL_SIZE_GQ);
00610 }
00611
00612 if (doUpperCase==1) {
00613 rstrcat(whereSQL, "upper (", MAX_SQL_SIZE_GQ);
00614 }
00615
00616 rstrcat(whereSQL, Tables[i].tableName, MAX_SQL_SIZE_GQ);
00617 rstrcat(whereSQL, ".", MAX_SQL_SIZE_GQ);
00618 rstrcat(whereSQL, Columns[colIx].columnName, MAX_SQL_SIZE_GQ);
00619
00620 if (doUpperCase==1) {
00621 rstrcat(whereSQL, " )", MAX_SQL_SIZE_GQ);
00622 }
00623
00624 if (castOption==1) {
00625
00626
00627
00628 #if ORA_ICAT
00629 rstrcat(whereSQL, " as number)", MAX_SQL_SIZE_GQ);
00630 #else
00631 rstrcat(whereSQL, " as decimal)", MAX_SQL_SIZE_GQ);
00632 #endif
00633 }
00634 }
00635 if (debug>1) printf("table index=%d, nToFind=%d\n",i, nToFind);
00636 return(i);
00637 }
00638 }
00639 return(-1);
00640 }
00641
00642
00643
00644
00645 void
00646 handleMultiDataAVUConditions(int nConditions) {
00647 int i;
00648 char *firstItem, *nextItem;
00649 char nextStr[20];
00650
00651
00652
00653
00654 firstItem = strstr(whereSQL, "r_data_meta_main.meta_attr_name");
00655 if (firstItem != NULL) {
00656 *firstItem = 'x';
00657 }
00658 for (i=2;i<=nConditions;i++) {
00659 nextItem = strstr(whereSQL, "r_data_meta_main.meta_attr_name");
00660 if (nextItem != NULL) {
00661 snprintf(nextStr, sizeof nextStr, "n%2.2d", i);
00662 *(nextItem+13)=nextStr[0];
00663 *(nextItem+14)=nextStr[1];
00664 *(nextItem+15)=nextStr[2];
00665 }
00666 }
00667 if (firstItem != NULL) {
00668 *firstItem = 'r';
00669 }
00670
00671
00672 firstItem = strstr(whereSQL, "r_data_meta_main.meta_attr_value");
00673 if (firstItem != NULL) {
00674 *firstItem = 'x';
00675 }
00676 for (i=2;i<=nConditions;i++) {
00677 nextItem = strstr(whereSQL, "r_data_meta_main.meta_attr_value");
00678 if (nextItem != NULL) {
00679 snprintf(nextStr, sizeof nextStr, "n%2.2d", i);
00680 *(nextItem+13)=nextStr[0];
00681 *(nextItem+14)=nextStr[1];
00682 *(nextItem+15)=nextStr[2];
00683 }
00684 }
00685 if (firstItem != NULL) {
00686 *firstItem = 'r';
00687 }
00688
00689
00690
00691
00692 for (i=2;i<=nConditions;i++) {
00693 char newStr[100];
00694 snprintf(newStr, sizeof newStr,
00695 ", R_OBJT_METAMAP r_data_metamap%d, R_META_MAIN r_data_meta_mn%2.2d ",
00696 i, i);
00697 rstrcat(fromSQL, newStr, MAX_SQL_SIZE_GQ);
00698 }
00699
00700
00701
00702
00703
00704 for (i=2;i<=nConditions;i++) {
00705 char newStr[100];
00706 snprintf(newStr, sizeof newStr,
00707 " AND r_data_metamap%d.meta_id = r_data_meta_mn%2.2d.meta_id", i, i);
00708 rstrcat(whereSQL, newStr, MAX_SQL_SIZE_GQ);
00709 snprintf(newStr, sizeof newStr,
00710 " AND R_DATA_MAIN.data_id = r_data_metamap%d.object_id ", i);
00711 rstrcat(whereSQL, newStr, MAX_SQL_SIZE_GQ);
00712 }
00713 }
00714
00715
00716
00717
00718 void
00719 handleMultiCollAVUConditions(int nConditions) {
00720 int i;
00721 char *firstItem, *nextItem;
00722 char nextStr[20];
00723
00724
00725
00726
00727 firstItem = strstr(whereSQL, "r_coll_meta_main.meta_attr_name");
00728 if (firstItem != NULL) {
00729 *firstItem = 'x';
00730 }
00731 for (i=2;i<=nConditions;i++) {
00732 nextItem = strstr(whereSQL, "r_coll_meta_main.meta_attr_name");
00733 if (nextItem != NULL) {
00734 snprintf(nextStr, sizeof nextStr, "n%2.2d", i);
00735 *(nextItem+13)=nextStr[0];
00736 *(nextItem+14)=nextStr[1];
00737 *(nextItem+15)=nextStr[2];
00738 }
00739 }
00740 if (firstItem != NULL) {
00741 *firstItem = 'r';
00742 }
00743
00744
00745 firstItem = strstr(whereSQL, "r_coll_meta_main.meta_attr_value");
00746 if (firstItem != NULL) {
00747 *firstItem = 'x';
00748 }
00749 for (i=2;i<=nConditions;i++) {
00750 nextItem = strstr(whereSQL, "r_coll_meta_main.meta_attr_value");
00751 if (nextItem != NULL) {
00752 snprintf(nextStr, sizeof nextStr, "n%2.2d", i);
00753 *(nextItem+13)=nextStr[0];
00754 *(nextItem+14)=nextStr[1];
00755 *(nextItem+15)=nextStr[2];
00756 }
00757 }
00758 if (firstItem != NULL) {
00759 *firstItem = 'r';
00760 }
00761
00762
00763
00764
00765 for (i=2;i<=nConditions;i++) {
00766 char newStr[100];
00767 snprintf(newStr, sizeof newStr,
00768 ", R_OBJT_METAMAP r_coll_metamap%d, R_META_MAIN r_coll_meta_mn%2.2d ",
00769 i, i);
00770 rstrcat(fromSQL, newStr, MAX_SQL_SIZE_GQ);
00771 }
00772
00773
00774
00775
00776
00777 for (i=2;i<=nConditions;i++) {
00778 char newStr[100];
00779 snprintf(newStr, sizeof newStr,
00780 " AND r_coll_metamap%d.meta_id = r_coll_meta_mn%2.2d.meta_id", i, i);
00781 rstrcat(whereSQL, newStr, MAX_SQL_SIZE_GQ);
00782 snprintf(newStr, sizeof newStr,
00783 " AND R_COLL_MAIN.coll_id = r_coll_metamap%d.object_id ", i);
00784 rstrcat(whereSQL, newStr, MAX_SQL_SIZE_GQ);
00785 }
00786 }
00787
00788
00789
00790
00791
00792
00793
00794
00795
00796 int
00797 compoundConditionSpecified(char *condition) {
00798 char myCondition[MAX_NAME_LEN*2];
00799 int quote;
00800 char *cptr;
00801
00802
00803 if (strstr(condition, "||") == NULL &&
00804 strstr(condition, "&&") == NULL) {
00805 return(0);
00806 }
00807
00808
00809 strncpy(myCondition, condition, MAX_NAME_LEN*2);
00810 for (cptr=myCondition,quote=0;*cptr!='\0';cptr++) {
00811 if (*cptr=='\'') {
00812 if (quote==0) quote=1;
00813 else quote=0;
00814 *cptr=' ';
00815 }
00816 if (quote==1) {
00817 *cptr=' ';
00818 }
00819 }
00820
00821
00822 if (strstr(myCondition, "||") == NULL &&
00823 strstr(myCondition, "&&") == NULL) {
00824 return(0);
00825 }
00826 return(1);
00827 }
00828
00829
00830
00831
00832
00833
00834 int
00835 handleCompoundCondition(char *condition, int prevWhereLen)
00836 {
00837 char tabAndColumn[MAX_SQL_SIZE_GQ];
00838 char condPart1[MAX_NAME_LEN*2];
00839 static char condPart2[MAX_NAME_LEN*2];
00840 static char conditionsForBind[MAX_NAME_LEN*2];
00841 static int conditionsForBindIx=0;
00842 int type;
00843 char *cptr;
00844 int status;
00845 int i;
00846 int first=1;
00847 int keepGoing=1;
00848
00849 if (prevWhereLen < 0) {
00850 conditionsForBindIx=0;
00851 return(0);
00852 }
00853
00854 rstrcpy(condPart1, condition, MAX_NAME_LEN*2);
00855
00856 while (keepGoing) {
00857 type=0;
00858 cptr = strstr(condPart1, "||");
00859 if (cptr != NULL) {
00860 type=1;
00861 }
00862 else {
00863 cptr = strstr(condPart1, "&&");
00864 type=2;
00865 }
00866 if (type) {
00867 *cptr='\0';
00868 cptr+=2;
00869 rstrcpy(condPart2, cptr, MAX_NAME_LEN*2);
00870 }
00871
00872 if (first) {
00873
00874 i = prevWhereLen;
00875 if (whereSQL[i]==' ') i++;
00876 if (whereSQL[i]==' ') i++;
00877 if (whereSQL[i]=='A') {
00878 i++;
00879 if (whereSQL[i]=='N') {
00880 i++;
00881 if (whereSQL[i]=='D') {
00882 i++;
00883 prevWhereLen=i;
00884 }
00885 }
00886 }
00887
00888 rstrcpy(tabAndColumn, (char *)&whereSQL[prevWhereLen], MAX_SQL_SIZE_GQ);
00889 whereSQL[prevWhereLen]='\0';
00890 rstrcat(whereSQL, " ( ", MAX_SQL_SIZE_GQ);
00891 }
00892 rstrcat(whereSQL, tabAndColumn, MAX_SQL_SIZE_GQ);
00893 rstrcpy((char*)&conditionsForBind[conditionsForBindIx], condPart1,
00894 (MAX_SQL_SIZE_GQ*2)-conditionsForBindIx);
00895 status = insertWhere((char*)&conditionsForBind[conditionsForBindIx], 0);
00896 if (status) return(status);
00897 conditionsForBindIx+=strlen(condPart1)+1;
00898
00899 if (type==1) {
00900 rstrcat(whereSQL, " OR ", MAX_SQL_SIZE_GQ);
00901 }
00902 else {
00903 rstrcat(whereSQL, " AND ", MAX_SQL_SIZE_GQ);
00904 }
00905
00906 if (strstr(condPart2, "||") == NULL &&
00907 strstr(condPart2, "&&") == NULL) {
00908 rstrcat(whereSQL, tabAndColumn, MAX_SQL_SIZE_GQ);
00909 status = insertWhere(condPart2, 0);
00910 if (status) return(status);
00911 keepGoing=0;
00912 }
00913 else {
00914 rstrcpy(condPart1, condPart2, MAX_NAME_LEN*2);
00915 }
00916 first=0;
00917 }
00918
00919 rstrcat(whereSQL, " ) ", MAX_SQL_SIZE_GQ);
00920 return(0);
00921 }
00922
00923
00924 void
00925 setOrderBy(genQueryInp_t genQueryInp, int column) {
00926 int i, j;
00927 int selectOpt, isAggregated;
00928 for (i=0;i<genQueryInp.selectInp.len;i++) {
00929 if (genQueryInp.selectInp.inx[i] == column) {
00930 selectOpt = genQueryInp.selectInp.value[i]&0xf;
00931 isAggregated=0;
00932 if (selectOpt == SELECT_MIN) isAggregated=1;
00933 if (selectOpt == SELECT_MAX) isAggregated=1;
00934 if (selectOpt == SELECT_SUM) isAggregated=1;
00935 if (selectOpt == SELECT_AVG) isAggregated=1;
00936 if (selectOpt == SELECT_COUNT) isAggregated=1;
00937 if (isAggregated==0) {
00938 for (j=0;j<nColumns;j++) {
00939 if (Columns[j].defineValue == column) {
00940 if (strlen(orderBySQL)>10) {
00941 rstrcat(orderBySQL, ", ", MAX_SQL_SIZE_GQ);
00942 }
00943 rstrcat(orderBySQL, Columns[j].tableName, MAX_SQL_SIZE_GQ);
00944 rstrcat(orderBySQL, ".", MAX_SQL_SIZE_GQ);
00945 rstrcat(orderBySQL, Columns[j].columnName, MAX_SQL_SIZE_GQ);
00946 break;
00947 }
00948 }
00949 }
00950 }
00951 }
00952 }
00953
00954
00955
00956
00957 void
00958 setOrderByUser(genQueryInp_t genQueryInp) {
00959 int i, j, done;
00960 for (i=0;i<genQueryInp.selectInp.len;i++) {
00961 if ( (genQueryInp.selectInp.value[i] & ORDER_BY) ||
00962 (genQueryInp.selectInp.value[i] & ORDER_BY_DESC) ) {
00963 done=0;
00964 for (j=0;j<nColumns && done==0;j++) {
00965 if (Columns[j].defineValue == genQueryInp.selectInp.inx[i]) {
00966 if (strlen(orderBySQL)>10) {
00967 rstrcat(orderBySQL, ", ", MAX_SQL_SIZE_GQ);
00968 }
00969 rstrcat(orderBySQL, Columns[j].tableName, MAX_SQL_SIZE_GQ);
00970 rstrcat(orderBySQL, ".", MAX_SQL_SIZE_GQ);
00971 rstrcat(orderBySQL, Columns[j].columnName, MAX_SQL_SIZE_GQ);
00972 if (genQueryInp.selectInp.value[i] & ORDER_BY_DESC) {
00973 rstrcat(orderBySQL, " DESC ", MAX_SQL_SIZE_GQ);
00974 }
00975 done=1;
00976 }
00977 }
00978 }
00979 }
00980 }
00981
00982 int
00983 setBlank(char *string, int count) {
00984 int i;
00985 char *cp;
00986 for (cp=string,i=0;i<count;i++) {
00987 *cp++=' ';
00988 }
00989 return(0);
00990 }
00991
00992
00993
00994
00995 int
00996 checkCondition(char *condition) {
00997 char tmpStr[25];
00998 char *cp;
00999
01000 rstrcpy(tmpStr, condition, 20);
01001 for (cp=tmpStr;*cp!='\0';cp++) {
01002 if (*cp=='<') *cp=' ';
01003 if (*cp=='>') *cp=' ';
01004 if (*cp=='=') *cp=' ';
01005 if (*cp=='!') *cp=' ';
01006 }
01007 cp = strstr(tmpStr,"begin_of");
01008 if (cp != NULL) setBlank(cp, 8);
01009 cp = strstr(tmpStr,"parent_of");
01010 if (cp != NULL) setBlank(cp, 9);
01011 cp = strstr(tmpStr,"not");
01012 if (cp != NULL) setBlank(cp, 3);
01013 cp = strstr(tmpStr,"NOT");
01014 if (cp != NULL) setBlank(cp, 3);
01015 cp = strstr(tmpStr,"between");
01016 if (cp != NULL) setBlank(cp, 7);
01017 cp = strstr(tmpStr,"BETWEEN");
01018 if (cp != NULL) setBlank(cp, 7);
01019 cp = strstr(tmpStr,"like");
01020 if (cp != NULL) setBlank(cp, 4);
01021 cp = strstr(tmpStr,"LIKE");
01022 if (cp != NULL) setBlank(cp, 4);
01023 cp = strstr(tmpStr,"in");
01024 if (cp != NULL) setBlank(cp, 2);
01025 cp = strstr(tmpStr,"IN");
01026 if (cp != NULL) setBlank(cp, 2);
01027
01028 for (cp=tmpStr;*cp!='\0';cp++) {
01029 if (*cp != ' ') return(CAT_INVALID_ARGUMENT);
01030 }
01031 return(0);
01032 }
01033
01034
01035
01036
01037
01038 int
01039 addInClauseToWhereForParentOf(char *inArg) {
01040 int i, len;
01041 int nput=0;
01042 char tmpStr[MAX_SQL_SIZE_GQ];
01043 static char inStrings[MAX_SQL_SIZE_GQ];
01044 int inStrIx=0;
01045
01046 rstrcat(whereSQL, " IN (", MAX_SQL_SIZE_GQ);
01047 len = strlen(inArg);
01048 for (i=0;i<len+1;i++) {
01049 if (inArg[i]=='/' || inArg[i]==' ' || inArg[i]=='\0') {
01050 int ncopy=i;
01051 if (nput==0) ncopy++;
01052 if (nput==0) {
01053 rstrcat(whereSQL, "?", MAX_SQL_SIZE_GQ);
01054 }
01055 else {
01056 rstrcat(whereSQL, ", ?", MAX_SQL_SIZE_GQ);
01057 }
01058 nput++;
01059
01060
01061 tmpStr[0]='\0';
01062 rstrncat(tmpStr, inArg, ncopy, MAX_SQL_SIZE_GQ);
01063 rstrcpy((char *)&inStrings[inStrIx], tmpStr,
01064 (MAX_SQL_SIZE_GQ)-inStrIx);
01065 inStrings[inStrIx+ncopy]='\0';
01066 cllBindVars[cllBindVarCount++]=(char *)&inStrings[inStrIx];
01067 inStrIx = inStrIx+ncopy+1;
01068 }
01069 }
01070 rstrcat(whereSQL, ")", MAX_SQL_SIZE_GQ);
01071 return 0;
01072 }
01073
01074
01075
01076
01077
01078 int
01079 addInClauseToWhereForIn(char *inArg, int option) {
01080 int i, len;
01081 int startIx, endIx;
01082 int nput=0;
01083 int quoteState=0;
01084 char tmpStr[MAX_SQL_SIZE_GQ];
01085 static char inStrings[MAX_SQL_SIZE_GQ*2];
01086 static int inStrIx;
01087 int ncopy;
01088
01089 if (option==1) {
01090 inStrIx=0;
01091 return(0);
01092 }
01093 rstrcat(whereSQL, " IN (", MAX_SQL_SIZE_GQ);
01094 len = strlen(inArg);
01095 for (i=0;i<len+1;i++) {
01096 if (inArg[i]=='\'') {
01097 quoteState++;
01098 if (quoteState==1) {
01099 startIx=i+1;
01100 }
01101 if (quoteState==2) {
01102 quoteState=0;
01103 endIx = i-1;
01104 if (nput==0) {
01105 rstrcat(whereSQL, "?", MAX_SQL_SIZE_GQ);
01106 }
01107 else {
01108 rstrcat(whereSQL, ", ?", MAX_SQL_SIZE_GQ);
01109 }
01110 nput++;
01111
01112
01113
01114 tmpStr[0]='\0';
01115 ncopy = endIx-startIx+1;
01116 rstrncat(tmpStr, (char *)&inArg[startIx], ncopy, MAX_SQL_SIZE_GQ);
01117 rstrcpy((char *)&inStrings[inStrIx], tmpStr,
01118 (MAX_SQL_SIZE_GQ*2)-inStrIx);
01119 inStrings[inStrIx+ncopy]='\0';
01120 if (cllBindVarCount+1 >= MAX_BIND_VARS) {
01121 return(CAT_BIND_VARIABLE_LIMIT_EXCEEDED);
01122 }
01123
01124 cllBindVars[cllBindVarCount++]=(char *)&inStrings[inStrIx];
01125 inStrIx = inStrIx+ncopy+1;
01126 }
01127 }
01128 }
01129 rstrcat(whereSQL, ")", MAX_SQL_SIZE_GQ);
01130 if (nput==0) return(CAT_INVALID_ARGUMENT);
01131 return(0);
01132 }
01133
01134
01135
01136
01137 int
01138 addBetweenClauseToWhere(char *inArg) {
01139 int i, len;
01140 int startIx, endIx;
01141 int nput=0;
01142 int quoteState=0;
01143 char tmpStr[MAX_SQL_SIZE_GQ];
01144 static char inStrings[MAX_SQL_SIZE_GQ];
01145 int inStrIx=0;
01146 int ncopy;
01147 rstrcat(whereSQL, " BETWEEN ", MAX_SQL_SIZE_GQ);
01148 len = strlen(inArg);
01149 for (i=0;i<len+1;i++) {
01150 if (inArg[i]=='\'') {
01151 quoteState++;
01152 if (quoteState==1) {
01153 startIx=i+1;
01154 }
01155 if (quoteState==2) {
01156 quoteState=0;
01157 endIx = i-1;
01158 if (nput==0) {
01159 rstrcat(whereSQL, "?", MAX_SQL_SIZE_GQ);
01160 }
01161 else {
01162 rstrcat(whereSQL, " AND ? ", MAX_SQL_SIZE_GQ);
01163 }
01164 nput++;
01165
01166
01167
01168 tmpStr[0]='\0';
01169 ncopy = endIx-startIx+1;
01170 rstrncat(tmpStr, (char *)&inArg[startIx], ncopy, MAX_SQL_SIZE_GQ);
01171 rstrcpy((char *)&inStrings[inStrIx], tmpStr,
01172 MAX_SQL_SIZE_GQ-inStrIx);
01173 inStrings[inStrIx+ncopy]='\0';
01174 if (cllBindVarCount+1 >= MAX_BIND_VARS) {
01175 return(CAT_BIND_VARIABLE_LIMIT_EXCEEDED);
01176 }
01177
01178 cllBindVars[cllBindVarCount++]=(char *)&inStrings[inStrIx];
01179 inStrIx = inStrIx+ncopy+1;
01180 }
01181 }
01182 }
01183 if (nput!=2) return(CAT_INVALID_ARGUMENT);
01184 return(0);
01185 }
01186
01187
01188
01189
01190 int
01191 insertWhere(char *condition, int option) {
01192 static int bindIx=0;
01193 static char bindVars[MAX_SQL_SIZE_GQ+100];
01194 char *cp1, *cpFirstQuote, *cpSecondQuote;
01195 char *cp;
01196 int i;
01197 char *thisBindVar;
01198 char tmpStr[20];
01199 char myCondition[20];
01200 char *condStart;
01201
01202 if (option==1) {
01203 bindIx=0;
01204 addInClauseToWhereForIn(condition, option);
01205 return(0);
01206 }
01207
01208 condStart=condition;
01209 while (*condStart==' ') {condStart++;}
01210
01211 cp = strstr(condition, "in");
01212 if (cp == NULL) cp = strstr(condition, "IN");
01213 if (cp != NULL && cp==condStart) {
01214 return (addInClauseToWhereForIn(condition,0));
01215 }
01216
01217 cp = strstr(condition, "between");
01218 if (cp == NULL) cp = strstr(condition, "BETWEEN");
01219 if (cp != NULL && cp==condStart) {
01220 return (addBetweenClauseToWhere(condition));
01221 }
01222
01223 cpFirstQuote=0;
01224 cpSecondQuote=0;
01225 for (cp1=condition;*cp1!='\0';cp1++) {
01226 if (*cp1=='\'') {
01227 if (cpFirstQuote==0) {
01228 cpFirstQuote=cp1;
01229 }
01230 else {
01231 cpSecondQuote=cp1;
01232 }
01233 }
01234 }
01235 if (strcmp(condition, "IS NULL")==0) {
01236 rstrcat(whereSQL, " ", MAX_SQL_SIZE_GQ);
01237 rstrcat(whereSQL, condition, MAX_SQL_SIZE_GQ);
01238 rstrcat(whereSQL, " ", MAX_SQL_SIZE_GQ);
01239 return(0);
01240 }
01241 if (strcmp(condition, "IS NOT NULL")==0) {
01242 rstrcat(whereSQL, " ", MAX_SQL_SIZE_GQ);
01243 rstrcat(whereSQL, condition, MAX_SQL_SIZE_GQ);
01244 rstrcat(whereSQL, " ", MAX_SQL_SIZE_GQ);
01245 return(0);
01246 }
01247 bindIx++;
01248 thisBindVar=(char*)&bindVars[bindIx];
01249 if (cpFirstQuote==0 || cpSecondQuote==0) {
01250 return(CAT_INVALID_ARGUMENT);
01251 }
01252 if ((cpSecondQuote-cpFirstQuote)+bindIx > MAX_SQL_SIZE_GQ+90)
01253 return(CAT_INVALID_ARGUMENT);
01254
01255 for (cp1=cpFirstQuote+1;cp1<cpSecondQuote;cp1++) {
01256 bindVars[bindIx++]=*cp1;
01257 }
01258 bindVars[bindIx++]='\0';
01259 if (cllBindVarCount+1 >= MAX_BIND_VARS) {
01260 return(CAT_BIND_VARIABLE_LIMIT_EXCEEDED);
01261 }
01262
01263 cllBindVars[cllBindVarCount++]=thisBindVar;
01264
01265
01266 if ((cpFirstQuote-condition) > 10) return(CAT_INVALID_ARGUMENT);
01267
01268 tmpStr[0]=' ';
01269 i=1;
01270 for (cp1=condition;;) {
01271 tmpStr[i++]= *cp1++;
01272 if (cp1==cpFirstQuote) break;
01273 }
01274 tmpStr[i]='\0';
01275 rstrcpy(myCondition, tmpStr, 20);
01276
01277 cp = strstr(myCondition,"begin_of");
01278 if (cp != NULL) {
01279 char tmpStr2[MAX_SQL_SIZE_GQ];
01280 cp1=whereSQL+strlen(whereSQL)-1;
01281 while (*cp1!=' ') cp1--;
01282 cp1++;
01283 rstrcpy(tmpStr2, cp1, MAX_SQL_SIZE_GQ);
01284 #if ORA_ICAT
01285 rstrcat(whereSQL, "=substr(?,1,length(", MAX_SQL_SIZE_GQ);
01286 rstrcat(whereSQL, tmpStr2, MAX_SQL_SIZE_GQ);
01287 rstrcat(whereSQL, "))", MAX_SQL_SIZE_GQ);
01288 rstrcat(whereSQL, " AND length(", MAX_SQL_SIZE_GQ);
01289 #else
01290 rstrcat(whereSQL, "=substr(?,1,char_length(", MAX_SQL_SIZE_GQ);
01291 rstrcat(whereSQL, tmpStr2, MAX_SQL_SIZE_GQ);
01292 rstrcat(whereSQL, "))", MAX_SQL_SIZE_GQ);
01293 rstrcat(whereSQL, " AND char_length(", MAX_SQL_SIZE_GQ);
01294 #endif
01295 rstrcat(whereSQL, tmpStr2, MAX_SQL_SIZE_GQ);
01296 rstrcat(whereSQL, ")>0", MAX_SQL_SIZE_GQ);
01297 }
01298 else {
01299 cp = strstr(myCondition, "parent_of");
01300 if (cp != NULL) {
01301
01302
01303
01304
01305 cllBindVarCount--;
01306 int status = addInClauseToWhereForParentOf(thisBindVar);
01307 if (status < 0) return(status);
01308 }
01309 else {
01310 tmpStr[i++]='?';
01311 tmpStr[i++]=' ';
01312 tmpStr[i++]='\0';
01313 rstrcat(whereSQL, tmpStr, MAX_SQL_SIZE_GQ);
01314 }
01315 }
01316 return(checkCondition(myCondition));
01317 }
01318
01319
01320
01321
01322
01323
01324
01325 int
01326 genqAppendAccessCheck() {
01327 int doCheck=0;
01328 int ACDebug=0;
01329
01330 if (ACDebug) printf("genqAC 1\n");
01331
01332 if (accessControlPriv==LOCAL_PRIV_USER_AUTH) return(0);
01333
01334 if (ACDebug) printf("genqAC 2 accessControlControlFlag=%d\n",
01335 accessControlControlFlag);
01336
01337 if (accessControlControlFlag > 1) {
01338 doCheck=1;
01339 }
01340
01341 if (ACDebug) printf("genqAC 3\n");
01342
01343 if (doCheck==0) {
01344 if (strncmp(accessControlUserName,ANONYMOUS_USER, MAX_NAME_LEN)==0) {
01345 doCheck=1;
01346 }
01347 }
01348
01349 if (doCheck==0) return(0);
01350
01351 if (ACDebug) printf("genqAC 4\n");
01352
01353
01354
01355 if (strstr(selectSQL, "R_DATA_MAIN") != NULL) {
01356 if (strlen(whereSQL)>6) rstrcat(whereSQL, " AND ", MAX_SQL_SIZE_GQ);
01357 cllBindVars[cllBindVarCount++]=accessControlUserName;
01358 cllBindVars[cllBindVarCount++]=accessControlZone;
01359 rstrcat(whereSQL, "R_DATA_MAIN.data_id in (select object_id from R_OBJT_ACCESS OA, R_USER_GROUP UG, R_USER_MAIN UM, R_TOKN_MAIN TM where UM.user_name=? and UM.zone_name=? and UM.user_type_name!='rodsgroup' and UM.user_id = UG.user_id and UG.group_user_id = OA.user_id and OA.object_id = R_DATA_MAIN.data_id and OA.access_type_id >= TM.token_id and TM.token_namespace ='access_type' and TM.token_name = 'read object')", MAX_SQL_SIZE_GQ);
01360 }
01361
01362
01363
01364 if (strstr(selectSQL, "R_COLL_MAIN") != NULL) {
01365 if (strlen(whereSQL)>6) rstrcat(whereSQL, " AND ", MAX_SQL_SIZE_GQ);
01366 cllBindVars[cllBindVarCount++]=accessControlUserName;
01367 cllBindVars[cllBindVarCount++]=accessControlZone;
01368 rstrcat(whereSQL, "R_COLL_MAIN.coll_id in (select object_id from R_OBJT_ACCESS OA, R_USER_GROUP UG, R_USER_MAIN UM, R_TOKN_MAIN TM where UM.user_name=? and UM.zone_name=? and UM.user_type_name!='rodsgroup' and UM.user_id = UG.user_id and OA.object_id = R_COLL_MAIN.coll_id and UG.group_user_id = OA.user_id and OA.access_type_id >= TM.token_id and TM.token_namespace ='access_type' and TM.token_name = 'read object')", MAX_SQL_SIZE_GQ);
01369 }
01370 return(0);
01371 }
01372
01373
01374
01375
01376 int specialQueryIx(int ix) {
01377 if (ix==0) return(COL_QUOTA_USER_ID);
01378 if (ix==1) return(COL_R_RESC_NAME);
01379 if (ix==2) return(COL_QUOTA_LIMIT);
01380 if (ix==3) return(COL_QUOTA_OVER);
01381 if (ix==4) return(COL_QUOTA_RESC_ID);
01382 return(0);
01383 }
01384
01385
01386
01387
01388
01389
01390
01391
01392
01393
01394
01395
01396
01397
01398
01399 int
01400 generateSpecialQuery(genQueryInp_t genQueryInp, char *resultingSQL) {
01401 static char rescName[LONG_NAME_LEN];
01402 static char userName[NAME_LEN]="";
01403 static char userZone[NAME_LEN]="";
01404 char quotaQuery1[]="select distinct QM.user_id, RM.resc_name, QM.quota_limit, QM.quota_over, QM.resc_id from R_QUOTA_MAIN QM, R_USER_MAIN UM, R_RESC_MAIN RM, R_USER_GROUP UG, R_USER_MAIN UM2 where ( (QM.user_id = UM.user_id and UM.user_name = ? and UM.zone_name = ?) or (QM.user_id = UG.group_user_id and UM2.user_name = ? and UM2.zone_name = ? and UG.user_id = UM2.user_id) ) and ((QM.resc_id = RM.resc_id) or QM.resc_id = '0') order by quota_over desc";
01405
01406 char quotaQuery2[]="select distinct QM.user_id, RM.resc_name, QM.quota_limit, QM.quota_over, QM.resc_id from R_QUOTA_MAIN QM, R_USER_MAIN UM, R_RESC_MAIN RM, R_USER_GROUP UG, R_USER_MAIN UM2 where ( (QM.user_id = UM.user_id and UM.user_name = ? and UM.zone_name=?) or (QM.user_id = UG.group_user_id and UM2.user_name = ? and UM2.zone_name = ? and UG.user_id = UM2.user_id) ) and ((QM.resc_id = RM.resc_id) or QM.resc_id = '0') and RM.resc_name = ? order by quota_over desc";
01407 int i, valid=0;
01408
01409 for (i=0; i<genQueryInp.sqlCondInp.len;i++) {
01410 if (genQueryInp.sqlCondInp.inx[i]==COL_USER_NAME) {
01411 parseUserName(genQueryInp.sqlCondInp.value[i], userName,
01412 userZone);
01413 if (userZone[0]=='\0') {
01414 char *zoneName;
01415 zoneName = chlGetLocalZone();
01416 strncpy(userZone, zoneName, sizeof userZone);
01417 rodsLog(LOG_ERROR,"userZone1=:%s:\n",userZone);
01418 }
01419 rodsLog(LOG_ERROR,"userZone2=:%s:\n",userZone);
01420 rodsLog(LOG_ERROR,"userName=:%s:\n",userName);
01421 rodsLog(LOG_ERROR,"in=:%s:\n",genQueryInp.sqlCondInp.value[i]);
01422 cllBindVars[cllBindVarCount++]=userName;
01423 cllBindVars[cllBindVarCount++]=userZone;
01424 cllBindVars[cllBindVarCount++]=userName;
01425 cllBindVars[cllBindVarCount++]=userZone;
01426 strncpy(resultingSQL, quotaQuery1, MAX_SQL_SIZE_GQ);
01427 valid=1;
01428 }
01429 }
01430 if (valid==0) return(CAT_INVALID_ARGUMENT);
01431 for (i=0; i<genQueryInp.sqlCondInp.len;i++) {
01432 if (genQueryInp.sqlCondInp.inx[i]==COL_R_RESC_NAME) {
01433 strncpy(rescName, genQueryInp.sqlCondInp.value[i], sizeof rescName);
01434 cllBindVars[cllBindVarCount++]=rescName;
01435 strncpy(resultingSQL, quotaQuery2, MAX_SQL_SIZE_GQ);
01436 }
01437 }
01438 return (0);
01439 }
01440
01441
01442
01443
01444 int
01445 generateSQL(genQueryInp_t genQueryInp, char *resultingSQL,
01446 char *resultingCountSQL) {
01447 int i, table, startingTable=0;
01448 int keepVal;
01449 char *condition;
01450 int status;
01451 int useGroupBy;
01452 int N_col_meta_data_attr_name=0;
01453 int N_col_meta_coll_attr_name=0;
01454 int N_col_meta_user_attr_name=0;
01455 int N_col_meta_resc_attr_name=0;
01456 int N_col_meta_resc_group_attr_name=0;
01457
01458 char combinedSQL[MAX_SQL_SIZE_GQ];
01459 #if ORA_ICAT
01460 char countSQL[MAX_SQL_SIZE_GQ];
01461 #else
01462 static char offsetStr[20];
01463 #endif
01464
01465 if (firstCall) {
01466 icatGeneralQuerySetup();
01467 }
01468 firstCall=0;
01469
01470 nToFind = 0;
01471 for (i=0;i<nTables;i++) {
01472 Tables[i].flag=0;
01473 }
01474
01475 insertWhere("",1);
01476
01477 if (genQueryInp.options & NO_DISTINCT) {
01478 rstrcpy(selectSQL, "select ", MAX_SQL_SIZE_GQ);
01479 }
01480 else {
01481 rstrcpy(selectSQL, "select distinct ", MAX_SQL_SIZE_GQ);
01482 }
01483 selectSQLInitFlag=1;
01484 doUpperCase=0;
01485 if (genQueryInp.options & UPPER_CASE_WHERE) {
01486 doUpperCase=1;
01487 }
01488
01489 rstrcpy(fromSQL, "from ", MAX_SQL_SIZE_GQ);
01490 fromCount=0;
01491 rstrcpy(whereSQL, "where ", MAX_SQL_SIZE_GQ);
01492 rstrcpy(groupBySQL, "group by ", MAX_SQL_SIZE_GQ);
01493 mightNeedGroupBy=0;
01494
01495 tableAbbrevs='a';
01496
01497 for (i=0;i<genQueryInp.selectInp.len;i++) {
01498 table = setTable(genQueryInp.selectInp.inx[i], 1,
01499 genQueryInp.selectInp.value[i]&0xf, 0);
01500 if (table < 0) {
01501 rodsLog(LOG_ERROR,"Table for column %d not found\n",
01502 genQueryInp.selectInp.inx[i]);
01503 return(CAT_UNKNOWN_TABLE);
01504 }
01505 #ifdef LIMIT_AUDIT_ACCESS
01506 if (genQueryInp.selectInp.inx[i] >= COL_AUDIT_RANGE_START &&
01507 genQueryInp.selectInp.inx[i] <= COL_AUDIT_RANGE_END) {
01508 if (accessControlPriv != LOCAL_PRIV_USER_AUTH) {
01509 return(CAT_NO_ACCESS_PERMISSION);
01510 }
01511 }
01512 #endif
01513 if (Tables[table].cycler<1 || startingTable==0) {
01514 startingTable = table;
01515 }
01516 }
01517
01518 handleCompoundCondition("", -1);
01519 for (i=0;i<genQueryInp.sqlCondInp.len;i++) {
01520 int prevWhereLen;
01521 int castOption;
01522 char *cptr;
01523
01524 prevWhereLen = strlen(whereSQL);
01525 if (genQueryInp.sqlCondInp.inx[i]==COL_META_DATA_ATTR_NAME) {
01526 N_col_meta_data_attr_name++;
01527 }
01528 if (genQueryInp.sqlCondInp.inx[i]==COL_META_COLL_ATTR_NAME) {
01529 N_col_meta_coll_attr_name++;
01530 }
01531 if (genQueryInp.sqlCondInp.inx[i]==COL_META_USER_ATTR_NAME) {
01532 N_col_meta_user_attr_name++;
01533 }
01534 if (genQueryInp.sqlCondInp.inx[i]==COL_META_RESC_ATTR_NAME) {
01535 N_col_meta_resc_attr_name++;
01536 }
01537 if (genQueryInp.sqlCondInp.inx[i]==COL_META_RESC_GROUP_ATTR_NAME) {
01538 N_col_meta_resc_group_attr_name++;
01539 }
01540
01541
01542
01543
01544 castOption=0;
01545 cptr = genQueryInp.sqlCondInp.value[i];
01546 while (*cptr==' ') cptr++;
01547 if ( (*cptr=='n' && *(cptr+1)=='<') ||
01548 (*cptr=='n' && *(cptr+1)=='>') ||
01549 (*cptr=='n' && *(cptr+1)=='=') ) {
01550 castOption=1;
01551 *cptr=' ';
01552
01553 }
01554 table = setTable(genQueryInp.sqlCondInp.inx[i], 0, 0,
01555 castOption);
01556 if (table < 0) {
01557 rodsLog(LOG_ERROR,"Table for column %d not found\n",
01558 genQueryInp.sqlCondInp.inx[i]);
01559 return(CAT_UNKNOWN_TABLE);
01560 }
01561 if (Tables[table].cycler<1) {
01562 startingTable = table;
01563 }
01564 condition = genQueryInp.sqlCondInp.value[i];
01565 if (compoundConditionSpecified(condition)) {
01566 status = handleCompoundCondition(condition, prevWhereLen);
01567 if (status) return(status);
01568 }
01569 else {
01570 status = insertWhere(condition, 0);
01571 if (status) return(status);
01572 }
01573 #ifdef LIMIT_AUDIT_ACCESS
01574 if (genQueryInp.sqlCondInp.inx[i] >= COL_AUDIT_RANGE_START &&
01575 genQueryInp.sqlCondInp.inx[i] <= COL_AUDIT_RANGE_END) {
01576 if (accessControlPriv != LOCAL_PRIV_USER_AUTH) {
01577 return(CAT_NO_ACCESS_PERMISSION);
01578 }
01579 }
01580 #endif
01581 }
01582
01583 keepVal = tScan(startingTable, -1);
01584 if (keepVal!=1 || nToFind!=0) {
01585 rodsLog(LOG_ERROR,"error failed to link tables\n");
01586 return(CAT_FAILED_TO_LINK_TABLES);
01587 }
01588 else {
01589 if (debug>1) printf("SUCCESS linking tables\n");
01590 }
01591
01592 if (N_col_meta_data_attr_name > 1) {
01593
01594 handleMultiDataAVUConditions(N_col_meta_data_attr_name);
01595 }
01596
01597 if (N_col_meta_coll_attr_name > 1) {
01598
01599 handleMultiCollAVUConditions(N_col_meta_coll_attr_name);
01600 }
01601
01602 if (N_col_meta_user_attr_name > 1) {
01603
01604 return(CAT_INVALID_ARGUMENT);
01605 }
01606 if (N_col_meta_resc_attr_name > 1) {
01607
01608 return(CAT_INVALID_ARGUMENT);
01609 }
01610 if (N_col_meta_resc_group_attr_name > 1) {
01611
01612 return(CAT_INVALID_ARGUMENT);
01613 }
01614
01615 if (debug) printf("selectSQL: %s\n",selectSQL);
01616 if (debug) printf("fromSQL: %s\n",fromSQL);
01617 if (debug) printf("whereSQL: %s\n",whereSQL);
01618 useGroupBy=0;
01619 if (mightNeedGroupBy) {
01620 if (strlen(groupBySQL)>10) useGroupBy=1;
01621 }
01622 if (debug && useGroupBy) printf("groupBySQL: %s\n",groupBySQL);
01623
01624 combinedSQL[0]='\0';
01625 rstrcat(combinedSQL, selectSQL, MAX_SQL_SIZE_GQ);
01626 rstrcat(combinedSQL, " " , MAX_SQL_SIZE_GQ);
01627 rstrcat(combinedSQL, fromSQL, MAX_SQL_SIZE_GQ);
01628
01629 genqAppendAccessCheck();
01630
01631 if (strlen(whereSQL)>6) {
01632 rstrcat(combinedSQL, " " , MAX_SQL_SIZE_GQ);
01633 rstrcat(combinedSQL, whereSQL, MAX_SQL_SIZE_GQ);
01634 }
01635 if (useGroupBy) {
01636 rstrcat(combinedSQL, " " , MAX_SQL_SIZE_GQ);
01637 rstrcat(combinedSQL, groupBySQL, MAX_SQL_SIZE_GQ);
01638 }
01639 rstrcpy(orderBySQL, " order by ", MAX_SQL_SIZE_GQ);
01640 setOrderByUser(genQueryInp);
01641 setOrderBy(genQueryInp, COL_COLL_NAME);
01642 setOrderBy(genQueryInp, COL_DATA_NAME);
01643 setOrderBy(genQueryInp, COL_DATA_REPL_NUM);
01644 if (strlen(orderBySQL)>10) {
01645 rstrcat(combinedSQL, orderBySQL, MAX_SQL_SIZE_GQ);
01646 }
01647
01648 if (genQueryInp.rowOffset > 0) {
01649 #if ORA_ICAT
01650
01651
01652
01653
01654
01655 #elif MY_ICAT
01656
01657 snprintf (offsetStr, sizeof offsetStr, "%d", genQueryInp.rowOffset);
01658 rstrcat(combinedSQL, " limit ", MAX_SQL_SIZE_GQ);
01659 rstrcat(combinedSQL, offsetStr, MAX_SQL_SIZE_GQ);
01660 rstrcat(combinedSQL, ",18446744073709551615", MAX_SQL_SIZE_GQ);
01661 #else
01662
01663 snprintf (offsetStr, sizeof offsetStr, "%d", genQueryInp.rowOffset);
01664 cllBindVars[cllBindVarCount++]=offsetStr;
01665 rstrcat(combinedSQL, " offset ?", MAX_SQL_SIZE_GQ);
01666 #endif
01667 }
01668
01669 if (debug) printf("combinedSQL=:%s:\n",combinedSQL);
01670 strncpy(resultingSQL, combinedSQL, MAX_SQL_SIZE_GQ);
01671
01672 #if ORA_ICAT
01673 countSQL[0]='\0';
01674 rstrcat(countSQL, "select distinct count(*) ", MAX_SQL_SIZE_GQ);
01675 rstrcat(countSQL, fromSQL, MAX_SQL_SIZE_GQ);
01676
01677 if (strlen(whereSQL)>6) {
01678 rstrcat(countSQL, " " , MAX_SQL_SIZE_GQ);
01679 rstrcat(countSQL, whereSQL, MAX_SQL_SIZE_GQ);
01680 }
01681
01682 if (debug) printf("countSQL=:%s:\n",countSQL);
01683 strncpy(resultingCountSQL, countSQL, MAX_SQL_SIZE_GQ);
01684 #endif
01685 return(0);
01686 }
01687
01688
01689
01690
01691
01692
01693
01694
01695 int
01696 checkCondInputAccess(genQueryInp_t genQueryInp, int statementNum,
01697 icatSessionStruct *icss, int continueFlag) {
01698 int i, nCols;
01699 int userIx=-1, zoneIx=-1, accessIx=-1, dataIx=-1, collIx=-1;
01700 int status;
01701 char *zoneName;
01702 static char prevDataId[LONG_NAME_LEN];
01703 static char prevUser[LONG_NAME_LEN];
01704 static char prevAccess[LONG_NAME_LEN];
01705 static int prevStatus;
01706
01707 for (i=0;i<genQueryInp.condInput.len;i++) {
01708 if (strcmp(genQueryInp.condInput.keyWord[i],
01709 USER_NAME_CLIENT_KW)==0) userIx=i;
01710 if (strcmp(genQueryInp.condInput.keyWord[i],
01711 RODS_ZONE_CLIENT_KW)==0) zoneIx=i;
01712 if (strcmp(genQueryInp.condInput.keyWord[i],
01713 ACCESS_PERMISSION_KW)==0) accessIx=i;
01714
01715 }
01716 if (genQueryInp.condInput.len==1 &&
01717 strcmp(genQueryInp.condInput.keyWord[0], ZONE_KW)==0) {
01718 return(0);
01719 }
01720
01721 if (userIx<0 || zoneIx<0 || accessIx<0) return(CAT_INVALID_ARGUMENT);
01722
01723
01724 nCols = icss->stmtPtr[statementNum]->numOfCols;
01725 for (i=0;i<nCols;i++) {
01726 if (strcmp(icss->stmtPtr[statementNum]->resultColName[i], "data_id")==0)
01727 dataIx = i;
01728
01729 if (strcmp(icss->stmtPtr[statementNum]->resultColName[i], "DATA_ID")==0)
01730 dataIx = i;
01731 if (strcmp(icss->stmtPtr[statementNum]->resultColName[i], "coll_id")==0)
01732 collIx = i;
01733
01734 if (strcmp(icss->stmtPtr[statementNum]->resultColName[i], "COLL_ID")==0)
01735 collIx = i;
01736 }
01737 if (dataIx<0 && collIx<0) return(CAT_INVALID_ARGUMENT);
01738
01739 if (dataIx>=0) {
01740 if (continueFlag==1) {
01741 if (strcmp(prevDataId,
01742 icss->stmtPtr[statementNum]->resultValue[dataIx])==0) {
01743 if (strcmp(prevUser, genQueryInp.condInput.value[userIx])==0) {
01744 if (strcmp(prevAccess,
01745 genQueryInp.condInput.value[accessIx])==0) {
01746 return(prevStatus);
01747 }
01748 }
01749 }
01750 }
01751
01752 strncpy(prevDataId, icss->stmtPtr[statementNum]->resultValue[dataIx],
01753 LONG_NAME_LEN);
01754 strncpy(prevUser, genQueryInp.condInput.value[userIx],
01755 LONG_NAME_LEN);
01756 strncpy(prevAccess, genQueryInp.condInput.value[accessIx],
01757 LONG_NAME_LEN);
01758 prevStatus=0;
01759
01760 if (strlen(genQueryInp.condInput.value[zoneIx])==0) {
01761 zoneName = chlGetLocalZone();
01762 }
01763 else {
01764 zoneName = genQueryInp.condInput.value[zoneIx];
01765 }
01766 status = cmlCheckDataObjId(
01767 icss->stmtPtr[statementNum]->resultValue[dataIx],
01768 genQueryInp.condInput.value[userIx],
01769 zoneName,
01770 genQueryInp.condInput.value[accessIx], icss);
01771 prevStatus=status;
01772 return(status);
01773 }
01774
01775 if (collIx>=0) {
01776 if (strlen(genQueryInp.condInput.value[zoneIx])==0) {
01777 zoneName = chlGetLocalZone();
01778 }
01779 else {
01780 zoneName = genQueryInp.condInput.value[zoneIx];
01781 }
01782 status = cmlCheckDirId(
01783 icss->stmtPtr[statementNum]->resultValue[collIx],
01784 genQueryInp.condInput.value[userIx],
01785 zoneName,
01786 genQueryInp.condInput.value[accessIx], icss);
01787 }
01788 return(status);
01789 }
01790
01791
01792
01793
01794
01795 int
01796 chlGenQueryAccessControlSetup(char *user, char *zone, int priv,
01797 int controlFlag) {
01798 if (user != NULL ) {
01799 rstrcpy(accessControlUserName, user, MAX_NAME_LEN);
01800 rstrcpy(accessControlZone, zone, MAX_NAME_LEN);
01801 accessControlPriv=priv;
01802 }
01803 if (controlFlag > 0 ) {
01804
01805
01806
01807
01808
01809
01810
01811 accessControlControlFlag=controlFlag;
01812 }
01813 return(0);
01814 }
01815
01816
01817 int
01818 chlGenQuery(genQueryInp_t genQueryInp, genQueryOut_t *result) {
01819 int i, j, k;
01820 int needToGetNextRow;
01821
01822 char combinedSQL[MAX_SQL_SIZE_GQ];
01823 char countSQL[MAX_SQL_SIZE_GQ];
01824
01825 int status, statementNum;
01826 int numOfCols;
01827 int attriTextLen;
01828 int totalLen;
01829 int maxColSize;
01830 int currentMaxColSize;
01831 char *tResult, *tResult2;
01832 static int recursiveCall=0;
01833
01834 if (logSQLGenQuery) rodsLog(LOG_SQL, "chlGenQuery");
01835
01836 icatSessionStruct *icss;
01837
01838 result->attriCnt=0;
01839 result->rowCnt=0;
01840 result->totalRowCount = 0;
01841
01842 currentMaxColSize=0;
01843
01844 icss = chlGetRcs();
01845 if (icss==NULL) return(CAT_NOT_OPEN);
01846 #ifdef ADDR_64BITS
01847 if (debug) printf("icss=%ld\n",(long int)icss);
01848 #else
01849 if (debug) printf("icss=%d\n",(int)icss);
01850 #endif
01851
01852 if (genQueryInp.continueInx == 0) {
01853 if (genQueryInp.options & QUOTA_QUERY) {
01854 countSQL[0]='\0';
01855 status = generateSpecialQuery(genQueryInp, combinedSQL);
01856 }
01857 else {
01858 status = generateSQL(genQueryInp, combinedSQL, countSQL);
01859 }
01860 if (status != 0) return(status);
01861 if (logSQLGenQuery) {
01862 if (genQueryInp.rowOffset==0) {
01863 rodsLog(LOG_SQL, "chlGenQuery SQL 1");
01864 }
01865 else {
01866 rodsLog(LOG_SQL, "chlGenQuery SQL 2");
01867 }
01868 #if 0
01869 rodsLog(LOG_NOTICE, "combinedSQL: %s", combinedSQL);
01870 #endif
01871 }
01872
01873 if (genQueryInp.options & RETURN_TOTAL_ROW_COUNT) {
01874
01875 if (logSQLGenQuery) rodsLog(LOG_SQL, "chlGenQuery SQL 3");
01876 }
01877
01878 #if ORA_ICAT
01879 if (genQueryInp.options & RETURN_TOTAL_ROW_COUNT) {
01880 int cllBindVarCountSave;
01881 rodsLong_t iVal;
01882 cllBindVarCountSave = cllBindVarCount;
01883 status = cmlGetIntegerValueFromSqlV3(countSQL, &iVal,
01884 icss);
01885 if (status < 0) {
01886 if (status != CAT_NO_ROWS_FOUND) {
01887 rodsLog(LOG_NOTICE,
01888 "chlGenQuery cmlGetIntegerValueFromSqlV3 failure %d",
01889 status);
01890 }
01891 return(status);
01892 }
01893 if (iVal >= 0) result->totalRowCount = iVal;
01894 cllBindVarCount = cllBindVarCountSave;
01895 }
01896 #endif
01897
01898 status = cmlGetFirstRowFromSql(combinedSQL, &statementNum,
01899 genQueryInp.rowOffset, icss);
01900 if (status < 0) {
01901 if (status != CAT_NO_ROWS_FOUND) {
01902 rodsLog(LOG_NOTICE,
01903 "chlGenQuery cmlGetFirstRowFromSql failure %d",
01904 status);
01905 }
01906 #if ORA_ICAT
01907 #else
01908 else {
01909 int saveStatus;
01910 if (genQueryInp.options & RETURN_TOTAL_ROW_COUNT &&
01911 genQueryInp.rowOffset > 0 ) {
01912
01913 saveStatus = status;
01914 recursiveCall=1;
01915 genQueryInp.rowOffset = 0;
01916 chlGenQuery(genQueryInp, result);
01917 return(saveStatus);
01918 }
01919 }
01920 #endif
01921 return(status);
01922 }
01923
01924 #if ORA_ICAT
01925 recursiveCall=0;
01926 #else
01927 if (genQueryInp.options & RETURN_TOTAL_ROW_COUNT) {
01928 i = cllGetRowCount(icss, statementNum);
01929 if (i >= 0) result->totalRowCount = i + genQueryInp.rowOffset;
01930 if (recursiveCall==1) {
01931 recursiveCall=0;
01932 return(status);
01933 }
01934 }
01935 #endif
01936
01937 if (genQueryInp.condInput.len > 0) {
01938 status = checkCondInputAccess(genQueryInp, statementNum, icss, 0);
01939 if (status != 0) return(status);
01940 }
01941 result->continueInx = statementNum+1;
01942 if (debug) printf("statement number =%d\n", statementNum);
01943 needToGetNextRow = 0;
01944 }
01945 else {
01946 statementNum = genQueryInp.continueInx-1;
01947 needToGetNextRow = 1;
01948 if (genQueryInp.maxRows<=0) {
01949 status = cmlFreeStatement(statementNum, icss);
01950 return(status);
01951 }
01952 }
01953 for (i=0;i<genQueryInp.maxRows;i++) {
01954 if (needToGetNextRow) {
01955 status = cmlGetNextRowFromStatement(statementNum, icss);
01956 if (status == CAT_NO_ROWS_FOUND) {
01957 cmlFreeStatement(statementNum, icss);
01958 result->continueInx=0;
01959 if (result->rowCnt==0) return(status);
01960
01961 return(0);
01962 }
01963 if (status < 0) return(status);
01964 if (genQueryInp.condInput.len > 0) {
01965 status = checkCondInputAccess(genQueryInp, statementNum, icss, 1);
01966 if (status != 0) return(status);
01967 }
01968 }
01969 needToGetNextRow = 1;
01970
01971 result->rowCnt++;
01972 if (debug) printf("result->rowCnt=%d\n", result->rowCnt);
01973 numOfCols = icss->stmtPtr[statementNum]->numOfCols;
01974 if (debug) printf("numOfCols=%d\n",numOfCols);
01975 result->attriCnt=numOfCols;
01976 result->continueInx = statementNum+1;
01977
01978 maxColSize=0;
01979
01980 for (k=0;k<numOfCols;k++) {
01981 j = strlen(icss->stmtPtr[statementNum]->resultValue[k]);
01982 if (maxColSize <= j) maxColSize=j;
01983 }
01984 maxColSize++;
01985 if (maxColSize < MINIMUM_COL_SIZE) {
01986 maxColSize=MINIMUM_COL_SIZE;
01987 }
01988 if (debug) printf("maxColSize=%d\n",maxColSize);
01989
01990 if (i==0) {
01991 attriTextLen= numOfCols * maxColSize;
01992 if (debug) printf("attriTextLen=%d\n",attriTextLen);
01993 totalLen = attriTextLen * genQueryInp.maxRows;
01994 for (j=0;j<numOfCols;j++) {
01995 tResult = (char*)malloc(totalLen);
01996 if (tResult==NULL) return(SYS_MALLOC_ERR);
01997 memset(tResult, 0, totalLen);
01998 if (genQueryInp.options & QUOTA_QUERY) {
01999 result->sqlResult[j].attriInx = specialQueryIx(j);
02000 }
02001 else {
02002 result->sqlResult[j].attriInx = genQueryInp.selectInp.inx[j];
02003 }
02004 result->sqlResult[j].len = maxColSize;
02005 result->sqlResult[j].value = tResult;
02006 }
02007 currentMaxColSize = maxColSize;
02008 }
02009
02010
02011
02012
02013
02014
02015 if (maxColSize > currentMaxColSize) {
02016 maxColSize += MINIMUM_COL_SIZE;
02017
02018 if (debug) printf("Bumping %d to %d\n",
02019 currentMaxColSize, maxColSize);
02020 attriTextLen= numOfCols * maxColSize;
02021 if (debug) printf("attriTextLen=%d\n",attriTextLen);
02022 totalLen = attriTextLen * genQueryInp.maxRows;
02023 for (j=0;j<numOfCols;j++) {
02024 char *cp1, *cp2;
02025 int k;
02026 tResult = (char*)malloc(totalLen);
02027 if (tResult==NULL) return(SYS_MALLOC_ERR);
02028 memset(tResult, 0, totalLen);
02029 cp1 = result->sqlResult[j].value;
02030 cp2 = tResult;
02031 for (k=0;k<result->rowCnt;k++) {
02032 strncpy(cp2, cp1, result->sqlResult[j].len);
02033 cp1 += result->sqlResult[j].len;
02034 cp2 += maxColSize;
02035 }
02036 free(result->sqlResult[j].value);
02037 result->sqlResult[j].len = maxColSize;
02038 result->sqlResult[j].value = tResult;
02039 }
02040 currentMaxColSize = maxColSize;
02041 }
02042
02043
02044
02045 for (j=0;j<numOfCols;j++) {
02046 tResult2 = result->sqlResult[j].value;
02047 tResult2 += currentMaxColSize*(result->rowCnt-1);
02048
02049 strncpy(tResult2, icss->stmtPtr[statementNum]->resultValue[j],
02050 currentMaxColSize);
02051 }
02052
02053 }
02054
02055 result->continueInx=statementNum+1;
02056
02057 if (genQueryInp.options & AUTO_CLOSE) {
02058 int status2;
02059 result->continueInx=-1;
02060 status2 = cmlFreeStatement(statementNum, icss);
02061 return(status2);
02062 }
02063 return(0);
02064
02065 }
02066
02067 int
02068 chlDebugGenQuery(int mode) {
02069 logSQLGenQuery = mode;
02070 return(0);
02071 }