| 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 | ▶ | use base qw(Class::Accessor); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 35 | |||||||||||
| 36 | 22 | __PACKAGE__->mk_accessors(qw(Root Connected)); | |||||||||
| 37 | |||||||||||
| 38 | 22 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 39 | 22 | ▶ | use vars qw($VERSION); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 40 | 22 | $VERSION = sprintf("%d.%02d", q$Revision: 1.6 $ =~ /(\d+)\.(\d+)/); | |||||||||
| 41 | |||||||||||
| 42 | 0 | ▶ | sub new { | ||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 43 | 0 | 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<connect_if_necessary>. Exceptions are | ||||||||||
| 53 | also forwarded to the caller, but after the connection is closed, if | ||||||||||
| 54 | needed. | ||||||||||
| 55 | |||||||||||
| 56 | =cut | ||||||||||
| 57 | |||||||||||
| 58 | 0 | ▶ | sub connect_if_necessary { | ||||||||
|
Subroutine
connect_if_necessary: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||
| 59 | 0 | my($self, $sub) = @_; | |||||||||
| 60 | 0 | my $connected = $self->Connected; | |||||||||
| 61 | 0 | my $do_disconnect; | |||||||||
| 62 | 0 | ▶ | if (!$connected) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | 0 | $self->connect; | |||||||||
| 64 | 0 | $do_disconnect=1; | |||||||||
| 65 | } | ||||||||||
| 66 | 0 | my $wantarray = wantarray; | |||||||||
| 67 | 0 | my @r; | |||||||||
| 68 | 0 | eval { | |||||||||
| 69 | 0 | ▶ | if ($wantarray) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | 0 | @r = $sub->(); | |||||||||
| 71 | } else { | ||||||||||
| 72 | 0 | $r[0] = $sub->(); | |||||||||
| 73 | } | ||||||||||
| 74 | }; | ||||||||||
| 75 | 0 | my $err = $@; | |||||||||
| 76 | 0 | ▶ | if ($do_disconnect) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | 0 | $self->disconnect; | |||||||||
| 78 | } | ||||||||||
| 79 | 0 | ▶ | if ($err) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | 0 | require Carp; | |||||||||
| 81 | 0 | Carp::croak($err); | |||||||||
| 82 | } | ||||||||||
| 83 | 0 | ▶ | if ($wantarray) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | 0 | ▶ | sub disconnect { | ||||||||
|
Subroutine
disconnect: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 97 | 0 | my $self = shift; | |||||||||
| 98 | 0 | ▶ | if ($self->Connected) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 99 | 0 | eval { | |||||||||
| 100 | 0 | untie %{ $self->{DB} }; | |||||||||
| 101 | 0 | ▶ | };warn $@ if $@; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | 0 | ▶ | sub delete_db { | ||||||||
|
Subroutine
delete_db: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 113 | 0 | my $self = shift; | |||||||||
| 114 | 0 | unlink $self->DBFile; | |||||||||
| 115 | } | ||||||||||
| 116 | |||||||||||
| 117 | 22 | 1; | |||||||||
| 118 | |||||||||||
| 119 | __END__ | ||||||||||