man DBIx::Intrors () - Embperl and DBIx::Recordset
NAME
DBIx::Intrors - Embperl and DBIx::Recordset
Embperl and DBIx::Recordset
This introduction gives an overview how to use DBIx::Recordset together with HTML::Embperl. Since there are only a few Embperl specific things herein, it should be also usefull for non Embperl users.
Overview
It is often very difficult to layout and design the output of normal CGI scripts, because you are dealing with HTML-sourcecode which spans multiple prints, and it isn't possible to use some sort of HTML-editor. Embperl takes a different approach to this problem. With Embperl, you can build your HTML-pages with any tool you like, and you can embed fragments of code directly in the page. This makes it much easier for non-programmers to use, because they are able to use their usual tools and they see the fragments of code as normal text. This indroduction will deal with the Perl Modules HTML::Embperl and DBIx::Recordset, with a focus on database access.
Embperl
In brief, the purpose of Embperl is to execute code that is embedded in HTML-pages as the page is requested from the server. There are two ways to do this with Embperl. The first way is to embed the code between [- and -] tags. This will cause Embperl to execute the code and remove it from the source before sending the page. The second way is to use [+ and +] as the delimiter, in which case the code will be executed and the result of the execution is send to the browser in place of the code. All database access is done via the module DBIx::Recordset, which simplifies a lot of common tasks when accessing a database via DBI.
Basic Example
The following example shows the basic functions of both modules. It shows the contents of a table whose name is passed as a parameter:
<body> <h1>Contents of table [+ CW$fdat{'!Table'} +]</h1>
[- use DBIx::Recordset ;
$fdat{'!DataSource'} = 'dbi:mysql:test' ; *set = DBIx::Recordset -> Search(\%fdat) ; $names = $set -> Names ; -]
<table> <tr> <th>[+ $names -> [$col] +]</th> </tr> <tr> [- $rec = $set[$row] -] <td>[+ $rec -> {$names->[$col]} +]</td> </tr> </table> </body>
To show the contents of the table CWaddress you may call it with:
http://www.domain.com/path/to/example1.htm?!Table=address
All query parameters are placed in the hash CW%fdat by Embperl. In our example, CW$fdat{'!Table'} would contain the value CWaddress. Additionally, Embperl replaces the code between CW[+ and CW+] with the result, so the headline of the page would be 'Contents of table address'.
The following [- -] block will be executed by Embperl. No trace of it will show up in the page that is sent to the browser. The first line sets the database which should be accessed. The syntax is the same as for the DBI connect call. If you omit the line, you must additionally send the databasename as a query parameter - but for security reasons, that isn't a very good idea.
Search
Next we call the method CWSearch of DBIx::Recordset, where we have the choice between the object and the class-method. This applies to a lot of other methods as well. When we call it as a class method, as we do in our example, it constructs a new DBIx::Recordset object and uses the passed parameters to query the database. It's also possible to divide these two steps and call CWSetup to first construct the object and then CWSearch with this object to execute the Search. In the example above, we do not pass any query parameters so CWSearch will return the contents of the whole table. (DBIx::Recordset converts the call internally to the SQL statement CWSELECT * FROM address).
The last line of the [- -] block retrieves the fieldnames of the table. Here we can see a special feature of DBIx::Recordset, which we will discuss in detail later on. The constructor returns a typeglob (CW*set), but the call to CWNames uses a scalar (CW$set). By returning a typeglob, DBIx::Recordset is able to return a scalar, an array and a hash at the same time. (If you don't like the idea of using typeglobs, you can also construct all three with different methods).
Display the table
At first glance, the following might appear to be a simple HTML-table. But Embperl expands it, so that the full contents of the database table is shown. Let us first look at the header, which should show the fieldnames of the database-table: CW$names contains a reference to an array which contains the fieldnames. Embperl gives us the magical variable CW$col. CW$col will be automatically incremented as long as the result of the expression which contains CW$col doesn't return undefined. At the same time, Embperl repeats the surrounding CW<th> or CW<td> tags. If we have a table with the three columns CWname, CWfirstname and CWtown, the output would look like this:
<th>name</th><th>firstname</th><th>town</th>
Now the header is ready and we can start to output the contents. Here we use the array part of the typeglob that is returned by CWSearch. Access to the results of the SQL-query is done via the array CW@set, and every row of the array contains one row of the database-table. It does not really contain the row, but DBIx::Recordset will fetch the row from the databases for you if you access the corresponding array row. The rows are stored as a hash, where the fieldnames are the hashkeys. This is the same mechanism that helped us to expand the columns of the header, but it's at work here in a two-dimensional manner. CW$row contains the row-count and CW$col contains the column-count.
Supplying query parameters
But our small example can do even more: If we supply more query parameters in our request, we can decide which parts of the table should be selected (and therefor, shown). If we request the page with
http://wwww.domain.com/path/to/example1.htm?!Table=address&town=Berlin
Embperl will not only place CW!CWTCWable in the hash CW%fdat, but also CWtown. Since CWtown corresponds to a fieldname in our table, DBIx::Recordset interprets it as a parameter for the CWWHERE part of the CWSELECT command. DBIx::Recordset will generate the following SQL-query:
SELECT * FROM address WHERE town='Berlin' ;
The programmer doesn't have to pay attention to datatypes or quoting, this is done automatically by DBIx::Recordset.
Also, complex queries are easy to implement: if, for example, the user wants to be able to search for a name or for a town, it would be possible to use the following form:
<form action="/path/to/example1.htm" method=GET > <input type=text name="+name|town"> <input type=hidden name="!Table" value="address"> <input type=submit> </form>
If the user enters Richter to the input field and presses the submit button, the following SQL-query will be generated:
SELECT * FROM address WHERE name='Richter' OR town='Richter' ;
Just by varying the parameters, it is possible to create simple or complex queries. In this way, you can use the same page with different parameters to create different sorts of queries.
Multiple tables
Until now, we only have worked with one table. In real life, you often have to deal with mulitple tables. For this reason, DBIx::Recordset helps you to reduce the expense associated with dealing with multiple tables. The simplest way to do this is to use the parameters CW!TabJoin and CW!TabRelation to tell DBIx::Recordset to create an SQL-join between two or more tables. This will link the tables together and the result looks just like one great table.
More interesting is the possibility to create links. As an example, we'll take the same table we used above and divide it into two tables: one table for the names and one table for the towns. As a link we add an id-field. If the fields are following some naming convention, DBIx::Recordset is able to find this link automatically. If fields are named in another way, you have to tell DBIx::Recordset manually how the tables belong together.
Table name: firstname, name, town_id Table town: id, town
Here, every name has exactly one town and every town has a number of names assigned. With a simple modification of our first example, we could get the same result as above (except that we are now dealing with two tables instead of one):
[- use DBIx::Recordset ; $db = DBIx::Database -> new ('dbi:mysql:test') ; $db -> TableAttr ('town', '!NameField', 'town') ;
$fdat{'!DataSource'} = $db ; $fdat{'!LinkName'} = 3 ; *set = DBIx::Recordset -> Search(\%fdat) ; $names = $set -> Names ; -]
And the request would be:
http://www.domain.com/path/to/example2.htm?!Table=name
DBIx::Database
The new thing here is the CWDBIx::Database object. It gathers meta-information about the database and stores it for later use. Because of the names of the fields the object can detect that the field CWtown_id in the table CWname points to field CWid in the table CWtown. Additionally, we tell the CWDBIx::Database object which column(s) contain the human-readable name of the table CWtown. These initialisations only have to be executed once. If you use mod_perl, for example, you should be able to move these lines into a common startup file.
Also new is the parameter CW!LinkName. It tells DBIx::Recordset to return the human-readable name (in our example, CWtown) instead of the field which links the two tables together (CWtown_id in our example). Internally, DBIx::Recordset generates an SQL-join, so there is only one CWSELECT command necessary and the result is just the same as in the last example.
Sub-Objects
But what to do if we have the CWid of a town and want to display all the names that belongs to it? An Embperl-page that does this job might look something like this:
<body> [- use DBIx::Recordset ; CW$fdat{'!DataSource'} = 'dbi:mysql:test' ; CW$fdat{'!Table'} = 'town' ; *set = DBIx::Recordset -> Search(\%fdat) ; -]
town: [+ $set{town} +]<br> <table> <tr> <th>name</th><th>firstname</th> </tr> <tr> [- $rec = $set{-name}[$row] -] <td>[+ $rec -> {name} +]</td><td>[+ $rec -> {firstname} +]</td> </tr> </table> </body>
A request to that page might look like this:
http://www.domain.com/path/to/example3.htm?id=5
In this example, we specify the name of the table directly inside the page, so it can't be overwritten from outside. The call to CWSearch returns the town for the given query parameters. In our example, it will select the town with the CWid 5. The command CW[+ $set{town} +] shows the value of the field town in the current record. After the call to CWSearch, this is the first selected record. Next, we need to display all the names. This is very easy using the special field CW-name. CW-name contains a sub-object for the table CWname. The query parameters for this sub-object are set by DBIx::Recordset in such a way that it contains all names which meet the link-condition. We just wrap it in a table and we are already done.
Modify the Database
Up to this point, we have only discussed the retrieval and display of data. But of course it's also possible to modify data. The simplest way to do this is to assign new values to the result of a CWSearch call. For example, you may write CW$set{town} = 'Frankfurt' to change the name of the town. DBIx::Recordset converts this into a vaild SQL-Update-command.
While this is very useful in normal Perl scripts, you probably won't use it very often in a cgi script. The methods CWInsert/CWUpdate/CWDelete will probably prove more useful. Just like CWSearch, these directly accept query parameters posted to the page. The method CWExecute combines all four of these together, making it possible to control the type of action via the CGI-parameters.
Here is an example:
<html> <head> <title>Database Access with HTML::Embperl and DBIx::Recordset</title> </head> <body>
[- ### Database-parameter ### use DBIx::Recordset ; $fdat{'!DataSource'} = 'dbi:mysql:test' ; $fdat{'!Table'} ||= 'town' ; $fdat{'!PrimKey'} = 'id' ; $fdat{'$max'} = 10 ;
### Execute action according to the query parameters ### *set = DBIx::Recordset -> Execute (\%fdat) ; -]
[$if $DBI::errstr $] <h1>Database Error [+ $DBI::errstr +]</h1> [$else$] [-$names = $set -> AllNames ; -] [$if $set[0] && $set -> MoreRecords $] [### We found more then one record ###] [### -> display as a table ###] <table> <tr> [### Display header ###] <th>[+ ucfirst ($names -> [$col]) +]</th> </tr> <tr> [### Display record -> Table will be expanded by Embperl ###] [- $rec = $set[$row] -] <td> [- $name = $names -> [$col] -] [$if $name eq $fdat{'!PrimKey'} $] [### Generate HTML link to edit this record ###] <a href="example4.htm?!Table=[+ $fdat{'!Table'} +]&[+ $fdat{'!PrimKey'} +]=[+ $rec ->{$fdat{'!PrimKey'}} +]">[+ $rec -> {$name} +]</a> [$elsif $set -> Link4Field($name) $] [### Link to other table -> generate HTML link ###] [- $link = $set -> Link($set -> Link4Field($name)) -] <a href="example4.htm?!Table=[+ $link -> {'!Table'} +]&[+ $link -> {'!LinkedField'} +]=[+ $rec -> {$link -> {'!MainField'}} +]">[+$rec -> {$name}+]</a> [$else$] [### Display contents of field ###] [+ $rec -> {$names->[$col]} +] [$endif$] </td> </tr> </table> [+ $set -> PrevNextForm ('\<\<Prev', 'Next\>\>', \%fdat) +] <hr> <a href="example4.htm?!Table=[+ $fdat{'!Table'} +]&%3dempty=1">Search</a> record in table '[+ $fdat{'!Table'} +]' [$else$] [### We found no/one record(s) ###] [### -> Display form ###] <form> <table> <tr> [- $name = $names -> [$row] -] <td> [### Display fieldname ###] [+ ucfirst ($name) +] </td> <td> [### Display content of field ###] <input type=text name="[+ $name +]" value="[+ $set{$name} +]"> [$if $set -> Link4Field($name) $] [### Link to other table -> generate HTML link ###] [- $link = $set -> Link($set -> Link4Field($name)) -] <a href="example4.htm?!Table=[+ $link -> {'!Table'} +]&[+ $link -> {'!LinkedField'} +]=[+ $set{$link -> {'!MainField'}} +]">Show record from table '[+ $link -> {'!Table'} +]'</a> [$endif$] </td> </tr> </table> [### Buttons for the different actions, the "name" attribute determinates ###] [### which action should be taken ###] <input type=submit name="=search" value="Search"> <input type=submit name="=empty" value="New"> <input type=submit name="=insert" value="Add"> <input type=submit name="=update" value="Update"> <input type=submit name="=delete" value="Delete"> <input type=hidden name="!Table" value="[+ $fdat{'!Table'} +]"> </form> [$endif$] [$endif$] </body> </html>
When you first request this page, it will show the contents of the preset table. Alternatively, you can supply a tablename with the parameter CW!TCWable. The link, which is shown at the bottom of the page, leads you to an imput form. There, you can fill in one or more fields and press the Search button. This invokes the page itself and CWExecute will be instructed by the parameter CW=sCWearch (Name of the button Search) to retrieve all records which match the entered values.
If the query finds more then one record, a table with all records found will be shown. If there are more records than specified by the parameter CW$max, only CW$max records are displayed. If this is the case, the CWPrevNextForm method adds a Previous and a Next button to the page, allowing you to browse through the whole table. In the example above, we assume that every table has a primary key, which is passed to DBIx::Recordset by the line CW$fdat{'!PrimKey'} = 'id' ;. The column which contains this primary key will be displayed as an HTML link containing the parameters to execute a search for just this record. As you can see in example4.htm, this can be used to display a form which includes some of the data from the found record (see below). Columns which are links to other tables will also be shown with an HTML-link. A click on that link will open the linked table or record.
If the search only selects one record, the same form is shown, but with the data from the record filled in. Now it's possible to change the content. The changes are written to the database when you press the button Update (parameter CW=update). A new, empty form could be shown with the button New (parameter CW=empty) and if you have written data into this empty form, you can add it as a new record with the Add button (parameter CW=insert). Last but not least, there is a Delete button (parameter CW=delete). In all of these cases, the content of the form is sent to the page itself, and the CWExecute method at the start of the page executes the desired action.
More comments can be found inside the source ([# #] blocks).