Newer Older
1339 lines | 32.845kb
update pod
Yuki Kimoto authored on 2011-01-28
1
=encoding utf8
2

            
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
3
=head1 NAME
4

            
update pod
Yuki Kimoto authored on 2011-01-28
5
DBIx::Custom::Guide - DBIx::Custom Guide
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
6

            
pod fix
Yuki Kimoto authored on 2011-01-21
7
=head1 GUIDE
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
8

            
selection can contain where ...
Yuki Kimoto authored on 2011-03-06
9
(This module contain many experimental featrues.
10
 in each pod of module, experimental marked method
11
 is maybe changed without warnings)
update pod
Yuki Kimoto authored on 2011-01-26
12

            
update pod
Yuki Kimoto authored on 2011-01-28
13
L<DBIx::Custom> is the class to make easy to execute SQL.
14
This is L<DBI> wrapper class like L<DBIx::Class> or L<DBIx::Simple>.
15
You can do thing more easy than L<DBIx::Class>, more flexible
16
than L<DBIx::Simple>.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
17

            
update pod
Yuki Kimoto authored on 2011-02-11
18
L<DBIx::Custom> is not O/R mapper, O/R mapper is usefule, but
update pod
Yuki Kimoto authored on 2011-01-28
19
you must learn many things. Created SQL is sometimes inefficient,
20
and in many cases you create raw SQL because
21
O/R mapper can't make complex SQL
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
22

            
update pod
Yuki Kimoto authored on 2011-01-28
23
L<DBIx::Custom> is opposit of O/R mapper.
24
The main purpose is that we respect SQL
25
and make easy difficult works if you use only L<DBI>.
26
If you already learn SQL, it is easy to use L<DBIx::Custom>.
deprecated DBIx::Custom::MyS...
root authored on 2010-11-26
27

            
update pod
Yuki Kimoto authored on 2011-01-28
28
I explain L<DBIx::Custom> a little in this section.
29
In L<DBIx::Custom>, you embbed tag in SQL.
deprecated DBIx::Custom::MyS...
root authored on 2010-11-26
30

            
update pod
Yuki Kimoto authored on 2011-01-28
31
    select * from book where {= title} and {=author};
deprecated DBIx::Custom::MyS...
root authored on 2010-11-26
32

            
update pod
Yuki Kimoto authored on 2011-01-28
33
The part arround {} is tag.
34
This SQL is converted to the one which contains place holder.
35

            
36
    select * from book where title = ? and author = ?;
37

            
38
Maybe you ask me that this conversion is meaningful.
39
On the top of this, usuful features is implemented.
40
See the following descriptions.
41

            
42
=over 4
43

            
44
=item 1. Specify place holder binding value as hash refernce
45

            
46
If you use L<DBI>, you must specify place holder binding value
47
as array.
48

            
49
    $sth->execute(@bind);
50

            
51
If you use L<DBIx::Custom>, you specify it as hash reference.
52
    
53
    my $param = {title => 'Perl', author => 'Ken'};
54
    $dbi->execute($sql, $param);
55

            
56
=item 2. Filtering
57

            
58
L<DBIx::Custom> provides filtering system.
59
For example, You think that about date value you want to 
60
manipulate it as date object like L<Time::Piece> in Perl,
61
and want to convert it to database DATE format.
62
and want to do reverse.
63

            
64
You can use filtering system.
65

            
66
At first, register filter.
67

            
68
    $dbi->register_filter(
69
        tp_to_date => sub {
70
            ...
71
        },
72
        date_to_tp => sub {
73
            ...
74
        }
75
    );
76

            
77
next, apply this filter to each column.
78

            
79
    $dbi->apply_filter('book',
80
        'issue_date' => {out => 'tp_to_date', in => 'date_to_tp'}
81
    );
82

            
83
C<out> is perl-to-database way. C<in> is perl-from-database way.
84

            
85
This filter is automatically enabled in many method.
86

            
87
    $dbi->insert(table => 'book', param => {issue_date => $tp});
88

            
89

            
90
=item 3. Selective search condition
91

            
92
It is difficult to create selective where clause in L<DBI>.
93
For example, If C<title> and C<author> is specified, we create 
94
the following SQL.
95

            
96
    select * from book where title = ? and author = ?;
97

            
98
If only C<title> is specified, the following one
99

            
100
    select * from book where title = ?;
101

            
102
If only C<author> is specified, the following one,
103

            
104
    select * from book where author = ?;
105

            
106
This is hard work. Generally we use modules like L<SQL::Abstract>.
107
L<DBIx::Custom> prepare the way to make it easy.
108

            
109
    # Where object
110
    my $where = $dbi->where;
111
    
112
    # Search condition
113
    $where->clause(
114
        ['and', '{= title}', {'= author'}]
115
    );
116
    
117
    # Setting to automatically select needed column
118
    $where->param({title => 'Perl'});
119

            
120
    # Embbed where clause to SQL
121
    my $sql = "select * from book $where";
122

            
123
You can create where clause which has selected search condition.
124
You can write nesting of where clause and C<or> condition
125

            
update pod
Yuki Kimoto authored on 2011-01-30
126
=item 4. Methods for insert, update, delete, select
update pod
Yuki Kimoto authored on 2011-01-28
127

            
update pod
Yuki Kimoto authored on 2011-01-30
128
L<DBIx::Custom> provides methods for insert, update, delete, select
129
There are C<insert()>, C<update()>, C<delete()>,C<select()>.
update pod
Yuki Kimoto authored on 2011-01-28
130

            
131
    my $param = {title => 'Perl', author => 'Ken'};
132
    $dbi->insert(table => 'book', param => $param);
133

            
update pod
Yuki Kimoto authored on 2011-01-30
134
=item 5. Register method for table.
deprecated DBIx::Custom::MyS...
root authored on 2010-11-26
135

            
update pod
Yuki Kimoto authored on 2011-01-30
136
You can register method for table.
update pod
Yuki Kimoto authored on 2011-01-28
137

            
update pod
Yuki Kimoto authored on 2011-01-30
138
    $dbi->table('book')->method(
update pod
Yuki Kimoto authored on 2011-01-28
139
        list => sub {
140
            ...
141
        },
update pod
Yuki Kimoto authored on 2011-01-30
142
        something => sub {
143
            ...
update pod
Yuki Kimoto authored on 2011-01-28
144
        }
145
    );
146

            
update pod
Yuki Kimoto authored on 2011-01-30
147
use the mehtod.
update pod
Yuki Kimoto authored on 2011-01-28
148

            
149
    $dbi->table('book')->list;
150

            
update pod
Yuki Kimoto authored on 2011-01-30
151
Many O/R mapper must create class for table,
152
but L<DBIx::Custom> make it easy.
update pod
Yuki Kimoto authored on 2011-01-28
153

            
154
=back
155

            
update pod
Yuki Kimoto authored on 2011-01-30
156
L<DBIx::Custom> is very useful.
157
See the following if you are interested in it.
update pod
Yuki Kimoto authored on 2011-01-28
158

            
update pod
Yuki Kimoto authored on 2011-01-30
159
=head2 1. Connect to database
update pod
Yuki Kimoto authored on 2011-01-28
160

            
update pod
Yuki Kimoto authored on 2011-01-30
161
Load L<DBIx::Custom>.
update pod
Yuki Kimoto authored on 2011-01-28
162

            
163
    use DBIx::Custom;
164

            
update pod
Yuki Kimoto authored on 2011-01-30
165
use C<connect()> to connect to database.
166
Return value is L<DBIx::Custom> object.
update pod
Yuki Kimoto authored on 2011-01-28
167

            
168
    my $dbi = DBIx::Custom->connect(
update pod
Yuki Kimoto authored on 2011-01-30
169
        data_source => "dbi:mysql:database=bookstore",
update pod
Yuki Kimoto authored on 2011-01-28
170
        user => 'ken',
171
        password => '!LFKD%$&',
172
        dbi_options => {mysql_enable_utf8 => 1}
173
    );
174

            
update pod
Yuki Kimoto authored on 2011-01-30
175
C<data_source> must be one corresponding to the database system.
176
The following ones are data source example.
update pod
Yuki Kimoto authored on 2011-01-28
177

            
178
B<MySQL>
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
179

            
180
    "dbi:mysql:database=$database"
181
    "dbi:mysql:database=$database;host=$hostname;port=$port"
182

            
update pod
Yuki Kimoto authored on 2011-01-28
183
B<SQLite>
184

            
185
    "dbi:SQLite:dbname=$database"
186
    "dbi:SQLite:dbname=:memory:"
