in reply to Re: Object Persistence to PostGRESQL
in thread Object Persistence to PostGRESQL

This direction has proven very useful, thanks! I will get the stable v from CVS. Any examples would be greatly appreciated as I find they greatly accelerate my learning. Thanks again for taking the time to reply to a novice perl guy.
  • Comment on Re: Re: Object Persistence to PostGRESQL

Replies are listed 'Best First'.
Re: Re: Re: Object Persistence to PostGRESQL
by lachoy (Parson) on Apr 02, 2001 at 05:22 UTC

    Okay, this is from one of the examples in the distribution modified for posting here (and to use Pg-specific stuff):

    #!/usr/bin/perl use strict; use DBI; use SPOPS::Configure::DBI; use Data::Dumper qw( Dumper ); { # Set these as appropriate my $dsn = 'DBI:Pg:dbname=test'; my $user = 'postgres'; my $pass = 'postgres'; my $db = DBI->connect( $dsn, $user, $pass, { AutoCommit => 1, RaiseError => 1, PrintError => 1 }); die "Cannot connect ($DBI::errstr)" unless ( $db ); # Table we're using for the example -- go ahead and create it my $fb_table = <<SQL; CREATE TABLE fatbomb ( fatbomb_id SERIAL, name VARCHAR(100) NULL, calories INT NULL, cost VARCHAR(10) NULL, servings INT DEFAULT 15, PRIMARY KEY ( fatbomb_id ) ) SQL eval { $db->do( $fb_table ) }; die "Cannot create table! Error: $@" if ( $@ ); # This is the SPOPS configuration -- typically you'd have this in # a file and process it once when your application starts my $spops = { fatbomb => { isa => [ qw/ SPOPS::DBI::Pg SPOPS::DBI / ], class => 'My::ObjectClass', field => [ qw/ fatbomb_id calories cost name servings / ], no_insert => [ qw/ fatbomb_id / ], base_table => 'fatbomb', id_field => 'fatbomb_id', increment_field => 1, skip_undef => [ qw/ servings / ], sql_defaults => [ qw/ servings / ], }, }; SPOPS::Configure::DBI->process_config({ config => $spops, require_isa => 1 }); # Poof! Our class now exists and we can call a class method on # it. This method is normally called when your application starts # up. My::ObjectClass->class_initialize; # ...and we can also create an object, fill it with info # and save it to the data store. my $object = My::ObjectClass->new; $object->{calories} = 1500; $object->{cost} = '$3.50'; $object->{name} = "Super Deluxe Jumbo Big Mac"; my $fb_id = eval { $object->save({ db => $db }) }; if ( $@ ) { my $ei = SPOPS::Error->get; die "Error found! ($@) Error information: ", Dumper( $ei ), "\n"; } # Show the ID returned and that the 'servings' property is now # filled in (we defined it with a DEFAULT and identified it in the # configuration in 'sql_defaults'. Also note that properties # are available as hash keys, which allows our favorite # interpolation rather than using get/set method calls. print "Object saved ok!\n", "Object ID: $fb_id\n", "Servings: $object->{servings}\n\n"; # Now re-fetch this object with the ID we got undef $object; my $new_object = eval { My::ObjectClass->fetch( $fb_id, { db => $db } ) }; if ( $@ ) { my $ei = SPOPS::Error->get; die "Error found! ($@) Error information: ", Dumper( $ei ), "\n"; } print "The next set of values (from re-fetched object) ", "should match that above:\n", "Object ID: $new_object->{fatbomb_id}\n", "Servings: $new_object->{servings}\n"; # Select all objects from the table that match critieria my $all_objects = eval { My::ObjectClass->fetch_group({ where => 'calories > 1000' }) }; foreach my $found_object ( @{ $all_objects } ) { print "I am $found_object->{name} and have ", "$found_object->{calories} calories.\n"; } # Cleanup $db->do( 'drop table fatbomb' ); $db->do( 'drop sequence fatbomb_fatbomb_id_seq' ); $db->disconnect; }

    I just tested this example with the latest code from CVS -- if you checked it out earlier do a cvs update in the directory openinteract/SPOPS/SPOPS/DBI/ since I fixed a small (but important!) bug.

    Hope this was useful!

    Chris
    M-x auto-bs-mode