TPDA | Tutorial - Creating a simple application

Home | Tutorial | Download | Summary | Support

Table of Contents

1 Introduction

This is a hands-on tutorial for creating a simple one screen application with the (experimental) tools provided with TPDA.

The RDBMS used can be Firebird, PostgreSQL or MySQL it's your choice.

The tools have a command line interface, and can be used, on GNU/Linux and Windows1, as long as you can install the necessary modules especially the DBD modules needed by the RDBMS you wish to work with.

Let's make a (tiny) database application to keep records for all the computers we have (or wish we have). This can also be useful, if you, like me, can't remember too long what hardware components are inside of the computers you own. And with different columns in the table can even be used by network administrators to keep records for every computer on the network they administer, or even other hardware. This may be a idea of a future new TPDA application (if anybody thinks it's worth the effort).

DISCLAIMER

I can't be held responsible for any damage that may occur by installing or using this software and it's components or by following this tutorial. Special care must be taken not to interfere with any database and/or Perl installation that is used for production. Read also the disclaimer in the license.

2 Prerequisites

You need basic knowledge of Perl, like installing a module and running an application, and of course basic knowledge of how to create a database and a table, grant user access, and know what XML is.

This tutorial assumes that you have installed, configured and tested TPDA version 0.75, at least with the classicmodels application, if not than install TPDA first, unpack and read the instructions in tpda/doc/INSTALL.

3 The application

Download tutorial support files2 as zip file or gzipped tar archive. These archives contain the SQL commands for creating the table, the configuration file for a new TPDA application and a finished screen module.

computers/
|-- ComputersH.pm
|-- conf
|   |-- computers-fb.xml
|   |-- computers-my.xml
|   `-- computers-pg.xml
`-- sql
    |-- computers-fb.sql
    |-- computers-my.sql
    `-- computers-pg.sql

Unpack in the tpda-apps directory in your HOME directory3, if there isn't one already :) then create it and make a .tpda-apps (or _tpda-apps on Windows) empty file in it, so the install-apps.pl script can recognize it as a TPDA applications directory.

tpda-apps
|-- .tpda-apps
`-- computers
    ...

Edit conf/computers-xx.xml and change the Server name and/or Port if needed (localhost is a valid choice to, or even the IP). Replace xx with fb for Firebird, pg for PostgreSQL or my for MySQL. The file name with fb, pg or my in it is just a convention I use, to make distinction between configurations, the name (ID) for the application or the configuration file can be anything you wish.

Next we configure TPDA to recognize and setup the new application(s) from tpda-apps. Change directory to tpda (where TPDA is installed, defaults to HOME and contains tpda.pl, TPDA.pm and install-apps.pl).

On GNU/Linux:

% cd

% cd tpda

% perl install-apps.pl /home/username/tpda-apps/

and the script should return:
  + computers
Done.

On Windows with the same result:

% c:

% cd C:\Documents and Settings\username\tpda

% perl install-apps.pl ..\tpda-apps

Just to be sure that TPDA can find the applications configuration files:

% perl tpda.pl -l

Applications list:
 id: [ computers      ] computers

3.1 The database

Create a new database named computers with your favorite tool. Special care must be taken not to interfere with any database and/or Perl or TPDA ;) installation that is used for production.

3.1.1 The table

The table structure and SQL command is the same for all three RDBMS's of choice. The differences came from the mechanism used to generate new primary keys.

CREATE TABLE computers (
      id_comp    INTEGER NOT NULL
    , user_name  VARCHAR(25)  -- the user of the computer
    , net_ip     VARCHAR(20)  -- IP
    , acq_date   DATE         -- acquisition date
    , processor  VARCHAR(100) -- processor type
    , ram        VARCHAR(100) -- RAM type and amount
    , hdd        VARCHAR(100) -- HDD vendor and capacity
    , os         VARCHAR(100) -- operating system(s)
    , graphics   VARCHAR(100) -- graphics_card
    , units      VARCHAR(100) -- other units
    , monitor    VARCHAR(100) -- the type of the monitor
    , fdd        SMALLINT     -- has a FDD?
    , comments   VARCHAR(100)
    , CONSTRAINT pk_computers_id_comp PRIMARY KEY (id_comp)
);

