Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "EDT:Tutorial: RUI With DataBase Lesson 6"

(Create binding to database connection)
 
(20 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
 
[[EDT:Tutorial: Access a database with EGL Rich UI|Access a database with EGL Rich UI]]
 
[[EDT:Tutorial: Access a database with EGL Rich UI|Access a database with EGL Rich UI]]
  
Line 6: Line 5:
 
|[[EDT:Tutorial: RUI With DataBase Lesson 5|< Previous]] | [[EDT:Tutorial: RUI With DataBase Lesson 7|Next >]]
 
|[[EDT:Tutorial: RUI With DataBase Lesson 5|< Previous]] | [[EDT:Tutorial: RUI With DataBase Lesson 7|Next >]]
 
|}
 
|}
= Lesson 6: Add code for the service functions =
+
= Lesson 6: Add code for the service functions =
  
In EGL, I/O statements such as '''add''' and '''get''' access data that resides in different kinds of persistent data storage, from file systems to queues to databases. The coding is similar for the different cases.
+
In EGL, I/O statements such as '''add''' and '''get''' access data that resides in different kinds of persistent data storage, from file systems to queues to databases. The coding is similar for the different cases.  
  
In this lesson, you add functions that access rows in a relational database. Add the functions in order, before the final '''end''' statement in SQLService.egl.
+
In this lesson, you add functions that access rows in a relational database. Add the functions in order, before the final '''end''' statement in SQLService.egl.  
  
== Create binding to database connection ==
+
== Create binding to database connection ==
  
In Lesson 3, you defined a database connection named '''Derby'''. Use these steps to use the '''Derby''' connection from your SQL service.
+
In Lesson 3, you defined a database connection named '''Derby'''. Use these steps to use the '''Derby''' connection from your SQL service.  
  
Note: The SQL binding will already be included in the deployment descriptor if you checked '''Save data source configuration to deployment descriptor''' when retrieving the table definition from the database.  
+
Note: The SQL binding will already be included in the deployment descriptor if you checked '''Save data source configuration to deployment descriptor''' when retrieving the table definition from the database.  
  
#EGL projects have associated deployment descriptor ('''.egldd''') files. Click on '''PaymentService''' in the project explorer and select '''Properties > EGLDevelopmentDeploymentDescriptor" to see that file '''PaymentService.egldd''' was specified as deployment descriptor when you defined project '''Payment Service'''Click on '''OK''' to close the properties window.[[Image:EDT_Tutorial_edt_richui_sql06_egldd_property.jpg|Deployment descriptor property for service project.]]
+
#EGL projects have associated deployment descriptor ('''.egldd''') files. To see the name of the file that is in use at development time, right click '''PaymentService''' in the project explorer, click '''Properties''', and at the Properties dialog, click '''EGL Development Deployment Descriptor'''. <br>&nbsp;<br>PaymentService.egldd was specified as the deployment descriptor when you defined the project'''. <br><br>'''Click '''OK''' to close the window.'''[[Image:EDT Tutorial edt richui sql06 egldd property.jpg|Deployment descriptor property for service project.]]'''
#Open file '''PaymentService.egldd''' with the EGL deployment descriptor editor. [[Image:EDT_Tutorial_edt_richui_sql06_open_deployment_descriptor.jpg|Open deployment descriptor for service project.]]
+
#In the PaymentService project, right-click the '''PaymentService.egldd''' file and open the file with the EGL Deployment Descriptor editor.<br> [[Image:EDT Tutorial edt richui sql06 open deployment descriptor.jpg|Open deployment descriptor for service project.]]  
#Select the '''Resource Bindings''' folder
+
#Select the '''Resource Bindings''' tab.
#Add a new resource binding named '''Derby''' of binding type '''SQL database binding'''.
+
#Add a new resource binding named '''Derby''' of binding type '''SQL database binding'''.  
#Select radio button '''Add the information from the selected connection below (hard-coded information)'''.   The runtime connection is not supported in EDT 7.0 or earlier.
+
#If you are using EDT 0.7.0, select radio button '''Add the information from the selected connection below (hard-coded information)'''. The runtime connection is not supported in EDT 7.0 or earlier.
#Select '''Derby''' connection details.
+
#If you are using EDT 0.8.0, you will want to select radio button '''Reference the selected workspace connection below (retrieved at runtime)'''.  When the application is deployed to the application server, the connection will be accessed at runtime using a JNDI name as a resource reference.  If you deploy to an  Apache Tomcat Server, EGL deployment uses both the JNDI name and the connection details to create the JNDI <resource-ref> entry in the '''WEB-INF\web.xml''' file and the <resource> entry in the '''META-INF\context.xml''' file that will be used at run time. The only additional thing you need to do is make sure the '''derbyclient.jar''' file is copied to the Tomcat lib directory.
#Clink on '''Finish'''.[[Image:EDT_Tutorial_edt_richui_sql05_add_sql_binding.jpg|Add SQL binding to deployment descriptor.]]
+
#Select '''Derby''' connection details.  
 +
