| 1 | # | ||
| 2 | # DESCRIPTION | ||
| 3 | # PerlORM - Object relational mapper (ORM) for Perl. PerlORM is Perl | ||
| 4 | # library that implements object-relational mapping. Its features are | ||
| 5 | # much similar to those of Java's Hibernate library, but interface is | ||
| 6 | # much different and easier to use. | ||
| 7 | # | ||
| 8 | # AUTHOR | ||
| 9 | # Alexey V. Akimov <akimov_alexey@sourceforge.net> | ||
| 10 | # | ||
| 11 | # COPYRIGHT | ||
| 12 | # Copyright (C) 2005-2006 Alexey V. Akimov | ||
| 13 | # | ||
| 14 | # This library is free software; you can redistribute it and/or | ||
| 15 | # modify it under the terms of the GNU Lesser General Public | ||
| 16 | # License as published by the Free Software Foundation; either | ||
| 17 | # version 2.1 of the License, or (at your option) any later version. | ||
| 18 | # | ||
| 19 | # This library is distributed in the hope that it will be useful, | ||
| 20 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 21 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 22 | # Lesser General Public License for more details. | ||
| 23 | # | ||
| 24 | # You should have received a copy of the GNU Lesser General Public | ||
| 25 | # License along with this library; if not, write to the Free Software | ||
| 26 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 27 | # | ||
| 28 | |||
| 29 | package ORM::Stat; | ||
| 30 | |||
| 31 | use ORM::StatMetaprop; | ||
| 32 | use ORM::Error; | ||
| 33 | |||
| 34 | $VERSION = 0.81; | ||
| 35 | |||
| 36 | sub new | ||
| 37 | { | ||
| 38 | my $class = shift; | ||
| 39 | my %arg = @_; | ||
| 40 | my $self; | ||
| 41 | |||
| 42 | $self = | ||
| 43 | { | ||
| 44 | class => $arg{class}, | ||
| 45 | data => $arg{data}, | ||
| 46 | filter => $arg{filter}, | ||
| 47 | group_by => $arg{group_by}, | ||
| 48 | default_order => $arg{default_order}, | ||
| 49 | }; | ||
| 50 | |||
| 51 | bless $self, $class; | ||
| 52 | } | ||
| 53 | |||
| 54 | sub find | ||
| 55 | { | ||
| 56 | my $class = shift; | ||
| 57 | my %arg = @_; | ||
| 58 | my $error = ORM::Error->new; | ||
| 59 | |||
| 60 | my $obj = $class->stat_class->stat | ||
| 61 | ( | ||
| 62 | data => $class->data, | ||
| 63 | preload => $class->preload, | ||
| 64 | filter => ( $class->filter & $arg{pre_filter} ), | ||
| 65 | group_by => $class->group_by, | ||
| 66 | |||
| 67 | post_filter => ( $class->post_filter & $arg{filter} ), | ||
| 68 | order => ($arg{order}||$class->default_order), | ||
| 69 | page => $arg{page}, | ||
| 70 | pagesize => $arg{pagesize}, | ||
| 71 | error => $error, | ||
| 72 | debug => $arg{debug}, | ||
| 73 | ); | ||
| 74 | |||
| 75 | unless( $error->fatal ) | ||
| 76 | { | ||
| 77 | for( my $i=$#$obj; $i>=0; $i-- ) | ||
| 78 | { | ||
| 79 | bless $obj->[$i], $class; | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | $error->upto( $arg{error} ); | ||
| 84 | return $error->fatal ? undef : (wantarray ? @$obj : $obj); | ||
| 85 | } | ||
| 86 | |||
| 87 | sub count | ||
| 88 | { | ||
| 89 | my $class = shift; | ||
| 90 | my %arg = @_; | ||
| 91 | my $error = ORM::Error->new; | ||
| 92 | my $count; | ||
| 93 | |||
| 94 | my $count = $class->stat_class->stat | ||
| 95 | ( | ||
| 96 | data => $class->data, | ||
| 97 | filter => ( $class->filter & $arg{pre_filter} ), | ||
| 98 | group_by => $class->group_by, | ||
| 99 | |||
| 100 | post_filter => ( $class->post_filter & $arg{filter} ), | ||
| 101 | count => 1, | ||
| 102 | error => $error, | ||
| 103 | debug => $arg{debug}, | ||
| 104 | ); | ||
| 105 | |||
| 106 | $error->upto( $arg{error} ); | ||
| 107 | return $count; | ||
| 108 | } | ||
| 109 | |||
| 110 | sub preload { undef; } | ||
| 111 | |||
| 112 | sub _all_props | ||
| 113 | { | ||
| 114 | my $class = shift; | ||
| 115 | keys %{$class->data}; | ||
| 116 | } | ||
| 117 | |||
| 118 | sub _property | ||
| 119 | { | ||
| 120 | my $self = shift; | ||
| 121 | my $prop = shift; | ||
| 122 | |||
| 123 | $self->{$prop}; | ||
| 124 | } | ||
| 125 | |||
| 126 | sub _property_id | ||
| 127 | { | ||
| 128 | my $self = shift; | ||
| 129 | my $prop = shift; | ||
| 130 | |||
| 131 | ref $self->{$prop} ? $self->{$prop}->__ORM_db_value : $self->{$prop}; | ||
| 132 | } | ||
| 133 | |||
| 134 | sub M | ||
| 135 | { | ||
| 136 | my $class = shift; | ||
| 137 | ORM::StatMetaprop->new( stat_class=>$class ); | ||
| 138 | } | ||
| 139 | |||
| 140 | sub AUTOLOAD | ||
| 141 | { | ||
| 142 | if( $AUTOLOAD =~ /^(.+)::(.+)$/ ) | ||
| 143 | { | ||
| 144 | my $prop = $2; | ||
| 145 | my $self = shift; | ||
| 146 | |||
| 147 | $self->_property( $prop ); | ||
| 148 | } | ||
| 149 | } | ||
| 150 | |||
| 151 | sub DESTROY | ||
| 152 | { | ||
| 153 | } | ||
| 154 | |||
| 155 | 1; |