#!/usr/bin/perl # SetDB Demo # # This is a demo script for SetDB, the new Set-Oriented database interface I'm # constructing. I will try to excercise all of the features of the set here so # that everyone knows what it can do. This will also act as a test case. # # History / Notes # 2004.02.13 # - Now using Darcs to keep track of versions, so this section may become # less verbose, and with good reason. # - My first goal has been attained! Can't tell you what it was. secret. # 2004.02.10 # - Lots of stuff has been added to SetDB but this demo is still not fully # functional as it will be # - At some point I changed the schema structure a bit. The ultimate plan # is to import from another format into the one shown here # 2004.01.30 # - Created #use strict; #use diagnostics; use DBI; use SetDB; use Data::Dumper; use Debug; #$Debug::debug{'setdb_demo'} = 1; # Since this is a demo, lets load the sql print "Re-loading SQL...\n"; print `mysql -u setdb --password='setdb' setdb_test < demo_db.sql`; print "done.\n\n"; # Okay, first we set up our DBI connection my $dbi = DBI->connect( 'DBI:mysql:database=setdb_test;host=localhost', 'setdb', 'setdb'); # Now we set up our schema use vars qw( $schema ); do 'schema.pl'; # This creates our SetDB database my $db = new SetDB($dbi, $schema); # Lets get a set of people and their books #my $people = $db->newSet('person', ['owns_book', 'book'], ['address']); my $people = $db->newSet('person', ['owns_book', 'book'], ['address']); print "People query: " . $people->getQuery() . "\n"; # Now loop through and print out each person while(my $person = $people->fetchNext()) { print "Name: $person->{name}\n"; Debug::print('setdb_demo', "person: $person"); # This is the set of books which that person has my $books = $person->{book}; Debug::print('setdb_demo', "books: $books"); print "Books query: " . $books->getQuery() . "\n"; # Lets go ahead and print all of their books while(my $book = $books->fetchNext()) { # print " BookDump: " . Dumper($book) . "\n"; print " Book: $book->{title}\n"; # Demonstrate updates by adding ' sucks!' to the book's title $book->{title} .= ' sucks!'; } print "Adding a new book for this person.\n"; my $newbook = $books->newEntry(); $newbook->{title} = 'I am a new book!'; $books->restart(); # Lets go ahead and print all of their books while(my $book = $books->fetchNext()) { print " Book: $book->{title}\n"; } # This is the set of books which that person has my $addrs = $person->{address}; # Lets go ahead and print all of their books while(my $addr = $addrs->fetchNext()) { print " Street: $addr->{street}\n"; } }