Blog: Posts from May, 2012

Labels
AJAX(112) App Studio(7) Apple(1) Application Builder(245) Application Factory(207) ASP.NET(95) ASP.NET 3.5(45) ASP.NET Code Generator(72) ASP.NET Membership(28) Azure(18) Barcode(2) Barcodes(3) BLOB(18) Business Rules(1) Business Rules/Logic(140) BYOD(13) Caching(2) Calendar(5) Charts(29) Cloud(14) Cloud On Time(2) Cloud On Time for Windows 7(2) Code Generator(54) Collaboration(11) command line(1) Conflict Detection(1) Content Management System(12) COT Tools for Excel(26) CRUD(1) Custom Actions(1) Data Aquarium Framework(122) Data Sheet(9) Data Sources(22) Database Lookups(50) Deployment(22) Designer(177) Device(1) DotNetNuke(12) EASE(20) Email(6) Features(101) Firebird(1) Form Builder(14) Globalization and Localization(6) How To(1) Hypermedia(2) Inline Editing(1) Installation(5) JavaScript(20) Kiosk(1) Low Code(3) Mac(1) Many-To-Many(4) Maps(6) Master/Detail(36) Microservices(4) Mobile(63) Mode Builder(3) Model Builder(3) MySQL(10) Native Apps(5) News(18) OAuth(9) OAuth Scopes(1) OAuth2(13) Offline(20) Offline Apps(4) Offline Sync(5) Oracle(11) PKCE(2) Postgre SQL(1) PostgreSQL(2) PWA(2) QR codes(2) Rapid Application Development(5) Reading Pane(2) Release Notes(183) Reports(48) REST(29) RESTful(29) RESTful Workshop(15) RFID tags(1) SaaS(7) Security(81) SharePoint(12) SPA(6) SQL Anywhere(3) SQL Server(26) SSO(1) Stored Procedure(4) Teamwork(15) Tips and Tricks(87) Tools for Excel(2) Touch UI(93) Transactions(5) Tutorials(183) Universal Windows Platform(3) User Interface(338) Video Tutorial(37) Web 2.0(100) Web App Generator(101) Web Application Generator(607) Web Form Builder(40) Web.Config(9) Workflow(28)
Archive
Blog
Posts from May, 2012
Thursday, May 3, 2012PrintSubscribe
Adding Output Cache Table to a Project

It is easy to add a data controller for the output cache table to an existing project.

Start the application generator, select the project name and choose Refresh action. Toggle the check box next to the table dbo.CustOrderHist_Cache and click Refresh button. Click Yes to confirm the refresh action.

Adding a table to a web application created with Code On Time generator

Select Design option on the project Summary Page of the application generator.

Activate Controllers tab in Project Explorer, and right-click the CustOrderHist_Cache / Business Rules node. Chose the  New Business Rule option.

Adding a declarative business rule to a data controller in a Code On Time web application

Enter the following business rule properties and click OK button.

Property Value
Command Name Select
View grid1
Type SQL
Phase Before
Script Paste the  output caching script in its entirety.

Note that the output caching script includes a section declaring parameters and their values that can be used for debugging.

The application framework will automatically remove
the script lines between Debug and End Debug.

That way you can develop a script using SQL Management Studio or any other SQL development tool compatible with your database engine. The entire script can be moved unchanged between the development tool and the Project Designer.

The application framework automatically recognizes and declares parameters and provides their values if a parameter name starts with “@BusinessRules_” or represents the name of the data controller field with optional suffixes “_NewValue”, “_OldValue”, “_Value”, “_FilterValue”, and “_FilterOperation”. All other parameters must be declared explicitly in the Script property of an SQL Business Rule or in the Data property of SQL action.

Expand the CustOrderHist_Cache / Views / grid1 view node, right-click the data field CacheUserName . Choose Delete option in context menu to remove the data field. Repeat this procedure for the field CacheExpires.

Deleteing a data field in a Code On Time web application project

