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