Perlshop 4 - PSDBI Documentation

Contents:



Overview

Perlshop was originally designed to expect each catalog page to be a static html file. Individual html files need to be modified whenever item prices or descriptions need to be changed. While this works fine for a business with a small inventory, businesses with medium to large size inventories rapidly find themselves spending large amounts of time on web page update and maintenance.

The ability to keep all of your product information in a central database is a tremendous advantage. The web pages draw their information directly from this database, eliminating the need for day to day alterations of the web page catalog files. The only time you'll need to alter your html code now is if you are making cosmetic changes to your web site, or are adding wholly new pages to your store.

The Perlshop 4 Database Interface (PSDBI) provides the linkage between your central database and your shopping cart software.



Basic Database Concepts, and an Example

Any relational database (Oracle, mySQL, SQL Server, etc.) is based upon the concept of tables. Each table is made up of rows and columns. Each row in a given table represents an object, and each column represents a piece of information about that object. A relational database can have any number of tables, with each table having a unique name.

One use for a table in a database could be to represent the inventory of a given business. For example, a typical item for sale in a typical store might have the following attributes:

Let's say we have a store named Wally's Widget World. Wally sells home made widgets of various kinds, and uses a database table to keep track of his inventory:

Inventory Table
Part Number Price Type Description Color Weight
1001 12.00 Basic Wally's Famous Basic Widget Red 1.0
1002 12.00 Basic Wally's Famous Basic Widget Blue 1.0
1003 12.00 Basic Wally's Famous Basic Widget Green 1.0
2001 15.00 Complex Wally's Amazing Complex Widget Blue 1.5
2002 15.00 Complex Wally's Amazing Complex Widget Green 1.5
3001 16.00 Super Wally's Astonishing Super Widget Racing Black 2.2

Wally has his web site catalog broken up into three web pages, with one page for Basic Widgets, a second page for Complex Widgets, and a third page for his Super Widget. With only six items in stock, and only three pages of html to worry about, Wally could simply alter his html files each time he wants to change a price or add a color.

The thing is, Wally is a lazy man. Instead of doing the work himself, Wally wants to have his Basic Widgets web page talk with his database. Wally wants that web page to ask the database "Hey, Database, give me all the data you've got on all your Basic Widgets". The web page will then display all of the data that Wally has designed it to display, using the information resulting from the database query. All of Wally's web pages will work this way.

Now, if Wally changes a price, or a description, or even adds a new Basic Widget to his inventory, he never has to change the html file. Thanks to the Perlshop Database Interface, all of the work is done automatically by software.


Technical Stuff : Structured Query Language (SQL)

All relational database stuff works around a simple query language called SQL (Structured Query Language). Some people pronounce this as "Ess Kyu Ell", other folks call it "Sequel", and some call it "Squeel". SQL has been around for a long time now, and is used in common by all relational database makers.

In order to design Perlshop catalog web pages that use the PSDBI package, you need to understand the basic SQL command that is used to query a database. This command is called a select statement, and is very simple to use.

The Select Statement is used to retrieve information from a database table. Some example queries might be:

A basic select statement has the following syntax:

select [columns] from [table name] where [conditions] 
where [table name] is the name of the table being queried, [columns] is a list of one or more column names from that table, and [conditions] is a list of conditions used to select specific rows from the table. If all rows are desired, then the where clause can be left out. If all columns are needed, the '*' symbol can be used as a shorthand way to say 'all columns'.

When you execute an SQL query, you get back all of the rows in the table that match your request.

This example query will retrieve all columns from all rows of the table named 'inventory':

select * from inventory

This example query will retrieve all columns from the inventory table, from all rows that have a color value of 'Blue':

select * from inventory where color='Blue'

This example query will retrieve the part number and price from the inventory table, from all rows the have a price less than $20:

select part_number,price from inventory where price < 20

This last example query will retrive the part number, description, and price from the inventory table, selecting only those rows that have a red color and a price under $15:

select part_number,description,price from inventory
where (color='Red') and (price < 15)



Using SQL in a Perlshop Catalog Page

OK, so now we get to the simple stuff. Let's use Wally's Simple Widgets page as an example. In this page, Wally needs to query the database for anything in the inventory that has a type value of 'Basic'. Wally can do this using the following SQL query:
select * from inventory where type='Basic'

But how does Wally put this query inside his Basic Widgets web page? This is where the Perlshop <PSDBI> tag comes into play.


The <PSDBI> Query Tag

In order for a web page to use the PSDBI software, it has to use the <PSDBI> query tag. This tag can be placed anywhere in the html file, but for clarity should be placed inside the <head> tag.

The <PSDBI> query tag works as follows:

<psdbi command=query>
select * from inventory where type='Basic'
</psdbi>

This entire command is placed inside the <head> element of the web page, resulting in something like this:

<html>
<head>
<title>Wally's Basic Widgets</title>

<psdbi command=query>
select * from inventory where type='Basic'
</psdbi>

</head>