187

            
188
B<PostgreSQL>
deprecated DBIx::Custom::MyS...
root authored on 2010-11-26
189

            
190
    "dbi:Pg:dbname=$dbname"
191

            
update pod
Yuki Kimoto authored on 2011-01-28
192
B<Oracle>
deprecated DBIx::Custom::MyS...
root authored on 2010-11-26
193

            
194
    "dbi:Oracle:$dbname"
195
    "dbi:Oracle:host=$host;sid=$sid"
196

            
update pod
Yuki Kimoto authored on 2011-01-28
197
B<ODBC(Microsoft Access)>
deprecated DBIx::Custom::MyS...
root authored on 2010-11-26
198

            
199
    "dbi:ODBC:driver=Microsoft Access Driver (*.mdb);dbq=hoge.mdb"
200

            
update pod
Yuki Kimoto authored on 2011-01-28
201
B<ODBC(SQL Server)>
deprecated DBIx::Custom::MyS...
root authored on 2010-11-26
202

            
203
   "dbi:ODBC:driver={SQL Server};Server=(local);database=test;Trusted_Connection=yes;AutoTranslate=No;"
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
204

            
update pod
Yuki Kimoto authored on 2011-01-30
205
If authentication is needed, you can specify C<user> and C<password>
206

            
207
L<DBIx::Custom> is wrapper class of L<DBI>.
208
You can use all methods of L<DBI> from L<DBIx::Custom> object.
209

            
210
    $dbi->do(...);
211
    $dbi->begin_work;
update pod
Yuki Kimoto authored on 2011-01-28
212

            
update pod
Yuki Kimoto authored on 2011-01-30
213
use C<dhb()> to get database handle of L<DBI>
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
214

            
update pod
Yuki Kimoto authored on 2011-01-28
215
    my $dbh = $dbi->dbh;
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
216

            
update pod
Yuki Kimoto authored on 2011-01-30
217
By default, the following ones is set to database handle attributes.
218

            
219
    RaiseError  ->  1
220
    PrintError  ->  0
221
    AutoCommit  ->  1
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
222

            
update pod
Yuki Kimoto authored on 2011-01-30
223
If fatal error occuer, program terminate.
224
If SQL is executed, commit is executed automatically.
update pod
Yuki Kimoto authored on 2011-01-28
225

            
update pod
Yuki Kimoto authored on 2011-01-31
226
=head2 2. Methods for insert, update, delete, or insert
update pod
Yuki Kimoto authored on 2011-01-28
227

            
update pod
Yuki Kimoto authored on 2011-01-31
228
There are following methods.
update pod
Yuki Kimoto authored on 2011-01-28
229

            
update pod
Yuki Kimoto authored on 2011-01-31
230
=head3 C<insert()>
update pod
Yuki Kimoto authored on 2011-01-28
231

            
update pod
Yuki Kimoto authored on 2011-01-31
232
use C<insert()> to insert row into database
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
233

            
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
234
    $dbi->insert(table  => 'book',
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
235
                 param  => {title => 'Perl', author => 'Ken'});
236

            
update pod
Yuki Kimoto authored on 2011-01-31
237
C<table> is table name, C<param> is insert data.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
238

            
update pod
Yuki Kimoto authored on 2011-01-31
239
Following SQL is executed.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
240

            
update pod
Yuki Kimoto authored on 2011-01-28
241
    insert into (title, author) values (?, ?);
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
242

            
update pod
Yuki Kimoto authored on 2011-01-31
243
=head3 C<update()>
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
244

            
update pod
Yuki Kimoto authored on 2011-01-31
245
use C<update()> to update row in database.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
246

            
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
247
    $dbi->update(table  => 'book', 
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
248
                 param  => {title => 'Perl', author => 'Ken'}, 
249
                 where  => {id => 5});
250

            
update pod
Yuki Kimoto authored on 2011-01-31
251
C<table> is table name, C<param> is update data, C<where> is condition.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
252

            
update pod
Yuki Kimoto authored on 2011-01-31
253
Following SQL is executed.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
254

            
update pod
Yuki Kimoto authored on 2011-01-28
255
    update book set title = ?, author = ?;
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
256

            
update pod
Yuki Kimoto authored on 2011-01-31
257
You can't execute C<update()> without C<where> for safety.
258
use C<update_all()> if you want to update all rows.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
259

            
update pod
Yuki Kimoto authored on 2011-01-28
260
    $dbi->update_all(table  => 'book', 
261
                     param  => {title => 'Perl', author => 'Ken'});
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
262

            
update pod
Yuki Kimoto authored on 2011-01-31
263
=head3 C<delete()>
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
264

            
update pod
Yuki Kimoto authored on 2011-01-31
265
use C<delete()> to delete rows from database.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
266

            
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
267
    $dbi->delete(table  => 'book',
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
268
                 where  => {author => 'Ken'});
269

            
update pod
Yuki Kimoto authored on 2011-01-31
270
C<table> is table name, C<where> is condition.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
271

            
update pod
Yuki Kimoto authored on 2011-01-31
272
Following SQL is executed.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
273

            
update pod
Yuki Kimoto authored on 2011-01-28
274
    delete from book where id = ?;
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
275

            
update pod
Yuki Kimoto authored on 2011-01-31
276
You can't execute C<delete()> without C<where> for safety.
277
use C<delete_all()> if you want to delete all rows.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
278

            
update pod
Yuki Kimoto authored on 2011-01-28
279
    $dbi->delete_all(table  => 'book');
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
280

            
update pod
Yuki Kimoto authored on 2011-01-31
281
=head3 C<select()>
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
282

            
update pod
Yuki Kimoto authored on 2011-01-31
283
use C<select()> to select rows from database
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
284

            
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
285
    my $result = $dbi->select(table => 'book');
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
286

            
update pod
Yuki Kimoto authored on 2011-01-31
287
Following SQL is executed.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
288

            
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
289
    select * from book;
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
290

            
update pod
Yuki Kimoto authored on 2011-01-31
291
Return value is L<DBIx::Custom::Result> object.
292
use C<fetch()> to fetch row.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
293

            
294
    while (my $row = $result->fetch) {
295
        my $title  = $row->[0];
296
        my $author = $row->[1];
297
    }
298

            
update pod
Yuki Kimoto authored on 2011-01-31
299
See L<3. Fetch row/"3. Fetch row"> about L<DBIx::Custom::Result>.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
300

            
update pod
Yuki Kimoto authored on 2011-01-31
301
Continue more examples.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
302

            
303
    my $result = $dbi->select(
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
304
        table  => 'book',
update pod
Yuki Kimoto authored on 2011-01-28
305
        column => ['author',  'title'],
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
306
        where  => {author => 'Ken'}
307
    );
308

            
update pod
Yuki Kimoto authored on 2011-01-31
309
C<column> is column names, C<where> is condition.
310

            
311
Following SQL is executed.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
312

            
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
313
    select author, title from book where author = ?;
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
314

            
update pod
Yuki Kimoto authored on 2011-01-31
315
Next example.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
316

            
317
    my $result = $dbi->select(
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
318
        table    => 'book',
update pod
Yuki Kimoto authored on 2011-01-31
319
        where    => {'book.name' => 'Perl'},
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
320
        relation => {'book.id' => 'rental.book_id'}
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
321
    );
322

            
update pod
Yuki Kimoto authored on 2011-01-31
323
C<relation> is relation of tables. This is inner join.
324

            
325
Following SQL is executed.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
326

            
update pod
Yuki Kimoto authored on 2011-01-28
327
    select * from book, rental where book.name = ? and book.id = rental.book_id;
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
328

            
update pod
Yuki Kimoto authored on 2011-01-31
329
Next example.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
330

            
331
    my $result = $dbi->select(
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
332
        table  => 'book',
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
333
        where  => {author => 'Ken'},
update pod
Yuki Kimoto authored on 2011-01-28
334
        append => 'for update',
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
335
    );
336

            
update pod
Yuki Kimoto authored on 2011-01-31
337
C<append> is string appending to end of SQL.
338

            
339
Following SQL is executed.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
340

            
update pod
Yuki Kimoto authored on 2011-01-28
341
    select * book where author = ? for update;
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
342

            
update pod
Yuki Kimoto authored on 2011-01-31
343
C<appned> is also used at C<insert()>, C<update()>, C<update_all()>
344
C<delete()>, C<delete_all()>, and C<select()>.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
345

            
update pod
Yuki Kimoto authored on 2011-02-11
346
Instead of C<column> and C<table>,
347
you can use C<selection>.
348
This is used to specify column names and table names at once
349

            
350
    my $selection = <<"EOS";
