copyright Ó oracle corporation, 1999. all rights reserved. 1717 query triggers

20
Copyright Oracle Corporation, 1999. All rights reserved. 17 17 Query Triggers

Upload: bertina-daniel

Post on 14-Jan-2016

213 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Copyright Ó Oracle Corporation, 1999. All rights reserved. 1717 Query Triggers

Copyright Oracle Corporation, 1999. All rights reserved.

17171717

Query TriggersQuery Triggers

Page 2: Copyright Ó Oracle Corporation, 1999. All rights reserved. 1717 Query Triggers

Copyright Oracle Corporation, 1999. All rights reserved. 17-17-22

ObjectivesObjectives

After completing this lesson, you should be able to do the following:

• Explain the processes involved in querying a data block

• Describe query triggers and their scope

• Write triggers Write triggers to supplement query results and scr screen query conditionseen query conditions

• Control trigger action based on the form query status

After completing this lesson, you should be able to do the following:

• Explain the processes involved in querying a data block

• Describe query triggers and their scope

• Write triggers Write triggers to supplement query results and scr screen query conditionseen query conditions

• Control trigger action based on the form query status

Page 3: Copyright Ó Oracle Corporation, 1999. All rights reserved. 1717 Query Triggers

Copyright Oracle Corporation, 1999. All rights reserved. 17-17-33

Construct SELECT...Construct SELECT...

Perform queryPerform query

Fetch a row into a new recordFetch a row into a new record

Mark record as validMark record as valid

Fire Post-Query triggerFire Post-Query trigger

Validate any record changesValidate any record changes

Abort queryAbort queryon failureon failure

Query ProcessingQuery Processing

FlushFlushrecordrecord

on failureon failure

Fire Pre-Query triggerFire Pre-Query trigger

Page 4: Copyright Ó Oracle Corporation, 1999. All rights reserved. 1717 Query Triggers

Copyright Oracle Corporation, 1999. All rights reserved. 17-17-44

SELECT Statements IssuedSELECT Statements Issued

SELECT base_column, ..., ROWID

INTO :base_item, ..., :ROWID

FROM base_table

WHERE default_where_clause

AND (example_record_conditions)

AND (query_where_conditions)

ORDER BY default_order_by_clause |

query_where_order_by

SELECT base_column, ..., ROWID

INTO :base_item, ..., :ROWID

FROM base_table

WHERE default_where_clause

AND (example_record_conditions)

AND (query_where_conditions)

ORDER BY default_order_by_clause |

query_where_order_by

Slightly different for COUNTSlightly different for COUNT

Page 5: Copyright Ó Oracle Corporation, 1999. All rights reserved. 1717 Query Triggers

Copyright Oracle Corporation, 1999. All rights reserved. 17-17-55

WHERE ClauseWHERE Clause

• Three sources for the WHERE clause:

– WHERE clause block property

– Example Record

– Query/Where dialog box

• WHERE clauses are combined by the AND operator

• Three sources for the WHERE clause:

– WHERE clause block property

– Example Record

– Query/Where dialog box

• WHERE clauses are combined by the AND operator

Page 6: Copyright Ó Oracle Corporation, 1999. All rights reserved. 1717 Query Triggers

Copyright Oracle Corporation, 1999. All rights reserved. 17-17-66

ORDER BY ClauseORDER BY Clause

• Two sources for the ORDER BY clause:

– ORDER BY clause block property

– Query/Where dialog box

• Second source for ORDER BY clause overrides the first one

• Two sources for the ORDER BY clause:

– ORDER BY clause block property

– Query/Where dialog box

• Second source for ORDER BY clause overrides the first one

Page 7: Copyright Ó Oracle Corporation, 1999. All rights reserved. 1717 Query Triggers

Copyright Oracle Corporation, 1999. All rights reserved. 17-17-77

Pre-Query TriggerPre-Query Trigger

IF TO_CHAR(:S_ORD.ID)||

TO_CHAR(:S_ORD.DATE_ORDERED)||

TO_CHAR(:S_ORD.DATE_SHIPPED)

IS NULL THEN

MESSAGE(’You must query by

Order ID or Date’);

RAISE form_trigger_failure;

END IF;

IF TO_CHAR(:S_ORD.ID)||

TO_CHAR(:S_ORD.DATE_ORDERED)||

TO_CHAR(:S_ORD.DATE_SHIPPED)

IS NULL THEN

MESSAGE(’You must query by

Order ID or Date’);

RAISE form_trigger_failure;

END IF;

• Defined at block level

