give Directory proper interface
[scpubgit/stemmatology.git] / script / save_to_db.pl
old mode 100644 (file)
new mode 100755 (executable)
index 59e5ef4..d7d485f
@@ -4,21 +4,18 @@ use lib 'lib';
 use strict;
 use warnings;
 use File::Basename;
-use KiokuDB;
-use KiokuDB::TypeMap::Entry::Naive;
 use Text::Tradition;
+use Text::Tradition::Directory;
 use Text::Tradition::Stemma;
 
+binmode( STDOUT, ':utf8' );
+binmode( STDERR, ':utf8' );
+
 # Make a KiokuDB store from the traditions data we have.
 
-my $kdb = KiokuDB->connect( "dbi:SQLite:dbname=db/traditions.db", 
-       create => 1,
-       typemap => KiokuDB::TypeMap->new(
-            isa_entries => {
-                "Graph" => KiokuDB::TypeMap::Entry::Naive->new,
-                "Graph::AdjacencyMap" => KiokuDB::TypeMap::Entry::Naive->new,
-            },
-        ),
+my $kdb = Text::Tradition::Directory->new(
+       'dsn' => "dbi:SQLite:dbname=db/traditions.db",
+       'extra_args' => { 'create' => 1 },
     );
     
 my %stemma_map = (
@@ -51,9 +48,8 @@ if( $dir ) {
                                );
                }
                        
-               my $scope = $kdb->new_scope;
-               my $tid = $kdb->store( $tradition );
-               my $sid = $kdb->store( $stemma ) if $stemma;
+               my $tid = $kdb->save_tradition( $tradition );
+               my $sid = $kdb->save_stemma( $stemma ) if $stemma;
                print STDERR "Stored tradition for " . $tradition->name . " at $tid\n";
                print STDERR "\tand stemma at $sid\n" if $stemma;
        }
@@ -61,27 +57,19 @@ if( $dir ) {
 
 # Now try reading the objects from the DB.
 
-my $scope = $kdb->new_scope;
-
-my $stream = $kdb->root_set;
-until( $stream->is_done ) {
-       foreach my $t ( $stream->items ) {
-               print STDERR "*** Object " . $kdb->object_to_id( $t ) . " ***\n";
-               if( ref( $t ) eq 'Text::Tradition' ) {
-                       print STDERR "Got tradition " . $t->name . " out of the database\n";
-                       my @wits = map { $_->sigil } $t->witnesses;
-                       print STDERR "...with witnesses @wits\n";
-                       my $c = $t->collation;
-                       print STDERR "Collation has " . scalar( $c->readings ) . " readings\n";
-                       print STDERR "Collation has " . scalar( $c->paths ) . " paths\n";
-                       print STDERR "Collation has " . scalar( $c->relationships ) . " relationship links\n";
-               } elsif( ref( $t ) eq 'Text::Tradition::Stemma' ) {
-                       print STDERR "Got stemma for tradition " . $t->collation->tradition->name 
-                               . " out of the database\n";
-                       print STDERR "Stemma graph is " . $t->graph . "\n";
-               } else {
-                       print STDERR "Got unexpected object of type " . ref( $t ) 
-                               . " out of the database\n";
-               }
+foreach my $tid ( $kdb->tradition_ids ) {
+       my $t = $kdb->tradition( $tid );
+       print STDERR "Got tradition " . $t->name . " out of the database\n";
+       my @wits = map { $_->sigil } $t->witnesses;
+       print STDERR "...with witnesses @wits\n";
+       my $c = $t->collation;
+       print STDERR "Collation has " . scalar( $c->readings ) . " readings\n";
+       print STDERR "Collation has " . scalar( $c->paths ) . " paths\n";
+       print STDERR "Collation has " . scalar( $c->relationships ) . " relationship links\n";
+       my $s = $kdb->stemma( $tid );
+       if( $s ) {
+               print STDERR "Got stemma for tradition " . $s->collation->tradition->name 
+                       . " out of the database\n";
+               print STDERR "Stemma graph is " . $s->graph . "\n";
        }
 }
\ No newline at end of file