MyObject - Store Perl objects in a MySql (or Oracle) database This document refers to version 1.2 of MyObject, released Apr 05, 2001. Download MyObject.pm from http://www.frontierworld.com/perl/MyObject.pm First create a table Foo in a MySql database Bar: create table Foo ( id INTEGER UNSIGNED AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255) NOT NULL, email VARCHAR(255), telephone VARCHAR(255), timestamp TIMESTAMP ); Then create a Perl program to access table Foo in database Bar as objects: package Foo; @ISA = qw(MyObject); use MyObject; package main; Foo->connect(database=>'Bar', user=>'fred', password=>'bloggs'); Foo->fields( qw(id name email telephone) ); my $foo = Foo->new(name=>'kev', email=>'kev@frontierworld.com'); $foo->insert(); $foo->{telephone} = '+44 207 359 0985'; $foo->update(); $foo = Foo->select('name=?', 'kev'); print $foo->{name} . ' has email address ' . $foo->{email} . '\n'; $foo->delete(); Foo->disconnect(); MyObject stores objects in a MySql database. It should be inherited by a class wishing to persist its objects in the database. The inheriting class should correspond to a table of the same name (case-sensitive) in the database. This table must have an auto_increment column as its primary key. MyObject provides database connectivity via methods connect() and disconnect(), object creation via methods new(), select() and next(), and object/database syncronisation via methods insert(), update() and delete(). MyObject does not check whether objects have been modified in the database while they were being modified in your Perl program. Nor does it support any locking mechanisms. Basically it's designed to be fast, simple, easy and predictable. To use MyObject with an Oracle database, you must set your ORACLE_HOME database environment variable. When using Oracle on Sun Solaris, you should also set the LD_PRELOAD to "/lib/libthread.so.1" to enable thread support. When you connect, you should add "driver=>'Oracle'" to the argument list. When setting and getting object field values, use the MyObject AUTOLOAD method because Oracle fields have uppercase names. For example, to set blah to be 2, use "$obj->blah(2)".

Class Methods
Connect to the database with optional connection details, and return the DBI connection. The database driver always defaults to "mysql" unless environment variable $DRIVER has been set. The database, user and password will default to environment variables $DATABASE, $USER and $PASSWORD. If a field list is not supplied, then a separate call to fields() must be made after connecting. Disconnect from the database cleanly. You must call disconnect() before calling connect() for a second time, otherwise connect() will merely return a handle to the current database connection. Setup a list of fields to keep in sync with the database. The first field should be the "id" field followed by the fields that you want to keep syncronised with the database. Call fields() immediately after calling connect() if you didn't provide a field list when you called connect(). Use an id field name of "null" if there is no auto increment id field. Get/set an object's field value in a driver independent manner. Create a new object, setting any field values that were passed. This call does not store the new object in the database - you should call insert() or update(). Get a table row as an object, by requesting a specific id. This is very useful when one table has a relationship with another table, implemented via foreign keys, and you wish to get the related object. Create and execute a parameterised SQL statement. The format of the conditions and arguments is similar to "printf", in that a "?" is used as a placeholder for a corresponding argument. For example, if you use three question marks, you should provide three arguments to instantiate them. Returns the query created. Select the first object matching some conditions. The format of the conditions and arguments is similar to "printf", in that a "?" is used as a placeholder for a corresponding argument. For example, if you use three question marks, you should provide three arguments to instantiate them. Use next() to iterate. Select the next object matching some conditions. The conditions must exactly match those originally provided in a select() call. When next() cannot return the next object, it returns an empty object. A typical use for select() and next() is in a for loop like this: my $obj; for ( $obj = Class->select("field=?",value); $obj->{id_field}; $obj = Class->next("field=?")) { # Do something with $obj }

Object Methods
Insert an object into the database and give the object a new id. Update an object in the database, or insert it if the object has no id. Delete an object from the database. DBI Kevin Hutchinson (kevin@frontierworld.com) Copyright (c) 1999-2001, FrontierWorld limited (UK). This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.