Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
40
Добавлен:
16.04.2013
Размер:
4.96 Mб
Скачать

10

Working With System Events

System events, like LOGON and SHUTDOWN, provide a mechanism for tracking system changes. Oracle Database lets you combine this tracking with database event notification, which provides a simple and elegant method of delivering asynchronous messaging to an application.

This chapter includes descriptions of the various events on which triggers can be created. It also provides the list of event attribute functions. Topics include the following:

Event Attribute Functions

List of Database Events

See Also:

Chapter 9, "Using Triggers" for background information useful for understanding the information in this chapter. You access event information inside a trigger body.

Oracle 2 Day DBA for information on Oracle Enterprise Manager. You can set up a flexible system for handling events using the Event panels in Enterprise Manager. This technique lets you detect more conditions than the system events in this chapter, and it lets you set up actions such as invoking shell scripts.

Working With System Events 10-1

Event Attribute Functions

Event Attribute Functions

When a trigger is fired, you can retrieve certain attributes about the event that fired the trigger. Each attribute is retrieved by a function call. Table 10–1 describes the system-defined event attributes.

Note:

To make these attributes available, you must first run the

CATPROC.SQL script.

The trigger dictionary object maintains metadata about events that will be published and their corresponding attributes.

In earlier releases, these functions were accessed through the SYS package. We recommend you use these public synonyms whose names begin with ora_.

Table 10–1

System-Defined Event Attributes

 

 

 

 

 

 

 

Attribute

 

Type

Description

Example

 

 

 

 

ora_client_ip_address

VARCHAR2

Returns the IP

if (ora_sysevent = 'LOGON')

 

 

 

address of the

then addr := ora_client_ip_

 

 

 

client in a

address;

 

 

 

LOGON event,

end if;

 

 

 

when the

 

 

 

 

 

 

 

underlying

 

 

 

 

protocol is

 

 

 

 

TCP/IP

 

 

 

 

 

ora_database_name

VARCHAR2(50)

Database name.

DECLARE

 

 

 

 

db_name VARCHAR2(50);

 

 

 

 

BEGIN

 

 

 

 

db_name := ora_database_name;

 

 

 

 

END;

 

 

 

 

ora_des_encrypted_password

VARCHAR2

The DES

IF (ora_dict_obj_type = 'USER')

 

 

 

encrypted

THEN INSERT INTO event_table

 

 

 

password of the

(ora_des_encrypted_password);

 

 

 

user being

END IF;

 

 

 

created or

 

 

 

 

 

 

 

altered.

 

 

 

 

 

 

10-2 Oracle Database Application Developer's Guide - Fundamentals

 

 

 

Event Attribute Functions

 

 

 

 

 

Table 10–1 (Cont.) System-Defined Event Attributes

 

 

 

 

 

 

 

Attribute

Type

Description

Example

 

 

 

 

 

 

ora_dict_obj_name

VARCHAR(30)

Name of the

INSERT INTO event_table

 

 

 

dictionary object

