Comments
Matt McLarty wrote: For more info... Follow me on Twitter See our website
Cloud Computing
Conference & Expo
November 2-4, 2009 NYC
Register Today and SAVE !..

2008 West
DIAMOND SPONSOR:
Data Direct
SOA, WOA and Cloud Computing: The New Frontier for Data Services
PLATINUM SPONSORS:
Red Hat
The Opening of Virtualization
GOLD SPONSORS:
Appsense
User Environment Management – The Third Layer of the Desktop
Cordys
Cloud Computing for Business Agility
EMC
CMIS: A Multi-Vendor Proposal for a Service-Based Content Management Interoperability Standard
Freedom OSS
Practical SOA” Max Yankelevich
Intel
Architecting an Enterprise Service Router (ESR) – A Cost-Effective Way to Scale SOA Across the Enterprise
Sensedia
Return on Assests: Bringing Visibility to your SOA Strategy
Symantec
Managing Hybrid Endpoint Environments
VMWare
Game-Changing Technology for Enterprise Clouds and Applications
Click For 2008 West
Event Webcasts

2008 West
PLATINUM SPONSORS:
Appcelerator
Get ‘Rich’ Quick: Rapid Prototyping for RIA with ZERO Server Code
Keynote Systems
Designing for and Managing Performance in the New Frontier of Rich Internet Applications
GOLD SPONSORS:
ICEsoft
How Can AJAX Improve Homeland Security?
Isomorphic
Beyond Widgets: What a RIA Platform Should Offer
Oracle
REAs: Rich Enterprise Applications
Click For 2008 Event Webcasts
In many cases, the end of the year gives you time to step back and take stock of the last 12 months. This is when many of us take a hard look at what worked and what did not, complete performance reviews, and formulate plans for the coming year. For me, it is all of those things plus a time when I u...
SYS-CON.TV
Extending SQLCA
The goal is to replace SQLCA with something a little more robust

We are going to create a bare-bones email object. Of course you can expand that in the future if you like, but for now, let's just create the functionality that we need.

I start by creating a custom class. In case you aren't familiar with the process look at Figure 1.

Let's start with an instance variable.

N_cst_mailer Instance Variables
private mailMessage io_mail_message // The message to send

I made the instance variables private so let's write a couple of functions to populate our mail Message. We'll start with the TO. Note that the io_mail_message contains an array of objects called recipient. One of the properties of that object is recipientType. We are looking for the recipientType of mailTo! There are several other types that don't concern us.

N_cst_mailer.of_to
// DESCRIPTION
// Returns the current TO address
int li_count, li_max
li_max = upperBound(io_mail_message.recipient)
for li_count = 1 to li_max
// We are looking for the TO address
if io_mail_message.recipient[li_count].recipientType = mailTo! then
return io_mail_message.recipient[1].name // We are done,let's just return
end if
next
return ""

Now let's do the SET function

N_cs_mailer.of_to
// n_cst_mailer.of_to
// ARGUMENTS string as_to
// RETURN VALUE              string
// DESCRIPTION

//

// Sets the mailTo item and returns the old one
// 2/4/2011 Rik Brooks
string ls_retVal
int li_count, li_max
ls_retVal = of_to()      // return the old value

li_max = upperBound(io_mail_message.recipient)
for li_count = 1 to li_max
if io_mail_message.recipient[li_count].recipientType = mailTo! Then
// Overwrite the existing mailto.
io_mail_message.recipient[li_count].name = as_to
return ls_retVal
end if
next
// We didn't find a mailTo. Give it one now
li_max ++
io_mail_message.recipient[li_max].recipientType = mailTo!
io_mail_message.recipient[li_max].name = as_to
return ls_retVal

We need a Get and Set for the subject as well.

N_cst_mailer.of_subject
// DESCRIPTION
// Returns the subject of the message
return io_mail_message.subject

N_cst_mailer.of_subject
// ARGUMENTS string as_subject
// RETURN VALUE  string
// DESCRIPTION
// Sets the subject of the message and returns the old subject
string ls_retVal
ls_retVal = of_subject()
io_mail_message.subject = as_subject
return ls_retVal

