... | ... |
@@ -1,3 +1,5 @@ |
1 |
+0.1647 |
|
2 |
+ add default_dbi_option() |
|
1 | 3 |
0.1646 |
2 | 4 |
add feture. all model class in namespace is included by include_model |
3 | 5 |
rename experimental include_table to include_model |
... | ... |
@@ -1,6 +1,6 @@ |
1 | 1 |
package DBIx::Custom; |
2 | 2 |
|
3 |
-our $VERSION = '0.1646'; |
|
3 |
+our $VERSION = '0.1647'; |
|
4 | 4 |
|
5 | 5 |
use 5.008001; |
6 | 6 |
use strict; |
... | ... |
@@ -21,7 +21,12 @@ use Encode qw/encode_utf8 decode_utf8/; |
21 | 21 |
__PACKAGE__->attr( |
22 | 22 |
[qw/data_source dbh password user/], |
23 | 23 |
cache => 1, |
24 |
- dbi_option => sub { {} }, |
|
24 |
+ dbi_option => sub { {} }, |
|
25 |
+ default_dbi_option => sub {{ |
|
26 |
+ RaiseError => 1, |
|
27 |
+ PrintError => 0, |
|
28 |
+ AutoCommit => 1 |
|
29 |
+ }}, |
|
25 | 30 |
query_builder => sub { DBIx::Custom::QueryBuilder->new }, |
26 | 31 |
result_class => 'DBIx::Custom::Result', |
27 | 32 |
safety_column_name => sub { qr/^[\w\.]*$/ }, |
... | ... |
@@ -156,9 +161,7 @@ sub connect { |
156 | 161 |
$user, |
157 | 162 |
$password, |
158 | 163 |
{ |
159 |
- RaiseError => 1, |
|
160 |
- PrintError => 0, |
|
161 |
- AutoCommit => 1, |
|
164 |
+ %{$self->default_dbi_option}, |
|
162 | 165 |
%$dbi_option |
163 | 166 |
} |
164 | 167 |
)}; |
... | ... |
@@ -1025,6 +1028,23 @@ L<DBI> object. You can call all methods of L<DBI>. |
1025 | 1028 |
$dbi = $dbi->dbi_option($dbi_option); |
1026 | 1029 |
|
1027 | 1030 |
DBI options. |
1031 |
+ |
|
1032 |
+Each option specified can ovewrite C<default_dbi_option>. |
|
1033 |
+ |
|
1034 |
+C<connect()> method use this value to connect the database. |
|
1035 |
+ |
|
1036 |
+ |
|
1037 |
+=head2 C<default_dbi_option> |
|
1038 |
+ |
|
1039 |
+ my $default_dbi_option = $dbi->default_dbi_option; |
|
1040 |
+ $dbi = $dbi->default_dbi_option($default_dbi_option); |
|
1041 |
+ |
|
1042 |
+DBI default options. |
|
1043 |
+ |
|
1044 |
+ RaiseError => 1, |
|
1045 |
+ PrintError => 0, |
|
1046 |
+ AutoCommit => 1, |
|
1047 |
+ |
|
1028 | 1048 |
C<connect()> method use this value to connect the database. |
1029 | 1049 |
|
1030 | 1050 |
Default filter when row is fetched. |
... | ... |
@@ -0,0 +1,63 @@ |
1 |
+use Test::More 'no_plan'; |
|
2 |
+ |
|
3 |
+{ |
|
4 |
+ package MyDBI1; |
|
5 |
+ |
|
6 |
+ use base 'DBIx::Custom'; |
|
7 |
+ |
|
8 |
+ use DBIx::Connector; |
|
9 |
+ |
|
10 |
+ __PACKAGE__->attr(connection_manager => sub { |
|
11 |
+ my $self = shift; |
|
12 |
+ |
|
13 |
+ my $cm = DBIx::Connector->new( |
|
14 |
+ $self->data_source, |
|
15 |
+ $self->user, |
|
16 |
+ $self->password, |
|
17 |
+ { |
|
18 |
+ %{$self->default_dbi_option}, |
|
19 |
+ %{$self->dbi_option} |
|
20 |
+ } |
|
21 |
+ ); |
|
22 |
+ |
|
23 |
+ return $cm |
|
24 |
+ }); |
|
25 |
+ |
|
26 |
+ sub dbh { shift->connection_manager->dbh } |
|
27 |
+ |
|
28 |
+ sub connect { |
|
29 |
+ my $self = shift->SUPER::new(@_); |
|
30 |
+ |
|
31 |
+ return $self; |
|
32 |
+ } |
|
33 |
+} |
|
34 |
+ |
|
35 |
+# user password database |
|
36 |
+our ($USER, $PASSWORD, $DATABASE) = connect_info(); |
|
37 |
+ |
|
38 |
+# Functions for tests |
|
39 |
+sub connect_info { |
|
40 |
+ my $file = 'password.tmp'; |
|
41 |
+ open my $fh, '<', $file |
|
42 |
+ or return; |
|
43 |
+ |
|
44 |
+ my ($user, $password, $database) = split(/\s/, (<$fh>)[0]); |
|
45 |
+ |
|
46 |
+ close $fh; |
|
47 |
+ |
|
48 |
+ return ($user, $password, $database); |
|
49 |
+} |
|
50 |
+ |
|
51 |
+my $dbi = MyDBI1->connect( |
|
52 |
+ user => $USER, password => $PASSWORD, |
|
53 |
+ data_source => "dbi:mysql:database=$DATABASE"); |
|
54 |
+ |
|
55 |
+$dbi->delete_all(table => 'table1'); |
|
56 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
57 |
+is_deeply($dbi->select(table => 'table1')->fetch_hash_all, [{key1 => 1, key2 => 2}]); |
|
58 |
+ |
|
59 |
+ |
|
60 |
+ |
|
61 |
+ |
|
62 |
+ |
|
63 |
+ |