('Changed object is ' || ora_

 

 

 

on which the

dict_obj_name');

 

 

 

DDL operation

 

 

 

 

occurred.

 

 

 

 

 

 

 

ora_dict_obj_name_list

BINARY_INTEGER

Return the list of

if (ora_sysevent = 'ASSOCIATE

 

(name_list OUT ora_name_

 

object names of

STATISTICS')

 

list_t)

 

objects being

then number_modified := ora_

 

 

 

modified in the

dict_obj_name_list (name_list);

 

 

 

event.

 

 

 

end if;

 

 

 

 

 

 

 

 

 

 

ora_dict_obj_owner

VARCHAR(30)

Owner of the

INSERT INTO event_table ('object

 

 

 

dictionary object

owner is' || ora_dict_obj_

 

 

 

on which the

owner');

 

 

 

DDL operation

 

 

 

 

occurred.

 

 

 

 

 

 

 

ora_dict_obj_owner_

BINARY_INTEGER

Returns the list

if (ora_sysevent = 'ASSOCIATE

 

list(owner_list OUT ora_

 

of object owners

STATISTICS')

 

name_list_t)

 

of objects being

then number_of_modified_

 

 

 

modified in the

objects := ora_dict_obj_owner_

 

 

 

event.

 

 

 

list(owner_list);

 

 

 

 

 

 

 

 

end if;

 

 

 

 

 

 

ora_dict_obj_type

VARCHAR(20)

Type of the

INSERT INTO event_table ('This

 

 

 

dictionary object

object is a ' || ora_dict_obj_

 

 

 

on which the

type);

 

 

 

DDL operation

 

 

 

 

occurred.

 

 

 

 

 

 

 

ora_grantee(

BINARY_INTEGER

Returns the

if (ora_sysevent = 'GRANT') then

 

user_list

 

grantees of a

number_of_users := ora_

 

OUT ora_name_list_t)

 

grant event in

grantee(user_list);

 

 

 

the OUT

end if;

 

 

 

parameter;

 

 

 

 

 

 

 

returns the

 

 

 

 

number of

 

 

 

 

grantees in the

 

 

 

 

return value.

 

 

 

 

 

 

 

ora_instance_num

NUMBER

Instance number.

IF (ora_instance_num = 1)

 

 

 

 

THEN INSERT INTO event_table

 

 

 

 

('1');

 

 

 

 

END IF;

 

 

 

 

 

 

Working With System Events 10-3

Event Attribute Functions

Table 10–1 (Cont.) System-Defined Event Attributes

Attribute

Type

Description

Example

 

 

 

 

ora_is_alter_column( column_

BOOLEAN

Returns true if

if (ora_sysevent = 'ALTER' and

name IN VARCHAR2)

 

the specified

ora_dict_obj_type = 'TABLE')

 

 

column is

then alter_column := ora_is_

 

 

altered.

alter_column('FOO');

 

 

 

 

 

 

end if;

 

 

 

 

ora_is_creating_nested_table

BOOLEAN

Returns true if

if (ora_sysevent = 'CREATE' and

 

 

the current event

ora_dict_obj_type = 'TABLE' and

 

 

is creating a

ora_is_creating_nested_table)

 

 

nested table

then insert into event_tab

 

 

 

 

 

 

values ('A nested table is

 

 

 

created');

 

 

 

end if;

 

 

 

 

ora_is_drop_column( column_

BOOLEAN

Returns true if

if (ora_sysevent = 'ALTER' and

name IN VARCHAR2)

 

the specified

ora_dict_obj_type = 'TABLE')

 

 

column is

then drop_column := ora_is_

 

 

dropped.

drop_column('FOO');

 

 

 

 

 

 

end if;

 

 

 

 

ora_is_servererror

BOOLEAN

Returns TRUE if

IF (ora_is_servererror(error_

 

 

given error is on

number))

 

 

error stack,

THEN INSERT INTO event_table

 

 

FALSE

('Server error!!');

 

 

otherwise.

 

 

END IF;

 

 

 

 

 

 

 

ora_login_user

VARCHAR2(30)

Login user name.

SELECT ora_login_user

 

 

 

FROM dual;

 

 

 

 

ora_partition_pos

BINARY_INTEGER

In an INSTEAD

-- Retrieve ora_sql_txt into

 

 

OF trigger for

-- sql_text variable first.

 

 

CREATE TABLE,

 

 

 

the position

n := ora_partition_pos;

 

 

within the SQL

 

 

new_stmt :=

 

 

text where you

 

 

substr(sql_text, 1, n-1) ||

 

 

could insert a

 

 

' ' || my_partition_clause ||

 

 

PARTITION

 

 

' ' || substr(sql_text, n));

 

 

clause.

 

 

 

 

10-4 Oracle Database Application Developer's Guide - Fundamentals

 

 

 

Event Attribute Functions

 

 

 

 

 

Table 10–1 (Cont.) System-Defined Event Attributes

 

 

 

 

 

 

 

Attribute

Type

Description

Example

 

 

 

 

 

 

ora_privilege_list(

BINARY_INTEGER

Returns the list

if (ora_sysevent = 'GRANT' or

 

privilege_list OUT ora_name_

 

of privileges

ora_sysevent = 'REVOKE')

 

list_t)

 

being granted by

then number_of_privileges :=

 

 

 

the grantee or

ora_privilege_list(priv_list);

 

 

 

the list of

 

 

 

end if;

 

 

 

privileges

 

 

 

 

 

 

 

revoked from the

 

 

 

 

revokees in the

 

 

 

 

OUT parameter;

 

 

 

 

returns the

 

 

 

 

number of

 

 

 

 

privileges in the

 

 

 

 

return value.

 

 

 

 

 

 

 

ora_revokee (

BINARY_INTEGER

Returns the

if (ora_sysevent = 'REVOKE')

 

user_list OUT ora_name_list_

 

revokees of a

then

 

t)

 

revoke event in

number_of_users := ora_

 

 

 

the OUT

revokee(user_list);

 

 

 

parameter;

 

 

 

 

 

 

 

returns the

 

 

 

 

number of

 

 

 

 

revokees in the

 

 

 

 

return value.

 

 

 

 

 

 

 

ora_server_error

NUMBER

Given a position

INSERT INTO event_table ('top

 

 

 

(1 for top of

stack error ' || ora_server_

 

 

 

stack), it returns

error(1));

 

 

 

the error number

 

 

 

 

at that position

 

 

 

 

on error stack

 

 

 

 

 

 

 

ora_server_error_depth

BINARY_INTEGER

Returns the total

n := ora_server_error_depth;

 

 

 

number of error

-- This value is used with

 

 

 

messages on the

-- other functions such as

 

 

 

error stack.

-- ora_server_error

 

 

 

 

 

 

 

 

 

 

ora_server_error_msg

VARCHAR2

Given a position

INSERT INTO event_table ('top

 

(position in binary_integer)

 

(1 for top of

stack error message' || ora_

 

 

 

stack), it returns

server_error_msg(1));

 

 

 

the error

 

 

 

 

message at that

 

 

 

 

position on error

 

 

 

 

stack

 

 

 

 

 

 

 

Working With System Events 10-5

Event Attribute Functions

Table 10–1 (Cont.) System-Defined Event Attributes

Attribute

Type

Description

Example

 

 

 

 

ora_server_error_num_params

BINARY_INTEGER

Given a position

n := ora_server_error_num_

(position in binary_integer)

 

(1 for top of

params(1);

 

 

stack), it returns

 

 

 

the number of

 

 

 

strings that have

 

 

 

been substituted

 

 

 

into the error

 

 

 

message using a

 

 

 

format like

 

 

 

"%s".

 

 

 

 

 

ora_server_error_param

VARCHAR2

Given a position

-- E.g. the 2rd %s in a message

(position in binary_integer,

 

(1 for top of

-- like "Expected %s, found %s"

param in binary_integer)

 

stack) and a

param := ora_server_error_

 

 

parameter

param(1,2);

 

 

number, returns

 

 

 

 

 

the matching

 

 

 

"%s", "%d", and

 

 

 

so on

 

 

 

substitution

 

 

 

value in the error

 

 

 

message.

 

 

 

 

 

ora_sql_txt (sql_text out

BINARY_INTEGER

Returns the SQL

sql_text ora_name_list_t;

ora_name_list_t)

 

text of the

stmt VARCHAR2(2000);

 

 

triggering

...

 

 

statement in the

n := ora_sql_txt(sql_text);

 

 

OUT parameter.

 

 

FOR i IN 1..n LOOP

 

 

If the statement

 

 

stmt := stmt || sql_text(i);

 

 

is long, it is

 

 

END LOOP;

 

 

broken up into

 

 

INSERT INTO event_table ('text

 

 

multiple

 

 

PL/SQL table

of triggering statement: ' ||

 

 

elements. The

stmt);

 

 

function return

 

 

 

value specifies

 

 

 

how many

 

 

 

elements are in

 

 

 

the PL/SQL

 

 

 

table.

 

 

 

 

 

10-6 Oracle Database Application Developer's Guide - Fundamentals

Соседние файлы в папке Oracle 10g