#Clink on '''Finish'''.[[Image:EDT Tutorial edt richui sql05 add sql binding.jpg|Add SQL binding to deployment descriptor.]]  
 
#Close file '''PaymentServices.egldd'''
 
#Close file '''PaymentServices.egldd'''
  
== Using an SQL resource binding in service program ==
+
== Using an SQL resource binding in service program ==
  
Insert a dataSource variable in the service program following the program name:<code>
+
Insert an SQLDataSource variable in the service program following the program name:<br><br>  
  package services;
+
 
 
+
*For EDT .7:<br>
  service SQLService
+
 
  ds dataSource? { @Resource { bindingKey="Derby" } } ;  
+
package services;
   end
+
</code>
+
service SQLService
 +
  ds SQLDataSource? { @Resource { bindingKey="Derby" } }&nbsp;;    // EDT 0.7.O syntax
 +
end
 +
 
 +
<br>
 +
 
 +
*For EDT .8:
 +
 
 +
package services;
 +
 +
service SQLService
 +
  ds SQLDataSource? { @Resource { uri="binding:Derby" } }&nbsp;;  // EDT 0.8.O syntax
 +
end
 +
 
 +
<br>  
  
 
The syntax directs the service to use the binding named "Derby" defined as a resource in the deployment descriptor file associated with the service project.
 
The syntax directs the service to use the binding named "Derby" defined as a resource in the deployment descriptor file associated with the service project.
  
== Handling SQL exceptions ==
+
== Handling SQL exceptions ==
  
SQL operations can fail for a multitude of reasons.   For our simple example, our service will catch all SQL and log all SQL exceptions on the server and then throw the exception back to the service client.   It will also log each service invocation.
+
SQL operations can fail for a multitude of reasons. For our simple example, our service will catch all SQL and log all SQL exceptions on the server and then throw the exception back to the service client. It will also log each service invocation.  
 +
 
 +
Copy and paste these logging functions to the service program before the final '''end''' statement:<code></code>
  
Copy and paste these logging functions to the service program before the final '''end''' statement:<code>
 
 
     logActive boolean = true;
 
     logActive boolean = true;
 
     activeService string;
 
     activeService string;
Line 54: Line 69:
 
     private function logException(ex sqlException?)
 
     private function logException(ex sqlException?)
 
         accumulatedMessage string = "Exception:  SQLService, " + activeService;
 
         accumulatedMessage string = "Exception:  SQLService, " + activeService;
         while(ex != null)
+
         while(ex&nbsp;!= null)
 
             accumulatedMessage = accumulatedMessage + ", SQLSTATE = " +
 
             accumulatedMessage = accumulatedMessage + ", SQLSTATE = " +
 
                     ex.SQLState + ", message = " + ex.message;
 
                     ex.SQLState + ", message = " + ex.message;
Line 68: Line 83:
 
         end
 
         end
 
     end
 
     end
</code>
 
  
== Add a payment record ==
 
  
Include a function to the service program that uses the '''add''' statement to insert a new
 
row to the database.
 
  
To code the function:
+
== Add a payment record  ==
 +
 
 +
Include a function to the service program that uses the '''add''' statement to insert a new row to the database.
 +
 
 +
To code the function:  
  
 
*In the EGL editor, copy and paste the following lines into '''SQLService.egl''' before the logging functions:
 
