home *** CD-ROM | disk | FTP | other *** search
- // BDE32 3.x - (C) Copyright 1996 by Borland International
-
- // lsqllive.c
- #include "snipit.h"
-
- // Name of table to be created.
- static const char szTblName[] = "cust";
- // Table type to use.
- static const char szTblType[] = szDBASE;
-
- //=====================================================================
- // Function:
- // LiveSQL();
- //
- // Description:
- // This example shows how to do live local SQL on a dBASE table.
- //=====================================================================
- void
- LiveSQL (void)
- {
- hDBIDb hDb; // Handle to the database
- hDBICur hCur; // Handle to the answer table
- hDBIStmt hStmt; // Handle to the SQL statement
- hDBICur hMCur; // Handle to the master table.
- CHAR szQry[] = { // The text of the SQL statement
- "SELECT cust_no, name, city, phone, country \r\n"
- "FROM \"cust.dbf\" \r\n"
- "WHERE cust_no>4000"
- };
- BOOL bLive = TRUE;
- CURProps TblProps; // Table descriptor
- CURProps TblMProps; // Table descriptor
- pBYTE pRecBuf; // Pointer to the record buffer
- pBYTE pMRecBuf; // Pointer to the record buffer
- BYTE szCity[20];
- BOOL bEmpty;
- DFLOAT ulCustNum = 0.0;
- BYTE szNewCity[]="Chicago";
- DBIResult rslt;
-
- Screen("*** Live Answer Table Example ***\r\n");
-
- BREAK_IN_DEBUGGER();
-
- Screen(" Initializing IDAPI...");
- if (InitAndConnect(&hDb) != DBIERR_NONE)
- {
- Screen("\r\n*** End of Example ***");
- return;
- }
-
- Screen(" Setting the database directory...");
- rslt = DbiSetDirectory(hDb, (pCHAR) szTblDirectory);
- ChkRslt(rslt, "SetDirectory");
-
- Screen(" Perform the following SQL statement on the table:\r\n");
- Screen(szQry);
-
- Screen("\r\n Allocate the statement handle...");
- rslt = DbiQAlloc(hDb, qrylangSQL, &hStmt);
- if (ChkRslt(rslt, "QAlloc") != DBIERR_NONE)
- {
- CloseDbAndExit(&hDb);
- Screen("\r\n*** End of Example ***");
- return;
- }
-
-
- Screen(" Prepare the SQL statement...");
- rslt = DbiQPrepare(hStmt, szQry);
- if (ChkRslt(rslt, "QPrepare") != DBIERR_NONE)
- {
- CloseDbAndExit(&hDb);
- Screen("\r\n*** End of Example ***");
- return;
- }
-
- // Check if the statement cursor is live.
- if (bLive != wantLIVE)
- {
- // Now set the statement cursor to be live.
- rslt = DbiSetProp((hDBIObj) hStmt, stmtLIVENESS, wantLIVE);
- ChkRslt(rslt, "SetProp");
- }
-
- Screen(" Execute the SQL statement...");
- rslt = DbiQExec(hStmt, &hCur);
- if (ChkRslt(rslt, "QExec") != DBIERR_NONE)
- {
- CloseDbAndExit(&hDb);
- Screen("\r\n*** End of Example ***");
- return;
- }
-
- // Check for a valid cursor
- if (hCur)
- {
- rslt = DbiGetCursorProps(hCur, &TblProps);
- ChkRslt(rslt, "GetCursorProps");
-
- // Check if the answer set is live (exit example if not)....
- if (TblProps.bTempTable == TRUE)
- {
- Screen("\r\n Could not get a live answer set...");
- rslt = DbiCloseCursor(&hCur);
- ChkRslt(rslt, "CloseCursor");
- CloseDbAndExit(&hDb);
- Screen("\r\n*** End of Example ***");
- return;
- }
-
- Screen(" Opening the master table...");
- rslt = DbiOpenTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType,
- NULL, NULL, 0, dbiREADWRITE, dbiOPENSHARED,
- xltFIELD, FALSE, NULL, &hMCur);
- if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
- {
- rslt = DbiCloseCursor(&hCur);
- ChkRslt(rslt, "CloseCursor");
- CloseDbAndExit(&hDb);
- Screen("\r\n*** End of Example ***");
- return;
- }
-
- // Get the table properties so we can allocate memory for the record
- // buffer.
- rslt = DbiGetCursorProps(hMCur, &TblMProps);
- ChkRslt(rslt, "GetCursorProps");
-
- // Allocate memory for the record buffer.
- pMRecBuf = (pBYTE) malloc(TblMProps.iRecBufSize);
- if (!pMRecBuf)
- {
- rslt = DbiCloseCursor(&hCur);
- ChkRslt(rslt, "CloseCursor");
- rslt = DbiCloseCursor(&hMCur);
- ChkRslt(rslt, "CloseCursor");
- CloseDbAndExit(&hDb);
- Screen("\r\n*** End of Example ***");
- return;
- }
-
- Screen("\r\n Switching to an index in the master table... ");
- rslt = DbiSwitchToIndex(&hMCur, "CUST.MDX", "CUST_NO", NULL, FALSE);
- ChkRslt(rslt, "SwitchToIndex");
-
- Screen(" Clear the record buffer...");
- rslt = DbiInitRecord(hMCur, pMRecBuf);
- ChkRslt(rslt, "InitRecord");
-
- Screen(" Display the first record in the answer table...");
- rslt = DbiSetToBegin(hCur);
- ChkRslt(rslt, "SetToBegin");
- DisplayNextRecord(hCur);
-
- // Modify the table and change back to the original
- // values.
-
- // Allocate memory for the record buffer.
- pRecBuf = (pBYTE) malloc(TblProps.iRecBufSize);
- if (!pRecBuf)
- {
- free(pMRecBuf);
- rslt = DbiCloseCursor(&hCur);
- ChkRslt(rslt, "CloseCursor");
- rslt = DbiCloseCursor(&hMCur);
- ChkRslt(rslt, "CloseCursor");
- CloseDbAndExit(&hDb);
- Screen("\r\n*** End of Example ***");
- return;
- }
-
- // Get the first record in the live query.
- rslt = DbiSetToBegin(hCur);
- ChkRslt(rslt, "SetToBegin");
-
- rslt = DbiGetNextRecord(hCur, dbiNOLOCK, pRecBuf, NULL);
- ChkRslt(rslt, "GetNextRecord");
-
- // Get the master table's fourth field (which is the 3rd field for
- // the answer table) value so that it can be restored after
- // modifying the table.
- rslt = DbiGetField(hCur, 3, pRecBuf, szCity, &bEmpty);
- ChkRslt(rslt, "GetField");
-
- rslt = DbiGetField(hCur, 1, pRecBuf, (pBYTE) &ulCustNum, &bEmpty);
- ChkRslt(rslt, "GetField");
-
- // Modify the City of the first record.
- rslt = DbiPutField(hCur, 3, pRecBuf, szNewCity);
- ChkRslt(rslt, "PutField");
-
- Screen("\r\n Modifying the City field in the first record "
- "of the DynaSet from %s to %s....", szCity, szNewCity);
-
- rslt = DbiModifyRecord(hCur, pRecBuf, TRUE);
- ChkRslt(rslt, "ModifyRecord");
-
- Screen(" Re-displaying the modified record of the master table "
- "to show the change took place...");
-
- // Set the value for the search into the record buffer.
- rslt = DbiPutField(hMCur, 1, pMRecBuf, (pBYTE)&ulCustNum);
- ChkRslt(rslt, "PutField");
-
- // Set the cursor to the field in the master table - to verify
- // that it was modified.
- rslt = DbiSetToKey(hMCur, keySEARCHEQ, FALSE, NULL, NULL, pMRecBuf);
- ChkRslt(rslt, "SetToKey");
-
- DisplayNextRecord(hMCur);
-
- rslt = DbiSetToBegin(hCur);
- ChkRslt(rslt, "SetToBegin");
-
- // Change the data back to the way it was.
- rslt = DbiGetNextRecord(hCur, dbiNOLOCK, pRecBuf, NULL);
- ChkRslt(rslt, "GetRecord");
-
- rslt = DbiPutField(hCur, 3, pRecBuf, szCity);
- ChkRslt(rslt, "PutField");
-
- Screen("\r\n Saving back the original City name to keep the "
- "example table consistent...");
- rslt = DbiModifyRecord(hCur, pRecBuf, TRUE);
- ChkRslt(rslt, "ModifyRecord");
-
- Screen("\r\n Close the cursor to the answer set...");
- rslt = DbiCloseCursor(&hCur);
- ChkRslt(rslt, "CloseCursor");
-
- Screen("\r\n Close the cursor to the master table...");
- rslt = DbiCloseCursor(&hMCur);
- ChkRslt(rslt, "CloseCursor");
-
- free(pRecBuf);
- free(pMRecBuf);
- }
- else
- {
- Screen(" Could not get cursor to the answer table");
- }
-
- Screen(" Release memory allocated for the query...");
- rslt = DbiQFree(&hStmt);
- ChkRslt(rslt, "QryFree");
-
- Screen(" Close the database and exit IDAPI...");
- CloseDbAndExit(&hDb);
-
- Screen("\r\n*** End of Example ***");
- }
-
-