This set of functions for PERL implement a simple flat-file database. It
isn't fast. It isn't complex. It works, and is easy. I call it DBClerk.
It is best if you only use this on small data sets – as the data
grows, processing time grows exponentially.
The functions are configured with a few variables in the beginning. The
defaults should be pretty good for most use.
The database has a header line followed by 0 or more data lines. The
header line defines the fields. A sample database looks like this:
User::Pass::LName::FName
Tyler::password::Tyler::Akins
JDoe::who?::John::Doe
|
The "key" field is the first one defined. In the example, it is "User".
Field names are case sensitive. Invalid characters are stripped, so this is
only good for data that can be represented textually without newlines and
without the delimeter. If you pass in data that has the delimeter (by
default it is "::"), it will be silently changed to another value (by
default it is ":").
Download dbclerk.txt and rename it to
dbclerk.pl. You will also need to download the lockfile function on this web site (or alter this to
suit your own locking needs).
DBClerk_Add($DB_File, %Data) - Adds a record to the database (at
the end). $DB_File is the name of the database file, %Data contains the
information needed. Returns 0 on success, 1 on error.
DBClerk_Delete($DB_File, $Key) - Deletes the record with the
specified key if it exists. $DB_File is the name of the database file, $Key
is the key that is looked for. Returns 0 on success, 1 if the ID didn't
exist.
%Data = DBClerk_Get($DB_File, $Key) - Retrieves the record from
the database with the specified key. $DB_File is the name of the database
file, $Key is the key that is looked for. Returns the hash with the data,
or an empty hash if the ID was not found.
@Keys = DBClerk_KeyList($DB_File) - Returns an array of the keys
(column names) in the database. $DB_File is the name of the database file.
The first one in the returned array is the ID field.
DBClerk_Update($DB_File, %Data) - Updates the specified record.
$DB_File is the database file, %Data is the data that it should contain.
Returns 0 if the ID existed and the record was changed, 0 if the ID didn't
exist in the file.
@IDs = DBClerk_FindAll($DB_File) - Returns a list of all of the
IDs for all of the records in the database. $DB_File is the name of the
database to scan.
@FilteredIDs = DBClerk_Refine($Field, $Condition, $Data, $DB_File, @IDs) -
Returns a list of all keys that match the condition. Used to remove entries
from the @IDs array that don't match the criteria. All of the records that
do match are returned to @FilteredIDs. $Field is the name of the database
field to use, $Condition is how to match (described later), $Data is what
you are comparing to, $DB_File is the name of the database file and @IDs is
the list of IDs that you want to whittle down a bit.
- < - Less than (numerical)
- <= - Less than or equal to (numerical)
- == - Equal to (numerical)
- != - Not equal to (numerical)
- >= - Greater than or equal to (numerical)
- > - Greater than (numerical)
- ne - Not equal to (string)
- eq - Equal to (string)
- lt - Less than (string)
- gt - Greater than (string)
- =~ - Regular expression equals case sensitive (string)
- !~ - Regular expression not equals case sensitive (string)
- =~i - Regular expression equals case insensitive (string)
- !~i - Regular expression not equals case insensitive (string)
If you want to find all people in the sample database with a last name
starting with A, you can use DBClerk_Refine('LName', '=~i', 'a.*', $DB_File,
@IDs).