Now we just need a send function for our mailer object, then we are ready to test it. This object can be expanded to be a full-featured mail object so let's not use the of_send. Let's use a special name for a specialized function. I'm calling mine of_sqlca_error_send since I'm sending an sqlca error.

N_cst_mailer.of_sqlca_error_send
// DESCRIPTION
// Sends the message and returns a status
string ls_retVal, ls_body
mailReturnCode lo_return
ls_retVal = "Success"

lo_return = io_mail_session.mailLogon(mailNewSession!)
IF lo_return = mailReturnSuccess! THEN
if of_to( ) <> "" then

ls_body = "Database Error detected at " + &
string(datetime(today(), now()), "MM/DD/YYYY HH:MM")

ls_body += "~n"
ls_body += "~n SQLDBCODE: " + string(sqlca.sqldbcode)
ls_body += "~n User: " + sqlca.logid
ls_body += "~n~n" + sqlca.sqlerrtext
io_mail_message.notetext = ls_body
lo_return = io_mail_session.mailsend(io_mail_message)
choose case lo_return
case mailReturnFailure!
ls_retVal = "Failure"
case mailReturnInsufficientMemory!
ls_retVal = "Out of memory"
case mailReturnDiskFull!
ls_retVal = "Out of disk space"
case mailReturnTooManySessions!
ls_retVal = "Too many mail sessions"
case mailReturnTooManyFiles!
ls_retVal = "Too many files open"
end choose
end if
else
ls_retVal = "Failure to log in to mail server"
END IF
return ls_retVal

Now our object will work and I'm tempted to end it right here but right here I have an opportunity to show you polymorphism at work in an uncontrived way.

Just a few words first. We are about to implement optional parameters in PowerBuilder. We are going to implement something that looks like this:

Of_sent(To {, message {, subject } } )

What this means is that you must at least pass a TO to the function, but you can also pass a message or a message and a subject. It will default the last two. Look at the help for the messagebox function to see a good example of this.

This will require that you write three functions. I always start with the function that has all the arguments passed.

N_cst_mailer.of_send(to, message, subject)
// ARGUMENTS
//          string as_to - recipient address
//          string as_message - the body
//          string as_subject - the subject
// RETURN VALUE
// A status
// DESCRIPTION
// Sends the message and returns a status
// 2/5/2011 Rik Brooks
string ls_retVal, ls_body
mailReturnCode lo_return
ls_retVal = "Success"
mailSession lo_mail_session

lo_mail_session = create mailSession
lo_return = lo_mail_session.mailLogon(mailNewSession!)
IF lo_return = mailReturnSuccess! THEN

// Set the values in the message object
of_to(as_to)
of_subject( as_subject)
io_mail_message.notetext = as_message

lo_return = lo_mail_session.mailsend(io_mail_message)
choose case lo_return
case mailReturnFailure!
ls_retVal = "Failure"
case mailReturnInsufficientMemory!
ls_retVal = "Out of memory"
case mailReturnDiskFull!
ls_retVal = "Out of disk space"
case mailReturnTooManySessions!
ls_retVal = "Too many mail sessions"
case mailReturnTooManyFiles!
ls_retVal = "Too many files open"
end choose
else
ls_retVal = "Failure to log in to mail server"
END IF
return ls_retVal

Now I default the last argument, in this case the subject line. Notice that I only have to write one line. I call the function that I just wrote but for the last parameter I send a default. If you really wanted to get fancy you could put these defaults into an ini file or your database.

N_cst_mailer.of_send(as_to, as_message)
// ARGUMENTS
//          string as_to                              The recipient
//          string as_body             The body of the message
// RETURN VALUE              string a status
// DESCRIPTION
// The send message with a defaulted subject line
// 2/5/2011 Rik Brooks
return of_send(as_to, as_body, "(No Subject)")

Finally the simplest form of the function:

N_cst_mailer.of_send(to)
// ARGUMENTS
//          as_to
// RETURN VALUE
//          A status
// DESCRIPTION
// Sends an email message
// 2/5/2011 Rik Brooks
return of_send(as_to, "No body sent", "(No Subject)")

Okay, I've made my point. You can create your own function with optional parameters and you do it using polymorphism. Now let's finish out the subject of this article - your custom sqlca.

