| 1 | package SPOPS::DBI::TypeInfo; | ||
| 2 | |||
| 3 | # $Id: TypeInfo.pm,v 1.6 2004/02/26 01:03:23 lachoy Exp $ | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use base qw( Class::Accessor ); | ||
| 7 | use DBI qw( :sql_types ); | ||
| 8 | use Log::Log4perl qw( get_logger ); | ||
| 9 | use SPOPS::Exception qw( spops_error ); | ||
| 10 | use SPOPS::Exception::DBI qw( spops_dbi_error ); | ||
| 11 | |||
| 12 | my @FIELDS = qw( table database ); | ||
| 13 | __PACKAGE__->mk_accessors( @FIELDS ); | ||
| 14 | |||
| 15 | my ( $log ); | ||
| 16 | |||
| 17 | my %FAKE_TYPES = ( | ||
| 18 | int => SQL_INTEGER, | ||
| 19 | num => SQL_NUMERIC, | ||
| 20 | float => SQL_FLOAT, | ||
| 21 | char => SQL_VARCHAR, | ||
| 22 | date => SQL_DATE, | ||
| 23 | ); | ||
| 24 | |||
| 25 | |||
| 26 | ######################################## | ||
| 27 | # CONSTRUCTOR | ||
| 28 | |||
| 29 | sub new { | ||
| 30 | my ( $class, $params ) = @_; | ||
| 31 | my $self = bless( {}, $class ); | ||
| 32 | |||
| 33 | # First assign the simple fields | ||
| 34 | |||
| 35 | foreach my $field ( @FIELDS ) { | ||
| 36 | next unless ( $params->{ $field } ); | ||
| 37 | $self->$field( $params->{ $field } ); | ||
| 38 | } | ||
| 39 | |||
| 40 | # Next assign parallel field/type arrays | ||
| 41 | |||
| 42 | if ( ref $params->{fields} eq 'ARRAY' and ref $params->{types} eq 'ARRAY' ) { | ||
| 43 | my $num_fields = scalar @{ $params->{fields} }; | ||
| 44 | my $num_types = scalar @{ $params->{types} }; | ||
| 45 | if ( $num_fields != $num_types ) { | ||
| 46 | spops_error "Incorrect parameters: number of fields ($num_fields) ", | ||
| 47 | "is not equal to the number of types ($num_types)"; | ||
| 48 | } | ||
| 49 | $self->{_fields} = $params->{fields}; | ||
| 50 | $self->{_types} = $params->{types}; | ||
| 51 | for ( 0 .. $num_fields - 1 ) { | ||
| 52 | $self->add_type( $params->{fields}[ $_ ], $params->{types}[ $_ ] ); | ||
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | # Next assign a map of field -> type | ||
| 57 | |||
| 58 | if ( ref $params->{map} eq 'HASH' ) { | ||
| 59 | while ( my ( $field, $type ) = each %{ $params->{map} } ) { | ||
| 60 | $self->add_type( $field, $type ); | ||
| 61 | } | ||
| 62 | } | ||
| 63 | |||
| 64 | return $self; | ||
| 65 | } | ||
| 66 | |||
| 67 | |||
| 68 | ######################################## | ||
| 69 | # GET/SET METHODS | ||
| 70 | |||
| 71 | sub add_type { | ||
| 72 | my ( $self, $field, $type ) = @_; | ||
| 73 | $log ||= get_logger(); | ||
| 74 | |||
| 75 | # If it's already defined, issue a warning but don't change it | ||
| 76 | |||
| 77 | if ( my $existing_type = $self->get_type( $field ) ) { | ||
| 78 | $log->warn( "Field [$field] was already defined with type ", | ||
| 79 | "[$existing_type]. No action taken." ); | ||
| 80 | return $existing_type; | ||
| 81 | } | ||
| 82 | |||
| 83 | # Check to see if it's a fake type (suppress warnings since this | ||
| 84 | # use of int() will issue a warning if it's 'char'/'date'/etc., | ||
| 85 | # which is what we're checking for. | ||
| 86 | |||
| 87 | { | ||
| 88 | no warnings; | ||
| 89 | unless ( int( $type ) ) { | ||
| 90 | my $faked = $FAKE_TYPES{ $type }; | ||
| 91 | unless ( $faked ) { | ||
| 92 | spops_error "Type [$type] for [$field] is invalid -- it is ", | ||
| 93 | "not a DBI type and not one of the 'fake' types, ", | ||
| 94 | "which are: ", join( ', ', sort keys %FAKE_TYPES ); | ||
| 95 | } | ||
| 96 | $type = $faked; | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | # Assign | ||
| 101 | |||
| 102 | $self->{_map}{ lc $field } = int( $type ); | ||
| 103 | push @{ $self->{_fields} }, $field; | ||
| 104 | push @{ $self->{_types} }, $type; | ||
| 105 | return $type; | ||
| 106 | } | ||
| 107 | |||
| 108 | |||
| 109 | sub get_type { | ||
| 110 | my ( $self, $field ) = @_; | ||
| 111 | return undef unless ( $field ); | ||
| 112 | return $self->{_map}{ lc $field }; | ||
| 113 | } | ||
| 114 | |||
| 115 | |||
| 116 | sub get_fields { | ||
| 117 | my ( $self ) = @_; | ||
| 118 | return @{ $self->{_fields} }; | ||
| 119 | } | ||
| 120 | |||
| 121 | |||
| 122 | sub get_types { | ||
| 123 | my ( $self ) = @_; | ||
| 124 | return @{ $self->{_types} }; | ||
| 125 | } | ||
| 126 | |||
| 127 | |||
| 128 | sub fetch_types { | ||
| 129 | my ( $self, $dbh, $sql ) = @_; | ||
| 130 | |||
| 131 | # Provide a default SQL implementation | ||
| 132 | |||
| 133 | unless ( $sql ) { | ||
| 134 | unless ( $self->table ) { | ||
| 135 | spops_error "Cannot retrieve fields and types from database: ", | ||
| 136 | "no SQL given to use, and the 'table' object ", | ||
| 137 | "property is not set."; | ||
| 138 | } | ||
| 139 | $sql = sprintf( "SELECT * FROM %s WHERE 1 = 0", $self->table ); | ||
| 140 | } | ||
| 141 | |||
| 142 | my $sth = eval { $dbh->prepare( $sql ) }; | ||
| 143 | if ( $@ ) { | ||
| 144 | spops_dbi_error $@, { sql => $sql, action => 'prepare' }; | ||
| 145 | } | ||
| 146 | |||
| 147 | my $rv = eval { $sth->execute }; | ||
| 148 | if ( $@ ) { | ||
| 149 | spops_dbi_error $@, { sql => $sql, action => 'execute' }; | ||
| 150 | } | ||
| 151 | |||
| 152 | eval { | ||
| 153 | my $fields = $sth->{NAME}; | ||
| 154 | my $types = $sth->{TYPE}; | ||
| 155 | for ( my $i = 0; $i < scalar @{ $fields }; $i++ ) { | ||
| 156 | $self->add_type( $fields->[ $i ], $types->[ $i ] ); | ||
| 157 | } | ||
| 158 | }; | ||
| 159 | if ( $@ ) { | ||
| 160 | spops_error "Cannot retrieve name/type info for ", | ||
| 161 | $self->table, ": $@"; | ||
| 162 | } | ||
| 163 | $sth->finish; | ||
| 164 | return $self; | ||
| 165 | } | ||
| 166 | |||
| 167 | sub as_hash { | ||
| 168 | my ( $self ) = @_; | ||
| 169 | my %h = (); | ||
| 170 | my $num_fields = scalar @{ $self->{_fields} }; | ||
| 171 | for ( my $i = 0; $i < $num_fields; $i++ ) { | ||
| 172 | $h{ $self->{_fields}[ $i ] } = $self->{_types}[ $i ]; | ||
| 173 | } | ||
| 174 | return %h; | ||
| 175 | } | ||
| 176 | |||
| 177 | 1; | ||
| 178 | |||
| 179 | __END__ |