DBC BEFOREUPDATE

From Lianjapedia
Jump to: navigation, search

Purpose

Database events are triggered by certain database operations

See Also

ADATABASES(), ALTER TABLE, ADD TABLE, ALTER INDEX, BASE64_DECODE(), BASE64_ENCODE(), CLOSE DATABASES, CLOSE TABLES, COMPILE DATABASE, CREATE DATABASE, CREATE TABLE, CREATE INDEX, CREATE VIEW, DATABASE(), Database Events, DBUSED(), DISPLAY DATABASE, DISPLAY INDEXES, DISPLAY TABLES, DROP DATABASE, DROP INDEX, DROP TABLE, GETENV(), LIST DATABASE, LIST INDEXES, LIST TABLES, OPEN DATABASE, PACK DATABASE, REBUILD DATABASE, REINDEX DATABASE, SET AUTOCATALOG, SET EXCLUSIVE, USE, USERLOG()

Description

The DBC_BEFOREUPDATE database event is triggered prior to committing an update operation when a database is open and runs dbc_beforeupdate.prg (.dbo compiled/runtime) if it exists.

If dbc_beforeupdate returns .F. (false) the insert operation is cancelled.

Programs associated with database events should reside in the database's directory. These can be created and modified from the Events tab in the Data Workspace when a database is open.

The following parameters are passed to the dbc_beforeupdate program:

Parameter Description
<cDatabase> The database name
<cTable> The table name
<cJSON> Base64 encoded JSON data for the operation. See notes below.

<cJSON>

<cJSON> is populated on the Web and Mobile clients. For the Desktop client, where <cJSON> is empty, the current record is accessible and can be queried using standard Lianja/VFP functions (recno(), field(), fldcount() etc.).

If the JSON data exceeds the maximum command line length (16KB), <cJSON> will contain '@' and the data can be accessed using the JSONDATASTRING() function up to maximum string length (64KB).

For the dbc_beforeupdate event, <cJSON> will contain the rowid and the new and old data for the record, e.g.

{"companyname":"Acme UK Ltd.","phone":"0123 456 7890","__rowid":5,
"__olddata":{"companyname":"Acme International Inc.","phone":"(123) 456 7890"}}

Example

//
// Database: dbcdoc
// Event: dbc_beforeupdate
//
parameters cDatabase, cTable, cJSON
userlog("====================================")
userlog("Before Update in "+"dbcdoc database")
userlog("Database: " +cDatabase)
userlog("Table: " +cTable)
 
if ! isserver()
	userlog("Update record number: " + etos(recno())
	for i = 1 to fldcount()
		if type("&(field(i))") = "G"
			loop
		endif		
		userlog("Field: " + field(i))
		userlog("Curval(): " + etos(curval(field(i))))
		userlog("Value: " + etos(&(field(i))))
	endfor
else
	userlog("JSON: " +base64_decode(cJSON)
endif
 
userlog("End of Before Update")
userlog("====================================")
return .t.