*In the EGL editor, copy and paste the following lines into '''SQLService.egl''' before the logging functions:
 +
 
   function addPayment(newPayment paymentRec in)
 
   function addPayment(newPayment paymentRec in)
         logEntry ( "addPayment" ) ;
+
         logEntry ( "addPayment" )&nbsp;;
 
         try
 
         try
             add newPayment to ds ;
+
             add newPayment to ds&nbsp;;
 
         onException(ex sqlException)
 
         onException(ex sqlException)
 
             logException(ex);
 
             logException(ex);
 
         end
 
         end
 
   end
 
   end
*Before you continue, you must resolve the reference to the '''paymentRec''' Record part. You can automatically create '''import''' statements by using the Organize Imports feature. Right-click any blank area in the editor and click '''EGL Source > Organize Imports'''.  
+
 
:EGL adds the following statement to the beginning of the file:  
+
*Before you continue, you must resolve the reference to the '''paymentRec''' Record type. You can automatically create '''import''' statements by using the Organize Imports feature. Right-click any blank area in the editor and click '''EGL Source &gt; Organize Imports'''.
 +
 
 +
:EGL adds the following statement to the beginning of the file:
 +
 
 
   import records.paymentRec;
 
   import records.paymentRec;
 +
 
:The reference is now resolved. You will use this feature often, whether by selecting the menu item or by pressing Ctrl-Shift-O.
 
:The reference is now resolved. You will use this feature often, whether by selecting the menu item or by pressing Ctrl-Shift-O.
*Save the file (Ctrl-S), and then place your cursor anywhere in the '''add''' statement. Press keys CTRL-1 and select '''Add SQL Statement''' from the popup menu. This feature changes the implicit SQL that underlies the EGL '''add''' statement into embedded code that you can modify:  
+
 
 +
*Save the file (Ctrl-S), and then place your cursor anywhere in the '''add''' statement. Press keys CTRL-1 and select '''Add SQL Statement''' from the popup menu. This feature changes the implicit SQL that underlies the EGL '''add''' statement into embedded code that you can modify:
 +
 
 
   function addPayment(newPayment paymentRec in)
 
   function addPayment(newPayment paymentRec in)
         logEntry ( "addPayment" ) ;
+
         logEntry ( "addPayment" )&nbsp;;
 
         try
 
         try
 
             add newPayment to ds
 
             add newPayment to ds
Line 100: Line 122:
 
                               PAYEE_NAME, PAYEE_ADDRESS1, PAYEE_ADDRESS2)
 
                               PAYEE_NAME, PAYEE_ADDRESS1, PAYEE_ADDRESS2)
 
                         values
 
                         values
                               (?, ?, ?, ?, ?, ?, ?, ?)
+
                               (?,&nbsp;?,&nbsp;?,&nbsp;?,&nbsp;?,&nbsp;?,&nbsp;?,&nbsp;?)
 
                   };
 
                   };
 
         onException(ex sqlException)
 
         onException(ex sqlException)
Line 106: Line 128:
 
         end
 
         end
 
   end  
 
   end  
Notice the PAYMENT_ID field is not included in the record fields added to the table.  The '''@GeneratedValue''' annotation for the field in the record definition tells the EGL SQL builder not to explicitly add the column.
 
  
''' Related reference '''<br>
+
Notice the PAYMENT_ID field is not included in the record fields added to the table. The '''@GeneratedValue''' annotation for the field in the record definition tells the EGL SQL builder not to explicitly add the column.
  
* Help topic:  add considerations for SQL<br />
+
'''Related reference '''<br>
* Help topic:  Functions<br />
+
* Help topic:  import<br />
+
* Help topic:  SQL data access<br />
+
  
== Read all database records ==
+
*Help topic: add considerations for SQL<br>
 +
*Help topic: Functions<br>
 +
*Help topic: import<br>
 +
*Help topic: SQL data access<br>
  
The <code>getAllPayments</code> function uses the get statement with an array object to read all
+
== Read all database records ==
of the records from the table and stores them in an array.
+
  
To
+
The <code>getAllPayments</code> function uses the get statement with an array object to read all of the records from the table and stores them in an array.
code the function:
+
 
 +
