| 1 | # Table.pm | ||
| 2 | # | ||
| 3 | # $Id: Table.pm,v 1.2 2002/05/24 10:33:05 rsandberg Exp $ | ||
| 4 | # | ||
| 5 | |||
| 6 | package DBIx::IO::Table; | ||
| 7 | |||
| 8 | use strict; | ||
| 9 | use DBIx::IO::GenLib (); | ||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | DBIx::IO::Table - Objectified abstract of a database table | ||
| 14 | |||
| 15 | |||
| 16 | =head1 SYNOPSIS | ||
| 17 | |||
| 18 | use DBIx::IO::Table; | ||
| 19 | |||
| 20 | |||
| 21 | |||
| 22 | $table = DBIx::IO::Table->new($dbh,[$attrs_or_key_value],[$key_name],[$table_name]); | ||
| 23 | |||
| 24 | $record = $table->fetch($id_val_or_id_hash); | ||
| 25 | |||
| 26 | $record = $table->new_record(); | ||
| 27 | |||
| 28 | $record = $table->exist_record(); | ||
| 29 | |||
| 30 | $value = $table-><COLUMN_NAME>($value); # set <COLUMN_NAME> | ||
| 31 | |||
| 32 | $value = $table-><COLUMN_NAME>(); # get value of <COLUMN_NAME> | ||
| 33 | |||
| 34 | $success = $table->delete(); | ||
| 35 | |||
| 36 | $rv = $table->delete_all($id_hash); | ||
| 37 | |||
| 38 | $success = $table->update([$update],[$persist]); | ||
| 39 | |||
| 40 | $pk = $table->insert([$insert],[$persist]); | ||
| 41 | |||
| 42 | $success = $table->add_values($add_hash); | ||
| 43 | |||
| 44 | $value = $table->single_column_lookup($column_name,$table_name,$key,$key_name); | ||
| 45 | $value = DBIx::IO::Table->single_column_lookup($column_name,$table_name,$key,$key_name,$dbh); | ||
| 46 | |||
| 47 | $datatype = $table->column_type($field_name); | ||
| 48 | |||
| 49 | $integer = $table->field_length($field_name); | ||
| 50 | |||
| 51 | $bool = $table->required($field_name); | ||
| 52 | |||
| 53 | $default_value = $table->default_value($field_name); | ||
| 54 | |||
| 55 | $rv = $table->verify_datatype($value,[$field_name],[$type]); | ||
| 56 | |||
| 57 | =head2 Attribute Accessors/Modifiers | ||
| 58 | |||
| 59 | =head3 Get the values of these READ-ONLY attributes. | ||
| 60 | |||
| 61 | $table_name = $table->name(); | ||
| 62 | $dbh = $table->dbh(); | ||
| 63 | $io = $table->io(); | ||
| 64 | $arrayref = $table->pk(); | ||
| 65 | $key_name = $table->key_name(); | ||
| 66 | $id_value = $table->id(); | ||
| 67 | $table_columns = $table->column_types(); | ||
| 68 | |||
| 69 | =head3 Get or set the values of these attributes. | ||
| 70 | |||
| 71 | $persistence = $table->persist(); | ||
| 72 | $persistence = $table->persist($bool); | ||
| 73 | |||
| 74 | |||
| 75 | =head1 DESCRIPTION | ||
| 76 | |||
| 77 | This class is useful for handling db I/O for a particular table. You create | ||
| 78 | the object, associate a row with the object, then update, insert, view or delete | ||
| 79 | the corresponding record in the table. Qualifying the values is done automatically | ||
| 80 | according to column datatypes. All dates should be in the normalized format mentioned | ||
| 81 | in DBIx::IO::GenLib, which also contains helpful methods of getting dates in the | ||
| 82 | correct format. | ||
| 83 | |||
| 84 | The object potentially stores 2 records: exist_rec and new_rec. exist_rec is populated if an | ||
| 85 | existing record is fetched from the table. This also populates new_rec which can be | ||
| 86 | subsequently overwritten with the column setter methods. Before an update can occur, a fetch | ||
| 87 | must be performed so that exist_rec is populated; exist_rec never changes. | ||
| 88 | See also DBIx::IO. | ||
| 89 | |||
| 90 | |||
| 91 | =head2 Persistence | ||
| 92 | |||
| 93 | After insert() update() or delete() has been called, all data in exist_rec and new_rec will be | ||
| 94 | wiped out, use the persist() method if you want update() and insert() to fetch the new record | ||
| 95 | as it exists in the db for further use. (triggers and such can modify data after committing them). | ||
| 96 | Persistence can be set by calling persist(1). | ||
| 97 | |||
| 98 | |||
| 99 | =head2 Multi-part unique keys | ||
| 100 | |||
| 101 | See fetch(). | ||
| 102 | |||
| 103 | |||
| 104 | =head2 Driver Specific | ||
| 105 | |||
| 106 | Each $table object has an $io object attribute constructed from the corresponding | ||
| 107 | driver specific DBIx::IO::XXXIO class. Consequently, DBIx::IO::Table can only support Oracle and MySQL as yet. | ||
| 108 | Use this object to manipulate driver-specific features. | ||
| 109 | |||
| 110 | |||
| 111 | =head2 Transactions | ||
| 112 | |||
| 113 | You are responsible for transaction control. | ||
| 114 | |||
| 115 | |||
| 116 | =head2 Implementation And Subclassing | ||
| 117 | |||
| 118 | Derived classes can have the | ||
| 119 | same name as the table they represent, this way DBIx::IO::Table can assume the | ||
| 120 | name of the table, primary key and other attributes (see the contructor for details). | ||
| 121 | |||
| 122 | Column accessor methods are automatically created using AUTOLOAD so that: | ||
| 123 | $table-><COLUMN_NAME>($value) will set <COLUMN_NAME> to $value, | ||
| 124 | $table-><COLUMN_NAME>() will return the value of <COLUMN_NAME> according to the | ||
| 125 | following rules: | ||
| 126 | The associated value from new_rec will be returned. | ||
| 127 | If a record has not been fetched and the value not previously set, undef | ||
| 128 | will be returned. | ||
| 129 | |||
| 130 | AUTOLOAD also creates a parallel set of methods for each column just before an update is done: | ||
| 131 | __update__<COLUMN_NAME>() These methods are called explicitly on each column just before an update | ||
| 132 | is finished, and allows for an audit trail or trigger. The individual column accessors ($table-><COLUMN_NAME>()) are also called explicitly for each column on the | ||
| 133 | bulk setters (e.g. using add_values() or by providing a hash upon construction or in the insert and update methods). | ||
| 134 | NOTE: For the individual setters a final action of update is assumed if a fetch was done previously, | ||
| 135 | (exist_rec is defined) otherwise a final action of insert is assumed. | ||
| 136 | |||
| 137 | =head2 Database trigger simulation | ||
| 138 | |||
| 139 | If your derived class wants to capture control before a particular value is set, override the | ||
| 140 | appropriate <COLUMN_NAME>() method in your subclass. If you want to | ||
| 141 | capture control before a particular column => value is committed to the db (for updates), | ||
| 142 | override the corresponding __update__<COLUMN_NAME>() method. | ||
| 143 | By overridding these accessor methods you can effectively create | ||
| 144 | an interactive db trigger. | ||
| 145 | To clarify, <COLUMN_NAME>() methods are called to modify the object's record with the | ||
| 146 | intention of calling insert() or update(). Once update() is called, __update__<COLUMN_NAME>($new_value) | ||
| 147 | is invoked for each column and the associated operation is performed | ||
| 148 | and committed in the database. | ||
| 149 | CAUTION! not do initiate a commit directly or indirectly when overriding these methods. | ||
| 150 | |||
| 151 | Example: | ||
| 152 | For a table object that is updating STATUS, the class may | ||
| 153 | want to define a STATUS() function to force the user to enter a | ||
| 154 | comment for certain status changes. Also __update__STATUS() | ||
| 155 | can be defined to log the status change to a separate table. | ||
| 156 | |||
| 157 | |||
| 158 | =head1 Method Details | ||
| 159 | |||
| 160 | |||
| 161 | =over 4 | ||
| 162 | |||
| 163 | =item C<new> (constructor) | ||
| 164 | |||
| 165 | $table = DBIx::IO::Table->new($dbh,[$attrs_or_key_value],[$key_name],[$table_name]); | ||
| 166 | |||
| 167 | $dbh must be a valid db handle | ||
| 168 | created with DBI (or DBIAccess). | ||
| 169 | |||
| 170 | $key_name and $table_name can be given explicitly | ||
| 171 | or implied by the name of the derived class initiating this constructor. | ||
| 172 | If these values are implied, $table_name becomes the name of the derived class | ||
| 173 | (less any XXX:: package qualifiers - I recommend the class name should be lower case) and $key_name = <table_name>_ID || <table_name> depending on which | ||
| 174 | corresponding column name exists in the table. | ||
| 175 | In either case, $key_name should be a unique key on $table_name. | ||
| 176 | See fetch() if you need to access a table with a multi-part unique key. | ||
| 177 | |||
| 178 | $attrs_or_key_value can be given in 1 of 2 datatypes: | ||
| 179 | |||
| 180 | 1) If a scalar is given, it will be treated as a value of the | ||
| 181 | primary key column. The corresponding | ||
| 182 | record will be fetched and added to new_rec and exist_rec (described below). | ||
| 183 | NOTE: All objects need to retrieve a record either by this method or fetch() | ||
| 184 | (described elsewhere) before an update can occur. | ||
| 185 | |||
| 186 | 2) If a reference is given it will be treated as a hashref of COLUMN_NAME => value | ||
| 187 | pairs that will be added to the object's new_rec (described below). | ||
| 188 | |||
| 189 | If all goes well, a new $table object is returned. If an error occurs, undef will be returned. | ||
| 190 | If the table doesn't exist, or $attrs_or_key_value is given as a scalar and | ||
| 191 | the corresponding row doesn't exist, 0 will be returned. | ||
| 192 | |||
| 193 | =item C<Column setter methods> | ||
| 194 | |||
| 195 | $table-><COLUMN_NAME>('value') where <COLUMN_NAME> is a valid column in the table | ||
| 196 | |||
| 197 | To set a value to NULL (for updates), call the setter with '' as a value: | ||
| 198 | $table-><COLUMN_NAME>(''). This makes updating a value to NULL more explicit, | ||
| 199 | which is a good thing. If the empty string, '' is a desirable update value, | ||
| 200 | use $DBIx::IO::GenLib::EMPTY_STRING. | ||
| 201 | |||
| 202 | See also new(), update(), add_values() and insert() which take hashref's to set column values in bulk. | ||
| 203 | |||
| 204 | =item C<Column getter methods> | ||
| 205 | |||
| 206 | $table-><COLUMN_NAME>() where <COLUMN_NAME> is a valid column in the table | ||
| 207 | |||
| 208 | Return undef if error. | ||
| 209 | Return '' if NULL. | ||
| 210 | Return $DBIx::IO::GenLib::EMPTY_STRING if the value is the empty string, ''. | ||
| 211 | |||
| 212 | See also: | ||
| 213 | $table->exist_record() | ||
| 214 | $table->new_record() | ||
| 215 | |||
| 216 | Note that these COLUMN_NAME methods are always in UPPER CASE. | ||
| 217 | |||
| 218 | =cut | ||
| 219 | ##at note that for Oracle this will only work if $dbh is connected to the schema owner because you use select from USER_TABLES etc | ||
| 220 | sub new | ||
| 221 | { | ||
| 222 | my ($caller,$dbh,$attrs_or_pk,$key_name,$table) = @_; | ||
| 223 | my $class = ref($caller) || $caller; | ||
| 224 | my $self = bless({},$class); | ||
| 225 | ref($dbh) || (warn("\$dbh doesn't appear to be valid"), return undef); | ||
| 226 | eval{ $dbh->{FetchHashKeyName} = 'NAME_uc'; }; # If they have an older DBI just hope for the best? | ||
| 227 | $table ||= $class; | ||
| 228 | $table =~ s/.*:://; # strip fully-qualified portion | ||
| 229 | |||
| 230 | my $ioclass = $self->_pull_driver($dbh); | ||
| 231 | return undef unless defined($ioclass); | ||
| 232 | |||
| 233 | my $rv; | ||
| 234 | unless ($rv = $ioclass->new($dbh,$table,$key_name)) | ||
| 235 | { | ||
| 236 | return $rv; | ||
| 237 | } | ||
| 238 | $self->{io} = $rv; | ||
| 239 | |||
| 240 | if (defined($attrs_or_pk) && ref($attrs_or_pk)) | ||
| 241 | { | ||
| 242 | $self->add_values($attrs_or_pk) || (warn("Could not add values from \$attrs_or_key_value"), return undef); | ||
| 243 | } | ||
| 244 | elsif (defined($attrs_or_pk)) | ||
| 245 | { | ||
| 246 | unless ($rv = $self->fetch($attrs_or_pk)) | ||
| 247 | { | ||
| 248 | defined($rv) || (warn("Error fetching $attrs_or_pk"), return undef); | ||
| 249 | return $rv; | ||
| 250 | } | ||
| 251 | } | ||
| 252 | return $self; | ||
| 253 | } | ||
| 254 | |||
| 255 | sub _pull_driver | ||
| 256 | { | ||
| 257 | my ($caller,$dbh) = @_; | ||
| 258 | |||
| 259 | return $caller->{ioclass} if ref($caller) && defined($caller->{ioclass}); | ||
| 260 | |||
| 261 | # IO classes must be named after the DBI driver name!! | ||
| 262 | my $ioclass = "DBIx::IO::$dbh->{Driver}{Name}IO"; | ||
| 263 | my $libclass = "DBIx::IO::$dbh->{Driver}{Name}Lib"; | ||
| 264 | eval qq(require $ioclass) || (warn("Database driver not supported"),return undef); | ||
| 265 | if (eval qq(require $libclass)) | ||
| 266 | { | ||
| 267 | #$caller->{norm_datetime_format} = eval("\$${libclass}::NORMAL_DATETIME_FORMAT") if ref($caller); | ||
| 268 | } | ||
| 269 | |||
| 270 | $caller->{ioclass} = $ioclass if ref($caller); | ||
| 271 | return $ioclass; | ||
| 272 | } | ||
| 273 | |||
| 274 | ##at could have a function normalize_all_dates() to facilitate use with unknown date formats. | ||
| 275 | |||
| 276 | =pod | ||
| 277 | |||
| 278 | =item C<single_column_lookup> | ||
| 279 | |||
| 280 | $value = $table->single_column_lookup($column_name,$table_name,$key,$key_name); | ||
| 281 | $value = DBIx::IO::Table->single_column_lookup($column_name,$table_name,$key,$key_name,$dbh); | ||
| 282 | |||
| 283 | Return "SELECT $column_name FROM $table_name WHERE $key_name = $key" | ||
| 284 | If called with a $table object then $table_name, $key_name and $dbh default to | ||
| 285 | those properties of $table. | ||
| 286 | |||
| 287 | Return undef if error. | ||
| 288 | Return '' if NULL. | ||
| 289 | Return $DBIx::IO::GenLib::EMPTY_STRING if the value is the empty string, ''. | ||
| 290 | |||
| 291 | =cut | ||
| 292 | sub single_column_lookup | ||
| 293 | { | ||
| 294 | my ($caller,$column_name,$table_name,$key,$key_name,$dbh) = @_; | ||
| 295 | if (ref($caller)) | ||
| 296 | { | ||
| 297 | $table_name ||= $caller->name(); | ||
| 298 | $key_name ||= $caller->key_name(); | ||
| 299 | $dbh ||= $caller->dbh(); | ||
| 300 | } | ||
| 301 | my $table = $caller->new($dbh,$key,$key_name,$table_name) || | ||
| 302 | (warn("Can't allocate table object for data: ($column_name,$table_name,$key,$key_name)"), return undef); | ||
| 303 | my $rec = $table->exist_record(); | ||
| 304 | my $ret = $rec->{uc($column_name)}; | ||
| 305 | return (defined($ret) ? ($ret eq '' ? $DBIx::IO::GenLib::EMPTY_STRING : $ret) : ''); # return a defined value in any case | ||
| 306 | } | ||
| 307 | |||
| 308 | =pod | ||
| 309 | |||
| 310 | =item C<fetch> | ||
| 311 | |||
| 312 | $record = $table->fetch($id_val_or_id_hash); | ||
| 313 | |||
| 314 | Return a row in hashref form (COLUMN_NAME => value pairs). | ||
| 315 | All date values are returned in the canonical format (see DBIx::IO::GenLib). | ||
| 316 | |||
| 317 | The row to be fetched is identified depending on the datatype of $id_val_or_id_hash. | ||
| 318 | |||
| 319 | If $id_val_or_id_hash is a scalar, the value is used in conjunction with the table's primary key. | ||
| 320 | If $id_val_or_id_hash is a hash ref it is interpreted as column => value | ||
| 321 | pairs to be AND'ed together in a WHERE clause. | ||
| 322 | |||
| 323 | This method assumes that key(s) given form a unique key, so only 1 row is returned. | ||
| 324 | The object's existing record is set for comparison when/if update() (described elsewhere) is called. | ||
| 325 | This routine overwrites all column attributes previously set by setting new_rec; | ||
| 326 | equivalent to creating a new object with a key value. | ||
| 327 | |||
| 328 | Oracle users: | ||
| 329 | LOB columns won't be retreived because they aren't supported in DBD::Oracle (as of v1.19). LONG columns seem to work | ||
| 330 | fine though so if you can get away with using a LONG over a LOB, do that. | ||
| 331 | $DBIx::IO::GenLib::LONG_READ_LENGTH gives the limit size of a LONG that will be returned. | ||
| 332 | If the table is a concrete table (rather than a view, for instance) | ||
| 333 | ROWID will be included as a column with ROWID datatype. You may find this | ||
| 334 | useful for updates and deletes (See also DBIx::IO::GenLib for a ROWID column name constant). | ||
| 335 | |||
| 336 | |||
| 337 | Return undef if error. | ||
| 338 | Return 0 if no row was found. | ||
| 339 | |||
| 340 | =cut | ||
| 341 | sub fetch | ||
| 342 | { | ||
| 343 | my ($self,$key_val) = @_; | ||
| 344 | my $row; | ||
| 345 | unless ($row = $self->{io}->fetch($key_val)) | ||
| 346 | { | ||
| 347 | return $row; | ||
| 348 | } | ||
| 349 | if (ref($key_val)) | ||
| 350 | { | ||
| 351 | $self->{mult_id} = $key_val; | ||
| 352 | $self->{fetch_key} = $key_val; | ||
| 353 | } | ||
| 354 | else | ||
| 355 | { | ||
| 356 | $self->{fetch_key} = { $self->key_name() => $key_val }; | ||
| 357 | } | ||
| 358 | $self->{exist_rec} = { %$row }; # Make a copy of the hash | ||
| 359 | $self->{new_rec} = { %$row }; # '' | ||
| 360 | return $row; | ||
| 361 | } | ||
| 362 | |||
| 363 | =item C<Attribute Accessors/Modifiers> | ||
| 364 | |||
| 365 | =over 2 | ||
| 366 | |||
| 367 | Get the values of these READ-ONLY attributes. | ||
| 368 | CAUTION: Don't modify these values - you've been warned. | ||
| 369 | |||
| 370 | $table_name = $table->name(); | ||
| 371 | $dbh = $table->dbh(); | ||
| 372 | |||
| 373 | May return undef if multi-part key (see also C<pk>). | ||
| 374 | $key_name = $table->key_name(); | ||
| 375 | |||
| 376 | Return the underlying RDBMS-specific DBIx::IO driver. | ||
| 377 | $io = $table->io(); | ||
| 378 | |||
| 379 | Return the value(s) of the primary key. Return a scalar if single-valued | ||
| 380 | or a hashref of COLUMN_NAME => value pairs if a multi-column key. | ||
| 381 | $id_value = $table->id(); | ||
| 382 | |||
| 383 | Return an arrayref of the key column names from the $io object | ||
| 384 | $key_arrayref = $table->pk(); | ||
| 385 | |||
| 386 | Return a hash of COLUMN_NAME => DATA_TYPE pairs. | ||
| 387 | $table_columns = $table->column_types(); | ||
| 388 | |||
| 389 | |||
| 390 | Get or set the values of these attributes. | ||
| 391 | |||
| 392 | $persistence = $table->persist(); | ||
| 393 | $persistence = $table->persist($bool); | ||
| 394 | |||
| 395 | =back | ||
| 396 | |||
| 397 | =cut | ||
| 398 | sub column_types | ||
| 399 | { | ||
| 400 | my $self = shift; | ||
| 401 | return $self->{io}->column_types(); | ||
| 402 | } | ||
| 403 | |||
| 404 | sub name | ||
| 405 | { | ||
| 406 | my $self = shift; | ||
| 407 | return $self->{io}->table_name(); | ||
| 408 | } | ||
| 409 | |||
| 410 | sub dbh | ||
| 411 | { | ||
| 412 | my $self = shift; | ||
| 413 | return $self->{io}->dbh(); | ||
| 414 | } | ||
| 415 | |||
| 416 | sub io | ||
| 417 | { | ||
| 418 | my $self = shift; | ||
| 419 | return $self->{io}; | ||
| 420 | } | ||
| 421 | |||
| 422 | sub pk | ||
| 423 | { | ||
| 424 | my ($self) = @_; | ||
| 425 | return $self->{io}{pk}; | ||
| 426 | } | ||
| 427 | |||
| 428 | sub key_name | ||
| 429 | { | ||
| 430 | my $self = shift; | ||
| 431 | return $self->{io}->key_name(); | ||
| 432 | } | ||
| 433 | |||
| 434 | sub persist | ||
| 435 | { | ||
| 436 | my ($self,$persist) = @_; | ||
| 437 | if (defined($persist)) | ||
| 438 | { | ||
| 439 | return $self->{persist} = ($persist ? 1 : 0); | ||
| 440 | } | ||
| 441 | return $self->{persist}; | ||
| 442 | } | ||
| 443 | |||
| 444 | sub id | ||
| 445 | { | ||
| 446 | my $self = shift; | ||
| 447 | my $rec = $self->exist_record(); | ||
| 448 | return (defined($rec->{$self->key_name()}) ? $rec->{$self->key_name()} : $self->{mult_id}); | ||
| 449 | } | ||
| 450 | |||
| 451 | =pod | ||
| 452 | |||
| 453 | =item C<new_record> | ||
| 454 | |||
| 455 | $record = $table->new_record() | ||
| 456 | |||
| 457 | Returns the record built with column setter methods pending update or insert. | ||
| 458 | Values default to those of the existing record if a fetch() was done (see exist_record()). | ||
| 459 | Return false if no record was retrieved. | ||
| 460 | |||
| 461 | =cut | ||
| 462 | sub new_record | ||
| 463 | { | ||
| 464 | my $self = shift; | ||
| 465 | ref($self) || ($self->{io}->_alert("Method must be called by an object"), return undef); | ||
| 466 | return (ref($self->{new_rec}) ? { %{$self->{new_rec}} } : undef); | ||
| 467 | } | ||
| 468 | |||
| 469 | =pod | ||
| 470 | |||
| 471 | =item C<exist_record> | ||
| 472 | |||
| 473 | $record = $table->exist_record() | ||
| 474 | |||
| 475 | Returns the COLUMN_NAME => value pairs of the existing record which was originally retrieved | ||
| 476 | by the constructor or by calling fetch(). | ||
| 477 | Return false if no record was retrieved. | ||
| 478 | |||
| 479 | =cut | ||
| 480 | sub exist_record | ||
| 481 | { | ||
| 482 | my $self = shift; | ||
| 483 | ref($self) || ($self->{io}->_alert("Method must be called by an object"), return undef); | ||
| 484 | return (ref($self->{exist_rec}) ? { %{$self->{exist_rec}} } : undef); | ||
| 485 | } | ||
| 486 | |||
| 487 | |||
| 488 | # this prevents column names from being named DESTROY, also __update__XXX | ||
| 489 | sub DESTROY | ||
| 490 | { | ||
| 491 | } | ||
| 492 | |||
| 493 | # This generates all getters and setters for each column. | ||
| 494 | sub AUTOLOAD | ||
| 495 | { | ||
| 496 | my $self = shift; | ||
| 497 | my ($new_val) = @_; | ||
| 498 | ref($self) || ($self->{io}->_alert("$self is not an object"), return undef); | ||
| 499 | |||
| 500 | my $name = $DBIx::IO::Table::AUTOLOAD; | ||
| 501 | $name =~ s/.*://; # strip fully-qualified portion | ||
| 502 | |||
| 503 | my $column_types = $self->column_types(); | ||
| 504 | if ($name =~ /__update__(.*)/) | ||
| 505 | { | ||
| 506 | exists($column_types->{$1}) || ($self->{io}->_alert("$1 is not a column of " . $self->name()), return undef); | ||
| 507 | return shift; | ||
| 508 | } | ||
| 509 | |||
| 510 | exists($column_types->{$name}) || ($self->{io}->_alert("$name is not a column of " . $self->name()), return undef); | ||
| 511 | |||
| 512 | if (defined($new_val)) | ||
| 513 | { | ||
| 514 | return $self->{new_rec}{$name} = $new_val; | ||
| 515 | } | ||
| 516 | else | ||
| 517 | { | ||
| 518 | my $ret = $self->{new_rec}{$name}; | ||
| 519 | return (defined($ret) ? ($ret eq '' ? $DBIx::IO::GenLib::EMPTY_STRING : $ret) : ''); | ||
| 520 | } | ||
| 521 | } | ||
| 522 | |||
| 523 | |||
| 524 | =pod | ||
| 525 | |||
| 526 | =item C<delete> | ||
| 527 | |||
| 528 | $success = $table->delete(); | ||
| 529 | |||
| 530 | Delete $table's current record. | ||
| 531 | Its up to you to make sure that the current record was fetched via a | ||
| 532 | unique key, otherwise unexpected results can occur. See also delete_all(). | ||
| 533 | |||
| 534 | Returns the number of rows deleted or false if error (0 is represented as '0E0' which is true). | ||
| 535 | Return -1 if there is no current record (no fetch() was done). | ||
| 536 | |||
| 537 | =cut | ||
| 538 | sub delete | ||
| 539 | { | ||
| 540 | my ($self) = @_; | ||
| 541 | my $rv = $self->{io}->delete_all($self->{fetch_key}); | ||
| 542 | unless ($rv) | ||
| 543 | { | ||
| 544 | return $rv; | ||
| 545 | } | ||
| 546 | delete $self->{exist_rec}; | ||
| 547 | delete $self->{new_rec}; | ||
| 548 | return $rv; | ||
| 549 | } | ||
| 550 | |||
| 551 | =pod | ||
| 552 | |||
| 553 | =item C<delete_all> | ||
| 554 | |||
| 555 | $rv = $table->delete_all($id_hash); | ||
| 556 | |||
| 557 | Delete all rows that satisfy $id_hash, where $id_hash | ||
| 558 | is a hash of COLUMN_NAME => value pairs that will be AND'ed together for the | ||
| 559 | WHERE clause of the DELETE statement. | ||
| 560 | |||
| 561 | Returns the number of rows deleted or false if error (0 is represented as '0E0' which is true). | ||
| 562 | Return -1 if $id_hash is empty or not a reference. | ||
| 563 | |||
| 564 | =cut | ||
| 565 | sub delete_all | ||
| 566 | { | ||
| 567 | my ($self,$id_hash) = @_; | ||
| 568 | my $rv = $self->{io}->delete_all($id_hash); | ||
| 569 | unless ($rv) | ||
| 570 | { | ||
| 571 | return $rv; | ||
| 572 | } | ||
| 573 | return $rv; | ||
| 574 | } | ||
| 575 | |||
| 576 | =pod | ||
| 577 | |||
| 578 | =item C<update> | ||
| 579 | |||
| 580 | $success = $table->update([$update],[$persist]) | ||
| 581 | |||
| 582 | Update the object's exist_rec to new_rec. If $update is defined, its | ||
| 583 | COLUMN_NAME => value pairs will be added to new_rec via add_values() (explained elsewhere). | ||
| 584 | $persist: if true, the updated record will be retrieved for further work, | ||
| 585 | otherwise this object's exist_rec and new_rec values will be undef'ed. | ||
| 586 | |||
| 587 | Only a delta of column values that differ between exist_rec and new_rec are updated. | ||
| 588 | __update__<COLUMN_NAME>($new_val) is called for each column in the delta. | ||
| 589 | Its up to you to make sure the current record was fetched via a | ||
| 590 | unique key, otherwise unexpected results can occur. | ||
| 591 | |||
| 592 | Return the number of rows updated or false if error (0 is represented as '0E0' which is true). | ||
| 593 | Return -1 if there was no data to update. | ||
| 594 | Return -2 if persistence is desired and the updated row could not be fetched. | ||
| 595 | |||
| 596 | =cut | ||
| 597 | sub update | ||
| 598 | { | ||
| 599 | my ($self,$update,$persist) = @_; | ||
| 600 | $self->add_values($update) || ($self->{io}->_alert("Can't add values from \$update"), return undef) if ref($update); | ||
| 601 | my $delta; | ||
| 602 | my $rt; | ||
| 603 | unless ($delta = $self->_prepare_update()) | ||
| 604 | { | ||
| 605 | return undef; | ||
| 606 | } | ||
| 607 | unless ($rt = $self->{io}->update_hash($delta,$self->{fetch_key})) | ||
| 608 | { | ||
| 609 | return undef; | ||
| 610 | } | ||
| 611 | my $id = $self->id(); | ||
| 612 | delete $self->{exist_rec}; | ||
| 613 | delete $self->{new_rec}; | ||
| 614 | $self->persist() || $persist || return $rt; | ||
| 615 | $self->fetch($id) || return -2; | ||
| 616 | return $rt; | ||
| 617 | } | ||
| 618 | |||
| 619 | sub _prepare_update | ||
| 620 | { | ||
| 621 | my $self = shift; | ||
| 622 | ref($self) || ($self->{io}->_alert("Method must be called by an object"), return undef); | ||
| 623 | my $new_rec = $self->new_record() || {}; | ||
| 624 | my $exist_rec = $self->exist_record() || {}; | ||
| 625 | my $column_attrs = $self->column_types(); | ||
| 626 | my ($field,$new_val,%ret); | ||
| 627 | while (($field,$new_val) = each %$new_rec) | ||
| 628 | { | ||
| 629 | # No attempt is made to find numerical equivalents because new_rec is set to exist_rec from fetch() | ||
| 630 | if ($new_val ne $exist_rec->{$field}) | ||
| 631 | { | ||
| 632 | defined(eval("\$self->__update__${field}(\$new_val)")) || | ||
| 633 | ($self->{io}->_alert("pre-update routine failed for $field: $new_val"), return undef); | ||
| 634 | $ret{$field} = $new_val; | ||
| 635 | } | ||
| 636 | } | ||
| 637 | return (\%ret); | ||
| 638 | } | ||
| 639 | |||
| 640 | =pod | ||
| 641 | |||
| 642 | =item C<insert> | ||
| 643 | |||
| 644 | $pk = $table->insert([$insert],[$persist]); | ||
| 645 | |||
| 646 | Insert the current record, if $insert is defined, its | ||
| 647 | COLUMN_NAME => value pairs will be added via add_values() (explained elsewhere) before the insert. | ||
| 648 | $persist: if true, the inserted record will be retrieved for further work. | ||
| 649 | This only works if a key column was discovered in the constructor. | ||
| 650 | Otherwise this object's exist_record and new_record values will be undef'ed. | ||
| 651 | |||
| 652 | Return the generated pk ID value or -1.2 if there wasn't a value generated (e.g. if the table has a multi-column pk) | ||
| 653 | If there was no data to insert, -1.1 is returned. | ||
| 654 | Return -1.3 if persistence is desired and the new row could not be fetched. | ||
| 655 | Return -1.4 if a unique key violation occurred. | ||
| 656 | Return undef if error. | ||
| 657 | |||
| 658 | =cut | ||
| 659 | sub insert | ||
| 660 | { | ||
| 661 | my ($self,$insert,$persist) = @_; | ||
| 662 | $self->add_values($insert) || ($self->{io}->_alert("Can't add values from \$insert"), return undef) if ref($insert); | ||
| 663 | $insert = $self->new_record() || {}; | ||
| 664 | my $pk = $self->{io}->insert_hash($insert); | ||
| 665 | unless (defined($pk)) | ||
| 666 | { | ||
| 667 | return undef; | ||
| 668 | } | ||
| 669 | delete $self->{exist_rec}; | ||
| 670 | delete $self->{new_rec}; | ||
| 671 | $self->persist() || $persist || return $pk; | ||
| 672 | return $pk if $pk == -1.1; | ||
| 673 | my $id; | ||
| 674 | if ($pk == -1.2) | ||
| 675 | { | ||
| 676 | my $ps = $self->pk(); | ||
| 677 | foreach my $p (@$ps) | ||
| 678 | { | ||
| 679 | $id->{$p} = $insert->{$p}; | ||
| 680 | } | ||
| 681 | } | ||
| 682 | else | ||
| 683 | { | ||
| 684 | $id = $pk; | ||
| 685 | } | ||
| 686 | $self->fetch($id) || ($self->{io}->_alert("Can't fetch inserted record for persistence"), return -1.3); | ||
| 687 | return $pk; | ||
| 688 | } | ||
| 689 | |||
| 690 | =pod | ||
| 691 | |||
| 692 | =item C<column_type> | ||
| 693 | |||
| 694 | $datatype = $table->column_type($field_name); | ||
| 695 | |||
| 696 | Return the datatype of $field_name | ||
| 697 | See also DBIx::IO. | ||
| 698 | |||
| 699 | =cut | ||
| 700 | sub column_type | ||
| 701 | { | ||
| 702 | my $self = shift; | ||
| 703 | return $self->{io}->column_type(@_); | ||
| 704 | } | ||
| 705 | |||
| 706 | =item C<verify_datatype> | ||
| 707 | |||
| 708 | $rv = $table->verify_datatype($value,[$field_name],[$type]); | ||
| 709 | |||
| 710 | NOTE: Use DBIx::IO::GenLib::normalize_date to verify dates. | ||
| 711 | |||
| 712 | Verify the datatype of $value. Mostly useful for numerical | ||
| 713 | values. $field_name or $type must be given. | ||
| 714 | |||
| 715 | Return 0 if a numeric type was required but not given. | ||
| 716 | Return -1 if a decimal was given and will be rounded to an integer. | ||
| 717 | |||
| 718 | mysql users: | ||
| 719 | Return -2 if a negative number was given for an unsigned integer type. | ||
| 720 | |||
| 721 | See also DBIx::IO. | ||
| 722 | |||
| 723 | =cut | ||
| 724 | sub verify_datatype | ||
| 725 | { | ||
| 726 | my $self = shift; | ||
| 727 | return $self->{io}->verify_datatype(@_); | ||
| 728 | } | ||
| 729 | |||
| 730 | =pod | ||
| 731 | |||
| 732 | =item C<default_value> | ||
| 733 | |||
| 734 | $default_value = $table->default_value($field_name); | ||
| 735 | |||
| 736 | Return the default value listed in the data dictionary | ||
| 737 | for $field_name. | ||
| 738 | See also DBIx::IO. | ||
| 739 | |||
| 740 | =cut | ||
| 741 | sub default_value | ||
| 742 | { | ||
| 743 | my $self = shift; | ||
| 744 | return $self->{io}->default_value(@_); | ||
| 745 | } | ||
| 746 | |||
| 747 | |||
| 748 | =pod | ||
| 749 | |||
| 750 | =item C<required> | ||
| 751 | |||
| 752 | $bool = $table->required($field_name); | ||
| 753 | |||
| 754 | Return true if $field_name is listed as NOT NULL in the data dictionary. | ||
| 755 | See also DBIx::IO. | ||
| 756 | |||
| 757 | =cut | ||
| 758 | sub required | ||
| 759 | { | ||
| 760 | my $self = shift; | ||
| 761 | return $self->{io}->required(@_); | ||
| 762 | } | ||
| 763 | |||
| 764 | |||
| 765 | =pod | ||
| 766 | |||
| 767 | =item C<field_length> | ||
| 768 | |||
| 769 | $integer = $table->field_length($field_name); | ||
| 770 | |||
| 771 | Return the maximum length of $field_name according to the data dictionary. | ||
| 772 | Length will be compensated for numbers with decimals, and sign. | ||
| 773 | See also DBIx::IO. | ||
| 774 | |||
| 775 | =cut | ||
| 776 | sub field_length | ||
| 777 | { | ||
| 778 | my $self = shift; | ||
| 779 | return $self->{io}->field_length(@_); | ||
| 780 | } | ||
| 781 | |||
| 782 | =pod | ||
| 783 | |||
| 784 | =item C<add_values> | ||
| 785 | |||
| 786 | $success = $table->add_values($add_hash); | ||
| 787 | |||
| 788 | Add a batch of COLUMN_NAME => value pairs from $add_hash. | ||
| 789 | For each value added, the corresponding <COLUMN_NAME>($val) | ||
| 790 | method will be called. | ||
| 791 | |||
| 792 | Return false if error. | ||
| 793 | |||
| 794 | =cut | ||
| 795 | sub add_values | ||
| 796 | { | ||
| 797 | my ($self,$add) = @_; | ||
| 798 | ref($self) || ($self->{io}->_alert("Method must be called by an object"), return undef); | ||
| 799 | ref($add) || ($self->{io}->_alert("\$add must be a hash ref!"), return undef); | ||
| 800 | my ($field,$val); | ||
| 801 | while (($field,$val) = each %$add) | ||
| 802 | { | ||
| 803 | $field = uc($field); | ||
| 804 | defined(eval("\$self->${field}(\$val)")) || ($self->{io}->_alert("Check routine failed for $field: $val"), return undef); | ||
| 805 | } | ||
| 806 | return 1; | ||
| 807 | } | ||
| 808 | |||
| 809 | |||
| 810 | =pod | ||
| 811 | |||
| 812 | =item C<existing_table_names> | ||
| 813 | |||
| 814 | $sorted_arrayref = DBIx::IO::Table->existing_table_names(); | ||
| 815 | |||
| 816 | Return a sorted arrayref of table names found in the | ||
| 817 | data dictionary. | ||
| 818 | |||
| 819 | Class or object method. | ||
| 820 | |||
| 821 | Return undef if db error. | ||
| 822 | |||
| 823 | =cut | ||
| 824 | sub existing_table_names | ||
| 825 | { | ||
| 826 | my ($caller,$dbh) = @_; | ||
| 827 | my $ioclass = $caller->_pull_driver($dbh); | ||
| 828 | return $ioclass->existing_table_names($dbh); | ||
| 829 | } | ||
| 830 | |||
| 831 | |||
| 832 | =pod | ||
| 833 | |||
| 834 | =back | ||
| 835 | |||
| 836 | =cut | ||
| 837 | |||
| 838 | |||
| 839 | 1; | ||
| 840 | |||
| 841 | __END__ |