{ lsqllive.pas } program LSqlLive; {$IfDef VER80} uses SnipTool, SnipData, SysUtils, WinTypes, WinProcs, DbiProcs, DbiTypes, DbiErrs; {$Else} uses WinTypes, WinCrt, Strings, DbiProcs, DbiTypes, DbiErrs, SnipTool, SnipData; {$EndIf} const szTblName = 'cust'; { Name of table to be created. } szTblType = szDBASE; { Table type to use. } {===================================================================== Function: LiveSQL(); Description: This example shows how to do live local SQL on a dBASE table. ===================================================================== } var hDb: hDBIDb; { Handle to the Database } hCur: hDBICur; { Handle to the Answer Table } hStmt: hDBIStmt; { Handle to the SQL Statement } hMCur: hDBICur; { Handle to the master table. } uLength: UINT16; TblProps: CURProps; { Table descriptor } TblMProps: CURProps; { Table descriptor } bEmpty: BOOL; szCity: array[0..20] of CHAR; bLive: LIVENESS; ulCustNum: FLOAT; const szQry = 'SELECT cust_no, name, city, phone, country '#13+ 'FROM "cust.dbf" '#13+ 'WHERE cust_no>4000'; { The Text of the SQL Statement } pRecBuf: pBYTE = nil; { Pointer to the record buffer } pMRecBuf: pBYTE = nil; { Pointer to the record buffer } szNewCity: pCHAR = 'Chicago'; begin Screen('*** Live Answer Table Example ***'); Screen(' Initializing IDAPI...'); if (InitAndConnect(hDb) <> DBIERR_NONE) then { Terminate example if } begin { Initialization fails } Screen(''); Screen('*** End of Example ***'); exit; end; Screen(' Setting the default Database directory...'); ChkRslt(DbiSetDirectory(hDb, pCHAR(szTblDirectory)), DBIERR_NONE, ' Error - SetDirectory.'); Screen(' Perform the following SQL statement on the table:'); Screen(''); Screen(szQry); Screen(''); Screen(' Prepare the SQL statement...'); if (ChkRslt(DbiQPrepare(hDb, qrylangSQL, szQry, hStmt), DBIERR_NONE, ' Error - QPrepare.') <> DBIERR_NONE) then begin CloseDbAndExit(hDb); Screen(''); Screen('*** End of Example ***'); exit; end; { Determine the state of the answer set - is it live or not. } ChkRslt(DbiGetProp (hDBIObj(hStmt), stmtLIVENESS, @(bLive), sizeof(BOOL), uLength), DBIERR_NONE, ' Error - GetProp.'); { Check if the statement cursor is live. } if (bLive <> wantLIVE) then begin { Now set the statement cursor to be live. } ChkRslt(DbiSetProp(hDBIObj(hStmt), stmtLIVENESS, LongInt(wantLIVE)), DBIERR_NONE, ' Error - SetProp.'); end; Screen(' Execute the SQL statement...'); if (ChkRslt(DbiQExec(hStmt, @hCur), DBIERR_NONE, ' Error - QExec.') <> DBIERR_NONE) then begin CloseDbAndExit(hDb); Screen(''); Screen('*** End of Example ***'); exit; end; { Determine if a valid hCursor was returned. } if Assigned (hCur) then begin Screen(' Opening the master table....'); if (ChkRslt(DbiOpenTable(hDb, pCHAR(szTblName), pCHAR(szTblType), nil, nil, 0, dbiREADWRITE, dbiOPENSHARED, xltFIELD, FALSE, nil, hMCur), DBIERR_NONE, ' Error - OpenTable.') <> DBIERR_NONE) then begin ChkRslt(DbiCloseCursor(hCur), DBIERR_NONE, ' Error - CloseCursor.'); CloseDbAndExit(hDb); Screen(''); Screen('*** End of Example ***'); exit; end; { Next allocate memory for the record buffer. } ChkRslt(DbiGetCursorProps(hMCur, TblMProps), DBIERR_NONE, ' Error - GetCursorProps.'); GetMem(pMRecBuf, TblMProps.iRecBufSize); Screen(''); Screen(' Switching to an index in the master table... '); ChkRslt(DbiSwitchToIndex(hMCur, 'CUST.MDX', 'CUST_NO', 0, FALSE), DBIERR_NONE, ' Error - SwitchToIndex.'); Screen(' Clear the record buffer...'); ChkRslt(DbiInitRecord(hMCur, pMRecBuf), DBIERR_NONE, ' Error - InitRecord.'); Screen(' Display the first Record in the answer table...'); ChkRslt(DbiSetToBegin(hCur), DBIERR_NONE, ' Error - SetToBegin.'); DisplayTable(hCur, 1); { Modify the table and then change back to the original values. } ChkRslt(DbiGetCursorProps(hCur, TblProps), DBIERR_NONE, ' Error - GetCursorProps.'); { Next allocate memory for the record buffer. } GetMem(pRecBuf, TblProps.iRecBufSize); { Now get the first record in the live query. } ChkRslt(DbiSetToBegin(hCur), DBIERR_NONE, ' Error - SetToBegin.'); ChkRslt(DbiGetNextRecord(hCur, dbiNOLOCK, pRecBuf, nil), DBIERR_NONE, ' Error - GetNextRecord.'); { Get the Master table's fourth field (which is the 3rd field for the answer table) value so that we can put it back after modifying the table. } ChkRslt(DbiGetField(hCur, 3, pRecBuf, @szCity, bEmpty), DBIERR_NONE, ' Error - GetField.'); ChkRslt(DbiGetField(hCur, 1, pRecBuf, @(ulCustNum), bEmpty), DBIERR_NONE, ' Error - GetField.'); { Modify the City of the first record. } ChkRslt(DbiPutField(hCur, 3, pRecBuf, szNewCity), DBIERR_NONE, ' Error - PutField.'); Screen(''); Screen(' Modifying the City field in the first record '+ 'of the DynaSet from '+StrPas(szCity)+' to ' + ''+StrPas(szNewCity)+'....'); ChkRslt(DbiModifyRecord(hCur, pRecBuf, TRUE), DBIERR_NONE, ' Error - ModifyRecord.'); Screen(' Re-displaying the Modified Record of the master table '+ 'to show the change took place...'); { Now set the value for the search into the record buffer. } ChkRslt(DbiPutField(hMCur, 1, pMRecBuf, @(ulCustNum)), DBIERR_NONE, ' Error - PutField.'); { Now set the cursor unto the field in the master table - to verify that it was modified. } ChkRslt(DbiSetToKey(hMCur, keySEARCHEQ, FALSE, 0, 0, pMRecBuf), DBIERR_NONE, ' Error - SetToKey'); DisplayNextRecord(hMCur); ChkRslt(DbiSetToBegin(hCur), DBIERR_NONE, ' Error - SetToBegin.'); { Change the data back to the way it was. } ChkRslt(DbiGetNextRecord(hCur, dbiNOLOCK, pRecBuf, nil), DBIERR_NONE, ' Error - GetRecord.'); ChkRslt(DbiPutField(hCur, 3, pRecBuf, @szCity), DBIERR_NONE, ' Error - PutField.'); Screen(''); Screen(' Saving back the original City name to keep the ' + 'example table consistent...'); ChkRslt(DbiModifyRecord(hCur, pRecBuf, TRUE), DBIERR_NONE, ' Error - ModifyRecord.'); Screen(''); Screen(' Close the cursor to the answer set... '); ChkRslt(DbiCloseCursor(hCur), DBIERR_NONE, ' Error - CloseCursor.'); Screen(''); Screen(' Close the cursor to the master table... '); ChkRslt(DbiCloseCursor(hMCur), DBIERR_NONE, ' Error - CloseCursor.'); FreeMem(pRecBuf, TblProps.iRecBufSize); FreeMem(pMRecBuf, TblMProps.iRecBufSize); end else Screen(' Could not get cursor to the answer table.'); Screen(''); Screen(' Release memory allocated for the Query...'); ChkRslt(DbiQFree(hStmt), DBIERR_NONE, ' Error - QryFree.'); Screen(' Close the Database and exit IDAPI...'); CloseDbAndExit(hDb); Screen('*** End of Example ***'); end.