351
    title, author, company_name
352
    from book inner join company on book.company_id = company.id
353
    EOS
354

            
355
    $dbi->select(selection => $selection);
356

            
357
Note that you can't use where clause in C<selection>.
358
use clause like "inner join".
359

            
update pod
Yuki Kimoto authored on 2011-02-11
360
=head3 C<execute()>
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
361

            
update pod
Yuki Kimoto authored on 2011-01-31
362
use C<execute()> to execute SQL
update pod
Yuki Kimoto authored on 2011-01-28
363

            
364
    $dbi->execute("select * from book;");
365

            
update pod
Yuki Kimoto authored on 2011-01-31
366
Process tag and execute SQL.
update pod
Yuki Kimoto authored on 2011-01-28
367

            
368
    $dbi->execute(
369
        "select * from book {= title} and {= author};"
370
        param => {title => 'Perl', author => 'Ken'}
371
    );
372

            
update pod
Yuki Kimoto authored on 2011-01-31
373
Following SQL is executed.
update pod
Yuki Kimoto authored on 2011-01-28
374

            
375
    select * from book title = ? and author = ?;
376

            
update pod
Yuki Kimoto authored on 2011-01-31
377
Values of title and author is embbdeded into placeholder.
update pod
Yuki Kimoto authored on 2011-01-28
378

            
update pod
Yuki Kimoto authored on 2011-01-31
379
See L<5. Tag/"5. Tag"> about tag.
update pod
Yuki Kimoto authored on 2011-01-28
380

            
update pod
Yuki Kimoto authored on 2011-01-31
381
You don't have to wirte last semicolon in C<execute()>. 
update pod
Yuki Kimoto authored on 2011-01-28
382

            
383
    $dbi->execute('select * from book');
384

            
update pod
Yuki Kimoto authored on 2011-02-28
385
=head3 insert by using primary key : C<insert_at()>
386

            
387
To insert row by using primary key, use C<insert_at()>
388

            
389
    $dbi->insert_at(
390
        table => 'book', primary_key => ['id'],
391
        where => ['123'], param => {name => 'Ken'}
392
    );
393

            
394
In this example, row which id column is 123 is inserted.
395
NOTE that you must pass array reference as C<where>.
396
If C<param> contains primary key, the key and value is delete from C<param>.
397

            
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
398
=head3 Update by using primary key : C<update_at()>
399

            
400
To update row by using primary key, use C<update_at()>
401

            
402
    $dbi->update_at(
403
        table => 'book', primary_key => ['id'],
404
        where => ['123'], param => {name => 'Ken'}
405
    );
406

            
407
In this example, row which id column is 123 is updated.
408
NOTE that you must pass array reference as C<where>.
409
If C<param> contains primary key, the key and value is delete from C<param>.
410

            
411
=head3 Delete by using primary key : C<delete_at()>
412

            
413
To delete row by using primary key, use C<delete_at()>
414

            
415
    $dbi->delete_at(table => 'book', primary_key => ['id'], where => ['123']);
416

            
417
In this example, row which id column is 123 is deleted.
418
NOTE that you must pass array reference as C<where>.
419

            
420
You can also write arguments like this.
421

            
422
    $dbi->delete_at(table => 'book', primary_key => ['id'], param => {id => '123'});
423

            
424
=head3 Select by using primary key : C<select_at()>
425

            
426
To select row by using primary key, use C<select_at()>.
427

            
428
    $dbi->select_at(table => 'book', primary_key => ['id'], where => ['123']);
429

            
430
In this example, row which id colunm is 123 is selected.
431
NOTE that you must pass array reference as C<where>.
432

            
433
You can also write arguments like this.
434

            
435
    $dbi->select_at(table => 'book', primary_key => ['id'], param => {id => '123'});
436

            
update pod
Yuki Kimoto authored on 2011-02-04
437
=head2 3. Fetch row
update pod
Yuki Kimoto authored on 2011-01-28
438

            
update pod
Yuki Kimoto authored on 2011-02-04
439
Return value of C<select()> is L<DBIx::Custom::Result> object.
440
There are many methods to fetch row.
update pod
Yuki Kimoto authored on 2011-01-28
441

            
update pod
Yuki Kimoto authored on 2011-02-11
442
=head3 Fetch a row (array) : C<fetch()>
update pod
Yuki Kimoto authored on 2011-01-28
443

            
update pod
Yuki Kimoto authored on 2011-02-04
444
use C<fetch()> to fetch a row and assign it into array reference.
445

            
446
    my $row = $result->fetch;
447

            
448
You can get all rows.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
449

            
450
    while (my $row = $result->fetch) {
update pod
Yuki Kimoto authored on 2011-01-28
451
        my $title  = $row->[0];
452
        my $author = $row->[1];
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
453
    }
454

            
update pod
Yuki Kimoto authored on 2011-02-11
455
=head3 Fetch only first row (array) : C<fetch_first()>
update pod
Yuki Kimoto authored on 2011-01-28
456

            
update pod
Yuki Kimoto authored on 2011-02-04
457
use C<fetch_first()> to fetch only first row.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
458

            
459
    my $row = $result->fetch_first;
460

            
update pod
Yuki Kimoto authored on 2011-02-04
461
You can't fetch rest rows
462
because statement handle C<finish()> is executed.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
463

            
update pod
Yuki Kimoto authored on 2011-02-11
464
=head3 Fetch rows (array) : C<fetch_multi()>
update pod
Yuki Kimoto authored on 2011-01-28
465

            
update pod
Yuki Kimoto authored on 2011-02-04
466
use C<fetch_multi()> to fetch rows and assign it into
467
array reference which has array references as element.
update pod
Yuki Kimoto authored on 2011-01-28
468

            
469
    while (my $rows = $result->fetch_multi(2)) {
470
        my $title0   = $rows->[0][0];
471
        my $author0  = $rows->[0][1];
472
        
473
        my $title1   = $rows->[1][0];
474
        my $author1  = $rows->[1][1];
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
475
    }
update pod
Yuki Kimoto authored on 2011-01-28
476

            
update pod
Yuki Kimoto authored on 2011-02-04
477
Specify row count as argument.
update pod
Yuki Kimoto authored on 2011-01-28
478

            
update pod
Yuki Kimoto authored on 2011-02-04
479
You can get the following data.
update pod
Yuki Kimoto authored on 2011-01-28
480

            
481
    [
482
        ['Perl', 'Ken'],
483
        ['Ruby', 'Mark']
484
    ]
485

            
update pod
Yuki Kimoto authored on 2011-02-11
486
=head3 Fetch all rows (array) : C<fetch_all>
update pod
Yuki Kimoto authored on 2011-01-28
487

            
update pod
Yuki Kimoto authored on 2011-02-04
488
use C<fetch_all()> to fetch all rows and assign it into
489
array reference which has array reference as element.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
490

            
491
    my $rows = $result->fetch_all;
492

            
update pod
Yuki Kimoto authored on 2011-02-04
493
You can get the following data.
update pod
Yuki Kimoto authored on 2011-01-28
494

            
495
    [
496
        ['Perl', 'Ken'],
497
        ['Ruby', 'Mark']
498
    ]
499

            
update pod
Yuki Kimoto authored on 2011-02-11
500
=head3 Fetch a row (hash) : C<fetch_hash()>
update pod
Yuki Kimoto authored on 2011-01-28
501

            
update pod
Yuki Kimoto authored on 2011-02-04
502
use C<fetch_hash()> to fetch a row and assign it into hash reference.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
503

            
504
    while (my $row = $result->fetch_hash) {
505
        my $title  = $row->{title};
506
        my $author = $row->{author};
507
    }
508

            
update pod
Yuki Kimoto authored on 2011-02-11
509
=head3 Fetch only first row (hash) : C<fetch_hash_first()>
update pod
Yuki Kimoto authored on 2011-01-28
510

            
update pod
Yuki Kimoto authored on 2011-02-04
511
use C<fetch_hash_first()> to fetch only first row
512
and assign it into hash reference.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
513

            
514
    my $row = $result->fetch_hash_first;
