Merge branch 'master' into topic/constructor_rewrite
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / CDBICompat / Retrieve.pm
CommitLineData
c0e7b4e5 1package # hide from PAUSE
2 DBIx::Class::CDBICompat::Retrieve;
656796f2 3
4use strict;
5use warnings FATAL => 'all';
6
716b3d29 7
9387c904 8sub retrieve {
9 my $self = shift;
10 die "No args to retrieve" unless @_ > 0;
11
12 my @cols = $self->primary_columns;
13
14 my $query;
15 if (ref $_[0] eq 'HASH') {
16 $query = { %{$_[0]} };
17 }
18 elsif (@_ == @cols) {
19 $query = {};
20 @{$query}{@cols} = @_;
21 }
22 else {
23 $query = {@_};
24 }
25
26 $query = $self->_build_query($query);
27 $self->find($query);
28}
29
30sub find_or_create {
31 my $self = shift;
32 my $query = ref $_[0] eq 'HASH' ? shift : {@_};
33
34 $query = $self->_build_query($query);
35 $self->next::method($query);
36}
37
38# _build_query
39#
40# Build a query hash. Defaults to a no-op; ColumnCase overrides.
41
42sub _build_query {
43 my ($self, $query) = @_;
44
45 return $query;
716b3d29 46}
6009260a 47
48sub retrieve_from_sql {
49 my ($class, $cond, @rest) = @_;
e60dc79f 50
6009260a 51 $cond =~ s/^\s*WHERE//i;
e60dc79f 52
7210819b 53 # Need to parse the SQL clauses after WHERE in reverse
54 # order of appearance.
55
56 my %attrs;
57
58 if( $cond =~ s/\bLIMIT\s+(\d+)\s*$//i ) {
59 $attrs{rows} = $1;
60 }
61
62 if ( $cond =~ s/\bORDER\s+BY\s+(.*)\s*$//i ) {
63 $attrs{order_by} = $1;
e60dc79f 64 }
65
7210819b 66 if( $cond =~ s/\bGROUP\s+BY\s+(.*)\s*$//i ) {
67 $attrs{group_by} = $1;
e2bdf485 68 }
69
869e9aac 70 return $class->search_literal($cond, @rest, ( %attrs ? \%attrs : () ) );
e60dc79f 71}
72
73sub construct {
74 my $class = shift;
75 my $obj = $class->resultset_instance->new_result(@_);
76 $obj->in_storage(1);
d4daee7b 77
e60dc79f 78 return $obj;
6009260a 79}
656796f2 80
716b3d29 81sub retrieve_all { shift->search }
3125eb1f 82sub count_all { shift->count }
e60dc79f 83
84sub maximum_value_of {
85 my($class, $col) = @_;
86 return $class->resultset_instance->get_column($col)->max;
87}
88
89sub minimum_value_of {
90 my($class, $col) = @_;
91 return $class->resultset_instance->get_column($col)->min;
92}
cba994a1 93
656796f2 941;