Showing 6 changed files with 179 additions and 55 deletions
+2
Changes
... ...
@@ -1,3 +1,5 @@
1
+0.1628
2
+  added examples
1 3
 0.1627
2 4
   added insert, update, update_all, delete, delete_all, select method to DBIx::Custom::Table
3 5
   added experimental txn_scope
+1 -1
lib/DBIx/Custom.pm
... ...
@@ -1,6 +1,6 @@
1 1
 package DBIx::Custom;
2 2
 
3
-our $VERSION = '0.1627';
3
+our $VERSION = '0.1628';
4 4
 
5 5
 use 5.008001;
6 6
 use strict;
+41 -2
lib/DBIx/Custom/Guides.pod
... ...
@@ -540,9 +540,9 @@ You can change Result class if you need.
540 540
     my $dbi = DBIx::Custom->connect(...);
541 541
     $dbi->result_class('Your::Result');
542 542
 
543
-=head3 Custamize SQL builder object
543
+=head3 Custamize query builder object
544 544
 
545
-You can custamize SQL builder object
545
+You can custamize query builder object
546 546
 
547 547
     my $dbi = DBIx::Custom->connect(...);
548 548
     $dbi->query_builder->register_tag_processor(
... ...
@@ -572,4 +572,43 @@ These method can be called from L<DBIx::Custom> object directory.
572 572
     $dbi->update_or_insert;
573 573
     $dbi->find_or_create;
574 574
 
575
+=head2 EXAMPLES
576
+
577
+=head3 Limit clause
578
+
579
+    my $rows = $dbi->select(
580
+        table => 'table1',
581
+        where => {key1 => 1},
582
+        append => "order by key2 {limit 1 0}" # {limit COUNT OFFSET}
583
+    )->fetch_hash_all;
584
+
585
+SQLite
586
+
587
+    $dbi->query_builder->register_tag_processor(
588
+        limit => sub {
589
+            my ($count, $offset) = @_;
590
+            
591
+            my $s = '';
592
+            $s .= "limit $count";
593
+            $s .= " offset $offset" if defined $offset;
594
+            
595
+            return [$s, []];
596
+        }
597
+    );
598
+
599
+MySQL
600
+
601
+    $dbi->query_builder->register_tag_processor(
602
+        limit => sub {
603
+            my ($count, $offset) = @_;
604
+            
605
+            my $s = '';
606
+            $offset = 0 unless defined $offset;
607
+            $s .= "limit $offset";
608
+            $s .= ", $count";
609
+            
610
+            return [$s, []];
611
+        }
612
+    );
613
+
575 614
 =cut
+1 -1
lib/DBIx/Custom/Table.pm
... ...
@@ -57,7 +57,7 @@ sub DESTROY { }
57 57
 
58 58
 =head1 NAME
59 59
 
60
-DBIx::Custom::Model - Table base class(experimental)
60
+DBIx::Custom::Table - Table base class(experimental)
61 61
 
62 62
 =head1 SYNOPSIS
63 63
 
+35
t/dbix-custom-core-sqlite.t
... ...
@@ -794,4 +794,39 @@ $table->delete_all;
794 794
 $rows = $table->select->fetch_hash_all;
795 795
 is_deeply($rows, [], "$test: delete_all");
796 796
 
797
+test 'limit';
798
+$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
799
+$dbi->execute($CREATE_TABLE->{0});
800
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
801
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4});
802
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 6});
803
+$dbi->query_builder->register_tag_processor(
804
+    limit => sub {
805
+        my ($count, $offset) = @_;
806
+        
807
+        my $s = '';
808
+        $s .= "limit $count";
809
+        $s .= " offset $offset" if defined $offset;
810
+        
811
+        return [$s, []];
812
+    }
813
+);
814
+$rows = $dbi->select(
815
+  table => 'table1',
816
+  where => {key1 => 1},
817
+  append => "order by key2 {limit 1 0}"
818
+)->fetch_hash_all;
819
+is_deeply($rows, [{key1 => 1, key2 => 2}], $test);
820
+$rows = $dbi->select(
821
+  table => 'table1',
822
+  where => {key1 => 1},
823
+  append => "order by key2 {limit 2 1}"
824
+)->fetch_hash_all;
825
+is_deeply($rows, [{key1 => 1, key2 => 4},{key1 => 1, key2 => 6}], $test);
826
+$rows = $dbi->select(
827
+  table => 'table1',
828
+  where => {key1 => 1},
829
+  append => "order by key2 {limit 1}"
830
+)->fetch_hash_all;
831
+is_deeply($rows, [{key1 => 1, key2 => 2}], $test);
797 832
 