update pod
Yuki Kimoto authored on 2011-01-28
515

            
update pod
Yuki Kimoto authored on 2011-02-04
516
You can't fetch rest rows
517
because statement handle C<finish()> is executed.
update pod
Yuki Kimoto authored on 2011-01-28
518

            
update pod
Yuki Kimoto authored on 2011-02-11
519
=head3 Fetch rows (hash) : C<fetch_hash_multi()>
update pod
Yuki Kimoto authored on 2011-01-28
520

            
update pod
Yuki Kimoto authored on 2011-02-04
521
use C<fetch_hash_multi()> to fetch rows and
522
assign it into array reference which has hash references as element.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
523

            
524
    while (my $rows = $result->fetch_hash_multi(5)) {
update pod
Yuki Kimoto authored on 2011-01-28
525
        my $title0   = $rows->[0]{title};
526
        my $author0  = $rows->[0]{author};
527
        my $title1  = $rows->[1]{title};
528
        my $author1 = $rows->[1]{author};
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
529
    }
update pod
Yuki Kimoto authored on 2011-01-28
530

            
update pod
Yuki Kimoto authored on 2011-02-04
531
Specify row count as argument.
update pod
Yuki Kimoto authored on 2011-01-28
532

            
update pod
Yuki Kimoto authored on 2011-02-04
533
You can get the following data.
update pod
Yuki Kimoto authored on 2011-01-28
534

            
535
    [
536
        {title => 'Perl', author => 'Ken'},
537
        {title => 'Ruby', author => 'Mark'}
538
    ]
539

            
update pod
Yuki Kimoto authored on 2011-02-11
540
=head3 Fetch all rows (hash) : C<fetch_hash_all()>
update pod
Yuki Kimoto authored on 2011-01-28
541

            
update pod
Yuki Kimoto authored on 2011-02-04
542
use C<fetch_hash_all()> to fetch all rows and
543
assign it into array reference which has hash 
544
references as element.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
545

            
546
    my $rows = $result->fetch_hash_all;
547

            
update pod
Yuki Kimoto authored on 2011-02-04
548
You can get the following data.
update pod
Yuki Kimoto authored on 2011-01-28
549

            
550
    [
551
        {title => 'Perl', author => 'Ken'},
552
        {title => 'Ruby', author => 'Mark'}
553
    ]
554

            
update pod
Yuki Kimoto authored on 2011-02-11
555
=head3 Statement handle : C<sth()>
update pod
Yuki Kimoto authored on 2011-01-28
556

            
update pod
Yuki Kimoto authored on 2011-02-04
557
use <sth()> to get statement handle.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
558

            
559
    my $sth = $result->sth;
560

            
update pod
Yuki Kimoto authored on 2011-02-07
561
=head2 4. Filtering
update pod
Yuki Kimoto authored on 2011-01-28
562

            
update pod
Yuki Kimoto authored on 2011-02-07
563
L<DBIx::Custom> provide value filtering. 
564
For example, You maybe want to convert L<Time::Piece> object to
565
database date format when register data into database.
566
and convert database date fromat to L<Time::Piece> object
567
when get data from database.
update pod
Yuki Kimoto authored on 2011-01-28
568

            
update pod
Yuki Kimoto authored on 2011-02-11
569
=head3 Register filter : C<register_filter()>
update pod
Yuki Kimoto authored on 2011-01-28
570

            
update pod
Yuki Kimoto authored on 2011-02-11
571
use C<register_filter()> to register filter.
update pod
Yuki Kimoto authored on 2011-01-28
572

            
573
    $dbi->register_filter(
574
        # Time::Piece object to DATE format
575
        tp_to_date => sub {
576
            my $date = shift;
577

            
578
            return '0000-00-00' unless $tp;
579
            return $tp->strftime('%Y-%m-%d');
580
        },
581
        
582
        # DATE to Time::Piece object
583
        date_to_tp => sub {
584
            my $date = shift;
585

            
586
            return if $date eq '0000-00-00';
587
            return Time::Piece->strptime($date, '%Y-%m-%d');
588
        },
589
    );
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
590

            
update pod
Yuki Kimoto authored on 2011-02-07
591
Registered filter is used by C<apply_filter()> or etc.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
592

            
update pod
Yuki Kimoto authored on 2011-02-11
593
=head3 Apply filter : C<apply_filter()>
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
594

            
update pod
Yuki Kimoto authored on 2011-02-07
595
use C<apply_filter()> to apply registered filter.
update pod
Yuki Kimoto authored on 2011-01-28
596

            
597
    $dbi->apply_filter('book',
598
        issue_date => {out => 'tp_to_date', in => 'date_to_tp'},
599
        first_issue_date => {out => 'tp_to_date', in => 'date_to_tp'}
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
600
    );
601

            
update pod
Yuki Kimoto authored on 2011-02-07
602
First argument is table name. Arguments after first argument are pairs of column
603
name and fitering rule. C<out> of filtering rule is filter which is used when data
604
is send to database. C<in> of filtering rule is filter which is used when data
605
is got from database. 
606

            
607
You can specify code reference as filter.
update pod
Yuki Kimoto authored on 2011-01-28
608

            
609
    issue_date => {out => sub { ... }, in => sub { ... }}
610

            
update pod
Yuki Kimoto authored on 2011-02-07
611
Applied filter become effective at insert()>, C<update()>, C<update_all()>,
612
C<delete()>, C<delete_all()>, C<select()>.
update pod
Yuki Kimoto authored on 2011-01-28
613

            
614
    my $tp = Time::Piece->strptime('2010/10/14', '%Y/%m/%d');
update pod
Yuki Kimoto authored on 2011-02-07
615
    my $result = $dbi->select(table => 'book', where => {issue_date => $tp});
update pod
Yuki Kimoto authored on 2011-01-28
616

            
update pod
Yuki Kimoto authored on 2011-02-07
617
When data is send to database, L<Time::Piece> object is converted
618
to database date format "2010-10-14"
update pod
Yuki Kimoto authored on 2011-01-28
619

            
update pod
Yuki Kimoto authored on 2011-02-07
620
When data is fetched, database date format is
621
converted to L<Time::Piece> object.
update pod
Yuki Kimoto authored on 2011-01-28
622

            
623
    my $row = $resutl->fetch_hash_first;
624
    my $tp = $row->{issue_date};
625

            
update pod
Yuki Kimoto authored on 2011-02-07
626
You can also use column name which contains table name.
update pod
Yuki Kimoto authored on 2011-01-28
627

            
628
    $dbi->select(
629
        table => 'book',
update pod
Yuki Kimoto authored on 2011-02-07
630
        where => {'book.issue_date' => $tp}
update pod
Yuki Kimoto authored on 2011-01-28
631
    );
632

            
update pod
Yuki Kimoto authored on 2011-02-11
633
In fetching, Filter is effective if you use "TABLE__COLUMN" as column name.
634

            
635
    my $result = $dbi->execute(
636
       "select issue_date as book__issue_date from book");
637

            
638
You can apply C<end> filter execute after C<in> filter.
639

            
640
    $dbi->apply_filter('book',
641
        issue_date => {out => 'tp_to_date', in => 'date_to_tp',
642
                       end => 'tp_to_displaydate'},
643
    );
644

            
update pod
Yuki Kimoto authored on 2011-02-07
645
=head3 Individual filter C<filter>
update pod
Yuki Kimoto authored on 2011-01-28
646

            
update pod
Yuki Kimoto authored on 2011-02-07
647
You can apply individual filter .
648
This filter overwrite the filter by C<apply_filter()>
update pod
Yuki Kimoto authored on 2011-01-28
649

            
update pod
Yuki Kimoto authored on 2011-02-07
650
use C<filter> option to apply individual filter
651
when data is send to database.
652
This option is used at C<insert()>, C<update()>,
653
C<update_all()>, C<delete()>, C<delete_all()>, C<select()>,
654
C<execute()>.
update pod
Yuki Kimoto authored on 2011-01-28
655

            
update pod
Yuki Kimoto authored on 2011-02-07
656
C<insert()> example:
update pod
Yuki Kimoto authored on 2011-01-28
657

            
658
    $dbi->insert(
659
        table => 'book',
660
        param => {issue_date => $tp, first_issue_date => $tp},
661
        filter => {issue_date => 'tp_to_date', first_issue_date => 'tp_to_date'}
662
    );
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
663

            
update pod
Yuki Kimoto authored on 2011-02-07
664
C<execute()> example:
update pod
Yuki Kimoto authored on 2011-01-28
665

            
666
my $sql = <<"EOS";
667
select YEAR(issue_date) as issue_year
668
from book
669
where YEAR(issue_date) = {? issue_year}
670
EOS
671
   
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
672
    my $result = $dbi->execute(
update pod
Yuki Kimoto authored on 2011-01-28
673
        $sql,
674
        param => {issue_year => '2010'},
675
        filter => {issue_year => 'tp_to_year'}
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
676
    );