• Fires once, before query is performed

• Defined at block level

• Fires once, before query is performed

Page 8: Copyright Ó Oracle Corporation, 1999. All rights reserved. 1717 Query Triggers

Copyright Oracle Corporation, 1999. All rights reserved. 17-17-88

Post-Query TriggerPost-Query Trigger

SELECT COUNT(ord_id)

INTO :S_ORD.lineitem_count

FROM S_ITEM

WHERE ord_id = :S_ORD.id;

SELECT COUNT(ord_id)

INTO :S_ORD.lineitem_count

FROM S_ITEM

WHERE ord_id = :S_ORD.id;

• Fires for each fetched record (except during array processing)

• Use to populate nondatabase items and calculate statistics

• Fires for each fetched record (except during array processing)

• Use to populate nondatabase items and calculate statistics

Page 9: Copyright Ó Oracle Corporation, 1999. All rights reserved. 1717 Query Triggers

Copyright Oracle Corporation, 1999. All rights reserved. 17-17-99

Using SELECT Statements in TriggersUsing SELECT Statements in Triggers

• Form Builder variables are preceded by a colon.

• The query must return one row for success.

• Code exception handlers.

• The INTO clause is mandatory, with a variable for each selected column or expression.

• ORDER BY is not relevant.

• Form Builder variables are preceded by a colon.

• The query must return one row for success.

• Code exception handlers.

• The INTO clause is mandatory, with a variable for each selected column or expression.

• ORDER BY is not relevant.

Page 10: Copyright Ó Oracle Corporation, 1999. All rights reserved. 1717 Query Triggers

Copyright Oracle Corporation, 1999. All rights reserved. 17-17-1010

Query Array ProcessingQuery Array Processing

• Reduces network traffic

• Enables Query Array processing:

– Enable Array Processing option

– Set Query Array Size property

• Query Array Size property

• Query All Records property

• Reduces network traffic

• Enables Query Array processing:

– Enable Array Processing option

– Set Query Array Size property

• Query Array Size property

• Query All Records property

Page 11: Copyright Ó Oracle Corporation, 1999. All rights reserved. 1717 Query Triggers

Copyright Oracle Corporation, 1999. All rights reserved. 17-17-1111

Coding for ENTER-QUERY Mode

Coding for ENTER-QUERY Mode

• Some triggers may fire in Enter-Query mode.

• Set to fire in Enter-Query Mode property.

• Test mode during execution with :SYSTEM.MODE

– NORMAL

– ENTER-QUERY

– QUERY

• Some triggers may fire in Enter-Query mode.

• Set to fire in Enter-Query Mode property.

• Test mode during execution with :SYSTEM.MODE

– NORMAL

– ENTER-QUERY

– QUERY

Page 12: Copyright Ó Oracle Corporation, 1999. All rights reserved. 1717 Query Triggers

Copyright Oracle Corporation, 1999. All rights reserved. 17-17-1212

Coding for ENTER-QUERY Mode

Coding for ENTER-QUERY Mode

• Example

• Some built-ins are illegal.

• Consult online Help.

• You cannot navigate to another record in the current form.

• Example

• Some built-ins are illegal.

• Consult online Help.

• You cannot navigate to another record in the current form.

IF :SYSTEM.MODE = ’NORMAL’

THEN ENTER_QUERY;

ELSE EXECUTE_QUERY;

END IF;

IF :SYSTEM.MODE = ’NORMAL’

THEN ENTER_QUERY;

ELSE EXECUTE_QUERY;

END IF;

Page 13: Copyright Ó Oracle Corporation, 1999. All rights reserved. 1717 Query Triggers

Copyright Oracle Corporation, 1999. All rights reserved. 17-17-1313

Overriding Default Query ProcessingOverriding Default Query Processing

Do-the-Right-Thing Built-in

COUNT_QUERY

FETCH_RECORDS

SELECT_RECORDS

Trigger

On-Close

On-Count

On-Fetch

Pre-Select

On-Select

Post-Select

Page 14: Copyright Ó Oracle Corporation, 1999. All rights reserved. 1717 Query Triggers

Copyright Oracle Corporation, 1999. All rights reserved. 17-17-1414

Overriding Default Query ProcessingOverriding Default Query Processing

• On-Fetch continues to fire until:

– It fires without executing CREATE_QUERIED_RECORD.

– The query is closed by the user or by ABORT_QUERY.

– It raises FORM_TRIGGER_FAILURE.

• On-Select replaces open cursor, parse, and execute phases.

• On-Fetch continues to fire until:

