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);
68 my $rs_class = $class->_resultset_class;
69 eval "use $rs_class;";
70 my $rs = $rs_class->new($class, @_);
75 Identical to search except defaults to 'LIKE' instead of '=' in condition
82 if (@_ > 1 && ref $_[$#_] eq 'HASH') {
85 my $query = ref $_[0] eq "HASH" ? { %{shift()} }: {@_};
86 $query->{$_} = { 'like' => $query->{$_} } for keys %$query;
87 return $class->search($query, { %$attrs });
91 return keys %{$_[0]->_columns};
96 __PACKAGE__->table('tbl_name');
101 shift->_table_name(@_);
106 $class->find_or_create({ key => $val, ... });
108 Searches for a record matching the search condition; if it doesn't find one,
109 creates one and returns that instead
115 my $hash = ref $_[0] eq "HASH" ? shift: {@_};
116 my $exists = $class->find($hash);
117 return defined($exists) ? $exists : $class->create($hash);
122 if ($obj->has_column($col)) { ... }
124 Returns 1 if the object has a column of this name, 0 otherwise
129 my ($self, $column) = @_;
130 return exists $self->_columns->{$column};
135 my $info = $obj->column_info($col);
137 Returns the column metadata hashref for the column
142 my ($self, $column) = @_;
143 die "No such column $column" unless exists $self->_columns->{$column};
144 return $self->_columns->{$column};
149 my @column_names = $obj->columns;
153 sub columns { return keys %{shift->_columns}; }
161 Matt S. Trout <mst@shadowcatsystems.co.uk>
165 You may distribute this code under the same terms as Perl itself.