677

            
update pod
Yuki Kimoto authored on 2011-02-07
678
You can also apply indivisual filter when you fetch row.
679
use C<DBIx::Custom::Result>'s C<filter()>.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
680

            
update pod
Yuki Kimoto authored on 2011-01-28
681
    $result->filter(issue_year => 'year_to_tp');
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
682

            
selection can contain where ...
Yuki Kimoto authored on 2011-03-06
683
You can remove filter by C<remove_filter()>
684

            
685
    $result->remove_filter;
686

            
update pod
Yuki Kimoto authored on 2011-02-07
687
=head3 End filtering : C<end_filter()>
update pod
Yuki Kimoto authored on 2011-01-28
688

            
update pod
Yuki Kimoto authored on 2011-02-07
689
You can add filter at end.
690
It is useful to create last output.
691
use C<end_filter()> to add end filter.
update pod
Yuki Kimoto authored on 2011-01-28
692

            
693
    $result->end_filter(issue_date => sub {
694
        my $tp = shift;
695
        
696
        return '' unless $tp;
697
        return $tp->strftime('%Y/%m/%d %h:%m:%s (%a)');
698
    });
699

            
update pod
Yuki Kimoto authored on 2011-02-07
700
In this example, L<Time::Piece> object is converted to readable format.
update pod
Yuki Kimoto authored on 2011-01-28
701

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-02-28
702
You can remove end_filter by C<end_filter>
703

            
704
    $result->remove_end_filter;
705

            
update pod
Yuki Kimoto authored on 2011-02-11
706
=head3 Automate applying filter : C<each_column()>
update pod
Yuki Kimoto authored on 2011-01-28
707

            
update pod
Yuki Kimoto authored on 2011-02-07
708
It is useful to apply filter automatically at date type columns.
709
You can use C<each_column()> to process all column infos.
update pod
Yuki Kimoto authored on 2011-01-28
710

            
711
    $dbi->each_column(
712
        sub {
713
            my ($self, $table, $column, $info) = @_;
714
            
715
            my $type = $info->{TYPE_NAME};
716
            
717
            my $filter = $type eq 'DATE'     ? {out => 'tp_to_date', in => 'date_to_tp'}
718
                       : $type eq 'DATETIME' ? {out => 'tp_to_datetime', in => 'datetime_to_tp'}
719
                                             : undef;
720
            
721
            $self->apply_filter($table, $column, $filter)
722
              if $filter;
723
        }
724
    );
725

            
update pod
Yuki Kimoto authored on 2011-02-11
726
C<each_column()> receive callback.
update pod
Yuki Kimoto authored on 2011-02-07
727
callback arguments are L<DBIx::Custom> object, table name, column name, column information.
728
Filter is applied automatically by column type.
update pod
Yuki Kimoto authored on 2011-01-28
729

            
update pod
Yuki Kimoto authored on 2011-02-07
730
=head2 5. Tag
update pod
Yuki Kimoto authored on 2011-01-28
731

            
update pod
Yuki Kimoto authored on 2011-02-07
732
=head3 Basic of Tag
update pod
Yuki Kimoto authored on 2011-01-28
733

            
update pod
Yuki Kimoto authored on 2011-02-07
734
You can embedd tag into SQL.
update pod
Yuki Kimoto authored on 2011-01-28
735

            
736
    select * from book where {= title} and {like author};
737

            
update pod
Yuki Kimoto authored on 2011-02-07
738
{= title} and {like author} are tag. Tag has the folloring format.
update pod
Yuki Kimoto authored on 2011-01-28
739

            
update pod
Yuki Kimoto authored on 2011-02-07
740
    {TAG_NAME ARG1 ARG2 ...}
update pod
Yuki Kimoto authored on 2011-01-28
741

            
update pod
Yuki Kimoto authored on 2011-02-07
742
Tag start C<{> and end C<}>. 
update pod
Yuki Kimoto authored on 2011-02-11
743
Don't insert space between C<{}> and tag name.
update pod
Yuki Kimoto authored on 2011-02-07
744

            
745
C<{> and C<}> are reserved word.
746
If you want to use these, escape it by '\';
update pod
Yuki Kimoto authored on 2011-01-28
747

            
748
    select from book \\{ ... \\}
749

            
update pod
Yuki Kimoto authored on 2011-02-07
750
\ is perl's escape character, you need two \.
751

            
752
Tag is expanded before executing SQL.
update pod
Yuki Kimoto authored on 2011-01-28
753

            
754
    select * from book where title = ? and author like ?;
755

            
update pod
Yuki Kimoto authored on 2011-02-07
756
use C<execute()> to execute SQL which contains tag
update pod
Yuki Kimoto authored on 2011-01-28
757

            
758
    my $sql = "select * from book where {= author} and {like title};"
759
    $dbi->execute($sql, param => {title => 'Perl', author => '%Ken%'});
