Building Business Logic
SQLData Server Customization
Building Business Logic
Creating Web Applications
Introduction
SQLData Server provides both web services and database services. The web services enable HTML being delivered to user browser. It can be understood as user interface component in a multi-tier web application. The database services enable generic database operations, such as insert, delete and update, to be processed against your ODBC data sources. It is virtually the backend processor. The things that are missing are your application specific logic: How to control data presentations, how to preserve data integrity and consistency.
This document describes the processes of building powerful web applications using SQLData enterprise server.
Feature Highlights
SQLData Enterprise Server provides many ways for building your own business logic with virtually no programming necessary:
- Create customized reports: Users can view reports with a one-button click. You can also control what users can or cannot see in the presentation. There is virtually no limit on the number of reports, you can build all your common queries as reports.
- Build stored procedures: Stored procedures are a series of SQL statements that can be executed as a whole. SQLData Enterprise Server allows you to create stored procedures even your DBMS doesn't support. Stored procedures can be referenced by procedure names in your web pages.
- Add Triggers: Similar to stored procedures, triggers are groups SQL statements. But they are only executed when certain events occur. SQLData Enterprise Server supports both before-trigger (triggered before an action) and after-trigger (triggered after an action).
- Define Visual Presentations: You can map a database field to a visual object, such as a text box, a dropdown box or a multi-choice list. The field will be displayed as such. SQLData Enterprise Server allows you to define your presentation schema using a web browser.
- Specify data verifications/validations: This allows you to make sure that data submitted in a HTML form has the right type, correct format, and legal size.
Table Logic
Business logic is categorized into two types: Table Logic and Field Logic. Table logic is associated with database tables and Field Logic is related to columns in database tables.
Table Logic includes procedures, triggers and reports, which operate on database tables. All table logic items are stored in a SQLData System table called TABLELOGIC. SQLData server retrieves logic items from the table and executes them at runtime. Because TABLELOGIC is a normal database table, it can be edited using the Record Editor and Record Browser shipped with the SQLData Enterprise Server.
The fields in the TABLELOGIC are described below:
- LOGICTYPE
: The name of the database table this logic item is associated with. Its value must be REPORT for all reports and PROCEDURE for all stored procedures.
- LOGICNAME
: The name of the logic item. It is used for referencing the logic item. It is the name of your report for customized report and the name of the procedure for stored procedures. The following is a list of reserved logic names that have predefined meanings:
1. INSERT: This is a logic item for insert operations of the table (name defined by LOGICTYPE). The SQL statement defined in the item will be executed whenever there is an insert operation on the table.
2. UPDATE: This is a logic item for update operations of the table. The SQL Statement defined in the item will be executed whenever there is a record update to the table.
3. DELETE: This is a logic item for deletion operations of the table. The SQL Statement defined in the item will be executed whenever there is a delete operation from the table.
4. QUERY: This is a logic item for query operations of the table. The SQL Statement defined in the item will be executed whenever there is search operation for the table. This is often used for defining the view of a table and limiting the number of columns user can see in the table.
5. UNIQUE: This is a logic item for assuring uniqueness of records to be inserted into the table. It is usually a query statement with primary key in the WHERE clause. The server executes the query statement and make sure the record does not exist before adding the record.
- SQLSTATEMENT
: The SQL statements to be executed. Multiple statements are separated by semicolon. This is a required field for all table logic items. It should contain one query statement for each customized report.
- TEMPLATEFILE
: The template file that is used for formatting results of the operation. The formatted result will be send to user as HTML file. This is only needed for certain reports or queries. It is not used for triggers and stored procedures.
- DESCRIPTION
: The description or comment about the logic item. This is what will be displayed under My Report in user's browser if the logic item is a report. It is used as comments for all other logic items.
Field Logic
Field Logic is logic that associated with a particular column in a database table. For example, you may want to display STATE in a customer table as dropdown list, or encrypt/decrypt a PASSWORD field. A field logic item defines how a database field is presented, converted and validated.
The Field Logic is very often used for creating HTML forms that link directly to your database tables. It allows you to control what to present, how to display, when to validate and where to find helps.
SQLData Enterprise/Desktop Servers shipped with a Logic Editor which can be used for defining the field logic very easily. The Logic Editor can be invoked on the Record Editor page where a record detail is displayed. You can create field logic by clicking on a small image with a hand and pen right after each field name.
Field logic items are stored in a database table, FIELDLOGIC, in the SQLData system database. The following is the definition and description of all fields in the FIELDLOGIC table.
- TABLENAME
: The name of a database table this logic is operated on. It is required for all field logic items.
- LOGICNAME
: The name of the logic item. This should be the field name the logic is operated on. It is required for all field logic items.
- DISPLAYNAME
: The name of the field to be displayed. Field names in database table are sometime abbreviated and hard to understand. For instance, you may wish to display a field named UID as User ID. This is accomplished by specify a different display name from the field name.
- VISUALTYPE
: The type of visual element that is used for displaying the value of the field. Visual Type is usually used in HTML forms where user can enter data. This value is stored as an integer in the database. But you are given the following choices if you edit field logic:
1. TextBox: A single line text field.
2. MultilineText: A multi-line text box.
3. List: A list of multiple choices. The values of the list are given in the SELECTSTRING and SELECTVALUE field (see below).
4. Password: A password field. Value of the field will be masked.
5. Checkbox: checkbox buttons where user can make multiple choices. The values of the button is given in the SELECTSTRING and SELECTVALUE field (see below).
6. RadioButton: Radio buttons where user can select one from many. The values of the button is given in the SELECTSTRING and SELECTVALUE field (see below).
7. StaticText: Display the value of the field as static text so that user can not make any changes.
8. DataSourceList: A dropdown list that contains all available ODBC data sources.
9. TableList: A dropdown list that contains all available tables in the current ODBC data source.
10. ValueList: A dropdown list that contains all unique values in the database field.
11. Hidden: Hide the field and its value.
12. MapList: A dropdown list populated using a SQL select statement stored in the SELECSTRING field. The first field value selected by the query statement will be used as the actual value, and the rest of the field values will be used as strings displayed in the dropdown list.
- SELECTSTRING
: A list of strings to be displayed as the values of an HTML element. HTML elements, such as selection list, radio button and checkbox, have display values (the value users see on the screen) and actual values (the value submitted to the web server). SELECTSTRING is the display value of List,Dropdown, Checkbox, and RadioButton. Each item in the string is separated by the '+' character. For example, to display two states in the United States: Virginia and Maryland, the SELECTSTRING should be:
Virginia+Maryland
SELECTSTRING should be an SQL select statement if VISUALTYPE is MapList.
- SELECTVALUE
: A string that contains all possible values that are to be submitted to the server. Values are separated by the '+' character. The value should match what are stored in the database table. For example, if Virginia and Maryland are stored as VA and MD respectively in the database. The SELECTVALUE should be:
VA+MD
When a user selects Virginia from the dropdown list, The value VA is submitted to the web server. SELECTSTRING and SELECTVALUE are very useful for displaying encoded values with human understandable terms.
- FIELDTYPE
: This defines data conversions to be applied to the data field. The conversion is done at the server side when it received the data posted from a HTML form. The followings are possible choices:
1. AllCap : Convert the field value to upper case before storing in the database table.
2. Password: Encrypt the data.
3. Selection: Convert the values from multiple choices into a single value. The values are ORed together before saving to the database.
4. Trim: Remove the trailing spaces from the value.
5. Oneline: Remove all linefeeds and carriage returns from the data.
- VALIDATIONRULE
: A string of regular expressions separated by semicolon. The regular expressions are used for matching user-submitted data. An error will be returned to user if the matching failed.
- ROWS
: The number of rows in a multiline text box, or the size of a list box
- COLS
: This value is used by both the multi-line text box and radio buttons. It defines the number of characters in each row for text boxes and specifies the number of radio buttons in each line for radio buttons.
- DEFAULTVALUE
: A value to be used when the field value is empty. This value is particular useful when the field is displayed as a list or dropdown list. The server uses the default value for selecting (highlighting) a value. The value is also put into a text box if the field visual type is text box and there is no value defined for the field.
- NORETURN
: This is a Boolean value to instruct the server that a carriage return should not be appended at end of the field, which means that the next field will be displayed on the same line as the current field.
- DESCRIPTION
: A help message for your users. It the value is not empty, the server will hyperlink the field name to the description so that when users click on the field name, a separate window will pop up with your description of the field. This is sometimes called a data dictionary.
- HINT
: This is a string that can be used for helping users to enter data into your HTML form. It can be the format of the data you expected, such as mm/dd/yyyy for a date string. The string is displayed at end of the field.
Create Reports
Reports can be accessed directly on your desktop and can be executed with one button click. Reports are also displayed on both the Record Browser and Record Editor pages for quick execution.
SQLData Enterprise/Desktop Server shipped with a Report Editor which makes report management extremely easy. The following is a procedure to add your reports using a web browser:
- Click the MyReport link in your browser.
- Once you are in the Report Editor, click on the Add New button on the bottom of the page.
- Type in REPORT for LogicType and give a unique name as the LogicName. LogicName is the name of your report.
- Select an ODBC data source from the dropdown list.
- Type in a template file name if you want to display the result set in your own HTML file. Leave the TemplateFile field empty if you wish to use HTML files supplied by the system.
- Give a brief description to your report. The description will be displayed in the Report list.
- Type in your SQL SELECT statement in the SQLStatement field. This query will be executed when the report is requested.
- Put a "*" character in the Permission box if you want users of your report authenticated, otherwise leave it empty.
- Click the Add New button to save your report.
Build Procedures
A procedure is a group of SQL statements that can be executed as a whole by the server. Procedures are saved in the TableLogic table. Once defined, procedure can be referenced in HTML files as part of a URL or in HTTP requests as one of the parameters. For example, if MyProc is a defined procedure in the TableLogic table, you can use:
http://www.yourhost.com/record_browser.html?ProcedureName=MyProc
in your HTML file. MyProc will be executed and the result will be displayed in the record_browser.html file when user clicks on the link. Using procedures is a preferred way for accessing database content in HTML pages or forms. It separates user interface (HTML) from business logic. Procedures can be altered with no impact on the HTML files.
Procedures can be edited just as any other database records using tools provided by the SQLData server. To add a procedure:
- Click on TableLogic table on the left panel of your browser. TableLogic should be under the SQLDataSystem database.
- Click the Add New Button on the button of the page.. The Add New Record page should appear.
- Type in PROCEDURE for LogicType and give a unique name as the LogicName. LogicName is the name of your procedure.
- Select an ODBC data source from the dropdown list.
- Make sure the TemplateFile entry is empty.
- Give a brief description to your procedure. It is for your reference only.
- Type in your SQL statements in the SQLStatement field.
- Put a "*" character in the Permission box if you want users of your report authenticated, otherwise leave it empty.
- Click the Add New button to save your procedure.
Define Visual Presentations
With SQLData server, it is easy to change the way data is displayed. The server automatically generates HTML forms where users can add new records and update old ones. Data fields in the forms are text boxes by default. You can change the display type to radio buttons, check boxes, dropdown selections and others using the Logic Editor. The Logic Editor can be accessed from the Record Editor page where a database record is displayed. Clicking on the small image right after the field name will invoke the Logic Editor.
Using Radio Buttons
Radio buttons give use a limited number of choices and user can select one and only one from them. The actual values stored in database are often integers, but it could be other data types. To use radio button for a database field:
- Click on the small image after the field name when one of the data records is displayed in the Record Editor.
- Select RadioButton as the Visual Type on the Logic Editor Page.
- Enter available choices, separated by the '+' character, in the SelectString entry. The string will be displayed to user.
- Enter a list of values, separated by the '+' character, in the SelectValue entry. These are values to be stored in the database.
- Click the update button.
Using Check Boxes
Check boxes allows user to select multiple entries from a list of choices. The procedure of using check boxes is very similar to that of radio buttons except (1) The Visual Type should be CheckBox, (2) You should check the Selection box in the Conversion field. The server combines multiple values submitted into a single value before writing to the database.
The data type of the database field must be numerical for using check boxes.
Using Dropdown Lists
Dropdown list allows user to select one from many different choices. The procedure of using dropdown list is the same as using radio buttons except the Visual Type should be Dropdown.
Using Value Lists
A value list is a dropdown list with values come from a database field. The server uses an SQL select statement in the SELECTSTRING field to populate the list automatically.
The following is a procedure for defining a value list:
- Click on the small image after the field name when one of the data records is displayed in the Record Editor.
- Select ValueList as the Visual Type on the Logic Editor Page.
- Enter an SQL select statement as the SelectString. The server uses this query statement to fill the list. Use the DISTINCT keyword in your query statement to make sure there is no duplicates.
- Leave the SelectValue field empty. User selected value from the dropdown list will be sent to the server.
- Click the update button.
Using Value Maps
A value map is a dropdown list with values come from another database field. However, the values displayed in the list are different from the actual values for the field. For instance, suppose you have a Classes table with a department ID as a foreign key to the Departments table. So, you want to display the name of the departments in the Classes table but to save the department Id when user makes a selection. In other words, Value Maps allows you to store different values from what users have selected.
The procedure for using Value Maps is very similar to that of Value List except the following differences:
- The Visual Type should be ValueMap.
- There should be at least two fields in the SELECT clause of your query statement. The first field contains the actual values for the database, and the rest for displaying in the dropdown list. Using the example above, you may enter,
select DeptID, DepartmentName from Departments
as the query statement. The names of departments are displayed in the dropdown list. DeptID is hidden from users, but it is sent to the server when users select a corresponding name.
Logic Processing Pipeline (LPP)
There are many situations where a user request needs to be processed in several stages, and the stages are related to each other. To deal with such complicated situations, SQLData server offers a Logic Processing Pipeline. It allows you to define your business logic as stages and link them together to perform complicate tasks. For example, in an e-commerce system, the application server may need to process payments, deduct inventory, send order notification and display confirmation once an order is finalized. Each of the stages can be viewed as a processing unit and the status of one stage has influences to the subsequent stages.
There are several advantages of using Logic Processing Pipeline:
- Since the business logic is further divided into smaller unit. It is much easier to deal with complex business requirements.
- Stages can be shared by multiple pipelines and, thus reduce redundancy.
- Flow control is much more flexible than traditional method. As we will demonstrate later, processing status of current stage can be used to determine what to do next.
- Business logic is further extracted out of other part of the system design. Logic Processing Pipeline provides a central depository for not only business logic, but also control logic.
LPP Structure
Stages in LPP are stored in a database table called PIPELINE in the SQLDataSystem database. Each stage is a record in the table. The table is defined as follows:
- Stage
: The name of the stage. It should be unique within each group.
- GroupName
: The name of the group. A group defines the scope of the stage. A group contains multiple stages. It can be any logic division of your business logic. For instance, a group name may be a store name for a multi-store e-commerce application.
- SQLProcedure
: The name of the SQL procedure associated with the stage.
- Action
: An operation to be performed by SQLData server. It can be any of the followings:
ShowFile: Display an HTML file specified in the Template column.
Insert: Processing the HTML form and insert the data into a table.
Delete: Delete a record from a table.
Update: Update a record using data in the request.
- Processors
: The name of functions in a custom-supplied processing component. This is where you may plug-in your own DLL. More than one function may be entered separated by semicolon. Processors are executed after operations specified in Action if any.
- Template
: The template file to be processed by the server. The result HTML file will be sent to users.
- Params
: Additional parameters to be added to user requests. For instance, you may add an ODBC data source and UserName as parameters
DataSource=MyDatabase;UserName=Tiger
Parameters are name-value pairs separated by semicolon.
- SessionVariables
: Name-value pairs to be added into a user session. The variables can be accessed across different requests from the same user.
- ErrorString
: An error description to be sent to user if the stage failed.
- Description
: A string description of the stage.
- OnError
: The name of a stage to be executed if an error occurs while processing this one. It can also be an HTML file name that will be sent to users when the stage failed. You can use a special string "ignore" to indicate that the error is insignificant. The server will ignore any errors in the stage and proceed to the stages defined in OnSuccess no matter what happens.
- OnSuccess
: The name of a stage to be executed when the current stage is successful. It can also be an HTML file name to be sent to user if there is no error.
- OnEmpty
: The name of a stage to be run when the SQL statement in SQLProcedure returns no records.
Stage Control
Using the Logic Processing Pipeline, you can easily link multiple stages together to form a complete processing unit. The next stage to be executed is defined by OnSuccess, OnError and OnEmpty based on the current processing status.
Using LPP
LPP is an external component that resides in the dynamic linked library SHOPPER.DLL. The processor requires two parameters, the stage name and the group name in order to process a stage. It can be invoked using an URL:
http://www.yourhost.com/shopper.dll?Stage=MyStage&GroupName=MyGroup
Where MyStage and MyGroup is the name of your stage and group respectively. You may also post data to the processor using HTML forms similar to:
<form method="post" action=shopper.dll>
<input type=hidden name="Stage" value="MyStage">
<input type=hidden name="GroupName" value="MyGroup">
the rest of your form elements goes here…
</form>
Note that the stage and group are passed as hidden parameters in the form.
An Example
Our e-Bridge Server, an e-Commerce server, uses the Logic Processing Pipeline extensively. You can download the server at
http://www.sqldata.com/download/ebrdige20.exe and examine the Pipeline table in the Merchant.MDB file under the DATA subdirectory. You may also using the SQLData Desktop server to display the pipeline structure under MyStore data source once the e-Bridge server is installed. The logic-processing pipeline contains many stages including order processing, inventory deduction, mail notification, shipping/handing and customer registration.
Dynamic SQL Statements
There are many situations in which the parameters of SQL statements are unknown or the search criteria for queries are specified at runtime. SQLData Server allows you to create SQL statement templates and construct SQL statement based on parameters submitted in user requests.
Dynamic SQL statements are SQL templates in which special tokens are used for representing information to be supplied at runtime. For instance, if you allow user to search customers by country and the name of the country is supplied using an HTML form, you may use a dynamic SQL:
select * from customer where country = '$COUNTRY'
where $COUNTRY is a variable that will be replaced by the actual value submitted by users.
Dynamic SQL can be used in procedures, reports and triggers. It can also be used in the SQLStatement parameter that passed to the SQLData server. For example, you may search customers by country using the following HTML form
<form method="get" action="record_browser.html">
<input type=hidden name="SQLStatement"
value=" select * from customer where country = '$COUNTRY'">
Country Name: <input type=text name="Country" size=16>
<input type=submit value="Search">
</form>
The dynamic SQL statement is hidden in the form. When users type in the name of a country and click the Search button, the server replaces $COUNTRY with the specified country name, and displays results in the record_browser.html file. You can build powerful reports using the technique.
Using data in HTTP requests
All data in HTTP requests can be referenced by prefixing a '$' character in the name of a value, including parameters in URLs and data in HTML forms. The following SQL statement using OrderId in the HTTP requests:
select * from orders where orderid=$OrderId
If you wish not to use OrderId when it is empty, you may use a dynamic SQL statement similar to :
select * from orders where $F(OrderId, =)
The $F token adds a condition in a SQL where clause using the specified field name (first parameter) and the operator (the second parameter). The condition is not added, however, if the OrderId field is an empty string or non-exist. The statement reduces to
select * from orders
in such a situation. The $F() token is very useful if you want to construct queries with multiple where clauses.
Two other toknes, which are used exclusively in the where-clause of a SQL statements, are $AND() and $OR(). Both of them add not only a condition in the where clause, but also a logic operator: either AND or OR. For example,
select * from orders where $F(OrderId, =) $AND(CustomerId, =)
is constructed as:
select * from order where Orderid=12 and CustomerId='GTERE'
if OrderId=12 and CustomerId="GTERE'. Similar to $F(), the values for the two token are optional. The statement reduce to
select * from order where CustomerId='GTERE'
if the OrderId string is empty.
Referencing Cookies
You may also use cookie values in the dynamic SQL statement. Suppose the server sets customer ID as cookie, you may use:
select * from customers where CustomerId='$GetCookie(CustomerId)'
The $GetCookie token requires the name of a cookie as its parameter. The token will be replaced by the value of the cookie.
Using environment settings
You can also using values defined in the server configuration file as parameters in the dynamic SQL statement. The token is
$S(SettingName)
where SettingName is the name of a variable defined in the server configuration file. It can also be the name of a session variable associated with a user (Session variable can be set easily using the SessionVariables field in the Logic Processing Pipeline).