Showing 4 changed files with 66 additions and 14 deletions
+4
Changes
... ...
@@ -1,3 +1,7 @@
1
+0.1718
2
+    - added EXPERIMENTAL DBIx::Custom::Model call_dbi method
3
+    - added EXPERIMENTAL count method
4
+    - added EXPERIMENTAL DBIx::Custom::Model count method
1 5
 0.1717
2 6
     - added EXPERIMENTAL get_table_info
3 7
     - added EXPERIMETNAL user_table_info attribute
+12 -2
lib/DBIx/Custom.pm
... ...
@@ -186,6 +186,8 @@ sub connect {
186 186
     return $self;
187 187
 }
188 188
 
189
+sub count { shift->select(column => 'count(*)', @_)->fetch_first->[0] }
190
+
189 191
 sub dbh {
190 192
     my $self = shift;
191 193
     
... ...
@@ -813,7 +815,7 @@ sub new {
813 815
     # Check attributes
814 816
     my @attrs = keys %$self;
815 817
     foreach my $attr (@attrs) {
816
-        croak qq{"$attr" is wrong name } . _subname
818
+        croak qq{Invalid attribute: "$attr" } . _subname
817 819
           unless $self->can($attr);
818 820
     }
819 821
 
... ...
@@ -2281,7 +2283,15 @@ L<DBIx::Custom> is a wrapper of L<DBI>.
2281 2283
 C<AutoCommit> and C<RaiseError> options are true, 
2282 2284
 and C<PrintError> option is false by default.
2283 2285
 
2284
-=head2 create_model
2286
+=head2 C<count> EXPERIMENTAL
2287
+
2288
+    my $count = $model->count(table => 'book');
2289
+
2290
+Get rows count.
2291
+
2292
+Options is same as C<select> method's ones.
2293
+
2294
+=head2 C<create_model>
2285 2295
 
2286 2296
     my $model = $dbi->create_model(
2287 2297
         table => 'book',
+40 -12
lib/DBIx/Custom/Model.pm
... ...
@@ -39,22 +39,14 @@ sub AUTOLOAD {
39 39
 }
40 40
 
41 41
 my @methods = qw/insert insert_at update update_at update_all
42
-                 delete delete_at delete_all select select_at/;
42
+                 delete delete_at delete_all select select_at count/;
43 43
 foreach my $method (@methods) {
44 44
 
45 45
     my $code = sub {
46 46
         my $self = shift;
47
-
48
-        my @args = (
49
-            table => $self->table,
50
-            bind_type => $self->bind_type,
51
-            primary_key => $self->primary_key,
52
-            type => $self->type, # DEPRECATED!
53
-        );
54
-        push @args, (join => $self->join) if $method =~ /^select/;
55
-        unshift @args, shift if @_ % 2;
56
-        
57
-        $self->dbi->$method(@args, @_);
47
+        my $args = [qw/table bind_type primary_key type/];
48
+        push @$args, 'join' if $method =~ /^select/;
49
+        $self->call_dbi($method, {args => $args}, @_);
58 50
     };
59 51
     
60 52
     no strict 'refs';
... ...
@@ -62,6 +54,19 @@ foreach my $method (@methods) {
62 54
     *{"${class}::$method"} = $code;
63 55
 }
64 56
 
57
+sub call_dbi {
58
+    my $self = shift;
59
+    my $method = shift;
60
+    my $options = shift;
61
+    my $arg_names = $options->{args};
62
+    
63
+    my @args;
64
+    push @args, ($_ => $self->$_) for @$arg_names;
65
+    unshift @args, shift if @_ % 2;
66
+    
67
+    return $self->dbi->$method(@args, @_);
68
+}
69
+
65 70
 sub DESTROY { }
66 71
 
67 72
 sub method {
... ...
@@ -163,6 +168,29 @@ L<DBIx::Custom::Model> inherits all methods from L<Object::Simple>,
163 168
 and you can use all methods of L<DBIx::Custom> and L<DBI>
164 169
 and implements the following new ones.
165 170
 
171
+=head2 C<call_dbi> EXPERIMENTAL
172
+
173
+    $model->call_dbi('insert',
174
+      {args => ['table', 'primary_key' 'bind_type']}, @_)
175
+
176
+Call L<DBIx::Custom>(or subclass) method. you can add
177
+attribute values of model to arguments by C<args> option.
178
+
179
+Generally this method is used when you want to added dbi method to model.
180
+
181
+    sub insert {
182
+        shift->call_dbi('insert',
183
+          {args => ['table', 'primary_key' 'bind_type']}, @_);
184
+    }
185
+
186
+=head2 C<count> EXPERIMENTAL
187
+
188
+    my $count = $model->count;
189
+
190
+Get rows count.
191
+
192
+Options is same as C<select> method's ones.
193
+
166 194
 =head2 C<delete>
167 195
 
168 196
     $table->delete(...);
+10
t/common.t
... ...
@@ -3923,5 +3923,15 @@ test 'columns';
3923 3923
 $dbi = MyDBI1->connect;
3924 3924
 $model = $dbi->model($table1);
3925 3925
 
3926
+test 'count';
3927
+$dbi = DBIx::Custom->connect;
3928
+eval { $dbi->execute("drop table $table1") };
3929
+$dbi->execute($create_table1);
3930
+$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
3931
+$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 3});
3932
+is($dbi->count(table => $table1), 2);
3933
+is($dbi->count(table => $table1, where => {$key2 => 2}), 1);
3934
+$model = $dbi->create_model(table => $table1);
3935
+is($model->count, 2);
3926 3936
 
3927 3937
 1;