760

            
update pod
Yuki Kimoto authored on 2011-02-07
761
You can specify values embedded into place holder as hash reference using
762
C<param> option.
update pod
Yuki Kimoto authored on 2011-01-28
763

            
update pod
Yuki Kimoto authored on 2011-02-07
764
You can specify C<filter()> at C<execute()>.
update pod
Yuki Kimoto authored on 2011-01-28
765

            
766
    $dbi->execute($sql, param => {title => 'Perl', author => '%Ken%'}
767
                  filter => {title => 'to_something');
768

            
update pod
Yuki Kimoto authored on 2011-02-07
769
Note that at C<execute()> the filter applied by C<apply_filter()>
770
don't has effective to columns.
update pod
Yuki Kimoto authored on 2011-02-11
771
You have to use C<table> tag in SQL
update pod
Yuki Kimoto authored on 2011-01-28
772

            
update pod
Yuki Kimoto authored on 2011-02-11
773
    my $sql = "select * from {table book} where {= author} and {like title};"
update pod
Yuki Kimoto authored on 2011-01-28
774

            
update pod
Yuki Kimoto authored on 2011-02-07
775
=head3 Tag list
update pod
Yuki Kimoto authored on 2011-01-28
776

            
update pod
Yuki Kimoto authored on 2011-02-07
777
The following tag is available.
update pod
Yuki Kimoto authored on 2011-01-28
778

            
update pod
Yuki Kimoto authored on 2011-02-11
779
=head4 C<table>
780

            
781
    {table NAME} -> NAME
782

            
783
This is used to specify table name in SQL.
784
If you specify table name, Filtering by 
785
C<apply_filter()> is effective.
786

            
update pod
Yuki Kimoto authored on 2011-01-31
787
=head4 C<?>
update pod
Yuki Kimoto authored on 2011-01-28
788

            
789
    {? NAME}    ->   ?
790

            
update pod
Yuki Kimoto authored on 2011-01-31
791
=head4 C<=>
update pod
Yuki Kimoto authored on 2011-01-28
792

            
793
    {= NAME}    ->   NAME = ?
794

            
update pod
Yuki Kimoto authored on 2011-01-31
795
=head4 C<E<lt>E<gt>>
update pod
Yuki Kimoto authored on 2011-01-28
796

            
797
    {<> NAME}   ->   NAME <> ?
798

            
update pod
Yuki Kimoto authored on 2011-01-31
799
=head4 C<E<lt>>
update pod
Yuki Kimoto authored on 2011-01-28
800

            
801
    {< NAME}    ->   NAME < ?
802

            
update pod
Yuki Kimoto authored on 2011-01-31
803
=head4 C<E<gt>>
update pod
Yuki Kimoto authored on 2011-01-28
804

            
805
    {> NAME}    ->   NAME > ?
806

            
update pod
Yuki Kimoto authored on 2011-01-31
807
=head4 C<E<gt>=>
update pod
Yuki Kimoto authored on 2011-01-28
808

            
809
    {>= NAME}   ->   NAME >= ?
810

            
update pod
Yuki Kimoto authored on 2011-01-31
811
=head4 C<E<lt>=>
update pod
Yuki Kimoto authored on 2011-01-28
812

            
813
    {<= NAME}   ->   NAME <= ?
814

            
update pod
Yuki Kimoto authored on 2011-01-31
815
=head4 C<like>
update pod
Yuki Kimoto authored on 2011-01-28
816

            
817
    {like NAME}   ->   NAME like ?
818

            
update pod
Yuki Kimoto authored on 2011-01-31
819
=head4 C<in>
update pod
Yuki Kimoto authored on 2011-01-28
820

            
821
    {in NAME COUNT}   ->   NAME in [?, ?, ..]
822

            
update pod
Yuki Kimoto authored on 2011-01-31
823
=head4 C<insert_param>
update pod
Yuki Kimoto authored on 2011-01-28
824

            
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
825
    {insert_param NAME1 NAME2}   ->   (NAME1, NAME2) values (?, ?)
update pod
Yuki Kimoto authored on 2011-01-28
826

            
update pod
Yuki Kimoto authored on 2011-01-31
827
=head4 C<update_param>
update pod
Yuki Kimoto authored on 2011-01-28
828

            
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
829
    {update_param NAME1 NAME2}   ->   set NAME1 = ?, NAME2 = ?
830

            
update pod
Yuki Kimoto authored on 2011-02-07
831
=head3 Manipulate same name's columns
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
832

            
update pod
Yuki Kimoto authored on 2011-02-07
833
It is ok if there are same name's columns.
834
Let's think two date comparison.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
835

            
update pod
Yuki Kimoto authored on 2011-01-28
836
    my $sql = "select * from table where {> date} and {< date};";
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
837

            
update pod
Yuki Kimoto authored on 2011-02-07
838
In this case, You specify paramter values as array reference.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
839

            
update pod
Yuki Kimoto authored on 2011-01-28
840
    my $dbi->execute($sql, param => {date => ['2010-10-01', '2012-02-10']});
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
841

            
update pod
Yuki Kimoto authored on 2011-02-11
842
=head3 Register Tag : C<register_tag()>
update pod
Yuki Kimoto authored on 2011-01-28
843

            
update pod
Yuki Kimoto authored on 2011-02-11
844
You can register custom tag.
update pod
Yuki Kimoto authored on 2011-02-07
845
use C<register_tag()> to register tag.
update pod
Yuki Kimoto authored on 2011-01-28
846

            
847
    $dbi->register_tag(
848
        '=' => sub {
849
            my $column = shift;
850
            
851
            return ["$column = ?", [$column]];
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
852
        }
853
    );
854

            
update pod
Yuki Kimoto authored on 2011-02-11
855
This is implementation of C<=> tag.
856
Tag format is the following one.
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
857

            
update pod
Yuki Kimoto authored on 2011-02-11
858
    {TAG_NAME ARG1 ARG2 ...}
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
859

            
update pod
Yuki Kimoto authored on 2011-02-11
860
In case C<=> tag. Format is
cleanup
Yuki Kimoto authored on 2011-01-12
861

            
update pod
Yuki Kimoto authored on 2011-01-28
862
    {= title}
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
863

            
update pod
Yuki Kimoto authored on 2011-02-11
864
So subroutine receive one argument "title".
865
You have to return array reference in the following format.
update pod
Yuki Kimoto authored on 2011-01-28
866

            
867
    [
update pod
Yuki Kimoto authored on 2011-02-11
868
        String after expanding,
869
        [COLUMN1(This is used for place holder), COLUMN2 , ...]
update pod
Yuki Kimoto authored on 2011-01-28
870
    ]
871

            
update pod
Yuki Kimoto authored on 2011-02-11
872
First element is expanded stirng. In this example,
update pod
Yuki Kimoto authored on 2011-01-28
873

            
874
    'title = ?'
875

            
update pod
Yuki Kimoto authored on 2011-02-11
876
Secount element is array reference which is used to embedd value to
877
place holder. In this example,
update pod
Yuki Kimoto authored on 2011-01-28
878

            
879
    ['title']
880

            
update pod
Yuki Kimoto authored on 2011-02-11
881
If there are more than one placeholders,
882
This elements is multipul.
update pod
Yuki Kimoto authored on 2011-01-28
883

            
update pod
Yuki Kimoto authored on 2011-02-11
884
You return the following array reference.
update pod
Yuki Kimoto authored on 2011-01-28
885

            
886
    ['title = ?', ['title']]
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
887

            
update pod
Yuki Kimoto authored on 2011-02-11
888
See source of L<DBIx::Custom::Tag> to see many implementation.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
889

            
update pod
Yuki Kimoto authored on 2011-02-11
890
=head2 6. Dinamically create where clause
update pod
Yuki Kimoto authored on 2011-01-28
891

            
update pod
Yuki Kimoto authored on 2011-02-11
892
=head3 Dinamically create where clause : where()
update pod
Yuki Kimoto authored on 2011-01-28
893

            
update pod
Yuki Kimoto authored on 2011-02-11
894
You want to search multiple conditions in many times.
895
Let's think the following three cases.
update pod
Yuki Kimoto authored on 2011-01-28
896

            
update pod
Yuki Kimoto authored on 2011-02-11
897
Case1: Search only C<title>
update pod
Yuki Kimoto authored on 2011-01-28
898

            
899
    where {= title}
900

            
update pod
Yuki Kimoto authored on 2011-02-11
901
Case2: Search only C<author>
update pod
Yuki Kimoto authored on 2011-01-28
902

            
903
    where {= author}
904

            
update pod
Yuki Kimoto authored on 2011-02-11
905
Case3: Search C<title> and C<author>
update pod
Yuki Kimoto authored on 2011-01-28
906

            
907
    where {= title} and {=author}
908

            
update pod
Yuki Kimoto authored on 2011-02-11
909
L<DBIx::Custom> support dinamic where clause creating.
910
At first, create L<DBIx::Custom::Where> object by C<where()>.
update pod
Yuki Kimoto authored on 2011-01-28
911

            
912
    my $where = $dbi->where;
913

            
update pod
Yuki Kimoto authored on 2011-02-11
914
Set clause by C<clause()>
update pod
Yuki Kimoto authored on 2011-01-28
915

            
916
    $where->clause(
917
        ['and', '{= title'}, '{= author}']
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
918
    );
919

            
update pod
Yuki Kimoto authored on 2011-02-11
920
C<clause> is the following format.
update pod
Yuki Kimoto authored on 2011-01-28
921

            
update pod
Yuki Kimoto authored on 2011-02-11
922
    ['or' or 'and', TAG1, TAG2, TAG3]
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
923

            
update pod
Yuki Kimoto authored on 2011-02-11
924
First argument is 'or' or 'and'.
925
Later than first argument are tag names.
update pod
Yuki Kimoto authored on 2011-01-28
926

            
update pod
Yuki Kimoto authored on 2011-02-11
927
You can write more complex format.
update pod
Yuki Kimoto authored on 2011-01-28
928

            
929
    ['and', 
930
      '{= title}', 
931
      ['or', '{= author}', '{like date}']
932
    ]
933

            
update pod
Yuki Kimoto authored on 2011-02-11
934
This mean "{=title} and ( {=author} or {like date} )".
935

            
936
After setting C<clause>, set C<param>.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
937
    
update pod
Yuki Kimoto authored on 2011-02-11
938
    $where->param({title => 'Perl'});
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
939

            
update pod
Yuki Kimoto authored on 2011-02-11
940
In this example, parameter contains only title.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
941

            
update pod
Yuki Kimoto authored on 2011-02-11
942
If you execute C<string_to()>, you can get where clause
943
which contain only parameter name.
update pod
Yuki Kimoto authored on 2011-01-28
944

            
945
    my $where_clause = $where->to_string;
946

            
update pod
Yuki Kimoto authored on 2011-02-11
947
Parameter name is only title, the following where clause is created.
update pod
Yuki Kimoto authored on 2011-01-28
948

            
949
    where {= title}
950

            
update pod
Yuki Kimoto authored on 2011-02-11
951
You can also create where clause by stringification.
update pod
Yuki Kimoto authored on 2011-01-28
952

            
953
    my $where_clause = "$where";
954

            
update pod
Yuki Kimoto authored on 2011-02-11
955
This is useful to embbed it into SQL. 
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
956

            
update pod
Yuki Kimoto authored on 2011-02-11
957
=head3 In case where clause contains same name columns
check arguments of connect m...
Yuki Kimoto authored on 2010-12-20
958

            
update pod
Yuki Kimoto authored on 2011-02-11
959
Even if same name tags exists, you can create where clause.
960
Let's think that there are starting date and ending date.
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
961

            
update pod
Yuki Kimoto authored on 2011-01-28
962
    my $param = {start_date => '2010-11-15', end_date => '2011-11-21'};
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
963

            
update pod
Yuki Kimoto authored on 2011-02-11
964
In this case, you set parameter value as array reference.
update pod
Yuki Kimoto authored on 2011-01-28
965

            
966
    my $p = {date => ['2010-11-15', '2011-11-21']};
967

            
update pod
Yuki Kimoto authored on 2011-02-11
968
You can embbed these values into same name tags.
update pod
Yuki Kimoto authored on 2011-01-28
969

            
970
    $where->clause(
971
        ['and', '{> date}', '{< date}']
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
972
    );
