use svk, cpan target
[Fuse-DBI] / DBI.pm
diff --git a/DBI.pm b/DBI.pm
index 363f6d9..425822d 100755 (executable)
--- a/DBI.pm
+++ b/DBI.pm
@@ -111,6 +111,25 @@ running. Implementation is experimental.
 
 =back
 
+There is also alternative way which can generate C<read> and C<update>
+queries on the fly:
+
+  my $mnt = Fuse::DBI->mount({
+       'filenames' => 'select id,filename,size,writable from files',
+       'read' => sub {
+               my ($path,$file) = @_;
+               return( 'select content from files where id = ?', $file->{row}->{id} );
+       },
+       'update' => sub {
+               my ($path,$file) = @_;
+               return( 'update files set content = ? where id = ?', $file->{row}->{id} );
+       },
+       'dsn' => 'DBI:Pg:dbname=test_db',
+       'user' => 'database_user',
+       'password' => 'database_password',
+       'invalidate' => sub { ... },
+  });
+
 =cut
 
 my $dbh;
@@ -125,6 +144,8 @@ sub fuse_module_loaded;
 # be a problem.
 my $fuse_self;
 
+my $debug = 0;
+
 sub mount {
        my $class = shift;
        my $self = {};
@@ -176,16 +197,24 @@ sub mount {
 
        $sth->{'filenames'} = $dbh->prepare($arg->{'filenames'}) || die $dbh->errstr();
 
-       $sth->{'read'} = $dbh->prepare($arg->{'read'}) || die $dbh->errstr();
-       $sth->{'update'} = $dbh->prepare($arg->{'update'}) || die $dbh->errstr();
-
-
        $self->{'sth'} = $sth;
+       $self->{'dbh'} = $dbh;
 
        $self->{'read_filenames'} = sub { $self->read_filenames };
        $self->read_filenames;
 
-       $fuse_self = \$self;
+       foreach my $op (qw/read update/) {
+               if (ref($arg->{ $op }) ne 'CODE') {
+                       $self->{ $op . '_ref' } = sub {
+                               my $row = shift;
+                               return ($arg->{ $op }, $row->{'id'});
+                       }
+               } else {
+                       $self->{ $op . '_ref' } = $arg->{ $op };
+               }
+       }
+
+       $fuse_self = $self;
 
        Fuse::main(
                mountpoint=>$arg->{'mount'},
@@ -199,7 +228,7 @@ sub mount {
                truncate=>\&e_truncate,
                unlink=>\&e_unlink,
                rmdir=>\&e_unlink,
-               debug=>0,
+               debug=>$debug,
        );
        
        exit(0) if ($arg->{'fork'});
@@ -250,6 +279,7 @@ sub umount {
 
        if ($self->{'mount'} && $self->is_mounted) {
                system "( fusermount -u ".$self->{'mount'}." 2>&1 ) >/dev/null";
+               sleep 1;
                if ($self->is_mounted) {
                        system "sudo umount ".$self->{'mount'} ||
                        return 0;
@@ -261,13 +291,13 @@ sub umount {
 }
 
 $SIG{'INT'} = sub {
-       if ($fuse_self && $$fuse_self->umount) {
+       if ($fuse_self && $fuse_self->can('umount')) {
                print STDERR "umount called by SIG INT\n";
        }
 };
 
 $SIG{'QUIT'} = sub {
-       if ($fuse_self && $$fuse_self->umount) {
+       if ($fuse_self && $fuse_self->can('umount')) {
                print STDERR "umount called by SIG QUIT\n";
        }
 };
@@ -333,7 +363,8 @@ sub read_filenames {
                $files->{$row->{'filename'}} = {
                        size => $row->{'size'},
                        mode => $row->{'writable'} ? 0644 : 0444,
-                       id => $row->{'id'} || 99,
+                       id => $row->{'id'} || undef,
+                       row => $row,
                };
 
 
@@ -412,12 +443,18 @@ sub e_getdir {
 }
 
 sub read_content {
-       my ($file,$id) = @_;
+       my $file = shift || die "need file";
+
+       warn "# read_content($file)\n" if ($debug);
 
-       die "read_content needs file and id" unless ($file && $id);
+       my @args = $fuse_self->{'read_ref'}->($files->{$file});
+       my $sql = shift @args || die "need SQL for $file";
 
-       $sth->{'read'}->execute($id) || die $sth->{'read'}->errstr;
-       $files->{$file}->{cont} = $sth->{'read'}->fetchrow_array;
+       $fuse_self->{'read_sth'}->{$sql} ||= $fuse_self->{dbh}->prepare($sql) || die $dbh->errstr();
+       my $sth = $fuse_self->{'read_sth'}->{$sql} || die;
+
+       $sth->execute(@args) || die $sth->errstr;
+       $files->{$file}->{cont} = $sth->fetchrow_array;
        # I should modify ctime only if content in database changed
        #$files->{$file}->{ctime} = time() unless ($files->{$file}->{ctime});
        print "file '$file' content [",length($files->{$file}->{cont})," bytes] read in cache\n";
@@ -474,7 +511,7 @@ sub clear_cont {
 
 
 sub update_db {
-       my $file = shift || die;
+       my $file = shift || die "need file";
 
        $files->{$file}->{ctime} = time();
 
@@ -483,19 +520,31 @@ sub update_db {
                $files->{$file}->{id}
        );
 
-       if (!$sth->{'update'}->execute($cont,$id)) {
-               print "update problem: ",$sth->{'update'}->errstr;
+       my @args = $fuse_self->{'update_ref'}->($files->{$file});
+
+       my $sql = shift @args || die "need SQL for $file";
+
+       unshift @args, $files->{$file}->{cont} if ($#args == 0);
+
+       warn "## SQL: $sql\n# files->{$file} = ", Dumper($files->{$file}), $/ if ($debug);
+
+       my $sth = $fuse_self->{'update_sth'}->{$sql}
+               ||= $fuse_self->{dbh}->prepare($sql)
+               || die $dbh->errstr();
+
+       if (!$sth->execute(@args)) {
+               print "update problem: ",$sth->errstr;
                clear_cont;
                return 0;
        } else {
                if (! $dbh->commit) {
-                       print "ERROR: commit problem: ",$sth->{'update'}->errstr;
+                       print "ERROR: commit problem: ",$sth->errstr;
                        clear_cont;
                        return 0;
                }
                print "updated '$file' [",$files->{$file}->{id},"]\n";
 
-               $$fuse_self->{'invalidate'}->() if (ref $$fuse_self->{'invalidate'});
+               $fuse_self->{'invalidate'}->() if ($fuse_self->can('invalidate'));
        }
        return 1;
 }
@@ -578,7 +627,7 @@ sub e_unlink {
 #      if (exists( $dirs{$file} )) {
 #              print "unlink '$file' will re-read template names\n";
 #              print Dumper($fuse_self);
-#              $$fuse_self->{'read_filenames'}->();
+#              $fuse_self->{'read_filenames'}->();
 #              return 0;
        if (exists( $files->{$file} )) {
                print "unlink '$file' will invalidate cache\n";