| ... | ... |
@@ -9,26 +9,15 @@ use DBI::Custom::SQL::Template; |
| 9 | 9 |
use DBI::Custom::Result; |
| 10 | 10 |
|
| 11 | 11 |
### Class-Object Accessors |
| 12 |
-sub connect_info : ClassObjectAttr {
|
|
| 13 |
- type => 'hash', |
|
| 14 |
- initialize => {
|
|
| 15 |
- clone => sub {
|
|
| 16 |
- my $value = shift; |
|
| 17 |
- my $new_value = \%{$value || {}};
|
|
| 18 |
- $new_value->{options} = $value->{options} if $value->{options};
|
|
| 19 |
- return $new_value; |
|
| 20 |
- }, |
|
| 21 |
- default => sub { {} },
|
|
| 22 |
- } |
|
| 23 |
-} |
|
| 12 |
+sub user : ClassObjectAttr { initialize => {clone => 'scalar'} }
|
|
| 13 |
+sub password : ClassObjectAttr { initialize => {clone => 'scalar'} }
|
|
| 14 |
+sub data_source : ClassObjectAttr { initialize => {clone => 'scalar'} }
|
|
| 24 | 15 |
|
| 25 |
-sub bind_filter : ClassObjectAttr {
|
|
| 26 |
- initialize => {clone => 'scalar'}
|
|
| 27 |
-} |
|
| 16 |
+sub dbi_option : ClassObjectAttr { initialize => {clone => 'hash',
|
|
| 17 |
+ default => sub { {} } } }
|
|
| 28 | 18 |
|
| 29 |
-sub fetch_filter : ClassObjectAttr {
|
|
| 30 |
- initialize => {clone => 'scalar'}
|
|
| 31 |
-} |
|
| 19 |
+sub bind_filter : ClassObjectAttr { initialize => {clone => 'scalar'} }
|
|
| 20 |
+sub fetch_filter : ClassObjectAttr { initialize => {clone => 'scalar'} }
|
|
| 32 | 21 |
|
| 33 | 22 |
sub filters : ClassObjectAttr {
|
| 34 | 23 |
type => 'hash', |
| ... | ... |
@@ -48,20 +37,11 @@ sub result_class : ClassObjectAttr {
|
| 48 | 37 |
|
| 49 | 38 |
sub sql_template : ClassObjectAttr {
|
| 50 | 39 |
initialize => {
|
| 51 |
- clone => sub {my $value = shift; $value ? $value->clone : undef},
|
|
| 40 |
+ clone => sub {$_[0] ? $_[0]->clone : undef},
|
|
| 52 | 41 |
default => sub {DBI::Custom::SQL::Template->new}
|
| 53 | 42 |
} |
| 54 | 43 |
} |
| 55 | 44 |
|
| 56 |
-sub valid_connect_info : ClassObjectAttr {
|
|
| 57 |
- type => 'hash', |
|
| 58 |
- deref => 1, |
|
| 59 |
- initialize => {
|
|
| 60 |
- clone => 'hash', |
|
| 61 |
- default => sub { return {map {$_ => 1} qw/data_source user password options/} },
|
|
| 62 |
- } |
|
| 63 |
-} |
|
| 64 |
- |
|
| 65 | 45 |
### Object Accessor |
| 66 | 46 |
sub dbh : Attr {}
|
| 67 | 47 |
|
| ... | ... |
@@ -74,10 +54,11 @@ sub add_filter {
|
| 74 | 54 |
my %old_filters = $invocant->filters; |
| 75 | 55 |
my %new_filters = ref $_[0] eq 'HASH' ? %{$_[0]} : @_;
|
| 76 | 56 |
$invocant->filters(%old_filters, %new_filters); |
| 57 |
+ return $invocant; |
|
| 77 | 58 |
} |
| 78 | 59 |
|
| 79 | 60 |
# Auto commit |
| 80 |
-sub auto_commit {
|
|
| 61 |
+sub _auto_commit {
|
|
| 81 | 62 |
my $self = shift; |
| 82 | 63 |
|
| 83 | 64 |
croak("Cannot change AutoCommit becouse of not connected")
|
| ... | ... |
@@ -93,22 +74,20 @@ sub auto_commit {
|
| 93 | 74 |
# Connect |
| 94 | 75 |
sub connect {
|
| 95 | 76 |
my $self = shift; |
| 96 |
- my $connect_info = $self->connect_info; |
|
| 97 |
- |
|
| 98 |
- foreach my $key (keys %{$self->connect_info}) {
|
|
| 99 |
- croak("connect_info '$key' is wrong name")
|
|
| 100 |
- unless $self->valid_connect_info->{$key};
|
|
| 101 |
- } |
|
| 77 |
+ my $data_source = $self->data_source; |
|
| 78 |
+ my $user = $self->user; |
|
| 79 |
+ my $password = $self->password; |
|
| 80 |
+ my $dbi_option = $self->dbi_option; |
|
| 102 | 81 |
|
| 103 | 82 |
my $dbh = DBI->connect( |
| 104 |
- $connect_info->{data_source},
|
|
| 105 |
- $connect_info->{user},
|
|
| 106 |
- $connect_info->{password},
|
|
| 83 |
+ $data_source, |
|
| 84 |
+ $user, |
|
| 85 |
+ $password, |
|
| 107 | 86 |
{
|
| 108 | 87 |
RaiseError => 1, |
| 109 | 88 |
PrintError => 0, |
| 110 | 89 |
AutoCommit => 1, |
| 111 |
- %{$connect_info->{options} || {} }
|
|
| 90 |
+ %{$dbi_option || {} }
|
|
| 112 | 91 |
} |
| 113 | 92 |
); |
| 114 | 93 |
|
| ... | ... |
@@ -148,7 +127,7 @@ sub reconnect {
|
| 148 | 127 |
sub run_tranzaction {
|
| 149 | 128 |
my ($self, $tranzaction) = @_; |
| 150 | 129 |
|
| 151 |
- $self->auto_commit(0); |
|
| 130 |
+ $self->_auto_commit(0); |
|
| 152 | 131 |
|
| 153 | 132 |
eval {
|
| 154 | 133 |
$tranzaction->(); |
| ... | ... |
@@ -161,11 +140,11 @@ sub run_tranzaction {
|
| 161 | 140 |
$self->dbh->rollback or croak("$@ and rollback also failed");
|
| 162 | 141 |
croak("$tranzaction_error");
|
| 163 | 142 |
} |
| 164 |
- $self->auto_commit(1); |
|
| 143 |
+ $self->_auto_commit(1); |
|
| 165 | 144 |
} |
| 166 | 145 |
|
| 167 | 146 |
# Create SQL from SQL template |
| 168 |
-sub create_sql {
|
|
| 147 |
+sub _create_sql {
|
|
| 169 | 148 |
my $self = shift; |
| 170 | 149 |
|
| 171 | 150 |
my ($sql, @bind) = $self->sql_template->create_sql(@_); |
| ... | ... |
@@ -188,7 +167,7 @@ sub query {
|
| 188 | 167 |
|
| 189 | 168 |
$filter ||= $self->bind_filter; |
| 190 | 169 |
|
| 191 |
- my ($sql, @bind) = $self->create_sql($template, $values, $filter); |
|
| 170 |
+ my ($sql, @bind_values) = $self->_create_sql($template, $values, $filter); |
|
| 192 | 171 |
|
| 193 | 172 |
$self->connect unless $self->connected; |
| 194 | 173 |
|
| ... | ... |
@@ -201,7 +180,7 @@ sub query {
|
| 201 | 180 |
} |
| 202 | 181 |
|
| 203 | 182 |
# Execute |
| 204 |
- my $ret_val = $sth->execute(@bind); |
|
| 183 |
+ my $ret_val = $sth->execute(@bind_values); |
|
| 205 | 184 |
|
| 206 | 185 |
# Return resultset if select statement is executed |
| 207 | 186 |
if ($sth->{NUM_OF_FIELDS}) {
|
| ... | ... |
@@ -229,9 +208,18 @@ sub query_raw_sql {
|
| 229 | 208 |
my $sth = $self->dbh->prepare($sql); |
| 230 | 209 |
|
| 231 | 210 |
# Execute |
| 232 |
- $sth->execute(@bind_values); |
|
| 211 |
+ my $ret_val = $sth->execute(@bind_values); |
|
| 233 | 212 |
|
| 234 |
- return $sth; |
|
| 213 |
+ # Return resultset if select statement is executed |
|
| 214 |
+ if ($sth->{NUM_OF_FIELDS}) {
|
|
| 215 |
+ my $result_class = $self->result_class; |
|
| 216 |
+ my $result = $result_class->new({
|
|
| 217 |
+ sth => $sth, |
|
| 218 |
+ fetch_filter => $self->fetch_filter |
|
| 219 |
+ }); |
|
| 220 |
+ return $result; |
|
| 221 |
+ } |
|
| 222 |
+ return $ret_val; |
|
| 235 | 223 |
} |
| 236 | 224 |
|
| 237 | 225 |
Object::Simple->build_class; |
| ... | ... |
@@ -250,46 +238,216 @@ Version 0.0101 |
| 250 | 238 |
|
| 251 | 239 |
my $dbi = DBI::Custom->new; |
| 252 | 240 |
|
| 253 |
-=head1 METHODS |
|
| 241 |
+=head1 CLASS-OBJECT ACCESSORS |
|
| 254 | 242 |
|
| 255 |
-=head2 add_filter |
|
| 243 |
+=head2 user |
|
| 244 |
+ |
|
| 245 |
+ # Set and get database user name |
|
| 246 |
+ $self = $dbi->user($user); |
|
| 247 |
+ $user = $dbi->user; |
|
| 248 |
+ |
|
| 249 |
+ # Sample |
|
| 250 |
+ $dbi->user('taro');
|
|
| 251 |
+ |
|
| 252 |
+=head2 password |
|
| 253 |
+ |
|
| 254 |
+ # Set and get database password |
|
| 255 |
+ $self = $dbi->password($password); |
|
| 256 |
+ $password = $dbi->password; |
|
| 257 |
+ |
|
| 258 |
+ # Sample |
|
| 259 |
+ $dbi->password('lkj&le`@s');
|
|
| 260 |
+ |
|
| 261 |
+=head2 data_source |
|
| 262 |
+ |
|
| 263 |
+ # Set and get database data source |
|
| 264 |
+ $self = $dbi->data_source($data_soruce); |
|
| 265 |
+ $data_source = $dbi->data_source; |
|
| 266 |
+ |
|
| 267 |
+ # Sample(SQLite) |
|
| 268 |
+ $dbi->data_source(dbi:SQLite:dbname=$database); |
|
| 269 |
+ |
|
| 270 |
+ # Sample(MySQL); |
|
| 271 |
+ $dbi->data_source("dbi:mysql:dbname=$database");
|
|
| 272 |
+ |
|
| 273 |
+ # Sample(PostgreSQL) |
|
| 274 |
+ $dbi->data_source("dbi:Pg:dbname=$database");
|
|
| 275 |
+ |
|
| 276 |
+=head2 dbi_option |
|
| 277 |
+ |
|
| 278 |
+ # Set and get DBI option |
|
| 279 |
+ $self = $dbi->dbi_option({$options => $value, ...});
|
|
| 280 |
+ $dbi_option = $dbi->dbi_option; |
|
| 281 |
+ |
|
| 282 |
+ # Sample |
|
| 283 |
+ $dbi->dbi_option({PrintError => 0, RaiseError => 1});
|
|
| 284 |
+ |
|
| 285 |
+dbi_option is used when you connect database by using connect. |
|
| 286 |
+ |
|
| 287 |
+=head2 sql_template |
|
| 288 |
+ |
|
| 289 |
+ # Set and get SQL::Template object |
|
| 290 |
+ $self = $dbi->sql_template($sql_template); |
|
| 291 |
+ $sql_template = $dbi->sql_template; |
|
| 292 |
+ |
|
| 293 |
+ # Sample |
|
| 294 |
+ $dbi->sql_template(DBI::Cutom::SQL::Template->new); |
|
| 295 |
+ |
|
| 296 |
+=head2 filters |
|
| 297 |
+ |
|
| 298 |
+ # Set and get filters |
|
| 299 |
+ $self = $dbi->filters($filters); |
|
| 300 |
+ $filters = $dbi->filters; |
|
| 256 | 301 |
|
| 257 | 302 |
=head2 bind_filter |
| 258 | 303 |
|
| 259 |
-=head2 connect |
|
| 304 |
+ # Set and get binding filter |
|
| 305 |
+ $self = $dbi->bind_filter($bind_filter); |
|
| 306 |
+ $bind_filter = $dbi->bind_filter |
|
| 260 | 307 |
|
| 261 |
-=head2 connect_info |
|
| 308 |
+ # Sample |
|
| 309 |
+ $dbi->bind_filter($self->filters->{default_bind_filter});
|
|
| 310 |
+ |
|
| 262 | 311 |
|
| 263 |
-=head2 dbh |
|
| 312 |
+you can get DBI database handle if you need. |
|
| 264 | 313 |
|
| 265 | 314 |
=head2 fetch_filter |
| 266 | 315 |
|
| 267 |
-=head2 filters |
|
| 316 |
+ # Set and get Fetch filter |
|
| 317 |
+ $self = $dbi->fetch_filter($fetch_filter); |
|
| 318 |
+ $fetch_filter = $dbi->fetch_filter; |
|
| 268 | 319 |
|
| 269 |
-=head2 new |
|
| 320 |
+ # Sample |
|
| 321 |
+ $dbi->fetch_filter($self->filters->{default_fetch_filter});
|
|
| 270 | 322 |
|
| 271 |
-=head2 query |
|
| 323 |
+=head2 result_class |
|
| 272 | 324 |
|
| 273 |
-=head2 create_sql |
|
| 325 |
+ # Set and get resultset class |
|
| 326 |
+ $self = $dbi->result_class($result_class); |
|
| 327 |
+ $result_class = $dbi->result_class; |
|
| 328 |
+ |
|
| 329 |
+ # Sample |
|
| 330 |
+ $dbi->result_class('DBI::Custom::Result');
|
|
| 274 | 331 |
|
| 275 |
-=head2 query_raw_sql |
|
| 332 |
+=head2 dbh |
|
| 276 | 333 |
|
| 277 |
-=head2 sql_template |
|
| 334 |
+ # Get database handle |
|
| 335 |
+ $dbh = $self->dbh; |
|
| 278 | 336 |
|
| 279 |
-=head2 auto_commit |
|
| 337 |
+=head1 METHODS |
|
| 280 | 338 |
|
| 281 |
-=head2 connected |
|
| 339 |
+=head2 connect |
|
| 340 |
+ |
|
| 341 |
+ # Connect to database |
|
| 342 |
+ $self = $dbi->connect; |
|
| 343 |
+ |
|
| 344 |
+ # Sample |
|
| 345 |
+ $dbi = DBI::Custom->new(user => 'taro', password => 'lji8(',
|
|
| 346 |
+ data_soruce => "dbi:mysql:dbname=$database"); |
|
| 347 |
+ $dbi->connect; |
|
| 282 | 348 |
|
| 283 | 349 |
=head2 disconnect |
| 284 | 350 |
|
| 351 |
+ # Disconnect database |
|
| 352 |
+ $dbi->disconnect; |
|
| 353 |
+ |
|
| 354 |
+If database is already disconnected, this method do noting. |
|
| 355 |
+ |
|
| 285 | 356 |
=head2 reconnect |
| 286 | 357 |
|
| 287 |
-=head2 result_class |
|
| 358 |
+ # Reconnect |
|
| 359 |
+ $dbi->reconnect; |
|
| 360 |
+ |
|
| 361 |
+=head2 connected |
|
| 362 |
+ |
|
| 363 |
+ # Check connected |
|
| 364 |
+ $dbi->connected |
|
| 365 |
+ |
|
| 366 |
+=head2 add_filter |
|
| 367 |
+ |
|
| 368 |
+ # Add filter (hash ref or hash can be recieve) |
|
| 369 |
+ $self = $dbi->add_filter({$filter_name => $filter, ...});
|
|
| 370 |
+ $self = $dbi->add_filter($filetr_name => $filter, ...); |
|
| 371 |
+ |
|
| 372 |
+ # Sample |
|
| 373 |
+ $dbi->add_filter( |
|
| 374 |
+ decode_utf8 => sub {
|
|
| 375 |
+ my $value = shift; |
|
| 376 |
+ return Encode::decode('UTF-8', $value);
|
|
| 377 |
+ }, |
|
| 378 |
+ datetime_to_string => sub {
|
|
| 379 |
+ my $value = shift; |
|
| 380 |
+ return $value->strftime('%Y-%m-%d %H:%M:%S')
|
|
| 381 |
+ }, |
|
| 382 |
+ default_bind_filter => sub {
|
|
| 383 |
+ my ($value, $key, $filters) = @_; |
|
| 384 |
+ if (ref $value eq 'Time::Piece') {
|
|
| 385 |
+ return $filters->{datetime_to_string}->($value);
|
|
| 386 |
+ } |
|
| 387 |
+ else {
|
|
| 388 |
+ return $filters->{decode_utf8}->($value);
|
|
| 389 |
+ } |
|
| 390 |
+ }, |
|
| 391 |
+ |
|
| 392 |
+ encode_utf8 => sub {
|
|
| 393 |
+ my $value = shift; |
|
| 394 |
+ return Encode::encode('UTF-8', $value);
|
|
| 395 |
+ }, |
|
| 396 |
+ string_to_datetime => sub {
|
|
| 397 |
+ my $value = shift; |
|
| 398 |
+ return DateTime::Format::MySQL->parse_datetime($value); |
|
| 399 |
+ }, |
|
| 400 |
+ default_fetch_filter => sub {
|
|
| 401 |
+ my ($value, $key, $filters, $type, $sth, $i) = @_; |
|
| 402 |
+ if ($type eq 'DATETIME') {
|
|
| 403 |
+ return $self->filters->{string_to_datetime}->($value);
|
|
| 404 |
+ } |
|
| 405 |
+ else {
|
|
| 406 |
+ return $self->filters->{encode_utf8}->($value);
|
|
| 407 |
+ } |
|
| 408 |
+ } |
|
| 409 |
+ ); |
|
| 410 |
+ |
|
| 411 |
+add_filter add filter to filters |
|
| 288 | 412 |
|
| 413 |
+=head2 query |
|
| 414 |
+ |
|
| 415 |
+ # Parse SQL template and execute SQL |
|
| 416 |
+ $result = $dbi->query($sql_template, $param); |
|
| 417 |
+ $result = $dbi->query($sql_template, $param, $bind_filter); |
|
| 418 |
+ |
|
| 419 |
+ # Sample |
|
| 420 |
+ $result = $dbi->query("select * from authors where {= name} && {= age}",
|
|
| 421 |
+ {author => 'taro', age => 19});
|
|
| 422 |
+ |
|
| 423 |
+ while (my @row = $result->fetch) {
|
|
| 424 |
+ # do something |
|
| 425 |
+ } |
|
| 426 |
+ |
|
| 427 |
+See also L<DBI::Custom::SQL::Template> |
|
| 428 |
+ |
|
| 429 |
+=head2 query_raw_sql |
|
| 430 |
+ |
|
| 431 |
+ # Execute SQL |
|
| 432 |
+ $result = $dbi->query_raw_sql($sql, @bind_values); |
|
| 433 |
+ |
|
| 434 |
+ # Sample |
|
| 435 |
+ $result = $dbi->query("select * from table where name = ?,
|
|
| 436 |
+ title = ?;", 'taro', 'perl'); |
|
| 437 |
+ |
|
| 438 |
+ while (my @row = $result->fetch) {
|
|
| 439 |
+ # do something |
|
| 440 |
+ } |
|
| 441 |
+ |
|
| 289 | 442 |
=head2 run_tranzaction |
| 290 | 443 |
|
| 291 |
-=head2 valid_connect_info |
|
| 444 |
+ # Run tranzaction |
|
| 445 |
+ $dbi->run_tranzaction(sub {
|
|
| 446 |
+ # do something |
|
| 447 |
+ }); |
|
| 292 | 448 |
|
| 449 |
+If tranzaction is success, commit is execute. |
|
| 450 |
+If tranzation is died, rollback is execute. |
|
| 293 | 451 |
|
| 294 | 452 |
=head1 AUTHOR |
| 295 | 453 |
|
| ... | ... |
@@ -12,12 +12,10 @@ my $sql_tmpl3 = DBI::Custom::SQL::Template->new->upper_case(2); |
| 12 | 12 |
|
| 13 | 13 |
{
|
| 14 | 14 |
my $dbi = DBI::Custom->new( |
| 15 |
- connect_info => {
|
|
| 16 |
- user => 'a', |
|
| 17 |
- password => 'b', |
|
| 18 |
- data_source => 'c', |
|
| 19 |
- options => {d => 1, e => 2}
|
|
| 20 |
- }, |
|
| 15 |
+ user => 'a', |
|
| 16 |
+ password => 'b', |
|
| 17 |
+ data_source => 'c', |
|
| 18 |
+ dbi_option => {d => 1, e => 2},
|
|
| 21 | 19 |
filters => {
|
| 22 | 20 |
f => 3, |
| 23 | 21 |
}, |
| ... | ... |
@@ -25,12 +23,11 @@ my $sql_tmpl3 = DBI::Custom::SQL::Template->new->upper_case(2); |
| 25 | 23 |
fetch_filter => 'g', |
| 26 | 24 |
result_class => 'g', |
| 27 | 25 |
sql_template => $sql_tmpl1, |
| 28 |
- valid_connect_info => {i => 1}
|
|
| 29 | 26 |
); |
| 30 |
- is_deeply($dbi,{connect_info => {user => 'a', password => 'b', data_source => 'c',
|
|
| 31 |
- options => {d => 1, e => 2}}, filters => {f => 3}, bind_filter => 'f',
|
|
| 27 |
+ is_deeply($dbi,{user => 'a', password => 'b', data_source => 'c',
|
|
| 28 |
+ dbi_option => {d => 1, e => 2}, filters => {f => 3}, bind_filter => 'f',
|
|
| 32 | 29 |
fetch_filter => 'g', result_class => 'g', |
| 33 |
- sql_template => $sql_tmpl1, valid_connect_info => {i => 1}}, 'new');
|
|
| 30 |
+ sql_template => $sql_tmpl1}, 'new'); |
|
| 34 | 31 |
|
| 35 | 32 |
isa_ok($dbi, 'DBI::Custom'); |
| 36 | 33 |
} |
| ... | ... |
@@ -43,12 +40,10 @@ my $sql_tmpl3 = DBI::Custom::SQL::Template->new->upper_case(2); |
| 43 | 40 |
my $class = __PACKAGE__; |
| 44 | 41 |
|
| 45 | 42 |
$class |
| 46 |
- ->connect_info( |
|
| 47 |
- user => 'a', |
|
| 48 |
- password => 'b', |
|
| 49 |
- data_source => 'c', |
|
| 50 |
- options => {d => 1, e => 2}
|
|
| 51 |
- ) |
|
| 43 |
+ ->user('a')
|
|
| 44 |
+ ->password('b')
|
|
| 45 |
+ ->data_source('c')
|
|
| 46 |
+ ->dbi_option({d => 1, e => 2})
|
|
| 52 | 47 |
->filters( |
| 53 | 48 |
f => 3 |
| 54 | 49 |
) |
| ... | ... |
@@ -56,17 +51,14 @@ my $sql_tmpl3 = DBI::Custom::SQL::Template->new->upper_case(2); |
| 56 | 51 |
->fetch_filter('g')
|
| 57 | 52 |
->result_class('DBI::Custom::Result')
|
| 58 | 53 |
->sql_template($sql_tmpl1) |
| 59 |
- ->valid_connect_info({p => 1})
|
|
| 60 | 54 |
; |
| 61 | 55 |
} |
| 62 | 56 |
{
|
| 63 | 57 |
my $dbi = DBI::Custom::T1->new( |
| 64 |
- connect_info => {
|
|
| 65 |
- user => 'ao', |
|
| 66 |
- password => 'bo', |
|
| 67 |
- data_source => 'co', |
|
| 68 |
- options => {do => 10, eo => 20}
|
|
| 69 |
- }, |
|
| 58 |
+ user => 'ao', |
|
| 59 |
+ password => 'bo', |
|
| 60 |
+ data_source => 'co', |
|
| 61 |
+ dbi_option => {do => 10, eo => 20},
|
|
| 70 | 62 |
filters => {
|
| 71 | 63 |
fo => 30, |
| 72 | 64 |
}, |
| ... | ... |
@@ -74,13 +66,11 @@ my $sql_tmpl3 = DBI::Custom::SQL::Template->new->upper_case(2); |
| 74 | 66 |
fetch_filter => 'go', |
| 75 | 67 |
result_class => 'ho', |
| 76 | 68 |
sql_template => $sql_tmpl1, |
| 77 |
- valid_connect_info => {io => 1}
|
|
| 78 | 69 |
); |
| 79 | 70 |
my $sql_tmpl = delete $dbi->{sql_template};
|
| 80 | 71 |
is($sql_tmpl->upper_case, 0); |
| 81 |
- is_deeply($dbi,{connect_info => {user => 'ao', password => 'bo', data_source => 'co', options => {do => 10, eo => 20}}
|
|
| 72 |
+ is_deeply($dbi,{ user => 'ao', password => 'bo', data_source => 'co', dbi_option => {do => 10, eo => 20},
|
|
| 82 | 73 |
,filters => {fo => 30}, bind_filter => 'fo', fetch_filter => 'go', result_class => 'ho',
|
| 83 |
- ,valid_connect_info => {io => 1}
|
|
| 84 | 74 |
}, 'new arguments'); |
| 85 | 75 |
|
| 86 | 76 |
isa_ok($dbi, 'DBI::Custom::T1'); |
| ... | ... |
@@ -89,12 +79,14 @@ my $sql_tmpl3 = DBI::Custom::SQL::Template->new->upper_case(2); |
| 89 | 79 |
{
|
| 90 | 80 |
my $dbi = DBI::Custom::T1->new; |
| 91 | 81 |
|
| 92 |
- is_deeply($dbi->connect_info, {user => 'a', password => 'b', data_source => 'c', options => {d => 1, e => 2}});
|
|
| 82 |
+ is($dbi->user, 'a'); |
|
| 83 |
+ is($dbi->password, 'b'); |
|
| 84 |
+ is($dbi->data_source, 'c'); |
|
| 85 |
+ is_deeply($dbi->dbi_option, {d => 1, e => 2});
|
|
| 93 | 86 |
is_deeply({$dbi->filters}, {f => 3});
|
| 94 | 87 |
is($dbi->bind_filter, 'f'); |
| 95 | 88 |
is($dbi->fetch_filter, 'g'); |
| 96 | 89 |
is($dbi->result_class, 'DBI::Custom::Result'); |
| 97 |
- is_deeply({$dbi->valid_connect_info},{p => 1});
|
|
| 98 | 90 |
is($dbi->sql_template->upper_case, 0); |
| 99 | 91 |
isa_ok($dbi, 'DBI::Custom::T1'); |
| 100 | 92 |
|
| ... | ... |
@@ -108,12 +100,14 @@ my $sql_tmpl3 = DBI::Custom::SQL::Template->new->upper_case(2); |
| 108 | 100 |
{
|
| 109 | 101 |
my $dbi = DBI::Custom::T1_2->new; |
| 110 | 102 |
|
| 111 |
- is_deeply($dbi->connect_info, {user => 'a', password => 'b', data_source => 'c', options => {d => 1, e => 2}});
|
|
| 103 |
+ is($dbi->user, 'a'); |
|
| 104 |
+ is($dbi->password, 'b'); |
|
| 105 |
+ is($dbi->data_source, 'c'); |
|
| 106 |
+ is_deeply($dbi->dbi_option, {d => 1, e => 2});
|
|
| 112 | 107 |
is_deeply(scalar $dbi->filters, {f => 3});
|
| 113 | 108 |
is($dbi->bind_filter, 'f'); |
| 114 | 109 |
is($dbi->fetch_filter, 'g'); |
| 115 | 110 |
is($dbi->result_class, 'DBI::Custom::Result'); |
| 116 |
- is_deeply({$dbi->valid_connect_info}, {p => 1});
|
|
| 117 | 111 |
is($dbi->sql_template->upper_case, 0); |
| 118 | 112 |
|
| 119 | 113 |
isa_ok($dbi, 'DBI::Custom::T1_2'); |
| ... | ... |
@@ -126,12 +120,10 @@ my $sql_tmpl3 = DBI::Custom::SQL::Template->new->upper_case(2); |
| 126 | 120 |
my $class = __PACKAGE__; |
| 127 | 121 |
|
| 128 | 122 |
$class |
| 129 |
- ->connect_info( |
|
| 130 |
- user => 'ao', |
|
| 131 |
- password => 'bo', |
|
| 132 |
- data_source => 'co', |
|
| 133 |
- options => {do => 10, eo => 20}
|
|
| 134 |
- ) |
|
| 123 |
+ ->user('ao')
|
|
| 124 |
+ ->password('bo')
|
|
| 125 |
+ ->data_source('co')
|
|
| 126 |
+ ->dbi_option({do => 10, eo => 20})
|
|
| 135 | 127 |
->filters( |
| 136 | 128 |
fo => 30 |
| 137 | 129 |
) |
| ... | ... |
@@ -139,19 +131,20 @@ my $sql_tmpl3 = DBI::Custom::SQL::Template->new->upper_case(2); |
| 139 | 131 |
->fetch_filter('go')
|
| 140 | 132 |
->result_class('ho')
|
| 141 | 133 |
->sql_template($sql_tmpl2) |
| 142 |
- ->valid_connect_info({p => 3})
|
|
| 143 | 134 |
; |
| 144 | 135 |
} |
| 145 | 136 |
|
| 146 | 137 |
{
|
| 147 | 138 |
my $dbi = DBI::Custom::T1_3->new; |
| 148 | 139 |
|
| 149 |
- is_deeply($dbi->connect_info, {user => 'ao', password => 'bo', data_source => 'co', options => {do => 10, eo => 20}});
|
|
| 140 |
+ is($dbi->user, 'ao'); |
|
| 141 |
+ is($dbi->password, 'bo'); |
|
| 142 |
+ is($dbi->data_source, 'co'); |
|
| 143 |
+ is_deeply($dbi->dbi_option, {do => 10, eo => 20});
|
|
| 150 | 144 |
is_deeply(scalar $dbi->filters, {fo => 30});
|
| 151 | 145 |
is($dbi->bind_filter, 'fo'); |
| 152 | 146 |
is($dbi->fetch_filter, 'go'); |
| 153 | 147 |
is($dbi->result_class, 'ho'); |
| 154 |
- is_deeply(scalar $dbi->valid_connect_info, {p => 3});
|
|
| 155 | 148 |
is($dbi->sql_template->upper_case, 1); |
| 156 | 149 |
|
| 157 | 150 |
isa_ok($dbi, 'DBI::Custom::T1_3'); |
| ... | ... |
@@ -159,12 +152,10 @@ my $sql_tmpl3 = DBI::Custom::SQL::Template->new->upper_case(2); |
| 159 | 152 |
|
| 160 | 153 |
{
|
| 161 | 154 |
my $dbi = DBI::Custom::T1_3->new( |
| 162 |
- connect_info => {
|
|
| 163 |
- user => 'a', |
|
| 164 |
- password => 'b', |
|
| 165 |
- data_source => 'c', |
|
| 166 |
- options => {d => 1, e => 2}
|
|
| 167 |
- }, |
|
| 155 |
+ user => 'a', |
|
| 156 |
+ password => 'b', |
|
| 157 |
+ data_source => 'c', |
|
| 158 |
+ dbi_option => {d => 1, e => 2},
|
|
| 168 | 159 |
filters => {
|
| 169 | 160 |
f => 3, |
| 170 | 161 |
}, |
| ... | ... |
@@ -172,35 +163,26 @@ my $sql_tmpl3 = DBI::Custom::SQL::Template->new->upper_case(2); |
| 172 | 163 |
fetch_filter => 'g', |
| 173 | 164 |
result_class => 'h', |
| 174 | 165 |
sql_template => $sql_tmpl3, |
| 175 |
- valid_connect_info => {p => 4}
|
|
| 176 | 166 |
); |
| 177 | 167 |
|
| 178 |
- my $sql_tmpl = delete $dbi->{sql_template};
|
|
| 179 |
- is($sql_tmpl->upper_case, 2); |
|
| 180 |
- is_deeply($dbi,{connect_info => {user => 'a', password => 'b', data_source => 'c', options => {d => 1, e => 2}},
|
|
| 181 |
- filters => {f => 3}, bind_filter => 'f', fetch_filter => 'g', result_class => 'h',
|
|
| 182 |
- valid_connect_info => {p => 4}}, 'new');
|
|
| 168 |
+ is($dbi->user, 'a'); |
|
| 169 |
+ is($dbi->password, 'b'); |
|
| 170 |
+ is($dbi->data_source, 'c'); |
|
| 171 |
+ is_deeply($dbi->dbi_option, {d => 1, e => 2});
|
|
| 172 |
+ is_deeply({$dbi->filters}, {f => 3});
|
|
| 173 |
+ is($dbi->bind_filter, 'f'); |
|
| 174 |
+ is($dbi->fetch_filter, 'g'); |
|
| 175 |
+ is($dbi->result_class, 'h'); |
|
| 176 |
+ is($dbi->sql_template->upper_case, 2); |
|
| 183 | 177 |
|
| 184 | 178 |
isa_ok($dbi, 'DBI::Custom'); |
| 185 | 179 |
} |
| 186 | 180 |
|
| 187 |
- |
|
| 188 |
-{
|
|
| 189 |
- my $dbi = DBI::Custom->new( |
|
| 190 |
- connect_info => {
|
|
| 191 |
- no_exist => 1, |
|
| 192 |
- } |
|
| 193 |
- ); |
|
| 194 |
- eval{$dbi->connect};
|
|
| 195 |
- |
|
| 196 |
- like($@, qr/connect_info 'no_exist' is wrong name/, 'no exist'); |
|
| 197 |
-} |
|
| 198 |
- |
|
| 199 | 181 |
{
|
| 200 | 182 |
my $dbi = DBI::Custom->new; |
| 201 | 183 |
my $tmpl = "select * from table where {= title};";
|
| 202 | 184 |
my $values = {title => 'a'};
|
| 203 |
- my ($sql, @bind) = $dbi->create_sql($tmpl, $values); |
|
| 185 |
+ my ($sql, @bind) = $dbi->_create_sql($tmpl, $values); |
|
| 204 | 186 |
is($sql, "select * from table where title = ?;", 'sql template'); |
| 205 | 187 |
is_deeply(\@bind, ['a'], 'sql template bind' ); |
| 206 | 188 |
} |
| ... | ... |
@@ -219,7 +201,7 @@ my $sql_tmpl3 = DBI::Custom::SQL::Template->new->upper_case(2); |
| 219 | 201 |
return $value; |
| 220 | 202 |
}); |
| 221 | 203 |
|
| 222 |
- my ($sql, @bind) = $dbi->create_sql($tmpl, $values, $dbi->filters->{filter});
|
|
| 204 |
+ my ($sql, @bind) = $dbi->_create_sql($tmpl, $values, $dbi->filters->{filter});
|
|
| 223 | 205 |
|
| 224 | 206 |
is($sql, "select * from table where k1 = ? && k2 <> ? && k3 < ? && k4 > ? && k5 >= ? && k6 <= ? && k7 like ?;", 'sql template2'); |
| 225 | 207 |
is_deeply(\@bind, ['A', 'b', 'c', 'd', 'e', 'f', 'g'], 'sql template bind2' ); |
| ... | ... |
@@ -232,7 +214,7 @@ my $sql_tmpl3 = DBI::Custom::SQL::Template->new->upper_case(2); |
| 232 | 214 |
my $tmpl = "select * from table where {like k7}";
|
| 233 | 215 |
my $values = {k7 => 'g'};
|
| 234 | 216 |
|
| 235 |
- my ($sql, @bind) = $dbi->create_sql($tmpl, $values); |
|
| 217 |
+ my ($sql, @bind) = $dbi->_create_sql($tmpl, $values); |
|
| 236 | 218 |
is($sql, "select * from table where k7 LIKE ?;", 'sql template2'); |
| 237 | 219 |
is_deeply(\@bind, ['g'], 'sql template bind2' ); |
| 238 | 220 |
} |
| ... | ... |
@@ -252,7 +234,7 @@ my $sql_tmpl3 = DBI::Custom::SQL::Template->new->upper_case(2); |
| 252 | 234 |
return $value; |
| 253 | 235 |
}); |
| 254 | 236 |
|
| 255 |
- my ($sql, @bind) = $dbi->create_sql($tmpl, $values, $dbi->filters->{filter});
|
|
| 237 |
+ my ($sql, @bind) = $dbi->_create_sql($tmpl, $values, $dbi->filters->{filter});
|
|
| 256 | 238 |
is($sql, "insert into table (k1, k2) values (?, ?);"); |
| 257 | 239 |
is_deeply(\@bind, ['A', 'b'], 'sql template bind' ); |
| 258 | 240 |
} |
| ... | ... |
@@ -271,7 +253,7 @@ my $sql_tmpl3 = DBI::Custom::SQL::Template->new->upper_case(2); |
| 271 | 253 |
return $value; |
| 272 | 254 |
}); |
| 273 | 255 |
|
| 274 |
- my ($sql, @bind) = $dbi->create_sql($tmpl, $values, $dbi->filters->{filter});
|
|
| 256 |
+ my ($sql, @bind) = $dbi->_create_sql($tmpl, $values, $dbi->filters->{filter});
|
|
| 275 | 257 |
is($sql, "update table set k1 = ?, k2 = ?;"); |
| 276 | 258 |
is_deeply(\@bind, ['A', 'b'], 'sql template bind' ); |
| 277 | 259 |
} |
| ... | ... |
@@ -20,7 +20,7 @@ sub dbi : Attr {}
|
| 20 | 20 |
|
| 21 | 21 |
sub new {
|
| 22 | 22 |
my $self = shift->SUPER::new; |
| 23 |
- my $dbi = DBI::Custom->new->connect_info(data_source => 'dbi:SQLite:dbname=:memory:'); |
|
| 23 |
+ my $dbi = DBI::Custom->new->data_source('dbi:SQLite:dbname=:memory:');
|
|
| 24 | 24 |
|
| 25 | 25 |
$dbi->connect; |
| 26 | 26 |
$self->dbi($dbi); |
| ... | ... |
@@ -13,11 +13,9 @@ use DBI::Custom; |
| 13 | 13 |
use Scalar::Util 'blessed'; |
| 14 | 14 |
{
|
| 15 | 15 |
my $dbi = DBI::Custom->new( |
| 16 |
- connect_info => {
|
|
| 17 |
- user => $U, |
|
| 18 |
- password => $P, |
|
| 19 |
- data_source => "dbi:mysql:$D" |
|
| 20 |
- } |
|
| 16 |
+ user => $U, |
|
| 17 |
+ password => $P, |
|
| 18 |
+ data_source => "dbi:mysql:dbname=$D" |
|
| 21 | 19 |
); |
| 22 | 20 |
$dbi->connect; |
| 23 | 21 |
|