1 package DBIx::Class::Table;
6 use DBIx::Class::ResultSet;
8 use base qw/DBIx::Class/;
10 __PACKAGE__->mk_classdata('_columns' => {});
12 __PACKAGE__->mk_classdata('_table_name');
14 __PACKAGE__->mk_classdata('table_alias'); # FIXME: Doesn't actually do anything yet!
16 __PACKAGE__->mk_classdata('_resultset_class' => 'DBIx::Class::ResultSet');
18 sub iterator_class { shift->_resultset_class(@_) }
22 DBIx::Class::Table - Basic table methods
28 This class is responsible for defining and doing basic operations on
29 L<DBIx::Class> objects.
37 sub _register_columns {
38 my ($class, @cols) = @_;
39 my $names = { %{$class->_columns} };
40 while (my $col = shift @cols) {
41 $names->{$col} = (ref $cols[0] ? shift : {});
43 $class->_columns($names);
46 sub _mk_column_accessors {
47 my ($class, @cols) = @_;
48 $class->mk_group_accessors('column' => @cols);
53 __PACKAGE__->add_columns(qw/col1 col2 col3/);
55 Adds columns to the current package, and creates accessors for them
60 my ($class, @cols) = @_;
61 $class->_register_columns(@cols);
62 $class->_mk_column_accessors(@cols);
65 sub resultset_instance {
67 $class->next::method($class->construct_resultset);
70 sub construct_resultset {
72 my $rs_class = $class->_resultset_class;
73 eval "use $rs_class;";
74 return $rs_class->new($class);
79 Identical to search except defaults to 'LIKE' instead of '=' in condition
86 if (@_ > 1 && ref $_[$#_] eq 'HASH') {
89 my $query = ref $_[0] eq "HASH" ? { %{shift()} }: {@_};
90 $query->{$_} = { 'like' => $query->{$_} } for keys %$query;
91 return $class->search($query, { %$attrs });
95 return keys %{$_[0]->_columns};
100 __PACKAGE__->table('tbl_name');
105 shift->_table_name(@_);
110 $class->find_or_create({ key => $val, ... });
112 Searches for a record matching the search condition; if it doesn't find one,
113 creates one and returns that instead
119 my $hash = ref $_[0] eq "HASH" ? shift: {@_};
120 my $exists = $class->find($hash);
121 return defined($exists) ? $exists : $class->create($hash);
126 if ($obj->has_column($col)) { ... }
128 Returns 1 if the object has a column of this name, 0 otherwise
133 my ($self, $column) = @_;
134 return exists $self->_columns->{$column};
139 my $info = $obj->column_info($col);
141 Returns the column metadata hashref for the column
146 my ($self, $column) = @_;
147 die "No such column $column" unless exists $self->_columns->{$column};
148 return $self->_columns->{$column};
153 my @column_names = $obj->columns;
158 die "columns() is a read-only accessor, did you mean add_columns()?" if (@_ > 1);
159 return keys %{shift->_columns};
168 Matt S. Trout <mst@shadowcatsystems.co.uk>
172 You may distribute this code under the same terms as Perl itself.