use DBIx::Class::ResultSet;
-use base qw/Class::Data::Inheritable/;
+use base qw/DBIx::Class/;
__PACKAGE__->mk_classdata('_columns' => {});
sub _register_columns {
my ($class, @cols) = @_;
my $names = { %{$class->_columns} };
- $names->{$_} ||= {} for @cols;
+ while (my $col = shift @cols) {
+ $names->{$col} = (ref $cols[0] ? shift : {});
+ }
$class->_columns($names);
}
=item search
- my @obj = $class->search({ foo => 3 });
+ my @obj = $class->search({ foo => 3 }); # "... WHERE foo = 3"
my $cursor = $class->search({ foo => 3 });
-=cut
+To retrieve all rows, simply call C<search()> with no condition parameter,
-sub search {
- my $class = shift;
- #warn "@_";
- my $attrs = { };
- if (@_ > 1 && ref $_[$#_] eq 'HASH') {
- $attrs = { %{ pop(@_) } };
- }
- $attrs->{where} = (@_ == 1 || ref $_[0] eq "HASH" ? shift: {@_});
+ my @all = $class->search(); # equivalent to search({})
- my $rs = $class->resultset($attrs);
+If you need to pass in additional attributes (see
+L<DBIx::Class::ResultSet/Attributes> for details) an empty hash indicates
+no condition,
- return (wantarray ? $rs->all : $rs);
-}
+ my @all = $class->search({}, { cols => [qw/foo bar/] }); # "SELECT foo, bar FROM $class_table"
+
+=cut
sub resultset {
my $class = shift;
sub find_or_create {
my $class = shift;
my $hash = ref $_[0] eq "HASH" ? shift: {@_};
- my ($exists) = $class->search($hash);
+ my $exists = $class->find($hash);
return defined($exists) ? $exists : $class->create($hash);
}
+=item has_column
+
+ if ($obj->has_column($col)) { ... }
+
+Returns 1 if the object has a column of this name, 0 otherwise
+
+=cut
+
+sub has_column {
+ my ($self, $column) = @_;
+ return exists $self->_columns->{$column};
+}
+
+=item column_info
+
+ my $info = $obj->column_info($col);
+
+Returns the column metadata hashref for the column
+
+=cut
+
+sub column_info {
+ my ($self, $column) = @_;
+ die "No such column $column" unless exists $self->_columns->{$column};
+ return $self->_columns->{$column};
+}
+
+=item columns
+
+ my @column_names = $obj->columns;
+
+=cut
+
sub columns { return keys %{shift->_columns}; }
1;
=head1 AUTHORS
-Matt S. Trout <perl-stuff@trout.me.uk>
+Matt S. Trout <mst@shadowcatsystems.co.uk>
=head1 LICENSE