fhg
Volume 13, Number 5 -- March 6, 2013

When Who Did What

Published: March 6, 2013

by Ted Holt

When you create a new database table (physical file), you should include those columns (fields) that are fully functionally dependent on the key fields. I would like to mention some additional columns that you might like to include also.

The columns to which I refer are ones that store creation and change data. For example, you may want to be able to tell who inserted a row (added a record) and who last changed it. Such information may allow you to give a quick answer to a user's conundrum.

Look at this simple CREATE TABLE statement. Notice the last six columns.

create table MyFile
  (Key                    char (24  ) not null,
   OneField               char ( 1  ),
   TwoField               char (10  ),
   RedField               dec  ( 7,2),
   BlueField              dec  ( 5,0),
   CreateUser             char ( 10 ),
   CreateTime        timestamp,
   CreateJob              char ( 10 ),
   ChangeUser             char ( 10 ),
   ChangeTime        timestamp,
   ChangeJob              char ( 10),
   Primary key (Key));

The "create" columns track who created a row, when they created it, and the name of the job from which they created it. The job name is probably most helpful in terminal-based applications. The "change" columns report the same information for the last update to the row.

SQL can populate the user profile and timestamp columns, but as far as I know, has no function or special value that returns the job name. You can create a simple SQL function to retrieve that information.

First, you need a module that returns the job name.

H nomain
H option(*srcstmt)

D*** *entry plist
D RtvJobName      pr            10a

D psds           sds                  qualified
D  JobName              244    253a

P RtvJobName      b                   export
D                 pi            10a
 /free
     return psds.JobName;
 /end-free
P                 e

Create the module.

CRTRPGMOD MODULE(RTVJOBNAME) SRCFILE(QRPGLESRC) 
SRCMBR(RTVJOBNAME)

Create a service program from the module.

CRTSRVPGM SRVPGM(RTVJOBNAME) MODULE(RTVJOBNAME) 
EXPORT(*ALL)

Create an SQL function from the service program.

create function RtvJobName ()
   returns char(10)
   language rpgle
   parameter style general
   not deterministic
   no sql
   no external action
   not fenced
   no final call
   allow parallel
   no scratchpad
   external name 'MYLIB/RTVJOBNAME(RTVJOBNAME)'

It would be a mistake to try to make all applications update the creation and change columns. Instead, use two simple triggers. An insert trigger:

create trigger MyFileIns
   before insert on MyFile
   referencing new row as NewRow
   for each row mode db2row
      set NewRow.CreateUser = User,
          NewRow.CreateTime = Current_timestamp,
          NewRow.CreateJob  = RtvJobName(),
          NewRow.ChangeUser = User,
          NewRow.ChangeTime = Current_timestamp,
          NewRow.ChangeJob  = RtvJobName();

And a change trigger:

create trigger MyFileChg
   before update on MyFile
   referencing new row as NewRow
   for each row mode db2row
      set NewRow.ChangeUser = User,
          NewRow.ChangeTime = Current_timestamp,
          NewRow.ChangeJob  = RtvJobName();

Your applications won't need to load those columns. In fact, it will be useless for the applications to do so, because the triggers will overwrite those columns. For that reason, you may want to create views and/or logical files that omit the creation and change columns.

create view MyFile01 as
   select Key, OneField, TwoField, RedField, BlueField
     from MyFile

Some of your inquiry and report programs may need the creation and change columns. But any that do not need them, as well as programs that manipulate the database table, can work through the views and logical files.

Will these columns provide all answers when users want to know who did what and when? Of course, not. For that, you need something more powerful, such as Steven Wolk's excellent Display Journal utility, which is also found in this issue of Four Hundred Guru. But in many cases, these columns are sufficient.


RELATED STORY

Journal Forensics 101



                     Post this story to del.icio.us
               Post this story to Digg
    Post this story to Slashdot


Sponsored By
SEQUEL SOFTWARE

Advanced Query/400: Learn the Secrets

 

