| 1 | package SPOPS::ClassFactory::DBI; | ||
| 2 | |||
| 3 | # $Id: DBI.pm,v 3.11 2004/06/02 00:48:22 lachoy Exp $ | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use Log::Log4perl qw( get_logger ); | ||
| 7 | use SPOPS; | ||
| 8 | use SPOPS::ClassFactory qw( OK ERROR DONE ); | ||
| 9 | |||
| 10 | my $log = get_logger(); | ||
| 11 | |||
| 12 | $SPOPS::ClassFactory::DBI::VERSION = sprintf("%d.%02d", q$Revision: 3.11 $ =~ /(\d+)\.(\d+)/); | ||
| 13 | |||
| 14 | # NOTE: The behavior is installed in SPOPS::DBI | ||
| 15 | |||
| 16 | |||
| 17 | ######################################## | ||
| 18 | # MULTIPLE FIELD KEYS | ||
| 19 | ######################################## | ||
| 20 | |||
| 21 | my $generic_multifield_id = <<'MFID'; | ||
| 22 | |||
| 23 | sub %%GEN_CLASS%%::id { | ||
| 24 | my ( $self, $id ) = @_; | ||
| 25 | if ( $id ) { | ||
| 26 | ( %%ID_FIELD_OBJECT_LIST%% ) = split /\s*,\s*/, $id; | ||
| 27 | } | ||
| 28 | return wantarray ? ( %%ID_FIELD_OBJECT_LIST%% ) | ||
| 29 | : join( ',', %%ID_FIELD_OBJECT_LIST%% ); | ||
| 30 | } | ||
| 31 | MFID | ||
| 32 | |||
| 33 | |||
| 34 | # Generate an ID method for classes that have multiple-field primary | ||
| 35 | # keys | ||
| 36 | |||
| 37 | sub conf_multi_field_key_id { | ||
| 38 | my ( $class ) = @_; | ||
| 39 | my $CONFIG = $class->CONFIG; | ||
| 40 | my $id_field = $CONFIG->{id_field}; | ||
| 41 | |||
| 42 | return ( OK, undef ) unless ( ref $id_field eq 'ARRAY' ); | ||
| 43 | if ( scalar @{ $id_field } == 1 ) { | ||
| 44 | $CONFIG->{id_field} = $id_field->[0]; | ||
| 45 | return ( OK, undef ); | ||
| 46 | } | ||
| 47 | |||
| 48 | my $id_object_reference = join( ', ', | ||
| 49 | map { '$self->{' . $_ . '}' } | ||
| 50 | @{ $id_field } ); | ||
| 51 | my $id_sub = $generic_multifield_id; | ||
| 52 | $id_sub =~ s/%%GEN_CLASS%%/$class/g; | ||
| 53 | $id_sub =~ s/%%ID_FIELD_OBJECT_LIST%%/$id_object_reference/g; | ||
| 54 | $log->is_debug && | ||
| 55 | $log->debug( "Evaluation method 'id' for class [$class]\n$id_sub" ); | ||
| 56 | { | ||
| 57 | local $SIG{__WARN__} = sub { return undef }; | ||
| 58 | eval $id_sub; | ||
| 59 | if ( $@ ) { | ||
| 60 | warn "Code: $id_sub\n"; | ||
| 61 | return ( ERROR, "Cannot create multifield 'id()' method for " . | ||
| 62 | "class [$class]: $@" ); | ||
| 63 | } | ||
| 64 | } | ||
| 65 | return ( DONE, undef ); | ||
| 66 | } | ||
| 67 | |||
| 68 | |||
| 69 | # TODO: The explicit 'SPOPS::DBI' method below works, but it ignores | ||
| 70 | # anything in ISA between $class and it that might define-and-forward | ||
| 71 | # (or override) fetch(). | ||
| 72 | |||
| 73 | my $generic_multifield_etc = <<'MFETC'; | ||
| 74 | |||
| 75 | sub %%GEN_CLASS%%::fetch { | ||
| 76 | my ( $class, $id, @params ) = @_; | ||
| 77 | my $id_string = ( ref $id eq 'ARRAY' ) | ||
| 78 | ? join( ',', @{ $id } ) : $id; | ||
| 79 | return $class->SPOPS::DBI::fetch( $id_string, @params ); | ||
| 80 | } | ||
| 81 | |||
| 82 | sub %%GEN_CLASS%%::clone { | ||
| 83 | my ( $self, $p ) = @_; | ||
| 84 | my $class = $p->{_class} || ref $self; | ||
| 85 | $log->is_info && | ||
| 86 | $log->info( "Cloning new object of class ($class) from old ", | ||
| 87 | "object of class (", ref $self, ")" ); | ||
| 88 | my %initial_data = (); | ||
| 89 | |||
| 90 | my %id_field = map { $_ => 1 } $class->id_field; | ||
| 91 | |||
| 92 | while ( my ( $k, $v ) = each %{ $self } ) { | ||
| 93 | next unless ( $k ); | ||
| 94 | next if ( $id_field{ $k } ); | ||
| 95 | $initial_data{ $k } = $p->{ $k } || $v; | ||
| 96 | } | ||
| 97 | |||
| 98 | my $cloned = $class->new({ %initial_data, skip_default_values => 1 }); | ||
| 99 | if ( $p->{id} ) { | ||
| 100 | $cloned->id( $p->{id} ); | ||
| 101 | } | ||
| 102 | else { | ||
| 103 | foreach my $field ( keys %id_field ) { | ||
| 104 | $cloned->{ $field } = $p->{ $field } if ( $p->{ $field } ); | ||
| 105 | } | ||
| 106 | } | ||
| 107 | return $cloned; | ||
| 108 | } | ||
| 109 | |||
| 110 | sub %%GEN_CLASS%%::id_field { | ||
| 111 | return wantarray ? %%ID_FIELD_NAME_LIST%% | ||
| 112 | : join( ',', %%ID_FIELD_NAME_LIST%% ); | ||
| 113 | } | ||
| 114 | |||
| 115 | sub %%GEN_CLASS%%::id_clause { | ||
| 116 | my ( $self, $id, $opt, $p ) = @_; | ||
| 117 | $opt ||= ''; | ||
| 118 | $p ||= {}; | ||
| 119 | my %val = (); | ||
| 120 | my $db = $p->{db} || $self->global_datasource_handle( $p->{connect_key} ); | ||
| 121 | unless ( $db ) { | ||
| 122 | SPOPS::Exception->throw( "Cannot create ID clause: no DB handle available" ); | ||
| 123 | } | ||
| 124 | |||
| 125 | # let any errors bubble up | ||
| 126 | my $type_info = $self->db_discover_types( | ||
| 127 | $self->table_name, | ||
| 128 | { dbi_type_info => $p->{dbi_type_info}, | ||
| 129 | db => $db, | ||
| 130 | DEBUG => $p->{DEBUG} } ); | ||
| 131 | if ( $id and ref $id eq 'ARRAY' ) { | ||
| 132 | ( %%ID_FIELD_VARIABLE_LIST%% ) = @{ $id }; | ||
| 133 | } | ||
| 134 | elsif ( $id ) { | ||
| 135 | ( %%ID_FIELD_VARIABLE_LIST%% ) = split /\s*,\s*/, $id; | ||
| 136 | } | ||
| 137 | else { | ||
| 138 | ( %%ID_FIELD_VARIABLE_LIST%% ) = ( %%ID_FIELD_OBJECT_LIST%% ); | ||
| 139 | } | ||
| 140 | unless ( %%ID_FIELD_BOOLEAN_LIST%% ) { | ||
| 141 | SPOPS::Exception->throw( "Insufficient values for ID (%%ID_FIELD_VARIABLE_LIST%%)" ); | ||
| 142 | } | ||
| 143 | my @clause = (); | ||
| 144 | my $table_name = $self->table_name; | ||
| 145 | foreach my $id_field ( %%ID_FIELD_NAME_LIST%% ) { | ||
| 146 | my $use_id_field = ( $opt eq 'noqualify' ) | ||
| 147 | ? $id_field | ||
| 148 | : join( '.', $table_name, $id_field ); | ||
| 149 | my $quoted_value = $self->sql_quote( $val{ $id_field }, | ||
| 150 | $type_info->get_type( $id_field ), | ||
| 151 | $db ); | ||
| 152 | push @clause, join( ' = ', $use_id_field, $quoted_value ); | ||
| 153 | } | ||
| 154 | return join( ' AND ', @clause ); | ||
| 155 | } | ||
| 156 | |||
| 157 | # should return something like: | ||
| 158 | # ( 'mytable.id1', 'mytable.id2' ) | ||
| 159 | sub %%GEN_CLASS%%::id_field_select { | ||
| 160 | my ( $class, $p ) = @_; | ||
| 161 | return ( $p->{noqualify} ) | ||
| 162 | ? %%ID_FIELD_NAME_LIST%% | ||
| 163 | : map { join( '.', $class->table_name, $_ ) } %%ID_FIELD_NAME_LIST%%; | ||
| 164 | } | ||
| 165 | |||
| 166 | MFETC | ||
| 167 | |||
| 168 | |||
| 169 | sub conf_multi_field_key_other { | ||
| 170 | my ( $class ) = @_; | ||
| 171 | my $CONFIG = $class->CONFIG; | ||
| 172 | my $id_field = $CONFIG->{id_field}; | ||
| 173 | |||
| 174 | return ( OK, undef ) unless ( ref $id_field eq 'ARRAY' ); | ||
| 175 | if ( scalar @{ $id_field } == 1 ) { | ||
| 176 | $CONFIG->{id_field} = $id_field->[0]; | ||
| 177 | return ( OK, undef ); | ||
| 178 | } | ||
| 179 | |||
| 180 | my $id_object_reference = join( ', ', | ||
| 181 | map { '$self->{' . $_ . '}' } | ||
| 182 | @{ $id_field } ); | ||
| 183 | my $id_variable_reference = join( ', ', map { "\$val{$_}" } @{ $id_field } ); | ||
| 184 | my $id_boolean_reference = join( ' and ', map { "\$val{$_}" } @{ $id_field } ); | ||
| 185 | my $id_field_reference = 'qw( ' . join( ' ', @{ $id_field } ) . ' )'; | ||
| 186 | my $other_sub = $generic_multifield_etc; | ||
| 187 | $other_sub =~ s/%%GEN_CLASS%%/$class/g; | ||
| 188 | $other_sub =~ s/%%ID_FIELD_OBJECT_LIST%%/$id_object_reference/g; | ||
| 189 | $other_sub =~ s/%%ID_FIELD_VARIABLE_LIST%%/$id_variable_reference/g; | ||
| 190 | $other_sub =~ s/%%ID_FIELD_BOOLEAN_LIST%%/$id_boolean_reference/g; | ||
| 191 | $other_sub =~ s/%%ID_FIELD_NAME_LIST%%/$id_field_reference/g; | ||
| 192 | $log->is_debug && | ||
| 193 | $log->debug( "Evaluating other multifield key methods:\n$other_sub" ); | ||
| 194 | { | ||
| 195 | local $SIG{__WARN__} = sub { return undef }; | ||
| 196 | eval $other_sub; | ||
| 197 | if ( $@ ) { | ||
| 198 | return ( ERROR, "Cannot create multifield key 'clone()', " . | ||
| 199 | "'id_field(), 'id_clause()', and " . | ||
| 200 | "'id_field_select()' methods for [$class]. " . | ||
| 201 | "Error: $@" ); | ||
| 202 | } | ||
| 203 | } | ||
| 204 | return ( OK, undef ); | ||
| 205 | } | ||
| 206 | |||
| 207 | |||
| 208 | ######################################## | ||
| 209 | # links_to | ||
| 210 | ######################################## | ||
| 211 | |||
| 212 | # EVAL'D SUBROUTINES | ||
| 213 | # | ||
| 214 | # This is the routine we'll be putting in the namespace of all the | ||
| 215 | # classes that have asked to be linked to other classes; obviously, | ||
| 216 | # the items marked like this: %%KEY%% will be replaced before the eval | ||
| 217 | # is done. | ||
| 218 | |||
| 219 | my $generic_linksto = <<'LINKSTO'; | ||
| 220 | |||
| 221 | sub %%GEN_CLASS%%::%%LINKSTO_ALIAS%% { | ||
| 222 | my ( $self, $p ) = @_; | ||
| 223 | my $log = Log::Log4perl::get_logger(); | ||
| 224 | $p ||= {}; | ||
| 225 | $p->{select} = [ '%%LINKSTO_ID_FIELD%%' ]; | ||
| 226 | $p->{from} = [ '%%LINKSTO_TABLE%%' ]; | ||
| 227 | my $id_clause = $self->id_clause( $self->id, 'noqualify', $p ); | ||
| 228 | $p->{where} = ( $p->{where} ) | ||
| 229 | ? join ( ' AND ', $p->{where}, $id_clause ) : $id_clause; | ||
| 230 | $p->{return} = 'list'; | ||
| 231 | $p->{db} ||= %%LINKSTO_CLASS%%->global_datasource_handle; | ||
| 232 | my $rows = %%LINKSTO_CLASS%%->db_select( $p ); | ||
| 233 | my @obj = (); | ||
| 234 | foreach my $info ( @{ $rows } ) { | ||
| 235 | my $item = eval { %%LINKSTO_CLASS%%->fetch( $info->[0], $p ) }; | ||
| 236 | if ( $@ ) { | ||
| 237 | $log->error( " Cannot fetch linked object %%LINKSTO_CLASS%% [$info->[0]] ", | ||
| 238 | "from %%GEN_CLASS%%: $@\nContinuing with others..." ); | ||
| 239 | next; | ||
| 240 | } | ||
| 241 | push @obj, $item if ( $item ); | ||
| 242 | } | ||
| 243 | return \@obj; | ||
| 244 | } | ||
| 245 | |||
| 246 | sub %%GEN_CLASS%%::%%LINKSTO_ALIAS%%_add { | ||
| 247 | my ( $self, $link_id_list, $p ) = @_; | ||
| 248 | return 0 unless ( defined $link_id_list ); | ||
| 249 | my $log = Log::Log4perl::get_logger(); | ||
| 250 | |||
| 251 | $p ||= {}; | ||
| 252 | |||
| 253 | # Allow user to pass only one ID to add (scalar) or an | ||
| 254 | # arrayref (ref) | ||
| 255 | |||
| 256 | $link_id_list = ( ref $link_id_list eq 'ARRAY' ) | ||
| 257 | ? $link_id_list : [ $link_id_list ]; | ||
| 258 | my $added = 0; | ||
| 259 | $p->{db} ||= %%LINKSTO_CLASS%%->global_datasource_handle; | ||
| 260 | foreach my $link_item ( @{ $link_id_list } ) { | ||
| 261 | my $link_id = ( ref $link_item ) ? $link_item->id : $link_item; | ||
| 262 | $log->is_info && | ||
| 263 | $log->info( "Trying to add link to ID [$link_id]" ); | ||
| 264 | %%LINKSTO_CLASS%%->db_insert({ table => '%%LINKSTO_TABLE%%', | ||
| 265 | field => [ '%%ID_FIELD%%', '%%LINKSTO_ID_FIELD%%' ], | ||
| 266 | value => [ $self->{%%ID_FIELD%%}, $link_id ], | ||
| 267 | db => $p->{db}, | ||
| 268 | DEBUG => $p->{DEBUG} }); | ||
| 269 | $added++; | ||
| 270 | } | ||
| 271 | return $added; | ||
| 272 | } | ||
| 273 | |||
| 274 | sub %%GEN_CLASS%%::%%LINKSTO_ALIAS%%_remove { | ||
| 275 | my ( $self, $link_id_list, $p ) = @_; | ||
| 276 | $p ||= {}; | ||
| 277 | my $log = Log::Log4perl::get_logger(); | ||
| 278 | |||
| 279 | # Allow user to pass only one ID to remove (scalar) or an | ||
| 280 | # arrayref (ref) | ||
| 281 | |||
| 282 | $link_id_list = ( ref $link_id_list eq 'ARRAY' ) | ||
| 283 | ? $link_id_list : [ $link_id_list ]; | ||
| 284 | my $removed = 0; | ||
| 285 | $p->{db} ||= %%LINKSTO_CLASS%%->global_datasource_handle; | ||
| 286 | foreach my $link_item ( @{ $link_id_list } ) { | ||
| 287 | my $link_id = ( ref $link_item ) ? $link_item->id : $link_item; | ||
| 288 | $log->is_info && | ||
| 289 | $log->info( "Trying to remove link to ID ($link_id)" ); | ||
| 290 | my $from_id_clause = $self->id_clause( undef, 'noqualify', $p ); | ||
| 291 | my $to_id_clause = %%LINKSTO_CLASS%%->id_clause( $link_id, 'noqualify', $p ); | ||
| 292 | %%LINKSTO_CLASS%%->db_delete({ table => '%%LINKSTO_TABLE%%', | ||
| 293 | where => join( ' AND ', $from_id_clause, $to_id_clause ), | ||
| 294 | db => $p->{db}, | ||
| 295 | DEBUG => $p->{DEBUG} }); | ||
| 296 | $removed++; | ||
| 297 | } | ||
| 298 | return $removed; | ||
| 299 | } | ||
| 300 | |||
| 301 | LINKSTO | ||
| 302 | |||
| 303 | |||
| 304 | # | ||
| 305 | # ACTUAL SUBROUTINE | ||
| 306 | # | ||
| 307 | |||
| 308 | sub conf_relate_links_to { | ||
| 309 | my ( $class ) = @_; | ||
| 310 | my $config = $class->CONFIG; | ||
| 311 | $log->is_info && | ||
| 312 | $log->info( "Adding DBI relationships for: ($class)" ); | ||
| 313 | |||
| 314 | # Grab the information for the class we're modifying | ||
| 315 | |||
| 316 | my $this_id_field = $config->{id_field}; | ||
| 317 | my $this_alias = $config->{main_alias}; | ||
| 318 | |||
| 319 | # Process the 'links_to' aliases -- pretty straightforward (see pod) | ||
| 320 | |||
| 321 | if ( my $links_to = $config->{links_to} ) { | ||
| 322 | while ( my ( $to_class, $link_info ) = each %{ $links_to } ) { | ||
| 323 | |||
| 324 | # Since the class specified can be a subclass of what's | ||
| 325 | # generated, ensure that it's available | ||
| 326 | |||
| 327 | eval "require $to_class"; | ||
| 328 | my $require_error = $@; | ||
| 329 | my $to_config = eval { $to_class->CONFIG }; | ||
| 330 | if ( $@ ) { | ||
| 331 | return ( ERROR, "Failed to retrieve configuration from " . | ||
| 332 | "'$to_class': $@. (Require error: $require_error)" ); | ||
| 333 | } | ||
| 334 | |||
| 335 | my ( $to_alias, $to_id_field, $link_table, $from_id_field ); | ||
| 336 | |||
| 337 | # If the linking information is a hashref then give the | ||
| 338 | # user the opportunity to define everything | ||
| 339 | |||
| 340 | if ( ref( $link_info ) eq 'HASH' ) { | ||
| 341 | $link_table = $link_info->{table}; | ||
| 342 | $to_alias = $link_info->{alias} | ||
| 343 | || $to_config->{main_alias}; | ||
| 344 | $to_id_field = $link_info->{to_id_field} | ||
| 345 | || $to_config->{id_field}; | ||
| 346 | $from_id_field = $link_info->{from_id_field} | ||
| 347 | || $this_id_field; | ||
| 348 | } | ||
| 349 | |||
| 350 | # Otherwise, if the value is a simple scalar then it names | ||
| 351 | # the table | ||
| 352 | |||
| 353 | else { | ||
| 354 | $link_table = $link_info; | ||
| 355 | $to_alias = $to_config->{main_alias}; | ||
| 356 | $to_id_field = $to_config->{id_field}; | ||
| 357 | $from_id_field = $this_id_field; | ||
| 358 | } | ||
| 359 | |||
| 360 | my $link_subs = $generic_linksto; | ||
| 361 | $link_subs =~ s/%%ID_FIELD%%/$this_id_field/g; | ||
| 362 | $link_subs =~ s/%%GEN_CLASS%%/$class/g; | ||
| 363 | $link_subs =~ s/%%LINKSTO_CLASS%%/$to_class/g; | ||
| 364 | $link_subs =~ s/%%LINKSTO_ALIAS%%/$to_alias/g; | ||
| 365 | $link_subs =~ s/%%LINKSTO_ID_FIELD%%/$to_id_field/g; | ||
| 366 | $link_subs =~ s/%%LINKSTO_TABLE%%/$link_table/g; | ||
| 367 | $log->is_debug && | ||
| 368 | $log->debug( "Trying to create links_to routines from ", | ||
| 369 | "[$class: $from_id_field] to ", | ||
| 370 | "[$to_class: $to_id_field] using ", | ||
| 371 | "table [$link_table]" ); | ||
| 372 | $log->is_debug && | ||
| 373 | $log->debug( "Now going to eval the routine:\n$link_subs" ); | ||
| 374 | { | ||
| 375 | local $SIG{__WARN__} = sub { return undef }; | ||
| 376 | eval $link_subs; | ||
| 377 | if ( $@ ) { | ||
| 378 | return ( ERROR, "Cannot create 'links_to' methods for " . | ||
| 379 | "class [$class] linking to class " . | ||
| 380 | "[$to_class] via table [$link_table]. " . | ||
| 381 | "Error: $@" ); | ||
| 382 | } | ||
| 383 | } | ||
| 384 | } | ||
| 385 | } | ||
| 386 | $log->is_info && | ||
| 387 | $log->info( "Finished adding DBI relationships for ($class)" ); | ||
| 388 | return ( OK, undef ); | ||
| 389 | } | ||
| 390 | |||
| 391 | 1; | ||
| 392 | |||
| 393 | __END__ |