NAME

    DBIx::SetDB - View a database the way the designer does, as sets.


SYNOPSIS

    use strict;
    use DBI;
    use SetDB;
    # 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', ['book'], ['address']);
    # Now loop through and print out each person
    while(my $person = $people->fetchNext())
    {
      print "Name: $person->{name}\n";
      # This is the set of books which that person has
      my $books = $person->{book};
      # Lets go ahead and print all of their books
      while(my $book = $books->fetchNext())
      {
        print "  Book: $book->{title}\n";
        # Demonstrate updates by adding ' sucks!' to the book's title
        $book->{title} .= ' sucks!';
      }
      $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";
      }
    }


HISTORY / NOTES

    History / Notes
      2004.01.30
        - See http://thelackthereof.org/wiki.pl/SetDB for some thoughts
      2004.01.19
        - Created


INTRODUCTION

This package is an abstraction on top of DBI to view databases as sets of things (though it is also aware of relationships between the things).


CLASS METHODS

new create a new SetDB object =cut sub new { my $class = shift; my $dbi = shift; my $schema = shift; my $self = {}; bless $self, $class; $self->{dbi} = $dbi; $self->{schema} = $schema; $self->{network} = $self->getNetwork(); return $self; }
sub schema { my $self = shift; if(my $new_schema = shift) { $self->{schema} = $new_schema; } return $self->{schema}; }

sub newSet { my $self = shift; my @options = @_; my $set = new SetDB::Set($self, @options); return $set; }

# Set up an undirected graph. Each edge is labeled with the equality that # connects the two nodes. sub getNetwork { my $self = shift; my %schema = %{$self->{schema}}; my %network; foreach my $s (keys %schema) { my @refs = keys %{$schema{$s}{'refs'}};

    my $key = $schema{$s}{'key'};
    $key = 'id' unless $key;
    foreach my $r (@refs)
    {
      push @{$network{$s}},
      [
        $schema{$s}{'refs'}{$r}{'table'},
        "$s.$r = $schema{$s}{'refs'}{$r}{'table'}.$key"
      ];
      push @{$network{$schema{$s}{'refs'}{$r}{'table'}}},
      [
        $s,
        "$s.$r = $schema{$s}{'refs'}{$r}{'table'}.$key"
      ];
    }
  }
  return \%network;
}

1;