SQLPROG.H
上传用户:zdlsnail
上传日期:2007-01-06
资源大小:16k
文件大小:16k
- UCHAR rootDir[]="C:\DBSERVER\";
- UCHAR HTMLHead[]="00000000";
- HENV henv;
- typedef struct
- {
- UDWORD pcbColDef;
- SWORD pfSqlType;
- SWORD SqlType;
- SWORD pibScale;
- UDWORD len;}ColumnInfo;
- ColumnInfo ColInfo[256];
- typedef struct
- {
- short ID;
- UCHAR connString[200];}connIDInfo;
- connIDInfo ConIDStru[MAXCONNECTID];
- typedef struct
- {
- UCHAR userName[20];
- UCHAR passWord[20];
- UCHAR winName[20];
- UCHAR pathName[80];}userstru;
- userstru userInfoStru[MAXUSER];
- long userCount=0;
- char far author[]="\AUTHOR.SAV";
- short connectCount=0;
- typedef struct
- {
- //UCHAR dataDir[50];
- UCHAR sqlselectstring[300];
- short usermark;
- short ID;
- UCHAR connstring[200];
- HDBC hdbc;
- HSTMT phstmt;
- //UDWORD ulpos;
- UWORD iCol;
- UWORD i;
- UCHAR FAR * descptr;
- UCHAR FAR * sourceptr;
- unsigned char *returnstring;
- unsigned char *retstr;
- RETCODE retcode;
- ColumnInfo ColInfo[256];
- UCHAR DSN[80];
- UCHAR UID[30];
- UCHAR PASSWORD[30];
- unsigned long ii;
- unsigned long jj;
- unsigned char dwLib[30];
- unsigned char dwName[30];
- EXTENSION_CONTROL_BLOCK *pECB;
- BOOL errorUpdate;
- } ConnStruDef;
- ConnStruDef connectStru[MAXCONNECT];
- //char far author[]="\AUTHOR.SAV";
- //char far id_section[]="@_ID_section";
- //short connectCount=0;
- void WriteHtml(EXTENSION_CONTROL_BLOCK *pECB,LPSTR lpsz);
- void makeConnectString(UCHAR FAR *,short ID);
- void sendsqlresult(int connectID);
- UCHAR FAR * changerequest(UCHAR FAR * requeststring);
- UCHAR sqlerror[300],stateerror[300];
- SDWORD dwerror;
- SWORD dwerror1;
- BOOL oneUpdate(int ConnctID,UCHAR FAR * requeststring);
- //--------------------------------------------
- void makeDisconnect(connectID)
- int connectID;
- {
- if(connectStru[connectID].phstmt==SQL_NULL_HSTMT)
- SQLFreeStmt(connectStru[connectID].phstmt,SQL_DROP);
- if(connectStru[connectID].hdbc==SQL_NULL_HDBC)
- SQLDisconnect(connectStru[connectID].hdbc);
- //if(henv==SQL_NULL_HENV)
- // SQLFreeEnv(henv);
- connectStru[connectID].ID=0;}
- //----------------------------------------------------------
- BOOL makeConnect(connectID,ID)
- int connectID;
- short ID;
- {
- UCHAR FAR tmp[200];
- SWORD MAXStringLen;
- // UCHAR szSqlState[6],szErrorMsg[300];
- // SDWORD dwNativeError;
- // SWORD wErrorMsg;
- RETCODE retcode;
- makeConnectString(connectStru[connectID].connstring,ID);
- if(connectStru[connectID].connstring==NULL) return(FALSE);
- connectStru[connectID].ID=ID;
- retcode=SQLAllocConnect(henv,&(connectStru[connectID].hdbc));
- if(retcode!=SQL_SUCCESS) {
- makeDisconnect(connectID);return(FALSE);}
- // SQLSetConnectOption(connectStru[connectID].hdbc,SQL_LOGIN_TIMEOUT,10);
- SQLSetConnectOption(connectStru[connectID].hdbc,SQL_AUTOCOMMIT,SQL_AUTOCOMMIT_OFF);
- //SQLSetConnectOption(connectStru[connectID].hdbc,SQL_ACCESS_MODE,SQL_MODE_READ_WRITE);
- retcode=SQLDriverConnect(connectStru[connectID].hdbc,
- NULL,
- connectStru[connectID].connstring,SQL_NTS,
- tmp,200,&MAXStringLen,SQL_DRIVER_NOPROMPT);
- //retcode=SQLConnect(connectStru[connectID].hdbc,
- // connectStru[connectID].DSN,SQL_NTS,
- // connectStru[connectID].UID,SQL_NTS,
- // connectStru[connectID].PASSWORD,SQL_NTS);
- //itoa(retcode,tmp,10);
- //strcat(tmp,connectStru[connectID].DSN);
- //strcat(tmp,connectStru[connectID].UID);
- //strcat(tmp,connectStru[connectID].PASSWORD);
- //WriteHtml(connectStru[connectID].pECB,tmp);
- //WriteHtml(connectStru[connectID].pECB,connectStru[connectID].connstring);
- // SQLError(henv,
- // connectStru[connectID].hdbc,SQL_NULL_HSTMT,
- // szSqlState,&dwNativeError,szErrorMsg,300,&wErrorMsg);
- // WriteHtml(connectStru[connectID].pECB,szErrorMsg);
- if((retcode==SQL_SUCCESS) || (retcode==SQL_SUCCESS_WITH_INFO)) {
- retcode=SQLAllocStmt(connectStru[connectID].hdbc,
- &(connectStru[connectID].phstmt));
- if(retcode!=SQL_SUCCESS) {makeDisconnect(connectID);return(FALSE);}
- return(TRUE);}
- else {
- makeDisconnect(connectID);return(FALSE);}}
- //----------------------------------------------------------
- UCHAR szcolName[80];
- SWORD cbColNameMax=80,pcbColName;
- SWORD pibScale,pfNullable;
- BOOL sqlSelectFun(connectID,sqlselectstring)
- int connectID;
- UCHAR FAR * sqlselectstring;
- {
- RETCODE retcode;
- sqlselectstring=changerequest(sqlselectstring);
- //connectStru[connectID].ulpos=0;
- retcode=SQLExecDirect(connectStru[connectID].phstmt,
- sqlselectstring,SQL_NTS);
- if(retcode!=SQL_SUCCESS) {
- WriteHtml(connectStru[connectID].pECB,"ERROR004");
- return(FALSE);}
- connectStru[connectID].iCol=1;
- while(TRUE) {
- retcode=SQLDescribeCol(
- connectStru[connectID].phstmt,
- connectStru[connectID].iCol,
- szcolName,
- cbColNameMax,
- &pcbColName,
- &(connectStru[connectID].ColInfo[connectStru[connectID].iCol].pfSqlType),
- &(connectStru[connectID].ColInfo[connectStru[connectID].iCol].pcbColDef),
- &(connectStru[connectID].ColInfo[connectStru[connectID].iCol].pibScale),
- &pfNullable);
- connectStru[connectID].ColInfo[connectStru
- [connectID].iCol].SqlType=SQL_C_CHAR;
- switch(connectStru[connectID].ColInfo[connectStru[connectID].iCol].pfSqlType) {
- case SQL_BIGINT:
- connectStru[connectID].ColInfo[connectStru
- [connectID].iCol].pcbColDef=80;
- connectStru[connectID].ColInfo[connectStru
- [connectID].iCol].pfSqlType=0;break;
- case SQL_BINARY:
- connectStru[connectID].ColInfo[connectStru
- [connectID].iCol].pfSqlType=0;break;
- case SQL_BIT:
- connectStru[connectID].ColInfo[connectStru
- [connectID].iCol].pcbColDef=80;
- connectStru[connectID].ColInfo[connectStru
- [connectID].iCol].pfSqlType=0;break;
- case SQL_CHAR:
- connectStru[connectID].ColInfo[connectStru
- [connectID].iCol].pfSqlType=0;break;
- case SQL_DATE:
- connectStru[connectID].ColInfo[connectStru
- [connectID].iCol].pcbColDef=80;
- connectStru[connectID].ColInfo[connectStru
- [connectID].iCol].pfSqlType=0;break;
- case SQL_DECIMAL:
- connectStru[connectID].ColInfo[connectStru
- [connectID].iCol].pcbColDef=80;
- connectStru[connectID].ColInfo[connectStru
- [connectID].iCol].pfSqlType=1;break;
- case SQL_DOUBLE:
- connectStru[connectID].ColInfo[connectStru
- [connectID].iCol].pcbColDef=80;
- connectStru[connectID].ColInfo[connectStru
- [connectID].iCol].pfSqlType=1;break;
- case SQL_FLOAT:
- connectStru[connectID].ColInfo[connectStru
- [connectID].iCol].pcbColDef=80;
- connectStru[connectID].ColInfo[connectStru
- [connectID].iCol].pfSqlType=1;break;
- case SQL_INTEGER:
- connectStru[connectID].ColInfo[connectStru
- [connectID].iCol].pcbColDef=80;
- connectStru[connectID].ColInfo[connectStru
- [connectID].iCol].pfSqlType=0;break;
- case SQL_LONGVARCHAR:
- connectStru[connectID].ColInfo[connectStru
- [connectID].iCol].pfSqlType=0;break;
- case SQL_LONGVARBINARY:
- connectStru[connectID].ColInfo[connectStru
- [connectID].iCol].pfSqlType=0;break;
- case SQL_NUMERIC:
- connectStru[connectID].ColInfo[connectStru
- [connectID].iCol].pcbColDef=80;
- connectStru[connectID].ColInfo[connectStru
- [connectID].iCol].pfSqlType=1;break;
- case SQL_REAL:
- connectStru[connectID].ColInfo[connectStru
- [connectID].iCol].pcbColDef=80;
- connectStru[connectID].ColInfo[connectStru
- [connectID].iCol].pfSqlType=1;break;
- case SQL_SMALLINT:
- connectStru[connectID].ColInfo[connectStru
- [connectID].iCol].pcbColDef=80;
- connectStru[connectID].ColInfo[connectStru
- [connectID].iCol].pfSqlType=0;break;
- case SQL_TIME:
- connectStru[connectID].ColInfo[connectStru
- [connectID].iCol].pcbColDef=80;
- connectStru[connectID].ColInfo[connectStru
- [connectID].iCol].pfSqlType=0;break;
- case SQL_TIMESTAMP:
- connectStru[connectID].ColInfo[connectStru
- [connectID].iCol].pcbColDef=80;
- connectStru[connectID].ColInfo[connectStru
- [connectID].iCol].pfSqlType=0;break;
- case SQL_TINYINT:
- connectStru[connectID].ColInfo[connectStru
- [connectID].iCol].pcbColDef=80;
- connectStru[connectID].ColInfo[connectStru
- [connectID].iCol].pfSqlType=0;break;
- case SQL_VARBINARY:
- connectStru[connectID].ColInfo[connectStru
- [connectID].iCol].pfSqlType=0;break;
- case SQL_VARCHAR:
- connectStru[connectID].ColInfo[connectStru
- [connectID].iCol].pfSqlType=0;break;
- default:
- connectStru[connectID].ColInfo[connectStru
- [connectID].iCol].pfSqlType=0;break;}
- if(retcode!=SQL_SUCCESS) break;
- connectStru[connectID].iCol++;}
- connectStru[connectID].retstr=connectStru[connectID].returnstring;
- *connectStru[connectID].retstr=' ';
- while(TRUE) {
- connectStru[connectID].retcode=
- SQLFetch(connectStru[connectID].phstmt);
- if(connectStru[connectID].retcode!=SQL_SUCCESS) {
- if(strlen(connectStru[connectID].returnstring)>0)
- *(connectStru[connectID].retstr-2)=' ';
- sendsqlresult(connectID);break;}
- for(connectStru[connectID].i=1;
- connectStru[connectID].i<=connectStru[connectID].iCol;
- connectStru[connectID].i++) {
- retcode=SQLGetData(connectStru[connectID].phstmt,
- connectStru[connectID].i,
- connectStru[connectID].ColInfo[connectStru[connectID].i].SqlType,
- connectStru[connectID].retstr,
- connectStru[connectID].ColInfo[connectStru[connectID].i].pcbColDef,
- &(connectStru[connectID].ColInfo[connectStru[connectID].i].len));
- if(connectStru[connectID].ColInfo[connectStru[connectID].i].len!=
- (unsigned long)lstrlen(connectStru[connectID].retstr))
- connectStru[connectID].ColInfo[connectStru[connectID].i].len=0;
- if(connectStru[connectID].ColInfo[connectStru[connectID].i].pfSqlType==1) {
- connectStru[connectID].ii=0;
- while(connectStru[connectID].ii<connectStru[connectID].ColInfo[connectStru[connectID].i].len) {
- if(*(connectStru[connectID].retstr)!='.') {
- connectStru[connectID].retstr++;
- connectStru[connectID].ii++;}
- else {
- connectStru[connectID].jj=connectStru[connectID].
- ColInfo[connectStru[connectID].i].len - connectStru[connectID].ii - 1;
- while(connectStru[connectID].ii<connectStru[connectID].
- ColInfo[connectStru[connectID].i].len) {
- if(*(connectStru[connectID].retstr+connectStru[connectID].jj)=='0') {
- connectStru[connectID].
- ColInfo[connectStru[connectID].i].len--;
- connectStru[connectID].jj--;}
- else break;}
- if(*(connectStru[connectID].retstr+
- connectStru[connectID].jj)=='.') {
- connectStru[connectID].
- ColInfo[connectStru[connectID].i].len--;
- connectStru[connectID].jj--;}
- connectStru[connectID].retstr=connectStru[connectID].retstr+connectStru[connectID].jj+1;
- break;}}}
- else
- connectStru[connectID].retstr=connectStru[connectID].retstr+connectStru[connectID].ColInfo[connectStru[connectID].i].len;
- //connectStru[connectID].ulpos=
- //connectStru[connectID].ulpos+
- //connectStru[connectID].ColInfo[connectStru[connectID].i].len+1;
- *(connectStru[connectID].retstr)='t';
- connectStru[connectID].retstr++;}
- connectStru[connectID].retstr--;
- *(connectStru[connectID].retstr)='r';
- connectStru[connectID].retstr++;
- *(connectStru[connectID].retstr)='n';
- connectStru[connectID].retstr++;
- *connectStru[connectID].retstr=' ';
- if(strlen(connectStru[connectID].returnstring)>30000) {
- sendsqlresult(connectID);
- connectStru[connectID].retstr=
- connectStru[connectID].returnstring;
- *connectStru[connectID].retstr=' ';}}
- retcode=SQLFreeStmt(connectStru[connectID].phstmt,SQL_DROP);
- if(retcode!=SQL_SUCCESS) {makeDisconnect(connectID);return(TRUE);}
- else {
- retcode=SQLAllocStmt(connectStru[connectID].hdbc,
- &(connectStru[connectID].phstmt));
- if(retcode!=SQL_SUCCESS) {makeDisconnect(connectID);return(TRUE);}}}
- //----------------------------------------------------
- BOOL sqlUpdateFun(connectID,requeststring)
- int connectID;
- unsigned char *requeststring;
- {
- unsigned char *returnstring,*ptr;
- RETCODE retcode;
- returnstring=connectStru[connectID].returnstring;
- while(TRUE) {
- ptr=strchr(requeststring,';');
- if(ptr==NULL) break;
- *ptr=' ';
- if(oneUpdate(connectID,requeststring)==FALSE) {
- retcode=SQLTransact(henv,connectStru[connectID].hdbc,
- SQL_ROLLBACK);
- WriteHtml(connectStru[connectID].pECB,"ERROR004");
- return(FALSE);}
- requeststring=ptr+1;}
- if(oneUpdate(connectID,requeststring)==FALSE) {
- retcode=SQLTransact(henv,
- connectStru[connectID].hdbc,SQL_ROLLBACK);
- WriteHtml(connectStru[connectID].pECB,"ERROR004");
- return(FALSE);}
- retcode=SQLTransact(henv,
- connectStru[connectID].hdbc,SQL_COMMIT);
- if(retcode!=SQL_SUCCESS) {
- WriteHtml(connectStru[connectID].pECB,"ERROR004");
- return(FALSE);}
- WriteHtml(connectStru[connectID].pECB,"GOOD0000");
- return(TRUE);}
- //----------------------------------------
- UCHAR FAR * changerequest(requeststring)
- UCHAR FAR * requeststring;
- {
- UCHAR FAR * ptr;
- while((*requeststring<=' ')&&(*requeststring!=' ')) requeststring++;
- ptr=requeststring;
- while(TRUE) {
- if(*requeststring==' ') break;
- if(*requeststring=='t') *requeststring=' ';
- requeststring++;}
- return(ptr);
- }
- //------------------------------------------------------
- BOOL oneUpdate(connectID,requeststring)
- int connectID;
- UCHAR FAR * requeststring;
- {
- RETCODE retcode;
- requeststring=changerequest(requeststring);
- if(strlen(requeststring)==0) return(TRUE);
- // WriteHtml(connectStru[connectID].pECB,requeststring);
- retcode=SQLExecDirect(connectStru[connectID].phstmt,
- requeststring,SQL_NTS);
- // SQLError(
- // henv,
- // connectStru[connectID].hdbc,
- // connectStru[connectID].phstmt,
- // stateerror,&dwerror,sqlerror,300,&dwerror1);
- // WriteHtml(connectStru[connectID].pECB,sqlerror);
-
- if(retcode!=SQL_SUCCESS)
- return(connectStru[connectID].errorUpdate);
- return(TRUE);}
- //---------------------------
- void sendsqlresult(int connectID) {
- long len;
- short i,j;
- UCHAR FAR lenstr[20],lenstring[20],*ptr;
- len=strlen(connectStru[connectID].returnstring);
- len=compress_code(connectStru[connectID].returnstring,len);
- //len=uncode_expand(connectStru[connectID].returnstring,len);
- ltoa(len,lenstr,10);
- ptr=&lenstring[0];
- j=8 - (short)(strlen(lenstr));
- for(i=0;i<j;i++) {
- *ptr='0';ptr++;}
- *ptr=' ';
- strcat(lenstring,lenstr);
- WriteHtml(connectStru[connectID].pECB,lenstring);
- connectStru[connectID].returnstring[len]=' ';
- WriteHtml(connectStru[connectID].pECB,
- connectStru[connectID].returnstring);}
- //----------------------------------
- //connectStru[connectID].descptr=connectStru[connectID].sqlselectstring;
- //connectStru[connectID].sourceptr=sqlselectstring;
- //connectStru[connectID].i=0;
- //while(connectStru[connectID].i<300) {
- // if(strnicmp(connectStru[connectID].sourceptr,"from ",5)==0) {
- // strcat(connectStru[connectID].descptr,"from ");
- // strcat(connectStru[connectID].descptr,connectStru[connectID].dataDir);
- // connectStru[connectID].sourceptr=connectStru[connectID].sourceptr+5;
- // connectStru[connectID].i=connectStru[connectID].i+5;break;}
- // *(connectStru[connectID].descptr)=*(connectStru[connectID].sourceptr);
- // connectStru[connectID].descptr++;
- // connectStru[connectID].sourceptr++;connectStru[connectID].i++;}
- // if(connectStru[connectID].i==300) return(FALSE);
- // while((*(connectStru[connectID].sourceptr)<=0x20)&&(connectStru[connectID].i<300)) {
- // connectStru[connectID].sourceptr++;connectStru[connectID].i++;}
- // strcat(connectStru[connectID].sqlselectstring,connectStru[connectID].sourceptr);
- //