To code the function:  
  
 
*In the EGL editor, copy and paste the following lines into '''SQLService.egl''' before the logging functions:
 
*In the EGL editor, copy and paste the following lines into '''SQLService.egl''' before the logging functions:
 +
 
   function getAllPayments() returns(paymentRec[])
 
   function getAllPayments() returns(paymentRec[])
 
       payments paymentRec[];
 
       payments paymentRec[];
Line 134: Line 156:
 
       return(payments);
 
       return(payments);
 
   end
 
   end
:The EGL '''get''' statement generates an SQL SELECT statement to retrieve a result set. When the target of the '''get''' statement is a dynamic array of records, EGL retrieves all matching rows from the result set and inserts each successive row into the next array element. 
 
*As with the '''add''' statement, you may place the cursor anywhere in the statement and press CNTL-1 to see the SQL statement EGL will use to retrieve the rows.  You may again save the default statement and modify it if it does do what you want.
 
*Save the file.
 
  
''' Related reference '''<br>
+
:The EGL '''get''' statement generates an SQL SELECT statement to retrieve a result set. When the target of the '''get''' statement is a dynamic array of records, EGL retrieves all matching rows from the result set and inserts each successive row into the next array element.
  
* Help topic:  get considerations for SQL<br />
+
*As with the '''add''' statement, you may place the cursor anywhere in the statement and press CNTL-1 to see the SQL statement EGL will use to retrieve the rows. You may again save the default statement and modify it if it does do what you want.
 +
*Save the file.
  
== Replace a record ==
+
'''Related reference '''<br>
  
The '''editPayment''' function replaces an existing row in the database with an edited version. The default statement replaces all records in the table that have key values equal to the contents of the key fields in the record variable.  Key fields are those fields declared with the '''@Id''' attribute (field '''payment_id''' in our example).
+
*Help topic: get considerations for SQL<br>
  
To code the function:
+
== Replace a record  ==
 +
 
 +
The '''editPayment''' function replaces an existing row in the database with an edited version. The default statement replaces all records in the table that have key values equal to the contents of the key fields in the record variable. Key fields are those fields declared with the '''@Id''' attribute (field '''payment_id''' in our example).
 +
 
 +
To code the function:
 +
 
 +
*In the EGL editor, copy and paste the following lines into '''SQLService.egl''' before the logging functions:
  
*In the EGL editor, copy and paste the following lines into '''SQLService.egl''' before the logging functions: 
 
 
   function editPayment(chgPayment paymentRec in)
 
   function editPayment(chgPayment paymentRec in)
 
       logEntry("editPayment");
 
       logEntry("editPayment");
Line 157: Line 182:
 
       end
 
       end
 
   end
 
   end
 +
 
The EGL '''replace''' statement generates an SQL UPDATE statement.  
 
The EGL '''replace''' statement generates an SQL UPDATE statement.  
*Save the file.
 
  
''' Related reference '''<br>
+
*Save the file.
  
* Help topic:  replace considerations for SQL<br />
+
'''Related reference '''<br>
  
== Delete a record ==
+
*Help topic: replace considerations for SQL<br>
  
The <code>deletePayment</code> function deletes the specified record from the table.
+
== Delete a record ==
  
To code the function:
+
The '''deletePayment''' function deletes the specified record from the table.
 +
 
 +
To code the function:  
  
 
*In the EGL editor, copy and paste the following lines into '''SQLService.egl''' before the logging functions:
 
*In the EGL editor, copy and paste the following lines into '''SQLService.egl''' before the logging functions:
 +
 
   function deletePayment(delPayment paymentRec in)
 
   function deletePayment(delPayment paymentRec in)
 
       logEntry("deletePayment");
 
       logEntry("deletePayment");
Line 176: Line 204:
 
           delete delPayment from ds;
 
           delete delPayment from ds;
 
       onException(ex SQLException)
 
       onException(ex SQLException)
           if(ex.SQLState != "02000") // sql state is five digits
+
           if(ex.SQLState&nbsp;!= "02000") // sql state is five digits
               logException sqlException;
+
               logException(ex);
 
           end
 
           end
 
       end
 
       end
 
   end  
 
   end  
