package Maypole::Model::CDBI;
use strict;
+use Data::Dumper;
+
=head1 NAME
Maypole::Model::CDBI - Model class based on Class::DBI
# sub _column_info {
sub _column_info {
my $self = shift;
- warn "__column_info called by ", join (', ', caller), "\n";
my $dbh = $self->db_Main;
my $meta; # The info we are after
# required / nullable
$meta->{$colname}{nullable} = $row->{NULLABLE};
-
- $meta->{$colname}{required} = ($row->{NULLABLE} && $row->{NULLABLE} == 0) ? 1 : 0;
+ $meta->{$colname}{required} = ( $meta->{$colname}{nullable} == 0 ) ? 1 : 0;
# default
if (defined $row->{COLUMN_DEF}) {
my ($class, $columns) = @_;
$class->_column_info() unless ref $class->COLUMN_INFO;
my $column_info = $class->COLUMN_INFO;
+
if ($columns) {
foreach my $colname ( @$columns ) {
if ($class->_isa_class($colname)) {
warn "$colname is not a recognised column in this class ", ref $class || $class, "\n";
next;
}
- $column_info->{required} = 1;
+ $column_info->{$colname}{required} = 1;
}
$class->COLUMN_INFO($column_info);
}
- return [ grep ($column_info->{$_}{required}, keys %{$column_info}) ] ;
+ return [ grep ($column_info->{$_}{required}, keys %$column_info) ] ;
}
=head2 column_required
use Data::Dumper;
use lib 'ex'; # Where BeerDB should live
BEGIN {
- plan tests => 35;
+ plan tests => 65;
}
$db = 'test';
sub run_method_tests {
($class, $method, %correct) = @_;
for $col (sort keys %correct) {
-# warn "class : $class\n";
-# warn "ISA : ", join(', ',@BeerDB::BeerTestmysql::ISA);
+
$val = $class->$method($col);
# Hacks for various val types
run_method_tests($DB_Class,'column_type', %correct_types);
run_method_tests($DB_Class,'column_default', %correct_defaults);
run_method_tests($DB_Class,'column_nullable', %correct_nullables);
+
+
+ foreach my $colname ( @{$DB_Class->required_columns()} ) {
+ ok($correct_nullables{$colname} == 0,"nullable column $colname is required (via required_columns)");
+ }
+
+ foreach my $colname (keys %correct_nullables) {
+ ok( $DB_Class->column_required($colname) == !$correct_nullables{$colname}, "nullable column $colname is required (via column_required)" )
+ }
+
+ ok($DB_Class->required_columns([qw/score/]), 'set required column(s)');
+
+ foreach my $colname ( @{$DB_Class->required_columns()} ) {
+ ok($correct_nullables{$colname} == 0 || $colname eq 'score',"nullable or required column $colname is required (via required_columns)" );
+ }
+
+ foreach my $colname (keys %correct_nullables) {
+ if ($colname eq 'score') {
+ ok( $DB_Class->column_required($colname) == 0, "nullable column $colname is required (via column_required)");
+ } else {
+ ok( $DB_Class->column_required($colname) == !$correct_nullables{$colname}, "nullable column $colname is required (via column_required)");
+ }
+ }
};
# SQLite test
# I think sqlite driver allows everything to be nullable.
#run_method_tests($DB_Class,'column_nullable', %correct_nullables);
+ ok($DB_Class->required_columns([qw/score style name tasted/]), 'set required column(s)');
+
+
+ foreach my $colname ( @{$DB_Class->required_columns()} ) {
+ ok($correct_nullables{$colname} == 0 || $colname eq 'score',"nullable or required column $colname is required (via required_columns)" );
+ }
+
+ foreach my $colname (keys %correct_nullables) {
+ if ($colname eq 'score') {
+ ok( $DB_Class->column_required($colname) == 0, "nullable column $colname is required (via column_required)");
+ } else {
+ ok( $DB_Class->column_required($colname) == !$correct_nullables{$colname}, "nullable column $colname is required (via column_required)");
+ }
+ }
+
};