📄 oradb.c
字号:
/*/* Fetch a ROW from the CURSOR and install each SELECT ITEM into/* The SYMBOL TABLE./* bClose Flag: bClose=eTrue means CLOSE CURSOR after the FETCH/* bClose=eFalse means CLOSE CURSOR when exhausted/*/* ******* SPECIAL NOTE ABOUT RETURN CODE: *******/* ONLY return(eFalse) on DATABASE ERROR/* USE pCursor->bFound=eFalse; to indicate NOT FOUND/* ******* SPECIAL NOTE ABOUT RETURN CODE: *******/* usage:/* SQLWEB_CURSOR cCur;/* ... connect .../* ... open .../* for(;;) {/* RETeFalse( DbFetchCursor(&cCur,eFalse), "Close Failed");/* if(cCur.bFound) break;/* } */eBoolean_tDbFetchCursor(SQLWEB_CURSOR *pCursor /* the CURSOR (from Open) */ ,eBoolean_t bClose /* Force Close after Fetch */ ){ eBoolean_t bStatus; if(ISeFalse(pCursor->bOpen)) { DebugHTML(__FILE__,__LINE__,1,"DbFetchCursor:Not Open"); pCursor->bFound=eFalse; return(eTrue); } if(ofetch(&gaCDA[pCursor->iCursorIndex]) /* && gaCDA[pCursor->iCursorIndex].rc != NULL_VALUE_RETURNED */) { pCursor->bFound = eFalse; if (gaCDA[pCursor->iCursorIndex].rc == NO_DATA_FOUND) { bStatus = eTrue; } else { PushDBErr(&gaLDA[pCursor->pLDA->iLdaIndex] ,&gaCDA[pCursor->iCursorIndex] ,"ofetch failed." ); bStatus = eFalse; } /* /* Close the Cursor */ if(oclose(&gaCDA[pCursor->iCursorIndex])) { PushDBErr(&gaLDA[pCursor->pLDA->iLdaIndex] ,&gaCDA[pCursor->iCursorIndex] ,"oclose failed." ); bStatus = eFalse; } else { pCursor->bOpen = eFalse; } /* /* Free the DbInfo_t's Created for this Cursor */ l_scan(pCursor->lBind,FreeDbInfo); l_scan(pCursor->lSelect,FreeDbInfo); return(bStatus); } /* /* Found a ROW */ l_scan(pCursor->lSelect,SetDbNulls); pCursor->bFound=eTrue; bStatus = eTrue; /* /* Force a Close, good for single row queries */ if(bClose) { /* Only required if the CURSOR will be ReUsed /* if(ocan(&gaCDA[pCursor->iCursorIndex])) { /* PushDBErr(&gaLDA[pCursor->pLDA->iLdaIndex] /* ,&gaCDA[pCursor->iCursorIndex] /* ,"ocan failed." /* ); /* bStatus = eFalse; /*} */ if(oclose(&gaCDA[pCursor->iCursorIndex])) { PushDBErr(&gaLDA[pCursor->pLDA->iLdaIndex] ,&gaCDA[pCursor->iCursorIndex] ,"oclose failed." ); bStatus = eFalse; } } return(bStatus);}/*************************************************************/* /* Supporting Routines, not part of the API, all additional/* functions should be declared static! *//*/* Set NULLs based on the INDICATOR Variable/* This is called from the DbFetchCursor() routine */static eBoolean_tSetDbNulls(DbInfo_t *pDbInfo){ if(pDbInfo->ind<0 && pDbInfo->pValue) { memset(pDbInfo->pValue,0,pDbInfo->iLen); } return(eTrue);}/*/* Get the Next Available CursorIndex from/* the gaCDA array. */static eBoolean_tGetCursorIndex(int *piCursorIndex){ static int siCursorIndex; int iStart, i; for(i=0, iStart=siCursorIndex ;i==0 || (iStart != siCursorIndex && i>0) ;i++, siCursorIndex = (siCursorIndex+1)%MAX_CURSORS) { if( gaCDA[siCursorIndex].fc == 0 /* Globals init'd to Zero */ || gaCDA[siCursorIndex].fc == FC_OCLOSE) { (*piCursorIndex) = siCursorIndex; return(eTrue); } } MsgPush("Exceeded Maximum Cursors"); return(eFalse);}/*/* Describe the BIND Variables... */static eBoolean_tDescribeBindVars(SQLWEB_CURSOR *pCursor){ sword i, n; eBoolean_t bLiteral; text *cp; /* Find and bind input variables for placeholders. */ for (i=0, bLiteral=eFalse, cp=pCursor->pStmt; *cp; cp++) { DbInfo_t *pDbInfo; /* Single Quoted Strings are SQL literals */ if (*cp == '\'') { bLiteral = ISeTrue(bLiteral)?eFalse:eTrue; /* Toggle */ } if(*cp == ':' && ISeFalse(bLiteral) && *(cp+1) != '=') { sb1 buf[MAX_SQL_IDENTIFIER]; for ( ++cp, n=0; *cp && (isalnum(*cp) || *cp == '_') && n < MAX_SQL_IDENTIFIER; cp++, n++ ) { buf[n]=*cp; } buf[n]=0; /* NULL the 'buf' Buffer */ if(! *cp) --cp; /* If this the end of the buffer, there /* is a problem because the outer loop /* increments the cp pointer again, /* BEYOND THE END OF THIS STRING!!! */ /* /* buf now points to the SYMBOL NAME /* so... go-n-get it! */ /* pDbInfo = (DbInfo_t*)malloc(sizeof(DbInfo_t)); */ pDbInfo = NewDbInfo(); if(!pDbInfo){ MsgPush("malloc failed"); return(eFalse); } ENQ(pCursor->lBind,pDbInfo); if(ISeFalse(GetSymbolValueREF(buf,&pDbInfo->pValue))){ /* There is no symbol by that name, stick in NULL /* WARNING? */ pDbInfo->pValue = 0; /* should have been done by Get.. */ } pDbInfo->iLen = pDbInfo->pValue ? strlen(pDbInfo->pValue) : 0; pDbInfo->ind = pDbInfo->iLen>0 ? (sb2)0 : (sb2)-1; DebugHTML(__FILE__,__LINE__,2 ,"DbOpen:GetS(%s):L=%d, I=%d" ,buf ,pDbInfo->iLen ,pDbInfo->ind ); if(obndrv(&gaCDA[pCursor->iCursorIndex] ,buf ,-1 ,&pDbInfo->pValue[0] ,-1 /* pDbInfo->iLen>2000?pDbInfo->iLen+1:-1 */ ,VARCHAR2_TYPE /*pDbInfo->iLen>2000?LONG_TYPE:VARCHAR2_TYPE*/ ,-1 ,&pDbInfo->ind ,(text *) 0 ,-1 ,-1 )) { PushDBErr(&gaLDA[pCursor->pLDA->iLdaIndex] ,&gaCDA[pCursor->iCursorIndex] ,"obndrv failed." ); MsgPush("s=%s,b=%x,rc=%d" ,buf ,pDbInfo->pValue ,gaCDA[pCursor->iCursorIndex].rc ); return(eFalse); } i++; } /* end if (*cp == ...) */ } /* end for () */ return(eTrue);}/*/* Describe the SELECT Variables... */static eBoolean_tDescribeSelectVars(SQLWEB_CURSOR *pCursor){ sword col; char *pBuf; sb4 dbsize; sb2 dbtype; sb1 buf[MAX_ITEM_BUFFER_SIZE]; sb4 buflen = MAX_ITEM_BUFFER_SIZE; sb4 dsize; sb2 precision; sb2 scale; sb2 nullok; DbInfo_t *pDbInfo; /* Describe the select-list items. */ for (col = 0; ; col++) { buflen = MAX_ITEM_BUFFER_SIZE; if(odescr(&gaCDA[pCursor->iCursorIndex] ,col+1 ,&dbsize ,&dbtype ,&buf[0] ,&buflen ,&dsize ,&precision ,&scale ,&nullok )) { /* Break on end of select list. */ if ((&gaCDA[pCursor->iCursorIndex])->rc == VAR_NOT_IN_LIST) break; else { PushDBErr(&gaLDA[pCursor->pLDA->iLdaIndex] ,&gaCDA[pCursor->iCursorIndex] ,"odescr failed." ); return(eFalse); } } /* adjust sizes */ buf[buflen]=0; switch (dbtype) { case NUMBER_TYPE: case INT_TYPE: case FLOAT_TYPE: dbsize = 44; break; case DATE_TYPE: dbsize = 44; break; case ROWID_TYPE: dbsize = 18; break; case LONG_TYPE: dbsize = 65533; break; } /* /* Build a "DB" Symbol */ buf[buflen]=0; /* /* Got a "Select-Item" in buf setup a SYMBOL for it. */ /* pDbInfo = (DbInfo_t*)malloc(sizeof(DbInfo_t)); */ pDbInfo = NewDbInfo(); if(!pDbInfo){ MsgPush("malloc failed"); return(eFalse); } memset(pDbInfo,0,sizeof(DbInfo_t)); pDbInfo->pValue = (char*)malloc(dbsize+1); pDbInfo->iLen = dbsize+1; if(!pDbInfo->pValue) { MsgPush("malloc failed"); return(eFalse); } memset(pDbInfo->pValue,0,dbsize+1); ENQ(pCursor->lSelect,pDbInfo); /* For NULL Processing */ if(ISeTrue(GetSymbolValueREF(buf,&pBuf)) && strlen(pBuf)<=dbsize) { strcpy(pDbInfo->pValue,pBuf); /* Don't assume a symbol was malloc'd, (you hosehead) /* Add Symbol will take care of memory leakage... /* FreeBuf(pBuf); /* Free previous Symbol Value */ }/*/* SELSym((char*)strdup(buf),pDbInfo->pValue); */ RETeFalse2(AddSymbol(SEL_SYMBOL ,(char*)strdup(buf) ,pDbInfo->pValue ,eTrue ,dbsize+1 ) ,"AddSymbol(SEL,%s) Failed." \ ,buf ); DebugHTML(__FILE__,__LINE__,2,"DbOpen:AddS(%s)",buf); /* /* Let Oracle Convert everything to STRING */ dbtype = STRING_TYPE; if (odefin(&gaCDA[pCursor->iCursorIndex] ,col+1 ,&pDbInfo->pValue[0] ,dbsize+1 ,dbtype ,-1 ,&pDbInfo->ind ,(text *) 0 ,-1 ,-1 ,&pDbInfo->rlen ,&pDbInfo->rcode )) { PushDBErr(&gaLDA[pCursor->pLDA->iLdaIndex] ,&gaCDA[pCursor->iCursorIndex] ,"odefin failed." ); return(eFalse); } } return(eTrue);}static voidPushDBErr(Lda_Def *pLDA ,Cda_Def *pCDA ,char *pErrMsg ){ sword n; text tBuf[512]; /* if(pLDA==(Lda_Def*)0) return; */ if(pCDA==(Cda_Def*)0) return; n = oerhms(pLDA,pCDA->rc,tBuf,(sword)sizeof(tBuf)); tBuf[n]=0; MsgPush("DBERR:Oracle(%d):%s",pCDA->rc,tBuf); if(pErrMsg) MsgPush("%s",pErrMsg); return;}static DbInfo_t *NewDbInfo(){ DbInfo_t *pDbInfo; if(!glDbInfoList){ glDbInfoList=l_create("STACK"); if(!glDbInfoList) { MsgPush("NewDbInfo:l_create failed"); return(0); } } if(l_size(glDbInfoList)>0) { DebugHTML(__FILE__,__LINE__,3,"NewDbInfo:POP(%d)",l_size(glDbInfoList)); pDbInfo=(DbInfo_t*)POP(glDbInfoList); } else { pDbInfo=(DbInfo_t*)malloc(sizeof(DbInfo_t)); } if(!pDbInfo) { MsgPush("NewDbInfo:malloc or POP failed"); return(0); } (void)memset(pDbInfo,0,sizeof(DbInfo_t)); return(pDbInfo);}static eBoolean_tFreeDbInfo(DbInfo_t *pDbInfo){ DebugHTML(__FILE__,__LINE__,3,"FreeDbInfo[%x]",pDbInfo); if(!glDbInfoList) { glDbInfoList=l_create("STACK"); if(!glDbInfoList) { MsgPush("FreeDbInfo:l_create failed"); return(eFalse); } } /* /* Free pValue.... Free(pDbInfo->pValue); */ /* /* Store This PI on the FreeLIst */ DebugHTML(__FILE__,__LINE__,3,"FreeDbInfo:PUSH(%d)",l_size(glDbInfoList)); PUSH(glDbInfoList,pDbInfo); return(eTrue);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -