|
Comments
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
DataWindows Extending SQLCA
The goal is to replace SQLCA with something a little more robust
May. 22, 2011 11:00 AM
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 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 Now let's do the SET function N_cs_mailer.of_to // // Sets the mailTo item and returns the old one li_max = upperBound(io_mail_message.recipient) We need a Get and Set for the subject as well. N_cst_mailer.of_subject N_cst_mailer.of_subject 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 lo_return = io_mail_session.mailLogon(mailNewSession!) ls_body = "Database Error detected at " + & ls_body += "~n" 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) lo_mail_session = create mailSession // Set the values in the message object lo_return = lo_mail_session.mailsend(io_mail_message) 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) Finally the simplest form of the function: N_cst_mailer.of_send(to) 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 n_cst_mailer lo_mailer 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() string temp SELECT "bonus"."no_column" 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 SQLSTATE = S0002 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 Reader Feedback: Page 1 of 1
SOA World Latest Stories
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||