Written by: Prograph International (Mark Szpakowski)
Contents: DB example code.pgs
videos.txt
videosDB
videosDB keys
DB example code.ReadMe
Needs Prograph Extensions:
Math Primitives
More Primitives
Primitives
Datafile Primitives
Needs Libraries to Compile:
Library 2.6
How to Use in Your Program:
Selectively load any method or class that seems useful.
DESCRIPTION
DB example code.pgs contains the example code fragments used as part of the Database Engine primitives documentation, as well as a few other useful and illustrative methods. It is deliberately kept very simple: it assumes that at any one time there is only one database, one table and one key that is open, the path ID of which can be accessed in the persistent "current db", "current table" and "current key" respectively. The videos.txt file is a text-only file with a few videos records in tab-delimited format. This file was imported into the videosDB database via the "table-import" primitive, into the "videos" table. Each cluster of that table is an instance of class "videos".
PERSISTENTS
current-DB
current-table
current-key The videos database provided has two key attributes, "Artist" (string, case-insensitive by default) and ID (natural number (integer > 0)). Be sure to set the current-key to be the key you want to work with.
current-cluster In the example code provided, current-cluster is used only to store the cluster ID of the currently referenced cluster.
CLASS "path"
• Instance Attributes: name, ID, owner
Database entities have names (useful on the user level), path IDs (required by the database primitives), and owners (databases could belong to an application, tables belong to databases, and keys belong to tables).
• error?
Succeeds if error number 0 (database operation succeeded) is reported. Otherwise it fails, after indicating whether a file (error # < 0) or database (error # > 0) occurred. This failure usually triggers immediate termination of the method in which it occurs. One of the first improvements that could be made, of course, would be to provide a full-text error message for each database error that can occur.
CLASS "database" (inherits from "path")
• Class Attributes: databases, current DB
The class attribute "databases" could be used to store a list of databases, while "current DB" could be used to refer to the (one) database currently in use (this would provide an alternative and somewhat more flexible way of referring to paths than that provided by using persistents). A further extension would be to maintain a library list and an "active" (those that are open) list of databases.
• Instance Attribute: tables
A list of tables belonging to the database.
• find all by key
Finds all the clusters that have the same value for the key "Artist". Asks the user for that value, and then puts together a list of cluster values (instances of class "videos"), which, after telling the user how many clusters it found, it displays via the "display" primitive. Note that the comparison routine it uses to determine whether the key being read by "key-next" is still the one requested by the user is a case-insensitive string comparison routine; this routine will not work with a key that is not a string.
This method assumes you have 1) opened the "videosDB" database (use "open db"), 2) opened the "videos" table (use "open table"), and 3) opened the "Artist" key (use "open key").
• list all keys
List all the keys for the "videos" table (eliminating redundant entries).