| 1 | package DBM::Deep::Manager; | ||
| 2 | $DBM::Deep::Manager::VERSION = 0.02; | ||
| 3 | |||
| 4 | |||
| 5 | use DBM::Deep 2.0; | ||
| 6 | use Data::Interactive::Inspect; | ||
| 7 | use YAML; | ||
| 8 | |||
| 9 | |||
| 10 | |||
| 11 | use vars qw(@ISA @EXPORT @EXPORT_OK $db); | ||
| 12 | require Exporter; | ||
| 13 | @ISA = qw(Exporter); | ||
| 14 | @EXPORT = qw(getshell opendb _export _import); | ||
| 15 | @EXPORT_OK = qw(); | ||
| 16 | |||
| 17 | sub getshell { | ||
| 18 | my ($db, $dbfile) = @_; | ||
| 19 | my $shell = Data::Interactive::Inspect->new( | ||
| 20 | begin => sub { | ||
| 21 | my ($self) = @_; | ||
| 22 | my $maindb = tied(%{$self->{struct}}); | ||
| 23 | if (!$self->{session}) { | ||
| 24 | eval { $maindb->begin_work; }; | ||
| 25 | if ($@) { | ||
| 26 | print STDERR "transactions not supported by $dbfile," . | ||
| 27 | " re-create with 'num_txns' > 1\n"; | ||
| 28 | } | ||
| 29 | else { | ||
| 30 | $self->{session} = 1; | ||
| 31 | print "ok\n"; | ||
| 32 | } | ||
| 33 | } | ||
| 34 | }, | ||
| 35 | commit => sub { | ||
| 36 | my ($self) = @_; | ||
| 37 | my $maindb = tied(%{$self->{struct}}); | ||
| 38 | if ($self->{session}) { | ||
| 39 | $maindb->commit(); | ||
| 40 | $self->{session} = 0; | ||
| 41 | print "ok\n"; | ||
| 42 | } | ||
| 43 | }, | ||
| 44 | rollback => sub { | ||
| 45 | my ($self) = @_; | ||
| 46 | my $maindb = tied(%{$self->{struct}}); | ||
| 47 | if ($self->{session}) { | ||
| 48 | $maindb->rollback(); | ||
| 49 | $self->{session} = 0; | ||
| 50 | print "ok\n"; | ||
| 51 | } | ||
| 52 | }, | ||
| 53 | name => $dbfile, | ||
| 54 | struct => $db, | ||
| 55 | export => sub { | ||
| 56 | my ($db) = @_; | ||
| 57 | return tied(%{$db})->export(); | ||
| 58 | } | ||
| 59 | ); | ||
| 60 | return $shell; | ||
| 61 | } | ||
| 62 | |||
| 63 | sub opendb { | ||
| 64 | my ($dbfile, %dbparams) = @_; | ||
| 65 | my $db; | ||
| 66 | if (tie my %db, 'DBM::Deep', %dbparams) { | ||
| 67 | $db = \%db; | ||
| 68 | } | ||
| 69 | else { | ||
| 70 | die "Could not open dbfile $dbfile: $!\n"; | ||
| 71 | } | ||
| 72 | return $db; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub _export { | ||
| 76 | my ($file, $dbfile, %dbparams) = @_; | ||
| 77 | my $db = &opendb($dbfile, %dbparams); | ||
| 78 | my $fd; | ||
| 79 | if ($file eq '-') { | ||
| 80 | $fd = *STDOUT; | ||
| 81 | } | ||
| 82 | else { | ||
| 83 | open $fd, ">$file" or die "Could not open export file $file for writing: $!\n"; | ||
| 84 | } | ||
| 85 | print $fd YAML::Dump(tied(%{$db})->export()); | ||
| 86 | close $fd; | ||
| 87 | } | ||
| 88 | |||
| 89 | sub _import { | ||
| 90 | my ($file, $dbfile, %dbparams) = @_; | ||
| 91 | my $db = &opendb($dbfile, %dbparams); | ||
| 92 | my $fd; | ||
| 93 | if ($file eq '-') { | ||
| 94 | $fd = *STDIN; | ||
| 95 | } | ||
| 96 | else { | ||
| 97 | open $fd, "<$file" or die "Could not open import file $file for reading: $!\n"; | ||
| 98 | } | ||
| 99 | my $yaml = join '', <$fd>; | ||
| 100 | my $perl = YAML::Load($yaml); | ||
| 101 | tied(%{$db})->import($perl); | ||
| 102 | close $fd; | ||
| 103 | } | ||
| 104 | |||
| 105 | |||
| 106 | 1; | ||
| 107 | |||
| 108 | =head1 NAME | ||
| 109 | |||
| 110 | DBM::Deep::Manager - A container for functions for the dbmdeep program | ||
| 111 | |||
| 112 | =head1 SYNOPSIS | ||
| 113 | |||
| 114 | If you want to know about the L<dbmdeep> program, see the L<dbmdeep> file itself. | ||
| 115 | No user-serviceable parts inside. ack is all that should use this. | ||
| 116 | |||
| 117 | =head1 AUTHOR | ||
| 118 | |||
| 119 | T.v.Dein <tlinden@cpan.org> | ||
| 120 | |||
| 121 | =head1 BUGS | ||
| 122 | |||
| 123 | Report bugs to | ||
| 124 | http://rt.cpan.org/NoAuth/ReportBug.html?Queue=DBM::Deep::Manager | ||
| 125 | |||
| 126 | =head1 SEE ALSO | ||
| 127 | |||
| 128 | L<dbmtree> | ||
| 129 | |||
| 130 | L<DBM::Deep> | ||
| 131 | |||
| 132 | L<Data::Interactive::Inspect> | ||
| 133 | |||
| 134 | =head1 COPYRIGHT | ||
| 135 | |||
| 136 | Copyright (c) 2015 by T.v.Dein <tlinden@cpan.org>. | ||
| 137 | All rights reserved. | ||
| 138 | |||
| 139 | =head1 LICENSE | ||
| 140 | |||
| 141 | This program is free software; you can redistribute it | ||
| 142 | and/or modify it under the same terms as Perl itself. | ||
| 143 | |||
| 144 | =head1 VERSION | ||
| 145 | |||
| 146 | This is the manual page for B<DBM::Deep::Manager> Version 0.02. | ||
| 147 | |||
| 148 | =cut |