X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FText%2FTradition%2FDirectory.pm;h=09aed7b0717b03077379405f5d657616a80ce2f6;hb=896fe649a80575aaa06d3484c09579a2cb34ba8a;hp=246d83f0b5ffc3c93d4d6f3903f2923e23ce4a6e;hpb=086f445c07c8d7882fc84bcbd328691c59c3126b;p=scpubgit%2Fstemmatology.git diff --git a/lib/Text/Tradition/Directory.pm b/lib/Text/Tradition/Directory.pm index 246d83f..09aed7b 100644 --- a/lib/Text/Tradition/Directory.pm +++ b/lib/Text/Tradition/Directory.pm @@ -3,6 +3,8 @@ package Text::Tradition::Directory; use strict; use warnings; use Moose; +use DBI; +use Encode qw/ decode_utf8 /; use KiokuDB::GC::Naive; use KiokuDB::TypeMap; use KiokuDB::TypeMap::Entry::Naive; @@ -40,9 +42,9 @@ Text::Tradition::Directory is an interface for storing and retrieving text tradi Returns a Directory object. -=head2 tradition_ids +=head2 traditionlist -Returns the ID of all traditions in the database. +Returns a hashref mapping of ID => name for all traditions in the directory. =head2 tradition( $id ) @@ -52,6 +54,11 @@ Returns the Text::Tradition object of the given ID. Writes the given tradition to the database, returning its ID. +=head2 delete( $tradition ) + +Deletes the given tradition object from the database. +WARNING!! Garbage collection does not yet work. Use this sparingly. + =begin testing use TryCatch; @@ -101,11 +108,14 @@ is( ref( $nt ), 'Text::Tradition', "Made new tradition" ); { my $f = Text::Tradition::Directory->new( 'dsn' => $dsn ); my $scope = $f->new_scope; - is( scalar $f->tradition_ids, 1, "Directory index has our tradition" ); + is( scalar $f->traditionlist, 1, "Directory index has our tradition" ); my $nuuid = $f->save( $nt ); ok( $nuuid, "Stored second tradition" ); - is( scalar $f->tradition_ids, 2, "Directory index has both traditions" ); + my @tlist = $f->traditionlist; + is( scalar @tlist, 2, "Directory index has both traditions" ); my $tf = $f->tradition( $uuid ); + my( $tlobj ) = grep { $_->{'id'} eq $uuid } @tlist; + is( $tlobj->{'name'}, $tf->name, "Directory index has correct tradition name" ); is( $tf->name, $t->name, "Retrieved the tradition from a new directory" ); my $sid = $f->object_to_id( $tf->stemma(0) ); try { @@ -121,36 +131,71 @@ is( ref( $nt ), 'Text::Tradition', "Made new tradition" ); like( $e->message, qr/Cannot directly delete non-Tradition object/, "Exception has correct message" ); } + $f->delete( $uuid ); ok( !$f->exists( $uuid ), "Object is deleted from DB" ); ok( !$f->exists( $sid ), "Object stemma also deleted from DB" ); - is( scalar $f->tradition_ids, 1, "Object is deleted from index" ); + is( scalar $f->traditionlist, 1, "Object is deleted from index" ); } { my $g = Text::Tradition::Directory->new( 'dsn' => $dsn ); my $scope = $g->new_scope; - is( scalar $g->tradition_ids, 1, "Now one object in new directory index" ); + is( scalar $g->traditionlist, 1, "Now one object in new directory index" ); + my $ntobj = $g->tradition( 'CX' ); + my @w1 = sort { $a->sigil cmp $b->sigil } $ntobj->witnesses; + my @w2 = sort{ $a->sigil cmp $b->sigil } $nt->witnesses; + is_deeply( \@w1, \@w2, "Looked up remaining tradition by name" ); } =end testing =cut +use Text::Tradition::TypeMap::Entry; has +typemap => ( - is => 'rw', - isa => 'KiokuDB::TypeMap', - default => sub { - KiokuDB::TypeMap->new( - isa_entries => { - "Graph" => KiokuDB::TypeMap::Entry::Naive->new, - "Graph::AdjacencyMap" => KiokuDB::TypeMap::Entry::Naive->new, - } - ); - }, + is => 'rw', + isa => 'KiokuDB::TypeMap', + default => sub { + KiokuDB::TypeMap->new( + isa_entries => { + "Text::Tradition" => + KiokuDB::TypeMap::Entry::Naive->new(), + "Graph" => Text::Tradition::TypeMap::Entry->new(), + "Graph::AdjacencyMap" => Text::Tradition::TypeMap::Entry->new(), + } + ); + }, ); -before [ qw/ store update insert delete / ] => sub { +# Push some columns into the extra_args +around BUILDARGS => sub { + my $orig = shift; + my $class = shift; + my $args; + if( @_ == 1 ) { + $args = $_[0]; + } else { + $args = { @_ }; + } + if( $args->{'dsn'} =~ /^dbi/ ) { # We're using Backend::DBI + my @column_args = ( 'columns', + [ 'name' => { 'data_type' => 'varchar', 'is_nullable' => 1 } ] ); + my $ea = $args->{'extra_args'}; + if( ref( $ea ) eq 'ARRAY' ) { + push( @$ea, @column_args ); + } elsif( ref( $ea ) eq 'HASH' ) { + $ea = { %$ea, @column_args }; + } else { + $ea = { @column_args }; + } + $args->{'extra_args'} = $ea; + } + return $class->$orig( $args ); +}; + +# before [ qw/ store update insert delete / ] => sub { +before [ qw/ delete / ] => sub { my $self = shift; my @nontrad; foreach my $obj ( @_ ) { @@ -185,17 +230,48 @@ sub save { sub tradition { my( $self, $id ) = @_; my $obj = $self->lookup( $id ); - unless( ref( $obj ) eq 'Text::Tradition' ) { + unless( $obj ) { + # Try looking up by name. + foreach my $item ( $self->traditionlist ) { + if( $item->{'name'} eq $id ) { + $obj = $self->lookup( $item->{'id'} ); + last; + } + } + } + if( $obj && ref( $obj ) ne 'Text::Tradition' ) { throw( "Retrieved object is a " . ref( $obj ) . ", not a Text::Tradition" ); } return $obj; } -sub tradition_ids { +sub traditionlist { my $self = shift; - my @ids; - $self->scan( sub { push( @ids, $self->object_to_id( @_ ) ) } ); - return @ids; + # If we are using DBI, we can do it the easy way; if not, the hard way. + # Easy way still involves making a separate DBI connection. Ew. + my @tlist; + if( $self->dsn =~ /^dbi:(\w+):/ ) { + my $dbtype = $1; + my @connection = @{$self->directory->backend->connect_info}; + # Get rid of KiokuDB-specific arg + pop @connection if scalar @connection > 4; + $connection[3]->{'sqlite_unicode'} = 1 if $dbtype eq 'SQLite'; + $connection[3]->{'pg_enable_utf8'} = 1 if $dbtype eq 'Pg'; + my $dbh = DBI->connect( @connection ); + my $q = $dbh->prepare( 'SELECT id, name from entries WHERE class = "Text::Tradition"' ); + $q->execute(); + while( my @row = $q->fetchrow_array ) { + my( $id, $name ) = @row; + # Horrible horrible hack + $name = decode_utf8( $name ) if $dbtype eq 'mysql'; + push( @tlist, { 'id' => $row[0], 'name' => $row[1] } ); + } + } else { + $self->scan( sub { my $o = shift; + push( @tlist, { 'id' => $self->object_to_id( $o ), + 'name' => $o->name } ) } ); + } + return @tlist; } sub throw {