| 1 | package DBIx::DBObj; | ||
| 2 | |||
| 3 | # Copyright (C) 2003 Matt Knopp <mhat@cpan.org> | ||
| 4 | # This library is free software released under the GNU Lesser General Public | ||
| 5 | # License, Version 2.1. Please read the important licensing and disclaimer | ||
| 6 | # information included in the LICENSE file included with this distribution. | ||
| 7 | |||
| 8 | use strict; | ||
| 9 | no strict 'refs'; | ||
| 10 | use Error qw (:try); | ||
| 11 | use Error::Better; | ||
| 12 | use DBIx::SimpleDBI; | ||
| 13 | |||
| 14 | our $VERSION = 1.00; | ||
| 15 | |||
| 16 | sub new { | ||
| 17 | my ($class) = @_; | ||
| 18 | my $objTable = ${$class . "::DBObjTable"}; | ||
| 19 | my $objThrow = ${$class . "::DBObjThrow"}; | ||
| 20 | my $objTypes = \%{$class . "::DBObjTypes"}; | ||
| 21 | my $objPKeys = \@{$class . "::DBObjPKeys"}; | ||
| 22 | my $objFields = \@{$class . "::DBObjFields"}; | ||
| 23 | |||
| 24 | ## Subclasses must have a DBObjFields && PKeys list | ||
| 25 | ## | ||
| 26 | if (! defined( @{$objFields} )) { | ||
| 27 | throw Error::Better::OperationFailed("DBObjFields is not defined!") | ||
| 28 | if (! defined @{$objFields}); | ||
| 29 | return(undef); | ||
| 30 | } | ||
| 31 | if (! defined( @{$objPKeys} )) { | ||
| 32 | throw Error::Better::OperationFailed("DBObjPKeys is not defined!") | ||
| 33 | if (! defined @{$objFields}); | ||
| 34 | return(undef); | ||
| 35 | } | ||
| 36 | |||
| 37 | throw Error::Better::OperationFailed("$class::DBObjPKeys is not defined.") | ||
| 38 | if (! defined @{$objPKeys}); | ||
| 39 | |||
| 40 | my %ocore = (); | ||
| 41 | my %fields = (); | ||
| 42 | my %defaults = (); | ||
| 43 | |||
| 44 | %defaults = ('_Value' => undef, | ||
| 45 | '_ChangedP' => 0); | ||
| 46 | %ocore = ('_Table' => $objTable, | ||
| 47 | '_Throw' => $objThrow, | ||
| 48 | '_Types' => $objTypes, | ||
| 49 | '_PKeys' => $objPKeys, | ||
| 50 | '_Fields' => \%fields); | ||
| 51 | |||
| 52 | ## Populate the Fields hash from the DBObjFields list. | ||
| 53 | %fields = map { ("_$_" => { %defaults }) } @{$objFields}; | ||
| 54 | |||
| 55 | ## Create some methods ... but only do that if the method in question | ||
| 56 | ## has not already been created. There is no need to be wasteful! or | ||
| 57 | ## to override an explicitly defined method. | ||
| 58 | ## | ||
| 59 | foreach my $field (@{$objFields}) { | ||
| 60 | if (!defined *{$class . "::get$field"}) { | ||
| 61 | *{$class . "::get$field"} = sub { | ||
| 62 | return($_[0]->{"_Fields"}{"_$field"}{"_Value"}); | ||
| 63 | }; | ||
| 64 | } | ||
| 65 | |||
| 66 | if (!defined *{$class . "::set$field"}) { | ||
| 67 | if (defined $objTypes->{$field}) { | ||
| 68 | my $dtclass = $objTypes->{$field}; | ||
| 69 | &{$dtclass . "::addSymbols"}($class, $field); | ||
| 70 | } | ||
| 71 | else { | ||
| 72 | ## Default mutator | ||
| 73 | *{$class . "::set$field"} = sub { | ||
| 74 | return(1) if ($_[1] eq $_[0]->{"_Fields"}{"_$field"}{"_Value"}); | ||
| 75 | $_[0]->{"_Fields"}{"_$field"}{"_Value"} = $_[1]; | ||
| 76 | $_[0]->{"_Fields"}{"_$field"}{"_ChangedP"} = 1 if ($_[2] != 1); | ||
| 77 | return(1); | ||
| 78 | }; | ||
| 79 | } | ||
| 80 | } | ||
| 81 | } | ||
| 82 | return(bless(\%ocore, $class)); | ||
| 83 | } | ||
| 84 | |||
| 85 | |||
| 86 | |||
| 87 | sub find { | ||
| 88 | my ($class, @args) = @_; | ||
| 89 | my %newSearchPairs = (); | ||
| 90 | my @pkeys = @{$class . "::DBObjPKeys"}; | ||
| 91 | my $throw = ${$class . "::DBObjThrow"}; | ||
| 92 | my $results = undef; | ||
| 93 | |||
| 94 | for(my $i=0; $i<scalar(@pkeys); $i++) { | ||
| 95 | $newSearchPairs{$pkeys[$i]}=$args[$i]; | ||
| 96 | } | ||
| 97 | |||
| 98 | ## First I need to make sure I have the right number of keys! | ||
| 99 | if(scalar(@{$class."::DBObjPKeys"}) != scalar(keys(%newSearchPairs))) { | ||
| 100 | throw Error::Better::InvalidArguments ('Invalid arguments.') | ||
| 101 | if ($throw == 1); | ||
| 102 | return(undef); | ||
| 103 | } | ||
| 104 | |||
| 105 | ## Call search() to perfrom the SELECT query | ||
| 106 | try { | ||
| 107 | $results = $class->search(\%newSearchPairs); | ||
| 108 | } otherwise {}; | ||
| 109 | |||
| 110 | ## Return the first (only) entry in the result set. | ||
| 111 | return($results->[0]) if (ref($results->[0])); | ||
| 112 | |||
| 113 | ## If there was an Error! | ||
| 114 | throw Error::Better::ObjectNotFound ("No matching Object.") | ||
| 115 | if ($throw == 1); | ||
| 116 | return(undef); | ||
| 117 | } | ||
| 118 | |||
| 119 | sub search { | ||
| 120 | my ($class, $searchPairs) = @_; | ||
| 121 | my $table = ${$class . "::DBObjTable"}; | ||
| 122 | my $throw = ${$class . "::DBObjThrow"}; | ||
| 123 | my $queryStr = ""; | ||
| 124 | my $whereStr = ""; | ||
| 125 | my $fieldStr = join(",", @{$class . "::DBObjFields"}); | ||
| 126 | my $bindp = 1; | ||
| 127 | my $results = undef; | ||
| 128 | my @objects = (); | ||
| 129 | my @fields = (); | ||
| 130 | my @values = (); | ||
| 131 | |||
| 132 | if($searchPairs =~ m/where/i) { | ||
| 133 | $bindp = 0; | ||
| 134 | $whereStr = $searchPairs; | ||
| 135 | } | ||
| 136 | else { | ||
| 137 | foreach my $field (@{$class . "::DBObjFields"}) { | ||
| 138 | if (defined $searchPairs->{$field}) { | ||
| 139 | push(@fields, join('=', $field, '?')); | ||
| 140 | push(@values, $searchPairs->{"$field"}); | ||
| 141 | } | ||
| 142 | } | ||
| 143 | $bindp = 1; | ||
| 144 | $whereStr = 'WHERE ' . join(' AND ', @fields) if (scalar(@fields)); | ||
| 145 | } | ||
| 146 | |||
| 147 | $queryStr = "SELECT $fieldStr FROM $table $whereStr"; | ||
| 148 | $results = query($queryStr,\@values) if ($bindp == 1); | ||
| 149 | $results = query($queryStr,) if ($bindp == 0); | ||
| 150 | |||
| 151 | ## The new age way to give up. | ||
| 152 | return([]) if (! ref($results) && $throw == 1); | ||
| 153 | |||
| 154 | ## The old school way to give up. | ||
| 155 | return(undef) if (! ref($results)); | ||
| 156 | |||
| 157 | ## Finally transform the SQL result set into a set db objects | ||
| 158 | foreach my $row (@{$results}) { | ||
| 159 | my $count = 0; | ||
| 160 | my $dbobj = $class->new(); | ||
| 161 | foreach my $field (@{$class . "::DBObjFields"}) { | ||
| 162 | &{$class . "::set$field"}($dbobj, $row->[$count], 1); | ||
| 163 | $count++; | ||
| 164 | } | ||
| 165 | push (@objects, $dbobj); | ||
| 166 | } | ||
| 167 | return(\@objects); | ||
| 168 | } | ||
| 169 | |||
| 170 | |||
| 171 | ## Throws SQL Exception | ||
| 172 | sub create { | ||
| 173 | my ($class, $createPairs, $returnObject) = @_; | ||
| 174 | return(undef) unless ($createPairs); | ||
| 175 | my $objTypes = \%{$class . "::DBObjTypes"}; | ||
| 176 | my $table = ${$class . "::DBObjTable"}; | ||
| 177 | my $throw = ${$class . "::DBObjThrow"}; | ||
| 178 | my $queryStr = ""; | ||
| 179 | my $fields = ""; | ||
| 180 | my $values = ""; | ||
| 181 | my @values = (); | ||
| 182 | |||
| 183 | foreach my $field (@{$class . "::DBObjFields"}) { | ||
| 184 | if (defined($createPairs->{$field})) { | ||
| 185 | $fields .= "$field,"; | ||
| 186 | $values .= "?,"; | ||
| 187 | if (defined $objTypes->{$field}) { | ||
| 188 | my $dtclass = $objTypes->{$field}; | ||
| 189 | my $v = &{$dtclass . "::sqlValue"}($class, $createPairs->{$field}); | ||
| 190 | push (@values, $v); | ||
| 191 | } | ||
| 192 | else { | ||
| 193 | push (@values, $createPairs->{$field}); | ||
| 194 | } | ||
| 195 | } | ||
| 196 | } | ||
| 197 | |||
| 198 | ## FTSO Perl | ||
| 199 | { | ||
| 200 | local $/ = ',' and chomp($fields) and chomp($values); | ||
| 201 | } | ||
| 202 | |||
| 203 | $queryStr = join(' ', | ||
| 204 | 'INSERT','INTO',$table,'(',$fields,')','VALUES','(',$values,')'); | ||
| 205 | |||
| 206 | if(query($queryStr, \@values)) { | ||
| 207 | |||
| 208 | ## Life is easy when you have the Insert ID! | ||
| 209 | ## | ||
| 210 | if (${$class . "::DBObjIID"} == 1) { | ||
| 211 | ## We either return the instert id, or we find and return an object. | ||
| 212 | my $iid = getLastInsertID(); | ||
| 213 | return($class->find($iid)) if ($returnObject); | ||
| 214 | return($iid); | ||
| 215 | } | ||
| 216 | |||
| 217 | ## Its not as easy, but we have the PKey(s) so hey! | ||
| 218 | ## | ||
| 219 | if($returnObject == 1) { | ||
| 220 | my @find_args = (); | ||
| 221 | foreach my $key (@{$class . "::DBObjPKeys"}) { | ||
| 222 | push(@find_args, $createPairs->{$key}); | ||
| 223 | } | ||
| 224 | return($class->find(@find_args)); | ||
| 225 | } | ||
| 226 | |||
| 227 | ## If we don't want an object, and we don't have a IID. | ||
| 228 | return(1); | ||
| 229 | } | ||
| 230 | |||
| 231 | ## | ||
| 232 | ## If the query failed | ||
| 233 | throw Error::Better::OperationFailed ("SQL: Insert failed.") | ||
| 234 | if ($throw == 1); | ||
| 235 | return(0); | ||
| 236 | } | ||
| 237 | |||
| 238 | |||
| 239 | |||
| 240 | sub delete { | ||
| 241 | my ($this) = @_; | ||
| 242 | return(undef) unless (ref $this); | ||
| 243 | |||
| 244 | my $queryStr = "DELETE FROM " . $this->{'_Table'} . " WHERE "; | ||
| 245 | foreach my $pkey (@{ref($this) . "::DBObjPKeys"}) { | ||
| 246 | my $pkeyValue = $this->{"_Fields"}{"_$pkey"}{'_Value'}; | ||
| 247 | $queryStr .= " $pkey='$pkeyValue' AND"; | ||
| 248 | } | ||
| 249 | $queryStr =~ s/AND$//; | ||
| 250 | return(query($queryStr)); | ||
| 251 | } | ||
| 252 | |||
| 253 | |||
| 254 | |||
| 255 | sub update { | ||
| 256 | my ($this) = @_; | ||
| 257 | my $queryStr = ""; | ||
| 258 | my $fieldStr = ""; | ||
| 259 | my $whereStr = ""; | ||
| 260 | |||
| 261 | my @updated_fields = (); | ||
| 262 | my $changed_ctr = 0; | ||
| 263 | ## Setup the fieldStr | ||
| 264 | foreach my $field (keys %{$this->{"_Fields"}}) { | ||
| 265 | $field =~ s/\_//; | ||
| 266 | if ($this->{"_Fields"}{"_$field"}{"_ChangedP"} == 1) { | ||
| 267 | $changed_ctr++; | ||
| 268 | my $fieldValue = $this->{"_Fields"}{"_$field"}{"_Value"}; | ||
| 269 | $fieldStr .= "$field='" . $fieldValue . "',"; | ||
| 270 | push(@updated_fields, $field); | ||
| 271 | } | ||
| 272 | } | ||
| 273 | $fieldStr =~ s/\,$//; | ||
| 274 | |||
| 275 | ## Return true if update() was called without anything to update! | ||
| 276 | return(1) if ($changed_ctr == 0); | ||
| 277 | |||
| 278 | ## Build our WHERE clause. | ||
| 279 | ## | ||
| 280 | $whereStr = join(' AND ', | ||
| 281 | map { | ||
| 282 | join('', $_, '=', "'", $this->{"_Fields"}{"_$_"}{"_Value"}, "'") | ||
| 283 | } | ||
| 284 | @{$this->{'_PKeys'}}); | ||
| 285 | |||
| 286 | ## Build the rest of the QUERY. | ||
| 287 | ## | ||
| 288 | $queryStr = join(' ', | ||
| 289 | 'UPDATE',$this->{'_Table'},'SET',$fieldStr,'WHERE','(',$whereStr,')'); | ||
| 290 | |||
| 291 | if (query($queryStr)) { | ||
| 292 | ## Now return the update-flage for the updated fields to '0'. | ||
| 293 | foreach my $field (@updated_fields) { | ||
| 294 | $this->{_Fields}{"_$field"}{_ChangedP} = 0; | ||
| 295 | } | ||
| 296 | return(1); | ||
| 297 | } | ||
| 298 | return(0); | ||
| 299 | } | ||
| 300 | |||
| 301 | sub commit { | ||
| 302 | print STDERR "Depricated use of dbobj->commit()\n"; | ||
| 303 | return($_[0]->dbobj_query("commit")); | ||
| 304 | } | ||
| 305 | |||
| 306 | sub rollback { | ||
| 307 | print STDERR "Depricated use of dbobj->rollback()\n"; | ||
| 308 | return($_[0]->dbobj_query("rollback")); | ||
| 309 | } | ||
| 310 | |||
| 311 | sub dbobj_query { | ||
| 312 | print STDERR "Depricated use of dbobj->dbobj_query()\n"; | ||
| 313 | my $class = shift; | ||
| 314 | return(DBIx::SimpleDBI::query(@_)); | ||
| 315 | } | ||
| 316 | |||
| 317 | sub get_table_name { | ||
| 318 | my ($class) = @_; | ||
| 319 | my $tbl_name = ${$class . "::DBObjTable"}; | ||
| 320 | return($tbl_name); | ||
| 321 | } | ||
| 322 | |||
| 323 | sub get_field_string { | ||
| 324 | my ($class) = @_; | ||
| 325 | my $fieldStr = join(",", @{$class . "::DBObjFields"}); | ||
| 326 | return($fieldStr); | ||
| 327 | } | ||
| 328 | |||
| 329 | sub res_to_objects { | ||
| 330 | my ($class, $results) = @_; | ||
| 331 | my @objects = (); | ||
| 332 | |||
| 333 | foreach my $row (@{$results}) { | ||
| 334 | my $count = 0; | ||
| 335 | my $dbObj = $class->new(); | ||
| 336 | foreach my $field (@{$class . "::DBObjFields"}) { | ||
| 337 | &{$class . "::set$field"}($dbObj, $row->[$count], 1); | ||
| 338 | $count++; | ||
| 339 | } | ||
| 340 | push (@objects, $dbObj); | ||
| 341 | } | ||
| 342 | return(\@objects); | ||
| 343 | } | ||
| 344 | |||
| 345 | |||
| 346 | ## | ||
| 347 | 1; | ||
| 348 | |||
| 349 | __END__ |