Note: Minimum requirements regarding the table structure: the table must have a primary key of type integer so we can generate it automagicaly, and at least one other column, so we can store something useful in it. The column names and types can be changed with no other restrictions as you like.

For Firebird create a generator:

CREATE GENERATOR g_computers_id_comp;

For PostgreSQL, a sequence:

CREATE SEQUENCE g_computers_id_comp;
ALTER SEQUENCE g_computers_id_comp OWNED BY computers.id_comp;

For MySQL, a simulated sequence:

CREATE TABLE g_computers_id_comp (id INTEGER NOT NULL);
INSERT INTO  g_computers_id_comp VALUES (0);

We don't have any code or computed columns in the table, so we don't need a view. I usually use VIEWS for displaying information in the screen for columns that belong to other referenced tables, with the help of SQL joins.

3.2 The screen

3.2.1 Generate an XML template file

First step is to generate a XML template file with column information from the table. The following command will (hopefully) do that for us with little effort:

% cd tpda
% perl tools/tpda-gen_xml.pl -c ../tpda-apps/computers/conf/computers-xx.xml \
     -u username -p password -t computers

Replace username and password with your login information and xx with the appropriate string (fb|pg|my) depending on the database you use.

Here -t | -table is the option for the table name to use for the new screen and -c | -config is the option for the path and file name of the configuration to be used. The config file, among other things, contains the settings of the connection to the RDBMS to be used.

3.2.2 Generate the screen

Next based on the setting from the XML file we just have created, we can make a basic screen and later modify it as we like so we don't need to start from scratch.

% perl tools/tpda-gen_scr.pl -f computers.xml

3.2.3 Test the screen

We do a quick test of the screen to see how it looks like, with another tool in our toolbox:

% perl tools/tpda-test_scr.pl -s Computers.pm

Press the first toolbar button to load the screen.

imagine1

The screen as generated by the script.

3.2.4 Customize the screen

The generated screen can be used as is but we can make some small customizations, like changing the labels of the entries, reorder the entries or even split the frame in smaller frames, all this just by editing the XML file. Of course, we need to regenerate the screen every time the XML changes.

Let's experiment a little.

<metadata>
  <topwin name="inreg_p" campid="id_comp">
    <frame label="Computers at home" name="frame1" gridpos="0,0">
      <entry pos="0" label="ID" name="id_comp" ...
      <entry pos="2" label="IP" name="ip_net" ...
      ...
    </frame>
    <frame label="Test frame" name="frame2" gridpos="1,0">
      <entry pos="10" label="Monitor" name="monitor" ...
      ...
    </frame>
  </topwin>
</metadata>

If we split the frame we have to set gridpos, first is the row setting and second the column. If we want the second frame bellow the first set gridpos to 1,0, if you want it near on the right set to 0,1.

The pos elements in the XML file refer to the position of the column in the table schema, don't bother to renumber when moving a column to another position, the screen generator ignores them.

Don't change topwin element name setting, it's hard-coded in the application.

imagine2

The screen generated after the XML file editing.

For the final look, moved the fdd entry near hdd an made it into a Check button. The Entry objects hashref eobj_rec must be updated to know that fdd is a Check button.

fdd => [ 'c','w',\$vfdd, $efdd, 'normal', $bg, undef, 1 ],

Also moved user_name near comp_id and acq_date near the ip_net entry.

3.2.5 Install the screen

Last step is to install the screen in the Computers application. Rename the perl module file from Computer.pm to ComputersH.pm and move it to the computers path. Edit ComputersH.pm and change the package name to ComputersH.

Here is the configuration section that we add to computers-xx.xml:

<screen id="ComputersH" style="default" find_type="default">
  <menu menubar="Computers" label="Computers"/>
  <pos linux="496x402+475+216" MSWin32="439x190+716+581"/>
  <columns>
    <column key="ccol2" width="15" name="net_ip" label="IP" sort="A"/>
    <column key="ccol3" width="25" name="processor" label="Processor" sort="A"/>
    <column key="ccol4" width="15" name="user_name" label="User" sort="A"/>
  </columns>
  <lists></lists>
  <raport script=""></raport>
  <table name="computers">
    <pk_col width="6" name="id_comp" label="ID"/>
    <generator>g_computers_id_comp</generator>
    <view>computers</view>
  </table>
  <toolbar>
    <!-- toolbar states 1=flip state, 0=unchanged -->
    <off>0,0,0,0,0,0,0,0,0,0,0,0</off>
    <sele>0,0,0,0,0,0,0,0,0,0,0,0</sele>
    <add>0,0,0,0,0,0,0,0,0,0,0,0</add>
    <find>0,0,0,0,0,0,0,0,0,0,0,0</find>
    <edit>0,0,0,0,0,0,0,0,0,0,0,0</edit>
    <idle>0,0,0,1,0,0,0,0,0,0,0,0</idle>
  </toolbar>