– It fires without executing CREATE_QUERIED_RECORD.

– The query is closed by the user or by ABORT_QUERY.

– It raises FORM_TRIGGER_FAILURE.

• On-Select replaces open cursor, parse, and execute phases.

Page 15: Copyright Ó Oracle Corporation, 1999. All rights reserved. 1717 Query Triggers

Copyright Oracle Corporation, 1999. All rights reserved. 17-17-1515

Obtaining Query Information at Run TimeObtaining Query Information at Run Time

• SYSTEM.MODE

• SYSTEM.LAST_QUERY

– Contains bind variables (ORD_ID = :1) before SELECT_RECORDS

– Contains actual values (ORD_ID = 102) after SELECT_RECORDS

• SYSTEM.MODE

• SYSTEM.LAST_QUERY

– Contains bind variables (ORD_ID = :1) before SELECT_RECORDS

– Contains actual values (ORD_ID = 102) after SELECT_RECORDS

Page 16: Copyright Ó Oracle Corporation, 1999. All rights reserved. 1717 Query Triggers

Copyright Oracle Corporation, 1999. All rights reserved. 17-17-1616

Obtaining Query Information at Run TimeObtaining Query Information at Run Time

• GET_BLOCK_PROPERTY SET_BLOCK_PROPERTY

– Get and set:DEFAULT_WHEREORDER_BYQUERY_ALLOWEDQUERY_HITS

– Get only:QUERY_OPTIONSRECORDS_TO_FETCH

• GET_BLOCK_PROPERTY SET_BLOCK_PROPERTY

– Get and set:DEFAULT_WHEREORDER_BYQUERY_ALLOWEDQUERY_HITS

– Get only:QUERY_OPTIONSRECORDS_TO_FETCH

Page 17: Copyright Ó Oracle Corporation, 1999. All rights reserved. 1717 Query Triggers

Copyright Oracle Corporation, 1999. All rights reserved. 17-17-1717

Obtaining Query Information at Run TimeObtaining Query Information at Run Time

• GET_ITEM_PROPERTY SET_ITEM_PROPERTY

– Get and set:CASE_INSENSITIVE_QUERYQUERYABLEQUERY_ONLY

– Get only:QUERY_LENGTH

• GET_ITEM_PROPERTY SET_ITEM_PROPERTY

– Get and set:CASE_INSENSITIVE_QUERYQUERYABLEQUERY_ONLY

– Get only:QUERY_LENGTH

Page 18: Copyright Ó Oracle Corporation, 1999. All rights reserved. 1717 Query Triggers

Copyright Oracle Corporation, 1999. All rights reserved. 17-17-1818

SummarySummary

• A Pre-Query trigger fires before a query executes. Use it to check or modify query conditions.

• A Post-Query trigger fires as each record is fetched (except array processing). Use it to perform calculations and populate additional items.

• A Pre-Query trigger fires before a query executes. Use it to check or modify query conditions.

• A Post-Query trigger fires as each record is fetched (except array processing). Use it to perform calculations and populate additional items.

Page 19: Copyright Ó Oracle Corporation, 1999. All rights reserved. 1717 Query Triggers

Copyright Oracle Corporation, 1999. All rights reserved. 17-17-1919

SummarySummary

• Some triggers can fire in both Normal and Enter Query modes:

– Test the current mode with SYSTEM.MODE.

– Some built-ins are illegal in Enter Query mode.

• Obtain query information at run time:

– SYSTEM.MODE

– SYSTEM.LAST_QUERY

• Some triggers can fire in both Normal and Enter Query modes:

– Test the current mode with SYSTEM.MODE.

– Some built-ins are illegal in Enter Query mode.

• Obtain query information at run time:

– SYSTEM.MODE

– SYSTEM.LAST_QUERY

Page 20: Copyright Ó Oracle Corporation, 1999. All rights reserved. 1717 Query Triggers

Copyright Oracle Corporation, 1999. All rights reserved. 17-17-2020

Practice 17 OverviewPractice 17 Overview

This practice covers the following topics:

• Populating customer names and sales representative names for each row of the S_ORD block

• Populating descriptions for each row of the S_ITEM block

• Disabling the effect of the Exit button in Enter Query mode

• Adding two check boxes to enable case-sensitive and exact match query

This practice covers the following topics:

• Populating customer names and sales representative names for each row of the S_ORD block

• Populating descriptions for each row of the S_ITEM block

• Disabling the effect of the Exit button in Enter Query mode

• Adding two check boxes to enable case-sensitive and exact match query