*:The EGL '''delete''' statement generates an SQL DELETE statement. If no rows are present, the Derby database returns an SQLState value of "02000", and the EGL runtime code throws an exception that the function ''catches'': that is, processes,
 
in some onException logic.
 
  
*:When a function catches but ignores an exception, processing continues without interruption. That rule applies to the preceding logic, when the value of SQLState is "02000". When a function uses the '''throw''' statement to ''throw'' an exception, the exception stays active. That rule also applies to the preceding logic, when the value of SQLState is other than "02000".
+
*The EGL '''delete''' statement generates an SQL DELETE statement. If no rows are present, the Derby database returns an SQLState value of "02000", and the EGL runtime code throws an exception that the function ''catches'': that is,&nbsp;processes, in some onException logic.
 +
*When a function catches but ignores an exception, processing continues without interruption. That rule applies to the preceding logic, when the value of SQLState is "02000". When a function uses the '''throw''' statement to ''throw'' an exception, the exception stays active. That rule also applies to the preceding logic, when the value of SQLState is other than "02000".
 +
*At run time, if a service does not handle an exception, the service requester receives an exception of type ServiceInvocationException. Incidentally, if the service cannot be accessed, the requester receives an exception of type ServiceInvocationException or ServiceBindingException, depending on the details of the error.
 +
*Save the file.
  
:*At run time, if a service does not handle an exception, the service requester receives an exception of type ServiceInvocationException. Incidentally, if the service cannot be
+
'''Related reference '''<br>
accessed, the requester receives an exception of type ServiceInvocationException
+
or ServiceBindingException, depending on the details of the error.
+
  
*Save the file.
+
*Help topic: delete considerations for SQL<br>
 +
*Help topic: Exception handling<br>
  
''' Related reference '''<br>
+
== Create test data  ==
  
* Help topic:  delete considerations for SQL<br />
+
The '''createDefaultTable''' function creates a set of data for testing your completed application.
* Help topic:  Exception handling<br />
+
  
== Create test data ==
+
To code the function:
  
The '''createDefaultTable''' function creates a set of data for testing your completed application.
+
*In the EGL editor, copy and paste the following lines into '''SQLService.egl''' before the logging functions:
 
+
To code the function:
+
  
*In the EGL editor, copy and paste the following lines into '''SQLService.egl''' before the logging functions: 
 
 
   function createDefaultTable() returns(paymentRec[])
 
   function createDefaultTable() returns(paymentRec[])
 
       payments paymentRec[];
 
       payments paymentRec[];
Line 214: Line 238:
 
               };
 
               };
 
         onException(ex SQLException)
 
         onException(ex SQLException)
             if(ex.SQLState != "02000")  // sqlState is five digits
+
             if(ex.SQLState&nbsp;!= "02000")  // sqlState is five digits
 
                 throw ex;
 
                 throw ex;
 
             end
 
             end
Line 240: Line 264:
 
         return(payments);
 
         return(payments);
 
     end
 
     end
 +
 
The code acts as follows:  
 
The code acts as follows:  
  
<ul><li>The EGL '''execute''' statement runs a literal SQL statement that deletes all rows from the PAYMENT table.  
+
*The EGL '''execute''' statement runs a literal SQL statement that deletes all rows from the PAYMENT table.  
<li>The <code>ispDate</code> variable receives a date value from the '''dateTimeLib.dateValueFromGregorian()''' system function. The content of the variable is then in a format that is appropriate for insertion into the <code>dueDate</code> field in the database.  
+
*The '''ispDate''' variable receives a date value from the '''dateTimeLib.dateValueFromGregorian()''' system function. The content of the variable is then in a format that is appropriate for insertion into the <code>dueDate</code> field in the database.  
<li>The <code>addPayment</code> function is repeatedly invoked to add new rows to the PAYMENT table.  
+
*The '''addPayment''' function is repeatedly invoked to add new rows to the PAYMENT table.  
<li>The call to the <code>getAllPayments</code> function returns an array of rows that were retrieved from the table.
+
*The call to the '''getAllPayments''' function returns an array of rows that were retrieved from the table.
</ul>
+
  
