home *** CD-ROM | disk | FTP | other *** search
- .Na "dbqual"
- .mc |
- .Aa
- .Fu
- Return a pointer to a WHERE clause suitable for use
- in updating the current row in a browsable table.
- .Ih "WHERE clause, for use in updating a browsable table"
- .Ih "browse mode WHERE clause"
- .Sy
- .Sf "char *dbqual(dbproc, tabnum, tabname)"
- .Sp "DBPROCESS" "*dbproc"
- .Sp "int" "tabnum"
- .Sp "char" "*tabname"
- .Co
- .Bl
- \f2dbqual()\f1 is one of the \*L browse mode routines.
- See the Introduction for a detailed discussion of browse mode.
- .Bl
- \f2dbqual()\f1 provides a
- WHERE clause that the application can use to update a single row in a browsable table.
- Columns from this row must have previously been retrieved into the application
- through a browse-mode SELECT query (\f2i.e.,\f1 a SELECT that ends with the key words
- FOR BROWSE).
- .sp 0.5v
- The WHERE clause produced by \f2dbqual()\f1 begins with the keyword WHERE
- and contains references to the row's unique index and timestamp column.
- .Ih "timestamp, browse mode"
- The application simply
- appends the WHERE clause to an UPDATE or DELETE statement; it does not
- need to examine it or manipulate it in any way.
- .sp 0.5v
- The timestamp column indicates the time that the particular row was last updated.
- An update on a browsable table will fail if the timestamp column in the \f2dbqual()\f1-generated
- WHERE clause is different from the timestamp column in the table.
- Such a condition, which provokes \*S error message 532,
- indicates that another user updated the row between
- the time this application selected it for browsing and the time it tried to update it.
- The application itself must provide the logic for handling the update failure.
- The following program fragment illustrates one approach:
- .SD
- .ta +4n +4n +4n +4n +4n +4n
- /* This code fragment illustrates a technique for handling the case where
- * a browse-mode update fails because the row has already been updated
- * by another user. In this example, we simply retrieve the entire row
- * again, allow the user to examine and modify it, and try the update
- * again.
- *
- * Note that "q_dbproc" is the DBPROCESS used to query the database, and
- * "u_dbproc" is the DBPROCESS used to update the database.
- */
-
- /* First, find out which employee record the user wants to update. */
- employee_id = which_employee();
-
- while (1)
- {
- /* Retrieve that employee record from the database. We'll
- * assume that "empid" is a unique index, so this query will
- * return only one row.
- */
- dbfcmd
- (q_dbproc,
- "select * from employees where empid = %d for browse",
- employee_id);
- dbsqlexec(q_dbproc);
- dbresults(q_dbproc);
- dbnextrow(q_dbproc);
-
- /* Now, let the user examine or edit the employee's
- * data, first placing the data into program variables.
- */
- extract_employee_data(q_dbproc, employee_struct);
- examine_and_edit(employee_struct, &edit_flag);
-
- if (edit_flag == FALSE)
- {
- /* The user didn't edit this record,
- * so we're done.
- */
- break;
- }
- else
- {
- /* The user edited this record, so we'll use the edited
- * data to update the corresponding row in the database.
- */
- qualptr = dbqual(q_dbproc, -1, "employees");
- dbcmd(u_dbproc, "update employees");
- dbfcmd
- (u_dbproc,
- " set address = '%s', salary = %d %s",
- employee_struct->address, employee_struct->salary,
- qualptr);
- dbfreequal(qualptr);
- if ((dbsqlexec(u_dbproc) == FAIL)
- || (dbresults(u_dbproc) == FAIL))
- {
- /* Our update failed. In a real program, it
- * would be necessary to examine the messages
- * returned from the SQL Server to determine
- * why it failed. In this example, we'll
- * assume that the update failed because
- * someone else has already updated this
- * row, thereby changing the timestamp.
- *
- * To cope with this situation, we'll just
- * repeat the loop, retrieving the changed
- * row for our user to examine and edit.
- * This will give our user the opportunity
- * to decide whether to overwrite the change
- * made by the other user.
- */
- continue;
- }
- else
- {
- /* The update succeeded, so we're done. */
- break;
- }
- }
- }
- .ED
- .Bl
- \f2dbqual()\f1 can only construct WHERE clauses for browsable tables.
- You can use \f2dbtabbrowse()\f1 to determine whether a table is browsable.
- .Bl
- \f2dbqual()\f1 is usually called after \f2dbnextrow()\f1.
- .Bl
- For a complete example that uses \f2dbqual()\f1 to perform a browse mode update, see Example 6 in the \f2\*L Reference Supplement\f1.
- .Bz
- .Pa
- .Pi dbproc
- A pointer to the DBPROCESS structure that provides the connection
- for a particular front-end/\*S process. It contains all the
- information that \*L uses to manage communications and data between the
- front end and \*S.
- .Pi tabnum
- The number of the table of interest, as specified in the SELECT statement's FROM clause.
- Table numbers start at 1.
- If \f2tabnum\f1 is -1, the \f2tabname\f1 parameter will be
- used to identify the table.
- .Pi tabname
- A pointer to the null-terminated name of a table specified in the SELECT statement's FROM clause.
- If \f2tabname\f1 is NULL,
- the \f2tabnum\f1 parameter will be used to identify the table.
- .in -.375i
- .Re
- .br
- A pointer to a null-terminated WHERE clause for the current
- row in the specified table. This buffer is dynamically allocated,
- and it is the application's responsibility to free it via \f2dbfreequal()\f1.
- .sp 0.5v
- \f2dbqual()\f1 will return a NULL pointer if the specified table is not browsable.
- For a table to be ``browsable,'' it must have a unique index and a timestamp column.
- .sp 0.5v
- \f2dbqual()\f1 will also return a NULL pointer if the preceding SELECT
- did not include the FOR BROWSE option.
- .Sa
- dbcolbrowse,
- dbcolsource,
- dbfreequal,
- dbtabbrowse,
- dbtabcount,
- dbtabname,
- dbtabsource,
- dbtsnewlen,
- dbtsnewval,
- dbtsput
- .mc
-