| 1 | ################################################### | ||||||||||||||||||||||||||||||||||||||||||
| 2 | ## YAWM.pm | ||||||||||||||||||||||||||||||||||||||||||
| 3 | ## Andrew N. Hicox <andrew@hicox.com> | ||||||||||||||||||||||||||||||||||||||||||
| 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 | ▶ | use 5.6.0; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 1 | require Exporter; | |||||||||||||||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use AutoLoader qw(AUTOLOAD); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | ## Class Global Values ############################ | ||||||||||||||||||||||||||||||||||||||||||
| 20 | 1 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||||||||||||||||
| 21 | 1 | our $VERSION = 2.35; | |||||||||||||||||||||||||||||||||||||||||
| 22 | 1 | our $errstr = (); | |||||||||||||||||||||||||||||||||||||||||
| 23 | 1 | our @EXPORT_OK = ($VERSION, $errstr); | |||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | ## new ############################################ | ||||||||||||||||||||||||||||||||||||||||||
| 27 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=10 SCAR=47.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | #local vars | ||||||||||||||||||||||||||||||||||||||||||
| 29 | 0 | my %p = @_; | |||||||||||||||||||||||||||||||||||||||||
| 30 | 0 | my $obj = bless ({}); | |||||||||||||||||||||||||||||||||||||||||
| 31 | #you must at least, include Server, User, Pass, and DBType | ||||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | ▶ | unless ( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
exists $p{"Server"} and exists $p{"User"} and exists $p{"DBType"}
exists $p{"Server"} and exists $p{"User"}
MC/DC0%0/3
exists $p{"Server"} and exists $p{"User"} and exists $p{"DBType"}
exists $p{"Server"}
exists $p{"User"}
exists $p{"DBType"}
Truth table0%0/4
A and B and C
A= exists $p{"Server"}B= exists $p{"User"}C= exists $p{"DBType"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | ▶ | if (($p{DBType} eq "Oracle") && (! exists ($p{SID}))){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$p{"DBType"} eq "Oracle" and not exists $p{"SID"}
MC/DC0%0/2
$p{"DBType"} eq "Oracle" and not exists $p{"SID"}
$p{"DBType"} eq "Oracle"
not exists $p{"SID"}
Truth table0%0/3
A and not(B)
A= $p{"DBType"} eq "Oracle"B= not 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{$_}; } | |||||||||||||||||||||||||||||||||||||||||
| 47 | #default values | ||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | ▶ | $obj->{'LongReadLen'} = 15000 unless (exists($obj->{'LongReadLen'})); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | $obj->{'LongTruncOk'} = 0 unless (exists($obj->{'LongTruncOk'})); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | #login to database | ||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | ▶ | unless ($obj->Login()){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | $errstr = $obj->{errstr}; | |||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | return (undef); | |||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||
| 55 | #return object | ||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | return ($obj); | |||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | ## Login ########################################## | ||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | sub Login { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Login: not called CC=11 SCAR=48.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | #local vars | ||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | my $self = shift(); | |||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | my %p = @_; | |||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | my ($connect_str, @connect_args) = (); | |||||||||||||||||||||||||||||||||||||||||
| 66 | #are we already logged in? | ||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | ▶ | if (exists ($self->{dbh})){ return (1); } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | #require appropriate dbi module | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | my $mod = "DBD\::$self->{DBType}"; | |||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | eval "require $mod"; | |||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | ▶ | if ($@){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | $self->{'errstr'} = "Login: failed to load DBD module $mod: $@"; | |||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | return (undef); | |||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||
| 75 | #wow, a "connection string" ... | ||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | ▶ | if ($self->{DBType} eq "Sybase"){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | ▶ | if (exists($self->{Port})){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | ▶ | if ($self->{'Database'}){ $connect_str .= ":database=$self->{'Database'}"; } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | $connect_str .= ";host=$self->{Server};"; | |||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | ▶ | if ($self->{'Port'}){ $connect_str .= "port=$self->{'Port'};"; } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | ▶ | push (@connect_args, $self->{Pass}) if (exists($self->{Pass})); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | #make the connection | ||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | ▶ | unless ($self->{dbh} = DBI->connect(@connect_args)){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | 0 | ▶ | sub Destroy { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Destroy: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | $self->{dbh}->disconnect; | |||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | $self = undef; | |||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | ## True for perl include ########################## | ||||||||||||||||||||||||||||||||||||||||||
| 122 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 123 | __END__ | ||||||||||||||||||||||||||||||||||||||||||