SQL::DB::Schema::Column - description


SQL-DB documentation Contained in the SQL-DB distribution.

Index


Code Index:

NAME

Top

SQL::DB::Schema::Column - description

SYNOPSIS

Top

  use SQL::DB::Schema::Column;

DESCRIPTION

Top

SQL::DB::Schema::Column is ...

METHODS

Top

table

name

null

default

unique

auto_increment

primary

type =head2 type_Pg =head2 type_mysql

bind_type =head2 bind_type_Pg =head2 bind_type_mysql

ref

references

deferrable

set

Takes an object method (subroutine reference) which is run when SQL::DB::Row->set_column($val) is called. The subref has access to the row object and all of its columns, but must return the value for the column and not set it.

inflate

deflate

sql_default

sql

FILES

Top

SEE ALSO

Top

Other

AUTHOR

Top

Mark Lawrence <nomad@null.net>

COPYRIGHT AND LICENSE

Top


SQL-DB documentation Contained in the SQL-DB distribution.

package SQL::DB::Schema::Column;
use strict;
use warnings;
use base qw(Class::Accessor::Fast);
use Carp qw(carp croak);
use Scalar::Util qw(weaken);

SQL::DB::Schema::Column->mk_accessors(qw(
    table
    name
    null
    default
    unique
    auto_increment
));


our $DEBUG;


sub table {
    my $self = shift;
    if ( @_ ) {
        my $table = shift;
        unless(CORE::ref($table) and CORE::ref($table) eq 'SQL::DB::Schema::Table') {
            croak "table must be a SQL::DB::Schema::Table";
        }
        $self->{table} = $table;
        weaken($self->{table});
    }
    return $self->{table};
}


sub primary {
    my $self = shift;
    if (@_) {
        if ($self->{primary} = shift) {
            $self->{table}->add_primary($self);
        }
    }
    else {
        return $self->{primary};
    }
}


sub type {
    my $self = shift;
    if (@_) {
        $self->{type} = shift;
        return;
    }
    if (!$self->{table}) {
        return $self->{type};
    }

    my $type = 'type_' . $self->{table}->db_type;
    if (exists($self->{$type}) and $self->{$type}) {
        return $self->{$type};
    }
    return $self->{type};
}


sub type_Pg {
    my $self = shift;
    if (@_) {
        $self->{type_Pg} = shift;
        return;
    }
    croak 'usage: type_Pg($type)';
}


sub type_mysql {
    my $self = shift;
    if (@_) {
        $self->{type_mysql} = shift;
        return;
    }
    croak 'usage: type_mysql($type)';
}


sub bind_type {
    my $self = shift;
    if (@_) {
        $self->{bind_type} = shift;
        return;
    }
    if (!$self->{table}) {
        return $self->{bind_type};
    }

    my $type = 'bind_type_' . $self->{table}->db_type;
    if (exists($self->{$type}) and $self->{$type}) {
        return $self->{$type};
    }
    return $self->{bind_type};
}


sub bind_type_Pg {
    my $self = shift;
    if (@_) {
        $self->{bind_type_Pg} = shift;
        return;
    }
    croak 'usage: bind_type_Pg($type)';
}


sub bind_type_mysql {
    my $self = shift;
    if (@_) {
        $self->{bind_type_mysql} = shift;
        return;
    }
    croak 'usage: bind_type_mysql($type)';
}


#
# This is a delayed value function. Takes a string, but first time
# is accessed it finds the real column and sets itself to that column.
#
sub ref {references(@_);};
sub references {
    my $self = shift;
    # Set a value
    if (@_) {
        $self->{references} = shift;
        return;
    }

    # Not set
    if (!$self->{references}) {
        return;
    }

    # Already accessed - return the reference to SQL::DB::Schema::Column
    if (CORE::ref($self->{references})) {
        return $self->{references};
    }

    # Not yet accessed - find the reference to SQL::DB::Schema::Column
    my @cols = $self->table->text2cols($self->{references});
    $self->{references} = $cols[0];
    weaken($self->{references});
    $cols[0]->table->ref_by($self->table);
    return $self->{references};
}


sub deferrable {
    my $self = shift;

    if (@_) {
        $self->{deferrable} = uc(shift);
    }
    return $self->{deferrable} if(exists($self->{deferrable}));
    return;
}


sub set {
    my $self = shift;

    if (@_) {
        my $sub = shift;
        (CORE::ref($sub) && CORE::ref($sub) eq 'CODE') ||
            croak 'set requires a CODEREF argument';
        $self->{set} = $sub;
    }

    return $self->{set};
}


sub inflate {
    my $self = shift;

    if (@_) {
        my $sub  = shift || croak 'inflate requires a CODEREF argument';
        (CORE::ref($sub) && CORE::ref($sub) eq 'CODE') ||
            croak 'inflate requires a CODEREF argument';
        $self->{inflate} = $sub;
    }
    return $self->{inflate};
}


sub deflate {
    my $self = shift;

    if (@_) {
        my $sub  = shift || croak 'deflate requires a CODEREF argument';
        (CORE::ref($sub) && CORE::ref($sub) eq 'CODE') ||
            croak 'deflate requires a CODEREF argument';
        $self->{deflate} = $sub;
    }
    return $self->{deflate};
}


sub sql_default {
    my $self = shift;
    my $default = $self->default;
    if (!defined($default)) {
        return '';
    }
    if (CORE::ref($default) and CORE::ref($default) eq 'CODE') {
        return '';
    }

    if ($self->type =~ m/(int)|(real)|(float)|(double)|(numeric)/i) {
        return ' DEFAULT ' . $default
    }
    return " DEFAULT '" . $default ."'";
}


sub sql {
    my $self = shift;
    my $def = '';
    if (exists($self->{deferrable})) {
        if ($self->{deferrable}) {
            $def = ' DEFERRABLE '.$self->{deferrable};
        }
        else {
            $def = ' NOT DEFERRABLE';
        }
    }

    return sprintf('%-15s %-15s', $self->name, $self->type)
           . ($self->null ? 'NULL' : 'NOT NULL')
           . $self->sql_default
           . ($self->auto_increment ? ' AUTO_INCREMENT' : '')
           . ($self->unique ? ' UNIQUE' : '')
#           . ($self->primary ? ' PRIMARY KEY' : '')
           . ($self->references ? 
                (' REFERENCES ' . $self->references->table->name .'('
                 . $self->references->name .')'. $def  
                ) : ''
             )
    ;
}


DESTROY {
    my $self = shift;
    warn "DESTROY $self" if($DEBUG);
}

1;
__END__
# vim: set tabstop=4 expandtab:


# vim: set tabstop=4 expandtab: