| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | # -*- perl -*- | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | # | 
| 4 |  |  |  |  |  |  | # $Id: Base.pm,v 1.6 2004/10/04 19:21:10 eserte Exp $ | 
| 5 |  |  |  |  |  |  | # Author: Slaven Rezic | 
| 6 |  |  |  |  |  |  | # | 
| 7 |  |  |  |  |  |  | # Copyright (C) 2001 Online Office Berlin. All rights reserved. | 
| 8 |  |  |  |  |  |  | # Copyright (C) 2002 Slaven Rezic. | 
| 9 |  |  |  |  |  |  | # This is free software; you can redistribute it and/or modify it under the | 
| 10 |  |  |  |  |  |  | # terms of the GNU General Public License, see the file COPYING. | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | # | 
| 13 |  |  |  |  |  |  | # Mail: slaven@rezic.de | 
| 14 |  |  |  |  |  |  | # WWW:  http://we-framework.sourceforge.net | 
| 15 |  |  |  |  |  |  | # | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | =head1 NAME | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | WE::DB::Base - base class for all database classes | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | =head2 METHODS | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | =over | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | =cut | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | package WE::DB::Base; | 
| 33 |  |  |  |  |  |  |  | 
| 34 | 22 |  |  | 22 |  | 132 | use base qw(Class::Accessor); | 
|  | 22 |  |  |  |  | 40 |  | 
|  | 22 |  |  |  |  | 11533 |  | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | __PACKAGE__->mk_accessors(qw(Root Connected)); | 
| 37 |  |  |  |  |  |  |  | 
| 38 | 22 |  |  | 22 |  | 20226 | use strict; | 
|  | 22 |  |  |  |  | 92 |  | 
|  | 22 |  |  |  |  | 815 |  | 
| 39 | 22 |  |  | 22 |  | 117 | use vars qw($VERSION); | 
|  | 22 |  |  |  |  | 59 |  | 
|  | 22 |  |  |  |  | 9092 |  | 
| 40 |  |  |  |  |  |  | $VERSION = sprintf("%d.%02d", q$Revision: 1.6 $ =~ /(\d+)\.(\d+)/); | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | sub new { | 
| 43 | 0 |  |  | 0 | 1 |  | my($class) = @_; | 
| 44 | 0 |  |  |  |  |  | my $self = {}; | 
| 45 | 0 |  |  |  |  |  | bless $self, $class; | 
| 46 |  |  |  |  |  |  | } | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | =item connect_if_necessary($sub) | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | Connect to the database, if there is not a database connection yet, | 
| 51 |  |  |  |  |  |  | and execute the supplied subroutine C<$sub>. The return value of | 
| 52 |  |  |  |  |  |  | C<$sub> will be returned by C. Exceptions are | 
| 53 |  |  |  |  |  |  | also forwarded to the caller, but after the connection is closed, if | 
| 54 |  |  |  |  |  |  | needed. | 
| 55 |  |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  | =cut | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  | sub connect_if_necessary { | 
| 59 | 0 |  |  | 0 | 1 |  | my($self, $sub) = @_; | 
| 60 | 0 |  |  |  |  |  | my $connected = $self->Connected; | 
| 61 | 0 |  |  |  |  |  | my $do_disconnect; | 
| 62 | 0 | 0 |  |  |  |  | if (!$connected) { | 
| 63 | 0 |  |  |  |  |  | $self->connect; | 
| 64 | 0 |  |  |  |  |  | $do_disconnect=1; | 
| 65 |  |  |  |  |  |  | } | 
| 66 | 0 |  |  |  |  |  | my $wantarray = wantarray; | 
| 67 | 0 |  |  |  |  |  | my @r; | 
| 68 | 0 |  |  |  |  |  | eval { | 
| 69 | 0 | 0 |  |  |  |  | if ($wantarray) { | 
| 70 | 0 |  |  |  |  |  | @r = $sub->(); | 
| 71 |  |  |  |  |  |  | } else { | 
| 72 | 0 |  |  |  |  |  | $r[0] = $sub->(); | 
| 73 |  |  |  |  |  |  | } | 
| 74 |  |  |  |  |  |  | }; | 
| 75 | 0 |  |  |  |  |  | my $err = $@; | 
| 76 | 0 | 0 |  |  |  |  | if ($do_disconnect) { | 
| 77 | 0 |  |  |  |  |  | $self->disconnect; | 
| 78 |  |  |  |  |  |  | } | 
| 79 | 0 | 0 |  |  |  |  | if ($err) { | 
| 80 | 0 |  |  |  |  |  | require Carp; | 
| 81 | 0 |  |  |  |  |  | Carp::croak($err); | 
| 82 |  |  |  |  |  |  | } | 
| 83 | 0 | 0 |  |  |  |  | if ($wantarray) { | 
| 84 | 0 |  |  |  |  |  | @r; | 
| 85 |  |  |  |  |  |  | } else { | 
| 86 | 0 |  |  |  |  |  | $r[0]; | 
| 87 |  |  |  |  |  |  | } | 
| 88 |  |  |  |  |  |  | } | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | =item disconnect | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | Disconnect the database. No further access on the database may be done. | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | =cut | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | sub disconnect { | 
| 97 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 98 | 0 | 0 |  |  |  |  | if ($self->Connected) { | 
| 99 | 0 |  |  |  |  |  | eval { | 
| 100 | 0 |  |  |  |  |  | untie %{ $self->{DB} }; | 
|  | 0 |  |  |  |  |  |  | 
| 101 | 0 | 0 |  |  |  |  | };warn $@ if $@; | 
| 102 | 0 |  |  |  |  |  | $self->Connected(0); | 
| 103 |  |  |  |  |  |  | } | 
| 104 |  |  |  |  |  |  | } | 
| 105 |  |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  | =item delete_db | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  | Delete the database completely, including the disk file. | 
| 109 |  |  |  |  |  |  |  | 
| 110 |  |  |  |  |  |  | =cut | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  | sub delete_db { | 
| 113 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 114 | 0 |  |  |  |  |  | unlink $self->DBFile; | 
| 115 |  |  |  |  |  |  | } | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | 1; | 
| 118 |  |  |  |  |  |  |  | 
| 119 |  |  |  |  |  |  | __END__ |