Comments
bruce.armstrong wrote: Somebody just said it better than I did, and with more chops to say it: Open Letter to Mark Zuckerberg, Sheryl Sandberg & Facebook Mobile
Cloud Expo on Google News

SYS-CON.TV
Cloud Expo & Virtualization 2009 East
PLATINUM SPONSORS:
IBM
Smarter Business Solutions Through Dynamic Infrastructure
IBM
Smarter Insights: How the CIO Becomes a Hero Again
Microsoft
Windows Azure
GOLD SPONSORS:
Appsense
Why VDI?
CA
Maximizing the Business Value of Virtualization in Enterprise and Cloud Computing Environments
ExactTarget
Messaging in the Cloud - Email, SMS and Voice
Freedom OSS
Stairway to the Cloud
Sun
Sun's Incubation Platform: Helping Startups Serve the Enterprise
POWER PANELS:
Cloud Computing & Enterprise IT: Cost & Operational Benefits
How and Why is a Flexible IT Infrastructure the Key To the Future?
Click For 2008 West
Event Webcasts
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

Latest Cloud Developer Stories
With BigDataExpo 2012 New York (www.BigDataExpo.net), co-located with 10th Cloud Expo, now just three weeks away, what better time to introduce you in greater detail to the distinguished individuals in our incredible Speaker Faculty for the technical and strategy sessions at the ...
If your organization already uses virtualized infrastructure, you are well on your way to providing IT as a Service. But as businesses demand faster results in today’s competitive market, organizations look to gain more benefits from cloud computing than just virtualized infrastr...
Facebook sold off again Tuesday scrapping the bottom at $30.98 after Reuters reported that Scott Devitt, a research analyst at the IPO’s lead underwriter Morgan Stanley, unexpectedly cut his revenue estimates on the company during the roadshow leading up to it going public last F...
With Cloud Expo 2012 New York (10th Cloud Expo) now just three weeks away, what better time to start introducing you in greater detail to the distinguished individuals in our incredible Speaker Faculty for the technical and strategy sessions at the conference...
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 poin...
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

Breaking Cloud Computing News

Spirent Communications today announced the launch of its new May. 23, 2012 08:32 AM EDT