File Coverage

blib/lib/DBIx/YAWM.pm
Criterion Covered Total %
statement 9 62 14.5
branch 0 30 0.0
condition 0 9 0.0
subroutine 3 6 50.0
pod 0 3 0.0
total 12 110 10.9


line stmt bran cond sub pod time code
1             ###################################################
2             ## YAWM.pm
3             ## Andrew N. Hicox
4             ## http://www.hicox.com
5             ##
6             ## Yet Annother Wrapper Module
7             ## Handy tools for talking to databases
8             ###################################################
9              
10              
11             ## Global Stuff ###################################
12             package DBIx::YAWM;
13 1     1   1075 use 5.6.0;
  1         4  
  1         61  
14 1     1   5 use warnings;
  1         2  
  1         55  
15              
16             require Exporter;
17 1     1   1245 use AutoLoader qw(AUTOLOAD);
  1         2003  
  1         8  
18            
19             ## Class Global Values ############################
20             our @ISA = qw(Exporter);
21             our $VERSION = 2.35;
22             our $errstr = ();
23             our @EXPORT_OK = ($VERSION, $errstr);
24              
25              
26             ## new ############################################
27             sub new {
28             #local vars
29 0     0 0   my %p = @_;
30 0           my $obj = bless ({});
31             #you must at least, include Server, User, Pass, and DBType
32 0 0 0       unless (
      0        
33             (exists ($p{Server})) &&
34             (exists ($p{User})) &&
35             (exists ($p{DBType}))
36             ){
37 0           $errstr = "Server, User, and DBType are required options to New";
38 0           return (undef);
39             }
40             #if it's Oracle, we'll be needing a SID too
41 0 0 0       if (($p{DBType} eq "Oracle") && (! exists ($p{SID}))){
42 0           $errstr = "SID is a required option for DBType Oracle";
43 0           return (undef);
44             }
45             #add in anything which might have been sent in
46 0           foreach (keys %p){ $obj->{$_} = $p{$_}; }
  0            
47             #default values
48 0 0         $obj->{'LongReadLen'} = 15000 unless (exists($obj->{'LongReadLen'}));
49 0 0         $obj->{'LongTruncOk'} = 0 unless (exists($obj->{'LongTruncOk'}));
50             #login to database
51 0 0         unless ($obj->Login()){
52 0           $errstr = $obj->{errstr};
53 0           return (undef);
54             }
55             #return object
56 0           return ($obj);
57             }
58              
59              
60             ## Login ##########################################
61             sub Login {
62             #local vars
63 0     0 0   my $self = shift();
64 0           my %p = @_;
65 0           my ($connect_str, @connect_args) = ();
66             #are we already logged in?
67 0 0         if (exists ($self->{dbh})){ return (1); }
  0            
68             #require appropriate dbi module
69 0           my $mod = "DBD\::$self->{DBType}";
70 0           eval "require $mod";
71 0 0         if ($@){
72 0           $self->{'errstr'} = "Login: failed to load DBD module $mod: $@";
73 0           return (undef);
74             }
75             #wow, a "connection string" ...
76 0 0         if ($self->{DBType} eq "Sybase"){
    0          
    0          
77 0           $connect_str = "dbi:Sybase:server=$self->{Server}";
78             }elsif ($self->{DBType} eq "Oracle"){
79             #if we have a port number we could give that too
80 0 0         if (exists($self->{Port})){
81 0           $connect_str = "dbi:Oracle:host=$self->{Server};sid=$self->{SID};port=$self->{Port}";
82             }else{
83 0           $connect_str = "dbi:Oracle:host=$self->{Server};sid=$self->{SID}";
84             }
85             }elsif ($self->{DBType} eq "mysql"){
86 0           $connect_str = "dbi:mysql";
87 0 0         if ($self->{'Database'}){ $connect_str .= ":database=$self->{'Database'}"; }
  0            
88 0           $connect_str .= ";host=$self->{Server};";
89 0 0         if ($self->{'Port'}){ $connect_str .= "port=$self->{'Port'};"; }
  0            
90             }else{
91             #wow this is really ghetto
92 0           $self->{errstr} = "Sorry Dude, ";
93 0           $self->{errstr}.= "I don't know how to make connection strings for this DBType ";
94 0           $self->{errstr}.= "someone needs to edit YAWM.pm";
95 0           return (undef);
96             }
97 0           push (@connect_args, $connect_str);
98 0           push (@connect_args, $self->{User});
99 0 0         push (@connect_args, $self->{Pass}) if (exists($self->{Pass}));
100             #make the connection
101 0 0         unless ($self->{dbh} = DBI->connect(@connect_args)){
102 0           $self->{errstr} = "Login failed: $DBI::errstr";
103 0           return (undef);
104             }
105             #go ahead and set LongReadLen and LongTruncOk
106 0           $self->{dbh}->{'LongReadLen'} = $self->{'LongReadLen'};
107 0           $self->{dbh}->{'LongTruncOk'} = $self->{'LongTruncOk'};
108             #it's all good baby bay bay ...
109 0           return (1);
110             }
111              
112              
113             ## Destroy ########################################
114             sub Destroy {
115 0     0 0   my $self = shift;
116 0           $self->{dbh}->disconnect;
117 0           $self = undef;
118             }
119            
120              
121             ## True for perl include ##########################
122             1;
123             __END__