Correcting the C Language Program
The replacement pages for program cex10a in chapter 8 of the "ALLBASE/SQL
C Application Programming Guide" appear on the next three pages. The
first of the three contains only the unchanged comments for the routine.
________________________________________________________________________________
| |
| /* DataBuffer is the buffer containing retrieved data as a result */ |
| /* of a dynamic SELECT. */ |
| |
| char DataBuffer[MaxDataBuff]; |
| boolean Abort; |
| |
| struct SQLVarChar { |
| int Length; |
| char VarCharCol[MaxColSize]; |
| }; |
| |
| main() /* Beginning of Program */ |
| { |
| printf("\nC program illustrating dynamic command processing -- cex10a"); |
| printf("\n"); |
| printf("\nEvent List:"); |
| printf("\n CONNECT TO PartsDBE"); |
| printf("\n Prompt for any SQL command"); |
| printf("\n BEGIN WORK"); |
| printf("\n PREPARE"); |
| printf("\n DESCRIBE"); |
| printf("\n If command is a non-query command, EXECUTE it"); |
| printf("\n Otherwise execute the following:"); |
| printf("\n DECLARE CURSOR"); |
| printf("\n OPEN Cursor"); |
| printf("\n FETCH a row"); |
| printf("\n CLOSE Cursor"); |
| printf("\n COMMIT WORK"); |
| printf("\n Repeat the above ten steps"); |
| printf("\n RELEASE PartsDBE\n"); |
| |
| if (ConnectDBE()) { 4 |
| Describe(); 23 |
| ReleaseDBE(); |
| printf("\n"); |
| } |
| else |
| printf("\nError: Cannot Connect to PartsDBE"); |
| printf("\n"); |
| } /* End of Main Program */ |
| |
| |
| /* Function BCDToString converts a binary field in the "DataBuffer" */ |
| /* buffer to its ASCII representation. Input parameters are */ |
| /* the Length, Precision and Scale. The input decimal field is passed */|
| /* via "DataBuffer" and the output String is passed via "result". */ |
| |
________________________________________________________________________________
Figure 8-9. Program cex10a: Dynamic Commands of Unknown Format (page 2 of 11)
_____________________________________________________________________________
| |
| int BCDToString(DataBuffer, Length, Precision, Scale, Result0) 2 |
| char DataBuffer[]; |
| short Length, Precision, Scale; |
| char Result0[]; |
| { |
| #define hexd '0123456789ABCDEF' |
| #define ASCIIZero '0' |
| #define PlusSign 12 |
| #define MinusSign 13 |
| #define UnSigned 14 |
| #define btod(d,i) ((i&1)?((d[i/2])&0xf):((d[i/2]>>4)&0xf)) |
| |
| int i; |
| int DecimalPlace; |
| int PutPos=0; |
| int DataEnd; |
| int DataStart; |
| boolean done; |
| char space[MaxStr]; |
| char *Result; |
| |
| Result = space; |
| DataEnd = (Length*2) - 2; |
| DataStart = (DataEnd - Precision) + 1; |
| for (i = 0; i < MaxStr; i++) Result[i] = '\0'; |
| DecimalPlace = (Precision-Scale) - 1; |
| |
| /* convert decimal to character String */ |
| if (DecimalPlace == 0) Result[PutPos++] = '.'; |
| |
| /* convert each Nibble into a character */ |
| for (i = DataStart; i <= DataEnd; i++) { |
| Result[PutPos] = ASCIIZero + btod(DataBuffer,i); |
| if (PutPos == DecimalPlace) Result[++PutPos] = '.'; |
| PutPos++; |
| } |
| |
| i = 0; |
| done = FALSE; |
| while (i<strlen(Result) && Result[i]=='0') ++Result; |
| |
| if (Result[0] == '\0') |
| Result[0] = '0'; |
| else { |
| /* place a zero at the left of the decimal point */ |
| if (Result[0] == '.') StrInsert('0', Result); |
| |
_____________________________________________________________________________
Figure 8-9. Program cex10a: Dynamic Commands of Unknown Format (page 3 of 11)
_____________________________________________________________________________
| |
| /* insert sign */ |
| switch (btod(DataBuffer,(DataEnd + 1))) { |
| case PlusSign: StrInsert(' ', Result); |
| break; |
| case MinusSign: StrInsert('-', Result); |
| break; |
| default: break; |
| } /* End switch */ |
| } /* End else */ |
| strcpy(Result0, Result); |
| } /* End BCDToString */ |
| |
| int getline(linebuff) /*Function to get a line of characters */ |
| char linebuff[80]; |
| { |
| while (strlen(gets(linebuff)) ==0); |
| } /* End of function to get a line of characters */ |
| |
| int SQLStatusCheck() /* Function to Display Error Messages */ 3 |
| { |
| |
| Abort = FALSE; |
| if (sqlca.sqlcode < DeadLock) Abort = TRUE; |
| |
| do { |
| EXEC SQL SQLEXPLAIN :SQLMessage; |
| printf("\n"); |
| printf("%s\n",SQLMessage); |
| } while (sqlca.sqlcode != 0); |
| |
| if (Abort) { |
| |
| EXEC SQL COMMIT WORK RELEASE; |
| DynamicCommand[0] = '/'; |
| DynamicCommand[1] = '\0'; |
| } |
| |
| } /* End SQLStatusCheck Function */ |
| |
| |
| int ConnectDBE() /* Function to Connect to PartsDBE */ |
| { 4 |
| boolean Connect; |
| printf("\nConnect to PartsDBE"); |
| EXEC SQL CONNECT TO 'PartsDBE'; 37 |
| |
| |
| |
_____________________________________________________________________________
Figure 8-9. Program cex10a: Dynamic Commands of Unknown Format (page 4 of 11)