| 1 | use strict; | ||
| 2 | use warnings FATAL => 'all'; | ||
| 3 | |||
| 4 | package HTML::Tested::ClassDBI::Field::Column; | ||
| 5 | use Carp; | ||
| 6 | |||
| 7 | sub verify_arg { | ||
| 8 | my ($class, $root, $w, $arg) = @_; | ||
| 9 | my $gr = $w->options->{cdbi_group}; | ||
| 10 | confess($w->name . ": $arg - unknown column. Wrong cdbi_bind usage") | ||
| 11 | unless $root->_CDBI_Class->{$gr}->find_column($arg); | ||
| 12 | $root->_PrimaryFields->{$gr}->{ $w->name } = [ $arg ] | ||
| 13 | if $w->options->{cdbi_primary}; | ||
| 14 | } | ||
| 15 | |||
| 16 | sub bless_arg { | ||
| 17 | my ($class, $root, $w, $arg) = @_; | ||
| 18 | $class->verify_arg($root, $w, $arg); | ||
| 19 | return bless([ $arg ], $class); | ||
| 20 | } | ||
| 21 | |||
| 22 | sub get_column_value { | ||
| 23 | my ($self, $cdbi) = @_; | ||
| 24 | my $c = $self->column_name; | ||
| 25 | return exists $cdbi->{$c} ? $cdbi->{$c} : $cdbi->$c; | ||
| 26 | } | ||
| 27 | |||
| 28 | sub column_name { return shift()->[0]; } | ||
| 29 | |||
| 30 | sub update_column { | ||
| 31 | my ($self, $setter, $root, $name) = @_; | ||
| 32 | $setter->($self->[0], $root->$name) unless $root->ht_get_widget_option( | ||
| 33 | $name, "cdbi_readonly"); | ||
| 34 | } | ||
| 35 | |||
| 36 | my %_dt_fmts = (date => '%x', 'time' => '%X', timestamp => '%c'); | ||
| 37 | |||
| 38 | sub setup_datetime_from_info { | ||
| 39 | my ($self, $w, $info) = @_; | ||
| 40 | return unless $info->{type}; | ||
| 41 | my ($t) = ($info->{type} =~ /^(\w+)/); | ||
| 42 | $w->setup_datetime_option($_dt_fmts{$t}) if ($_dt_fmts{$t}); | ||
| 43 | } | ||
| 44 | |||
| 45 | sub setup_type_info { | ||
| 46 | my ($self, $root, $w, $info) = @_; | ||
| 47 | ($info ||= $root->pg_column_info($self->[0])) or return; | ||
| 48 | $w->options->{cdbi_column_info} = $info; | ||
| 49 | $w->options->{is_integer} = 1 if ($info->{type} eq 'integer' | ||
| 50 | || $info->{type} eq 'smallint'); | ||
| 51 | $w->push_constraint([ 'defined', '' ]) unless | ||
| 52 | ($w->options->{cdbi_readonly} || $info->{is_nullable}); | ||
| 53 | $self->setup_datetime_from_info($w, $info); | ||
| 54 | } | ||
| 55 | |||
| 56 | package HTML::Tested::ClassDBI::Field::Primary; | ||
| 57 | use base 'HTML::Tested::ClassDBI::Field::Column'; | ||
| 58 | |||
| 59 | sub verify_arg { | ||
| 60 | my ($class, $root, $w, $arg) = @_; | ||
| 61 | my @pc = map { $_->name } $root->_CDBI_Class->{ | ||
| 62 | $w->options->{cdbi_group} }->primary_columns; | ||
| 63 | $root->_PrimaryFields->{ $w->options->{cdbi_group} } | ||
| 64 | ->{ $w->name } = \@pc; | ||
| 65 | $root->ht_set_widget_option($w->name, "is_sealed", 1) | ||
| 66 | unless exists $w->options->{is_sealed}; | ||
| 67 | $root->ht_set_widget_option($w->name, "cdbi_readonly", 1) | ||
| 68 | unless exists $w->options->{cdbi_readonly}; | ||
| 69 | } | ||
| 70 | |||
| 71 | sub get_column_value { | ||
| 72 | my ($self, $cdbi) = @_; | ||
| 73 | my @pvals = map { $cdbi->$_ } $cdbi->primary_columns; | ||
| 74 | return join('_', @pvals); | ||
| 75 | } | ||
| 76 | |||
| 77 | sub update_column {} | ||
| 78 | |||
| 79 | sub setup_type_info { | ||
| 80 | my ($self, $root, $w) = @_; | ||
| 81 | my @pc = $root->primary_columns; | ||
| 82 | return if @pc > 1; | ||
| 83 | $self->SUPER::setup_type_info($root, $w, $root->pg_column_info($pc[0])); | ||
| 84 | } | ||
| 85 | |||
| 86 | package HTML::Tested::ClassDBI::Field::Array; | ||
| 87 | |||
| 88 | sub bless_arg { | ||
| 89 | my ($class, $root, $w, $arg) = @_; | ||
| 90 | return bless([ map { HTML::Tested::ClassDBI::Field->do_bless_arg( | ||
| 91 | $root, $w, $_) } @$arg ]); | ||
| 92 | } | ||
| 93 | |||
| 94 | sub get_column_value { | ||
| 95 | my ($self, $cdbi) = @_; | ||
| 96 | return [ map { $_->get_column_value($cdbi) } @$self ]; | ||
| 97 | } | ||
| 98 | |||
| 99 | sub update_column {} | ||
| 100 | |||
| 101 | sub setup_type_info { | ||
| 102 | my ($self, $root, $w) = @_; | ||
| 103 | for (my $i = 0; $i < @$self; $i++) { | ||
| 104 | my $iopts = $w->options->{$i} || {}; | ||
| 105 | $self->[$i]->setup_datetime_from_info($w, $iopts); | ||
| 106 | $w->options->{$i} = $iopts if %$iopts; | ||
| 107 | } | ||
| 108 | } | ||
| 109 | |||
| 110 | package HTML::Tested::ClassDBI::Field; | ||
| 111 | use HTML::Tested::ClassDBI::Upload; | ||
| 112 | |||
| 113 | sub do_bless_arg { | ||
| 114 | my ($class, $root, $w, $arg) = @_; | ||
| 115 | if (ref($arg) eq 'ARRAY') { | ||
| 116 | $class .= "::Array"; | ||
| 117 | } elsif ($arg eq 'Primary') { | ||
| 118 | $class .= "::Primary"; | ||
| 119 | } else { | ||
| 120 | $class .= "::Column"; | ||
| 121 | } | ||
| 122 | return $class->bless_arg($root, $w, $arg); | ||
| 123 | } | ||
| 124 | |||
| 125 | sub new { | ||
| 126 | my ($class, $root, $w) = @_; | ||
| 127 | return HTML::Tested::ClassDBI::Upload->new($root | ||
| 128 | , $w->options->{cdbi_upload} || $w->name) | ||
| 129 | if (exists $w->options->{cdbi_upload}); | ||
| 130 | return HTML::Tested::ClassDBI::Upload->new($root | ||
| 131 | , $w->options->{cdbi_upload_with_mime} || $w->name, 1) | ||
| 132 | if (exists $w->options->{cdbi_upload_with_mime}); | ||
| 133 | return unless exists $w->options->{cdbi_bind}; | ||
| 134 | |||
| 135 | my $arg = $w->options->{cdbi_bind} || $w->name; | ||
| 136 | return $class->do_bless_arg($root, $w, $arg); | ||
| 137 | } | ||
| 138 | |||
| 139 | 1; |