| ... | ... |
@@ -1,3 +1,5 @@ |
| 1 |
+0.1690 |
|
| 2 |
+ - use latest Object::Simple features |
|
| 1 | 3 |
0.1689 |
| 2 | 4 |
- added EXPERIMENTAL available_data_type |
| 3 | 5 |
- simplified type_rule |
| ... | ... |
@@ -13,7 +13,7 @@ WriteMakefile( |
| 13 | 13 |
PL_FILES => {},
|
| 14 | 14 |
PREREQ_PM => {
|
| 15 | 15 |
'Test::More' => 0, |
| 16 |
- 'Object::Simple' => 3.0616, |
|
| 16 |
+ 'Object::Simple' => 3.0621, |
|
| 17 | 17 |
'DBD::SQLite' => '1.25', |
| 18 | 18 |
'DBI' => '1.605' |
| 19 | 19 |
}, |
| ... | ... |
@@ -1,12 +1,9 @@ |
| 1 | 1 |
package DBIx::Custom; |
| 2 | 2 |
|
| 3 |
-our $VERSION = '0.1689'; |
|
| 4 |
- |
|
| 3 |
+our $VERSION = '0.1690'; |
|
| 5 | 4 |
use 5.008001; |
| 6 |
-use strict; |
|
| 7 |
-use warnings; |
|
| 8 | 5 |
|
| 9 |
-use base 'Object::Simple'; |
|
| 6 |
+use Object::Simple -base; |
|
| 10 | 7 |
|
| 11 | 8 |
use Carp 'croak'; |
| 12 | 9 |
use DBI; |
| ... | ... |
@@ -24,8 +21,7 @@ use constant DEBUG_ENCODING => $ENV{DBIX_CUSTOM_DEBUG_ENCODING} || 'UTF-8';
|
| 24 | 21 |
|
| 25 | 22 |
our @COMMON_ARGS = qw/table query filter type id primary_key type_rule_off/; |
| 26 | 23 |
|
| 27 |
-__PACKAGE__->attr( |
|
| 28 |
- [qw/connector dsn password user/], |
|
| 24 |
+has [qw/connector dsn password user/], |
|
| 29 | 25 |
cache => 0, |
| 30 | 26 |
cache_method => sub {
|
| 31 | 27 |
sub {
|
| ... | ... |
@@ -60,8 +56,7 @@ __PACKAGE__->attr( |
| 60 | 56 |
result_class => 'DBIx::Custom::Result', |
| 61 | 57 |
reserved_word_quote => '', |
| 62 | 58 |
safety_character => '\w', |
| 63 |
- stash => sub { {} }
|
|
| 64 |
-); |
|
| 59 |
+ stash => sub { {} };
|
|
| 65 | 60 |
|
| 66 | 61 |
our $AUTOLOAD; |
| 67 | 62 |
sub AUTOLOAD {
|
| ... | ... |
@@ -4,297 +4,108 @@ |
| 4 | 4 |
|
| 5 | 5 |
DBIx::Custom::Guide - DBIx::Custom Guide |
| 6 | 6 |
|
| 7 |
-=head1 GUIDE |
|
| 8 |
- |
|
| 9 |
-L<DBIx::Custom> is the class to make easy to execute SQL. |
|
| 10 |
-This is L<DBI> wrapper class like L<DBIx::Class> or L<DBIx::Simple>. |
|
| 11 |
-You can do thing more easy than L<DBIx::Class>, more flexible |
|
| 12 |
-than L<DBIx::Simple>. |
|
| 13 |
- |
|
| 14 |
-L<DBIx::Custom> is not O/R mapper, O/R mapper is usefule, but |
|
| 15 |
-you must learn many things. Created SQL is sometimes inefficient, |
|
| 16 |
-and in many cases you create raw SQL because |
|
| 17 |
-O/R mapper can't make complex SQL |
|
| 18 |
- |
|
| 19 |
-L<DBIx::Custom> is opposit of O/R mapper. |
|
| 20 |
-The main purpose is that we respect SQL |
|
| 21 |
-and make easy difficult works if you use only L<DBI>. |
|
| 22 |
-If you already learn SQL, it is easy to use L<DBIx::Custom>. |
|
| 23 |
- |
|
| 24 |
-I explain L<DBIx::Custom> a little in this section. |
|
| 25 |
-In L<DBIx::Custom>, you embbed parameter in SQL. |
|
| 7 |
+=head1 FEATURES |
|
| 26 | 8 |
|
| 27 |
- select * from book where title = :title and author = :author; |
|
| 28 |
- |
|
| 29 |
-The part :title is parameter. |
|
| 30 |
-This SQL is converted to the one which contains place holder. |
|
| 31 |
- |
|
| 32 |
- select * from book where title = ? and author = ?; |
|
| 33 |
- |
|
| 34 |
-Maybe you ask me that this conversion is meaningful. |
|
| 35 |
-On the top of this, usuful features is implemented. |
|
| 36 |
-See the following descriptions. |
|
| 9 |
+L<DBIx::Custom> is the wrapper class of L<DBI> to execute SQL easily. |
|
| 10 |
+This module have the following features. |
|
| 37 | 11 |
|
| 38 | 12 |
=over 4 |
| 39 | 13 |
|
| 40 |
-=item 1. Specify place holder binding value as hash refernce |
|
| 41 |
- |
|
| 42 |
-If you use L<DBI>, you must specify place holder binding value |
|
| 43 |
-as array. |
|
| 44 |
- |
|
| 45 |
- $sth->execute(@bind); |
|
| 46 |
- |
|
| 47 |
-If you use L<DBIx::Custom>, you specify it as hash reference. |
|
| 48 |
- |
|
| 49 |
- my $param = {title => 'Perl', author => 'Ken'};
|
|
| 50 |
- $dbi->execute($sql, $param); |
|
| 51 |
- |
|
| 52 |
-=item 2. Filtering |
|
| 53 |
- |
|
| 54 |
-L<DBIx::Custom> provides filtering system. |
|
| 55 |
-For example, You think that about date value you want to |
|
| 56 |
-manipulate it as date object like L<Time::Piece> in Perl, |
|
| 57 |
-and want to convert it to database DATE format. |
|
| 58 |
-and want to do reverse. |
|
| 59 |
- |
|
| 60 |
-You can use filtering system. |
|
| 61 |
- |
|
| 62 |
-At first, register filter. |
|
| 63 |
- |
|
| 64 |
- $dbi->register_filter( |
|
| 65 |
- tp_to_date => sub {
|
|
| 66 |
- ... |
|
| 67 |
- }, |
|
| 68 |
- date_to_tp => sub {
|
|
| 69 |
- ... |
|
| 70 |
- } |
|
| 71 |
- ); |
|
| 72 |
- |
|
| 73 |
-next, apply this filter to each column. |
|
| 74 |
- |
|
| 75 |
- $dbi->apply_filter('book',
|
|
| 76 |
- 'issue_date' => {out => 'tp_to_date', in => 'date_to_tp'}
|
|
| 77 |
- ); |
|
| 78 |
- |
|
| 79 |
-C<out> is perl-to-database way. C<in> is perl-from-database way. |
|
| 80 |
- |
|
| 81 |
-This filter is automatically enabled in many method. |
|
| 14 |
+=item * Execute INSERT, UPDATE, DELETE, SELECT statement easily |
|
| 82 | 15 |
|
| 83 |
- $dbi->insert(table => 'book', param => {issue_date => $tp});
|
|
| 16 |
+=item * You can specify bind values by hash reference |
|
| 84 | 17 |
|
| 18 |
+=item * Filtering by data type. and you can set filter to any column |
|
| 85 | 19 |
|
| 86 |
-=item 3. Selective search condition |
|
| 20 |
+=item * Creating where clause flexibly |
|
| 87 | 21 |
|
| 88 |
-It is difficult to create selective where clause in L<DBI>. |
|
| 89 |
-For example, If C<title> and C<author> is specified, we create |
|
| 90 |
-the following SQL. |
|
| 91 |
- |
|
| 92 |
- select * from book where title = ? and author = ?; |
|
| 93 |
- |
|
| 94 |
-If only C<title> is specified, the following one |
|
| 95 |
- |
|
| 96 |
- select * from book where title = ?; |
|
| 97 |
- |
|
| 98 |
-If only C<author> is specified, the following one, |
|
| 99 |
- |
|
| 100 |
- select * from book where author = ?; |
|
| 101 |
- |
|
| 102 |
-This is hard work. Generally we use modules like L<SQL::Abstract>. |
|
| 103 |
-L<DBIx::Custom> prepare the way to make it easy. |
|
| 104 |
- |
|
| 105 |
- # Where object |
|
| 106 |
- my $where = $dbi->where; |
|
| 107 |
- |
|
| 108 |
- # Search condition |
|
| 109 |
- $where->clause( |
|
| 110 |
- ['and', 'title = :title', 'author = :author'] |
|
| 111 |
- ); |
|
| 112 |
- |
|
| 113 |
- # Setting to automatically select needed column |
|
| 114 |
- $where->param({title => 'Perl'});
|
|
| 115 |
- |
|
| 116 |
- # Embbed where clause to SQL |
|
| 117 |
- my $sql = "select * from book $where"; |
|
| 118 |
- |
|
| 119 |
-You can create where clause which has selected search condition. |
|
| 120 |
-You can write nesting of where clause and C<or> condition |
|
| 121 |
- |
|
| 122 |
-=item 4. Methods for insert, update, delete, select |
|
| 123 |
- |
|
| 124 |
-L<DBIx::Custom> provides methods for insert, update, delete, select |
|
| 125 |
-There are C<insert()>, C<update()>, C<delete()>,C<select()>. |
|
| 126 |
- |
|
| 127 |
- my $param = {title => 'Perl', author => 'Ken'};
|
|
| 128 |
- $dbi->insert(table => 'book', param => $param); |
|
| 129 |
- |
|
| 130 |
-=item 5. Register method for table. |
|
| 131 |
- |
|
| 132 |
-You can register method for table. |
|
| 133 |
- |
|
| 134 |
- $dbi->table('book')->method(
|
|
| 135 |
- list => sub {
|
|
| 136 |
- ... |
|
| 137 |
- }, |
|
| 138 |
- something => sub {
|
|
| 139 |
- ... |
|
| 140 |
- } |
|
| 141 |
- ); |
|
| 142 |
- |
|
| 143 |
-use the mehtod. |
|
| 144 |
- |
|
| 145 |
- $dbi->table('book')->list;
|
|
| 146 |
- |
|
| 147 |
-Many O/R mapper must create class for table, |
|
| 148 |
-but L<DBIx::Custom> make it easy. |
|
| 22 |
+=imte * Support model |
|
| 149 | 23 |
|
| 150 | 24 |
=back |
| 151 | 25 |
|
| 152 |
-L<DBIx::Custom> is very useful. |
|
| 153 |
-See the following if you are interested in it. |
|
| 154 |
- |
|
| 155 |
-=head2 1. Connect to database |
|
| 26 |
+=head1 GUIDE |
|
| 156 | 27 |
|
| 157 |
-Load L<DBIx::Custom>. |
|
| 28 |
+=head2 Connect To Database |
|
| 158 | 29 |
|
| 159 | 30 |
use DBIx::Custom; |
| 160 |
- |
|
| 161 |
-use C<connect()> to connect to database. |
|
| 162 |
-Return value is L<DBIx::Custom> object. |
|
| 163 |
- |
|
| 164 | 31 |
my $dbi = DBIx::Custom->connect( |
| 165 | 32 |
dsn => "dbi:mysql:database=bookshop", |
| 166 | 33 |
user => 'ken', |
| 167 | 34 |
password => '!LFKD%$&', |
| 168 |
- dbi_options => {mysql_enable_utf8 => 1}
|
|
| 35 |
+ dbi_option => {mysql_enable_utf8 => 1}
|
|
| 169 | 36 |
); |
| 170 | 37 |
|
| 171 |
-C<dsn> must be one corresponding to the database system. |
|
| 172 |
-The following ones are data source example. |
|
| 173 |
- |
|
| 174 |
-B<MySQL> |
|
| 175 |
- |
|
| 176 |
- "dbi:mysql:database=$database" |
|
| 177 |
- "dbi:mysql:database=$database;host=$hostname;port=$port" |
|
| 178 |
- |
|
| 179 |
-B<SQLite> |
|
| 180 |
- |
|
| 181 |
- "dbi:SQLite:dbname=$database" |
|
| 182 |
- "dbi:SQLite:dbname=:memory:" |
|
| 183 |
- |
|
| 184 |
-B<PostgreSQL> |
|
| 185 |
- |
|
| 186 |
- "dbi:Pg:dbname=$dbname" |
|
| 38 |
+You can connect to database by C<connect> method. |
|
| 39 |
+C<dsn> is data source name, C<user> is user name, C<password> is password. |
|
| 187 | 40 |
|
| 188 |
-B<Oracle> |
|
| 41 |
+C<dbi_option> is L<DBI> option. |
|
| 42 |
+By default, the following option is set. |
|
| 43 |
+Fatal error throw exeption and commit mode is auto commit. |
|
| 189 | 44 |
|
| 190 |
- "dbi:Oracle:$dbname" |
|
| 191 |
- "dbi:Oracle:host=$host;sid=$sid" |
|
| 192 |
- |
|
| 193 |
-B<ODBC(Microsoft Access)> |
|
| 194 |
- |
|
| 195 |
- "dbi:ODBC:driver=Microsoft Access Driver (*.mdb);dbq=hoge.mdb" |
|
| 196 |
- |
|
| 197 |
-B<ODBC(SQL Server)> |
|
| 198 |
- |
|
| 199 |
- "dbi:ODBC:driver={SQL Server};Server=(local);database=test;Trusted_Connection=yes;AutoTranslate=No;"
|
|
| 200 |
- |
|
| 201 |
-If authentication is needed, you can specify C<user> and C<password> |
|
| 202 |
- |
|
| 203 |
-L<DBIx::Custom> is wrapper class of L<DBI>. |
|
| 204 |
-You can use all methods of L<DBI> from L<DBIx::Custom> object. |
|
| 205 |
- |
|
| 206 |
- $dbi->do(...); |
|
| 207 |
- $dbi->begin_work; |
|
| 208 |
- |
|
| 209 |
-use C<dhb()> to get database handle of L<DBI> |
|
| 210 |
- |
|
| 211 |
- my $dbh = $dbi->dbh; |
|
| 212 |
- |
|
| 213 |
-By default, the following ones is set to database handle attributes. |
|
| 214 |
- |
|
| 215 |
- RaiseError -> 1 |
|
| 216 |
- PrintError -> 0 |
|
| 217 |
- AutoCommit -> 1 |
|
| 218 |
- |
|
| 219 |
-If fatal error occuer, program terminate. |
|
| 220 |
-If SQL is executed, commit is executed automatically. |
|
| 221 |
- |
|
| 222 |
-=head2 2. Methods for insert, update, delete, or insert |
|
| 223 |
- |
|
| 224 |
-There are following methods. |
|
| 225 |
- |
|
| 226 |
-=head3 C<insert()> |
|
| 227 |
- |
|
| 228 |
-use C<insert()> to insert row into database |
|
| 229 |
- |
|
| 230 |
- $dbi->insert(table => 'book', |
|
| 231 |
- param => {title => 'Perl', author => 'Ken'});
|
|
| 45 |
+ {
|
|
| 46 |
+ RaiseError => 1 |
|
| 47 |
+ PrintError => 0 |
|
| 48 |
+ AutoCommit => 1 |
|
| 49 |
+ } |
|
| 232 | 50 |
|
| 233 |
-C<table> is table name, C<param> is insert data. |
|
| 51 |
+=head2 Execute Query |
|
| 234 | 52 |
|
| 235 |
-Following SQL is executed. |
|
| 53 |
+=head3 Insert Statement : C<insert> |
|
| 236 | 54 |
|
| 237 |
- insert into (title, author) values (?, ?); |
|
| 55 |
+If you want to execute insert statement, use C<insert> method. |
|
| 238 | 56 |
|
| 239 |
-=head3 C<update()> |
|
| 57 |
+ $dbi->insert({title => 'Perl', author => 'Ken'}, table => 'book');
|
|
| 240 | 58 |
|
| 241 |
-use C<update()> to update row in database. |
|
| 59 |
+First argument is insert row data, C<table> is table name. |
|
| 242 | 60 |
|
| 243 |
- $dbi->update(table => 'book', |
|
| 244 |
- param => {title => 'Perl', author => 'Ken'},
|
|
| 245 |
- where => {id => 5});
|
|
| 61 |
+=head3 Update Statement : C<update> |
|
| 246 | 62 |
|
| 247 |
-C<table> is table name, C<param> is update data, C<where> is condition. |
|
| 63 |
+If you want to execute update stateimuse, use C<update> method. |
|
| 248 | 64 |
|
| 249 |
-Following SQL is executed. |
|
| 65 |
+ $dbi->update( |
|
| 66 |
+ {title => 'Perl', author => 'Ken'},
|
|
| 67 |
+ table => 'book', |
|
| 68 |
+ where => {id => 5}
|
|
| 69 |
+ ); |
|
| 250 | 70 |
|
| 251 |
- update book set title = ?, author = ?; |
|
| 71 |
+First argument is update row data, C<table> is table name, C<where> is condition. |
|
| 252 | 72 |
|
| 253 |
-You can't execute C<update()> without C<where> for safety. |
|
| 254 |
-use C<update_all()> if you want to update all rows. |
|
| 73 |
+Note that you can't execute C<update> method without C<where>. |
|
| 74 |
+If you want to update all rows, use update_all. |
|
| 255 | 75 |
|
| 256 |
- $dbi->update_all(table => 'book', |
|
| 257 |
- param => {title => 'Perl', author => 'Ken'});
|
|
| 76 |
+ $dbi->update_all({title => 'Perl', author => 'Ken'}, table => 'book');
|
|
| 258 | 77 |
|
| 259 |
-=head3 C<delete()> |
|
| 78 |
+=head3 Delete Statement : C<delete> |
|
| 260 | 79 |
|
| 261 |
-use C<delete()> to delete rows from database. |
|
| 80 |
+If you want to execute delete statement, use C<delete> method. |
|
| 262 | 81 |
|
| 263 |
- $dbi->delete(table => 'book', |
|
| 264 |
- where => {author => 'Ken'});
|
|
| 82 |
+ $dbi->delete(table => 'book', where => {author => 'Ken'});
|
|
| 265 | 83 |
|
| 266 | 84 |
C<table> is table name, C<where> is condition. |
| 267 | 85 |
|
| 268 |
-Following SQL is executed. |
|
| 269 |
- |
|
| 270 |
- delete from book where id = ?; |
|
| 271 |
- |
|
| 272 |
-You can't execute C<delete()> without C<where> for safety. |
|
| 273 |
-use C<delete_all()> if you want to delete all rows. |
|
| 86 |
+Note that you can't execute C<delete> method without C<where>. |
|
| 87 |
+If you want to delete all rows, use C<delete_all> method. |
|
| 274 | 88 |
|
| 275 | 89 |
$dbi->delete_all(table => 'book'); |
| 276 | 90 |
|
| 277 |
-=head3 C<select()> |
|
| 91 |
+=head3 Select Statement : C<select> |
|
| 278 | 92 |
|
| 279 |
-use C<select()> to select rows from database |
|
| 93 |
+If you want to execute select statement, use C<select> method. |
|
| 280 | 94 |
|
| 281 | 95 |
my $result = $dbi->select(table => 'book'); |
| 282 | 96 |
|
| 283 |
-Following SQL is executed. |
|
| 284 |
- |
|
| 285 |
- select * from book; |
|
| 286 |
- |
|
| 287 | 97 |
Return value is L<DBIx::Custom::Result> object. |
| 288 |
-use C<fetch()> to fetch row. |
|
| 98 |
+You can fetch rows by C<fetch> method. |
|
| 289 | 99 |
|
| 290 | 100 |
while (my $row = $result->fetch) {
|
| 291 | 101 |
my $title = $row->[0]; |
| 292 | 102 |
my $author = $row->[1]; |
| 293 | 103 |
} |
| 294 | 104 |
|
| 295 |
-See L<3. Fetch row/"3. Fetch row"> about L<DBIx::Custom::Result>. |
|
| 105 |
+See also L<Fetch row/"Fetch row"> about L<DBIx::Custom::Result>. |
|
| 296 | 106 |
|
| 297 |
-Continue more examples. |
|
| 107 |
+You can specify column names by C<column> option |
|
| 108 |
+and condition by C<where> option. |
|
| 298 | 109 |
|
| 299 | 110 |
my $result = $dbi->select( |
| 300 | 111 |
table => 'book', |
| ... | ... |
@@ -302,67 +113,19 @@ Continue more examples. |
| 302 | 113 |
where => {author => 'Ken'}
|
| 303 | 114 |
); |
| 304 | 115 |
|
| 305 |
-C<column> is column names, C<where> is condition. |
|
| 306 |
- |
|
| 307 |
-Following SQL is executed. |
|
| 308 |
- |
|
| 309 |
- select author, title from book where author = ?; |
|
| 310 |
- |
|
| 311 |
-Next example. |
|
| 116 |
+You can specify join clause by C<join> option. |
|
| 312 | 117 |
|
| 313 | 118 |
my $result = $dbi->select( |
| 314 | 119 |
table => 'book', |
| 315 |
- column => ['company.name as company__name'] |
|
| 120 |
+ column => ['company.name as company_name'] |
|
| 316 | 121 |
where => {'book.name' => 'Perl'},
|
| 317 | 122 |
join => ['left outer join company on book.company_id = company.id] |
| 318 | 123 |
); |
| 319 | 124 |
|
| 320 |
-You can join table by C<join>. |
|
| 321 |
- |
|
| 322 |
-Following SQL is executed. |
|
| 323 |
- |
|
| 324 |
- select company.name as company__name |
|
| 325 |
- from book |
|
| 326 |
- left outer join company on book.company_id = company.id |
|
| 327 |
- where book.name = ?; |
|
| 328 |
- |
|
| 329 |
-company_if of book and id of company is left outer joined. |
|
| 330 |
- |
|
| 331 |
-Note that only when C<where> or C<column> contain table name, |
|
| 332 |
-C<join> is joined. |
|
| 333 |
-if you specify the following option, C<join> is not joined |
|
| 334 |
-because C<join> is not needed. |
|
| 335 |
- |
|
| 336 |
- my $result = $dbi->select( |
|
| 337 |
- table => 'book', |
|
| 338 |
- where => {'name' => 'Perl'},
|
|
| 339 |
- join => ['left outer join company on book.company_id = company.id] |
|
| 340 |
- ); |
|
| 341 |
- |
|
| 342 |
-Following SQL is executeed. |
|
| 343 |
- |
|
| 344 |
- select * from book where book.name = ?; |
|
| 345 |
- |
|
| 346 |
-You can specify column names easily using C<mycolumn()> and C<column()>. |
|
| 347 |
- |
|
| 348 |
- my $result = $dbi->select( |
|
| 349 |
- table => 'book', |
|
| 350 |
- column => [ |
|
| 351 |
- $dbi->mycolumn('book' => ['name']),
|
|
| 352 |
- $dbi->column('company' => ['id', 'name'])
|
|
| 353 |
- ], |
|
| 354 |
- join => ['left outer join company on book.company_id = company.id] |
|
| 355 |
- ); |
|
| 356 |
- |
|
| 357 |
-The following SQL is executed. |
|
| 358 |
- |
|
| 359 |
- select book.name as name, |
|
| 360 |
- company.id as comapny__id, |
|
| 361 |
- company.name as company__name |
|
| 362 |
- from book |
|
| 363 |
- left outer join company on book.company_id = company.id |
|
| 125 |
+Note that join clause is joined only when C<where> or C<column> option contains table name, |
|
| 126 |
+such as book.name. |
|
| 364 | 127 |
|
| 365 |
-Next example. |
|
| 128 |
+You can append statement to the end of whole statement by C<append> option. |
|
| 366 | 129 |
|
| 367 | 130 |
my $result = $dbi->select( |
| 368 | 131 |
table => 'book', |
| ... | ... |
@@ -370,334 +133,104 @@ Next example. |
| 370 | 133 |
append => 'for update', |
| 371 | 134 |
); |
| 372 | 135 |
|
| 373 |
-C<append> is string appending to end of SQL. |
|
| 374 |
- |
|
| 375 |
-Following SQL is executed. |
|
| 376 |
- |
|
| 377 |
- select * book where author = ? for update; |
|
| 136 |
+=head3 C<execute> |
|
| 378 | 137 |
|
| 379 |
-C<appned> is also used at C<insert()>, C<update()>, C<update_all()> |
|
| 380 |
-C<delete()>, C<delete_all()>, and C<select()>. |
|
| 381 |
- |
|
| 382 |
-=head3 C<execute()> |
|
| 383 |
- |
|
| 384 |
-use C<execute()> to execute SQL |
|
| 138 |
+If you want to execute SQL, use C<execute> method. |
|
| 385 | 139 |
|
| 386 | 140 |
$dbi->execute("select * from book;");
|
| 387 | 141 |
|
| 388 |
-Process parameter and execute SQL. |
|
| 142 |
+You can specify parameters. |
|
| 389 | 143 |
|
| 390 | 144 |
$dbi->execute( |
| 391 | 145 |
"select * from book title = :title and author = :author;" |
| 392 |
- param => {title => 'Perl', author => 'Ken'}
|
|
| 146 |
+ {title => 'Perl', author => 'Ken'}
|
|
| 393 | 147 |
); |
| 394 | 148 |
|
| 395 |
-Following SQL is executed. |
|
| 149 |
+:title and :author is parameters, which is replaced to placeholers. |
|
| 396 | 150 |
|
| 397 | 151 |
select * from book title = ? and author = ?; |
| 398 | 152 |
|
| 399 |
-Values of title and author is embbdeded into placeholder. |
|
| 153 |
+See also L<Parameter/"Parameter"> about parameter. |
|
| 400 | 154 |
|
| 401 |
-See L<5. Parameter/"5. Parameter"> about parameter. |
|
| 155 |
+=head3 C<dbh> |
|
| 402 | 156 |
|
| 403 |
-You don't have to wirte last semicolon in C<execute()>. |
|
| 157 |
+ my $dbh = $dbi->dbh; |
|
| 404 | 158 |
|
| 405 |
- $dbi->execute('select * from book');
|
|
| 159 |
+Get get database handle object of L<DBI>. |
|
| 406 | 160 |
|
| 407 |
-=head2 3. Fetch row |
|
| 161 |
+=head3 C<DBI> methods |
|
| 408 | 162 |
|
| 409 |
-Return value of C<select()> is L<DBIx::Custom::Result> object. |
|
| 410 |
-There are many methods to fetch row. |
|
| 163 |
+ $dbi->do(...); |
|
| 164 |
+ $dbi->begin_work; |
|
| 411 | 165 |
|
| 412 |
-=head3 Fetch a row (array) : C<fetch()> |
|
| 166 |
+You can call all methods of L<DBI> from L<DBIx::Custom> object. |
|
| 413 | 167 |
|
| 414 |
-use C<fetch()> to fetch a row and assign it into array reference. |
|
| 168 |
+=head2 Fetch Rows |
|
| 169 |
+ |
|
| 170 |
+C<select> method return value is L<DBIx::Custom::Result> object. |
|
| 171 |
+You can fetch a row or rows by various methods. |
|
| 172 |
+ |
|
| 173 |
+=head3 Fetch a row (array) : C<fetch> |
|
| 415 | 174 |
|
| 416 | 175 |
my $row = $result->fetch; |
| 417 | 176 |
|
| 418 |
-You can get all rows. |
|
| 177 |
+C<fetch> method fetch a row and put it into array reference. |
|
| 178 |
+You can continue to fetch |
|
| 419 | 179 |
|
| 420 | 180 |
while (my $row = $result->fetch) {
|
| 421 | 181 |
my $title = $row->[0]; |
| 422 | 182 |
my $author = $row->[1]; |
| 423 | 183 |
} |
| 424 | 184 |
|
| 425 |
-=head3 Fetch only first row (array) : C<fetch_first()> |
|
| 426 |
- |
|
| 427 |
-use C<fetch_first()> to fetch only first row. |
|
| 185 |
+=head3 Fetch only first row (array) : C<fetch_first> |
|
| 428 | 186 |
|
| 429 | 187 |
my $row = $result->fetch_first; |
| 430 | 188 |
|
| 431 |
-You can't fetch rest rows |
|
| 432 |
-because statement handle C<finish()> is executed. |
|
| 433 |
- |
|
| 434 |
-=head3 Fetch rows (array) : C<fetch_multi()> |
|
| 435 |
- |
|
| 436 |
-use C<fetch_multi()> to fetch rows and assign it into |
|
| 437 |
-array reference which has array references as element. |
|
| 438 |
- |
|
| 439 |
- while (my $rows = $result->fetch_multi(2)) {
|
|
| 440 |
- my $title0 = $rows->[0][0]; |
|
| 441 |
- my $author0 = $rows->[0][1]; |
|
| 442 |
- |
|
| 443 |
- my $title1 = $rows->[1][0]; |
|
| 444 |
- my $author1 = $rows->[1][1]; |
|
| 445 |
- } |
|
| 446 |
- |
|
| 447 |
-Specify row count as argument. |
|
| 448 |
- |
|
| 449 |
-You can get the following data. |
|
| 450 |
- |
|
| 451 |
- [ |
|
| 452 |
- ['Perl', 'Ken'], |
|
| 453 |
- ['Ruby', 'Mark'] |
|
| 454 |
- ] |
|
| 189 |
+C<fetch_first> fetch a only first row and finish statment handle, |
|
| 190 |
+and put it into array refrence. |
|
| 455 | 191 |
|
| 456 | 192 |
=head3 Fetch all rows (array) : C<fetch_all> |
| 457 | 193 |
|
| 458 |
-use C<fetch_all()> to fetch all rows and assign it into |
|
| 459 |
-array reference which has array reference as element. |
|
| 460 |
- |
|
| 461 | 194 |
my $rows = $result->fetch_all; |
| 462 | 195 |
|
| 463 |
-You can get the following data. |
|
| 196 |
+C<fetch_all> fetch all rows and put them into array of array reference. |
|
| 464 | 197 |
|
| 465 |
- [ |
|
| 466 |
- ['Perl', 'Ken'], |
|
| 467 |
- ['Ruby', 'Mark'] |
|
| 468 |
- ] |
|
| 198 |
+=head3 Fetch a row (hash) : C<fetch_hash> |
|
| 469 | 199 |
|
| 470 |
-=head3 Fetch a row (hash) : C<fetch_hash()> |
|
| 200 |
+ my $row = $result->fetch_hash; |
|
| 471 | 201 |
|
| 472 |
-use C<fetch_hash()> to fetch a row and assign it into hash reference. |
|
| 202 |
+C<fetch_hash> fetch a row and put it into hash reference. |
|
| 203 |
+You can fetch a row while row exists. |
|
| 473 | 204 |
|
| 474 | 205 |
while (my $row = $result->fetch_hash) {
|
| 475 | 206 |
my $title = $row->{title};
|
| 476 | 207 |
my $author = $row->{author};
|
| 477 | 208 |
} |
| 478 | 209 |
|
| 479 |
-=head3 Fetch only first row (hash) : C<fetch_hash_first()> |
|
| 480 |
- |
|
| 481 |
-use C<fetch_hash_first()> to fetch only first row |
|
| 482 |
-and assign it into hash reference. |
|
| 210 |
+=head3 Fetch only a first row (hash) : C<fetch_hash_first> |
|
| 483 | 211 |
|
| 484 | 212 |
my $row = $result->fetch_hash_first; |
| 485 | 213 |
|
| 486 |
-You can't fetch rest rows |
|
| 487 |
-because statement handle C<finish()> is executed. |
|
| 488 |
- |
|
| 489 |
-=head3 Fetch rows (hash) : C<fetch_hash_multi()> |
|
| 490 |
- |
|
| 491 |
-use C<fetch_hash_multi()> to fetch rows and |
|
| 492 |
-assign it into array reference which has hash references as element. |
|
| 493 |
- |
|
| 494 |
- while (my $rows = $result->fetch_hash_multi(5)) {
|
|
| 495 |
- my $title0 = $rows->[0]{title};
|
|
| 496 |
- my $author0 = $rows->[0]{author};
|
|
| 497 |
- my $title1 = $rows->[1]{title};
|
|
| 498 |
- my $author1 = $rows->[1]{author};
|
|
| 499 |
- } |
|
| 500 |
- |
|
| 501 |
-Specify row count as argument. |
|
| 502 |
- |
|
| 503 |
-You can get the following data. |
|
| 214 |
+C<fetch_hash_first> fetch only a first row and finish statement handle, |
|
| 215 |
+and put them into hash refrence. |
|
| 504 | 216 |
|
| 505 |
- [ |
|
| 506 |
- {title => 'Perl', author => 'Ken'},
|
|
| 507 |
- {title => 'Ruby', author => 'Mark'}
|
|
| 508 |
- ] |
|
| 217 |
+C<one> is C<fetch_hash_first> synonym to save word typing. |
|
| 509 | 218 |
|
| 510 |
-=head3 Fetch all rows (hash) : C<fetch_hash_all()> |
|
| 219 |
+ my $row = $result->one; |
|
| 511 | 220 |
|
| 512 |
-use C<fetch_hash_all()> to fetch all rows and |
|
| 513 |
-assign it into array reference which has hash |
|
| 514 |
-references as element. |
|
| 221 |
+=head3 Fetch all rows (hash) : C<fetch_hash_all> |
|
| 515 | 222 |
|
| 516 | 223 |
my $rows = $result->fetch_hash_all; |
| 517 | 224 |
|
| 518 |
-You can get the following data. |
|
| 519 |
- |
|
| 520 |
- [ |
|
| 521 |
- {title => 'Perl', author => 'Ken'},
|
|
| 522 |
- {title => 'Ruby', author => 'Mark'}
|
|
| 523 |
- ] |
|
| 225 |
+C<fetch_hash_all> fetch all rows and put them into array of hash reference. |
|
| 524 | 226 |
|
| 525 |
-=head3 Statement handle : C<sth()> |
|
| 526 |
- |
|
| 527 |
-use <sth()> to get statement handle. |
|
| 227 |
+=head3 Statement Handle : C<sth> |
|
| 528 | 228 |
|
| 529 | 229 |
my $sth = $result->sth; |
| 530 | 230 |
|
| 531 |
-=head2 4. Filtering |
|
| 532 |
- |
|
| 533 |
-L<DBIx::Custom> provide value filtering. |
|
| 534 |
-For example, You maybe want to convert L<Time::Piece> object to |
|
| 535 |
-database date format when register data into database. |
|
| 536 |
-and convert database date fromat to L<Time::Piece> object |
|
| 537 |
-when get data from database. |
|
| 538 |
- |
|
| 539 |
-=head3 Register filter : C<register_filter()> |
|
| 540 |
- |
|
| 541 |
-use C<register_filter()> to register filter. |
|
| 542 |
- |
|
| 543 |
- $dbi->register_filter( |
|
| 544 |
- # Time::Piece object to DATE format |
|
| 545 |
- tp_to_date => sub {
|
|
| 546 |
- my $date = shift; |
|
| 547 |
- |
|
| 548 |
- return '0000-00-00' unless $tp; |
|
| 549 |
- return $tp->strftime('%Y-%m-%d');
|
|
| 550 |
- }, |
|
| 551 |
- |
|
| 552 |
- # DATE to Time::Piece object |
|
| 553 |
- date_to_tp => sub {
|
|
| 554 |
- my $date = shift; |
|
| 555 |
- |
|
| 556 |
- return if $date eq '0000-00-00'; |
|
| 557 |
- return Time::Piece->strptime($date, '%Y-%m-%d'); |
|
| 558 |
- }, |
|
| 559 |
- ); |
|
| 560 |
- |
|
| 561 |
-Registered filter is used by C<apply_filter()> or etc. |
|
| 562 |
- |
|
| 563 |
-=head3 Apply filter : C<apply_filter()> |
|
| 564 |
- |
|
| 565 |
-use C<apply_filter()> to apply registered filter. |
|
| 566 |
- |
|
| 567 |
- $dbi->apply_filter('book',
|
|
| 568 |
- issue_date => {out => 'tp_to_date', in => 'date_to_tp'},
|
|
| 569 |
- first_issue_date => {out => 'tp_to_date', in => 'date_to_tp'}
|
|
| 570 |
- ); |
|
| 571 |
- |
|
| 572 |
-First argument is table name. Arguments after first argument are pairs of column |
|
| 573 |
-name and fitering rule. C<out> of filtering rule is filter which is used when data |
|
| 574 |
-is send to database. C<in> of filtering rule is filter which is used when data |
|
| 575 |
-is got from database. |
|
| 576 |
- |
|
| 577 |
-You can specify code reference as filter. |
|
| 578 |
- |
|
| 579 |
- issue_date => {out => sub { ... }, in => sub { ... }}
|
|
| 580 |
- |
|
| 581 |
-Applied filter become effective at insert()>, C<update()>, C<update_all()>, |
|
| 582 |
-C<delete()>, C<delete_all()>, C<select()>. |
|
| 583 |
- |
|
| 584 |
- my $tp = Time::Piece->strptime('2010/10/14', '%Y/%m/%d');
|
|
| 585 |
- my $result = $dbi->select(table => 'book', where => {issue_date => $tp});
|
|
| 586 |
- |
|
| 587 |
-When data is send to database, L<Time::Piece> object is converted |
|
| 588 |
-to database date format "2010-10-14" |
|
| 231 |
+If you want to get statment handle, use <sth> method. |
|
| 589 | 232 |
|
| 590 |
-When data is fetched, database date format is |
|
| 591 |
-converted to L<Time::Piece> object. |
|
| 592 |
- |
|
| 593 |
- my $row = $resutl->fetch_hash_first; |
|
| 594 |
- my $tp = $row->{issue_date};
|
|
| 595 |
- |
|
| 596 |
-You can also use column name which contains table name. |
|
| 597 |
- |
|
| 598 |
- $dbi->select( |
|
| 599 |
- table => 'book', |
|
| 600 |
- where => {'book.issue_date' => $tp}
|
|
| 601 |
- ); |
|
| 602 |
- |
|
| 603 |
-In fetching, Filter is effective if you use "TABLE__COLUMN" as column name. |
|
| 604 |
- |
|
| 605 |
- my $result = $dbi->execute( |
|
| 606 |
- "select issue_date as book__issue_date from book"); |
|
| 607 |
- |
|
| 608 |
-You can apply C<end> filter execute after C<in> filter. |
|
| 609 |
- |
|
| 610 |
- $dbi->apply_filter('book',
|
|
| 611 |
- issue_date => {out => 'tp_to_date', in => 'date_to_tp',
|
|
| 612 |
- end => 'tp_to_displaydate'}, |
|
| 613 |
- ); |
|
| 614 |
- |
|
| 615 |
-=head3 Individual filter C<filter> |
|
| 616 |
- |
|
| 617 |
-You can apply individual filter . |
|
| 618 |
-This filter overwrite the filter by C<apply_filter()> |
|
| 619 |
- |
|
| 620 |
-use C<filter> option to apply individual filter |
|
| 621 |
-when data is send to database. |
|
| 622 |
-This option is used at C<insert()>, C<update()>, |
|
| 623 |
-C<update_all()>, C<delete()>, C<delete_all()>, C<select()>, |
|
| 624 |
-C<execute()>. |
|
| 625 |
- |
|
| 626 |
-C<insert()> example: |
|
| 627 |
- |
|
| 628 |
- $dbi->insert( |
|
| 629 |
- table => 'book', |
|
| 630 |
- param => {issue_date => $tp, first_issue_date => $tp},
|
|
| 631 |
- filter => {issue_date => 'tp_to_date', first_issue_date => 'tp_to_date'}
|
|
| 632 |
- ); |
|
| 633 |
- |
|
| 634 |
-C<execute()> example: |
|
| 635 |
- |
|
| 636 |
-my $sql = <<"EOS"; |
|
| 637 |
-select YEAR(issue_date) as issue_year |
|
| 638 |
-from book |
|
| 639 |
-where YEAR(issue_date) = {? issue_year}
|
|
| 640 |
-EOS |
|
| 641 |
- |
|
| 642 |
- my $result = $dbi->execute( |
|
| 643 |
- $sql, |
|
| 644 |
- param => {issue_year => '2010'},
|
|
| 645 |
- filter => {issue_year => 'tp_to_year'}
|
|
| 646 |
- ); |
|
| 647 |
- |
|
| 648 |
-You can also apply indivisual filter when you fetch row. |
|
| 649 |
-use C<DBIx::Custom::Result>'s C<filter()>. |
|
| 650 |
- |
|
| 651 |
- $result->filter(issue_year => 'year_to_tp'); |
|
| 652 |
- |
|
| 653 |
-You can remove filter by C<remove_filter()> |
|
| 654 |
- |
|
| 655 |
- $result->remove_filter; |
|
| 656 |
- |
|
| 657 |
-=head3 End filtering : C<end_filter()> |
|
| 658 |
- |
|
| 659 |
-You can add filter at end. |
|
| 660 |
-It is useful to create last output. |
|
| 661 |
-use C<end_filter()> to add end filter. |
|
| 662 |
- |
|
| 663 |
- $result->end_filter(issue_date => sub {
|
|
| 664 |
- my $tp = shift; |
|
| 665 |
- |
|
| 666 |
- return '' unless $tp; |
|
| 667 |
- return $tp->strftime('%Y/%m/%d %h:%m:%s (%a)');
|
|
| 668 |
- }); |
|
| 669 |
- |
|
| 670 |
-In this example, L<Time::Piece> object is converted to readable format. |
|
| 671 |
- |
|
| 672 |
-You can remove end_filter by C<end_filter> |
|
| 673 |
- |
|
| 674 |
- $result->remove_end_filter; |
|
| 675 |
- |
|
| 676 |
-=head3 Automate applying filter : C<each_column()> |
|
| 677 |
- |
|
| 678 |
-It is useful to apply filter automatically at date type columns. |
|
| 679 |
-You can use C<each_column()> to process all column infos. |
|
| 680 |
- |
|
| 681 |
- $dbi->each_column( |
|
| 682 |
- sub {
|
|
| 683 |
- my ($self, $table, $column, $info) = @_; |
|
| 684 |
- |
|
| 685 |
- my $type = $info->{TYPE_NAME};
|
|
| 686 |
- |
|
| 687 |
- my $filter = $type eq 'DATE' ? {out => 'tp_to_date', in => 'date_to_tp'}
|
|
| 688 |
- : $type eq 'DATETIME' ? {out => 'tp_to_datetime', in => 'datetime_to_tp'}
|
|
| 689 |
- : undef; |
|
| 690 |
- |
|
| 691 |
- $self->apply_filter($table, $column, $filter) |
|
| 692 |
- if $filter; |
|
| 693 |
- } |
|
| 694 |
- ); |
|
| 695 |
- |
|
| 696 |
-C<each_column()> receive callback. |
|
| 697 |
-callback arguments are L<DBIx::Custom> object, table name, column name, column information. |
|
| 698 |
-Filter is applied automatically by column type. |
|
| 699 |
- |
|
| 700 |
-=head2 5. Parameter |
|
| 233 |
+=head2 Parameter |
|
| 701 | 234 |
|
| 702 | 235 |
=head3 Basic of Parameter |
| 703 | 236 |
|
| ... | ... |
@@ -711,20 +244,20 @@ Parameter is converted to place holder. |
| 711 | 244 |
|
| 712 | 245 |
select * from book where title = ? and author like ?; |
| 713 | 246 |
|
| 714 |
-use C<execute()> to execute SQL. |
|
| 247 |
+use C<execute> to execute SQL. |
|
| 715 | 248 |
|
| 716 | 249 |
my $sql = "select * from book where title = :title and author like :author;" |
| 717 |
- $dbi->execute($sql, param => {title => 'Perl', author => '%Ken%'});
|
|
| 250 |
+ $dbi->execute($sql, {title => 'Perl', author => '%Ken%'});
|
|
| 718 | 251 |
|
| 719 | 252 |
You can specify values embedded into place holder as hash reference using |
| 720 | 253 |
C<param> option. |
| 721 | 254 |
|
| 722 |
-You can specify C<filter()> at C<execute()>. |
|
| 255 |
+You can specify C<filter> at C<execute>. |
|
| 723 | 256 |
|
| 724 |
- $dbi->execute($sql, param => {title => 'Perl', author => '%Ken%'}
|
|
| 257 |
+ $dbi->execute($sql, {title => 'Perl', author => '%Ken%'}
|
|
| 725 | 258 |
filter => {title => 'to_something');
|
| 726 | 259 |
|
| 727 |
-Note that at C<execute()> the filter applied by C<apply_filter()> |
|
| 260 |
+Note that at C<execute> the filter applied by C<apply_filter> |
|
| 728 | 261 |
don't has effective to columns. |
| 729 | 262 |
You have to use C<table> option |
| 730 | 263 |
|
| ... | ... |
@@ -737,13 +270,13 @@ Let's think two date comparison. |
| 737 | 270 |
|
| 738 | 271 |
my $sql = "select * from table where date > :date and date < :date;"; |
| 739 | 272 |
|
| 740 |
-In this case, You specify paramter values as array reference. |
|
| 273 |
+In this case, You specify parameter values as array reference. |
|
| 741 | 274 |
|
| 742 |
- my $dbi->execute($sql, param => {date => ['2010-10-01', '2012-02-10']});
|
|
| 275 |
+ my $dbi->execute($sql, {date => ['2010-10-01', '2012-02-10']});
|
|
| 743 | 276 |
|
| 744 |
-=head2 6. Dinamically create where clause |
|
| 277 |
+=head2 Create where clause |
|
| 745 | 278 |
|
| 746 |
-=head3 Dinamically create where clause : where() |
|
| 279 |
+=head3 Dinamically create where clause : where |
|
| 747 | 280 |
|
| 748 | 281 |
You want to search multiple conditions in many times. |
| 749 | 282 |
Let's think the following three cases. |
| ... | ... |
@@ -761,11 +294,11 @@ Case3: Search C<title> and C<author> |
| 761 | 294 |
where title = :title and author = :author |
| 762 | 295 |
|
| 763 | 296 |
L<DBIx::Custom> support dinamic where clause creating. |
| 764 |
-At first, create L<DBIx::Custom::Where> object by C<where()>. |
|
| 297 |
+At first, create L<DBIx::Custom::Where> object by C<where>. |
|
| 765 | 298 |
|
| 766 | 299 |
my $where = $dbi->where; |
| 767 | 300 |
|
| 768 |
-Set clause by C<clause()> |
|
| 301 |
+Set clause by C<clause> |
|
| 769 | 302 |
|
| 770 | 303 |
$where->clause( |
| 771 | 304 |
['and', 'title = :title, 'author = :author'] |
| ... | ... |
@@ -793,7 +326,7 @@ After setting C<clause>, set C<param>. |
| 793 | 326 |
|
| 794 | 327 |
In this example, parameter contains only title. |
| 795 | 328 |
|
| 796 |
-If you execute C<string_to()>, you can get where clause |
|
| 329 |
+If you execute C<string_to>, you can get where clause |
|
| 797 | 330 |
which contain only parameter name. |
| 798 | 331 |
|
| 799 | 332 |
my $where_clause = $where->to_string; |
| ... | ... |
@@ -830,7 +363,7 @@ If starting date isn't exists, create the following parameter. |
| 830 | 363 |
|
| 831 | 364 |
my $p = {date => [$dbi->not_exists, '2011-11-21']};
|
| 832 | 365 |
|
| 833 |
-You can get DBIx::Custom::NotExists object by C<not_exists()> |
|
| 366 |
+You can get DBIx::Custom::NotExists object by C<not_exists> |
|
| 834 | 367 |
This mean correnspondinf value isn't exists. |
| 835 | 368 |
|
| 836 | 369 |
If ending date isn't exists, create the following parameter. |
| ... | ... |
@@ -849,18 +382,18 @@ This logic is a little difficut. See the following ones. |
| 849 | 382 |
push @date, $param->{end_date} if exists $param->{end_date};
|
| 850 | 383 |
my $p = {date => \@date};
|
| 851 | 384 |
|
| 852 |
-=head3 With C<select()> |
|
| 385 |
+=head3 With C<select> |
|
| 853 | 386 |
|
| 854 |
-You can pass L<DBIx::Custom::Where> object to C<where> of C<select()>. |
|
| 387 |
+You can pass L<DBIx::Custom::Where> object to C<where> of C<select>. |
|
| 855 | 388 |
|
| 856 | 389 |
my $where = $dbi->where; |
| 857 | 390 |
$where->clause(['and', 'title = :title', 'author = :author']); |
| 858 | 391 |
$where->param({title => 'Perl'});
|
| 859 | 392 |
my $result = $dbi->select(table => 'book', where => $where); |
| 860 | 393 |
|
| 861 |
-You can also pass it to C<where> of C<update()>AC<delete()> |
|
| 394 |
+You can also pass it to C<where> of C<update>AC<delete> |
|
| 862 | 395 |
|
| 863 |
-=head3 With C<execute()> |
|
| 396 |
+=head3 With C<execute> |
|
| 864 | 397 |
|
| 865 | 398 |
L<DBIx::Custom::Where> object is embedded into SQL. |
| 866 | 399 |
|
| ... | ... |
@@ -873,7 +406,53 @@ L<DBIx::Custom::Where> object is embedded into SQL. |
| 873 | 406 |
$where |
| 874 | 407 |
EOS |
| 875 | 408 |
|
| 876 |
- $dbi->execute($sql, param => $param, table => 'book'); |
|
| 409 |
+ $dbi->execute($sql, $param, table => 'book'); |
|
| 410 |
+ |
|
| 411 |
+=head2 Filtering |
|
| 412 |
+ |
|
| 413 |
+=head3 Register filter : C<register_filter> |
|
| 414 |
+ |
|
| 415 |
+If you want to register filter, use C<register_filter>. |
|
| 416 |
+ |
|
| 417 |
+ $dbi->register_filter( |
|
| 418 |
+ # Time::Piece object to DATE format |
|
| 419 |
+ tp_to_date => sub {
|
|
| 420 |
+ my $date = shift; |
|
| 421 |
+ return $tp->strftime('%Y-%m-%d');
|
|
| 422 |
+ }, |
|
| 423 |
+ |
|
| 424 |
+ # DATE to Time::Piece object |
|
| 425 |
+ date_to_tp => sub {
|
|
| 426 |
+ my $date = shift; |
|
| 427 |
+ return Time::Piece->strptime($date, '%Y-%m-%d'); |
|
| 428 |
+ }, |
|
| 429 |
+ ); |
|
| 430 |
+ |
|
| 431 |
+=head3 Filter before sending data into database : C<filter> option |
|
| 432 |
+ |
|
| 433 |
+If you filter sending data, use C<filter> option. |
|
| 434 |
+ |
|
| 435 |
+ $dbi->execute( |
|
| 436 |
+ 'insert into book (date) values (:date)', |
|
| 437 |
+ {date => $tp},
|
|
| 438 |
+ filter => {date => 'tp_to_date'}
|
|
| 439 |
+ ); |
|
| 440 |
+ |
|
| 441 |
+You can use C<filter> option in C<insert>, C<update>, C<delete>, C<select> method. |
|
| 442 |
+ |
|
| 443 |
+ $dbi->insert( |
|
| 444 |
+ {date => $tp},
|
|
| 445 |
+ table => 'book', |
|
| 446 |
+ filter => {date => 'tp_to_date'}
|
|
| 447 |
+ ); |
|
| 448 |
+ |
|
| 449 |
+=head3 Filter after fetching data from database. |
|
| 450 |
+ |
|
| 451 |
+If you filter fetch data, use L<DBIx::Custom::Result>'s C<filter> method. |
|
| 452 |
+ |
|
| 453 |
+ my $result = $dbi->select(column => 'date', table => 'book'); |
|
| 454 |
+ $result->filter(date => 'date_to_tp'); |
|
| 455 |
+ my $row = $result->one; |
|
| 877 | 456 |
|
| 878 | 457 |
=head2 7. Model |
| 879 | 458 |
|
| ... | ... |
@@ -883,18 +462,18 @@ you can define model extending L<DBIx::Custom::Model> |
| 883 | 462 |
to improve source code view. |
| 884 | 463 |
|
| 885 | 464 |
At first, you create basic model class extending <DBIx::Custom::Model>. |
| 465 |
+Each L<DBIx::Custom> class inherit L<Object::Simple>. |
|
| 466 |
+so you can inherit the following way. |
|
| 886 | 467 |
|
| 887 | 468 |
package MyModel; |
| 888 |
- |
|
| 889 |
- use base 'DBIx::Custom::Model'; |
|
| 469 |
+ use DBIx::Custom::Model -base; |
|
| 890 | 470 |
|
| 891 | 471 |
Next, you create each model classes. |
| 892 | 472 |
|
| 893 | 473 |
MyModel::book |
| 894 | 474 |
|
| 895 | 475 |
package MyModel::book; |
| 896 |
- |
|
| 897 |
- use base 'MyModel'; |
|
| 476 |
+ use MyModel -base; |
|
| 898 | 477 |
|
| 899 | 478 |
sub insert { ... }
|
| 900 | 479 |
sub list { ... }
|
| ... | ... |
@@ -902,8 +481,7 @@ MyModel::book |
| 902 | 481 |
MyModel::company |
| 903 | 482 |
|
| 904 | 483 |
package MyModel::company; |
| 905 |
- |
|
| 906 |
- use base 'MyModel'; |
|
| 484 |
+ use MyModel -base; |
|
| 907 | 485 |
|
| 908 | 486 |
sub insert { ... }
|
| 909 | 487 |
sub list { ... }
|
| ... | ... |
@@ -914,7 +492,7 @@ The follwoing modules location is needed. |
| 914 | 492 |
MyModel / book.pm |
| 915 | 493 |
/ company.pm |
| 916 | 494 |
|
| 917 |
-You can include these models by C<include_model()> |
|
| 495 |
+You can include these models by C<include_model> |
|
| 918 | 496 |
|
| 919 | 497 |
$dbi->include_model('MyModel');
|
| 920 | 498 |
|
| ... | ... |
@@ -925,15 +503,15 @@ You can use model like this. |
| 925 | 503 |
my $result = $dbi->model('book')->list;
|
| 926 | 504 |
|
| 927 | 505 |
In mode, You can use such as methods, |
| 928 |
-C<insert()>, C<update()>, C<update_all()>, |
|
| 929 |
-C<delete()>, C<delete_all()>, C<select()> |
|
| 506 |
+C<insert>, C<update>, C<update_all>, |
|
| 507 |
+C<delete>, C<delete_all>, C<select> |
|
| 930 | 508 |
without C<table> option. |
| 931 | 509 |
|
| 932 |
- $dbi->model('book')->insert(param => $param);
|
|
| 510 |
+ $dbi->model('book')->insert($param);
|
|
| 933 | 511 |
|
| 934 | 512 |
Model is L<DBIx::Custom::Model>. |
| 935 | 513 |
|
| 936 |
-If you need table nameAyou can get it by C<table()>. |
|
| 514 |
+If you need table nameAyou can get it by C<table>. |
|
| 937 | 515 |
|
| 938 | 516 |
my $table = $model->table; |
| 939 | 517 |
|
| ... | ... |
@@ -950,7 +528,7 @@ You can also call all methods of L<DBIx::Custom> and L<DBI>. |
| 950 | 528 |
$model->begin_work; |
| 951 | 529 |
$model->commit; |
| 952 | 530 |
|
| 953 |
-If you want to get all models, you can get them by keys of C<models()>. |
|
| 531 |
+If you want to get all models, you can get them by keys of C<models>. |
|
| 954 | 532 |
|
| 955 | 533 |
my @models = keys %{$self->models};
|
| 956 | 534 |
|
| ... | ... |
@@ -958,24 +536,24 @@ You can set primary key to model. |
| 958 | 536 |
|
| 959 | 537 |
$model->primary_key(['id', 'number_id']); |
| 960 | 538 |
|
| 961 |
-Primary key is used by C<insert_at>, C<update_at()>, C<delete_at()>, |
|
| 962 |
-C<select_at()>. |
|
| 539 |
+Primary key is used by C<insert>, C<update>, C<delete>, |
|
| 540 |
+and C<select> methods. |
|
| 963 | 541 |
|
| 964 |
-by C<filter> you can define filters applied by C<apply_filter()> |
|
| 542 |
+by C<filter> you can define filters applied by C<apply_filter> |
|
| 965 | 543 |
|
| 966 | 544 |
$model->filter({
|
| 967 | 545 |
title => {out => ..., in => ..., end => ...},
|
| 968 | 546 |
author => {out => ..., in => ..., end => ...}
|
| 969 | 547 |
}); |
| 970 | 548 |
|
| 971 |
-This filters is applied when C<include_model()> is called. |
|
| 549 |
+This filters is applied when C<include_model> is called. |
|
| 972 | 550 |
|
| 973 | 551 |
You can set column names |
| 974 | 552 |
|
| 975 | 553 |
$model->columns(['id', 'number_id']); |
| 976 | 554 |
|
| 977 |
-Column names is automarically set by C<setup_model()>. |
|
| 978 |
-This method is needed to be call after C<include_model()>. |
|
| 555 |
+Column names is automarically set by C<setup_model>. |
|
| 556 |
+This method is needed to be call after C<include_model>. |
|
| 979 | 557 |
|
| 980 | 558 |
$dbi->setup_model; |
| 981 | 559 |
|
| ... | ... |
@@ -983,7 +561,7 @@ You can set C<join> |
| 983 | 561 |
|
| 984 | 562 |
$model->join(['left outer join company on book.company_id = company.id']); |
| 985 | 563 |
|
| 986 |
-This C<join> is used by C<select()>, C<select_at()> |
|
| 564 |
+C<join> is used by C<select> method. |
|
| 987 | 565 |
|
| 988 | 566 |
=head2 Class name, Model name, Table name |
| 989 | 567 |
|
| ... | ... |
@@ -1002,7 +580,7 @@ You can change model name. |
| 1002 | 580 |
CLASS MODEL TABLE |
| 1003 | 581 |
book book_model (MODEL) -> book_model |
| 1004 | 582 |
|
| 1005 |
-Model name is the name used by L<model()> of L<DBIx::Custom>. |
|
| 583 |
+Model name is the name used by L<model> of L<DBIx::Custom>. |
|
| 1006 | 584 |
|
| 1007 | 585 |
$dbi->model('book_model');
|
| 1008 | 586 |
|
| ... | ... |
@@ -1019,9 +597,9 @@ Table name is the table really accessed. |
| 1019 | 597 |
|
| 1020 | 598 |
$dbi->model('book')->insert(...); # access to "book_table"
|
| 1021 | 599 |
|
| 1022 |
-=head2 Create column clause automatically : mycolumn(), column() |
|
| 600 |
+=head2 Create column clause automatically : mycolumn, column |
|
| 1023 | 601 |
|
| 1024 |
-To create column clause automatically, use C<mycolumn()>. |
|
| 602 |
+To create column clause automatically, use C<mycolumn>. |
|
| 1025 | 603 |
Valude of C<table> and C<columns> is used. |
| 1026 | 604 |
|
| 1027 | 605 |
my $column_clause = $model->mycolumn; |
| ... | ... |
@@ -1042,9 +620,9 @@ the following clause is created. |
| 1042 | 620 |
|
| 1043 | 621 |
company.id as company__id, company.name as company__name |
| 1044 | 622 |
|
| 1045 |
-=head2 Create column clause automatically : column_clause() |
|
| 623 |
+=head2 Create column clause automatically : column_clause |
|
| 1046 | 624 |
|
| 1047 |
-To create column clause automatically, use C<column_clause()>. |
|
| 625 |
+To create column clause automatically, use C<column_clause>. |
|
| 1048 | 626 |
Valude of C<table> and C<columns> is used. |
| 1049 | 627 |
|
| 1050 | 628 |
my $column_clause = $model->column_clause; |
| ... | ... |
@@ -1069,8 +647,7 @@ If you add some column, use C<add> option. |
| 1069 | 647 |
Model examples |
| 1070 | 648 |
|
| 1071 | 649 |
package MyDBI; |
| 1072 |
- |
|
| 1073 |
- use base 'DBIx::Custom'; |
|
| 650 |
+ use DBIx::Custom -base; |
|
| 1074 | 651 |
|
| 1075 | 652 |
sub connect {
|
| 1076 | 653 |
my $self = shift->SUPER::connect(@_); |
| ... | ... |
@@ -1084,98 +661,19 @@ Model examples |
| 1084 | 661 |
} |
| 1085 | 662 |
|
| 1086 | 663 |
package MyModel::book; |
| 1087 |
- use base 'DBIx::Custom::Model'; |
|
| 664 |
+ use DBIx::Custom::Model -base; |
|
| 1088 | 665 |
|
| 1089 |
- __PACKAGE__->attr('primary_key' => sub { ['id'] };
|
|
| 666 |
+ has primary_key => sub { ['id'] };
|
|
| 1090 | 667 |
|
| 1091 | 668 |
sub insert { ... }
|
| 1092 | 669 |
sub list { ... }
|
| 1093 | 670 |
|
| 1094 | 671 |
package MyModel::company; |
| 1095 |
- use base 'DBIx::Custom::Model'; |
|
| 672 |
+ use DBIx::Custom::Model -base; |
|
| 1096 | 673 |
|
| 1097 |
- __PACKAGE__->attr('primary_key' => sub { ['id'] };
|
|
| 674 |
+ has primary_key => sub { ['id'] };
|
|
| 1098 | 675 |
|
| 1099 | 676 |
sub insert { ... }
|
| 1100 | 677 |
sub list { ... }
|
| 1101 | 678 |
|
| 1102 |
-=head2 8. Improve performance |
|
| 1103 |
- |
|
| 1104 |
-=head3 Create query |
|
| 1105 |
- |
|
| 1106 |
-If you can't get performance, create query by C<query> option. |
|
| 1107 |
-For example, many insert is needed. |
|
| 1108 |
- |
|
| 1109 |
- my $params = [ |
|
| 1110 |
- {title => 'Perl', author => 'Ken'},
|
|
| 1111 |
- {title => 'Good day', author => 'Tom'}
|
|
| 1112 |
- ] |
|
| 1113 |
- my $query = $dbi->insert(table => 'book', param => $params->[0], query => 1); |
|
| 1114 |
- |
|
| 1115 |
-Return value is L<DBIx::Custom::Query> object. |
|
| 1116 |
-This query is executed by C<execute()>. |
|
| 1117 |
- |
|
| 1118 |
- foreach my $param (@$params) {
|
|
| 1119 |
- $dbi->execute($query, $param); |
|
| 1120 |
- } |
|
| 1121 |
- |
|
| 1122 |
-Performance is improved because statement handle is reused |
|
| 1123 |
-C<query> option is used in C<insert()>, C<update()>, C<update_all()>, |
|
| 1124 |
-C<delete()>, C<delete_all()>. |
|
| 1125 |
- |
|
| 1126 |
-Note that parameters count is same as method for creating query and C<execute()>. |
|
| 1127 |
- |
|
| 1128 |
-You can create query from any SQL by C<create_query()>. |
|
| 1129 |
- |
|
| 1130 |
- my $query = $dbi->create_query( |
|
| 1131 |
- "insert into book {insert_param title author};";
|
|
| 1132 |
- ); |
|
| 1133 |
- |
|
| 1134 |
-=head2 9. Other features |
|
| 1135 |
- |
|
| 1136 |
-=head3 Add method |
|
| 1137 |
- |
|
| 1138 |
-You can add method to L<DBIx::Custom> object. |
|
| 1139 |
-use C<method()>. |
|
| 1140 |
- |
|
| 1141 |
- $dbi->method( |
|
| 1142 |
- update_or_insert => sub {
|
|
| 1143 |
- my $self = shift; |
|
| 1144 |
- # something |
|
| 1145 |
- }, |
|
| 1146 |
- find_or_create => sub {
|
|
| 1147 |
- my $self = shift; |
|
| 1148 |
- # something |
|
| 1149 |
- } |
|
| 1150 |
- ); |
|
| 1151 |
- |
|
| 1152 |
-You can call these methods from L<DBIx::Custom> object. |
|
| 1153 |
- |
|
| 1154 |
- $dbi->update_or_insert; |
|
| 1155 |
- $dbi->find_or_create; |
|
| 1156 |
- |
|
| 1157 |
-=head3 Change result class |
|
| 1158 |
- |
|
| 1159 |
-You can change result class. By default it is L<DBIx::Custom::Result>. |
|
| 1160 |
- |
|
| 1161 |
- package MyResult; |
|
| 1162 |
- use base 'DBIx::Custom::Result'; |
|
| 1163 |
- |
|
| 1164 |
- sub some_method { ... }
|
|
| 1165 |
- |
|
| 1166 |
- 1; |
|
| 1167 |
- |
|
| 1168 |
- package main; |
|
| 1169 |
- |
|
| 1170 |
- use MyResult; |
|
| 1171 |
- |
|
| 1172 |
- my $dbi = DBIx::Custom->connect(...); |
|
| 1173 |
- $dbi->result_class('MyResult');
|
|
| 1174 |
- |
|
| 1175 |
-=head1 EXAMPLES |
|
| 1176 |
- |
|
| 1177 |
-You can see exsamples in the following wiki. |
|
| 1178 |
- |
|
| 1179 |
-L<DBIx::Custom Wiki|https://github.com/yuki-kimoto/DBIx-Custom/wiki> - Many useful examples |
|
| 1180 |
- |
|
| 1181 | 679 |
=cut |
| ... | ... |
@@ -1,9 +1,6 @@ |
| 1 | 1 |
package DBIx::Custom::Model; |
| 2 | 2 |
|
| 3 |
-use strict; |
|
| 4 |
-use warnings; |
|
| 5 |
- |
|
| 6 |
-use base 'Object::Simple'; |
|
| 3 |
+use Object::Simple -base; |
|
| 7 | 4 |
|
| 8 | 5 |
use Carp 'croak'; |
| 9 | 6 |
use DBIx::Custom::Util '_subname'; |
| ... | ... |
@@ -11,16 +8,14 @@ use DBIx::Custom::Util '_subname'; |
| 11 | 8 |
# Carp trust relationship |
| 12 | 9 |
push @DBIx::Custom::CARP_NOT, __PACKAGE__; |
| 13 | 10 |
|
| 14 |
-__PACKAGE__->attr( |
|
| 15 |
- ['dbi', 'name', 'table', 'view'], |
|
| 11 |
+has [qw/dbi name table view/], |
|
| 16 | 12 |
table_alias => sub { {} },
|
| 17 | 13 |
columns => sub { [] },
|
| 18 | 14 |
filter => sub { [] },
|
| 19 | 15 |
result_filter => sub { [] },
|
| 20 | 16 |
join => sub { [] },
|
| 21 | 17 |
type => sub { [] },
|
| 22 |
- primary_key => sub { [] }
|
|
| 23 |
-); |
|
| 18 |
+ primary_key => sub { [] };
|
|
| 24 | 19 |
|
| 25 | 20 |
our $AUTOLOAD; |
| 26 | 21 |
|
| ... | ... |
@@ -1,19 +1,14 @@ |
| 1 | 1 |
package DBIx::Custom::Query; |
| 2 | 2 |
|
| 3 |
-use strict; |
|
| 4 |
-use warnings; |
|
| 5 |
- |
|
| 6 |
-use base 'Object::Simple'; |
|
| 3 |
+use Object::Simple -base; |
|
| 7 | 4 |
|
| 8 | 5 |
use Carp 'croak'; |
| 9 | 6 |
use DBIx::Custom::Util '_subname'; |
| 10 | 7 |
|
| 11 |
-__PACKAGE__->attr( |
|
| 12 |
- [qw/sth filters/], |
|
| 8 |
+has [qw/sth filters/], |
|
| 13 | 9 |
sql => '', |
| 14 | 10 |
tables => sub { [] },
|
| 15 |
- columns => sub { [] }
|
|
| 16 |
-); |
|
| 11 |
+ columns => sub { [] };
|
|
| 17 | 12 |
|
| 18 | 13 |
sub filter {
|
| 19 | 14 |
my $self = shift; |
| ... | ... |
@@ -1,9 +1,6 @@ |
| 1 | 1 |
package DBIx::Custom::QueryBuilder; |
| 2 | 2 |
|
| 3 |
-use strict; |
|
| 4 |
-use warnings; |
|
| 5 |
- |
|
| 6 |
-use base 'Object::Simple'; |
|
| 3 |
+use Object::Simple -base; |
|
| 7 | 4 |
|
| 8 | 5 |
use Carp 'croak'; |
| 9 | 6 |
use DBIx::Custom::Query; |
| ... | ... |
@@ -1,17 +1,12 @@ |
| 1 | 1 |
package DBIx::Custom::Result; |
| 2 | 2 |
|
| 3 |
-use strict; |
|
| 4 |
-use warnings; |
|
| 5 |
- |
|
| 6 |
-use base 'Object::Simple'; |
|
| 3 |
+use Object::Simple -base; |
|
| 7 | 4 |
|
| 8 | 5 |
use Carp 'croak'; |
| 9 | 6 |
use DBIx::Custom::Util qw/_array_to_hash _subname/; |
| 10 | 7 |
|
| 11 |
-__PACKAGE__->attr( |
|
| 12 |
- [qw/filters filter_off sth type_rule type_rule_off/], |
|
| 13 |
- stash => sub { {} }
|
|
| 14 |
-); |
|
| 8 |
+has [qw/filters filter_off sth type_rule type_rule_off/], |
|
| 9 |
+ stash => sub { {} };
|
|
| 15 | 10 |
|
| 16 | 11 |
*all = \&fetch_hash_all; |
| 17 | 12 |
|
| ... | ... |
@@ -1,9 +1,6 @@ |
| 1 | 1 |
package DBIx::Custom::Where; |
| 2 | 2 |
|
| 3 |
-use strict; |
|
| 4 |
-use warnings; |
|
| 5 |
- |
|
| 6 |
-use base 'Object::Simple'; |
|
| 3 |
+use Object::Simple -base; |
|
| 7 | 4 |
|
| 8 | 5 |
use overload 'bool' => sub {1}, fallback => 1;
|
| 9 | 6 |
use overload '""' => sub { shift->to_string }, fallback => 1;
|
| ... | ... |
@@ -14,11 +11,9 @@ use Carp 'croak'; |
| 14 | 11 |
# Carp trust relationship |
| 15 | 12 |
push @DBIx::Custom::CARP_NOT, __PACKAGE__; |
| 16 | 13 |
|
| 17 |
-__PACKAGE__->attr( |
|
| 18 |
- [qw/param query_builder safety_character/], |
|
| 14 |
+has [qw/param query_builder safety_character/], |
|
| 19 | 15 |
clause => sub { [] },
|
| 20 |
- reserved_word_quote => '' |
|
| 21 |
-); |
|
| 16 |
+ reserved_word_quote => ''; |
|
| 22 | 17 |
|
| 23 | 18 |
sub new {
|
| 24 | 19 |
my $self = shift->SUPER::new(@_); |
| ... | ... |
@@ -1,9 +1,6 @@ |
| 1 | 1 |
package MyModel1::book; |
| 2 | 2 |
|
| 3 |
-use strict; |
|
| 4 |
-use warnings; |
|
| 5 |
- |
|
| 6 |
-use base 'DBIx::Custom::Model'; |
|
| 3 |
+use DBIx::Custom::Model -base; |
|
| 7 | 4 |
|
| 8 | 5 |
sub insert {
|
| 9 | 6 |
my ($self, $param) = @_; |
| ... | ... |
@@ -1,10 +1,7 @@ |
| 1 | 1 |
package MyModel5::table1; |
| 2 | 2 |
|
| 3 |
-use strict; |
|
| 4 |
-use warnings; |
|
| 3 |
+use MyModel5 -base; |
|
| 5 | 4 |
|
| 6 |
-use base 'MyModel5'; |
|
| 7 |
- |
|
| 8 |
-__PACKAGE__->attr('primary_key' => sub { ['key1', 'key2'] });
|
|
| 5 |
+has primary_key => sub { ['key1', 'key2'] };
|
|
| 9 | 6 |
|
| 10 | 7 |
1; |