| 1 | =head1 NAME | ||
| 2 | |||
| 3 | HTML::Tested::ClassDBI - Enhances HTML::Tested to work with Class::DBI | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | package MyClass; | ||
| 8 | use base 'HTML::Tested::ClassDBI'; | ||
| 9 | |||
| 10 | __PACKAGE__->ht_add_widget('HTML::Tested::Value' | ||
| 11 | , id => cdbi_bind => "Primary"); | ||
| 12 | __PACKAGE__->ht_add_widget('HTML::Tested::Value' | ||
| 13 | , x => cdbi_bind => ""); | ||
| 14 | __PACKAGE__->ht_add_widget('HTML::Tested::Value::Upload' | ||
| 15 | , x => cdbi_upload => "largeobjectoid"); | ||
| 16 | __PACKAGE__->bind_to_class_dbi('MyClassDBI'); | ||
| 17 | |||
| 18 | # And later somewhere ... | ||
| 19 | # Query and load underlying Class::DBI: | ||
| 20 | my $list = MyClass->query_class_dbi(search => x => 15); | ||
| 21 | |||
| 22 | # or sync it to the database: | ||
| 23 | $obj->cdbi_create_or_update; | ||
| 24 | |||
| 25 | =head1 DESCRIPTION | ||
| 26 | |||
| 27 | This class provides mapping between Class::DBI and HTML::Tested objects. | ||
| 28 | |||
| 29 | It inherits from HTML::Tested. Widgets created with C<ht_add_widget> can have | ||
| 30 | additional C<cdbi_bind> property. | ||
| 31 | |||
| 32 | After calling C<bind_to_class_dbi> you would be able to automatically | ||
| 33 | synchronize between HTML::Tested::ClassDBI instance and underlying Class::DBI. | ||
| 34 | |||
| 35 | =cut | ||
| 36 | |||
| 37 | use strict; | ||
| 38 | use warnings FATAL => 'all'; | ||
| 39 | |||
| 40 | package HTML::Tested::ClassDBI; | ||
| 41 | use base 'HTML::Tested'; | ||
| 42 | use Carp; | ||
| 43 | use HTML::Tested::ClassDBI::Field; | ||
| 44 | use Data::Dumper; | ||
| 45 | |||
| 46 | my @_cdata = qw(_CDBI_Class _PrimaryFields _Field_Handlers _PrimaryKey); | ||
| 47 | __PACKAGE__->mk_classdata($_) for @_cdata; | ||
| 48 | |||
| 49 | our $VERSION = '0.23'; | ||
| 50 | |||
| 51 | sub class_dbi_object { shift()->class_dbi_object_gr('_CDBIM_', @_); } | ||
| 52 | |||
| 53 | sub class_dbi_object_gr { | ||
| 54 | my ($self, $gr, $val) = @_; | ||
| 55 | return $self->{_class_dbi_objects}->{$gr} if @_ == 2; | ||
| 56 | $self->{_class_dbi_objects}->{$gr} = $val; | ||
| 57 | } | ||
| 58 | |||
| 59 | sub cdbi_bind_from_fields { | ||
| 60 | my ($class, $gr) = @_; | ||
| 61 | for my $v (@{ $class->Widgets_List }) { | ||
| 62 | my $wgr = $v->options->{cdbi_group} || '_CDBIM_'; | ||
| 63 | $v->options->{cdbi_group} = $wgr; | ||
| 64 | next unless $wgr eq $gr; | ||
| 65 | my $f = HTML::Tested::ClassDBI::Field->new($class, $v, $gr) | ||
| 66 | or next; | ||
| 67 | $class->_Field_Handlers->{ $v->options->{cdbi_group} } | ||
| 68 | ->{$v->name} = $f; | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | sub CDBI_Class { return shift()->_CDBI_Class->{_CDBIM_} } | ||
| 73 | sub PrimaryFields { return shift()->_PrimaryFields->{_CDBIM_} } | ||
| 74 | sub Field_Handlers { return shift()->_Field_Handlers->{_CDBIM_} } | ||
| 75 | sub PrimaryKey { return shift()->_PrimaryKey->{_CDBIM_} } | ||
| 76 | |||
| 77 | =head1 METHODS | ||
| 78 | |||
| 79 | =head2 $class->bind_to_class_dbi($cdbi_class) | ||
| 80 | |||
| 81 | Binds $class to $cdbi_class, by going over all fields declared with C<cdbi_bind> | ||
| 82 | or C<cdbi_upload> option. | ||
| 83 | |||
| 84 | C<cdbi_bind> option value could be one of the following: | ||
| 85 | name of the column, empty string for the column named the same as field or for | ||
| 86 | array of columns. | ||
| 87 | |||
| 88 | C<cdbi_upload> can be used to upload file into the database. Uploaded file is | ||
| 89 | stored as PostgreSQL's large object. Its OID is assigned to the bound column. | ||
| 90 | |||
| 91 | C<cdbi_upload_with_mime> uploads the file and prepends its mime type as a | ||
| 92 | header. Use HTML::Tested::ClassDBI::Upload->strip_mime_header to pull it from | ||
| 93 | the data. | ||
| 94 | |||
| 95 | C<cdbi_readonly> boolean option can be used to make its widget readonly thus | ||
| 96 | skipping its value during update. Read only widgets will not be validated. | ||
| 97 | |||
| 98 | C<cdbi_primary> boolean option is used to make an unique column behave as | ||
| 99 | primary key. C<cdbi_load> will use this field while retrieving the object from | ||
| 100 | the database. | ||
| 101 | |||
| 102 | =cut | ||
| 103 | sub bind_to_class_dbi { shift()->bind_to_class_dbi_gr('_CDBIM_', @_); } | ||
| 104 | |||
| 105 | =head2 $class->bind_to_class_dbi_gr($group, $cdbi_class) | ||
| 106 | |||
| 107 | Binds $class to $cdbi class in group $group. Special group _CDBIM_ is used | ||
| 108 | as the default group. | ||
| 109 | |||
| 110 | =cut | ||
| 111 | sub bind_to_class_dbi_gr { | ||
| 112 | my ($class, $gr, $dbi_class, $opts) = @_; | ||
| 113 | $class->$_({}) for grep { !$class->$_ } @_cdata; | ||
| 114 | $class->_CDBI_Class->{$gr} = $dbi_class; | ||
| 115 | $class->_Field_Handlers->{$gr} = {}; | ||
| 116 | $class->_PrimaryFields->{$gr} = {}; | ||
| 117 | $class->cdbi_bind_from_fields($gr); | ||
| 118 | $class->_load_db_info($gr); | ||
| 119 | |||
| 120 | my $pk = $opts ? $opts->{PrimaryKey} : undef; | ||
| 121 | $class->_PrimaryKey->{$gr} = $pk if $pk; | ||
| 122 | confess "# No Primary fields given\n" | ||
| 123 | unless ($pk || %{ $class->_PrimaryFields->{$gr} }); | ||
| 124 | } | ||
| 125 | |||
| 126 | sub _get_cdbi_pk_for_retrieve { | ||
| 127 | my ($self, $gr) = @_; | ||
| 128 | my $pk = $self->_PrimaryKey->{$gr} or goto PFIELDS; | ||
| 129 | |||
| 130 | my %pkh; | ||
| 131 | for my $f (@$pk) { | ||
| 132 | my $v = $self->$f; | ||
| 133 | goto PFIELDS unless defined($v); | ||
| 134 | |||
| 135 | my $h = $self->_Field_Handlers->{$gr}->{$f}; | ||
| 136 | $pkh{ $h ? $h->column_name : $f } = $v; | ||
| 137 | } | ||
| 138 | return \%pkh if %pkh; | ||
| 139 | |||
| 140 | PFIELDS: | ||
| 141 | my $res = {}; | ||
| 142 | my %pf = %{ $self->_PrimaryFields->{$gr} }; | ||
| 143 | my ($pv, $pc); | ||
| 144 | while (my ($k, $v) = each %pf) { | ||
| 145 | $pv = $self->$k; | ||
| 146 | next unless defined $pv; | ||
| 147 | $pc = $v; | ||
| 148 | last; | ||
| 149 | } | ||
| 150 | return undef unless defined($pv); | ||
| 151 | my @vals = split('_', $pv); | ||
| 152 | for (my $i = 0; $i < @$pc; $i++) { | ||
| 153 | $res->{ $pc->[$i] } = $vals[$i]; | ||
| 154 | } | ||
| 155 | return $res; | ||
| 156 | } | ||
| 157 | |||
| 158 | sub _fill_in_from_class_dbi { | ||
| 159 | my ($self, $gr, $is_update) = @_; | ||
| 160 | my $fhs = $self->_Field_Handlers->{$gr}; | ||
| 161 | my $cdbi = $self->class_dbi_object_gr($gr); | ||
| 162 | while (my ($f, $h) = each %$fhs) { | ||
| 163 | next if ($is_update && defined $self->{$f}); | ||
| 164 | $self->$f($h->get_column_value($cdbi)); | ||
| 165 | } | ||
| 166 | } | ||
| 167 | |||
| 168 | sub cdbi_retrieve { shift()->_call_for_all('cdbi_retrieve_gr', @_); } | ||
| 169 | |||
| 170 | sub cdbi_retrieve_gr { | ||
| 171 | my ($self, $gr) = @_; | ||
| 172 | my $pk = $self->_get_cdbi_pk_for_retrieve($gr); | ||
| 173 | return unless defined($pk); | ||
| 174 | my $cdbi = $self->_CDBI_Class->{$gr}->retrieve(ref($pk) ? %$pk : $pk); | ||
| 175 | $self->class_dbi_object_gr($gr, $cdbi); | ||
| 176 | return $cdbi; | ||
| 177 | } | ||
| 178 | |||
| 179 | =head2 $obj->cdbi_load | ||
| 180 | |||
| 181 | Loads Class::DBI object using primary key field - the widget with special | ||
| 182 | C<cdbi_bind> => 'Primary'. | ||
| 183 | |||
| 184 | This method populates the rest of the bound fields with values of the loaded | ||
| 185 | Class::DBI object. | ||
| 186 | |||
| 187 | Returns retrieved Class::DBI object or undef. | ||
| 188 | |||
| 189 | =cut | ||
| 190 | sub cdbi_load { return shift()->_call_for_all('cdbi_load_gr', @_); } | ||
| 191 | |||
| 192 | sub _get_cdbi_object { | ||
| 193 | my ($self, $gr) = @_; | ||
| 194 | return $self->class_dbi_object_gr($gr) || $self->cdbi_retrieve_gr($gr); | ||
| 195 | } | ||
| 196 | |||
| 197 | sub cdbi_load_gr { | ||
| 198 | my ($self, $gr) = @_; | ||
| 199 | my $cdbi = $self->_get_cdbi_object($gr) or return; | ||
| 200 | $self->_fill_in_from_class_dbi($gr); | ||
| 201 | return $cdbi; | ||
| 202 | } | ||
| 203 | |||
| 204 | =head2 $class->query_class_dbi($func, @params) | ||
| 205 | |||
| 206 | This function loads underlying Class::DBI objects using query function $func | ||
| 207 | (e.g C<search>) with parameters contained in C<@params>. | ||
| 208 | |||
| 209 | For each of those objects new HTML::Tested::ClassDBI instance is created. | ||
| 210 | |||
| 211 | =cut | ||
| 212 | sub query_class_dbi { | ||
| 213 | my ($class, $func, @params) = @_; | ||
| 214 | my @cdbis = $class->CDBI_Class->$func(@params); | ||
| 215 | return [ map { | ||
| 216 | my $c = $class->new; | ||
| 217 | $c->class_dbi_object($_); | ||
| 218 | $c->_fill_in_from_class_dbi('_CDBIM_'); | ||
| 219 | $c; | ||
| 220 | } @cdbis ]; | ||
| 221 | } | ||
| 222 | |||
| 223 | sub _call_for_all { | ||
| 224 | my ($self, $func, @args) = @_; | ||
| 225 | $self->$func($_, @args) for keys %{ $self->_CDBI_Class }; | ||
| 226 | return $self->class_dbi_object; | ||
| 227 | } | ||
| 228 | |||
| 229 | =head2 $obj->cdbi_create($args) | ||
| 230 | |||
| 231 | Creates new database record using $obj fields. | ||
| 232 | |||
| 233 | Additional (optional) arguments are given by $args hash refernce. | ||
| 234 | |||
| 235 | =cut | ||
| 236 | sub cdbi_create { return shift()->_call_for_all('cdbi_create_gr', @_); } | ||
| 237 | |||
| 238 | =head2 $class->cdbi_create_gr($group, $args) | ||
| 239 | |||
| 240 | Creates new database record using $obj fields in group $group. | ||
| 241 | |||
| 242 | =cut | ||
| 243 | sub cdbi_create_gr { | ||
| 244 | my ($self, $gr, $args) = @_; | ||
| 245 | my $cargs = $self->_get_cdbi_pk_for_retrieve($gr) || {}; | ||
| 246 | $self->_update_fields($gr, sub { $cargs->{ $_[0] } = $_[1]; }, $args); | ||
| 247 | my $res; | ||
| 248 | eval { $res = $self->_CDBI_Class->{$gr}->create($cargs); }; | ||
| 249 | confess "SQL error: $@\n" . Dumper($self) if $@; | ||
| 250 | $self->class_dbi_object_gr($gr, $res); | ||
| 251 | $self->_fill_in_from_class_dbi($gr, 1); | ||
| 252 | return $res; | ||
| 253 | } | ||
| 254 | |||
| 255 | sub _update_fields { | ||
| 256 | my ($self, $gr, $setter, $args) = @_; | ||
| 257 | while (my ($field, $h) = each %{ $self->_Field_Handlers->{$gr} }) { | ||
| 258 | $h->update_column($setter, $self, $field) | ||
| 259 | if exists $self->{$field}; | ||
| 260 | } | ||
| 261 | my $cdbi = $self->_CDBI_Class->{$gr}; | ||
| 262 | while (my ($n, $v) = each %{ $args || {} }) { | ||
| 263 | $setter->($n, $v) if $cdbi->can($n); | ||
| 264 | } | ||
| 265 | } | ||
| 266 | |||
| 267 | =head2 $obj->cdbi_update($args) | ||
| 268 | |||
| 269 | Updates database records using $obj fields. | ||
| 270 | |||
| 271 | Additional (optional) arguments are given by $args hash refernce. | ||
| 272 | |||
| 273 | =cut | ||
| 274 | sub cdbi_update { return shift()->_call_for_all('cdbi_update_gr', @_); } | ||
| 275 | |||
| 276 | sub cdbi_update_gr { | ||
| 277 | my ($self, $gr, $args) = @_; | ||
| 278 | my $cdbi = $self->_get_cdbi_object($gr) | ||
| 279 | or confess("# Nothing found to update"); | ||
| 280 | $self->_update_fields($gr, sub { | ||
| 281 | my ($c, $val) = @_; | ||
| 282 | no warnings 'uninitialized'; | ||
| 283 | $cdbi->$c($val) if $cdbi->$c ne $val; | ||
| 284 | }, $args); | ||
| 285 | eval { $cdbi->update; }; | ||
| 286 | confess "SQL error: $@\n" . Dumper($self) if $@; | ||
| 287 | $self->_fill_in_from_class_dbi($gr, 1); | ||
| 288 | return $cdbi; | ||
| 289 | } | ||
| 290 | |||
| 291 | =head2 $obj->cdbi_create_or_update($args) | ||
| 292 | |||
| 293 | Calls C<cdbi_create> or C<cdbi_update> based on whether the database record | ||
| 294 | exists already. | ||
| 295 | |||
| 296 | Additional (optional) arguments are given by $args hash refernce. | ||
| 297 | |||
| 298 | =cut | ||
| 299 | sub cdbi_create_or_update { | ||
| 300 | return shift()->_call_for_all('cdbi_create_or_update_gr', @_); | ||
| 301 | } | ||
| 302 | |||
| 303 | sub cdbi_create_or_update_gr { | ||
| 304 | my ($self, $gr, $args) = @_; | ||
| 305 | return $self->_get_cdbi_object($gr) ? $self->cdbi_update_gr($gr, $args) | ||
| 306 | : $self->cdbi_create_gr($gr, $args); | ||
| 307 | } | ||
| 308 | |||
| 309 | =head2 $obj->cdbi_construct | ||
| 310 | |||
| 311 | Constructs underlying Class::DBI object using $obj fields. | ||
| 312 | |||
| 313 | =cut | ||
| 314 | sub cdbi_construct { return shift()->cdbi_construct_gr('_CDBIM_'); } | ||
| 315 | |||
| 316 | sub cdbi_construct_gr { | ||
| 317 | my ($self, $gr) = @_; | ||
| 318 | my $pk = $self->_get_cdbi_pk_for_retrieve($gr) | ||
| 319 | or confess "No primary key for $gr"; | ||
| 320 | return $self->_CDBI_Class->{$gr}->construct($pk); | ||
| 321 | } | ||
| 322 | |||
| 323 | =head2 $obj->cdbi_delete | ||
| 324 | |||
| 325 | Deletes database record using $obj fields. | ||
| 326 | |||
| 327 | =cut | ||
| 328 | sub cdbi_delete { shift()->cdbi_delete_gr('_CDBIM_', @_); } | ||
| 329 | |||
| 330 | sub cdbi_delete_gr { | ||
| 331 | my $c = shift()->cdbi_construct_gr(shift()); | ||
| 332 | $c->delete; | ||
| 333 | } | ||
| 334 | |||
| 335 | sub _load_db_info { | ||
| 336 | my ($class, $gr) = @_; | ||
| 337 | while (my ($n, $h) = each %{ $class->_Field_Handlers->{$gr} }) { | ||
| 338 | my $w = $class->ht_find_widget($n); | ||
| 339 | $h->setup_type_info($class->_CDBI_Class->{$gr}, $w); | ||
| 340 | } | ||
| 341 | } | ||
| 342 | |||
| 343 | =head2 $class->cdbi_set_many($class_objs, $cdbi_objs) | ||
| 344 | |||
| 345 | Initializes C<class_dbi_object> field of C<$class_objs> arrayref from the | ||
| 346 | Class::DBI objects given in C<$cdbi_objs>. | ||
| 347 | |||
| 348 | Useful to avoid overhead of retrieving Class::DBI objects one by one. | ||
| 349 | |||
| 350 | =cut | ||
| 351 | sub cdbi_set_many { | ||
| 352 | my ($class, $h_objs, $c_objs) = @_; | ||
| 353 | my @pcs = $class->CDBI_Class->primary_columns; | ||
| 354 | my %c_objs; | ||
| 355 | for my $co (@$c_objs) { | ||
| 356 | $c_objs{ join('_', map { $co->$_ } @pcs) } = $co; | ||
| 357 | } | ||
| 358 | for my $ho (@$h_objs) { | ||
| 359 | my $pk = $ho->_get_cdbi_pk_for_retrieve('_CDBIM_'); | ||
| 360 | my $co = $c_objs{ join('_', grep { defined($_) } | ||
| 361 | map { $pk->{$_} } @pcs) }; | ||
| 362 | $ho->class_dbi_object($co); | ||
| 363 | } | ||
| 364 | } | ||
| 365 | |||
| 366 | 1; | ||
| 367 | |||
| 368 | =head1 AUTHOR | ||
| 369 | |||
| 370 | Boris Sukholitko | ||
| 371 | CPAN ID: BOSU | ||
| 372 | |||
| 373 | boriss@gmail.com | ||
| 374 | |||
| 375 | |||
| 376 | =head1 COPYRIGHT | ||
| 377 | |||
| 378 | This program is free software; you can redistribute | ||
| 379 | it and/or modify it under the same terms as Perl itself. | ||
| 380 | |||
| 381 | The full text of the license can be found in the | ||
| 382 | LICENSE file included with this module. | ||
| 383 | |||
| 384 | |||
| 385 | =head1 SEE ALSO | ||
| 386 | |||
| 387 | HTML::Tested, Class::DBI | ||
| 388 | |||
| 389 | =cut | ||
| 390 |