The project gives a new demand, using trigger to generate log table, listen to the database of key table additions and deletions and change operations. Writes the data of the operation into JSON format as a CLOB into Oracle.
1 Create or Replace procedureLogging_messages (tableNamevarchar2 default '%') is2 cursorC_cl is3 SELECTcolumn_name TCL4 fromUser_tab_columns5 WHEREtable_name= Upper(tableName);6 Newjson Clob;7 Oldjson Clob;8V_sqlvarchar2(32000);9V_tmpvarchar2(32000);Ten begin One forClinchC_CL LOOP A ifNewjson is NULL Then -Newjson:= '{"' ||Cl.tcl|| '":""'||'||': NEW.' ||Cl.tcl||'||"'"}'; - Else theNewjson:=Newjson|| ',' || '{"' ||Cl.tcl|| '":""'||'||': NEW.' ||Cl.tcl||'||"'"}'; - End if; - ifOldjson is NULL Then -Oldjson:= '{"' ||Cl.tcl|| '":""'||'||': Old.' ||Cl.tcl||'||"'"}'; + Else -Oldjson:=Oldjson|| ',' || '{"' ||Cl.tcl|| '":""'||'||': Old.' ||Cl.tcl||'||"'"}'; + End if; A EndLOOP; atDbms_output.put_line ('Newjson:'||Newjson); -Dbms_output.put_line ('Oldjson:'||Oldjson); -V_tmp:=Q'{CREATE OR REPLACE TRIGGER [email protected][email protected] - before INSERT or UPDATE or DELETE on @[email protected] for each ROW - DECLARE - JSON Clob; in BEGIN - IF INSERTING Then to JSON: ='@newjson@'; + Dbms_output.put_line ('The JSON string is insert:'|| JSON); - INSERT into ddllog values ('@TABLE_NAME@','Insert', json,sysdate); the End If; * $ IF DELETING ThenPanax Notoginseng JSON: ='@oldjson@'; - Dbms_output.put_line ('The JSON string is delete:'|| JSON); the INSERT into ddllog values ('@TABLE_NAME@','Delete', json,sysdate); + End If; A the IF UPDATING Then + JSON: ='@newjson@'; - Dbms_output.put_line ('The JSON string is update:'|| JSON); $ INSERT into ddllog values ('@TABLE_NAME@','Update', json,sysdate); $ End If; - - END [email protected][email protected];}'; theV_tmp:= REPLACE(V_tmp,'@[email protected]', tableName); -V_tmp:= REPLACE(V_tmp,'@[email protected]', Newjson);WuyiV_sql:= REPLACE(V_tmp,'@[email protected]', Oldjson); the Dbms_output.put_line (v_sql); - EXECUTEIMMEDIATE V_sql; Wu EndLogging_messages;
Establishes a listener trigger for the specified table, with the following results:
Realized the expected function, there is still a lot of perfect space. But the level is lower, slowly to change it.
The stored procedure gives the specified table binding trigger SQL to spell JSON as the storage log