update pod
Yuki Kimoto authored on 2011-01-28
973
    $where->param($p);
974

            
update pod
Yuki Kimoto authored on 2011-02-11
975
If starting date isn't exists, create the following parameter.
update pod
Yuki Kimoto authored on 2011-01-28
976

            
977
    my $p = {date => [$dbi->not_exists, '2011-11-21']};
978

            
update pod
Yuki Kimoto authored on 2011-02-11
979
You can get DBIx::Custom::NotExists object by C<not_exists()>
980
This mean correnspondinf value isn't exists.
update pod
Yuki Kimoto authored on 2011-01-28
981

            
update pod
Yuki Kimoto authored on 2011-02-11
982
If ending date isn't exists, create the following parameter.
update pod
Yuki Kimoto authored on 2011-01-28
983

            
984
    my $p = {date => ['2010-11-15']};
985

            
update pod
Yuki Kimoto authored on 2011-02-11
986
If both date isn't exists, create the following parameter.
update pod
Yuki Kimoto authored on 2011-01-28
987

            
988
    my $p = {date => []};
989

            
update pod
Yuki Kimoto authored on 2011-02-11
990
This logic is a little difficut. See the following ones.
update pod
Yuki Kimoto authored on 2011-01-28
991

            
992
    my @date;
993
    push @date, exists $param->{start_date} ? $param->{start_date}
994
                                            : $dbi->not_exists;
995
    push @date, $param->{end_date} if exists $param->{end_date};
996
    my $p = {date => \@date};
997

            
update pod
Yuki Kimoto authored on 2011-02-11
998
=head3 With C<select()>
update pod
Yuki Kimoto authored on 2011-01-28
999

            
update pod
Yuki Kimoto authored on 2011-02-11
1000
You can pass L<DBIx::Custom::Where> object to C<where> of C<select()>.
update pod
Yuki Kimoto authored on 2011-01-28
1001
    
1002
    my $where = $dbi->where;
update pod
Yuki Kimoto authored on 2011-02-11
1003
    $where->clause(['and', '{= title}', '{= author}']);
1004
    $where->param({title => 'Perl'});
update pod
Yuki Kimoto authored on 2011-01-28
1005
    my $result = $dbi->select(table => 'book', where => $where);
1006

            
update pod
Yuki Kimoto authored on 2011-02-11
1007
You can also pass it to C<where> of C<update()>�AC<delete()>
update pod
Yuki Kimoto authored on 2011-01-28
1008

            
update pod
Yuki Kimoto authored on 2011-02-11
1009
=head3 With C<execute()>
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
1010

            
update pod
Yuki Kimoto authored on 2011-02-11
1011
L<DBIx::Custom::Where> object is embedded into SQL.
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
1012

            
update pod
Yuki Kimoto authored on 2011-01-28
1013
    my $where = $dbi->where;
update pod
Yuki Kimoto authored on 2011-02-11
1014
    $where->clause(['and', '{= title}', '{= author}']);
1015
    $where->param({title => 'Perl'});
update pod
Yuki Kimoto authored on 2011-01-28
1016

            
update pod
Yuki Kimoto authored on 2011-02-11
1017
    my $sql = <<"EOS";
1018
    select * from {table book};
update pod
Yuki Kimoto authored on 2011-01-28
1019
    $where
1020
    EOS
1021

            
1022
    $dbi->execute($sql, param => $param);
1023

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1024
=head2 7. Model
update pod
Yuki Kimoto authored on 2011-01-28
1025

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1026
=head3 Model
update pod
Yuki Kimoto authored on 2011-01-28
1027

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1028
you can define model extending L<DBIx::Custom::Model>
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1029
to improve source code view.
update pod
Yuki Kimoto authored on 2011-01-28
1030

            
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
1031
At first, you create basic model class extending <DBIx::Custom::Model>.
update pod
Yuki Kimoto authored on 2011-01-28
1032

            
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
1033
    package MyModel;
1034
    
1035
    use base 'DBIx::Custom::Model';
update pod
Yuki Kimoto authored on 2011-01-28
1036

            
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
1037
Next, you create each model classes.
update pod
Yuki Kimoto authored on 2011-01-28
1038

            
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
1039
MyModel::book
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1040

            
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
1041
    package MyModel::book;
1042
    
1043
    use base 'MyModel';
1044
    
1045
    sub insert { ... }
1046
    sub list { ... }
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1047

            
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
1048
MyModel::company
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1049

            
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
1050
    package MyModel::company;
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1051
    
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
1052
    use base 'MyModel';
1053
    
1054
    sub insert { ... }
1055
    sub list { ... }
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1056

            
1057
The follwoing modules location is needed.
1058

            
1059
    MyModel.pm
1060
    MyModel / book.pm
1061
            / company.pm
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
1062

            
1063
You can include these models by C<include_model()>
1064

            
1065
    $dbi->include_model('MyModel');
1066

            
1067
First argument is name space of model.
1068

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1069
You can use model like this.
1070

            
1071
    my $result = $dbi->model('book')->list;
1072

            
1073
In mode, You can use such as methods,
1074
C<insert()>, C<update()>, C<update_all()>,
1075
C<delete()>, C<delete_all()>, C<select()>
1076
without C<table> option.
1077

            
1078
    $dbi->model('book')->insert(param => $param);
update pod
Yuki Kimoto authored on 2011-01-28
1079

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1080
Model is L<DBIx::Custom::Model>.
update pod
Yuki Kimoto authored on 2011-01-28
1081

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1082
If you need table name�Ayou can get it by C<table()>.
update pod
Yuki Kimoto authored on 2011-01-28
1083

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1084
    my $table = $model->table;
update pod
Yuki Kimoto authored on 2011-01-28
1085

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1086
You can get L<DBIx::Custom>.
update pod
Yuki Kimoto authored on 2011-01-28
1087

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1088
    my $dbi = $model->dbi;
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1089

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1090
You can also call all methods of L<DBIx::Custom> and L<DBI>. 
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
1091

            
update pod
Yuki Kimoto authored on 2011-02-11
1092
    # DBIx::Custom method
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1093
    $model->execute($sql);
update pod
Yuki Kimoto authored on 2011-02-11
1094
    
1095
    # DBI method
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1096
    $model->begin_work;
1097
    $model->commit;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
1098

            
add models() attribute
Yuki Kimoto authored on 2011-02-21
1099
If you want to get all models, you can get them by keys of C<models()>.
1100

            
1101
    my @models = keys %{$self->models};
1102

            
add DBIx::Custom::Model fore...
Yuki Kimoto authored on 2011-02-21
1103
You can set primary key to model.
1104

            
1105
   $model->primary_key(['id', 'number_id']);
1106

            
update pod
Yuki Kimoto authored on 2011-02-28
1107
Primary key is used by C<insert_at>, C<update_at()>, C<delete_at()>,
add DBIx::Custom::Model fore...
Yuki Kimoto authored on 2011-02-21
1108
C<select_at()>.
1109

            
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
1110
by C<filter> you can define filters applied by C<apply_filter()>
1111

            
1112
    $model->filter({
1113
        title  => {out => ..., in => ..., end => ...},
1114
        author => {out => ..., in => ..., end => ...}
1115
    });
1116

            
1117
This filters is applied when C<include_model()> is called.
1118

            
add DBIx::Custom::Model colu...
Yuki Kimoto authored on 2011-02-21
1119
You can set column names
1120

            
1121
    $model->columns(['id', 'number_id']);
1122

            
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
1123
Column names is automarically set by C<setup_model()>.
1124
This method is needed to be call after C<include_model()>.
1125

            
1126
    $dbi->setup_model;
1127

            
cleanup
Yuki Kimoto authored on 2011-02-22
1128
You can set C<relation>
1129

            
1130
    $model->relation({'book.company_id' => 'company.id'});
1131

            
1132
This relation is used by C<select()>, C<select_at()>
1133

            
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
1134
=head2 Class name, Model name, Table name
1135

            
1136
Class name, model name, and table name is a little different.
1137
Generally Class name is model name, and table name is model name.
1138

            
1139
    CLASS        MODEL              TABLE
1140
    book         (CLASS) -> book    (MODEL) -> book
1141

            
1142
You can change model name.
1143

            
1144
    package MyModel::book;
1145
    
1146
    __PACAKGE__->attr(name => 'book_model');
1147

            
1148
    CLASS        MODEL         TABLE
1149
    book         book_model    (MODEL) -> book_model
