SQLPROG.H
上传用户:zdlsnail
上传日期:2007-01-06
资源大小:16k
文件大小:16k
源码类别:

压缩解压

开发平台:

Visual C++

  1. UCHAR rootDir[]="C:\DBSERVER\";
  2. UCHAR HTMLHead[]="00000000";
  3. HENV henv;
  4. typedef struct
  5.   {
  6.     UDWORD  pcbColDef;
  7.     SWORD  pfSqlType;
  8.     SWORD  SqlType;
  9.     SWORD  pibScale;
  10.     UDWORD len;}ColumnInfo;
  11. ColumnInfo ColInfo[256];
  12. typedef struct
  13.   {
  14.     short  ID;
  15.     UCHAR  connString[200];}connIDInfo;
  16. connIDInfo ConIDStru[MAXCONNECTID];
  17. typedef struct
  18.   {
  19.     UCHAR  userName[20];
  20.     UCHAR  passWord[20];
  21.     UCHAR  winName[20];
  22.     UCHAR  pathName[80];}userstru;
  23. userstru userInfoStru[MAXUSER];
  24. long userCount=0;
  25. char far author[]="\AUTHOR.SAV";
  26. short connectCount=0;
  27. typedef struct
  28. {
  29. //UCHAR dataDir[50];
  30. UCHAR sqlselectstring[300];
  31. short usermark;
  32. short ID;
  33. UCHAR connstring[200];
  34.     HDBC hdbc;
  35.     HSTMT phstmt;
  36.     //UDWORD ulpos;
  37.     UWORD iCol;
  38. UWORD i;
  39.     UCHAR FAR * descptr;
  40. UCHAR FAR * sourceptr;
  41. unsigned char *returnstring;
  42. unsigned char *retstr;
  43. RETCODE retcode;
  44. ColumnInfo ColInfo[256];
  45.     UCHAR DSN[80];
  46.     UCHAR UID[30];
  47.     UCHAR PASSWORD[30];
  48. unsigned long ii;
  49. unsigned long jj;
  50.     unsigned char dwLib[30];
  51.     unsigned char dwName[30];
  52. EXTENSION_CONTROL_BLOCK *pECB;
  53. BOOL errorUpdate;
  54. } ConnStruDef;
  55. ConnStruDef connectStru[MAXCONNECT];
  56. //char far author[]="\AUTHOR.SAV";
  57. //char far id_section[]="@_ID_section";
  58. //short connectCount=0;
  59. void WriteHtml(EXTENSION_CONTROL_BLOCK *pECB,LPSTR lpsz);
  60. void makeConnectString(UCHAR FAR *,short ID);
  61. void sendsqlresult(int connectID);
  62. UCHAR FAR * changerequest(UCHAR FAR * requeststring);
  63. UCHAR sqlerror[300],stateerror[300];
  64. SDWORD dwerror;
  65. SWORD dwerror1;
  66. BOOL oneUpdate(int ConnctID,UCHAR FAR * requeststring);
  67. //--------------------------------------------
  68. void makeDisconnect(connectID) 
  69. int connectID;
  70.   {
  71.   if(connectStru[connectID].phstmt==SQL_NULL_HSTMT)
  72.     SQLFreeStmt(connectStru[connectID].phstmt,SQL_DROP);
  73.   if(connectStru[connectID].hdbc==SQL_NULL_HDBC)
  74.     SQLDisconnect(connectStru[connectID].hdbc);
  75.   //if(henv==SQL_NULL_HENV)
  76.   //  SQLFreeEnv(henv);
  77.   connectStru[connectID].ID=0;}
  78. //----------------------------------------------------------
  79. BOOL makeConnect(connectID,ID)
  80. int connectID;
  81. short ID;
  82.  {
  83.     UCHAR FAR tmp[200];
  84. SWORD MAXStringLen;
  85. //    UCHAR szSqlState[6],szErrorMsg[300];
  86. //    SDWORD dwNativeError;
  87.  //   SWORD wErrorMsg;
  88.   RETCODE retcode;
  89.   makeConnectString(connectStru[connectID].connstring,ID);
  90.   if(connectStru[connectID].connstring==NULL) return(FALSE);
  91.   connectStru[connectID].ID=ID;
  92.   retcode=SQLAllocConnect(henv,&(connectStru[connectID].hdbc));
  93.   if(retcode!=SQL_SUCCESS) {
  94.    makeDisconnect(connectID);return(FALSE);}
  95.   //  SQLSetConnectOption(connectStru[connectID].hdbc,SQL_LOGIN_TIMEOUT,10);
  96.     SQLSetConnectOption(connectStru[connectID].hdbc,SQL_AUTOCOMMIT,SQL_AUTOCOMMIT_OFF);
  97.     //SQLSetConnectOption(connectStru[connectID].hdbc,SQL_ACCESS_MODE,SQL_MODE_READ_WRITE);
  98. retcode=SQLDriverConnect(connectStru[connectID].hdbc,
  99.   NULL,
  100.   connectStru[connectID].connstring,SQL_NTS,
  101.   tmp,200,&MAXStringLen,SQL_DRIVER_NOPROMPT);
  102.     //retcode=SQLConnect(connectStru[connectID].hdbc,
  103. //  connectStru[connectID].DSN,SQL_NTS,
  104. //  connectStru[connectID].UID,SQL_NTS,
  105. //  connectStru[connectID].PASSWORD,SQL_NTS);
  106. //itoa(retcode,tmp,10);
  107. //strcat(tmp,connectStru[connectID].DSN);
  108. //strcat(tmp,connectStru[connectID].UID);
  109. //strcat(tmp,connectStru[connectID].PASSWORD);
  110.   //WriteHtml(connectStru[connectID].pECB,tmp);
  111.   //WriteHtml(connectStru[connectID].pECB,connectStru[connectID].connstring);
  112.  // SQLError(henv,
  113. //   connectStru[connectID].hdbc,SQL_NULL_HSTMT,
  114. //   szSqlState,&dwNativeError,szErrorMsg,300,&wErrorMsg);
  115.  // WriteHtml(connectStru[connectID].pECB,szErrorMsg);
  116.     if((retcode==SQL_SUCCESS) || (retcode==SQL_SUCCESS_WITH_INFO)) {
  117.       retcode=SQLAllocStmt(connectStru[connectID].hdbc,
  118. &(connectStru[connectID].phstmt));
  119.       if(retcode!=SQL_SUCCESS) {makeDisconnect(connectID);return(FALSE);}
  120.       return(TRUE);}
  121.     else {   
  122.       makeDisconnect(connectID);return(FALSE);}}
  123. //----------------------------------------------------------
  124. UCHAR szcolName[80];
  125. SWORD cbColNameMax=80,pcbColName;
  126. SWORD pibScale,pfNullable;
  127. BOOL sqlSelectFun(connectID,sqlselectstring)
  128. int connectID;
  129. UCHAR FAR * sqlselectstring;
  130.   {
  131. RETCODE retcode;
  132.     sqlselectstring=changerequest(sqlselectstring);
  133. //connectStru[connectID].ulpos=0;
  134.     retcode=SQLExecDirect(connectStru[connectID].phstmt,
  135.   sqlselectstring,SQL_NTS);
  136.     if(retcode!=SQL_SUCCESS) {
  137.       WriteHtml(connectStru[connectID].pECB,"ERROR004");
  138.   return(FALSE);} 
  139.     connectStru[connectID].iCol=1;
  140.     while(TRUE) {
  141.       retcode=SQLDescribeCol(
  142. connectStru[connectID].phstmt,
  143. connectStru[connectID].iCol,
  144.     szcolName,
  145. cbColNameMax,
  146. &pcbColName,
  147.     &(connectStru[connectID].ColInfo[connectStru[connectID].iCol].pfSqlType),
  148. &(connectStru[connectID].ColInfo[connectStru[connectID].iCol].pcbColDef),
  149.     &(connectStru[connectID].ColInfo[connectStru[connectID].iCol].pibScale),
  150. &pfNullable);
  151.        connectStru[connectID].ColInfo[connectStru
  152.          [connectID].iCol].SqlType=SQL_C_CHAR;
  153.       switch(connectStru[connectID].ColInfo[connectStru[connectID].iCol].pfSqlType) {
  154.   case SQL_BIGINT:
  155.     connectStru[connectID].ColInfo[connectStru
  156.       [connectID].iCol].pcbColDef=80;
  157.     connectStru[connectID].ColInfo[connectStru
  158.       [connectID].iCol].pfSqlType=0;break;
  159.   case SQL_BINARY:
  160.     connectStru[connectID].ColInfo[connectStru
  161.       [connectID].iCol].pfSqlType=0;break;
  162.   case SQL_BIT:
  163.     connectStru[connectID].ColInfo[connectStru
  164.       [connectID].iCol].pcbColDef=80;
  165.     connectStru[connectID].ColInfo[connectStru
  166.       [connectID].iCol].pfSqlType=0;break;
  167.   case SQL_CHAR:
  168.     connectStru[connectID].ColInfo[connectStru
  169.       [connectID].iCol].pfSqlType=0;break;
  170.   case SQL_DATE:
  171.     connectStru[connectID].ColInfo[connectStru
  172.       [connectID].iCol].pcbColDef=80;
  173.     connectStru[connectID].ColInfo[connectStru
  174.       [connectID].iCol].pfSqlType=0;break;
  175.   case SQL_DECIMAL:
  176.     connectStru[connectID].ColInfo[connectStru
  177.       [connectID].iCol].pcbColDef=80;
  178.     connectStru[connectID].ColInfo[connectStru
  179.       [connectID].iCol].pfSqlType=1;break;
  180.   case SQL_DOUBLE:
  181.     connectStru[connectID].ColInfo[connectStru
  182.       [connectID].iCol].pcbColDef=80;
  183.     connectStru[connectID].ColInfo[connectStru
  184.       [connectID].iCol].pfSqlType=1;break;
  185.   case SQL_FLOAT:
  186.     connectStru[connectID].ColInfo[connectStru
  187.       [connectID].iCol].pcbColDef=80;
  188.     connectStru[connectID].ColInfo[connectStru
  189.       [connectID].iCol].pfSqlType=1;break;
  190.   case SQL_INTEGER:
  191.     connectStru[connectID].ColInfo[connectStru
  192.       [connectID].iCol].pcbColDef=80;
  193.     connectStru[connectID].ColInfo[connectStru
  194.       [connectID].iCol].pfSqlType=0;break;
  195.   case SQL_LONGVARCHAR:
  196.     connectStru[connectID].ColInfo[connectStru
  197.       [connectID].iCol].pfSqlType=0;break;
  198.   case SQL_LONGVARBINARY:
  199.     connectStru[connectID].ColInfo[connectStru
  200.       [connectID].iCol].pfSqlType=0;break;
  201.   case SQL_NUMERIC:
  202.     connectStru[connectID].ColInfo[connectStru
  203.       [connectID].iCol].pcbColDef=80;
  204.     connectStru[connectID].ColInfo[connectStru
  205.       [connectID].iCol].pfSqlType=1;break;
  206.   case SQL_REAL:
  207.     connectStru[connectID].ColInfo[connectStru
  208.       [connectID].iCol].pcbColDef=80;
  209.     connectStru[connectID].ColInfo[connectStru
  210.       [connectID].iCol].pfSqlType=1;break;
  211.   case SQL_SMALLINT:
  212.     connectStru[connectID].ColInfo[connectStru
  213.       [connectID].iCol].pcbColDef=80;
  214.     connectStru[connectID].ColInfo[connectStru
  215.       [connectID].iCol].pfSqlType=0;break;
  216.   case SQL_TIME:
  217.     connectStru[connectID].ColInfo[connectStru
  218.       [connectID].iCol].pcbColDef=80;
  219.     connectStru[connectID].ColInfo[connectStru
  220.       [connectID].iCol].pfSqlType=0;break;
  221.   case SQL_TIMESTAMP:
  222.     connectStru[connectID].ColInfo[connectStru
  223.       [connectID].iCol].pcbColDef=80;
  224.     connectStru[connectID].ColInfo[connectStru
  225.       [connectID].iCol].pfSqlType=0;break;
  226.   case SQL_TINYINT:
  227.     connectStru[connectID].ColInfo[connectStru
  228.       [connectID].iCol].pcbColDef=80;
  229.     connectStru[connectID].ColInfo[connectStru
  230.       [connectID].iCol].pfSqlType=0;break;
  231.   case SQL_VARBINARY:
  232.     connectStru[connectID].ColInfo[connectStru
  233.       [connectID].iCol].pfSqlType=0;break;
  234.   case SQL_VARCHAR:
  235.     connectStru[connectID].ColInfo[connectStru
  236.       [connectID].iCol].pfSqlType=0;break;
  237.   default:
  238.     connectStru[connectID].ColInfo[connectStru
  239.       [connectID].iCol].pfSqlType=0;break;}
  240.   if(retcode!=SQL_SUCCESS) break;
  241.       connectStru[connectID].iCol++;}
  242.     connectStru[connectID].retstr=connectStru[connectID].returnstring;
  243.     *connectStru[connectID].retstr='';
  244.   while(TRUE) {
  245.   connectStru[connectID].retcode=
  246.     SQLFetch(connectStru[connectID].phstmt);
  247.       if(connectStru[connectID].retcode!=SQL_SUCCESS) {
  248. if(strlen(connectStru[connectID].returnstring)>0) 
  249.   *(connectStru[connectID].retstr-2)='';
  250.         sendsqlresult(connectID);break;}
  251.   for(connectStru[connectID].i=1;
  252.     connectStru[connectID].i<=connectStru[connectID].iCol;
  253.     connectStru[connectID].i++) {
  254.     retcode=SQLGetData(connectStru[connectID].phstmt,
  255.       connectStru[connectID].i,
  256.   connectStru[connectID].ColInfo[connectStru[connectID].i].SqlType,
  257.   connectStru[connectID].retstr,
  258.   connectStru[connectID].ColInfo[connectStru[connectID].i].pcbColDef,
  259.   &(connectStru[connectID].ColInfo[connectStru[connectID].i].len));
  260. if(connectStru[connectID].ColInfo[connectStru[connectID].i].len!=
  261.   (unsigned long)lstrlen(connectStru[connectID].retstr)) 
  262.   connectStru[connectID].ColInfo[connectStru[connectID].i].len=0;
  263. if(connectStru[connectID].ColInfo[connectStru[connectID].i].pfSqlType==1) {
  264.   connectStru[connectID].ii=0;
  265.   while(connectStru[connectID].ii<connectStru[connectID].ColInfo[connectStru[connectID].i].len) {
  266. if(*(connectStru[connectID].retstr)!='.') {
  267.   connectStru[connectID].retstr++;
  268.   connectStru[connectID].ii++;}
  269. else {
  270.   connectStru[connectID].jj=connectStru[connectID].
  271. ColInfo[connectStru[connectID].i].len - connectStru[connectID].ii - 1;
  272.   while(connectStru[connectID].ii<connectStru[connectID].
  273. ColInfo[connectStru[connectID].i].len) {
  274. if(*(connectStru[connectID].retstr+connectStru[connectID].jj)=='0') {
  275.         connectStru[connectID].
  276.      ColInfo[connectStru[connectID].i].len--;
  277.   connectStru[connectID].jj--;}
  278. else break;}
  279.               if(*(connectStru[connectID].retstr+
  280. connectStru[connectID].jj)=='.') {
  281.       connectStru[connectID].
  282.        ColInfo[connectStru[connectID].i].len--;
  283.     connectStru[connectID].jj--;}
  284.   connectStru[connectID].retstr=connectStru[connectID].retstr+connectStru[connectID].jj+1;
  285.   break;}}}
  286. else
  287.           connectStru[connectID].retstr=connectStru[connectID].retstr+connectStru[connectID].ColInfo[connectStru[connectID].i].len;
  288.         //connectStru[connectID].ulpos=
  289. //connectStru[connectID].ulpos+
  290. //connectStru[connectID].ColInfo[connectStru[connectID].i].len+1;
  291. *(connectStru[connectID].retstr)='t';
  292. connectStru[connectID].retstr++;}
  293. connectStru[connectID].retstr--;
  294. *(connectStru[connectID].retstr)='r';
  295. connectStru[connectID].retstr++;
  296.         *(connectStru[connectID].retstr)='n';
  297. connectStru[connectID].retstr++;
  298. *connectStru[connectID].retstr='';
  299. if(strlen(connectStru[connectID].returnstring)>30000) {
  300.           sendsqlresult(connectID);
  301.           connectStru[connectID].retstr=
  302. connectStru[connectID].returnstring;
  303.          *connectStru[connectID].retstr='';}}
  304. retcode=SQLFreeStmt(connectStru[connectID].phstmt,SQL_DROP);
  305. if(retcode!=SQL_SUCCESS) {makeDisconnect(connectID);return(TRUE);}
  306. else {
  307.   retcode=SQLAllocStmt(connectStru[connectID].hdbc,
  308.   &(connectStru[connectID].phstmt));
  309.   if(retcode!=SQL_SUCCESS) {makeDisconnect(connectID);return(TRUE);}}}
  310. //----------------------------------------------------
  311. BOOL sqlUpdateFun(connectID,requeststring)
  312. int connectID;
  313. unsigned char *requeststring;
  314.   {
  315.      unsigned char *returnstring,*ptr;
  316.  RETCODE retcode;
  317.  returnstring=connectStru[connectID].returnstring;
  318.  while(TRUE) {
  319.        ptr=strchr(requeststring,';');
  320.        if(ptr==NULL) break;
  321.    *ptr='';
  322.    if(oneUpdate(connectID,requeststring)==FALSE) {
  323.          retcode=SQLTransact(henv,connectStru[connectID].hdbc,
  324.    SQL_ROLLBACK);
  325.          WriteHtml(connectStru[connectID].pECB,"ERROR004");
  326.  return(FALSE);}
  327.    requeststring=ptr+1;}
  328.    if(oneUpdate(connectID,requeststring)==FALSE) {
  329.          retcode=SQLTransact(henv,
  330.            connectStru[connectID].hdbc,SQL_ROLLBACK);
  331.          WriteHtml(connectStru[connectID].pECB,"ERROR004");
  332.  return(FALSE);}
  333.       retcode=SQLTransact(henv,
  334. connectStru[connectID].hdbc,SQL_COMMIT);
  335.       if(retcode!=SQL_SUCCESS) {
  336.         WriteHtml(connectStru[connectID].pECB,"ERROR004");
  337.         return(FALSE);}
  338.       WriteHtml(connectStru[connectID].pECB,"GOOD0000");
  339.       return(TRUE);}
  340.   //----------------------------------------
  341.   UCHAR FAR * changerequest(requeststring)
  342.   UCHAR FAR * requeststring;
  343.     {
  344.   UCHAR FAR * ptr; 
  345.   while((*requeststring<=' ')&&(*requeststring!='')) requeststring++;
  346.   ptr=requeststring;
  347.   while(TRUE) {
  348. if(*requeststring=='') break;
  349. if(*requeststring=='t') *requeststring=' ';
  350. requeststring++;}
  351.   return(ptr);
  352. }
  353.   //------------------------------------------------------
  354.   BOOL oneUpdate(connectID,requeststring)
  355.   int connectID;
  356.   UCHAR FAR * requeststring;
  357.     {
  358.   RETCODE retcode;
  359.   requeststring=changerequest(requeststring);
  360.   if(strlen(requeststring)==0) return(TRUE);
  361. //      WriteHtml(connectStru[connectID].pECB,requeststring);
  362.       retcode=SQLExecDirect(connectStru[connectID].phstmt,
  363.     requeststring,SQL_NTS);
  364. //  SQLError(
  365. // henv,
  366. // connectStru[connectID].hdbc,
  367. // connectStru[connectID].phstmt,
  368. // stateerror,&dwerror,sqlerror,300,&dwerror1);
  369. //    WriteHtml(connectStru[connectID].pECB,sqlerror);
  370.     
  371.       if(retcode!=SQL_SUCCESS) 
  372. return(connectStru[connectID].errorUpdate);
  373.   return(TRUE);}
  374. //---------------------------
  375.  void sendsqlresult(int connectID) {
  376. long len;
  377. short i,j;
  378.     UCHAR FAR lenstr[20],lenstring[20],*ptr;
  379.     len=strlen(connectStru[connectID].returnstring);
  380.     len=compress_code(connectStru[connectID].returnstring,len);
  381.     //len=uncode_expand(connectStru[connectID].returnstring,len);
  382.     ltoa(len,lenstr,10);
  383.     ptr=&lenstring[0];
  384. j=8 - (short)(strlen(lenstr));
  385.     for(i=0;i<j;i++) {
  386.   *ptr='0';ptr++;}
  387.     *ptr='';
  388.     strcat(lenstring,lenstr);
  389.     WriteHtml(connectStru[connectID].pECB,lenstring);
  390. connectStru[connectID].returnstring[len]='';
  391.     WriteHtml(connectStru[connectID].pECB,
  392. connectStru[connectID].returnstring);}
  393. //----------------------------------
  394.      //connectStru[connectID].descptr=connectStru[connectID].sqlselectstring;
  395.     //connectStru[connectID].sourceptr=sqlselectstring;
  396. //connectStru[connectID].i=0;
  397. //while(connectStru[connectID].i<300) {
  398.  // if(strnicmp(connectStru[connectID].sourceptr,"from ",5)==0) {
  399. // strcat(connectStru[connectID].descptr,"from ");
  400. // strcat(connectStru[connectID].descptr,connectStru[connectID].dataDir);
  401. // connectStru[connectID].sourceptr=connectStru[connectID].sourceptr+5;
  402. // connectStru[connectID].i=connectStru[connectID].i+5;break;}  
  403. //   *(connectStru[connectID].descptr)=*(connectStru[connectID].sourceptr);
  404. //   connectStru[connectID].descptr++;
  405. //   connectStru[connectID].sourceptr++;connectStru[connectID].i++;}
  406. // if(connectStru[connectID].i==300) return(FALSE);
  407. // while((*(connectStru[connectID].sourceptr)<=0x20)&&(connectStru[connectID].i<300)) {
  408. //   connectStru[connectID].sourceptr++;connectStru[connectID].i++;}
  409. // strcat(connectStru[connectID].sqlselectstring,connectStru[connectID].sourceptr);
  410. //