Create a new object. This one needs to be a standard class (see Figure 2).

This selection will give you a dialog asking which standard class you want. In this case it's a transaction as shown in Figure 3.

Now we will have a painter with a transaction in it. I saved mine as n_cst_sqlca. There are only two small steps to making this work. First we need to put some code in our dbError event. We want to send an email to ourselves whenever there is a database error not caught by our DataWindow. Here's the code:

N_cst_sqlca.dberror
// DESCRIPTION
// Sends an email when an error happens

n_cst_mailer lo_mailer
lo_mailer = create n_cst_mailer
lo_mailer.of_to( "your_address@aol.com")
lo_mailer.of_subject( "test")
messagebox("", lo_mailer.of_sqlca_error_send( ))

Now we use the new object. First we have to tell PowerBuilder that sqlca is no longer the traditional one but it's our new one. Go to your application object and look at the properties. You will see a button that says Additional Properties. When you click on it you get Figure 4.

Now SQLCA is no longer a transaction; it's an n_cst_sqlca that derives or inherits from transaction (see Figure 4).

The last part is to test our sqlca. Create a window and put a button in it. In the clicked event of the button add the following code:

W_main.cb_1.clicked()
// Profile EAS Demo DB V115
SQLCA.DBMS = "ODBC"
SQLCA.AutoCommit = False
SQLCA.DBParm = "ConnectString='DSN=EAS Demo DB V115;UID=dba;PWD=sql'"
connect using sqlca ;

string temp

SELECT "bonus"."no_column" 
INTO :temp 
FROM "bonus"  ;

There is no column named "no_column" in the bonus table. This will throw an error. Now when you run it this will automatically send an email telling you when you got an error and what it was.

Database Error detected at 02/05/2011 05:38

SQLDBCODE: -143
User:

SQLSTATE = S0002
[Sybase][ODBC Driver][SQL Anywhere]Column ‘no_column' not found

This will happen automatically every time there is a database error. There is no coding required on your part and your user doesn't have to do anything. This is what we used to call "elegant programming" and now you know how to do it.

Source Code

About Richard (Rik) Brooks
Rik Brooks has been programming in PowerBuilder since the final beta release before version 1. He has authored or co-authored five books on PowerBuilder including “The Definitive DataWindow”. Currently he lives in Mississippi and works in Memphis, Tennessee.

In order to post a comment you need to be registered and logged in.

Register | Sign-in

Reader Feedback: Page 1 of 1

SOA World Latest Stories
Many key benefits make the Dell MDC a compelling alternative for your data center solution. In his session at the 10th International Cloud Expo, Steve Cuming, Executive Director of Data Center Solutions at Dell, will take a look at the hyper-efficient, snap-together, flexible choice m...
According to a 2011 survey by the Independent Oracle User Group, over 50% of Oracle’s customers have deployed or are considering deploying private clouds. Most private clouds today support non-production workloads because enterprises are unable to deploy mission-critical applications i...
What do the CTOs of the CIA and the U.S. Dept. of Justice and the CIO of the National Reconnaissance Office have in common with the CEOs of Eucalyptus, GoGrid, ActiveState, Appcara, OpSource and Nortonworks, the CTOs of Rackspace, SoftLayer, SOA Software and AppZero, the Founder & Gene...
In this CEO Power Panel at the 10th International Cloud Expo, moderated by Cloud Expo Conference Chair Jeremy Geelan, leading executives in the Cloud Computing and Big Data space will be discussing such topics as: Is it just wishful thinking to depict the Cloud as more than just a te...
In his session at the 10th International Cloud Expo, Marvin Wheeler, Open Data Center Alliance Chairman, will discuss the success the organization has had in charting the requirements for broad-scale enterprise adoption of the cloud and how 2012 is forecast to be the tipping point for ...
Cloud computing is creating the new Wall Street boom, according to NIA. The only industry that is as bright as cloud computing on Wall Street is social networking, NIA said in a recent report. 2012 will be known as the year cloud computing became widely adopted worldwide. Cloud comput...
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021


SYS-CON Featured Whitepapers
ADS BY GOOGLE