1150

            
1151
Model name is the name used by L<model()> of L<DBIx::Custom>.
1152

            
1153
    $dbi->model('book_model');
1154

            
1155
You can change table name.
1156

            
1157
    package MyModel::book;
1158
    
1159
    __PACAKGE__->attr(table => 'book_table');
1160

            
1161
    CLASS        MODEL              TABLE
1162
    book         (CLASS) -> book    book_table
1163

            
1164
Table name is the table really accessed.
1165

            
1166
    $dbi->model('book')->insert(...); # access to "book_table"
1167

            
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-22
1168
=head2 Create column clause automatically : column_clause()
1169

            
1170
To create column clause automatically, use C<column_clause()>.
1171
Valude of C<table> and C<columns> is used.
1172

            
1173
    my $column_clause = $model->column_clause;
1174

            
1175
If C<table> is 'book'�AC<column> is ['id', 'name'],
1176
the following clause is created.
1177

            
1178
    book.id as id, book.name as name
1179

            
1180
These column name is for removing column name ambiguities.
1181

            
1182
If you remove some columns, use C<remove> option.
1183

            
1184
    my $column_clause = $model->column_clause(remove => ['id']);
1185

            
1186
If you add some column, use C<add> option.
1187

            
1188
    my $column_clause = $model->column_clause(add => ['company.id as company__id']);
1189

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1190
=head2 Model Examples
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1191

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1192
Model examples
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1193

            
update pod
Yuki Kimoto authored on 2011-01-28
1194
    package MyDBI;
1195
    
1196
    use base 'DBIx::Custom';
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1197
    
update pod
Yuki Kimoto authored on 2011-01-28
1198
    sub connect {
1199
        my $self = shift->SUPER::connect(@_);
1200
        
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1201
        $self->include_model(
1202
            MyModel => [
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1203
                'book',
1204
                'company'
1205
            ]
update pod
Yuki Kimoto authored on 2011-01-28
1206
        );
1207
    }
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1208
    
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1209
    package MyModel::book;
1210
    use base 'DBIx::Custom::Model';
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1211
    
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1212
    __PACKAGE__->attr('primary_key' => sub { ['id'] };
1213
    
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1214
    sub insert { ... }
1215
    sub list { ... }
1216
    
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1217
    package MyModel::company;
1218
    use base 'DBIx::Custom::Model';
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1219

            
1220
    __PACKAGE__->attr('primary_key' => sub { ['id'] };
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1221
    
1222
    sub insert { ... }
1223
    sub list { ... }
update pod
Yuki Kimoto authored on 2011-01-28
1224

            
update pod
Yuki Kimoto authored on 2011-02-11
1225
=head2 8. Improve performance
update pod
Yuki Kimoto authored on 2011-01-28
1226

            
update pod
Yuki Kimoto authored on 2011-02-11
1227
=head3 Create query
update pod
Yuki Kimoto authored on 2011-01-28
1228

            
update pod
Yuki Kimoto authored on 2011-02-11
1229
If you can't get performance, create query by C<query> option.
1230
For example, many insert is needed.
update pod
Yuki Kimoto authored on 2011-01-28
1231

            
update pod
Yuki Kimoto authored on 2011-02-11
1232
    my $params = [
1233
        {title => 'Perl', author => 'Ken'},
1234
        {title => 'Good day', author => 'Tom'}
1235
    ]
1236
    my $query = $dbi->insert(table => 'book', param => $params->[0], query => 1);
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1237

            
update pod
Yuki Kimoto authored on 2011-02-11
1238
Return value is L<DBIx::Custom::Query> object.
1239
This query is executed by C<execute()>.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1240

            
update pod
Yuki Kimoto authored on 2011-02-11
1241
    foreach my $param (@$params) {
1242
        $dbi->execute($query, $param);
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1243
    }
1244

            
update pod
Yuki Kimoto authored on 2011-02-11
1245
Performance is improved because statement handle is reused
1246
C<query> option is used in C<insert()>, C<update()>, C<update_all()>,
1247
C<delete()>, C<delete_all()>.
1248

            
1249
Note that parameters count is same as method for creating query and C<execute()>.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1250

            
update pod
Yuki Kimoto authored on 2011-02-11
1251
You can create query from any SQL by C<create_query()>.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1252

            
update pod
Yuki Kimoto authored on 2011-02-11
1253
    my $query = $dbi->create_query(
1254
        "insert into book {insert_param title author};";
1255
    );
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1256

            
update pod
Yuki Kimoto authored on 2011-02-11
1257
=head2 9. Other features
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1258

            
update pod
Yuki Kimoto authored on 2011-02-11
1259
=head3 Add method
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1260

            
update pod
Yuki Kimoto authored on 2011-02-11
1261
You can add method to L<DBIx::Custom> object.
update pod
Yuki Kimoto authored on 2011-02-11
1262
use C<method()>.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1263

            
update pod
Yuki Kimoto authored on 2011-01-28
1264
    $dbi->method(
1265
        update_or_insert => sub {
1266
            my $self = shift;
1267
            # something
1268
        },
1269
        find_or_create   => sub {
1270
            my $self = shift;
1271
            # something
1272
        }
1273
    );
1274

            
update pod
Yuki Kimoto authored on 2011-02-11
1275
You can call these methods from L<DBIx::Custom> object.
update pod
Yuki Kimoto authored on 2011-01-28
1276

            
1277
    $dbi->update_or_insert;
1278
    $dbi->find_or_create;
1279

            
update pod
Yuki Kimoto authored on 2011-02-11
1280
=head3 Change result class
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1281

            
update pod
Yuki Kimoto authored on 2011-02-11
1282
You can change result class. By default it is L<DBIx::Custom::Result>.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1283

            
update pod
Yuki Kimoto authored on 2011-01-28
1284
    package MyResult;
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1285
    use base 'DBIx::Custom::Result';
1286
    
1287
    sub some_method { ... }
1288

            
1289
    1;
1290
    
1291
    package main;
1292
    
update pod
Yuki Kimoto authored on 2011-01-28
1293
    use MyResult;
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1294
    
1295
    my $dbi = DBIx::Custom->connect(...);
update pod
Yuki Kimoto authored on 2011-01-28
1296
    $dbi->result_class('MyResult');
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1297

            
update pod
Yuki Kimoto authored on 2011-02-11
1298
=head3 Caching
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1299

            
update pod
Yuki Kimoto authored on 2011-02-11
1300
SQL after parsing tag is cached for performance.
1301
You can set C<cache()>. By default, chaching is true.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1302

            
update pod
Yuki Kimoto authored on 2011-01-28
1303
    $dbi->cache(1);
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1304

            
update pod
Yuki Kimoto authored on 2011-02-11
1305
The way to cache is changed by C<cache_method()>.
1306
Default method is the following one.
1307
Cache is saved to memory.
update pod
Yuki Kimoto authored on 2011-01-28
1308

            
1309
    $dbi->cache_method(sub {
1310
        sub {
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1311
            my $self = shift;
update pod
Yuki Kimoto authored on 2011-01-28
1312
            
1313
            $self->{_cached} ||= {};
1314
            
1315
            if (@_ > 1) {
update pod
Yuki Kimoto authored on 2011-02-11
1316
                # Save cache
update pod
Yuki Kimoto authored on 2011-01-28
1317
                $self->{_cached}{$_[0]} = $_[1] 
1318
            }
1319
            else {
update pod
Yuki Kimoto authored on 2011-02-11
1320
                # Get cache
update pod
Yuki Kimoto authored on 2011-01-28
1321
                return $self->{_cached}{$_[0]}
1322
            }
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1323
        }
update pod
Yuki Kimoto authored on 2011-01-28
1324
    });
1325
    
update pod
Yuki Kimoto authored on 2011-02-11
1326
First argument is L<DBIx::Custom> object.
1327
Second argument is SQL before parsing.
1328
Third argument is SQL information after parsing. This is hash reference.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1329

            
update pod
Yuki Kimoto authored on 2011-02-11
1330
If third argument exists, you save cache,
1331
and if third argument isn't exists, you get chace.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1332

            
pod fix
Yuki Kimoto authored on 2011-01-21
1333
=head1 EXAMPLES
1334

            
update pod
Yuki Kimoto authored on 2011-02-11
1335
You can see exsamples in the following wiki.
1336

            
pod fix
Yuki Kimoto authored on 2011-01-21
1337
L<DBIx::Custom Wiki|https://github.com/yuki-kimoto/DBIx-Custom/wiki> - Many useful examples
add examples
Yuki Kimoto authored on 2011-01-07
1338

            
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1339
=cut