| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Eidolon::Driver::DB; | 
| 2 |  |  |  |  |  |  | # ============================================================================== | 
| 3 |  |  |  |  |  |  | # | 
| 4 |  |  |  |  |  |  | #   Eidolon | 
| 5 |  |  |  |  |  |  | #   Copyright (c) 2009, Atma 7 | 
| 6 |  |  |  |  |  |  | #   --- | 
| 7 |  |  |  |  |  |  | #   Eidolon/Driver/DB.pm - generic database driver | 
| 8 |  |  |  |  |  |  | # | 
| 9 |  |  |  |  |  |  | # ============================================================================== | 
| 10 |  |  |  |  |  |  |  | 
| 11 | 1 |  |  | 1 |  | 4094 | use base qw/Eidolon::Driver/; | 
|  | 1 |  |  |  |  | 4 |  | 
|  | 1 |  |  |  |  | 1093 |  | 
| 12 | 1 |  |  | 1 |  | 560 | use Eidolon::Driver::DB::Exceptions; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 34 |  | 
| 13 | 1 |  |  | 1 |  | 6837 | use DBI; | 
|  | 1 |  |  |  |  | 75090 |  | 
|  | 1 |  |  |  |  | 150 |  | 
| 14 | 1 |  |  | 1 |  | 14 | use warnings; | 
|  | 1 |  |  |  |  | 4 |  | 
|  | 1 |  |  |  |  | 98 |  | 
| 15 | 1 |  |  | 1 |  | 7 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 2203 |  | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | our $VERSION = "0.02"; # 2009-05-14 05:18:37 | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | # ------------------------------------------------------------------------------ | 
| 20 |  |  |  |  |  |  | # \% new($dbd, $db, $user, $password, $host, $port, $cfg) | 
| 21 |  |  |  |  |  |  | # constructor | 
| 22 |  |  |  |  |  |  | # ------------------------------------------------------------------------------ | 
| 23 |  |  |  |  |  |  | sub new | 
| 24 |  |  |  |  |  |  | { | 
| 25 | 0 |  |  | 0 | 1 |  | my ($class, $dbd, $db, $user, $password, $host, $port, $cfg, $self); | 
| 26 |  |  |  |  |  |  |  | 
| 27 | 0 |  |  |  |  |  | ($class, $dbd, $db, $user, $password, $host, $port, $cfg) = @_; | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | # class attributes | 
| 30 | 0 | 0 | 0 |  |  |  | $self = | 
|  |  | 0 | 0 |  |  |  |  | 
| 31 |  |  |  |  |  |  | { | 
| 32 |  |  |  |  |  |  | # connection settings | 
| 33 |  |  |  |  |  |  | "dbd"         => $dbd, | 
| 34 |  |  |  |  |  |  | "database"    => $db, | 
| 35 |  |  |  |  |  |  | "user"        => $user, | 
| 36 |  |  |  |  |  |  | "password"    => $password, | 
| 37 |  |  |  |  |  |  | "host"        => $host, | 
| 38 |  |  |  |  |  |  | "port"        => $port, | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | # other settings | 
| 41 |  |  |  |  |  |  | "auto_fetch"  => $cfg && exists $cfg->{"auto_fetch"}  ? $cfg->{"auto_fetch"}  : 0, | 
| 42 |  |  |  |  |  |  | "auto_commit" => $cfg && exists $cfg->{"auto_commit"} ? $cfg->{"auto_commit"} : 1, | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | # handles | 
| 45 |  |  |  |  |  |  | "dbh"         => undef, | 
| 46 |  |  |  |  |  |  | "sth"         => undef, | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | # data | 
| 49 |  |  |  |  |  |  | "query"       => undef, | 
| 50 |  |  |  |  |  |  | "query_count" => 0, | 
| 51 |  |  |  |  |  |  | "rows"        => 0, | 
| 52 |  |  |  |  |  |  | "dataset"     => [] | 
| 53 |  |  |  |  |  |  | }; | 
| 54 |  |  |  |  |  |  |  | 
| 55 | 0 |  |  |  |  |  | bless $self, $class; | 
| 56 | 0 |  |  |  |  |  | $self->_init(); | 
| 57 |  |  |  |  |  |  |  | 
| 58 | 0 |  |  |  |  |  | return $self; | 
| 59 |  |  |  |  |  |  | } | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | # ------------------------------------------------------------------------------ | 
| 62 |  |  |  |  |  |  | # _init() | 
| 63 |  |  |  |  |  |  | # class initialization | 
| 64 |  |  |  |  |  |  | # ------------------------------------------------------------------------------ | 
| 65 |  |  |  |  |  |  | sub _init | 
| 66 |  |  |  |  |  |  | { | 
| 67 | 0 |  |  | 0 |  |  | my $self = shift; | 
| 68 |  |  |  |  |  |  |  | 
| 69 |  |  |  |  |  |  | eval | 
| 70 | 0 |  |  |  |  |  | { | 
| 71 | 0 |  |  |  |  |  | $self->{"dbh"} = DBI->connect | 
| 72 |  |  |  |  |  |  | ( | 
| 73 |  |  |  |  |  |  | "DBI:$self->{'dbd'}:database=$self->{'database'};host=$self->{'host'};port=$self->{'port'}", | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  | $self->{"user"}, | 
| 76 |  |  |  |  |  |  | $self->{"password"}, | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | { | 
| 79 |  |  |  |  |  |  | "RaiseError"      => 1, | 
| 80 |  |  |  |  |  |  | "PrintError"      => 0, | 
| 81 |  |  |  |  |  |  | "PrintWarn"       => 0, | 
| 82 |  |  |  |  |  |  | "InactiveDestroy" => 1, | 
| 83 |  |  |  |  |  |  | "AutoCommit"      => $self->{"auto_commit"} | 
| 84 |  |  |  |  |  |  | } | 
| 85 |  |  |  |  |  |  | ); | 
| 86 |  |  |  |  |  |  | }; | 
| 87 |  |  |  |  |  |  |  | 
| 88 | 0 | 0 |  |  |  |  | throw DriverError::DB::Connect($@) if ($@); | 
| 89 |  |  |  |  |  |  | } | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | # ------------------------------------------------------------------------------ | 
| 92 |  |  |  |  |  |  | # \@ execute($query, @params) | 
| 93 |  |  |  |  |  |  | # query execution | 
| 94 |  |  |  |  |  |  | # ------------------------------------------------------------------------------ | 
| 95 |  |  |  |  |  |  | sub execute | 
| 96 |  |  |  |  |  |  | { | 
| 97 | 0 |  |  | 0 | 1 |  | my ($self, $query, @params, $row); | 
| 98 |  |  |  |  |  |  |  | 
| 99 | 0 |  |  |  |  |  | ($self, $query, @params) = @_; | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | eval | 
| 102 | 0 |  |  |  |  |  | { | 
| 103 | 0 |  |  |  |  |  | $self->free; | 
| 104 |  |  |  |  |  |  |  | 
| 105 | 0 |  |  |  |  |  | $self->{"query"} = $query; | 
| 106 | 0 |  |  |  |  |  | $self->{"query_count"}++; | 
| 107 |  |  |  |  |  |  |  | 
| 108 | 0 |  |  |  |  |  | $self->{"sth"}  = $self->{"dbh"}->prepare($query); | 
| 109 | 0 |  |  |  |  |  | $self->{"rows"} = $self->{"sth"}->execute(@params); | 
| 110 |  |  |  |  |  |  | }; | 
| 111 |  |  |  |  |  |  |  | 
| 112 | 0 | 0 |  |  |  |  | throw DriverError::DB::SQL($query) if ($@); | 
| 113 |  |  |  |  |  |  |  | 
| 114 | 0 | 0 |  |  |  |  | if ($self->{"auto_fetch"}) | 
| 115 |  |  |  |  |  |  | { | 
| 116 | 0 |  |  |  |  |  | $self->_fetch_all; | 
| 117 | 0 |  |  |  |  |  | return $self->{"dataset"}; | 
| 118 |  |  |  |  |  |  | } | 
| 119 |  |  |  |  |  |  | } | 
| 120 |  |  |  |  |  |  |  | 
| 121 |  |  |  |  |  |  | # ------------------------------------------------------------------------------ | 
| 122 |  |  |  |  |  |  | # \@ execute_prepared(@params) | 
| 123 |  |  |  |  |  |  | # execute prepared query | 
| 124 |  |  |  |  |  |  | # ------------------------------------------------------------------------------ | 
| 125 |  |  |  |  |  |  | sub execute_prepared | 
| 126 |  |  |  |  |  |  | { | 
| 127 | 0 |  |  | 0 | 1 |  | my ($self, @params, $row); | 
| 128 |  |  |  |  |  |  |  | 
| 129 | 0 |  |  |  |  |  | ($self, @params) = @_; | 
| 130 |  |  |  |  |  |  |  | 
| 131 |  |  |  |  |  |  | eval | 
| 132 | 0 |  |  |  |  |  | { | 
| 133 | 0 |  |  |  |  |  | $self->free; | 
| 134 |  |  |  |  |  |  |  | 
| 135 | 0 |  |  |  |  |  | $self->{"query_count"}++; | 
| 136 | 0 |  |  |  |  |  | $self->{"rows"} = $self->{"sth"}->execute(@params); | 
| 137 |  |  |  |  |  |  | }; | 
| 138 |  |  |  |  |  |  |  | 
| 139 | 0 | 0 |  |  |  |  | throw DriverError::DB::SQL($self->{"query"}) if ($@); | 
| 140 |  |  |  |  |  |  |  | 
| 141 | 0 | 0 |  |  |  |  | if ($self->{"auto_fetch"}) | 
| 142 |  |  |  |  |  |  | { | 
| 143 | 0 |  |  |  |  |  | $self->_fetch_all; | 
| 144 | 0 |  |  |  |  |  | return $self->{"dataset"}; | 
| 145 |  |  |  |  |  |  | } | 
| 146 |  |  |  |  |  |  | } | 
| 147 |  |  |  |  |  |  |  | 
| 148 |  |  |  |  |  |  | # ------------------------------------------------------------------------------ | 
| 149 |  |  |  |  |  |  | # fetch() | 
| 150 |  |  |  |  |  |  | # fetch result | 
| 151 |  |  |  |  |  |  | # ------------------------------------------------------------------------------ | 
| 152 |  |  |  |  |  |  | sub fetch | 
| 153 |  |  |  |  |  |  | { | 
| 154 | 0 |  |  | 0 | 1 |  | my ($self, $row); | 
| 155 |  |  |  |  |  |  |  | 
| 156 | 0 |  |  |  |  |  | $self = shift; | 
| 157 |  |  |  |  |  |  |  | 
| 158 | 0 | 0 |  |  |  |  | throw DriverError::DB::AlreadyFetched if ($self->{"auto_fetch"}); | 
| 159 | 0 |  |  |  |  |  | $row  = $self->{"sth"}->fetchrow_hashref; | 
| 160 |  |  |  |  |  |  |  | 
| 161 | 0 |  |  |  |  |  | return $row; | 
| 162 |  |  |  |  |  |  | } | 
| 163 |  |  |  |  |  |  |  | 
| 164 |  |  |  |  |  |  | # ------------------------------------------------------------------------------ | 
| 165 |  |  |  |  |  |  | # _fetch_all() | 
| 166 |  |  |  |  |  |  | # private version of fetch all results | 
| 167 |  |  |  |  |  |  | # ------------------------------------------------------------------------------ | 
| 168 |  |  |  |  |  |  | sub _fetch_all | 
| 169 |  |  |  |  |  |  | { | 
| 170 | 0 |  |  | 0 |  |  | my ($self, $row); | 
| 171 |  |  |  |  |  |  |  | 
| 172 | 0 |  |  |  |  |  | $self = shift; | 
| 173 |  |  |  |  |  |  |  | 
| 174 | 0 |  |  |  |  |  | while ($row = $self->{"sth"}->fetchrow_hashref) | 
| 175 |  |  |  |  |  |  | { | 
| 176 | 0 |  |  |  |  |  | push @{ $self->{"dataset"} }, $row; | 
|  | 0 |  |  |  |  |  |  | 
| 177 |  |  |  |  |  |  | } | 
| 178 |  |  |  |  |  |  | } | 
| 179 |  |  |  |  |  |  |  | 
| 180 |  |  |  |  |  |  | # ------------------------------------------------------------------------------ | 
| 181 |  |  |  |  |  |  | # fetch_all() | 
| 182 |  |  |  |  |  |  | # fetch all results | 
| 183 |  |  |  |  |  |  | # ------------------------------------------------------------------------------ | 
| 184 |  |  |  |  |  |  | sub fetch_all | 
| 185 |  |  |  |  |  |  | { | 
| 186 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 187 |  |  |  |  |  |  |  | 
| 188 | 0 | 0 |  |  |  |  | throw DriverError::DB::AlreadyFetched if ($self->{"auto_fetch"}); | 
| 189 | 0 |  |  |  |  |  | $self->_fetch_all; | 
| 190 |  |  |  |  |  |  |  | 
| 191 | 0 |  |  |  |  |  | return $self->{"dataset"}; | 
| 192 |  |  |  |  |  |  | } | 
| 193 |  |  |  |  |  |  |  | 
| 194 |  |  |  |  |  |  | # ------------------------------------------------------------------------------ | 
| 195 |  |  |  |  |  |  | # free() | 
| 196 |  |  |  |  |  |  | # free memory | 
| 197 |  |  |  |  |  |  | # ------------------------------------------------------------------------------ | 
| 198 |  |  |  |  |  |  | sub free | 
| 199 |  |  |  |  |  |  | { | 
| 200 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 201 |  |  |  |  |  |  |  | 
| 202 | 0 | 0 |  |  |  |  | $self->{"sth"}->finish  if ($self->{"sth"}); | 
| 203 |  |  |  |  |  |  |  | 
| 204 | 0 | 0 |  |  |  |  | if ($self->{"dataset"}) | 
| 205 |  |  |  |  |  |  | { | 
| 206 | 0 |  |  |  |  |  | undef $self->{"dataset"}; | 
| 207 | 0 |  |  |  |  |  | $self->{"dataset"} = []; | 
| 208 |  |  |  |  |  |  | } | 
| 209 |  |  |  |  |  |  | } | 
| 210 |  |  |  |  |  |  |  | 
| 211 |  |  |  |  |  |  | # ------------------------------------------------------------------------------ | 
| 212 |  |  |  |  |  |  | # \@ call($function, @params) | 
| 213 |  |  |  |  |  |  | # stored function/procedure call | 
| 214 |  |  |  |  |  |  | # ------------------------------------------------------------------------------ | 
| 215 |  |  |  |  |  |  | sub call | 
| 216 |  |  |  |  |  |  | { | 
| 217 | 0 |  |  | 0 | 1 |  | throw CoreError::AbstractMethod; | 
| 218 |  |  |  |  |  |  | } | 
| 219 |  |  |  |  |  |  |  | 
| 220 |  |  |  |  |  |  | # ------------------------------------------------------------------------------ | 
| 221 |  |  |  |  |  |  | # DESTROY() | 
| 222 |  |  |  |  |  |  | # destructor | 
| 223 |  |  |  |  |  |  | # ------------------------------------------------------------------------------ | 
| 224 |  |  |  |  |  |  | sub DESTROY | 
| 225 |  |  |  |  |  |  | { | 
| 226 | 0 | 0 |  | 0 |  |  | $_[0]->{"dbh"}->disconnect if ($_[0]->{"dbh"}); | 
| 227 |  |  |  |  |  |  | } | 
| 228 |  |  |  |  |  |  |  | 
| 229 |  |  |  |  |  |  | 1; | 
| 230 |  |  |  |  |  |  |  | 
| 231 |  |  |  |  |  |  | __END__ |