*Press Ctrl-Shift-F to format the code. If you see any red Xs, compare your code with the finished code in [[EDT:Tutorial: RUI With DataBase Lesson 6 Code|Code for PaymentFileMaintenance.egl after lesson 6]]
+
*Press Ctrl-Shift-F to format the code. If you see any red Xs, compare your code with the finished code in [[EDT:Tutorial: RUI With DataBase Lesson 6 Code|Code for SQLService.egl after lesson 6]]  
*Save and close the file.  
+
*Save and close the file.
  
''' Related reference '''<br>
+
'''Related reference '''<br>  
  
* Help topic:   execute considerations for SQL<br />
+
*Help topic: execute considerations for SQL<br>  
* Help topic:   dateValueFromGregorian()<br />
+
*Help topic: dateValueFromGregorian()<br>
  
== Lesson checkpoint ==
+
== Lesson checkpoint ==
  
You learned how to complete the following tasks:
+
You learned how to complete the following tasks:  
  
<ul><li>Add embedded SQL code to a program and modify that code
+
*Add embedded SQL code to a program and modify that code  
<li>Automatically create and organize '''import''' statements
+
*Automatically create and organize '''import''' statements
</ul>
+
  
In the next lesson, you will create a widget to hold the table
+
In the next lesson, you will create a widget to hold the table of expense data.  
of expense data.
+
  
{| style="float: right"
+
{| style="float: right" class="FCK__ShowTableBorders"
|[[EDT:Tutorial: RUI With DataBase Lesson 5|&lt; Previous]] | [[EDT:Tutorial: RUI With DataBase Lesson 7|Next >]]
+
|-
 +
| [[EDT:Tutorial: RUI With DataBase Lesson 5|&lt; Previous]] &#124; [[EDT:Tutorial: RUI With DataBase Lesson 7|Next &gt;]]
 
|}
 
|}
  
 
[[Category:EDT]]
 
[[Category:EDT]]

Latest revision as of 09:30, 12 April 2012

Access a database with EGL Rich UI


< Previous | Next >

Lesson 6: Add code for the service functions

In EGL, I/O statements such as add and get access data that resides in different kinds of persistent data storage, from file systems to queues to databases. The coding is similar for the different cases.

In this lesson, you add functions that access rows in a relational database. Add the functions in order, before the final end statement in SQLService.egl.

Create binding to database connection

In Lesson 3, you defined a database connection named Derby. Use these steps to use the Derby connection from your SQL service.

Note: The SQL binding will already be included in the deployment descriptor if you checked Save data source configuration to deployment descriptor when retrieving the table definition from the database.

  1. EGL projects have associated deployment descriptor (.egldd) files. To see the name of the file that is in use at development time, right click PaymentService in the project explorer, click Properties, and at the Properties dialog, click EGL Development Deployment Descriptor.
     
    PaymentService.egldd was specified as the deployment descriptor when you defined the project.

    Click OK to close the window.Deployment descriptor property for service project.
  2. In the PaymentService project, right-click the PaymentService.egldd file and open the file with the EGL Deployment Descriptor editor.
    Open deployment descriptor for service project.
  3. Select the Resource Bindings tab.
  4. Add a new resource binding named Derby of binding type SQL database binding.
  5. If you are using EDT 0.7.0, select radio button Add the information from the selected connection below (hard-coded information). The runtime connection is not supported in EDT 7.0 or earlier.
  6. If you are using EDT 0.8.0, you will want to select radio button Reference the selected workspace connection below (retrieved at runtime). When the application is deployed to the application server, the connection will be accessed at runtime using a JNDI name as a resource reference. If you deploy to an Apache Tomcat Server, EGL deployment uses both the JNDI name and the connection details to create the JNDI <resource-ref> entry in the WEB-INF\web.xml file and the <resource> entry in the META-INF\context.xml file that will be used at run time. The only additional thing you need to do is make sure the derbyclient.jar file is copied to the Tomcat lib directory.
  7. Select Derby connection details.
  8. Clink on Finish.Add SQL binding to deployment descriptor.
  9. Close file PaymentServices.egldd

Using an SQL resource binding in service program

Insert an SQLDataSource variable in the service program following the program name:

  • For EDT .7:
package services;

