Upload
carlos-oliveira
View
201
Download
0
Embed Size (px)
DESCRIPTION
How to move your Oracle PL/SQL code from an anonymous block in a file to a database stored object. In a simple procedure you achieve huge gains in security, performance, object reuse, and more. With a small enhancement comes a great financial cost reduction and productivity increase.
Citation preview
Oracle PL/SQL RefactoringFrom anonymous block to procedure
Carlos Oliveira / July 23, 2014
Agenda
Code Refactoring – From anonymous block to procedure
Introduction Quote Code Refactoring Benefits Next Steps Training & Reference Thank You
IntroductionI am a forward-looking Information Systems Architect with a solid Oracle DBA background comprising the daily infrastructure tasks of the DBA, several projects as a Data Modeler, and performance management projects.
I Started on the mainframe business, and soon had a deep dive in application development for Oracle databases. After acquiring an Oracle certification, I worked on performance enhancement for applications using Oracle databases, and later worked several years as an infrastructure DBA, later I worked on data modeling projects and more recently a performance management project, on both application and database layers.
“The limits of my language mean the limits of my world.”
Ludwig Wittgenstein
Code RefactoringScript_text_file_01.sql – file contents below
DECLARE /******************************************************** Description: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx Changed: xx/xx/xxxx Changed By: xxxxxxx ********************************************************/ v_msg_text VARCHAR2(4000); v_msg_num NUMBER(10) := 0;BEGIN v_msg_text := 'Procedure executed'; v_msg_num := 1;END;/quit
Code RefactoringCREATE OR REPLACE PROCEDURE DB_USER.PROC_NAME IS/******************************************************** Description: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx Changed: xx/xx/xxxx Changed By: xxxxxxx ********************************************************/ v_msg_text VARCHAR2(4000); v_msg_num NUMBER(10) := 0;BEGIN v_msg_text := 'Procedure executed'; v_msg_num := 1;END;/
Script_text_file_01.sql new BEGIN DB_USER.PROC_NAME;END;/quit
Since the code is now stored in the database, the Script_text_file_01.sql only contains the call to the procedure.
BenefitsCompilation
• Most of the code is already compiled – only caller script remains
Memory• Reduced memory usage• Faster load
Tracking• Code can be tracked inside DB• Explicit dependency – change in related objects are flagged
Reuse• Code is ready to be reused• Change in central stored object is immediate for all calling scripts
Security• Code can be included in database backup/restore• Access to code restricted by database privileges
Next StepsModularization• Identification of common code for grouping, organization or
creation of internal or even external procedures• Encapsulation of complex code in procedures of functions
Error Control• Inclusion of an Exception section for each code block• Use of user-created exceptions• Creation of common exception procedures
Best Practices• Variable initialization• Eliminate hard-code
And much more …
Training
• Performance Tuning Guide and Reference http://docs.oracle.com/cd/B10500_01/server.920/a96533/toc.htm
• SQL Reference http://docs.oracle.com/cd/B10500_01/server.920/a96540/toc.htm
• PL/SQL User's Guide and Reference http://
docs.oracle.com/cd/B10500_01/appdev.920/a96624/toc.htm
Resources at Oracle website
Thank you
Carlos Oliveira / July 23, 2014