Use PL/SQL to automate standards compliance Lewis Cunningham Data Architect and ACE Director
Introduction Oracle ACE Director Author Consultant Data Architect Blogger, An Expert's Guide to Oracle Technology 2
My books
Authors John Beresniewicz, Adrian Billington, Martin Buchi, Melanie Caffery, Ron Crisco, Lewis Cunningham, Dominic Delmolino, Sue Harper, Torben Holm, Connor McDonald, Arup Nanda, Stephan Petit, Michael Rosenblum, Robyn Sands, Riyaj Shamsudeen
Standards and Compliance Standards Coding Standards Naming Standards Performance Standards Testing Standards Compliance Code Reviews Test Scripts and Results
Source Code Analysis Static Analysis What you know at compile time Dynamic Analysis What you learn from running the code Instrumentation What you should make your code tell you
When to analyze 2 AM on the day before vacation? After implementation? After testing? After coding? During Coding? I vote for during coding.
Know Your Code Data Dictionary - Static PL/Scope - Static Profilers - Dynamic Source Code Static Logging - Instrumentation
The Data Dictionary USER_SOURCE ALL_SOURCE DBA_SOURCE Viewable source code Manual Code Reviews
The Data Dictionary USER_DEPENDENCIES ALL_DEPENDENCIES DBA_DEPENDENCIES View dependencies between code and database objects (including other code units) Who calls who
The Data Dictionary USER_PROCEDURES ALL_PROCEDURES DBA_PROCEDURES View information about code (aggregate, deterministic) Identify program units in packages and type bodies
The Data Dictionary USER_ARGUMENTS ALL_ARGUMENTS DBA_ARGUMENTS View argument names and data types Used with user_procedures it is a starting point for automated code reviews Naming Data Types
The Data Dictionary USER_TYPES and USER_TYPE_METHODS ALL_TYPES and ALL_TYPE_METHODS DBA_TYPES and DBA_TYPE_METHODS Combine with USER_PROCEDURES and USER_ARGUMENTS for information about types For reviewing code related to types
The Data Dictionary PL/Scope USER_IDENTIFIERS ALL_IDENTIFIERS DBA_IDENTIFIERS Everything discussed and more Great for Automating Code Reviews
PL/Scope Introduced in 11g Need to turn it on for it to work SQL Developer turns it on for you SQL*Plus: ALTER SESSION SET PLSCOPE_SETTINGS = 'IDENTIFIERS:ALL'; Compile the code
PL/Scope Works with identifiers Almost everything is an identifier Data types and subtypes used (including collections) Cursors and ref cursors Procedures Functions Packages and package bodies Triggers Object types and type bodies Parameters Function return values Variables, iterators, constants Exceptions Synonyms
PL/Scope Data Gathered on every Declaration procedures, variables, etc Definition the implementation of an identifier (such as a procedure within a package) Assignment Reference Calls
PL/Scope Can validate naming standards of variables as well as parameters Identifies scoping issues Change impact analysis Find unused identifiers Validate data types
Dynamic Analysis DBMS_PROFILER Code Profiling Timing between calls DBMS_TRACE Code Call Context Who called who DBMS_HPROF Code profiling and context
Dynamic Analysis Run a baseline profile Save timings Run a new profile after changes are made Can be mostly automated Validate the original timings and only worry if later times go beyond defined criteria
Code Coverage Make sure all executable lines are executed during testing Doesn't validate the logic, just makes sure the line was run Proves that for a certain scenario, the code will execute successfully Does not prove that the code is correct Does not prove that the code will execute correctly for a different data set
Code Coverage Combines Static Analysis with Dynamic Analysis Static analysis (PL/Scope) knows what lines are executable Dynamic Analysis shows what actually is executed Subtract dynamic from static and what s left are those lines of code not executed
Code Coverage Code Coverage is not 100% Doesn't account for all lines of SQL (because PL/Scope doesn't track SQL) I'm hoping for additional information from PL/Scope in the future
Summary Create standards Follow standards Test for standards compliance Automate when possible Be creative with the data dictionary
Buy the book
Use PL/SQL to automate standards compliance Lewis Cunningham Data Architect and ACE Director