service SQLService
  ds SQLDataSource? { @Resource { bindingKey="Derby" } } ;    // EDT 0.7.O syntax
end


  • For EDT .8:
package services;

service SQLService
  ds SQLDataSource? { @Resource { uri="binding:Derby" } } ;   // EDT 0.8.O syntax
end


The syntax directs the service to use the binding named "Derby" defined as a resource in the deployment descriptor file associated with the service project.

Handling SQL exceptions

SQL operations can fail for a multitude of reasons. For our simple example, our service will catch all SQL and log all SQL exceptions on the server and then throw the exception back to the service client. It will also log each service invocation.

Copy and paste these logging functions to the service program before the final end statement:

    logActive boolean = true;
    activeService string;
    
    private function logEntry(serviceFunction string in)
        activeService = serviceFunction;
        log("Entry:  SQLService, " + serviceFunction);
    end
    
    private function logException(ex sqlException?)
        accumulatedMessage string = "Exception:  SQLService, " + activeService;
        while(ex != null)
            accumulatedMessage = accumulatedMessage + ", SQLSTATE = " +
                    ex.SQLState + ", message = " + ex.message;
            ex = ex.nextException;
        end
        log(accumulatedMessage);
        throw new anyException{message = accumulatedMessage};
    end
    
    private function log(text string in)
        if(logActive)
            sysLib.writeStdOut(text);
        end
    end


Add a payment record

Include a function to the service program that uses the add statement to insert a new row to the database.

To code the function:

  • In the EGL editor, copy and paste the following lines into SQLService.egl before the logging functions:
  function addPayment(newPayment paymentRec in)
       logEntry ( "addPayment" ) ;
       try
           add newPayment to ds ;
       onException(ex sqlException)
           logException(ex);
       end
  end
  • Before you continue, you must resolve the reference to the paymentRec Record type. You can automatically create import statements by using the Organize Imports feature. Right-click any blank area in the editor and click EGL Source > Organize Imports.
EGL adds the following statement to the beginning of the file:
  import records.paymentRec;
The reference is now resolved. You will use this feature often, whether by selecting the menu item or by pressing Ctrl-Shift-O.
  • Save the file (Ctrl-S), and then place your cursor anywhere in the add statement. Press keys CTRL-1 and select Add SQL Statement from the popup menu. This feature changes the implicit SQL that underlies the EGL add statement into embedded code that you can modify:
  function addPayment(newPayment paymentRec in)
       logEntry ( "addPayment" ) ;
       try
           add newPayment to ds
               with #sql{
                       insert into PAYMENT
                             (CATEGORY, DESCRIPTION, AMOUNT, FIXED_PAYMENT, DUE_DATE, 
                             PAYEE_NAME, PAYEE_ADDRESS1, PAYEE_ADDRESS2)
                       values
                             (?, ?, ?, ?, ?, ?, ?, ?)
                 };
       onException(ex sqlException)
           logException(ex);
       end
  end 

Notice the PAYMENT_ID field is not included in the record fields added to the table. The @GeneratedValue annotation for the field in the record definition tells the EGL SQL builder not to explicitly add the column.

Related reference

  • Help topic: add considerations for SQL
  • Help topic: Functions
  • Help topic: import
  • Help topic: SQL data access

Read all database records

The getAllPayments function uses the get statement with an array object to read all of the records from the table and stores them in an array.

To code the function:

  • In the EGL editor, copy and paste the following lines into SQLService.egl before the logging functions:
  function getAllPayments() returns(paymentRec[])
      payments paymentRec[];
      logEntry("getAllPayments");
      try
          get payments from ds;
      onException(ex sqlException)
          logException(ex);
      end
      return(payments);
  end
The EGL get statement generates an SQL SELECT statement to retrieve a result set. When the target of the get statement is a dynamic array of records, EGL retrieves all matching rows from the result set and inserts each successive row into the next array element.
  • As with the add statement, you may place the cursor anywhere in the statement and press CNTL-1 to see the SQL statement EGL will use to retrieve the rows. You may again save the default statement and modify it if it does do what you want.
  • Save the file.

Related reference

  • Help topic: get considerations for SQL

Replace a record

