-=head2 fetch_objects
-
-Returns 1 or more objects of the given class when provided with the request
-
-=cut
-
-sub fetch_objects {
- my ($class, $r)=@_;
- my @pcs = $class->primary_columns;
- if ( $#pcs ) {
- my %pks;
- @pks{@pcs}=(@{$r->{args}});
- return $class->retrieve( %pks );
- }
- return $class->retrieve( $r->{args}->[0] );
-}
-
-
-
-
-
-=head2 _isa_class
-
-Private method to return the class a column
-belongs to that was inherited by an is_a relationship.
-This should probably be public but need to think of API
-
-=cut
-
-sub _isa_class {
- my ($class, $col) = @_;
- $class->_croak( "Need a column for _isa_class." ) unless $col;
- my $isaclass;
- my $isa = $class->meta_info("is_a") || {};
- foreach ( keys %$isa ) {
- $isaclass = $isa->{$_}->foreign_class;
- return $isaclass if ($isaclass->find_column($col));
- }
- return; # col not in a is_a class
-}
-
-
-# Thanks to dave baird -- form builder for these private functions
-# 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
- my ($catalog, $schema) = (undef, undef);
- # Dave is suspicious this (above undefs) could
- # break things if driver useses this info
-
- my $original_metadata;
- # '%' is a search pattern for columns - matches all columns
- if ( my $sth = $dbh->column_info( $catalog, $schema, $self->table, '%' ) ) {
- $dbh->errstr && die "Error getting column info sth: " . $dbh->errstr;
- $self->COLUMN_INFO ($self->_hash_type_meta( $sth ));
- } else {
- $self->COLUMN_INFO ($self->_hash_typeless_meta( ));
- }
-
- return $self->COLUMN_INFO;
-}
-
-sub _hash_type_meta {
- my ($self, $sth) = @_;
- my $meta;
- while ( my $row = $sth->fetchrow_hashref ) {
- my $colname = $row->{COLUMN_NAME} || $row->{column_name};
-
- # required / nullable
- $meta->{$colname}{nullable} = $row->{NULLABLE};
-
- $meta->{$colname}{required} = ($row->{NULLABLE} && $row->{NULLABLE} == 0) ? 1 : 0;
-
- # default
- if (defined $row->{COLUMN_DEF}) {
- my $default = $row->{COLUMN_DEF};
- $default =~ s/['"]?(.*?)['"]?::.*$/$1/;
- $meta->{$colname}{default} = $default;
- }else {
- $meta->{$colname}{default} = '';
- }
-
- # type
- my $type = $row->{mysql_type_name} || $row->{type};
- unless ($type) {
- $type = $row->{TYPE_NAME};
- if ($row->{COLUMN_SIZE}) {
- $type .= "($row->{COLUMN_SIZE})";
- }
- }
- $type =~ s/['"]?(.*)['"]?::.*$/$1/;
- # Bool if tinyint
- if ($type and $type =~ /^tinyint/i and $row->{COLUMN_SIZE} == 1) {
- $type = 'BOOL';
- }
- $meta->{$colname}{type} = $type;
-
- # order
- $meta->{$colname}{position} = $row->{ORDINAL_POSITION}
- }
- return $meta;
-}
-
-# typeless db e.g. sqlite
-sub _hash_typeless_meta {
- my ( $self ) = @_;
-
- $self->set_sql( fb_meta_dummy => 'SELECT * FROM __TABLE__ WHERE 1=0' )
- unless $self->can( 'sql_fb_meta_dummy' );
-
- my $sth = $self->sql_fb_meta_dummy;
-
- $sth->execute or die "Error executing column info: " . $sth->errstr;;
-
- # see 'Statement Handle Attributes' in the DBI docs for a list of available attributes
- my $cols = $sth->{NAME};
- my $types = $sth->{TYPE};
- # my $sizes = $sth->{PRECISION}; # empty
- # my $nulls = $sth->{NULLABLE}; # empty
-
- # we haven't actually fetched anything from the sth, so need to tell DBI we're not going to
- $sth->finish;
-
- my $order = 0;
- my $meta;
- foreach my $col ( @$cols ) {
- my $col_meta;
- $col_meta->{nullable} = 1;
- $col_meta->{required} = 0;
- $col_meta->{default} = '';
- $col_meta->{position} = $order++;
- # type_name is taken literally from the schema, but is not actually used by sqlite,
- # so it can be anything, e.g. varchar or varchar(xxx) or VARCHAR etc.
- my $type = shift( @$types );
- $col_meta->{type} = ($type =~ /(\w+)\((\w+)\)/) ? $1 :$type ;
- $meta->{$col} = $col_meta;
- }
- return $meta;
-}
-
-=head2 column_type
-
- my $type = $class->column_type('column_name');
-
-This returns the 'type' of this column (VARCHAR(20), BIGINT, etc.)
-For now, it returns "BOOL" for tinyints.
-
-TODO :: TEST with enums
-
-=cut
-
-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.