|
||||
IntroductionThe mod_db module provides several XML-to-RDBMS accessors. In this example we will create a web-based address book application. In building the application we will explore the RDBMS accessors and also show the use of server-side XForm processing. We will also demonstrate the XRL pull runtime and show how it can be used to decouple an external web-address space from the internal implementation address space. This trailmap is a step-by-step guide. If at any time you get out of step or find things not working you can download and install the completed application using the install wizard and examine it's source code after unzipping it - you will have to perform the Database admin steps outlined below to set up your backend address database. SpecificationWe will create an address book application. The address book will store surname, firstname, address, multiple telephone numbers and multiple email addresses. The address book will provide a new entry form, search and browse. Step1: Create and configure a moduleHere we will create and configure a module to host the application. We import the mod_db, session and xrl modules which we will use in our application. We create rewrite rules which map external requests to the XRL mapper accessor and add/reconnect a session resource to every request. We create and register a SessionPolicy and links definition.
Step 2: Creating the DatabaseFor this example, to keep things simple, we will create a single table database - a better approach would be to have multiple tables for user, addresses, phone numbers, emails but this isn't a course in how to be a DBA! We will assume you have a MySQL 4.x database though it should be straigtforward to port the SQL to other RDBMS. Run these SQL statements as root in your MySQL DB. We have assumed that the database is on the same box as your NetKernel installation - if not you'll have to set the privileges and alter the JDBC config appropriately (below).
########################
#Set up AddressBook Database
#Run as user root in mysql database
########################
CREATE DATABASE IF NOT EXISTS addressbook;
########################
#Grant access to user addressbook with password changeme
#Change password to something more secure!
########################
GRANT SELECT,INSERT,UPDATE,DELETE,INDEX, ALTER,CREATE,DROP,REFERENCES
ON addressbook.* TO addressbook@localhost IDENTIFIED BY 'changeme';
GRANT SELECT,INSERT,UPDATE,DELETE,INDEX, ALTER,CREATE,DROP,REFERENCES
ON addressbook.* TO addressbook@localhost.localdomain IDENTIFIED BY 'changeme';
FLUSH PRIVILEGES;
#######################
#Set up Table
#######################
CREATE TABLE IF NOT EXISTS addressbook.entries (
id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT, PRIMARY KEY(id),
firstname VARCHAR(100),
lastname VARCHAR(100),
add1 VARCHAR(200),
add2 VARCHAR(200),
add3 VARCHAR(200),
add4 VARCHAR(200),
region VARCHAR(100),
country VARCHAR(100),
zip VARCHAR(100),
phone1 VARCHAR(100),
phone2 VARCHAR(100),
phone3 VARCHAR(100),
fax VARCHAR(100),
email1 VARCHAR(200),
email2 VARCHAR(200),
email3 VARCHAR(200),
web1 VARCHAR(200),
web2 VARCHAR(200),
notes TEXT
);
Now we need to setup the mod_db configuration file - this will be used by the RDBMS accessors for their configuration. Create a directory etc/ in the root of your module (we mapped this to the internal address space earlier). Add a file ConfigRDBMS.xml <config>
<rdbms> <jdbcDriver>com.mysql.jdbc.Driver</jdbcDriver> <jdbcConnection>jdbc:mysql://localhost/addressbook?user=addressbook&password=changeme</jdbcConnection> </rdbms> </config> Testing your DB ConnectionWe now need to test the configuration of your Database. Change the resources/index.idoc to the following... <idoc>
<seq> <instr> <type>sqlQuery</type> <operand> <sql> SHOW COLUMNS FROM entries; </sql> </operand> <target>this:response</target> </instr> <instr> <type>cast</type> <operator> <cast> <mimetype>text/xml</mimetype> </cast> </operator> <operand>this:response</operand> <target>this:response</target> </instr> </seq> </idoc> Try the test link again http://localhost:8080/addressbook/test - you should see the result set containing column descriptions in XML form. The configuration is now complete - the application is now pretty simple to build. [Part 2]
|
||||
|
© 2003,2004, 1060® Research Limited
1060 registered trademark, NetKernel trademark of 1060 Research Limited
|
||||