The editPayment function replaces an existing row in the database with an edited version. The default statement replaces all records in the table that have key values equal to the contents of the key fields in the record variable. Key fields are those fields declared with the @Id attribute (field payment_id in our example).

To code the function:

  • In the EGL editor, copy and paste the following lines into SQLService.egl before the logging functions:
  function editPayment(chgPayment paymentRec in)
      logEntry("editPayment");
      try
          replace chgPayment to ds;
      onException(ex SQLException)
          logException(ex);
      end
  end

The EGL replace statement generates an SQL UPDATE statement.

  • Save the file.

Related reference

  • Help topic: replace considerations for SQL

Delete a record

The deletePayment function deletes the specified record from the table.

To code the function:

  • In the EGL editor, copy and paste the following lines into SQLService.egl before the logging functions:
  function deletePayment(delPayment paymentRec in)
      logEntry("deletePayment");
      try
          delete delPayment from ds;
      onException(ex SQLException)
          if(ex.SQLState != "02000") // sql state is five digits
              logException(ex);
          end
      end
  end 
  • The EGL delete statement generates an SQL DELETE statement. If no rows are present, the Derby database returns an SQLState value of "02000", and the EGL runtime code throws an exception that the function catches: that is, processes, in some onException logic.
  • When a function catches but ignores an exception, processing continues without interruption. That rule applies to the preceding logic, when the value of SQLState is "02000". When a function uses the throw statement to throw an exception, the exception stays active. That rule also applies to the preceding logic, when the value of SQLState is other than "02000".
  • At run time, if a service does not handle an exception, the service requester receives an exception of type ServiceInvocationException. Incidentally, if the service cannot be accessed, the requester receives an exception of type ServiceInvocationException or ServiceBindingException, depending on the details of the error.
  • Save the file.

Related reference

  • Help topic: delete considerations for SQL
  • Help topic: Exception handling

Create test data

The createDefaultTable function creates a set of data for testing your completed application.

To code the function:

  • In the EGL editor, copy and paste the following lines into SQLService.egl before the logging functions:
  function createDefaultTable() returns(paymentRec[])
      payments paymentRec[];
      logEntry("createDefaultTable");
      try
          try
              execute from ds
                 with #sql{
                    delete from PAYMENT
             };
        onException(ex SQLException)
            if(ex.SQLState != "02000")  // sqlState is five digits
                throw ex;
            end
        end 
        ispDate date = dateTimeLib.dateFromGregorian(20140405);
        addPayment(new paymentRec{category = 1, description = "Apartment"
              , amount = 880, fixedPayment = yes, dueDate = new date
              , payeeName = "A Jones", payeeAddress1 = "100 Jones Dr"
              , payeeAddress2 = "Jonesboro, NC"
              });
        addPayment(new paymentRec{category = 2, description = "Groceries"
              , amount = 450, fixedPayment = no, dueDate = new date
              , payeeName = "B Jones", payeeAddress1 = "200 Jones Dr"
              , payeeAddress2 = "Jonesboro, NC"
          });
        addPayment(new paymentRec{category = 5, description = "ISP"
              , amount = 19.99, fixedPayment = no, dueDate = ispDate
              , payeeName = "C Jones", payeeAddress1 = "300 Jones Dr"
              , payeeAddress2 = "Jonesboro, NC"
        });
        payments = getAllPayments();
          onException(ex anyException)
              logException(ex);
          end
        return(payments);
    end

The code acts as follows:

  • The EGL execute statement runs a literal SQL statement that deletes all rows from the PAYMENT table.
  • The ispDate variable receives a date value from the dateTimeLib.dateValueFromGregorian() system function. The content of the variable is then in a format that is appropriate for insertion into the dueDate field in the database.
  • The addPayment function is repeatedly invoked to add new rows to the PAYMENT table.
  • The call to the getAllPayments function returns an array of rows that were retrieved from the table.

Related reference

  • Help topic: execute considerations for SQL
  • Help topic: dateValueFromGregorian()

Lesson checkpoint

You learned how to complete the following tasks:

  • Add embedded SQL code to a program and modify that code
  • Automatically create and organize import statements

In the next lesson, you will create a widget to hold the table of expense data.

< Previous | Next >

Back to the top