| 1 | # $DBIx::SQL::Abstract.pm,v 1.1 2005/09/06 14:15:53 alex Exp $ | ||||||||||
| 2 | # | ||||||||||
| 3 | # Copyright (c) 2004 Alejandro Juarez <alex@bsdcoders.org> | ||||||||||
| 4 | # | ||||||||||
| 5 | # Permission to use, copy, modify, and distribute this software for any | ||||||||||
| 6 | # purpose with or without fee is hereby granted, provided that the above | ||||||||||
| 7 | # copyright notice and this permission notice appear in all copies. | ||||||||||
| 8 | # | ||||||||||
| 9 | # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||||||||||
| 10 | # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||||||||||
| 11 | # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||||||||||
| 12 | # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||||||||||
| 13 | # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||||||||||
| 14 | # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||||||||||
| 15 | # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||||||||||
| 16 | # | ||||||||||
| 17 | |||||||||||
| 18 | # PURPOSE: This module was created to serve several functions inhereted | ||||||||||
| 19 | # from the DBIx and SQL::Abstract modules ... | ||||||||||
| 20 | # | ||||||||||
| 21 | # USAGE: To read the HOW TO USE instructions you need to run perldoc: | ||||||||||
| 22 | # perldoc DBIx::SQL::Abstract | ||||||||||
| 23 | |||||||||||
| 24 | |||||||||||
| 25 | package DBIx::SQL::Abstract; | ||||||||||
| 26 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 27 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 28 | 1 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 29 | 1 | ▶ | use base 'DBIx'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 30 | 1 | ▶ | use SQL::Abstract; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 31 | 1 | ▶ | use DBI; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 32 | 1 | ▶ | use vars qw(@ISA); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 33 | 1 | require Exporter; | |||||||||
| 34 | 1 | @ISA = qw(DBI DBI::db DBI::st SQL::Abstract); | |||||||||
| 35 | |||||||||||
| 36 | 1 | our $VERSION = '0.07'; | |||||||||
| 37 | |||||||||||
| 38 | 0 | ▶ | sub new { | ||||||||
|
Subroutine
new: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||
| 39 | 0 | my $class = shift; | |||||||||
| 40 | 0 | my %params = @_; | |||||||||
| 41 | |||||||||||
| 42 | # Setting the DBIx and database default parameters | ||||||||||
| 43 | 0 | my $db = { driver => 'Pg', | |||||||||
| 44 | dbname => 'db', | ||||||||||
| 45 | host => undef, | ||||||||||
| 46 | port => undef, | ||||||||||
| 47 | user => 'user', | ||||||||||
| 48 | passwd => undef, | ||||||||||
| 49 | attr => undef, # Used for DBI attributes | ||||||||||
| 50 | }; | ||||||||||
| 51 | |||||||||||
| 52 | # Setting the DBIx and database default attributes | ||||||||||
| 53 | 0 | my $dbiattr = { PrintError => 1, | |||||||||
| 54 | RaiseError => 0, | ||||||||||
| 55 | AutoCommit => 0, | ||||||||||
| 56 | ChopBlanks => 1 | ||||||||||
| 57 | }; | ||||||||||
| 58 | 0 | my $knownargs = join ('|', keys %$db); | |||||||||
| 59 | |||||||||||
| 60 | # Checking for 2 explicit options in the arguments | ||||||||||
| 61 | 0 | ▶ | if ( $#_ >= 3 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | # Checking if here we had unknown arguments | ||||||||||
| 63 | 0 | my @unknownargs = grep { $_ !~ /^($knownargs)$/ } keys %params; | |||||||||
| 64 | |||||||||||
| 65 | 0 | ▶ | if ( ! @unknownargs ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | |||||||||||
| 67 | # Checking for the existence of explicit args: dbname && user | ||||||||||
| 68 | 0 | my @minargs = map { $_ =~ /^(dbname|user)$/ } keys %params; | |||||||||
| 69 | 0 | ▶ | if ( $#minargs == 1) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | |||||||||||
| 71 | # Setting the arguments for the database connection | ||||||||||
| 72 | 0 | map { $db->{$_} = $params{$_} } keys %params; | |||||||||
| 73 | 0 | my ($dbLine, @dbargs); | |||||||||
| 74 | 0 | $dbLine = "dbi:$db->{driver}:dbname='$db->{dbname}'"; | |||||||||
| 75 | 0 | push @dbargs, $dbLine, $db->{user}, $db->{passwd}; | |||||||||
| 76 | |||||||||||
| 77 | # Setting the DBI Attributes | ||||||||||
| 78 | 0 | ▶ | if ( $db->{attr} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | 0 | my $attr = $db->{attr}; #used only for a better style | |||||||||
| 80 | 0 | map { $dbiattr->{$_} = $attr->{$_} } keys %$attr; | |||||||||
| 81 | } | ||||||||||
| 82 | |||||||||||
| 83 | # Here, All is Right, we'll open the database connection | ||||||||||
| 84 | 0 | ▶ | my $dbh = DBI->connect(@dbargs, \%$dbiattr) or | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | die ("Failed to open database connection:\n", | ||||||||||
| 86 | $DBI::errstr); | ||||||||||
| 87 | |||||||||||
| 88 | 0 | return bless $dbh, $class; | |||||||||
| 89 | |||||||||||
| 90 | } else { | ||||||||||
| 91 | 0 | die 'You need the DSN options: [dbname | user]'; | |||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | } else { | ||||||||||
| 95 | 0 | die 'Unknown argument(s) received: ', join(', ', @unknownargs); | |||||||||
| 96 | } | ||||||||||
| 97 | |||||||||||
| 98 | } else { | ||||||||||
| 99 | 0 | die 'You need the DSN options: dbname => DBNAME, user => USER'; | |||||||||
| 100 | } | ||||||||||
| 101 | } | ||||||||||
| 102 | |||||||||||
| 103 | |||||||||||
| 104 | 0 | ▶ | sub DESTROY { | ||||||||
|
Subroutine
DESTROY: not called CC=3 SCAR=24.8
| |||||||||||
| 105 | # If we are not in autocommit mode, roll back any transactions left | ||||||||||
| 106 | # pending. Cleanly disconnect from the database before disappearing. | ||||||||||
| 107 | 0 | my $self = shift; | |||||||||
| 108 | |||||||||||
| 109 | 0 | ▶ | if (ref $self ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 110 | 0 | ▶ | if ($self->{AutoCommit} == 1 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 111 | 0 | $self->commit; | |||||||||
| 112 | } else { | ||||||||||
| 113 | 0 | $self->rollback; | |||||||||
| 114 | } | ||||||||||
| 115 | } | ||||||||||
| 116 | |||||||||||
| 117 | 0 | return 1; | |||||||||
| 118 | } | ||||||||||
| 119 | |||||||||||
| 120 | |||||||||||
| 121 | 1 | 1; | |||||||||
| 122 | __END__ | ||||||||||