Database III: jBase
along the way of JavaScript

One day, as the oxherder sat meditating on his Database Object, it dawned on him that two small changes would dramatically increase its usefulness.

  1. Besides being able to display the output as a table or glossary in a window, it would be nice to have it in a format that could be EMailed (like an order form - for example). Why not as a comma-delimited text string that could be imported into a database or spreadsheet program?
  2. Wouldn't it be nice to customize the fields in the report? What about calculated fields? What about html-encoded fields?
The result was jBase (now you know why the last one was renamed jBaseJr). The properties and methods of jBaseJr are maintained (except you can't enter the field order in showRecords). There are, however, some additions (shown in green). Here are the object's specifications:


Object:
jBase(field0, field1, ... , fieldn-1) Where fieldx is a list of field names.
Properties:
fields The number of fields.
field[i] An array of field names. [i=0 to fields-1]
records The number of records.
record[i][j] An array of the data's records. [i=0 to fields-1][j=0 to records-1]
columns The number of report fields defined.
column[m][n] An array of the report fields.
[m=0 definition][m=1 isMath?][m=2 caption][n=0 to columns-1]
order[j] The sort index (record numbers). [j=0 to records-1]
dataSet[j] The find index (true/false). [j=0 to records-1]
Methods:
addRecord(data0, data1, ... , datan-1)
datax A list of the data elements for the new record.
sortRecords(field, order, case)
field The number of a field to sort.
order The sort order. [0=natural][1=ascending][2=descending]
case Is the sort case sensitive? [true][false]
findRecords(field, string, method, case)
field The number of the field to search.
string The search string.
method The search method. ['exact'=exact match]['all'=anywhere in the field]
case Is the sort case sensitive? [true][false]
showRecords(style)
style The report style. ['table']['glossary'] ['string']
string returns a comma-delimited string of the data.
addColumn(definition, isMath, caption) Add a column to the report.
definition The 'formula' for this column.
isMath Does this column need to be calculated? [true/false]
caption The caption for this column.
newFormat() Clear the column definitions.
_parse(formula, recordNumber) An internal function to compute the column's value.
formula The formula (from the column's definition).
recordNumber The number of the record to use.

Using jBase

jBase is, then, a multipurpose JavaScript Database object. Set up the database by creating a new instance of jBase(field list) and populate it with addRecords(...). Sorting and finding a data subset with sortRecords() and findRecords() are as described previously.

First, if you do not use the addColumn() feature, showRecords() will do just what it says - show the records (as modified by the sort/find functions). Try jBase 1 on the left (with a database of url's):

html Fields

addColumn(): This method adds a column to the report, accepting three parameters:
  1. definition - the column definition is a string that can have many forms. Any field from the record may be added by enclosing the field number in square brackets ( [0] ). The mysterious method _parse() substitutes the contents of the record's field for the bracketed field number. So [0] by itself would put the data from field 0 for each record into the output. The whole column definition could have an html encoded phrase with information from one or more fields from each record. Or the string might be a mathematical expression - a formula with the database fields as variables.

  2. isMath - if a definition is a mathematical expression, jBase needs to be told so it can apply the eval() JavaScript function. For mathematical expressions, set isMath to true. Otherwise, set isMath to false.

  3. caption - is a string label for the column (if it is displayed as a table).

jBase 2 uses the same database as jBase 1. The function showLinks() demonstrates the results of addColumn() using html in the definition string. As you can see, the html tags are displayed by the browser. Thus, the links are shown as a glossary of functional hyperlinks. It doesn't take too much imagination to see the possibilities.

Essentially, anything that can be expressed as an html statement can be entered as a definition in addColumn() method, including pictures. Just put the filename of the image in the database, and the image tag in the definition parameter. Try jBase 3 to see the results - by now an old friend. (This is Database4 in the source code.)

newFormat(): jBase 2 also shows what this method is for. If it weren't in showLinks(), clicking the button a second time would add two more columns. newFormat() 'cleans things up'.

_parse(): has already been described. It is used by showRecords() to convert the bracketed field numbers into data.

Calculated Fields

You can do the same thing with mathematics. All that's required is using a valid string and setting isMath to true (evoking the eval() function). jBase 4 shows a single field database filled with serial integers. jBase 5 evokes the function circleCalc() which uses the database to illustrate jBase's mathematic prowess. Notice that it uses Math.PI, a JavaScript constant. jBase can use any Math Object methods in its definitions.

Comma Delimited Text

Finally, the oxherder wanted to be able to send the ouput of a query as EMail. The function - showRecords(param) - returns the words 'table' and 'glossary' in the jBases so far. If param='string', showRecords('string') shows nothing, but returns a comma delimited text string of the results. The first row has the column captions, followed by rows containing the records. jBase 7 shows the output from the database used in jBase 6 (displayed in a text box).

This is the last of the three Database supplements. All of these pages and an explanatory document can be downloaded as The Database Supplement in the last tutorial. Obviously, if you want to use the jBase object in your pages, you have to include all of the JavaScript between the [DATABASE OBJECT] comments in the source code. Notice, when you look at the source, that this page has four databases all sharing the jBase code.