update pod
|
1 |
=encoding utf8 |
2 | ||
added DBIx::Custom::Guides
|
3 |
=head1 NAME |
4 | ||
update pod
|
5 |
DBIx::Custom::Guide - DBIx::Custom Guide |
added DBIx::Custom::Guides
|
6 | |
pod fix
|
7 |
=head1 GUIDE |
added DBIx::Custom::Guides
|
8 | |
update pod
|
9 |
B<This guide is now writing.> |
update pod
|
10 | |
update pod
|
11 |
L<DBIx::Custom> is the class to make easy to execute SQL. |
12 |
This is L<DBI> wrapper class like L<DBIx::Class> or L<DBIx::Simple>. |
|
13 |
You can do thing more easy than L<DBIx::Class>, more flexible |
|
14 |
than L<DBIx::Simple>. |
|
added DBIx::Custom::Guides
|
15 | |
update pod
|
16 |
L<DBIx::Custom> is B<not< O/R mapper, O/R mapper is usefule, but |
17 |
you must learn many things. Created SQL is sometimes inefficient, |
|
18 |
and in many cases you create raw SQL because |
|
19 |
O/R mapper can't make complex SQL |
|
added DBIx::Custom::Guides
|
20 | |
update pod
|
21 |
L<DBIx::Custom> is opposit of O/R mapper. |
22 |
The main purpose is that we respect SQL |
|
23 |
and make easy difficult works if you use only L<DBI>. |
|
24 |
If you already learn SQL, it is easy to use L<DBIx::Custom>. |
|
deprecated DBIx::Custom::MyS...
|
25 | |
update pod
|
26 |
I explain L<DBIx::Custom> a little in this section. |
27 |
In L<DBIx::Custom>, you embbed tag in SQL. |
|
deprecated DBIx::Custom::MyS...
|
28 | |
update pod
|
29 |
select * from book where {= title} and {=author}; |
deprecated DBIx::Custom::MyS...
|
30 | |
update pod
|
31 |
The part arround {} is tag. |
32 |
This SQL is converted to the one which contains place holder. |
|
33 | ||
34 |
select * from book where title = ? and author = ?; |
|
35 | ||
36 |
Maybe you ask me that this conversion is meaningful. |
|
37 |
On the top of this, usuful features is implemented. |
|
38 |
See the following descriptions. |
|
39 | ||
40 |
=over 4 |
|
41 | ||
42 |
=item 1. Specify place holder binding value as hash refernce |
|
43 | ||
44 |
If you use L<DBI>, you must specify place holder binding value |
|
45 |
as array. |
|
46 | ||
47 |
$sth->execute(@bind); |
|
48 | ||
49 |
If you use L<DBIx::Custom>, you specify it as hash reference. |
|
50 |
|
|
51 |
my $param = {title => 'Perl', author => 'Ken'}; |
|
52 |
$dbi->execute($sql, $param); |
|
53 | ||
54 |
=item 2. Filtering |
|
55 | ||
56 |
L<DBIx::Custom> provides filtering system. |
|
57 |
For example, You think that about date value you want to |
|
58 |
manipulate it as date object like L<Time::Piece> in Perl, |
|
59 |
and want to convert it to database DATE format. |
|
60 |
and want to do reverse. |
|
61 | ||
62 |
You can use filtering system. |
|
63 | ||
64 |
At first, register filter. |
|
65 | ||
66 |
$dbi->register_filter( |
|
67 |
tp_to_date => sub { |
|
68 |
... |
|
69 |
}, |
|
70 |
date_to_tp => sub { |
|
71 |
... |
|
72 |
} |
|
73 |
); |
|
74 | ||
75 |
next, apply this filter to each column. |
|
76 | ||
77 |
$dbi->apply_filter('book', |
|
78 |
'issue_date' => {out => 'tp_to_date', in => 'date_to_tp'} |
|
79 |
); |
|
80 | ||
81 |
C<out> is perl-to-database way. C<in> is perl-from-database way. |
|
82 | ||
83 |
This filter is automatically enabled in many method. |
|
84 | ||
85 |
$dbi->insert(table => 'book', param => {issue_date => $tp}); |
|
86 | ||
87 | ||
88 |
=item 3. Selective search condition |
|
89 | ||
90 |
It is difficult to create selective where clause in L<DBI>. |
|
91 |
For example, If C<title> and C<author> is specified, we create |
|
92 |
the following SQL. |
|
93 | ||
94 |
select * from book where title = ? and author = ?; |
|
95 | ||
96 |
If only C<title> is specified, the following one |
|
97 | ||
98 |
select * from book where title = ?; |
|
99 | ||
100 |
If only C<author> is specified, the following one, |
|
101 | ||
102 |
select * from book where author = ?; |
|
103 | ||
104 |
This is hard work. Generally we use modules like L<SQL::Abstract>. |
|
105 |
L<DBIx::Custom> prepare the way to make it easy. |
|
106 | ||
107 |
# Where object |
|
108 |
my $where = $dbi->where; |
|
109 |
|
|
110 |
# Search condition |
|
111 |
$where->clause( |
|
112 |
['and', '{= title}', {'= author'}] |
|
113 |
); |
|
114 |
|
|
115 |
# Setting to automatically select needed column |
|
116 |
$where->param({title => 'Perl'}); |
|
117 | ||
118 |
# Embbed where clause to SQL |
|
119 |
my $sql = "select * from book $where"; |
|
120 | ||
121 |
You can create where clause which has selected search condition. |
|
122 |
You can write nesting of where clause and C<or> condition |
|
123 | ||
124 |
=item 4. Method of insert, update, delete, select |
|
125 | ||
126 |
L<DBIx::Custom>�ł�SQL���ɊȒP�Ɏ�s���邽�߂� |
|
127 |
���\�b�h����Ă��܂��B |
|
128 |
C<insert()>, C<update()>, C<delete()>,C<select()>�Ȃǂ� |
|
129 |
�V���K�[���\�b�h��g��āA�}��A�X�V�A�폜�A�I��Ƃ��������s�����Ƃ� |
|
130 |
�ł��܂��B |
|
131 | ||
132 |
my $param = {title => 'Perl', author => 'Ken'}; |
|
133 |
$dbi->insert(table => 'book', param => $param); |
|
134 | ||
135 |
=item 5. �e�[�u���P�ʂ̑���̓o�^ |
|
deprecated DBIx::Custom::MyS...
|
136 | |
update pod
|
137 |
�e�[�u���ɑ��đ����o�^���邱�Ƃ��ł��܂��B����ɂ��� |
138 |
�e�[�u������J��Ԃ��w�肷��K�v���Ȃ��Ȃ�A�\�[�X�R�[�h�� |
|
139 |
���ʂ����ǂ��Ȃ�܂��B |
|
140 | ||
141 |
$dbi->talbe('book', |
|
142 |
list => sub { |
|
143 |
... |
|
144 |
}, |
|
145 |
list_somethin => sub { |
|
146 |
|
|
147 |
} |
|
148 |
); |
|
149 | ||
150 |
�o�^�������\�b�h�͂��̂܂ܗ��p���邱�Ƃ��ł��܂��B |
|
151 | ||
152 |
$dbi->table('book')->list; |
|
153 | ||
154 |
�ʏ�O/R�}�b�p�̓e�[�u���ɑΉ�����N���X��쐬���Ȃ���� |
|
155 |
�Ȃ�Ȃ����Ƃ������ł����AL<DBIx::Custom>�ł͂��̍�Ƃ�ȕւ� |
|
156 |
���Ă���A��L�̂悤�ɓo�^���邱�Ƃ��ł��܂��B |
|
157 | ||
158 |
=back |
|
159 | ||
160 |
L<DBIx::Custom>��L<DBI>��₤�ƂĂ�֗��ȃ��W���[���ł��B |
|
161 |
��������ꂽ��́A���̌�ŏڂ�������s���܂��̂ŁA |
|
162 |
�����ɂȂ�Ă݂Ă��������B |
|
163 | ||
164 |
=head2 1. �f�[�^�x�[�X�ւ̐ڑ� |
|
165 | ||
166 |
�܂�L<DBIx::Custom>��ǂݍ��݂܂��B |
|
167 | ||
168 |
use DBIx::Custom; |
|
169 | ||
170 |
L<DBIx::Custom>�I�u�W�F�N�g�����A�f�[�^�x�[�X�ɐڑ�����ɂ� |
|
171 |
C<connect()>���\�b�h��g�p���܂��B |
|
172 | ||
173 |
my $dbi = DBIx::Custom->connect( |
|
174 |
data_source => "dbi:mysql:database=dbname", |
|
175 |
user => 'ken', |
|
176 |
password => '!LFKD%$&', |
|
177 |
dbi_options => {mysql_enable_utf8 => 1} |
|
178 |
); |
|
179 | ||
180 |
C<data_source>�̓f�[�^�x�[�X�V�X�e���ɉ������t�H�[�}�b�g�� |
|
181 |
�w�肷��K�v������܂��B�ȉ��Ƀf�[�^�x�[�X���Ƃ̃t�H�[�}�b�g |
|
182 |
��@�̃T���v����f�ڂ��Ă����܂��B |
|
183 | ||
184 |
B<MySQL> |
|
remove DBIx::Custom::Model
|
185 | |
186 |
"dbi:mysql:database=$database" |
|
187 |
"dbi:mysql:database=$database;host=$hostname;port=$port" |
|
188 | ||
update pod
|
189 |
B<SQLite> |
190 | ||
191 |
"dbi:SQLite:dbname=$database" |
|
192 |
"dbi:SQLite:dbname=:memory:" |
|
193 | ||
194 |
B<PostgreSQL> |
|
deprecated DBIx::Custom::MyS...
|
195 | |
196 |
"dbi:Pg:dbname=$dbname" |
|
197 | ||
update pod
|
198 |
B<Oracle> |
deprecated DBIx::Custom::MyS...
|
199 | |
200 |
"dbi:Oracle:$dbname" |
|
201 |
"dbi:Oracle:host=$host;sid=$sid" |
|
202 | ||
update pod
|
203 |
B<ODBC(Microsoft Access)> |
deprecated DBIx::Custom::MyS...
|
204 | |
205 |
"dbi:ODBC:driver=Microsoft Access Driver (*.mdb);dbq=hoge.mdb" |
|
206 | ||
update pod
|
207 |
B<ODBC(SQL Server)> |
deprecated DBIx::Custom::MyS...
|
208 | |
209 |
"dbi:ODBC:driver={SQL Server};Server=(local);database=test;Trusted_Connection=yes;AutoTranslate=No;" |
|
added DBIx::Custom::Guides
|
210 | |
update pod
|
211 |
�܂��F�����߂���ꍇ�́AC<user>��C<password>���[�U���� |
212 |
�p�X���[�h��w�肷��K�v������܂��B |
|
213 | ||
214 |
L<DBIx::Custom>��L<DBI>�̃��b�p�ł��B |
|
215 |
L<DBI>�̃f�[�^�x�[�X�n���h����C<dbh>�Ŏ擾���邱�Ƃ��ł��܂��B |
|
added DBIx::Custom::Guides
|
216 | |
update pod
|
217 |
my $dbh = $dbi->dbh; |
added DBIx::Custom::Guides
|
218 | |
update pod
|
219 |
L<DBIx::Custom>�ł̓f�[�^�x�[�X�n���h�������ɂ̓f�t�H���g�Ŏ��̂�̂��ݒ肳��܂��B |
220 |
|
|
221 |
$dbi->dbh->{RaiseError} = 1; |
|
222 |
$dbi->dbh->{PrintError} = 0; |
|
223 |
$dbi->dbh->{AutoCommit} = 1; |
|
added DBIx::Custom::Guides
|
224 | |
update pod
|
225 |
���̐ݒ��s��Ă���̂ŁA�v���I�ȃG���[���N����ƁA |
226 |
��O���������v���O�����͏I�����܂��B |
|
227 |
�܂��N�G�������s�����Ǝ����I�ɃR�~�b�g����܂��B |
|
228 | ||
229 |
=head2 2. �}��A�X�V�A�폜�A�I��̂��߂̃��\�b�h |
|
230 | ||
231 |
L<DBIx::Custom>�́A |
|
232 |
C<insert()>�AC<update()>�AC<delete()>�AC<select()> |
|
233 |
�̂悤�ȑ}��A�X�V�A�폜�A�I���s�����߂̃��\�b�h���Ă��܂��B |
|
234 |
�ȒP�Ȃ��Ƃ��s���̂ł���ASQL����ŋL�q����K�v������܂���B |
|
235 | ||
236 |
=head3 �f�[�^�̑}�� C<insert()> |
|
237 | ||
238 |
�f�[�^�x�[�X�Ƀf�[�^��}���ɂ�C<insert()>��g�p���܂��B |
|
added DBIx::Custom::Guides
|
239 | |
remove DBIx::Custom::Model
|
240 |
$dbi->insert(table => 'book', |
added DBIx::Custom::Guides
|
241 |
param => {title => 'Perl', author => 'Ken'}); |
242 | ||
update pod
|
243 |
C<table>�ɂ̓e�[�u�����AC<param>�ɂ͑}����f�[�^��w�肵�܂��B |
added DBIx::Custom::Guides
|
244 | |
update pod
|
245 |
����SQL�����s����܂��B |
added DBIx::Custom::Guides
|
246 | |
update pod
|
247 |
insert into (title, author) values (?, ?); |
added DBIx::Custom::Guides
|
248 | |
update pod
|
249 |
=head3 �f�[�^�̍X�V C<update()> |
added DBIx::Custom::Guides
|
250 | |
update pod
|
251 |
�f�[�^�x�[�X�̃f�[�^��X�V����ɂ́AC<update()>��g�p���܂��B |
added DBIx::Custom::Guides
|
252 | |
remove DBIx::Custom::Model
|
253 |
$dbi->update(table => 'book', |
added DBIx::Custom::Guides
|
254 |
param => {title => 'Perl', author => 'Ken'}, |
255 |
where => {id => 5}); |
|
256 | ||
update pod
|
257 |
C<table>�ɂ̓e�[�u�����AC<param>�ɂ͑}����f�[�^�AC<where>�ɂ� |
258 |
���w�肵�܂��B |
|
added DBIx::Custom::Guides
|
259 | |
update pod
|
260 |
����SQL�����s����܂��B |
added DBIx::Custom::Guides
|
261 | |
update pod
|
262 |
update book set title = ?, author = ?; |
added DBIx::Custom::Guides
|
263 | |
update pod
|
264 |
C<update>���\�b�h�͈�S�̂��� |
265 |
where��̂Ȃ�SQL�s���邱�Ƃ���Ă��܂���B |
|
266 |
�����ׂĂ̍s��X�V�������ꍇ�� |
|
267 |
C<update_all()>��g�p�����������B |
|
added DBIx::Custom::Guides
|
268 | |
update pod
|
269 |
$dbi->update_all(table => 'book', |
270 |
param => {title => 'Perl', author => 'Ken'}); |
|
added DBIx::Custom::Guides
|
271 | |
update pod
|
272 |
=head3 �f�[�^�̍폜 C<delete()> |
added DBIx::Custom::Guides
|
273 | |
update pod
|
274 |
�f�[�^�x�[�X�̃f�[�^��1���폜����ɂ́AC<delete()>��g�p���܂��B |
added DBIx::Custom::Guides
|
275 | |
remove DBIx::Custom::Model
|
276 |
$dbi->delete(table => 'book', |
added DBIx::Custom::Guides
|
277 |
where => {author => 'Ken'}); |
278 | ||
update pod
|
279 |
C<table>�ɂ̓e�[�u�����AC<where>�ɂ͏��w�肵�܂��B |
added DBIx::Custom::Guides
|
280 | |
update pod
|
281 |
����SQL�����s����܂��B |
added DBIx::Custom::Guides
|
282 | |
update pod
|
283 |
delete from book where id = ?; |
added DBIx::Custom::Guides
|
284 | |
update pod
|
285 |
C<delete>���\�b�h�͈�S�̂��� |
286 |
where��̂Ȃ�SQL�s���邱�Ƃ���Ă��܂���B |
|
287 |
�����ׂĂ̍s��폜�������ꍇ�� |
|
288 |
C<delete_all()>��g�p�����������B |
|
added DBIx::Custom::Guides
|
289 | |
update pod
|
290 |
$dbi->delete_all(table => 'book'); |
added DBIx::Custom::Guides
|
291 | |
update pod
|
292 |
=head3 �f�[�^�̑I�� C<select()> |
added DBIx::Custom::Guides
|
293 | |
update pod
|
294 |
�s��I���ɂ�C<select()>��g�p���܂��B |
added DBIx::Custom::Guides
|
295 | |
remove DBIx::Custom::Model
|
296 |
my $result = $dbi->select(table => 'book'); |
added DBIx::Custom::Guides
|
297 | |
update pod
|
298 |
C<table>������w�肵�āA���̏��w�肵�Ȃ��ꍇ�͎���SQL�����s����܂��B |
added DBIx::Custom::Guides
|
299 | |
remove DBIx::Custom::Model
|
300 |
select * from book; |
added DBIx::Custom::Guides
|
301 | |
update pod
|
302 |
C<select()>���\�b�h�̖߂�l��L<DBIx::Custom::Result> |
303 |
�I�u�W�F�N�g�ł��B�s��t�F�b�`����ɂ�C<fetch()>��g�p���܂��B |
|
added DBIx::Custom::Guides
|
304 | |
305 |
while (my $row = $result->fetch) { |
|
306 |
my $title = $row->[0]; |
|
307 |
my $author = $row->[1]; |
|
308 |
} |
|
309 | ||
update pod
|
310 |
L<DBIx::Custom::Result>�ɂ��Ă͂��̌�L<3. �s�̃t�F�b�`/"3. �s�̃t�F�b�`">�ŏڂ��������܂��B |
added DBIx::Custom::Guides
|
311 | |
update pod
|
312 |
���܂��܂�C<select()>�̎g�����Ă����܂��傤�B |
313 |
����C<select>�͍s�̖��O��where���w�肵����̂ł��B |
|
added DBIx::Custom::Guides
|
314 | |
315 |
my $result = $dbi->select( |
|
remove DBIx::Custom::Model
|
316 |
table => 'book', |
update pod
|
317 |
column => ['author', 'title'], |
added DBIx::Custom::Guides
|
318 |
where => {author => 'Ken'} |
319 |
); |
|
320 | ||
update pod
|
321 |
C<column>�ɂ͗�AC<where>�ɂ͏��w�肷�邱�Ƃ��ł��܂��B |
322 |
����SQL�����s����܂��B |
|
added DBIx::Custom::Guides
|
323 | |
remove DBIx::Custom::Model
|
324 |
select author, title from book where author = ?; |
added DBIx::Custom::Guides
|
325 | |
update pod
|
326 |
�e�[�u������������ꍇ�͂�C<relation>�Ƀe�[�u���� |
327 |
�W��L�q���܂��B |
|
added DBIx::Custom::Guides
|
328 | |
329 |
my $result = $dbi->select( |
|
remove DBIx::Custom::Model
|
330 |
table => ['book', 'rental'], |
update pod
|
331 |
where => {book.name => 'Perl'}, |
remove DBIx::Custom::Model
|
332 |
relation => {'book.id' => 'rental.book_id'} |
added DBIx::Custom::Guides
|
333 |
); |
334 | ||
update pod
|
335 |
book�e�[�u����id���rental�e�[�u����book_id���֘A�t�����܂��B |
336 |
����SQL�����s����܂��B |
|
added DBIx::Custom::Guides
|
337 | |
update pod
|
338 |
select * from book, rental where book.name = ? and book.id = rental.book_id; |
added DBIx::Custom::Guides
|
339 | |
update pod
|
340 |
SQL���̖���ɕ������lj�������ꍇ��<append>��g�p���܂��B |
added DBIx::Custom::Guides
|
341 | |
342 |
my $result = $dbi->select( |
|
remove DBIx::Custom::Model
|
343 |
table => 'book', |
added DBIx::Custom::Guides
|
344 |
where => {author => 'Ken'}, |
update pod
|
345 |
append => 'for update', |
added DBIx::Custom::Guides
|
346 |
); |
347 | ||
update pod
|
348 |
����SQL�����s����܂��B |
added DBIx::Custom::Guides
|
349 | |
update pod
|
350 |
select * book where author = ? for update; |
added DBIx::Custom::Guides
|
351 | |
update pod
|
352 |
�܂�C<append>�́AC<select>�����łȂ�C<insert()>�AC<update()>�AC<update_all()> |
353 |
C<delete()>�AC<delete_all()>�AC<select()>�Ŏg�p���邱�Ƃ�ł��܂��B |
|
added DBIx::Custom::Guides
|
354 | |
update pod
|
355 |
=head3 SQL�̎�s C<execute()> |
added DBIx::Custom::Guides
|
356 | |
update pod
|
357 |
�C�ӂ�SQL���s����ɂ�execute���\�b�h��g�p���܂��B |
358 | ||
359 |
$dbi->execute("select * from book;"); |
|
360 | ||
361 |
C<execute()>��L<DBIx::Custom>�̍����̃��\�b�h�ł���^�O��W�J���܂��B |
|
362 | ||
363 |
$dbi->execute( |
|
364 |
"select * from book {= title} and {= author};" |
|
365 |
param => {title => 'Perl', author => 'Ken'} |
|
366 |
); |
|
367 | ||
368 |
��L�̃^�O��܂�SQL�͎��̂悤�ɓW�J����܂��B |
|
369 | ||
370 |
select * from book title = ? and author = ?; |
|
371 | ||
372 |
SQL����s�����Ƃ��Ƀv���[�X�z���_(?)�ɑΉ�����ʒu��title��author |
|
373 |
�̒l���������I�ɖ��ߍ��܂�܂��B |
|
374 | ||
375 |
�^�O�ɂ��Ă�L<5. �^�O/"5. �^�O">�ŏڂ�������܂����A |
|
376 |
�ЂƂ̒��ӓ_������܂��B |
|
377 |
�^�O��W�J���邽�߂�C<{>��C<}>�͗\���ɂȂ�Ă��܂��B |
|
378 |
�����p�������ꍇ�͒��O��\����ăG�X�P�[�v��s���K�v������܂��B |
|
379 | ||
380 |
$dbi->execute("... \\{ ... \\} ..."); |
|
381 | ||
382 |
\���̂�Perl�̃G�X�P�[�v�����ł��̂ŁA��K�v�ɂȂ�Ƃ����_�ɒ��ӂ��Ă��������B |
|
383 | ||
384 |
�܂�execute�̃L���[�g�ȋ@�\�Ƃ��āASQL�̍Ō�ɃZ�~�R��������Ȃ��Ă� |
|
385 |
���܂��܂���B |
|
386 | ||
387 |
$dbi->execute('select * from book'); |
|
388 | ||
389 |
=head2 3. �s�̃t�F�b�` |
|
390 | ||
391 |
C<select()>���\�b�h�̖߂�l��L<DBIx::Custom::Result>�I�u�W�F�N�g�ł��B |
|
392 |
L<DBIx::Custom::Result>�ɂ͍s��t�F�b�`���邽�߂̂��܂��܂ȃ��\�b�h�� |
|
393 |
�p�ӂ���Ă��܂��B |
|
394 | ||
395 |
=head3 1�s�Ât�F�b�`(�z��) C<fetch()> |
|
396 | ||
397 |
��s�t�F�b�`���Ĕz��̃��t�@�����X�Ɋi�[����ɂ�C<fetch()>��g�p���܂��B |
|
added DBIx::Custom::Guides
|
398 | |
399 |
while (my $row = $result->fetch) { |
|
update pod
|
400 |
my $title = $row->[0]; |
401 |
my $author = $row->[1]; |
|
added DBIx::Custom::Guides
|
402 |
} |
403 | ||
update pod
|
404 |
while���[�v��g��āA���ׂĂ̍s��擾���邱�Ƃ��ł��܂��B |
405 | ||
406 |
=head3 �ŏ��̍s�����t�F�b�`(�z��) C<fetch_first()> |
|
407 | ||
408 |
��s�����t�F�b�`���Ĕz��̃��t�@�����X�Ɋi�[����ɂ�C<fetch_first()> |
|
409 |
��g�p���܂��B |
|
added DBIx::Custom::Guides
|
410 | |
411 |
my $row = $result->fetch_first; |
|
412 | ||
update pod
|
413 |
��s�̃t�F�b�`���I������͂���ȏ�t�F�b�`�ł��Ȃ��Ȃ�܂��B |
414 |
���I�ɂ�1�s�̃t�F�b�`���I������� |
|
415 |
�X�e�[�g�����g�n���h����C<finish()>����s����܂��B |
|
added DBIx::Custom::Guides
|
416 | |
update pod
|
417 |
=head3 �����s��Ƀt�F�b�`(�z��) C<fetch_multi()> |
418 | ||
419 |
�����s��t�F�b�`���Ĕz��̃��t�@�����X��v�f�Ɏ��� |
|
420 |
�z��̃��t�@�����X�Ɋi�[����ɂ�C<fetch_multi()>��g�p���܂��B |
|
421 | ||
422 |
while (my $rows = $result->fetch_multi(2)) { |
|
423 |
my $title0 = $rows->[0][0]; |
|
424 |
my $author0 = $rows->[0][1]; |
|
425 |
|
|
426 |
my $title1 = $rows->[1][0]; |
|
427 |
my $author1 = $rows->[1][1]; |
|
added DBIx::Custom::Guides
|
428 |
} |
update pod
|
429 | |
430 |
��ɂ͎��o�������s����w�肵�܂��B |
|
431 | ||
432 |
�w�肵���s��i�[�������̂悤�ȃf�[�^��擾�ł��܂��B |
|
433 | ||
434 |
[ |
|
435 |
['Perl', 'Ken'], |
|
436 |
['Ruby', 'Mark'] |
|
437 |
] |
|
438 | ||
439 |
=head3 ���ׂĂ̍s��t�F�b�`(�z��) C<fetch_all> |
|
440 | ||
441 |
���ׂĂ̍s��t�F�b�`���Ĕz��̃��t�@�����X��v�f�Ɏ��� |
|
442 |
�z��̃��t�@�����X�Ɋi�[����ɂ�C<fetch_all()>��g�p���܂��B |
|
added DBIx::Custom::Guides
|
443 | |
444 |
my $rows = $result->fetch_all; |
|
445 | ||
update pod
|
446 |
���ׂĂ̍s��i�[�������̂悤�ȃf�[�^��擾�ł��܂��B |
447 | ||
448 |
[ |
|
449 |
['Perl', 'Ken'], |
|
450 |
['Ruby', 'Mark'] |
|
451 |
] |
|
452 | ||
453 |
=head3 1�s�Ât�F�b�`(�n�b�V��) C<fetch_hash()> |
|
454 | ||
455 |
��s�t�F�b�`���ăn�b�V���̃��t�@�����X�Ɋi�[����ɂ�C<fetch_hash()>��g�p���܂��B |
|
added DBIx::Custom::Guides
|
456 | |
457 |
while (my $row = $result->fetch_hash) { |
|
458 |
my $title = $row->{title}; |
|
459 |
my $author = $row->{author}; |
|
460 |
} |
|
461 | ||
update pod
|
462 |
=head3 �ŏ��̍s�����t�F�b�`(�n�b�V��) C<fetch_hash_first()> |
463 | ||
464 |
��s�����t�F�b�`���ăn�b�V���̃��t�@�����X�Ɋi�[����ɂ� |
|
465 |
C<fetch_hash_first()>��g�p���܂��B |
|
added DBIx::Custom::Guides
|
466 | |
467 |
my $row = $result->fetch_hash_first; |
|
update pod
|
468 | |
469 |
��s�̃t�F�b�`���I������͂���ȏ�t�F�b�`�ł��Ȃ��Ȃ�܂��B |
|
470 |
���I�ɂ�1�s�̃t�F�b�`���I������� |
|
471 |
�X�e�[�g�����g�n���h����C<finish()>����s����܂��B |
|
472 | ||
473 |
=head3 �����s��Ƀt�F�b�`(�n�b�V��) C<fetch_hash_multi()> |
|
474 | ||
475 |
�����s��t�F�b�`���ăn�b�V���̃��t�@�����X��v�f�Ɏ��� |
|
476 |
�z��̃��t�@�����X�Ɋi�[����ɂ�C<fetch_hash_multi()> |
|
477 |
��g�p���܂��B |
|
added DBIx::Custom::Guides
|
478 | |
479 |
while (my $rows = $result->fetch_hash_multi(5)) { |
|
update pod
|
480 |
my $title0 = $rows->[0]{title}; |
481 |
my $author0 = $rows->[0]{author}; |
|
482 |
my $title1 = $rows->[1]{title}; |
|
483 |
my $author1 = $rows->[1]{author}; |
|
added DBIx::Custom::Guides
|
484 |
} |
update pod
|
485 | |
486 |
��ɂ͎��o�������s����w�肵�܂��B |
|
487 | ||
488 |
�w�肵���s��i�[�������̂悤�ȃf�[�^��擾�ł��܂��B |
|
489 | ||
490 |
[ |
|
491 |
{title => 'Perl', author => 'Ken'}, |
|
492 |
{title => 'Ruby', author => 'Mark'} |
|
493 |
] |
|
494 | ||
495 |
=head3 ���ׂĂ̍s��t�F�b�`(�n�b�V��) C<fetch_hash_all()> |
|
496 | ||
497 |
���ׂĂ̍s��t�F�b�`���ăn�b�V���̃��t�@�����X��v�f�Ɏ��� |
|
498 |
�z��̃��t�@�����X�Ɋi�[����ɂ�C<fetch_hash_all()> |
|
499 |
��g�p���܂��B |
|
added DBIx::Custom::Guides
|
500 | |
501 |
my $rows = $result->fetch_hash_all; |
|
502 | ||
update pod
|
503 |
���ׂĂ̍s��i�[�������̂悤�ȃf�[�^��擾�ł��܂��B |
504 | ||
505 |
[ |
|
506 |
{title => 'Perl', author => 'Ken'}, |
|
507 |
{title => 'Ruby', author => 'Mark'} |
|
508 |
] |
|
509 | ||
510 |
=head3 �X�e�[�g�����g�n���h�� C<sth()> |
|
511 | ||
512 |
�X�e�[�g�����g�n���h���ɒ��ڃA�N�Z�X�������ꍇ�� |
|
513 |
<sth>�Ŏ擾���邱�Ƃ��ł��܂��B |
|
added DBIx::Custom::Guides
|
514 | |
515 |
my $sth = $result->sth; |
|
516 | ||
update pod
|
517 |
�t�F�b�`�̃p�t�H�[�}���X���p�������Ȃ��Ƃ��ɂ́A |
518 |
�X�e�[�g�����g�n���h������ |
|
519 |
���p�ł��鑬�x�̑������\�b�h�𗘗p���邱�Ƃ��ł��܂��B |
|
520 | ||
521 |
=head2 4. �t�B���^�����O |
|
522 | ||
523 |
�f�[�^�x�[�X�Ƀf�[�^��o�^����Ƃ���f�[�^�x�[�X����f�[�^��擾���� |
|
524 |
�Ƃ��Ɏ����I�ɒl�̕ϊ���s�������ꍇ�������Ǝv���܂��B |
|
525 |
���Ƃ��A��t��\�������̏ꍇ�́A |
|
526 |
�f�[�^�x�[�X�ɓo�^����ꍇ��L<Time::Piece>�I�u�W�F�N�g���� |
|
527 |
�f�[�^�x�[�X�̓�t�̃t�H�[�}�b�g�ɁA |
|
528 |
�f�[�^�x�[�X����f�[�^��擾����Ƃ��́A���̋t��s����ƕ֗��ł��B |
|
529 | ||
530 |
=head3 �t�B���^�̓o�^ C<register_filter()> |
|
531 | ||
532 |
�t�B���^��o�^����ɂ�C<register_filter()>��g�p���܂��B |
|
533 | ||
534 |
$dbi->register_filter( |
|
535 |
# Time::Piece object to DATE format |
|
536 |
tp_to_date => sub { |
|
537 |
my $date = shift; |
|
538 | ||
539 |
return '0000-00-00' unless $tp; |
|
540 |
return $tp->strftime('%Y-%m-%d'); |
|
541 |
}, |
|
542 |
|
|
543 |
# DATE to Time::Piece object |
|
544 |
date_to_tp => sub { |
|
545 |
my $date = shift; |
|
546 | ||
547 |
return if $date eq '0000-00-00'; |
|
548 |
return Time::Piece->strptime($date, '%Y-%m-%d'); |
|
549 |
}, |
|
550 |
); |
|
added DBIx::Custom::Guides
|
551 | |
update pod
|
552 |
�o�^�����t�B���^��C<apply_filter()>�Ȃǂŗ��p���邱�Ƃ��ł��܂��B |
added DBIx::Custom::Guides
|
553 | |
update pod
|
554 |
=head3 �t�B���^�̓K�p C<apply_filter()> |
added DBIx::Custom::Guides
|
555 | |
update pod
|
556 |
�쐬�����t�B���^��K�p����ɂ́AC<apply_filter()>��g�p���܂��B |
557 | ||
558 |
$dbi->apply_filter('book', |
|
559 |
issue_date => {out => 'tp_to_date', in => 'date_to_tp'}, |
|
560 |
first_issue_date => {out => 'tp_to_date', in => 'date_to_tp'} |
|
added DBIx::Custom::Guides
|
561 |
); |
562 | ||
update pod
|
563 |
����̓e�[�u�����ł��B����ȍ~�́A�ƃt�B���^���[���̃y�A��L�q���܂��B |
564 |
�t�B���^���[����out�ɂ́A�f�[�^�x�[�X�Ƀf�[�^�𑗐M����Ƃ��ɓK�p����t�B���^��A |
|
565 |
�t�B���^���[����in�ɂ́A�f�[�^�x�[�X����f�[�^��擾����Ƃ��ɓK�p����t�B���^�� |
|
566 |
�L�q���܂��Bout���f�[�^�x�[�X�ɑ��M������Ain���f�[�^�x�[�X������o�����ł��B |
|
567 |
�t�B���^�ɂ́AC<register_filter>�œo�^�����t�B���^���̑��ɁA�R�[�h���t�@�����X�� |
|
568 |
�w�肷�邱�Ƃ�ł��܂��B |
|
569 | ||
570 |
issue_date => {out => sub { ... }, in => sub { ... }} |
|
571 | ||
572 |
�K�p���ꂽ�t�B���^��C<insert()>�AC<update()>�AC<update_all()>�AC<delete()>�A |
|
573 |
C<delete_all()>�AC<select()>�ŗL��ɂȂ�܂��B |
|
574 | ||
575 |
my $tp = Time::Piece->strptime('2010/10/14', '%Y/%m/%d'); |
|
576 |
my $result = $dbi->select(table => 'book', where => {issu_date => $tp}); |
|
577 | ||
578 |
�f�[�^�x�[�X�Ƀf�[�^�����M�����Ƃ��ɁAL<Time::Piece>�I�u�W�F�N�g�� |
|
579 |
�f�[�^�x�[�X�̓�t�̃t�H�[�}�b�g�u2010-10-14�v�ɕϊ�����܂��B |
|
580 | ||
581 |
�܂��t�Ƀf�[�^��t�F�b�`����Ƃ��ɂ́A�f�[�^�x�[�X�̓�t�̃t�H�[�}�b�g�� |
|
582 |
�^�C���s�[�X�I�u�W�F�N�g�ɕϊ�����܂��B |
|
583 | ||
584 |
my $row = $resutl->fetch_hash_first; |
|
585 |
my $tp = $row->{issue_date}; |
|
586 | ||
587 |
���̂悤�Ȏ����I�Ɏ�s�����t�B���^��o�^�ł��邱�Ƃ�L<DBIx::Custom>�� |
|
588 |
����̂ЂƂł��B |
|
added DBIx::Custom::Guides
|
589 | |
update pod
|
590 |
C<apply_filter()>�œK�p���ꂽ�t�B���^�̓e�[�u�������܂ޗɑ��Ă�L��ł��B |
591 | ||
592 |
$dbi->select( |
|
593 |
table => 'book', |
|
594 |
where => {'book.title' => 'Perl', 'book.author' => 'Ken'} |
|
595 |
); |
|
596 | ||
597 |
�e�[�u�����ʂ���K�v������Ƃ��ɕ֗��ȋ@�\�ł��B |
|
598 | ||
599 |
=head3 �ʂ̃t�B���^�̓K�p C<filter> |
|
600 | ||
601 |
C<apply_filter()>��g��čŏ��ɂ��ׂẴe�[�u���̗�ɂ��� |
|
602 |
�t�B���^���`���邱�Ƃ�ł��܂����A |
|
603 |
�ʂɃt�B���^��K�p���邱�Ƃ�ł��܂��B |
|
604 |
�ʂ̃t�B���^��C<apply_filter()>�œK�p�����t�B���^��㏑���܂��B |
|
605 |
�ʂ̃t�B���^��SQL��as��g��āA��̕ʖ���쐬����K�v������ꍇ�Ɋ��܂��B |
|
606 | ||
607 |
�f�[�^�x�[�X�ɑ��M����ꍇ�ɁA�ʂ̃t�B���^��K�p����ɂ́A�e���\�b�h�� |
|
608 |
C<filter>�I�v�V������g�p���܂��B�ʂ̃t�B���^�́AC<insert()>�AC<update()>�A |
|
609 |
C<update_all()>�AC<delete()>�AC<delete_all()>�AC<select()>�AC<execute()> |
|
610 |
�Ŏg�p���邱�Ƃ��ł��܂��B |
|
611 | ||
612 |
C<insert()>�̗����܂��B |
|
613 | ||
614 |
$dbi->insert( |
|
615 |
table => 'book', |
|
616 |
param => {issue_date => $tp, first_issue_date => $tp}, |
|
617 |
filter => {issue_date => 'tp_to_date', first_issue_date => 'tp_to_date'} |
|
618 |
); |
|
added DBIx::Custom::Guides
|
619 | |
update pod
|
620 |
C<execute()>�̗����܂��B |
621 | ||
622 |
my $sql = <<"EOS"; |
|
623 |
select YEAR(issue_date) as issue_year |
|
624 |
from book |
|
625 |
where YEAR(issue_date) = {? issue_year} |
|
626 |
EOS |
|
627 |
|
|
added DBIx::Custom::Guides
|
628 |
my $result = $dbi->execute( |
update pod
|
629 |
$sql, |
630 |
param => {issue_year => '2010'}, |
|
631 |
filter => {issue_year => 'tp_to_year'} |
|
added DBIx::Custom::Guides
|
632 |
); |
633 | ||
update pod
|
634 |
�����C<filter>��g���ǂ������ł��Bissue_date�̕ϊ��ɂ��Ă�C<apply_filter()> |
635 |
�œo�^���Ă���̂ł����A�V�����쐬������ł���issue_year�ɂ��ẮA |
|
636 |
���̕ϊ���o�^����Ă��܂���B�ł��̂ŁA�ʂɃt�B���^��ݒ肵�Ă��܂��B |
|
added DBIx::Custom::Guides
|
637 | |
update pod
|
638 |
�܂����ɍs��t�F�b�`����Ƃ��ɂ�ʂ̃t�B���^��K�p���邱�Ƃ��ł��܂��B |
639 |
�t�B���^��K�p����ɂ́A |
|
640 |
C<DBIx::Custom::Result>�N���X��C<filter>���\�b�h��g�p���܂��B |
|
added DBIx::Custom::Guides
|
641 | |
update pod
|
642 |
$result->filter(issue_year => 'year_to_tp'); |
added DBIx::Custom::Guides
|
643 | |
update pod
|
644 |
�p�ɂɗ��p����̂ł���A�ʂɓo�^�������C<apply_filter()>�œo�^ |
645 |
���Ă������ق����֗��ł��傤�BC<apply_filter()>�͑��݂��Ȃ���ɑ��Ă� |
|
646 |
�t�B���^��K�p�ł��邩��ł��B |
|
647 | ||
648 |
$dbi->apply_filter('book', |
|
649 |
'issue_year' => {out => 'tp_to_year', in => 'year_to_tp'} |
|
650 |
); |
|
651 | ||
652 |
=head3 �ŏI�o�͂̂��߂̃t�B���^�����O C<end_filter()> |
|
653 | ||
654 |
C<DBIx::Custom::Result>�ł͂���ɍŌ�ɂ���x�A�t�B���^��lj�� |
|
655 |
�o�^���邱�Ƃ��ł��܂��B���Ƃ���HTML�ɏo�͂������ꍇ�ɁATime::Piece |
|
656 |
�I�u�W�F�N�g����ǂ݂₷���L�q�ɕϊ����邱�Ƃ��ł��܂��B |
|
657 |
�Ō�̃t�B���^��o�^����ɂ́AC<end_filter()>��g�p���܂��B |
|
658 | ||
659 |
$result->end_filter(issue_date => sub { |
|
660 |
my $tp = shift; |
|
661 |
|
|
662 |
return '' unless $tp; |
|
663 |
return $tp->strftime('%Y/%m/%d %h:%m:%s (%a)'); |
|
664 |
}); |
|
665 | ||
666 |
��t��₷���`�Ƀt�H�[�}�b�g���邱�Ƃ��ł��܂��B |
|
667 | ||
668 |
�t�B���^�̓t�F�b�`��s���O�ɓo�^���Ă����K�v�����邱�Ƃ� |
|
669 |
���ӂ��Ă��������B |
|
670 | ||
671 |
$result->filter(...); |
|
672 |
$result->end_filter(...); |
|
673 |
my $row = $result->fetch_hash_first; |
|
674 | ||
675 |
=head3 ��̏���Ƀt�B���^��K�p���� C<each_column()> |
|
676 | ||
677 |
��t�^�̗�͎蓮�Őݒ肵�Ȃ��Ă�A�����I�ɐݒ�ł���ƕ֗��ł��B |
|
678 |
���̂��߂Ƀf�[�^�x�[�X�̃e�[�u���̗�̂��ׂĂ̏��� |
|
679 |
���Ԃɏ������邽�߂�C<each_column()>������܂��B |
|
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 |
each_column�̓R�[���o�b�N����܂��B�R�[���o�b�N�̈�� |
|
697 |
���Ԃ�L<DBIx::Custom>�I�u�W�F�N�g�A�e�[�u�����A�A��̏��ł��B |
|
698 |
��̌^���̏����ƂɎ����I�ɁA�t�B���^��K�p���Ă��܂��B |
|
699 | ||
700 |
�ЂƂ̒��ӓ_�Ƃ��ăR�[���o�b�N�̒�����A�R�[���o�b�N�̊O�� |
|
701 |
�̕ϐ���Q�Ƃ��Ȃ��悤�ɒ��ӂ��Ă��������Beach_column�� |
|
702 |
���X1����s����邾���Ȃ̂ŁA�قƂ�ǂ̏ꍇ��肠��܂��A |
|
703 |
�z�Q�Ƃɂ�郁�������[�N���������Ă��܂��\�����Ă��邩��ł��B |
|
704 | ||
705 |
=head2 5. �^�O |
|
706 | ||
707 |
=head3 �^�O�̋@�\ |
|
708 | ||
709 |
L<DBIx::Custom>��SQL�̒��Ƀ^�O�ߍ��ދ@�\���Ă��܂��B |
|
710 | ||
711 |
select * from book where {= title} and {like author}; |
|
712 | ||
713 |
{= title}��{like author}�̕������^�O�ł��B�^�O�͎��̂悤�Ȍ`�� |
|
714 |
����܂��B |
|
715 | ||
716 |
{�^�O�� ��1 ��2 ...} |
|
added DBIx::Custom::Guides
|
717 |
|
update pod
|
718 |
�^�O��C<{>�Ŏn�܂�AC<}>�ŏI���܂��B�ŏ���C<{>�ƃ^�O���̊� |
719 |
�ɂ͋�}��Ȃ��悤���ӂ��Ă��������B |
|
720 | ||
721 |
�^�O�̋@�\�̂��߂�C<{>��C<}>�͗\���ɂȂ�Ă��܂��B |
|
722 |
�����p�������ꍇ�͒��O��\����ăG�X�P�[�v��s���K�v������܂��B |
|
723 | ||
724 |
select from book \\{ ... \\} |
|
725 | ||
726 |
C<\>���̂�Perl�̃G�X�P�[�v�����ł��̂ŁA |
|
727 |
�G�X�P�[�v����ꍇ��C<\>����K�v�ɂȂ�Ƃ����_�ɒ��ӂ��Ă��������B |
|
728 | ||
729 |
��L�̃^�O��SQL����s�����O�Ɏ���SQL�ɓW�J����܂��B |
|
730 | ||
731 |
select * from book where title = ? and author like ?; |
|
732 | ||
733 |
�^�O��܂�SQL���s����ɂ�C<execute()>��g�p���܂��B |
|
734 | ||
735 |
my $sql = "select * from book where {= author} and {like title};" |
|
736 |
$dbi->execute($sql, param => {title => 'Perl', author => '%Ken%'}); |
|
737 | ||
738 |
C<param>�I�v�V������g��āA�v���[�X�z���_�ɖ��ߍ��݂����l�� |
|
739 |
�n�b�V�����t�@�����X�Ŏw�肷�邱�Ƃ��ł��܂��B |
|
740 | ||
741 |
���̃��\�b�h�Ɠ��l��C<execute()>�ɂ����Ă�C<filter>��w�肷�邱�Ƃ��ł��܂��B |
|
742 | ||
743 |
$dbi->execute($sql, param => {title => 'Perl', author => '%Ken%'} |
|
744 |
filter => {title => 'to_something'); |
|
745 | ||
746 |
C<execute>�̂ЂƂ̒��ӓ_�Ƃ��Ă�C<apply_filter()>�œK�p���ꂽ�t�B���^ |
|
747 |
�̓f�t�H���g�ł͗L��ł͂Ȃ��Ƃ������Ƃɒ��ӂ��Ă��������B |
|
748 |
C<apply_filter()>�œK�p���ꂽ�t�B���^��L��ɂ���ɂ́A |
|
749 |
C<table>��w�肷��K�v������܂��B |
|
750 | ||
751 |
$dbi->execute($sql, param => {title => 'Perl', author => '%Ken%'} |
|
752 |
table => ['book']); |
|
753 | ||
754 |
=head2 �^�O�ꗗ |
|
755 | ||
756 |
L<DBIx::Custom>�ł͎��̃^�O���g�p�\�ł��B |
|
757 | ||
758 |
���̂悤�Ƀ^�O��g���SQL����\������̂�L<DBIx::Custom>�� |
|
759 |
����ł��B�ȉ��̃^�O�����p�\�ł��B |
|
760 | ||
761 |
=head3 C<?> |
|
762 | ||
763 |
C<?>�^�O�͈ȉ��̂悤�ɓW�J����܂��B |
|
764 | ||
765 |
{? NAME} -> ? |
|
766 | ||
767 |
=head3 C<=> |
|
768 | ||
769 |
C<=>�^�O�͈ȉ��̂悤�ɓW�J����܂��B |
|
770 | ||
771 |
{= NAME} -> NAME = ? |
|
772 | ||
773 |
=head3 C<E<lt>E<gt>> |
|
774 | ||
775 |
C<E<lt>E<gt>>�^�O�͈ȉ��̂悤�ɓW�J����܂��B |
|
776 | ||
777 |
{<> NAME} -> NAME <> ? |
|
778 | ||
779 |
=head3 C<E<lt>> |
|
780 | ||
781 |
C<E<lt>>�^�O�͈ȉ��̂悤�ɓW�J����܂��B |
|
782 | ||
783 |
{< NAME} -> NAME < ? |
|
784 | ||
785 |
=head3 C<E<gt>> |
|
786 | ||
787 |
C<E<gt>>�^�O�͈ȉ��̂悤�ɓW�J����܂��B |
|
788 | ||
789 |
{> NAME} -> NAME > ? |
|
790 | ||
791 |
=head3 C<E<gt>=> |
|
792 | ||
793 |
C<E<gt>=>�^�O�͈ȉ��̂悤�ɓW�J����܂��B |
|
794 | ||
795 |
{>= NAME} -> NAME >= ? |
|
796 | ||
797 |
=head3 C<E<lt>=> |
|
798 | ||
799 |
C<E<lt>=>�^�O�͈ȉ��̂悤�ɓW�J����܂��B |
|
800 | ||
801 |
{<= NAME} -> NAME <= ? |
|
802 | ||
803 |
=head3 C<like> |
|
804 | ||
805 |
C<like>�^�O�͈ȉ��̂悤�ɓW�J����܂��B |
|
806 | ||
807 |
{like NAME} -> NAME like ? |
|
808 | ||
809 |
=head3 C<in> |
|
810 | ||
811 |
C<in>�^�O�͈ȉ��̂悤�ɓW�J����܂��B�v���[�X�z���_�� |
|
812 |
�����Ŏw�肷��K�v�����邱�Ƃɒ��ӂ��Ă��������B |
|
813 | ||
814 |
{in NAME COUNT} -> NAME in [?, ?, ..] |
|
815 | ||
816 |
=head3 C<insert_param> |
|
817 | ||
818 |
C<insert_param>�^�O�͈ȉ��̂悤�ɓW�J����܂��B |
|
819 | ||
added DBIx::Custom::Guides
|
820 |
{insert_param NAME1 NAME2} -> (NAME1, NAME2) values (?, ?) |
update pod
|
821 | |
822 |
=head3 C<update_param> |
|
823 | ||
824 |
C<update_param>�^�O�͈ȉ��̂悤�ɓW�J����܂��B |
|
825 | ||
added DBIx::Custom::Guides
|
826 |
{update_param NAME1 NAME2} -> set NAME1 = ?, NAME2 = ? |
827 | ||
update pod
|
828 |
=head2 �����̗�̈��� |
added DBIx::Custom::Guides
|
829 | |
update pod
|
830 |
�����̗��܂ރ^�O������ꍇ�ɂ�ASQL���s���邱�Ƃ��ł��܂��B |
831 |
���Ƃ��A��̓�t�Ŕ�r���Ȃ���Ȃ�Ȃ��ꍇ�� |
|
832 |
�l���Č��܂��傤�B |
|
added DBIx::Custom::Guides
|
833 | |
update pod
|
834 |
my $sql = "select * from table where {> date} and {< date};"; |
added DBIx::Custom::Guides
|
835 | |
update pod
|
836 |
���̂悤�ȏꍇ�͑Ή�����p�����[�^�̒l��z��̃��t�@�����X�ɂ��܂��B |
added DBIx::Custom::Guides
|
837 | |
update pod
|
838 |
my $dbi->execute($sql, param => {date => ['2010-10-01', '2012-02-10']}); |
added DBIx::Custom::Guides
|
839 | |
update pod
|
840 |
=head2 �^�O�̒lj� C<register_tag()> |
841 | ||
842 |
L<DBIx::Custom>�ł̓^�O��Ǝ��ɒlj���邱�Ƃ��ł��܂��B |
|
843 |
�^�O��lj����ɂ�C<register_tag()>��g�p���܂��B |
|
844 | ||
845 |
$dbi->register_tag( |
|
846 |
'=' => sub { |
|
847 |
my $column = shift; |
|
848 |
|
|
849 |
return ["$column = ?", [$column]]; |
|
added DBIx::Custom::Guides
|
850 |
} |
851 |
); |
|
852 | ||
update pod
|
853 |
�����ł̓f�t�H���g��C<=>�^�O���ǂ̂悤�Ɏ������Ă��邩����Ă��܂��B |
854 |
�^�O��o�^������̈�̓^�O�̒��ɏ����ꂽ��ɂȂ�܂��B |
|
added experimental DBIx::Cus...
|
855 | |
update pod
|
856 |
{�^�O�� ��1 ��2} |
added experimental DBIx::Cus...
|
857 | |
update pod
|
858 |
C<=>�^�O�̏ꍇ�� |
cleanup
|
859 | |
update pod
|
860 |
{= title} |
added experimental DBIx::Cus...
|
861 | |
update pod
|
862 |
�Ƃ����`���ł�����A�T�u���[�`���ɂ�title�Ƃ����ЂƂ̗��킽��Ă��܂��B |
added experimental DBIx::Cus...
|
863 | |
update pod
|
864 |
�T�u���[�`���̖߂�l�ɂ͎��̌`���̔z��̃��t�@�����X��Ԃ��K�v������܂��B |
865 | ||
866 |
[ |
|
867 |
�W�J��̕�����, |
|
868 |
[�v���[�X�z���_�ɖ��ߍ��݂ɗ��p�����1, ��2, ...] |
|
869 |
] |
|
870 | ||
871 |
��ڂ̗v�f�͓W�J��̕�����ł��B���̗�ł� |
|
872 | ||
873 |
'title = ?' |
|
874 | ||
875 |
��Ԃ��K�v������܂��B |
|
876 | ||
877 |
��ڂ̗v�f�̓v���[�X�z���_�ɖ��ߍ��݂ɗ��p�����܂ޔz��� |
|
878 |
���t�@�����X�ł��B����̗�ł� |
|
879 | ||
880 |
['title'] |
|
881 | ||
882 |
��Ԃ��K�v������܂��B�����̃v���[�X�z���_��܂ޏꍇ�́A���̕����� |
|
883 |
�����ɂȂ�܂��BC<insert_param>�^�O��C<update_param>�^�O�� |
|
884 |
���̕�������ە����ɂȂ�Ă��܂��B |
|
885 | ||
886 |
��L��킹��� |
|
887 | ||
888 |
['title = ?', ['title']] |
|
added experimental DBIx::Cus...
|
889 |
|
update pod
|
890 |
��Ԃ��K�v������Ƃ������Ƃł��B |
added DBIx::Custom::Guides
|
891 | |
update pod
|
892 |
�^�O�̎���̑��̃T���v����L<DBIx::Custom::Tag>�̃\�[�X�R�[�h |
893 |
����ɂȂ�Ă݂Ă��������B |
|
added DBIx::Custom::Guides
|
894 | |
update pod
|
895 |
=head2 6. Where��̓��I�Ȑ��� |
896 | ||
897 |
=head3 Where��̓��I�Ȑ��� where() |
|
898 | ||
899 |
�����̌�����w�肵�āA�����s�������ꍇ������܂��B |
|
900 |
����3�̃P�[�X��where���l���Ă݂܂��傤�B |
|
901 |
���L�̂悤��where�傪�K�v�ɂȂ�܂��B |
|
902 | ||
903 |
title�̒l�����Ō�������ꍇ |
|
904 | ||
905 |
where {= title} |
|
906 | ||
907 |
author�̒l�����Ō�������ꍇ |
|
908 | ||
909 |
where {= author} |
|
910 | ||
911 |
title��author�̗���̒l�Ō�������ꍇ |
|
912 | ||
913 |
where {= title} and {=author} |
|
914 | ||
915 |
L<DBIx::Custom>�ł͓��I��Where��̐�����T�|�[�g���Ă��܂��B |
|
916 |
�܂�C<where()>��L<DBIx::Custom::Where>�I�u�W�F�N�g�����܂��B |
|
917 | ||
918 |
my $where = $dbi->where; |
|
919 | ||
920 |
����C<clause()>��g�p����where���L�q���܂��B |
|
921 | ||
922 |
$where->clause( |
|
923 |
['and', '{= title'}, '{= author}'] |
|
added DBIx::Custom::Guides
|
924 |
); |
925 | ||
update pod
|
926 |
clause�̎w���@�͎��̂悤�ɂȂ�܂��B |
927 | ||
928 |
['or' ���邢�� 'and', �^�O1, �^�O2, �^�O3] |
|
added DBIx::Custom::Guides
|
929 | |
update pod
|
930 |
����ɂ�or���邢��and��w�肵�܂��B����ȍ~�ɂ� |
931 |
������^�O��g��ċL�q���܂��B |
|
932 | ||
933 |
C<clause>�̎w��͓��q�ɂ��邱�Ƃ�ł��A����ɕ��G�ȏ� |
|
934 |
��L�q���邱�Ƃ�ł��܂��B |
|
935 | ||
936 |
['and', |
|
937 |
'{= title}', |
|
938 |
['or', '{= author}', '{like date}'] |
|
939 |
] |
|
940 | ||
941 |
���̂悤��C<clause>��ݒ肵�����C<param>�Ƀp�����[�^��w�肵�܂��B |
|
added DBIx::Custom::Guides
|
942 |
|
update pod
|
943 |
my $param => {title => 'Perl'}; |
944 |
$where->param($param); |
|
added DBIx::Custom::Guides
|
945 | |
update pod
|
946 |
���̗�ł�title�������p�����[�^�Ɋ܂܂�Ă��܂��B |
added DBIx::Custom::Guides
|
947 | |
update pod
|
948 |
���̌�C<to_string()>���s�����$param�Ɋ܂܂��p�����[�^���� |
949 |
where������邱�Ƃ��ł��܂��B |
|
950 | ||
951 |
my $where_clause = $where->to_string; |
|
952 | ||
953 |
�p�����[�^��title�����ł��̂ŁA���̂悤��where�傪��������܂��B |
|
954 | ||
955 |
where {= title} |
|
956 | ||
957 |
�܂�L<DBIx::Custom>�͕�����̕]����I�[�o�[���[�h���āAC<to_string()> |
|
958 |
��Ăяo���悤�ɂ��Ă��܂��̂ŁA���̂悤�ɂ���where������邱�Ƃ� |
|
959 |
�ł��܂��B |
|
960 | ||
961 |
my $where_clause = "$where"; |
|
962 | ||
963 |
�����SQL�̒���where��ߍ��ނƂ��ɂƂĂ�𗧂@�\�ł��B |
|
added DBIx::Custom::Guides
|
964 | |
update pod
|
965 |
=head3 ����̗�܂ޏꍇ |
check arguments of connect m...
|
966 | |
update pod
|
967 |
�^�O�̒��ɓ���̖��O���̂����݂����ꍇ�ł��I�� |
968 |
where���쐬���邱�Ƃ��ł��܂��B |
|
added experimental DBIx::Cus...
|
969 | |
update pod
|
970 |
���Ƃ��A�p�����[�^�Ƃ��ĊJ�n��t�ƏI����t��������Ƃ� |
971 |
�l���Ă݂Ă��������B |
|
added experimental DBIx::Cus...
|
972 | |
update pod
|
973 |
my $param = {start_date => '2010-11-15', end_date => '2011-11-21'}; |
added experimental DBIx::Cus...
|
974 | |
update pod
|
975 |
�܂��J�n��t�ƏI����t�̕Е���A�ǂ������Ȃ��ꍇ���邩����܂���B |
added experimental DBIx::Cus...
|
976 | |
update pod
|
977 |
���̏ꍇ�͎��̂悤�ȃp�����[�^�ɕϊ����邱�ƂőΉ����邱�Ƃ��ł��܂��B |
978 | ||
979 |
my $p = {date => ['2010-11-15', '2011-11-21']}; |
|
980 | ||
981 |
�l���z��̃��t�@�����X�ɂȂ�Ă��邱�Ƃɒ��ڂ��Ă��������B���̂悤�ɂ���� |
|
982 |
�����̗��܂ރ^�O�ɏ��Ԃɖ��ߍ��ނ��Ƃ��ł��܂��B |
|
983 | ||
984 |
$where->clause( |
|
985 |
['and', '{> date}', '{< date}'] |
|
added experimental DBIx::Cus...
|
986 |
); |
update pod
|
987 |
$where->param($p); |
988 | ||
989 |
�܂��J�n��t�����݂��Ȃ��ꍇ�͎��̂悤�ȃf�[�^��쐬���܂��B |
|
990 | ||
991 |
my $p = {date => [$dbi->not_exists, '2011-11-21']}; |
|
992 | ||
993 |
L<DBIx::Custom>��C<not_exists>��DBIx::Custom::NotExists�I�u�W�F�N�g�� |
|
994 |
�擾�ł��܂��B����͑Ή�����l�����݂��Ȃ����Ƃ�����߂̂�̂ł��B |
|
995 | ||
996 |
�܂��I����t�����݂��Ȃ��ꍇ�͎��̂悤�ȃf�[�^��쐬���܂��B |
|
997 | ||
998 |
my $p = {date => ['2010-11-15']}; |
|
999 | ||
1000 |
�ǂ�����݂��Ȃ��ꍇ�͎��̂悤�ȃf�[�^��쐬���܂��B |
|
1001 | ||
1002 |
my $p = {date => []}; |
|
1003 | ||
1004 |
��������̂ň�ԊȒP�ɍ쐬�ł��郍�W�b�N����Ă����܂��B |
|
1005 | ||
1006 |
my @date; |
|
1007 |
push @date, exists $param->{start_date} ? $param->{start_date} |
|
1008 |
: $dbi->not_exists; |
|
1009 |
push @date, $param->{end_date} if exists $param->{end_date}; |
|
1010 |
my $p = {date => \@date}; |
|
1011 | ||
1012 |
=head3 C<select()>�Ƃ̘A�g |
|
1013 | ||
1014 |
L<DBIx::Custom::Where>�I�u�W�F�N�g�� |
|
1015 |
C<select()>��C<where>�ɒ��ړn�����Ƃ� |
|
1016 |
�ł��܂��B |
|
1017 |
|
|
1018 |
my $where = $dbi->where; |
|
1019 |
$where->clause(...); |
|
1020 |
$where->param($param); |
|
1021 |
my $result = $dbi->select(table => 'book', where => $where); |
|
1022 | ||
1023 |
���邢��C<update()>�AC<delete()>��where�Ɏw�肷�邱�Ƃ�\�ł��B |
|
1024 | ||
1025 |
=head3 C<execute()>�Ƃ̘A�g |
|
1026 | ||
1027 |
C<execute()>�Ƃ̘A�g�ł��BSQL��쐬����Ƃ��ɖ��ߍ��ނ��Ƃ��ł��܂��B |
|
added experimental DBIx::Cus...
|
1028 | |
1029 | ||
update pod
|
1030 |
my $where = $dbi->where; |
1031 |
$where->clause(...); |
|
1032 |
$where->param($param); |
|
1033 | ||
1034 |
my $sql = <<"EOS" |
|
1035 |
select * from book; |
|
1036 |
$where |
|
1037 |
EOS |
|
1038 | ||
1039 |
$dbi->execute($sql, param => $param); |
|
1040 | ||
1041 |
=head2 7. �e�[�u�����f�� |
|
1042 | ||
1043 |
=head3 �e�[�u���I�u�W�F�N�g�̍쐬 C<table()> |
|
1044 | ||
1045 |
L<DBIx::Custom>�̓��W�b�N�Ƃ��ăe�[�u���𒆐S�ɂ����� |
|
1046 |
���f���̍쐬��x�����܂��B |
|
1047 | ||
1048 |
�A�v���P�[�V�����̃��W�b�N��L�q����Ƃ��ɁA���̃��W�b�N�� |
|
1049 |
�f�[�^�x�[�X�̃e�[�u���ɂ����邱�Ƃ́ARDBMS�𗘗p���� |
|
1050 |
���f���ł���A�R�[�h�̏d�����Ȃ� |
|
1051 |
�킩��₷����̂ɂȂ�܂��B |
|
1052 | ||
1053 |
�e�[�u���I�u�W�F�N�g������ɂ�C<table()>��g�p���܂��B |
|
1054 | ||
1055 |
my $table = $dbi->table('book'); |
|
1056 | ||
1057 |
��ۂɃf�[�^�x�[�X�Ƀe�[�u���͑��݂��Ă���K�v�͂���܂���B |
|
1058 |
����͉��z�I�ȃe�[�u���I�u�W�F�N�g�ł��B����� |
|
1059 |
L<DBIx::Customm::Table>�I�u�W�F�N�g�ɂȂ�܂��B |
|
1060 | ||
1061 |
�e�[�u���I�u�W�F�N�g�����C<insert()>�AC<update()>�AC<update_all()>�A |
|
1062 |
C<delete()>�AC<delete_all()>�AC<select()>�Ȃǂ̃��\�b�h�Ăяo�����Ƃ��ł��܂��B |
|
1063 |
L<DBIx::Custom>�ƈقȂ�Ƃ���́AC<table>��K������w�肷��K�v�� |
|
1064 |
�Ȃ��Ƃ������Ƃł��B |
|
1065 | ||
1066 |
$table->insert(param => $param); |
|
1067 | ||
1068 |
C<table���̒l�͎����I��book�ɐݒ肳��܂��B |
|
1069 | ||
1070 |
�܂��e�[�u���I�u�W�F�N�g�ɂ͓Ǝ��̃��\�b�h��lj���邱�Ƃ��ł��܂��B |
|
1071 | ||
1072 |
$table->method( |
|
1073 |
register => sub { |
|
added experimental DBIx::Cus...
|
1074 |
my $self = shift; |
update pod
|
1075 |
my $table_name = $self->name; |
1076 |
# something |
|
added experimental DBIx::Cus...
|
1077 |
}, |
update pod
|
1078 |
list => sub { |
1079 |
my $self = shift; |
|
1080 |
my $table_name = $self->name; |
|
1081 |
# something |
|
1082 |
} |
|
1083 |
); |
|
1084 | ||
1085 |
���\�b�h�ɓn���������L<DBIx::Custom::Table>�I�u�W�F�N�g�ł��B |
|
1086 |
C<name()>��g�p���āA�e�[�u������擾���邱�Ƃ��ł��܂��B |
|
1087 | ||
1088 |
���̂悤�ɂ��ēo�^�������\�b�h�͒��ڌĂяo�����Ƃ��ł��܂��B |
|
1089 | ||
1090 |
$table->register(...); |
|
1091 |
$table->list(...); |
|
1092 | ||
1093 |
�܂��e�[�u����p�̃��\�b�h��I�[�o�[���C�h���č쐬���邱�Ƃ�ł��܂��B |
|
1094 | ||
1095 |
$table->method( |
|
1096 |
insert => sub { |
|
added experimental DBIx::Cus...
|
1097 |
my $self = shift; |
1098 |
|
|
update pod
|
1099 |
$self->base_insert(...); |
1100 |
|
|
1101 |
# something |
|
added experimental DBIx::Cus...
|
1102 |
} |
1103 |
); |
|
1104 | ||
update pod
|
1105 |
��Ƃ�Ƒ��݂��Ă���C<insert()>��ĂԂɂ�C<base_$method>�Ƃ��܂��BL<DBIx::Custom::Table> |
1106 |
�̃I�[�o�[���C�h�̋@�\�͊ȈՓI�Ȃ�̂ł����A�ƂĂ�֗��ł��B |
|
added experimental DBIx::Cus...
|
1107 | |
update pod
|
1108 |
=head2 �e�[�u���ŋ��L�̃��\�b�h�̓o�^ |
added experimental DBIx::Cus...
|
1109 | |
update pod
|
1110 |
���ׂẴe�[�u���Ń��\�b�h��L����ɂ�C<table>���\�b�h�Ńe�[�u����쐬����O�ɁA |
1111 |
C<base_table>�Ƀ��\�b�h��o�^���Ă����܂��B |
|
added experimental DBIx::Cus...
|
1112 | |
update pod
|
1113 |
$dbi->base_table->method( |
1114 |
count => sub { |
|
1115 |
my $self = shift; |
|
1116 |
return $self->select(column => ['count(*)']); |
|
1117 |
} |
|
1118 |
); |
|
added DBIx::Custom::Guides
|
1119 | |
update pod
|
1120 |
�܂��e�[�u�������L<DBIx::Custom>��L<DBI>�̂��ׂẴ��\�b�h��Ăяo�����Ƃ��ł��܂��B |
added DBIx::Custom::Guides
|
1121 | |
update pod
|
1122 |
# DBIx::Custom method |
1123 |
$table->execute($sql); |
|
1124 |
|
|
1125 |
# DBI method |
|
1126 |
$table->begin_work; |
|
1127 |
$table->commit; |
|
1128 | ||
1129 |
=head2 ��ʓI�ȃ��f���̍\�� |
|
added DBIx::Custom::Guides
|
1130 | |
update pod
|
1131 |
��ʓI�ɂ́AL<DBIx::Custom>��p�����ăR���X�g���N�^�̒��ɁA���f����쐬 |
1132 |
����̂��悢�ł��傤�B |
|
added DBIx::Custom::Guides
|
1133 | |
update pod
|
1134 |
package MyDBI; |
1135 |
|
|
1136 |
use base 'DBIx::Custom'; |
|
added DBIx::Custom::Guides
|
1137 |
|
update pod
|
1138 |
sub connect { |
1139 |
my $self = shift->SUPER::connect(@_); |
|
1140 |
|
|
1141 |
$self->base_table->method( |
|
1142 |
... => sub { ... } |
|
1143 |
); |
|
1144 |
|
|
1145 |
$self->table('book')->method( |
|
1146 |
insert_multi => sub { ... }, |
|
1147 |
... => sub { ... } |
|
1148 |
); |
|
1149 |
|
|
1150 |
$self->table('company')->method( |
|
1151 |
... => sub { ... }, |
|
1152 |
); |
|
1153 |
} |
|
1154 | ||
1155 |
���̂悤�ɂ��Ē�`���Ă����A���̂悤�ɗ��p���邱�Ƃ��ł��܂��B |
|
1156 | ||
1157 |
my $dbi = MyDBI->connect(...); |
|
1158 |
$dbi->table('book')->insert_multi(...); |
|
1159 | ||
1160 |
=head2 8. �p�t�H�[�}���X�̉�P |
|
1161 | ||
1162 |
=head3 �N�G���̍쐬 |
|
1163 | ||
1164 |
��C<insert()>���\�b�h��g�p���ăC���T�[�g���s�����ꍇ�A |
|
1165 |
�K�v�ȃp�t�H�[�}���X���Ȃ��ꍇ�����邩����܂���B |
|
1166 |
C<insert()>���\�b�h�́ASQL���ƃX�e�[�g�����g�n���h���� |
|
1167 |
����쐬���邽�߂ł��B |
|
1168 | ||
1169 |
���̂悤�ȏꍇ�́AC<query>�I�v�V������w�肷�邱�ƂŁA |
|
1170 |
�N�G����擾���邱�Ƃ��ł��܂��B |
|
1171 | ||
1172 |
my $query = $dbi->insert(table => 'book', param => $param, query => 1); |
|
1173 | ||
1174 |
�܂�C<create_query()>���\�b�h��g��ĔC�ӂ�SQL�̃N�G����쐬 |
|
1175 |
���邱�Ƃ�ł��܂��B |
|
1176 | ||
added DBIx::Custom::Guides
|
1177 |
my $query = $dbi->create_query( |
update pod
|
1178 |
"insert into book {insert_param title author};"; |
added DBIx::Custom::Guides
|
1179 |
); |
1180 | ||
update pod
|
1181 |
�߂�l��L<DBIx::Custom::Query>�I�u�W�F�N�g�ł��B |
1182 |
���̃I�u�W�F�N�g��SQL���ƃp�����[�^�o�C���h���̗� |
|
1183 |
�ێ����Ă��܂��B�܂��X�e�[�g�����g�n���h����ێ����Ă��܂��B |
|
added DBIx::Custom::Guides
|
1184 | |
1185 |
{ |
|
remove DBIx::Custom::Model
|
1186 |
sql => 'insert into book (title, author) values (?, ?);', |
update pod
|
1187 |
columns => ['title', 'author'], |
1188 |
sth => $sth |
|
added DBIx::Custom::Guides
|
1189 |
} |
1190 | ||
update pod
|
1191 |
�N�G���I�u�W�F�N�g��g��ČJ��Ԃ���s����ɂ�C<execute()>��g�p���܂��B |
added DBIx::Custom::Guides
|
1192 |
|
remove DBIx::Custom::Model
|
1193 |
my $params = [ |
added DBIx::Custom::Guides
|
1194 |
{title => 'Perl', author => 'Ken'}, |
1195 |
{title => 'Good days', author => 'Mike'} |
|
1196 |
]; |
|
1197 |
|
|
update pod
|
1198 |
foreach my $param (@$paramss) { |
1199 |
$dbi->execute($query, table => 'book', param => $input); |
|
added DBIx::Custom::Guides
|
1200 |
} |
1201 | ||
update pod
|
1202 |
C<execute>���\�b�h�̑���ɃN�G���I�u�W�F�g��n�����Ƃ��ł��܂��B |
1203 |
C<insert()>���\�b�h�������ł��B |
|
added DBIx::Custom::Guides
|
1204 | |
update pod
|
1205 |
���ӓ_������������܂��B����̓p�����[�^�̐��͕K�������łȂ��Ă͂Ȃ�Ȃ� |
1206 |
�Ƃ������Ƃł��B�ŏ���3�̃p�����[�^������n�����̂ɁA���̎�s�ł� |
|
1207 |
��̃p�����[�^��n���Ɨ\��Ȃ����ʂɂȂ�܂��B����� |
|
1208 |
���I�ɐ������ꂽSQL�Ɋ܂܂��v���[�X�z���_�̐����قȂ邩��ł��B |
|
1209 |
�܂�C<execute()>�ɂ��Ă͎����I�ɂ̓t�B���^���L��ɂȂ�Ȃ��̂ŁA |
|
1210 |
C<table>��w�肷��K�v�̂��邱�Ƃɒ��ӂ��Ă��������B |
|
1211 |
�{���ɕK�v�ȏꍇ�������p���Ă��������B |
|
added DBIx::Custom::Guides
|
1212 | |
update pod
|
1213 |
=head2 9. ���̑��̋@�\ |
added DBIx::Custom::Guides
|
1214 | |
update pod
|
1215 |
=head3 ���\�b�h�̓o�^ |
added DBIx::Custom::Guides
|
1216 | |
update pod
|
1217 |
���\�b�h��o�^����ɂ�C<method()>��g�p���܂��B |
added DBIx::Custom::Guides
|
1218 | |
update pod
|
1219 |
$dbi->method( |
1220 |
update_or_insert => sub { |
|
1221 |
my $self = shift; |
|
1222 |
# something |
|
1223 |
}, |
|
1224 |
find_or_create => sub { |
|
1225 |
my $self = shift; |
|
1226 |
# something |
|
1227 |
} |
|
1228 |
); |
|
1229 | ||
1230 |
<method()>�œo�^�������\�b�h�� |
|
1231 |
L<DBIx::Custom>�I�u�W�F�N�g���璼�ڌĂяo�����Ƃ��ł��܂��B |
|
1232 | ||
1233 |
$dbi->update_or_insert; |
|
1234 |
$dbi->find_or_create; |
|
1235 | ||
1236 |
=head3 ���ʃN���X�̕ύX |
|
added DBIx::Custom::Guides
|
1237 | |
update pod
|
1238 |
�K�v�Ȃ�Ό��ʃN���X��ύX���邱�Ƃ��ł��܂��B |
added DBIx::Custom::Guides
|
1239 | |
update pod
|
1240 |
package MyResult; |
added DBIx::Custom::Guides
|
1241 |
use base 'DBIx::Custom::Result'; |
1242 |
|
|
1243 |
sub some_method { ... } |
|
1244 | ||
1245 |
1; |
|
1246 |
|
|
1247 |
package main; |
|
1248 |
|
|
update pod
|
1249 |
use MyResult; |
added DBIx::Custom::Guides
|
1250 |
|
1251 |
my $dbi = DBIx::Custom->connect(...); |
|
update pod
|
1252 |
$dbi->result_class('MyResult'); |
added DBIx::Custom::Guides
|
1253 | |
update pod
|
1254 |
=head3 �L���b�V���O |
added DBIx::Custom::Guides
|
1255 | |
update pod
|
1256 |
�^�O�̓W�J���SQL�̓p�t�H�[�}���X�̗��R�̂��߂ɃL���b�V������܂��B |
1257 |
�����C<chace>�Őݒ�ł��A�f�t�H���g�ł̓L���b�V����s���ݒ�ł��B |
|
added DBIx::Custom::Guides
|
1258 | |
update pod
|
1259 |
$dbi->cache(1); |
added DBIx::Custom::Guides
|
1260 | |
update pod
|
1261 |
�L���b�V����@��C<cache_method>�Ƀ��\�b�h��w�肷�邱�Ƃ� |
1262 |
�ύX���邱�Ƃ��ł��܂��B |
|
1263 |
�f�[�^�̕ۑ��Ǝ擾�̂��߂̃��\�b�h���`���܂��B |
|
added DBIx::Custom::Guides
|
1264 | |
update pod
|
1265 |
�f�t�H���g�ł͎��̂悤�Ƀ�������ɃL���b�V����s����̂ɂȂ�Ă��܂��B |
1266 | ||
1267 |
$dbi->cache_method(sub { |
|
1268 |
sub { |
|
added DBIx::Custom::Guides
|
1269 |
my $self = shift; |
update pod
|
1270 |
|
1271 |
$self->{_cached} ||= {}; |
|
1272 |
|
|
1273 |
if (@_ > 1) { |
|
1274 |
# Set |
|
1275 |
$self->{_cached}{$_[0]} = $_[1] |
|
1276 |
} |
|
1277 |
else { |
|
1278 |
# Get |
|
1279 |
return $self->{_cached}{$_[0]} |
|
1280 |
} |
|
added DBIx::Custom::Guides
|
1281 |
} |
update pod
|
1282 |
}); |
1283 |
|
|
1284 |
����L<DBIx::Custom>�I�u�W�F�N�g�ł��B |
|
1285 |
����̓^�O�̓W�J�����O��SQL�ł��B |
|
1286 |
��O��̓^�O�̓W�J���SQL�ł��B |
|
added DBIx::Custom::Guides
|
1287 | |
update pod
|
1288 |
�����ō쐬����ꍇ�͑�O�����݂����ꍇ�̓L���b�V����ݒ肵�A |
1289 |
���݂��Ȃ�����ꍇ�̓L���b�V����擾�������� |
|
1290 |
�����������B |
|
added DBIx::Custom::Guides
|
1291 | |
update pod
|
1292 |
=cut |
added DBIx::Custom::Guides
|
1293 | |
pod fix
|
1294 |
=head1 EXAMPLES |
1295 | ||
1296 |
L<DBIx::Custom Wiki|https://github.com/yuki-kimoto/DBIx-Custom/wiki> - Many useful examples |
|
add examples
|
1297 | |
added DBIx::Custom::Guides
|
1298 |
=cut |