<body>
...
...
...

When this web page is loaded by Perlshop 4, the PSDBI package is called to handle the <PSDBI> query tag. The SQL query is analysed and recorded for later use. The <PSDBI> tag and its query are then removed from the html stream, so that the browser does not become confused by an unknown html tag.


The <PSDBI> Result Tag

Now that Wally's web page knows how to make an SQL database query, he needs a way for the resulting data to be displayed within the web page. This is where the <PSDBI> result tag is used.

The <PSDBI> result tag is placed inside the body of the web page. It is placed wherever the results of the database query are to be displayed. The result tag itself works as follows:

<psdbi command=result>
Some HTML goes here
</psdbi>

The HTML within the result tag is reproduced one time for each table row returned by the <PSDBI> query tag. The column values returned by the query are inserted into the HTML at specifically marked locations.

For example, let's use the query from the example above:

select * from inventory where type='Basic'
Wally wants his web page to display the items he has for sale as a simple table looking something like this:

Description Color Price Quantity
Item Description Here Some color The price
Other Description Here Some color The price

To do this, Wally uses the following <PSDBI> result tag (let's leave the Perlshop specific tags out of this example):

<psdbi command=result>
<tr>
<td>!description!</td>
<td>!color!</td>
<td>!price!</td>
<td><input type=text value=0 size=4></td>
</tr>
</psdbi>

The HTML content of the result tag contains substitution variables that are marked out with surrounding '!' symbols. Each substitution variable is named after a column from the table being queried. When the web page is loaded by Perlshop, the PSDBI package extracts the PSDBI result tag HTML, executes the associated SQL query, and replaces the PSDBI substitution variables with the actual values from the database. This process is repeated one time for each item returned by the SQL query.



Advanced Topics


Using Multiple Database Queries on the Same Page

It may be sometimes useful to create a PSDBI catalog page that contains multiple database queries. This can be useful in situations where the database contains multiple tables, or where the format of the page requires that each item from a given database table be handled separately.

In order to keep each of the queries in a given page distinct, each PSDBI query command is given a unique identifier. This identifier is then used in each PSDBI result tag intended to work with that query. The 'qid' attribute of the PSDBI tag is used for this purpose.

For example, let us have a catalog page in which all of the 'Basic' type items are handled differently from the 'Complex' type items:

<psdbi command=query qid=basic> select * from inventory where type='Basic' </psdbi>

<psdbi command=query qid=complex> select * from inventory where type='Complex' </psdbi>

<psdbi command=result qid=basic> HTML displaying the Basic type items goes here The inventory code for this item is !basic.code!. </psdbi>

<psdbi command=result qid=complex> HTML displaying the Complex type items goes here The description for this item is !complex.description!. </psdbi>

Note that in these examples, the substitution variable names are composed of both the query name and the database column name (!queryname.columnname!).


Nested Database Queries

In complex situations, it can be useful for the result of one query to be used as the input for a second query. To show how this can be useful, let us use an example where the database is made up of two tables. In this example, every item type in the inventory has a unique part number. Each unique part number is available in a variety of colors. (For Perlshop, the part number would be the ITEM_ID tag value, and the color would be the ITEM_OPTION1 tag value.)

The first table, named 'product', contains information about each product, including part number, description, price, etc., but contains no color information. The second table, named 'colors', contains nothing more than the colors available for each part number.

Using two tables in the fashion is complex, but removes the need for the duplicated information that would be needed if only a single table were used.

Example:

<psdbi command=query qid=product>
select * from inventory where type='Basic'
</psdbi>

<psdbi command=result qid=product> Product information for !product.description!.

<psdbi1 command=query qid=colors> select * from color_table where code=!product.code! </psdbi1>

Available colors are: <psdbi1 command=result qid=colors> !colors.color! </psdbi1>

</psdbi>

Note that the tags used for the 'colors' query are named psdbi1, and not psdbi. This is necessary to ensure proper order of evaluation of the query and result tags.

There is no restriction on the depth of psdbiX tag nesting.
Psdbix tag nesting levels must use consecutive numbers.


The <PSDBI> Execute Tag

The PSDBI Execute tag is intended for use by experienced Perl programmers only. This tag allows raw Perl code to be placed into a web page, with the Perl code being executed as the web page is loaded by Perlshop.

The Execute tag works as is shown in this example:

<psdbi command=query qid=product> select * from inventory where type='Basic' </psdbi>

<psdbi command=result qid=product> Our !product.description! product does amazing things!<br>
<psdbi1 command=execute> { my $output; if (!product.stock! == 0) { $output = 'SOLD OUT'; } else { $output = 'Price : $!product.price!'; } } </psdbi1>

</psdbi>

The Perl code contained within each PSDBI Execute tag is treated as an independant Perl 'eval' block. When the PSDBI catalog page file is loaded, the Execute tag an contents are replaced by the return value of the Perl 'eval' statement.



A Real Live Working Wally's Widget Page

Seeing is believing, so here are two links to click on:



Adding PSDBI Catalog Pages to a Perlshop Web Site

Using the PSDBI Action

Perlshop PSDBI pages are loaded using the PSDBI action. The following example shows how this works:
<a href="!MYURL!?action=psdbi&thispage=wally.html&ORDER_ID=!ORDERID!">

Any web page loaded in this fashion will automatically be processed through the psdbi software.


Direct Loading of PSDBI Pages

It is possible to load PSDBI pages using the traditional Perlshop 'thispage' action. If this method is to be used, then the Perlshop 4 DBI catalog pages must be kept in a separate subdirectory from the normal static catalog web pages. This is done to prevent the extra server processing needed for PSDBI from being performed on every web page. This subdirectory is named psdbi, and is located under the primary catalog directory.

Using this method, PSDBI catalog pages are loaded using the same commands used for any catalog page, except that the psdbi subdirectory must be included in the file name:

<a href="!MYURL!?action=thispage&thispage=psdbi%2Fwally.html&ORDER_ID=!ORDERID!">

For this example, most people will expect the file name referenced by the thispage attribute to be 'psdbi/wally.html'. This would be incorrect, since the slash character is not allowed in a URL string. Instead, the slash must be encoded as the hexidecimal value %2F.



Database Catalog Page Templates

Catalog Page Template Overview

Catalog page templates can be used to dramatically reduce the number of individual html files required to operate a business web site. For many businesses, each of the catalog pages has the same cosmetic appearance. Each page will have unique attributes, and each will of course contain different products, but the general look and feel of each page is very often the same.

A catalog page template is a single html file that can be used to represent multiple actual web pages. The template file contains template substitution variables that work in the same fashion as normal psdbi substitution variables.

The following example shows a simple catalog page template html file. Note how the template substitution variables are used:

<html>

<head>
<title>!template.title!</title>

<link rel=stylesheet type="text/css" 
	href="http://www.waveridersystems.com/!template.style!.css">

</head>

<body>
<h1 align="center">!template.title!</h1>

<p>
!template.header!

<psdbi command=query qid=product>
select * from wally_products where type='!template.type!'
</psdbi>

<p>
Product description:<br>

<psdbi command=result qid=product>
!product.description! : !product.color!<br>
</psdbi>

<p>
!template.footer!

</body>

</html>


Using Database Driven Templates

All template information can be stored as a database table. This keeps the URL required to load a template page neat and short. Each entry in this table would be a template definition. Each definition requires a name which will serve as the identifier used to load the template page. All of the column values for that identifier would be available as template variables for the web page. The syntax for using these values would be !template.columnname!, where columnname is the name of one of the columns in the template table.

The recommended design for a catalog page template table is as follows:

Template Definition Table
Column Name Column Data Type Column Description
Name Text The name of the template
File Text The name of the html template file
Style Text The name of the style sheet to use for this template
Title Text Title text for the web page
Header Text Header text for the web page
Footer Text Footer text for the web page
..... Any As many other columns as fit the needs of your business

Database driven template pages are loaded using the TEMPLATE action. The following example demonstrates how this command would be used to load a template page named 'basic':

<a href="!MYURL!?action=template&thispage=basic&ORDER_ID=!ORDERID!">

Any web page loaded in this fashion will automatically be processed through the psdbi software.

A Template Driven Wally's Widget Web Site

Like any smart person, Wally is essentially lazy. Why build three web pages when you can build just one? Here's how Wally built all of his catalog pages using a database and a single html file.



Recommended Database Inventory Table Design

The PSDBI package will work with any set of tables in a relational database. If you've got a database already, PSDBI will allow you to tie Perlshop 4 into it without any great difficulty.

For customers just starting out with an inventory control database, the following table design is recommeneded for Perlshop 4:

Inventory Table
Column Name Column Data Type Column Description Perlshop Input Tag Equivalent
Code Text Inventory control code. ITEM_ID
Name Text Product name. ITEM_NAME
Summary Text A short description of this item. Optional.  
Description Text A full description of this item. Optional.  
Option1 Text Site dependent. Optional. ITEM_OPTION1
Option2 Text Site dependent. Optional. ITEM_OPTION2
Option3 Text Site dependent. Optional. ITEM_OPTION3
Price Number Item price. ITEM_PRICE
Weight Number Item weight, in local units. Optional. ITEM_WEIGHT
Thumbnail Text Name of thumbnail image file. Optional.  
Image Text Name of big image file. Optional.  
Stock Number Number of this item in stock. Optional.  
Vendor Number Item vendor ID code. Optional.  

The following SQL table creation command can be used to create this table inside your database:


create table inventory
(
	code            varchar(20),
	name            varchar(40),
	summary         varchar(40),
	description     varchar(255),
	option1         varchar(20),
	option2         varchar(20),
	option3         varchar(20),
	price           float(10,2),
	weight          float(10,2),
	thumbnail	varchar(20),
	image		varchar(20),
	stock           int,
	vendor          int
);