How to program a database. (using ObjectPAL programming language) (Compute's Getting Started With: Databases)
by Tom Campbell
Database-programming languages have followed an interesting learning curve over the years. When dBASE II came out, the language was so much simpler than COBOL and other mainframe languages that people didn't even know it was a language.
Over the next few years, dBASE became incredibly baroque, moving from the svelet dBASE III PLUS to the massive, ungainly dBASE IV. Other languages, such as Clipper from Nantucket (now Computer Associates) and FoxBase from Fox (now Microsoft), tried to remain compatible with dBASE while adding their own extensions. By this time, the term XBase, meaning "any dBASE dialect," has arisen, and a standards committee had been formed to determine what it means to be dBASE--that is, XBase--compatible. Strangely, most XBase languages lack some seemingly fundamental capabilities, such as creating a new data file under program control if one doesn't exist. The market was ripe for Paradox, which can't run dBASE programs but can read dBASE data files. (It can create a data file under program control, too--see the example below.) Paradox is the premier example of a state-of-the-art database language, with both simplicity and depth.
The first program is the same in any language. To write it in Paradox, choose New/Form from the File menu and press Enter twice to accept the defaults. This creates a window on which you design a data entry form. Click on the push-button tool on the toolbar, and a button will appear. Click your right mouse button, and a menu will appear next to the button. This is called the Properties menu, a brand-new innovation that first saw the light of day in Smalltalk systems 25 years ago. Such a menu lets you edit things associated with the selected item; in the cause of a button, this would include such things as what happens when it's pushed and whether it's selectable. In the case of form, it would include its color and what happens when a button is pushed. At any rate, choose Methods from the Properties menu for your push button and double-click on pushButton. Now enter the following script.
method pushButton(var eventInfo Event) msginfo("Hello", "Hello, world.") endmethod
Click on the lightning bar icon when you've finished the script. You'll be asked if you want to save the script you've created; click on Yes. The form will appear with its lone button. Click on the button, and a handsome dialog with 3-D buttons will appear, complete with the Hello caption and, in the middle of the dialog, the message Hello world. You'll be given three choices: Yes, No, and Cancel. Any of them will do.
Another example will make use of the return value you can get from a dialog box. This is the kind of script you'd see for a Delete Record button.
userChoice = msgQuestion ("Delete", "Sure you want to delete?") if userChoice = "Yes" THEN message("Record now being deleted") contaxCursor.delete-Record() endif
The msgQuestion dialog is closely related to msginfo; if you click on its Yes button, userChoice will be given the value Yes. This is as easy as database programming gets. Like many aspects of ObjectPAL, the Paradox programming language, it shows how easily you can do what once would have been an onerous task.
My favorite short PAL example shows the ease with which you can create a new table (data file) under program control. In dBASE, you can't do this at all. Instead, you're forced to use interactive table design tool. while this is simple for a programmer, it's not for an end user. And it's hard to create a turnkey application for end users with this issue in mind. What if a data file is lost or damaged? It happens surprisingly often, and end users have no choice but to call in an expensive consultant (perhaps you, if you're lucky). Ideally, the application itself should be able to handle problems like this on its own. But dBASE can't, at least not without an extension written in C. In Paradox, it's this easy.
method pushButton(var eventInfo Event) var Contax Table endVar
Contax = create "contax.db" with "Last Name" : "A25", "First Name": "A20", "ID Number" : "S" Key "Last Name" endCreate endmethod
This script creates a file named CONTAX.DB with three fields: Last Name, First Name, and ID Number. Note also that long names and names with spaces are allowed in newer DBMSs such as Paradox and Access, whereas dBASE still limits you to ten characters and no spaces. The A25 for Last Name means you're limited to 25 characters for a last name; this could just as easily be A30 or A100. A Last Name key allows you to sort by last name and makes it much faster to search.