</screen>

imagine3

The finished screen integrated in the computers application.

3.2.6 Troubleshooting

There are for sure, many steps that can go wrong, but consider this project as experimental, and if you like it, than maybe you can help me improve it.

The message: (ComputersH=HASH(…)) Unimplemented screen data check! is not an error, just a warning, to remind me that I have to add data check to the screen.

When using tpda-genscr.pl, if you encounter a error like this below, than you, like I did at first, forget (or didn't know) to add a PRIMARY KEY constraint to the table :)

Error: Global symbol $eNONE requires explicit package name at /home/stefan/tpda/Computers.pm line 440.

Add a PRIMARY KEY, or can fix it, alternatively, follow the advice of the screen generation tool and edit XML result file and set a ID column.

Another common mistake is to misspell the name of the id attribute of the screen element in the XML configuration. This ID must match the name of the screen (the Perl module file name without the extension) and the name of the package in the module.

For MySQL, a message saying that the table doesn't exists even if you know is there, the problem might be due to the identifier case sensitivity on some platforms.

3.3 Running the application

Finally and hopefully, we now have a brand new and usable database application.

To run it just type:

% cd

% cd tpda

% perl tpda.pl -a computers -c computers-xx

or even

%  perl tpda.pl -a computers -c computers-xx -u username

%  perl tpda.pl -a computers -c computers-xx -u username -p password

4 Conclusion

In this tutorial I tried to present the steps to create a one screen application for TPDA:

  • Create or adapt an existing SQL table schema
  • Create a screen template in XML
  • Edit the template, change labels and frames
  • Generate a screen module based on that template
  • Test the module by previewing it
  • Make the necessary customizations, replace some widgets with other widget types, add entry check for some widgets
  • Install the screen
  • Run the application

That's all.

I think it's not so hard to make new applications for TPDA (for me it's fun), of course the tools are limited in functionality, but I think it's a good start. If you think the same way or have any questions or comments regarding this tutorial or TPDA, feel free to drop me a mail at the address below and/or join the Open discussion or the Help forum on SourceForge.

To quote Patrick J. Volkerding, founder and maintainer of the Slackware GNU/Linux distribution:

I'm glad to see you've made it this far! :^)

Thank you, have fun!

Legal notices:

Firebird is a registered trademark of Firebird Foundation Incorporated.

Linux is the registered trademark of Linus Torvalds in the U.S. and other countries.

MySQL is a registered trademark of MySQL AB in the United States, the European Union and other countries.

PostgreSQL Copyright © 1996 – 2009 PostgreSQL Global Development Group

Slackware is a registered trademark of Slackware Linux, Inc.

SourceForge and SourceForge.net are registered trademarks of SourceForge, Inc. in the United States and other countries.

Windows is a registered trademark of Microsoft Corporation in the United States and other countries.

Footnotes:

1 Tested on Slackware and Windows XP Home. Unfortunately on Windows and Perl 5.10.0, I did not find a DBD::InterBase module for Firebird to install in ActivePerl and it failed to install on Strawberry Perl, but the application is working fine with DBD::Pg and DBD::mysql for PostgreSQL and MySQL respectively.

2 A better name would be application files. In fact is a very simple but complete application.

3 The HOME/tpda-apps path is hard wired in tpda.pl file, in order to use another location it must be set in APPLICATIONS PATH, but that's not recommended yet.

Author: Stefan Suciu <stefansbv 'at' users . sourceforge . net>

Date: 2009-11-07 21:46:43 EET

HTML generated by org-mode 6.32 in emacs 23

Get TPDA (Tiny Perl Database Application)
  at SourceForge.net. Fast, secure and Free Open Source software
  downloads