-sub column_type {
- my $class = shift;
- my $colname = shift or die "Need a column for column_type";
- $class->_column_info() unless (ref $class->COLUMN_INFO);
-
- if ($class->_isa_class($colname)) {
- return $class->_isa_class($colname)->column_type($colname);
- }
- unless ( $class->find_column($colname) ) {
- warn "$colname is not a recognised column in this class ", ref $class || $class, "\n";
- return undef;
- }
- return $class->COLUMN_INFO->{$colname}{type};
-}
-
-=head2 required_columns
-
- Accessor to get/set required columns for forms, validation, etc.
-
- Returns list of required columns. Accepts an array ref of column names.
-
- $class->required_columns([qw/foo bar baz/]);
-
- Allows you to specify the required columns for a class, over-riding any
- assumptions and guesses made by Maypole.
-
- Use this instead of $config->{$table}{required_cols}
-
- Note : you need to setup the model class before calling this method.
-
-=cut
-
-sub required_columns {
- 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)) {
- $class->_isa_class($colname)->COLUMN_INFO->{$colname}{required} = 1
- unless ($class->_isa_class($colname)->column_required);
- next;
- }
- unless ( $class->find_column($colname) ) {
- warn "$colname is not a recognised column in this class ", ref $class || $class, "\n";
- next;
- }
- $column_info->{$colname}{required} = 1;
- }
- $class->COLUMN_INFO($column_info);
- }
-
- return [ grep ($column_info->{$_}{required}, keys %$column_info) ] ;
-}
-
-=head2 column_required
-
- Returns true if a column is required
-
- my $required = $class->column_required($column_name);
-
- Columns can be required by the application but not the database, but not the other way around,
- hence there is also a column_nullable method which will tell you if the column is nullable
- within the database itself.
-
-=cut
-
-sub column_required {
- my ($class, $colname) = @_;
- $colname or $class->_croak( "Need a column for column_nullable" );
- $class->_column_info() unless ref $class->COLUMN_INFO;
- if ($class->_isa_class($colname)) {
- return $class->_isa_class($colname)->column_required($colname);
- }
- unless ( $class->find_column($colname) ) {
- # handle non-existant columns
- warn "$colname is not a recognised column in this class ", ref $class || $class, "\n";
- return undef;
- }
- return $class->COLUMN_INFO->{$colname}{required} || 0;
-}
-
-=head2 column_nullable
-
- Returns true if a column can be NULL within the underlying database and false if not.
-
- my $nullable = $class->column_nullable($column_name);
-
- Any columns that are not nullable will automatically be specified as required, you can
- also specify nullable columns as required within your application.
-
- It is recomended you use column_required rather than column_nullable within your
- application, this method is more useful if extending the model or handling your own
- validation.
-
-=cut
-
-sub column_nullable {
- my $class = shift;
- my $colname = shift or $class->_croak( "Need a column for column_nullable" );
-
- $class->_column_info() unless ref $class->COLUMN_INFO;
- if ($class->_isa_class($colname)) {
- return $class->_isa_class($colname)->column_nullable($colname);
- }
- unless ( $class->find_column($colname) ) {
- # handle non-existant columns
- warn "$colname is not a recognised column in this class ", ref $class || $class, "\n";
- return undef;
- }
- return $class->COLUMN_INFO->{$colname}{nullable} || 0;
-}
-
-=head2 column_default
-
-Returns default value for column or the empty string.
-Columns with NULL, CURRENT_TIMESTAMP, or Zeros( 0000-00...) for dates and times
-have '' returned.
-
-=cut
-
-sub column_default {
- my $class = shift;
- my $colname = shift or $class->_croak( "Need a column for column_default");
- $class->_column_info() unless (ref $class->COLUMN_INFO);
- if ($class->_isa_class($colname)) {
- return $class->_isa_class($colname)->column_default($colname);
- }
- unless ( $class->find_column($colname) ) {
- warn "$colname is not a recognised column in this class ", ref $class || $class, "\n";
- return undef;
- }
-
- return $class->COLUMN_INFO->{$colname}{default};
-}
-
-=head2 get_classmetadata
-
-Gets class meta data *excluding cgi input* for the passed in class or the
-calling class. *NOTE* excludes cgi inputs. This method is handy to call from
-templates when you need some metadata for a related class.
-
-=cut
-
-sub get_classmetadata {
- my ($self, $class) = @_; # class is class we want data for
- $class ||= $self;
- $class = ref $class || $class;
-
- my %res;
- $res{name} = $class;
- $res{colnames} = {$class->column_names};
- $res{columns} = [$class->display_columns];
- $res{list_columns} = [$class->list_columns];
- $res{moniker} = $class->moniker;
- $res{plural} = $class->plural_moniker;
- $res{table} = $class->table;
- $res{column_metadata} = (ref $class->COLUMN_INFO) ? $class->COLUMN_INFO : $class->_column_info() ;
- return \%res;
-}
-
-