Showing 5 changed files with 101 additions and 12 deletions
+7
lib/DBIx/Custom/Guide/Ja.pod
... ...
@@ -1079,6 +1079,13 @@ C<delete()>, C<delete_all()>, C<select()>などのメソッドを
1079 1079
 
1080 1080
     $dbi->model('book')->insert(param => $param);
1081 1081
 
1082
+またモデルクラスでC<primary_key>の設定がなされていれば、
1083
+プライマリキーを指定することなしに
1084
+C<update_at()>, C<delete_at()>, C<select_at()>のメソッドを
1085
+利用できます。
1086
+
1087
+    $dbi->model('book')->delete_at(where => 123);
1088
+
1082 1089
 モデルはL<DBIx::Custom::Model>です。
1083 1090
 
1084 1091
 必要であれば、C<table()>でテーブル名を取得することができます。
+33 -12
lib/DBIx/Custom/Model.pm
... ...
@@ -51,20 +51,41 @@ sub method {
51 51
     return $self;
52 52
 }
53 53
 
54
-sub new {
55
-    my $self = shift->SUPER::new(@_);
54
+sub insert     { my $self = shift; $self->dbi->insert(table => $self->table, @_) }
55
+sub update     { my $self = shift; $self->dbi->update(table => $self->table, @_) }
56
+sub update_all { my $self = shift; $self->dbi->update_all(table => $self->table, @_) }
57
+sub delete     { my $self = shift; $self->dbi->delete(table => $self->table, @_) }
58
+sub delete_all { my $self = shift; $self->dbi->delete_all(table => $self->table, @_) }
59
+sub select     { my $self = shift; $self->dbi->select(table => $self->table, @_) }
60
+
61
+sub update_at {
62
+    my $self = shift;
56 63
     
57
-    # Methods
58
-    my @methods = qw/insert update update_all delete delete_all select/;
59
-    foreach my $method (@methods) {
60
-        $self->method(
61
-            $method => sub {
62
-                my $self = shift;
63
-                return $self->dbi->$method(table => $self->table, @_);
64
-            }
65
-        );
66
-    }
64
+    return $self->dbi->update_at(
65
+        table => $self->table,
66
+        primary_key => $self->primary_key,
67
+        @_
68
+    );
69
+}
70
+
71
+sub delete_at {
72
+    my $self = shift;
67 73
     
74
+    return $self->dbi->delete_at(
75
+        table => $self->table,
76
+        primary_key => $self->primary_key,
77
+        @_
78
+    );
79
+}
80
+
81
+sub select_at {
82
+    my $self = shift;
83
+    
84
+    return $self->dbi->select_at(
85
+        table => $self->table,
86
+        primary_key => $self->primary_key,
87
+        @_
88
+    );
68 89
     return $self;
69 90
 }
70 91
 
+46
t/dbix-custom-core-sqlite.t
... ...
@@ -1506,3 +1506,49 @@ $dbi = MyDBI1->connect($NEW_ARGS->{0});
1506 1506
 $model = $dbi->model('book');
1507 1507
 $model->relation({'book.id' => 'company.id'});
1508 1508
 is_deeply($model->relation, {'book.id' => 'company.id'});
1509
+
1510
+
1511
+test 'model delete_at';
1512
+{
1513
+    package MyDBI6;
1514
+    
1515
+    use base 'DBIx::Custom';
1516
+    
1517
+    sub connect {
1518
+        my $self = shift->SUPER::connect(@_);
1519
+        
1520
+        $self->include_model('MyModel5');
1521
+        
1522
+        return $self;
1523
+    }
1524
+}
1525
+$dbi = MyDBI6->connect($NEW_ARGS->{0});
1526
+$dbi->execute($CREATE_TABLE->{1});
1527
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1528
+$dbi->model('table1')->delete_at(where => [1, 2]);
1529
+is_deeply($dbi->select(table => 'table1')->fetch_hash_all, []);
1530
+
1531
+
1532
+test 'update_at';
1533
+$dbi = MyDBI6->connect($NEW_ARGS->{0});
1534
+$dbi->execute($CREATE_TABLE->{1});
1535
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1536
+$dbi->model('table1')->update_at(
1537
+    where => [1, 2],
1538
+    param => {key3 => 4}
1539
+);
1540
+$result = $dbi->model('table1')->select;
1541
+$row = $result->fetch_hash_first;
1542
+is($row->{key1}, 1);
1543
+is($row->{key2}, 2);
1544
+is($row->{key3}, 4);
1545
+
1546
+test 'select_at';
1547
+$dbi = MyDBI6->connect($NEW_ARGS->{0});
1548
+$dbi->execute($CREATE_TABLE->{1});
1549
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1550
+$result = $dbi->model('table1')->select_at(where => [1, 2]);
1551
+$row = $result->fetch_hash_first;
1552
+is($row->{key1}, 1);
1553
+is($row->{key2}, 2);
1554
+is($row->{key3}, 3);
+5
t/dbix-custom-core-sqlite/MyModel5.pm
... ...
@@ -0,0 +1,5 @@
1
+package MyModel5;
2
+
3
+use base 'DBIx::Custom::Model';
4
+
5
+1;
+10
t/dbix-custom-core-sqlite/MyModel5/table1.pm
... ...
@@ -0,0 +1,10 @@
1
+package MyModel5::table1;
2
+
3
+use strict;
4
+use warnings;
5
+
6
+use base 'MyModel5';
7
+
8
+__PACKAGE__->attr('primary_key' => sub { ['key1', 'key2'] });
9
+
10
+1;