Select the CustOrderHist_Cache / Views / grid1 view node and change its properties as follows. Click OK button to save the view properties.

Property New Value
Filter Expression
CacheUserName = @BusinessRules_UserName

View of the customized output cache controller 'CustOrderHist_Cache' in Project Explorer

Click Browse button on the designer tool bar to generate the project. The default web browser window will open.

Application generator has created a dedicated page for the data controller CustOrderHist_Cache when the project had been refreshed. If you have not customized any pages in the Northwind sample then the menu option to access the dedicated page is Region / Cust Order Hist Cache. Otherwise you will find it under New Pages / Cust Order Hist Cache.

Navigating to the dedicated page of the output cache table 'CustOrderHist_Cache' in the Northwind project

Navigate to the page and you will see the standard grid view of the output cache table data.

The view of the dedicated page of the output cache table 'CustOrderHist_Cache' in the Northwind project

There will be no records.

Wednesday, May 2, 2012PrintSubscribe
Implementing Caching of Stored Procedure Output

Now it is time to design a caching algorithm that will execute the stored procedure CustOrderHist and store the custom result set to the output cache table with the name CustOrderHist_Cache.

The framework of a Code On Time web application minimizes interactions with the database. For example, if a user has arrived to a page, then the application will count the total number of  rows in the visible data views to figure the number of pages presented in the view pager. Then the application will retrieve the first page of visible data rows. The framework will not try to figure the number of pages again until a filtering condition is changed or data has been modified.

Our objective is to minimize the calls to the stored procedure. The described behavior of the web application framework provides us a very convenient condition for execution of the stored procedure.

The stored procedure will be executed when the application
needs to count the number of available data rows.

It is necessary to populate the stored procedure output cache table only when there is no content or the content has expired. The expiration will be set to 30 seconds.

The following algorithm is written in T-SQL and can be adapted to other dialects of SQL supported by the database server you may be working with.

/*********************************************************
 The application framework will delete the "Debug" section
 from the business rule. The parameter values will be 
 provided by the application framework at runtime.
*********************************************************/

-- Business Rule Parameters: Debug
declare @BusinessRules_RequiresRowCount int
declare @BusinessRules_UserName nvarchar(40)
declare @CustomerID_FilterValue nvarchar(5)

set @BusinessRules_RequiresRowCount = 1
set @BusinessRules_UserName = 'username'
set @CustomerID_FilterValue = 'ANTON'
-- Business Rule Parameters: End Debug

/*********************************************************
 This is the actual business rule implementation 
*********************************************************/

if @BusinessRules_RequiresRowCount = 1 
    and @CustomerID_FilterValue is not null
    and not exists (
        select 1 from CustOrderHist_Cache
        where CacheUserName = @BusinessRules_UserName and 
            CustomerID = @CustomerID_FilterValue and
            CacheExpires > getdate()
    )
begin
    -- delete the data cached by the current user if it has expired.
    delete from CustOrderHist_Cache
    where CacheUserName = @BusinessRules_UserName and 
        CustomerID = @CustomerID_FilterValue
    -- create temporary table to capture the custom result set
    create table #CustOrderHist(
        ProductName nvarchar(50),
        Total int
    )
    -- execute the stored procedure and capture the custom result set
    insert into #CustOrderHist
    exec [CustOrderHist] @CustomerID_FilterValue
    -- copy the custom result set to the Output Cache Table
    insert into CustOrderHist_Cache(
        ProductName, Total, CustomerID, 
        CacheID, CacheUserName, CacheExpires)
    select 
        ProductName,                    -- column from #CustOrderHist
        Total,                          -- column from #CustOrderHist
        @CustomerID_FilterValue,        -- stored procedure parameter
        newid(),                        -- generic primary key value
        @BusinessRules_UserName,        -- the application user name
        dateadd(second, 30, getdate())  -- cache expiration date
    from #CustOrderHist
    -- drop the temporary table with the custom result set
    drop table #CustOrderHist
