... | ... |
@@ -1,3 +1,6 @@ |
1 |
+0.1657 |
|
2 |
+ - remaned experimental safty_charcter to safty_name |
|
3 |
+ - safty_charcter is changed, set only one character regex. |
|
1 | 4 |
0.1656 |
2 | 5 |
- fixed some select() join opition bug |
3 | 6 |
0.1655 |
... | ... |
@@ -31,7 +31,7 @@ __PACKAGE__->attr( |
31 | 31 |
models => sub { {} }, |
32 | 32 |
query_builder => sub { DBIx::Custom::QueryBuilder->new }, |
33 | 33 |
result_class => 'DBIx::Custom::Result', |
34 |
- safety_column_name => sub { qr/^[\w\.]*$/ }, |
|
34 |
+ safety_character => '\w', |
|
35 | 35 |
stash => sub { {} } |
36 | 36 |
); |
37 | 37 |
|
... | ... |
@@ -464,10 +464,10 @@ sub insert { |
464 | 464 |
|
465 | 465 |
# Columns |
466 | 466 |
my @columns; |
467 |
- my $safety = $self->safety_column_name; |
|
467 |
+ my $safety = $self->safety_character; |
|
468 | 468 |
foreach my $column (keys %$param) { |
469 | 469 |
croak qq{"$column" is not safety column name} |
470 |
- unless $column =~ /$safety/; |
|
470 |
+ unless $column =~ /^[$safety\.]+$/; |
|
471 | 471 |
push @columns, $column; |
472 | 472 |
} |
473 | 473 |
|
... | ... |
@@ -548,10 +548,10 @@ sub insert_param { |
548 | 548 |
# Insert paramter tag |
549 | 549 |
my @tag; |
550 | 550 |
push @tag, '{insert_param'; |
551 |
- my $safety = $self->safety_column_name; |
|
551 |
+ my $safety = $self->safety_character; |
|
552 | 552 |
foreach my $column (keys %$param) { |
553 | 553 |
croak qq{"$column" is not safety column name} |
554 |
- unless $column =~ /$safety/; |
|
554 |
+ unless $column =~ /^[$safety\.]+$/; |
|
555 | 555 |
push @tag, $column; |
556 | 556 |
} |
557 | 557 |
push @tag, '}'; |
... | ... |
@@ -921,10 +921,10 @@ sub update { |
921 | 921 |
|
922 | 922 |
# Columns |
923 | 923 |
my @columns; |
924 |
- my $safety = $self->safety_column_name; |
|
924 |
+ my $safety = $self->safety_character; |
|
925 | 925 |
foreach my $column (keys %$param) { |
926 | 926 |
croak qq{"$column" is not safety column name} |
927 |
- unless $column =~ /$safety/; |
|
927 |
+ unless $column =~ /^[$safety\.]+$/; |
|
928 | 928 |
push @columns, $column; |
929 | 929 |
} |
930 | 930 |
|
... | ... |
@@ -1042,10 +1042,10 @@ sub update_param { |
1042 | 1042 |
# Update parameter tag |
1043 | 1043 |
my @tag; |
1044 | 1044 |
push @tag, '{update_param'; |
1045 |
- my $safety = $self->safety_column_name; |
|
1045 |
+ my $safety = $self->safety_character; |
|
1046 | 1046 |
foreach my $column (keys %$param) { |
1047 | 1047 |
croak qq{"$column" is not safety column name} |
1048 |
- unless $column =~ /$safety/; |
|
1048 |
+ unless $column =~ /^[$safety\.]+$/; |
|
1049 | 1049 |
push @tag, $column; |
1050 | 1050 |
} |
1051 | 1051 |
push @tag, '}'; |
... | ... |
@@ -1058,7 +1058,7 @@ sub where { |
1058 | 1058 |
|
1059 | 1059 |
return DBIx::Custom::Where->new( |
1060 | 1060 |
query_builder => $self->query_builder, |
1061 |
- safety_column_name => $self->safety_column_name, |
|
1061 |
+ safety_character => $self->safety_character, |
|
1062 | 1062 |
@_ |
1063 | 1063 |
); |
1064 | 1064 |
} |
... | ... |
@@ -1130,9 +1130,9 @@ sub _tables { |
1130 | 1130 |
|
1131 | 1131 |
my $tables = []; |
1132 | 1132 |
|
1133 |
- my $safety_name = $self->safety_column_name; |
|
1133 |
+ my $safety_character = $self->safety_character; |
|
1134 | 1134 |
|
1135 |
- while ($source =~ /\b(\w+)\./g) { |
|
1135 |
+ while ($source =~ /\b($safety_character+)\./g) { |
|
1136 | 1136 |
push @$tables, $1; |
1137 | 1137 |
} |
1138 | 1138 |
|
... | ... |
@@ -1272,107 +1272,118 @@ sub _add_relation_table { |
1272 | 1272 |
|
1273 | 1273 |
=head1 NAME |
1274 | 1274 |
|
1275 |
-DBIx::Custom - DBI interface, having hash parameter binding and filtering system |
|
1275 |
+DBIx::Custom - Useful database access, respecting SQL! |
|
1276 | 1276 |
|
1277 | 1277 |
=head1 SYNOPSYS |
1278 | 1278 |
|
1279 | 1279 |
use DBIx::Custom; |
1280 |
- my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=dbname", |
|
1281 |
- user => 'ken', password => '!LFKD%$&', |
|
1282 |
- dbi_option => {mysql_enable_utf8 => 1}); |
|
1280 |
+ |
|
1281 |
+ # Connect |
|
1282 |
+ my $dbi = DBIx::Custom->connect( |
|
1283 |
+ data_source => "dbi:mysql:database=dbname", |
|
1284 |
+ user => 'ken', |
|
1285 |
+ password => '!LFKD%$&', |
|
1286 |
+ dbi_option => {mysql_enable_utf8 => 1} |
|
1287 |
+ ); |
|
1283 | 1288 |
|
1284 | 1289 |
# Insert |
1285 |
- $dbi->insert(table => 'book', |
|
1286 |
- param => {title => 'Perl', author => 'Ken'}, |
|
1287 |
- filter => [title => 'to_something']); |
|
1290 |
+ $dbi->insert( |
|
1291 |
+ table => 'book', |
|
1292 |
+ param => {title => 'Perl', author => 'Ken'} |
|
1293 |
+ ); |
|
1288 | 1294 |
|
1289 | 1295 |
# Update |
1290 |
- $dbi->update(table => 'book', |
|
1291 |
- param => {title => 'Perl', author => 'Ken'}, |
|
1292 |
- where => {id => 5}, |
|
1293 |
- filter => [title => 'to_something']); |
|
1294 |
- |
|
1295 |
- # Update all |
|
1296 |
- $dbi->update_all(table => 'book', |
|
1297 |
- param => {title => 'Perl'}, |
|
1298 |
- filter => [title => 'to_something']); |
|
1296 |
+ $dbi->update( |
|
1297 |
+ table => 'book', |
|
1298 |
+ param => {title => 'Perl', author => 'Ken'}, |
|
1299 |
+ where => {id => 5}, |
|
1300 |
+ ); |
|
1299 | 1301 |
|
1300 | 1302 |
# Delete |
1301 |
- $dbi->delete(table => 'book', |
|
1302 |
- where => {author => 'Ken'}, |
|
1303 |
- filter => [title => 'to_something']); |
|
1304 |
- |
|
1305 |
- # Delete all |
|
1306 |
- $dbi->delete_all(table => 'book'); |
|
1303 |
+ $dbi->delete( |
|
1304 |
+ table => 'book', |
|
1305 |
+ where => {author => 'Ken'}, |
|
1306 |
+ ); |
|
1307 | 1307 |
|
1308 | 1308 |
# Select |
1309 | 1309 |
my $result = $dbi->select( |
1310 | 1310 |
table => 'book', |
1311 |
- column => [qw/author title/], |
|
1312 | 1311 |
where => {author => 'Ken'}, |
1313 |
- relation => {'book.id' => 'rental.book_id'}, |
|
1314 |
- append => 'order by id limit 5', |
|
1315 |
- filter => [title => 'to_something'] |
|
1316 | 1312 |
); |
1317 | 1313 |
|
1318 |
- # Execute SQL |
|
1319 |
- $dbi->execute("select title from book"); |
|
1320 |
- |
|
1321 |
- # Execute SQL with hash binding and filtering |
|
1322 |
- $dbi->execute("select id from book where {= author} and {like title}", |
|
1323 |
- param => {author => 'ken', title => '%Perl%'}, |
|
1324 |
- filter => [title => 'to_something']); |
|
1325 |
- |
|
1326 |
- # Create query and execute it |
|
1327 |
- my $query = $dbi->create_query( |
|
1328 |
- "select id from book where {= author} and {like title}" |
|
1314 |
+ # Select, more complex |
|
1315 |
+ my $result = $dbi->select( |
|
1316 |
+ table => 'book', |
|
1317 |
+ column => [ |
|
1318 |
+ 'book.author as book__author', |
|
1319 |
+ 'company.name as company__name' |
|
1320 |
+ ], |
|
1321 |
+ where => {'book.author' => 'Ken'}, |
|
1322 |
+ join => ['left outer join company on book.company_id = company.id'], |
|
1323 |
+ append => 'order by id limit 5' |
|
1329 | 1324 |
); |
1330 |
- $dbi->execute($query, param => {author => 'Ken', title => '%Perl%'}) |
|
1331 |
- |
|
1332 |
- # Get DBI object |
|
1333 |
- my $dbh = $dbi->dbh; |
|
1334 |
- |
|
1325 |
+ |
|
1335 | 1326 |
# Fetch |
1336 | 1327 |
while (my $row = $result->fetch) { |
1337 |
- # ... |
|
1328 |
+ |
|
1338 | 1329 |
} |
1339 | 1330 |
|
1340 |
- # Fetch hash |
|
1331 |
+ # Fetch as hash |
|
1341 | 1332 |
while (my $row = $result->fetch_hash) { |
1342 | 1333 |
|
1343 | 1334 |
} |
1344 | 1335 |
|
1336 |
+ # Execute SQL with parameter. |
|
1337 |
+ $dbi->execute( |
|
1338 |
+ "select id from book where {= author} and {like title}", |
|
1339 |
+ param => {author => 'ken', title => '%Perl%'} |
|
1340 |
+ ); |
|
1341 |
+ |
|
1345 | 1342 |
=head1 DESCRIPTIONS |
1346 | 1343 |
|
1347 |
-L<DBIx::Custom> is one of L<DBI> interface modules, |
|
1348 |
-such as L<DBIx::Class>, L<DBIx::Simple>. |
|
1344 |
+L<DBIx::Custom> is L<DBI> wrapper module. |
|
1349 | 1345 |
|
1350 |
-This module is not O/R mapper. O/R mapper is useful, |
|
1351 |
-but you must learn many syntax of the O/R mapper, |
|
1352 |
-which is almost another language. |
|
1353 |
-Created SQL statement is offten not effcient and damage SQL performance. |
|
1354 |
-so you have to execute raw SQL in the end. |
|
1346 |
+=head1 FEATURES |
|
1355 | 1347 |
|
1356 |
-L<DBIx::Custom> is middle area between L<DBI> and O/R mapper. |
|
1357 |
-L<DBIx::Custom> provide flexible hash parameter binding and filtering system, |
|
1358 |
-and suger methods, such as C<insert()>, C<update()>, C<delete()>, C<select()> |
|
1359 |
-to execute SQL easily. |
|
1348 |
+=over 4 |
|
1360 | 1349 |
|
1361 |
-L<DBIx::Custom> respects SQL. SQL is very complex and not beautiful, |
|
1362 |
-but de-facto standard, |
|
1363 |
-so all people learing database know it. |
|
1364 |
-If you already know SQL, |
|
1365 |
-you learn a little thing to use L<DBIx::Custom>. |
|
1350 |
+=item * |
|
1351 |
+ |
|
1352 |
+There are many basic methods to execute various queries. |
|
1353 |
+C<insert()>, C<update()>, C<update_all()>,C<delete()>, |
|
1354 |
+C<delete_all()>, C<select()>, |
|
1355 |
+C<insert_at()>, C<update_at()>, |
|
1356 |
+C<delete_at()>, C<select_at()>, C<execute()> |
|
1366 | 1357 |
|
1367 |
-See L<DBIx::Custom::Guide> for more details. |
|
1358 |
+=item * |
|
1359 |
+ |
|
1360 |
+Filter when data is send or receive. |
|
1361 |
+ |
|
1362 |
+=item * |
|
1363 |
+ |
|
1364 |
+Data filtering system |
|
1365 |
+ |
|
1366 |
+=item * |
|
1367 |
+ |
|
1368 |
+Model support. |
|
1369 |
+ |
|
1370 |
+=item * |
|
1371 |
+ |
|
1372 |
+Generate where clause dinamically. |
|
1373 |
+ |
|
1374 |
+=item * |
|
1375 |
+ |
|
1376 |
+Generate join clause dinamically. |
|
1377 |
+ |
|
1378 |
+=back |
|
1368 | 1379 |
|
1369 | 1380 |
=head1 GUIDE |
1370 | 1381 |
|
1371 |
-L<DBIx::Custom::Guide> - L<DBIx::Custom> complete guide |
|
1382 |
+L<DBIx::Custom::Guide> - L<DBIx::Custom> Guide |
|
1372 | 1383 |
|
1373 |
-=head1 EXAMPLES |
|
1384 |
+=head1 Wiki |
|
1374 | 1385 |
|
1375 |
-L<DBIx::Custom Wiki|https://github.com/yuki-kimoto/DBIx-Custom/wiki> - Many useful examples |
|
1386 |
+L<DBIx::Custom Wiki|https://github.com/yuki-kimoto/DBIx-Custom/wiki> |
|
1376 | 1387 |
|
1377 | 1388 |
=head1 ATTRIBUTES |
1378 | 1389 |
|
... | ... |
@@ -1381,97 +1392,86 @@ L<DBIx::Custom Wiki|https://github.com/yuki-kimoto/DBIx-Custom/wiki> - Many usef |
1381 | 1392 |
my $cache = $dbi->cache; |
1382 | 1393 |
$dbi = $dbi->cache(1); |
1383 | 1394 |
|
1384 |
-Enable parsed L<DBIx::Custom::Query> object caching. |
|
1385 |
-Default to 1. |
|
1395 |
+Enable caching of L<DBIx::Custom::Query>, |
|
1396 |
+default to 1. |
|
1386 | 1397 |
|
1387 | 1398 |
=head2 C<data_source> |
1388 | 1399 |
|
1389 | 1400 |
my $data_source = $dbi->data_source; |
1390 | 1401 |
$dbi = $dbi->data_source("DBI:mysql:database=dbname"); |
1391 | 1402 |
|
1392 |
-Data source. |
|
1393 |
-C<connect()> method use this value to connect the database. |
|
1403 |
+Data source, used when C<connect()> is executed. |
|
1394 | 1404 |
|
1395 | 1405 |
=head2 C<dbh> |
1396 | 1406 |
|
1397 | 1407 |
my $dbh = $dbi->dbh; |
1398 | 1408 |
$dbi = $dbi->dbh($dbh); |
1399 | 1409 |
|
1400 |
-L<DBI> object. You can call all methods of L<DBI>. |
|
1410 |
+Database handle of L<DBI>. |
|
1401 | 1411 |
|
1402 | 1412 |
=head2 C<dbi_option> |
1403 | 1413 |
|
1404 | 1414 |
my $dbi_option = $dbi->dbi_option; |
1405 |
- $dbi = $dbi->dbi_option($dbi_option); |
|
1406 |
- |
|
1407 |
-DBI options. |
|
1408 |
- |
|
1409 |
-Each option specified can ovewrite C<default_dbi_option>. |
|
1410 |
- |
|
1411 |
-C<connect()> method use this value to connect the database. |
|
1415 |
+ $dbi = $dbi->dbi_option($dbi_option); |
|
1412 | 1416 |
|
1417 |
+L<DBI> option, used when C<connect()> is executed. |
|
1418 |
+Each value in option override the value of C<default_dbi_option>. |
|
1413 | 1419 |
|
1414 | 1420 |
=head2 C<default_dbi_option> |
1415 | 1421 |
|
1416 | 1422 |
my $default_dbi_option = $dbi->default_dbi_option; |
1417 | 1423 |
$dbi = $dbi->default_dbi_option($default_dbi_option); |
1418 | 1424 |
|
1419 |
-DBI default options. |
|
1420 |
- |
|
1421 |
- RaiseError => 1, |
|
1422 |
- PrintError => 0, |
|
1423 |
- AutoCommit => 1, |
|
1425 |
+L<DBI> default option, used when C<connect()> is executed, |
|
1426 |
+default to the following values. |
|
1424 | 1427 |
|
1425 |
-C<connect()> method use this value to connect the database. |
|
1426 |
- |
|
1427 |
-Default filter when row is fetched. |
|
1428 |
+ { |
|
1429 |
+ RaiseError => 1, |
|
1430 |
+ PrintError => 0, |
|
1431 |
+ AutoCommit => 1, |
|
1432 |
+ } |
|
1428 | 1433 |
|
1429 | 1434 |
=head2 C<filters> |
1430 | 1435 |
|
1431 | 1436 |
my $filters = $dbi->filters; |
1432 | 1437 |
$dbi = $dbi->filters(\%filters); |
1433 | 1438 |
|
1434 |
-Filters |
|
1439 |
+Filters, registered by C<register_filter()>. |
|
1435 | 1440 |
|
1436 | 1441 |
=head2 C<(experimental) models> |
1437 | 1442 |
|
1438 | 1443 |
my $models = $dbi->models; |
1439 | 1444 |
$dbi = $dbi->models(\%models); |
1440 | 1445 |
|
1441 |
-Models |
|
1446 |
+Models, included by C<include_model()>. |
|
1442 | 1447 |
|
1443 | 1448 |
=head2 C<password> |
1444 | 1449 |
|
1445 | 1450 |
my $password = $dbi->password; |
1446 | 1451 |
$dbi = $dbi->password('lkj&le`@s'); |
1447 | 1452 |
|
1448 |
-Password. |
|
1449 |
-C<connect()> method use this value to connect the database. |
|
1453 |
+Password, used when C<connect()> is executed. |
|
1450 | 1454 |
|
1451 | 1455 |
=head2 C<query_builder> |
1452 | 1456 |
|
1453 | 1457 |
my $sql_class = $dbi->query_builder; |
1454 | 1458 |
$dbi = $dbi->query_builder(DBIx::Custom::QueryBuilder->new); |
1455 | 1459 |
|
1456 |
-SQL builder. C<query_builder()> must be |
|
1457 |
-the instance of L<DBIx::Custom::QueryBuilder> subclass. |
|
1458 |
-Default to L<DBIx::Custom::QueryBuilder> object. |
|
1460 |
+Query builder, default to L<DBIx::Custom::QueryBuilder> object. |
|
1459 | 1461 |
|
1460 | 1462 |
=head2 C<result_class> |
1461 | 1463 |
|
1462 | 1464 |
my $result_class = $dbi->result_class; |
1463 | 1465 |
$dbi = $dbi->result_class('DBIx::Custom::Result'); |
1464 | 1466 |
|
1465 |
-Result class for select statement. |
|
1466 |
-Default to L<DBIx::Custom::Result>. |
|
1467 |
+Result class, default to L<DBIx::Custom::Result>. |
|
1467 | 1468 |
|
1468 |
-=head2 C<(experimental) safety_column_name> |
|
1469 |
+=head2 C<(experimental) safety_character> |
|
1469 | 1470 |
|
1470 |
- my $safety_column_name = $self->safety_column_name; |
|
1471 |
- $dbi = $self->safety_column_name($name); |
|
1471 |
+ my $safety_character = $self->safety_character; |
|
1472 | 1472 |
|
1473 |
-Safety column name regex. |
|
1474 |
-Default is qr/^[\w\.]*$/ |
|
1473 |
+Regex of safety character consist of table and column name, default to '\w'. |
|
1474 |
+Note that you don't have to specify like "[\w]". |
|
1475 | 1475 |
|
1476 | 1476 |
=head2 C<user> |
1477 | 1477 |
|
... | ... |
@@ -14,7 +14,7 @@ use Carp 'croak'; |
14 | 14 |
push @DBIx::Custom::CARP_NOT, __PACKAGE__; |
15 | 15 |
|
16 | 16 |
__PACKAGE__->attr( |
17 |
- [qw/param query_builder safety_column_name/], |
|
17 |
+ [qw/param query_builder safety_character/], |
|
18 | 18 |
clause => sub { [] }, |
19 | 19 |
); |
20 | 20 |
|
... | ... |
@@ -75,9 +75,9 @@ sub _parse { |
75 | 75 |
croak qq{Each tag contains one column name: tag "$clause"} |
76 | 76 |
unless @$columns == 1; |
77 | 77 |
my $column = $columns->[0]; |
78 |
- my $safety = $self->safety_column_name; |
|
78 |
+ my $safety = $self->safety_character; |
|
79 | 79 |
croak qq{"$column" is not safety column name} |
80 |
- unless $column =~ /$safety/; |
|
80 |
+ unless $column =~ /^[$safety\.]+$/; |
|
81 | 81 |
|
82 | 82 |
# Column count up |
83 | 83 |
my $count = ++$count->{$column}; |
... | ... |
@@ -138,10 +138,10 @@ If all parameter names is exists. |
138 | 138 |
date => ['2010-11-11', '2011-03-05']}, |
139 | 139 |
name => ['Ken', 'Taro']); |
140 | 140 |
|
141 |
-=head2 C<safety_column_name> |
|
141 |
+=head2 C<safety_character> |
|
142 | 142 |
|
143 |
- my $safety_column_name = $self->safety_column_name; |
|
144 |
- $dbi = $self->safety_column_name($name); |
|
143 |
+ my $safety_character = $self->safety_character; |
|
144 |
+ $dbi = $self->safety_character($name); |
|
145 | 145 |
|
146 | 146 |
=head1 METHODS |
147 | 147 |
|