| 1 | package MySQL::TableInfo; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Carp; | ||
| 5 | |||
| 6 | our $VERSION = '1.01'; | ||
| 7 | |||
| 8 | #### | ||
| 9 | # takes care of plural forms of methods and 'get_' prefix | ||
| 10 | #################### | ||
| 11 | #sub AUTOLOAD { } | ||
| 12 | |||
| 13 | |||
| 14 | |||
| 15 | |||
| 16 | |||
| 17 | #### | ||
| 18 | # initializes the object with table information | ||
| 19 | #################### | ||
| 20 | sub _init { | ||
| 21 | my ($self, $table) = @_; | ||
| 22 | |||
| 23 | $self->{"_data"} = $self->{"_dbh"}->selectall_arrayref(qq/SHOW COLUMNS FROM $table/); | ||
| 24 | |||
| 25 | unless ( $self->{"_data"} ) { | ||
| 26 | croak "Table '$table' doesn't seem to exist"; | ||
| 27 | } | ||
| 28 | |||
| 29 | $self->{"table_info"} = { }; | ||
| 30 | |||
| 31 | foreach my $row ( @{ $self->{"_data"} } ) { | ||
| 32 | $self->{"table_info"}->{ $row->[0] } = $row; | ||
| 33 | } | ||
| 34 | } | ||
| 35 | |||
| 36 | |||
| 37 | #### | ||
| 38 | # constructor method | ||
| 39 | #################### | ||
| 40 | sub new { | ||
| 41 | my ($class, $dbh, $table) = @_; | ||
| 42 | |||
| 43 | unless ($dbh && $table) { | ||
| 44 | croak <<'END_OF_USAGE'; | ||
| 45 | new() was called with insufficient arguments | ||
| 46 | |||
| 47 | Usage: | ||
| 48 | MySQL::TableInfo->new($dbh, $table) | ||
| 49 | END_OF_USAGE | ||
| 50 | } | ||
| 51 | |||
| 52 | $class = ref($class) || $class; | ||
| 53 | |||
| 54 | my $self = { | ||
| 55 | _dbh => $dbh, | ||
| 56 | _data => [], | ||
| 57 | table_info => {}, | ||
| 58 | }; | ||
| 59 | |||
| 60 | bless $self => $class; | ||
| 61 | |||
| 62 | $self->_init($table); | ||
| 63 | |||
| 64 | return $self; | ||
| 65 | } | ||
| 66 | |||
| 67 | |||
| 68 | |||
| 69 | |||
| 70 | |||
| 71 | #### | ||
| 72 | # desctructor method | ||
| 73 | #################### | ||
| 74 | sub DESTROY { } | ||
| 75 | |||
| 76 | |||
| 77 | |||
| 78 | |||
| 79 | |||
| 80 | |||
| 81 | #### | ||
| 82 | # gets the column name or names | ||
| 83 | #################### | ||
| 84 | sub column { | ||
| 85 | my ($self, $column) = @_; | ||
| 86 | |||
| 87 | if ($column) { | ||
| 88 | return @{$self->{table_info}{$column}}; | ||
| 89 | } | ||
| 90 | |||
| 91 | return keys %{$self->{table_info}}; | ||
| 92 | } | ||
| 93 | |||
| 94 | |||
| 95 | |||
| 96 | #### | ||
| 97 | # gets the size of the column | ||
| 98 | #################### | ||
| 99 | sub size { | ||
| 100 | my ($self, $col) = @_; | ||
| 101 | |||
| 102 | if ( $self->{table_info}{$col}[1] =~ m/\((\d+)\)/ ) { | ||
| 103 | return $1; | ||
| 104 | } | ||
| 105 | |||
| 106 | return undef; | ||
| 107 | } | ||
| 108 | |||
| 109 | |||
| 110 | |||
| 111 | |||
| 112 | |||
| 113 | #### | ||
| 114 | # gets the type of the column | ||
| 115 | #################### | ||
| 116 | sub type { | ||
| 117 | my ($self, $col) = @_; | ||
| 118 | |||
| 119 | if ($self->{table_info}{$col}[1] =~ m/(\w+)/) { | ||
| 120 | return $1; | ||
| 121 | } | ||
| 122 | |||
| 123 | return undef; | ||
| 124 | } | ||
| 125 | |||
| 126 | |||
| 127 | |||
| 128 | #### | ||
| 129 | # gets the default values of the column | ||
| 130 | #################### | ||
| 131 | sub default { | ||
| 132 | my ($self, $col) = @_; | ||
| 133 | |||
| 134 | return $self->{table_info}->{$col}[4]; | ||
| 135 | |||
| 136 | } | ||
| 137 | |||
| 138 | |||
| 139 | |||
| 140 | #### | ||
| 141 | # gets the extra information about the column | ||
| 142 | #################### | ||
| 143 | sub extra { | ||
| 144 | my ($self, $col) = @_; | ||
| 145 | |||
| 146 | if ($col) { | ||
| 147 | return $self->{table_info}{$col}[5]; | ||
| 148 | } | ||
| 149 | |||
| 150 | return undef; | ||
| 151 | } | ||
| 152 | |||
| 153 | |||
| 154 | |||
| 155 | |||
| 156 | #### | ||
| 157 | # determines if the column can hold NULL | ||
| 158 | #################### | ||
| 159 | sub is_null { | ||
| 160 | my ($self, $col) = @_; | ||
| 161 | |||
| 162 | if ( $self->{table_info}{$col}[2] =~ /^yes$/i ) { | ||
| 163 | return 1; | ||
| 164 | } | ||
| 165 | |||
| 166 | return undef; | ||
| 167 | } | ||
| 168 | |||
| 169 | |||
| 170 | |||
| 171 | #### | ||
| 172 | # gets the values of the set column | ||
| 173 | #################### | ||
| 174 | sub set { | ||
| 175 | my ($self, $col) = @_; | ||
| 176 | my @set; | ||
| 177 | |||
| 178 | if ( $self->{table_info}{$col}[1] =~ m/\((.+?)\)/ ) { | ||
| 179 | |||
| 180 | @set = split(",", $1); | ||
| 181 | map {s/^'(.+)'$/$1/} @set; | ||
| 182 | |||
| 183 | return @set; | ||
| 184 | } | ||
| 185 | |||
| 186 | } | ||
| 187 | |||
| 188 | |||
| 189 | |||
| 190 | |||
| 191 | #### | ||
| 192 | # gest the values of the enumeration column; uses set() | ||
| 193 | #################### | ||
| 194 | sub enum { | ||
| 195 | my ($self, $col) = @_; | ||
| 196 | |||
| 197 | return $self->set($col); | ||
| 198 | } | ||
| 199 | |||
| 200 | |||
| 201 | |||
| 202 | |||
| 203 | #### | ||
| 204 | # initializes default vlues into a CGI object | ||
| 205 | #################### | ||
| 206 | sub load_default { | ||
| 207 | my ($self, $CGI) = @_; | ||
| 208 | |||
| 209 | unless ($CGI) { | ||
| 210 | croak "load_default() needs a CGI object to be passed as an argument"; | ||
| 211 | } | ||
| 212 | |||
| 213 | foreach my $col ( $self->column ) { | ||
| 214 | $CGI->param(-name=>$col, -value=>$self->default($col) ); | ||
| 215 | } | ||
| 216 | |||
| 217 | } | ||
| 218 | |||
| 219 | |||
| 220 | |||
| 221 | #### | ||
| 222 | # checks if the CGI parameters' values are valid | ||
| 223 | # according to their corresponding mysql-table-columns | ||
| 224 | ##---------- Note:-- | ||
| 225 | # Still not working properly. I can feel the need for couple of private methods | ||
| 226 | # to make this validate() thing possible | ||
| 227 | #################### | ||
| 228 | sub validate { | ||
| 229 | my ($self, $CGI) = @_; | ||
| 230 | |||
| 231 | unless ($CGI) { | ||
| 232 | croak "validate() needs a CGI object to be passed as an argument"; | ||
| 233 | } | ||
| 234 | |||
| 235 | my %error; | ||
| 236 | |||
| 237 | foreach my $col ($self->column) { | ||
| 238 | |||
| 239 | next unless $CGI->param($col); | ||
| 240 | |||
| 241 | if ( $self->type($col) =~ m/^int$/i) { | ||
| 242 | $error{$col}.= " Not an integer," unless $CGI->param($col) =~ m/^\d+$/; | ||
| 243 | } | ||
| 244 | |||
| 245 | if ( $self->size($col) && ( length($CGI->param($col) ) > $self->size($col) ) ) { | ||
| 246 | $error{$col}.= " Longer than expected,"; | ||
| 247 | } | ||
| 248 | |||
| 249 | if ( $self->type($col) =~ m/^enum$/i) { | ||
| 250 | my $exists; | ||
| 251 | foreach my $enum ( $self->enum($col) ) { | ||
| 252 | if ($CGI->param($col) =~ m/$enum/i) { | ||
| 253 | $exists = 1; | ||
| 254 | last; | ||
| 255 | } | ||
| 256 | } | ||
| 257 | |||
| 258 | $error{$col}.= $CGI->param($col). " is not a supported option" unless $exists; | ||
| 259 | } | ||
| 260 | |||
| 261 | } | ||
| 262 | |||
| 263 | return %error; | ||
| 264 | } | ||
| 265 | |||
| 266 | #--- | ||
| 267 | # checks if the parameter is a valid enum element | ||
| 268 | ##--Note:----------- | ||
| 269 | # currently not implemented | ||
| 270 | #------------------- | ||
| 271 | sub _valid_enum { | ||
| 272 | my ($self, $param) = @_; | ||
| 273 | |||
| 274 | return 1; | ||
| 275 | } | ||
| 276 | |||
| 277 | #--- | ||
| 278 | # checks if the parameter is a valid set element | ||
| 279 | ##---Note:---------- | ||
| 280 | # currently not implemented | ||
| 281 | #------------------- | ||
| 282 | sub _valid_set { | ||
| 283 | my ($self, $param) = @_; | ||
| 284 | |||
| 285 | return 1; | ||
| 286 | } | ||
| 287 | |||
| 288 | |||
| 289 | #--- | ||
| 290 | # dumps the object into __PACKAGE__.dmp file | ||
| 291 | # for debugging purposes | ||
| 292 | #------------------- | ||
| 293 | sub _dump { | ||
| 294 | my $self = shift; | ||
| 295 | |||
| 296 | require Data::Dumper; | ||
| 297 | |||
| 298 | open DATA, ">".__PACKAGE__.".dmp" or die "Couldn't dump: $!\n"; | ||
| 299 | print DATA Dumper($self); | ||
| 300 | close DATA; | ||
| 301 | |||
| 302 | } | ||
| 303 | |||
| 304 | |||
| 305 | |||
| 306 | 1; | ||
| 307 | |||
| 308 | __END__ |