When it comes to Query/400, "advanced"
is in the eye of the beholder.

 

While it is true that the tool can handle functions
beyond static queries, the dependent variable functions we
detail in this guide represent the limits of Query/400's
ability to perform more in-depth tasks.

 

However, many shops still rely on basic query tools,
for better or worse, and we want to help you make the most of them.

 

Download the free Advanced Query/400 Pack.


Senior Technical Editor: Ted Holt
Technical Editor: Joe Hertvik
Contributing Technical Editors: Edwin Earley, Brian Kelly, Michael Sansoterra
Publisher and Advertising Director: Jenny Thomas
Advertising Sales Representative: Kim Reed
Contact the Editors: To contact anyone on the IT Jungle Team
Go to our contacts page and send us a message.

Sponsored Links

Profound Logic Software:  Live Webinar: 4 RPG Keys to Application Modernization Success. March 20
BCD:  IBM i Webinar - March 7. Jumpstart Web App Development with WebSmart Mobile
looksoftware:  Free Live Webcast: HTML5 & IBM i - Connect, Discover, Create. March 12 & 13


 

More IT Jungle Resources:

System i PTF Guide: Weekly PTF Updates
IBM i Events Calendar: National Conferences, Local Events, and Webinars
Breaking News: News Hot Off The Press
TPM @ The Reg: More News From ITJ EIC Timothy Prickett Morgan


 
The Four Hundred
Entry Power7+ Servers: Those 720+ and 740+ Boxes Are Gonna Cost Ya

Power7 Is The End Of The Line For Power Blades

Enthusiasm, Persistence, And The IBM i Payoff

Mad Dog 21/21: If You Want Cheap Cloud Backup, Raise Your ARM

The Server Racket Holds Its Own In The Fourth Quarter

Four Hundred Stuff
BCD Waves its Modernization Wand with Presto 4.5

McAfee Works with Raz-Lee to Monitor DB2 for i

Bytware Becomes More Friendly to LPM with Messenger Product

Townsend Adopts KMIP for License Key Interoperability

Spinnaker Nabs Former Rimini Executive

Four Hundred Monitor
Four Hundred Monitor's
Full iSeries Events Calendar

System i PTF Guide
March 2, 2013: Volume 15, Number 9

February 23, 2013: Volume 15, Number 8

February 16, 2013: Volume 15, Number 7

February 9, 2013: Volume 15, Number 6

February 2, 2013: Volume 15, Number 5

January 26, 2013: Volume 15, Number 4

TPM at The Register
IBM builds next-gen cloud control freak on OpenStack

Mellanox to open up its Ethernet networking stack

MapR smashes MinuteSort benchmark on Google Compute

EMC touts screeching Hawq SQL performance for Hadoop

Pricey mainframes, hyperscale boxes boost Q4 server sales

Big Blue to embiggen itself even more on your big data

IBM runs OLTP benchmark atop KVM hypervisor

ARM servers: From li'l Acorns big data center disruptions grow

SGI rejigs financing ahead of possible asset sale

The server racket recovers from the Great Recession
Rackspace swallows ObjectRocket for MongoDB smarts

Revolution weaves predictive analytics into Hortonworks Hadoop

THIS ISSUE SPONSORED BY:

WorksRight Software
SEQUEL Software
Northeast User Groups Conference


Printer Friendly Version


TABLE OF CONTENTS
Journal Forensics 101

When Who Did What

Admin Alert: A Checklist For Moving Power i Partitions To A Different Location

Four Hundred Guru

BACK ISSUES




 
Subscription Information:
You can unsubscribe, change your email address, or sign up for any of IT Jungle's free e-newsletters through our Web site at http://www.itjungle.com/sub/subscribe.html.

Copyright © 1996-2013 Guild Companies, Inc. All Rights Reserved.
Guild Companies, Inc., 50 Park Terrace East, Suite 8F, New York, NY 10034

Privacy Statement