end

Execute the script in SQL Management Studio. This is the output that will be produced if you select data from CustOrderHist_Cache table.

This output is produced by custom result set caching algorithm

Here is the recap of what the script will do if invoked.

  1. If the application requires a row count  and the customer ID filter is known, then the script will continue only if there are no unexpired data rows matched to the current user name and customer ID filter . The steps 2-6 will not execute if this condition is not met.
  2. The script will delete all cached records matched to the current user name and customer ID filter.
  3. The script will create  a temporary table #CustOrderHist to capture the output of the stored procedure CustOrderHist.
  4. The script will execute the stored procedure CustOrderHist with the parameter specified in @CustomerID_FilterValue and insert the output to the temporary table #CustOrderHist .
  5. The script will copy data from the temporary table to the persistent output cache table CustOrderHist_Cache and enrich the output with parameter value, user name, and expiration date.
  6. The temporary table #CustOrderHist is dropped.

If you execute the script a few times in SQL Management Studio then you will notice the status messages indicating that records are being deleted and recreated. The messages will include information about effected rows only every 30 seconds.

Tuesday, May 1, 2012PrintSubscribe
Creating a Table for Output Caching

Consider the stored procedure CustOrderHist from the Northwind sample.

CREATE PROCEDURE [dbo].[CustOrderHist] @CustomerID nchar(5)
AS
SELECT ProductName, Total=SUM(Quantity)
FROM Products P, [Order Details] OD, Orders O, Customers C
WHERE C.CustomerID = @CustomerID
AND C.CustomerID = O.CustomerID AND O.OrderID = OD.OrderID AND OD.ProductID = P.ProductID
GROUP BY ProductName

Enter the following query in SQL Management Studio to test the procedure.

EXEC [dbo].[CustOrderHist] @CustomerID = 'ANTON'

The stored procedure will produce a custom result set. The result set contains the total number of products purchased by the customer with ID of ANTON.

A custom result set produced by the stored procedure 'CustOrderHist' when executed with parameter 'ANTON'

Let’s design a table to cache the output of the stored procedure.

The columns of the output cache table will have to match the columns in the custom result set. It will help if all columns in the table allow storing a null value.

We will also include a column to capture the value of the parameter. If you have more than one parameter, then create a column for each of them. This will allow distinguishing cached result sets produced with different parameters.

You may need to incorporate a column capturing the user identity to segregate cached data produced by application users. You don’t have to implement the column if it is acceptable that data cached by any user is shared with other application users.

One more column will be required to register a cache expiration date. We will assign an expiration date to the cached data rows. This will simplify removing the “stale” data from the output cache table.

Let’s name the output cache table after the the stored procedure.

Output Cache Table captures parameters, user identity, and expiration date of each cached data row

The following script will create the output cache table with the name CustOrderHist_Cache.

CREATE TABLE [dbo].[CustOrderHist_Cache](
    [ProductName] [nvarchar](40) NULL,
    [Total] [int] NULL,
    [CustomerID] [nchar](5) NULL,
    [CacheID] [uniqueidentifier] NOT NULL PRIMARY KEY,
    [CacheUserName] [nvarchar](40) NULL,
    [CacheExpires] [datetime] NULL
) 

This is a brief description of the table columns.

Column Description Source
ProductName The name of the product purchased by customer. Custom Result Set
Total The total number of products purchased by customer. Custom Result Set
CustomerID The value of the CustomerID parameter. Stored Procedure Parameter
CacheID Generic primary key of the output cache table. Caching Algorithm
CacheUserName The name of the application user that has caused the caching of data rows. Caching Algorithm
CacheExpires The date when cached data rows need to be deleted. Caching Algorithm

The highlighted columns represent the custom result set produced by the stored procedure and its parameters.  If your own stored procedure or web service returns more columns in the output or requires more parameters, then incorporate them in the design of the table.