Showing 7 changed files with 49 additions and 75 deletions
+2 -1
Changes
... ...
@@ -2,7 +2,8 @@
2 2
     - removed from cache() and cache_method() document for a while and cache() value
3 3
       become 0 because I find something bug.
4 4
     - create_model() return model.
5
-    
5
+    - added document of hash filter
6
+    - adeed EXPERIMENTAL DBIx::Custom::Model method()
6 7
 0.1665
7 8
     - removed EXPERIMETNAL flag from insert_at(), update_at(), delete_at(), select_at(), insert_param(), not_exists(), select()'s query option, update_param(), where, table tag, each column, safety_character, DBIx::Where, where().
8 9
     - added EXPERIMETNAL create_model()
+2 -2
lib/DBIx/Custom.pm
... ...
@@ -1697,7 +1697,7 @@ and C<PrintError> option is false by default.
1697 1697
 
1698 1698
 =head2 create_model
1699 1699
 
1700
-    $dbi->create_model(
1700
+    my $model = $dbi->create_model(
1701 1701
         table => 'book',
1702 1702
         primary_key => 'id',
1703 1703
         join => [
... ...
@@ -1713,7 +1713,7 @@ and C<PrintError> option is false by default.
1713 1713
     );
1714 1714
 
1715 1715
 Create L<DBIx::Custom::Model> object and initialize model.
1716
-the module is used from model() method.
1716
+the module is also used from model() method.
1717 1717
 
1718 1718
    $dbi->model('book')->select(...);
1719 1719
 
-35
lib/DBIx/Custom/Guide.pod
... ...
@@ -1344,41 +1344,6 @@ You can change result class. By default it is L<DBIx::Custom::Result>.
1344 1344
     my $dbi = DBIx::Custom->connect(...);
1345 1345
     $dbi->result_class('MyResult');
1346 1346
 
1347
-=head3 Caching
1348
-
1349
-SQL after parsing tag is cached for performance.
1350
-You can set C<cache()>. By default, chaching is true.
1351
-
1352
-    $dbi->cache(1);
1353
-
1354
-The way to cache is changed by C<cache_method()>.
1355
-Default method is the following one.
1356
-Cache is saved to memory.
1357
-
1358
-    $dbi->cache_method(sub {
1359
-        sub {
1360
-            my $self = shift;
1361
-            
1362
-            $self->{_cached} ||= {};
1363
-            
1364
-            if (@_ > 1) {
1365
-                # Save cache
1366
-                $self->{_cached}{$_[0]} = $_[1] 
1367
-            }
1368
-            else {
1369
-                # Get cache
1370
-                return $self->{_cached}{$_[0]}
1371
-            }
1372
-        }
1373
-    });
1374
-    
1375
-First argument is L<DBIx::Custom> object.
1376
-Second argument is SQL before parsing.
1377
-Third argument is SQL information after parsing. This is hash reference.
1378
-
1379
-If third argument exists, you save cache,
1380
-and if third argument isn't exists, you get chace.
1381
-
1382 1347
 =head1 EXAMPLES
1383 1348
 
1384 1349
 You can see exsamples in the following wiki.
-36
lib/DBIx/Custom/Guide/Ja.pod
... ...
@@ -1369,42 +1369,6 @@ L<DBIx::Custom>オブジェクトから呼び出すことができます。
1369 1369
     my $dbi = DBIx::Custom->connect(...);
1370 1370
     $dbi->result_class('MyResult');
1371 1371
 
1372
-=head3 キャッシング
1373
-
1374
-タグの解析後のSQLはパフォーマンスの理由のためにキャッシュされます。
1375
-これはC<chace>で設定でき、デフォルトではキャッシュを行う設定です。
1376
-
1377
-    $dbi->cache(1);
1378
-
1379
-キャッシュ方法はC<cache_method>で変更することができます。
1380
-デフォルトのメソッドは以下のようになっていて、
1381
-メモリ上にキャッシュが保存されます。
1382
-
1383
-    $dbi->cache_method(sub {
1384
-        sub {
1385
-            my $self = shift;
1386
-            
1387
-            $self->{_cached} ||= {};
1388
-            
1389
-            if (@_ > 1) {
1390
-                # キャッシュの保存
1391
-                $self->{_cached}{$_[0]} = $_[1] 
1392
-            }
1393
-            else {
1394
-                # キャッシュの取得
1395
-                return $self->{_cached}{$_[0]}
1396
-            }
1397
-        }
1398
-    });
1399
-    
1400
-第一はL<DBIx::Custom>オブジェクトです。
1401
-第二引数はタグが解析される前のSQLです。
1402
-第三引数はタグの解析後のSQLの情報です。これはハッシュのリファレンスです。
1403
-
1404
-第三引数が存在した場合はキャッシュを設定し、
1405
-存在しなかった場合はキャッシュを取得する実装に
1406
-してください。
1407
-
1408 1372
 =head1 サンプル
1409 1373
 
1410 1374
 以下のWikiでサンプルを見ることができます。
+34 -1
lib/DBIx/Custom/Model.pm
... ...
@@ -29,7 +29,11 @@ sub AUTOLOAD {
29 29
     my ($package, $mname) = $AUTOLOAD =~ /^([\w\:]+)\:\:(\w+)$/;
30 30
 
31 31
     # Method
32
-    if (my $dbi_method = $self->dbi->can($mname)) {
32
+    $self->{_methods} ||= {};
33
+    if (my $method = $self->{_methods}->{$mname}) {
34
+        return $self->$method(@_)
35
+    }
36
+    elsif (my $dbi_method = $self->dbi->can($mname)) {
33 37
         $self->dbi->$dbi_method(@_);
34 38
     }
35 39
     elsif (my $dbh_method = $self->dbi->dbh->can($mname)) {
... ...
@@ -78,6 +82,16 @@ sub column {
78 82
 
79 83
 sub DESTROY { }
80 84
 
85
+sub method {
86
+    my $self = shift;
87
+    
88
+    # Merge
89
+    my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_};
90
+    $self->{_methods} = {%{$self->{_methods} || {}}, %$methods};
91
+    
92
+    return $self;
93
+}
94
+
81 95
 sub mycolumn {
82 96
     my $self = shift;
83 97
     my $table = shift unless ref $_[0];
... ...
@@ -232,6 +246,25 @@ you don't have to specify C<table> option.
232 246
 Same as C<insert_at()> of L<DBIx::Custom> except that
233 247
 you don't have to specify C<table> and C<primary_key> option.
234 248
 
249
+=head2 C<method> EXPERIMENTAL
250
+
251
+    $model->method(
252
+        update_or_insert => sub {
253
+            my $self = shift;
254
+            
255
+            # ...
256
+        },
257
+        find_or_create   => sub {
258
+            my $self = shift;
259
+            
260
+            # ...
261
+    );
262
+
263
+Register method. These method is called directly from L<DBIx::Custom::Model> object.
264
+
265
+    $model->update_or_insert;
266
+    $model->find_or_create;
267
+
235 268
 =head2 C<mycolumn>
236 269
 
237 270
     my $column = $self->mycolumn;
+1
lib/DBIx/Custom/Util.pm
... ...
@@ -6,6 +6,7 @@ use warnings;
6 6
 sub array_to_hash {
7 7
     my $array = shift;
8 8
     
9
+    return $array if ref $array eq 'HASH';
9 10
     return unless $array;
10 11
     return $array if ref $array eq 'HASH';
11 12
     
+10
t/dbix-custom-core-sqlite.t
... ...
@@ -1975,3 +1975,13 @@ is_deeply($result->fetch_hash_first,
1975 1975
           {key1 => 1, key2 => 2, 'table2__key1' => 1, 'table2__key3' => 3});
1976 1976
 is_deeply($model2->select->fetch_hash_first, {key1 => 1, key3 => 3});
1977 1977
 
1978
+test 'model method';
1979
+test 'create_model';
1980
+$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
1981
+$dbi->execute($CREATE_TABLE->{2});
1982
+$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
1983
+$model = $dbi->create_model(
1984
+    table => 'table2'
1985
+);
1986
+$model->method(foo => sub { shift->select(@_) });
1987
+is_deeply($model->foo->fetch_hash_first, {key1 => 1, key3 => 3});