__PACKAGE__->load_components(qw/AccessorGroup/);
__PACKAGE__->mk_group_accessors('simple' =>
- qw/_columns name resultset_class result_class storage/);
+ qw/_columns _primaries name resultset_class result_class storage/);
=head1 NAME
sub add_columns {
my ($self, @cols) = @_;
while (my $col = shift @cols) {
- $self->add_column($col => (ref $cols[0] ? shift : {}));
+ $self->_columns->{$col} = (ref $cols[0] ? shift : {});
}
}
-sub add_column {
- my ($self, $col, $info) = @_;
- $self->_columns->{$col} = $info || {};
-}
+*add_column = \&add_columns;
=head2 add_columns
Adds columns to the table object. If supplied key => hashref pairs uses
the hashref as the column_info for that column.
+=head2 add_column
+
+ $table->add_column('col' => \%info?);
+
+Convenience alias to add_columns
+
=cut
sub resultset {
my $self = shift;
- my $rs_class = $self->resultset_class;
- eval "use $rs_class;";
- return $rs_class->new($self);
+ return $self->{resultset} ||= $self->resultset_class->new($self);
}
=head2 has_column
return $self->_columns->{$column};
}
-=head2 columns
-
+=head2 columns
+
my @column_names = $obj->columns;
=cut
return keys %{shift->_columns};
}
+=head2 set_primary_key(@cols)
+
+Defines one or more columns as primary key for this table. Should be
+called after C<add_columns>.
+
+=cut
+
+sub set_primary_key {
+ my ($self, @cols) = @_;
+ # check if primary key columns are valid columns
+ for (@cols) {
+ $self->throw("No such column $_ on table ".$self->name)
+ unless $self->has_column($_);
+ }
+ $self->_primaries(\@cols);
+}
+
+=head2 primary_columns
+
+Read-only accessor which returns the list of primary keys.
+
+=cut
+
+sub primary_columns {
+ return @{shift->_primaries||[]};
+}
+
+=head2 from
+
+Returns the FROM entry for the table (i.e. the table name)
+
+=cut
+
+sub from { return shift->name(@_); }
+
+
1;
=head1 AUTHORS