+99 -51
t/dbix-custom-mysql-private.t
... ...
@@ -1,51 +1,99 @@
1
-use Test::More;
2
-use strict;
3
-use warnings;
4
-
5
-# user password database
6
-our ($USER, $PASSWORD, $DATABASE) = connect_info();
7
-
8
-plan skip_all => 'private MySQL test' unless $USER;
9
-
10
-plan 'no_plan';
11
-
12
-# Function for test name
13
-my $test;
14
-sub test {
15
-    $test = shift;
16
-}
17
-
18
-
19
-# Functions for tests
20
-sub connect_info {
21
-    my $file = 'password.tmp';
22
-    open my $fh, '<', $file
23
-      or return;
24
-    
25
-    my ($user, $password, $database) = split(/\s/, (<$fh>)[0]);
26
-    
27
-    close $fh;
28
-    
29
-    return ($user, $password, $database);
30
-}
31
-
32
-
33
-# Varialbes for tests
34
-my $dbi;
35
-my $dbname;
36
-
37
-use DBIx::Custom::MySQL;
38
-
39
-test 'connect';
40
-$dbi = DBIx::Custom::MySQL->new(user => $USER, password => $PASSWORD,
41
-                    database => $DATABASE, host => 'localhost', port => '10000');
42
-$dbi->connect;
43
-like($dbi->data_source, qr/dbi:mysql:database=.*;host=localhost;port=10000;/, "$test : created data source");
44
-is(ref $dbi->dbh, 'DBI::db', $test);
45
-
46
-test 'attributes';
47
-$dbi = DBIx::Custom::MySQL->new;
48
-$dbi->host('a');
49
-is($dbi->host, 'a', "$test: host");
50
-$dbi->port('b');
51
-is($dbi->port, 'b', "$test: port");
1
+use Test::More;
2
+use strict;
3
+use warnings;
4
+
5
+# user password database
6
+our ($USER, $PASSWORD, $DATABASE) = connect_info();
7
+
8
+plan skip_all => 'private MySQL test' unless $USER;
9
+
10
+plan 'no_plan';
11
+
12
+# Function for test name
13
+my $test;
14
+sub test {
15
+    $test = shift;
16
+}
17
+
18
+
19
+# Functions for tests
20
+sub connect_info {
21
+    my $file = 'password.tmp';
22
+    open my $fh, '<', $file
23
+      or return;
24
+    
25
+    my ($user, $password, $database) = split(/\s/, (<$fh>)[0]);
26
+    
27
+    close $fh;
28
+    
29
+    return ($user, $password, $database);
30
+}
31
+
32
+
33
+# Varialbes for tests
34
+my $dbi;
35
+my $dbname;
36
+my $rows;
37
+
38
+# Constant varialbes for test
39
+my $CREATE_TABLE = {
40
+    0 => 'create table table1 (key1 char(255), key2 char(255));'
41
+};
42
+
43
+
44
+use DBIx::Custom::MySQL;
45
+
46
+test 'connect';
47
+$dbi = DBIx::Custom::MySQL->new(user => $USER, password => $PASSWORD,
48
+                    database => $DATABASE, host => 'localhost', port => '10000');
49
+$dbi->connect;
50
+like($dbi->data_source, qr/dbi:mysql:database=.*;host=localhost;port=10000;/, "$test : created data source");
51
+is(ref $dbi->dbh, 'DBI::db', $test);
52
+
53
+test 'attributes';
54
+$dbi = DBIx::Custom::MySQL->new;
55
+$dbi->host('a');
56
+is($dbi->host, 'a', "$test: host");
57
+$dbi->port('b');
58
+is($dbi->port, 'b', "$test: port");
59
+
60
+test 'limit';
61
+$dbi = DBIx::Custom->connect(
62
+    data_source => "dbi:mysql:database=$DATABASE",
63
+    user => $USER,
64
+    password => $PASSWORD
65
+);
66
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
67
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4});
68
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 6});
69
+$dbi->query_builder->register_tag_processor(
70
+    limit => sub {
71
+        my ($count, $offset) = @_;
72
+        
73
+        my $s = '';
74
+        $offset = 0 unless defined $offset;
75
+        $s .= "limit $offset";
76
+        $s .= ", $count";
77
+        
78
+        return [$s, []];
79
+    }
80
+);
81
+$rows = $dbi->select(
82
+  table => 'table1',
83
+  where => {key1 => 1},
84
+  append => "order by key2 {limit 1 0}"
85
+)->fetch_hash_all;
86
+is_deeply($rows, [{key1 => 1, key2 => 2}], $test);
87
+$rows = $dbi->select(
88
+  table => 'table1',
89
+  where => {key1 => 1},
90
+  append => "order by key2 {limit 2 1}"
91
+)->fetch_hash_all;
92
+is_deeply($rows, [{key1 => 1, key2 => 4},{key1 => 1, key2 => 6}], $test);
93
+$rows = $dbi->select(
94
+  table => 'table1',
95
+  where => {key1 => 1},
96
+  append => "order by key2 {limit 1}"
97
+)->fetch_hash_all;
98
+is_deeply($rows, [{key1 => 1, key2 => 2}], $test);
99
+$dbi->delete_all(table => 'table1');