... | ... |
@@ -1,456 +1,1243 @@ |
1 |
+=encoding utf8 |
|
2 |
+ |
|
1 | 3 |
=head1 NAME |
2 | 4 |
|
3 |
-DBIx::Custom::Guide - DBIx::Custom Guides |
|
5 |
+DBIx::Custom::Guide - DBIx::Custom Guide |
|
4 | 6 |
|
5 | 7 |
=head1 GUIDE |
6 | 8 |
|
7 |
-B<This guide is a little old and not complete. Please wait for a while.> |
|
9 |
+B<This guide is now writing.> |
|
8 | 10 |
|
9 |
-=head2 1. Connect to the database |
|
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>. |
|
10 | 15 |
|
11 |
- use DBIx::Custom; |
|
12 |
- my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=$database", |
|
13 |
- user => 'ken', password => '!LFKD%$&'); |
|
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 |
|
14 | 20 |
|
15 |
-use C<connect()> to connect to the database. |
|
16 |
-You can sepecfiy C<data_soruce>, C<user>, and C<password>. |
|
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>. |
|
17 | 25 |
|
18 |
-The following ones are data source exmaple in variouse dabase system. |
|
26 |
+I explain L<DBIx::Custom> a little in this section. |
|
27 |
+In L<DBIx::Custom>, you embbed tag in SQL. |
|
19 | 28 |
|
20 |
-SQLite |
|
29 |
+ select * from book where {= title} and {=author}; |
|
21 | 30 |
|
22 |
- "dbi:SQLite:dbname=$database" |
|
23 |
- "dbi:SQLite:dbname=:memory:" |
|
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�^ |
|
24 | 136 |
|
25 |
-MySQL |
|
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> |
|
26 | 185 |
|
27 | 186 |
"dbi:mysql:database=$database" |
28 | 187 |
"dbi:mysql:database=$database;host=$hostname;port=$port" |
29 | 188 |
|
30 |
-PostgreSQL |
|
189 |
+B<SQLite> |
|
190 |
+ |
|
191 |
+ "dbi:SQLite:dbname=$database" |
|
192 |
+ "dbi:SQLite:dbname=:memory:" |
|
193 |
+ |
|
194 |
+B<PostgreSQL> |
|
31 | 195 |
|
32 | 196 |
"dbi:Pg:dbname=$dbname" |
33 | 197 |
|
34 |
-Oracle |
|
198 |
+B<Oracle> |
|
35 | 199 |
|
36 | 200 |
"dbi:Oracle:$dbname" |
37 | 201 |
"dbi:Oracle:host=$host;sid=$sid" |
38 | 202 |
|
39 |
-ODBC(Microsoft Access) |
|
203 |
+B<ODBC(Microsoft Access)> |
|
40 | 204 |
|
41 | 205 |
"dbi:ODBC:driver=Microsoft Access Driver (*.mdb);dbq=hoge.mdb" |
42 | 206 |
|
43 |
-ODBC(SQL Server) |
|
207 |
+B<ODBC(SQL Server)> |
|
44 | 208 |
|
45 | 209 |
"dbi:ODBC:driver={SQL Server};Server=(local);database=test;Trusted_Connection=yes;AutoTranslate=No;" |
46 | 210 |
|
47 |
-=head2 2. Suger methods |
|
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 |
|
48 | 216 |
|
49 |
-L<DBIx::Custom> has suger methods, such as C<insert()>, C<update()>, |
|
50 |
-C<delete()> or C<select()>. If you want to do small works, |
|
51 |
-You don't have to create SQL statements. |
|
217 |
+ my $dbh = $dbi->dbh; |
|
52 | 218 |
|
53 |
-=head3 insert() |
|
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; |
|
54 | 224 |
|
55 |
-Execute insert statement. |
|
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 |
|
56 | 239 |
|
57 | 240 |
$dbi->insert(table => 'book', |
58 | 241 |
param => {title => 'Perl', author => 'Ken'}); |
59 | 242 |
|
60 |
-The following SQL is executed. |
|
61 |
- |
|
62 |
- insert into (title, author) values (?, ?); |
|
243 |
+C<table>�ɂ̓e�[�u�����AC<param>�ɂ͑}����f�[�^��w�肵�܂��B |
|
63 | 244 |
|
64 |
-The values of C<title> and C<author> is embedded into the placeholders. |
|
245 |
+����SQL�����s����܂��B |
|
65 | 246 |
|
66 |
-C<append> and C<filter> argument can be specified. |
|
67 |
-See also "METHODS" section. |
|
247 |
+ insert into (title, author) values (?, ?); |
|
68 | 248 |
|
69 |
-=head3 update() |
|
249 |
+=head3 �f�[�^�̍X�V C<update()> |
|
70 | 250 |
|
71 |
-Execute update statement. |
|
251 |
+�f�[�^�x�[�X�̃f�[�^��X�V����ɂ́AC<update()>��g�p���܂��B |
|
72 | 252 |
|
73 | 253 |
$dbi->update(table => 'book', |
74 | 254 |
param => {title => 'Perl', author => 'Ken'}, |
75 | 255 |
where => {id => 5}); |
76 | 256 |
|
77 |
-The following SQL is executed. |
|
257 |
+C<table>�ɂ̓e�[�u�����AC<param>�ɂ͑}����f�[�^�AC<where>�ɂ� |
|
258 |
+���w�肵�܂��B |
|
78 | 259 |
|
79 |
- update book set title = ?, author = ?; |
|
260 |
+����SQL�����s����܂��B |
|
80 | 261 |
|
81 |
-The values of C<title> and C<author> is embedded into the placeholders. |
|
262 |
+ update book set title = ?, author = ?; |
|
82 | 263 |
|
83 |
-C<append> and C<filter> argument can be specified. |
|
84 |
-See also "METHOD" section. |
|
264 |
+C<update>���\�b�h�͈�S�̂��� |
|
265 |
+where��̂Ȃ�SQL�s���邱�Ƃ���Ă��܂���B |
|
266 |
+�����ׂĂ̍s��X�V�������ꍇ�� |
|
267 |
+C<update_all()>��g�p�����������B |
|
85 | 268 |
|
86 |
-If you want to update all rows, use C<update_all()> method. |
|
269 |
+ $dbi->update_all(table => 'book', |
|
270 |
+ param => {title => 'Perl', author => 'Ken'}); |
|
87 | 271 |
|
88 |
-=head3 delete() |
|
272 |
+=head3 �f�[�^�̍폜 C<delete()> |
|
89 | 273 |
|
90 |
-Execute delete statement. |
|
274 |
+�f�[�^�x�[�X�̃f�[�^��1���폜����ɂ́AC<delete()>��g�p���܂��B |
|
91 | 275 |
|
92 | 276 |
$dbi->delete(table => 'book', |
93 | 277 |
where => {author => 'Ken'}); |
94 | 278 |
|
95 |
-The following SQL is executed. |
|
279 |
+C<table>�ɂ̓e�[�u�����AC<where>�ɂ͏��w�肵�܂��B |
|
96 | 280 |
|
97 |
- delete from book where id = ?; |
|
281 |
+����SQL�����s����܂��B |
|
98 | 282 |
|
99 |
-The value of C<id> is embedded into the placehodler. |
|
283 |
+ delete from book where id = ?; |
|
100 | 284 |
|
101 |
-C<append> and C<filter> argument can be specified. |
|
102 |
-see also "METHODS" section. |
|
285 |
+C<delete>���\�b�h�͈�S�̂��� |
|
286 |
+where��̂Ȃ�SQL�s���邱�Ƃ���Ă��܂���B |
|
287 |
+�����ׂĂ̍s��폜�������ꍇ�� |
|
288 |
+C<delete_all()>��g�p�����������B |
|
103 | 289 |
|
104 |
-If you want to delete all rows, use C<delete_all()> method. |
|
290 |
+ $dbi->delete_all(table => 'book'); |
|
105 | 291 |
|
106 |
-=head3 select() |
|
292 |
+=head3 �f�[�^�̑I�� C<select()> |
|
107 | 293 |
|
108 |
-Execute select statement, only C<table> argument specified : |
|
294 |
+�s��I���ɂ�C<select()>��g�p���܂��B |
|
109 | 295 |
|
110 | 296 |
my $result = $dbi->select(table => 'book'); |
111 | 297 |
|
112 |
-The following SQL is executed. |
|
298 |
+C<table>������w�肵�āA���̏��w�肵�Ȃ��ꍇ�͎���SQL�����s����܂��B |
|
113 | 299 |
|
114 | 300 |
select * from book; |
115 | 301 |
|
116 |
-the result of C<select()> method is L<DBIx::Custom::Result> object. |
|
117 |
-You can fetch a row by C<fetch()> method. |
|
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 |
|
118 | 304 |
|
119 | 305 |
while (my $row = $result->fetch) { |
120 | 306 |
my $title = $row->[0]; |
121 | 307 |
my $author = $row->[1]; |
122 | 308 |
} |
123 | 309 |
|
124 |
-L<DBIx::Custom::Result> has various methods to fetch row. |
|
125 |
-See "3. Fetch row". |
|
310 |
+L<DBIx::Custom::Result>�ɂ��Ă͂��̌�L<3. �s�̃t�F�b�`/"3. �s�̃t�F�b�`">�ŏڂ��������܂��B |
|
126 | 311 |
|
127 |
-C<column> and C<where> arguments specified. |
|
312 |
+���܂��܂�C<select()>�̎g�����Ă����܂��傤�B |
|
313 |
+����C<select>�͍s�̖��O��where���w�肵����̂ł��B |
|
128 | 314 |
|
129 | 315 |
my $result = $dbi->select( |
130 | 316 |
table => 'book', |
131 |
- column => [qw/author title/], |
|
317 |
+ column => ['author', 'title'], |
|
132 | 318 |
where => {author => 'Ken'} |
133 | 319 |
); |
134 | 320 |
|
135 |
-The following SQL is executed. |
|
321 |
+C<column>�ɂ͗�AC<where>�ɂ͏��w�肷�邱�Ƃ��ł��܂��B |
|
322 |
+����SQL�����s����܂��B |
|
136 | 323 |
|
137 | 324 |
select author, title from book where author = ?; |
138 | 325 |
|
139 |
-the value of C<author> is embdded into the placeholder. |
|
140 |
- |
|
141 |
-If you want to join tables, specify C<relation> argument. |
|
326 |
+�e�[�u������������ꍇ�͂�C<relation>�Ƀe�[�u���� |
|
327 |
+�W��L�q���܂��B |
|
142 | 328 |
|
143 | 329 |
my $result = $dbi->select( |
144 | 330 |
table => ['book', 'rental'], |
145 |
- column => ['book.name as book_name'] |
|
331 |
+ where => {book.name => 'Perl'}, |
|
146 | 332 |
relation => {'book.id' => 'rental.book_id'} |
147 | 333 |
); |
148 | 334 |
|
149 |
-The following SQL is executed. |
|
335 |
+book�e�[�u����id���rental�e�[�u����book_id���֘A�t�����܂��B |
|
336 |
+����SQL�����s����܂��B |
|
150 | 337 |
|
151 |
- select book.name as book_name from book, rental |
|
152 |
- where book.id = rental.book_id; |
|
338 |
+ select * from book, rental where book.name = ? and book.id = rental.book_id; |
|
153 | 339 |
|
154 |
-If you want to add some string to the end of SQL statement, |
|
155 |
-use C<append> argument. |
|
340 |
+SQL���̖���ɕ������lj�������ꍇ��<append>��g�p���܂��B |
|
156 | 341 |
|
157 | 342 |
my $result = $dbi->select( |
158 | 343 |
table => 'book', |
159 | 344 |
where => {author => 'Ken'}, |
160 |
- append => 'order by price limit 5', |
|
345 |
+ append => 'for update', |
|
161 | 346 |
); |
162 | 347 |
|
163 |
-The following SQL is executed. |
|
348 |
+����SQL�����s����܂��B |
|
164 | 349 |
|
165 |
- select * book where author = ? order by price limit 5; |
|
350 |
+ select * book where author = ? for update; |
|
166 | 351 |
|
167 |
-C<filter> argument can be specified. |
|
168 |
-see also "METHODS" section. |
|
352 |
+�܂�C<append>�́AC<select>�����łȂ�C<insert()>�AC<update()>�AC<update_all()> |
|
353 |
+C<delete()>�AC<delete_all()>�AC<select()>�Ŏg�p���邱�Ƃ�ł��܂��B |
|
169 | 354 |
|
170 |
-=head2 3. Result manipulation |
|
355 |
+=head3 SQL�̎�s C<execute()> |
|
171 | 356 |
|
172 |
-C<select()> method return L<DBIx::Custom::Result> object. |
|
173 |
-You can fetch row by various methods. |
|
174 |
-Note that in this section, array means array reference, |
|
175 |
-and hash meanse hash reference. |
|
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 |
|
176 | 398 |
|
177 |
-Fetch row into array. |
|
178 |
- |
|
179 | 399 |
while (my $row = $result->fetch) { |
180 |
- my $author = $row->[0]; |
|
181 |
- my $title = $row->[1]; |
|
182 |
- |
|
400 |
+ my $title = $row->[0]; |
|
401 |
+ my $author = $row->[1]; |
|
183 | 402 |
} |
184 | 403 |
|
185 |
-Fetch only a first row into array. |
|
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 |
|
186 | 410 |
|
187 | 411 |
my $row = $result->fetch_first; |
188 | 412 |
|
189 |
-Fetch multiple rows into array of array. |
|
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 |
|
190 | 416 |
|
191 |
- while (my $rows = $result->fetch_multi(5)) { |
|
192 |
- my $first_author = $rows->[0][0]; |
|
193 |
- my $first_title = $rows->[0][1]; |
|
194 |
- my $second_author = $rows->[1][0]; |
|
195 |
- my $second_value = $rows->[1][1]; |
|
196 |
- |
|
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]; |
|
197 | 428 |
} |
198 |
- |
|
199 |
-Fetch all rows into array of array. |
|
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 |
|
200 | 443 |
|
201 | 444 |
my $rows = $result->fetch_all; |
202 | 445 |
|
203 |
-Fetch row into hash. |
|
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 |
|
204 | 456 |
|
205 |
- # Fetch a row into hash |
|
206 | 457 |
while (my $row = $result->fetch_hash) { |
207 | 458 |
my $title = $row->{title}; |
208 | 459 |
my $author = $row->{author}; |
209 |
- |
|
210 | 460 |
} |
211 | 461 |
|
212 |
-Fetch only a first row into hash |
|
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 |
|
213 | 466 |
|
214 | 467 |
my $row = $result->fetch_hash_first; |
215 |
- |
|
216 |
-Fetch multiple rows into array of hash |
|
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 |
|
217 | 478 |
|
218 | 479 |
while (my $rows = $result->fetch_hash_multi(5)) { |
219 |
- my $first_title = $rows->[0]{title}; |
|
220 |
- my $first_author = $rows->[0]{author}; |
|
221 |
- my $second_title = $rows->[1]{title}; |
|
222 |
- my $second_author = $rows->[1]{author}; |
|
223 |
- |
|
480 |
+ my $title0 = $rows->[0]{title}; |
|
481 |
+ my $author0 = $rows->[0]{author}; |
|
482 |
+ my $title1 = $rows->[1]{title}; |
|
483 |
+ my $author1 = $rows->[1]{author}; |
|
224 | 484 |
} |
225 |
- |
|
226 |
-Fetch all rows into array of hash |
|
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 |
|
227 | 500 |
|
228 | 501 |
my $rows = $result->fetch_hash_all; |
229 | 502 |
|
230 |
-If you want to access statement handle of L<DBI>, use C<sth> attribute. |
|
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 |
|
231 | 514 |
|
232 | 515 |
my $sth = $result->sth; |
233 | 516 |
|
234 |
-=head2 4. Parameter binding |
|
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 |
+ ); |
|
235 | 551 |
|
236 |
-L<DBIx::Custom> provides hash parameter binding. |
|
552 |
+�o�^�����t�B���^��C<apply_filter()>�Ȃǂŗ��p���邱�Ƃ��ł��܂��B |
|
237 | 553 |
|
238 |
-At frist, I show normal parameter binding. |
|
554 |
+=head3 �t�B���^�̓K�p C<apply_filter()> |
|
239 | 555 |
|
240 |
- use DBI; |
|
241 |
- my $dbh = DBI->connect(...); |
|
242 |
- my $sth = $dbh->prepare( |
|
243 |
- "select * from book where author = ? and title like ?;" |
|
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'} |
|
244 | 561 |
); |
245 |
- $sth->execute('Ken', '%Perl%'); |
|
246 | 562 |
|
247 |
-This is very good way because database system can enable SQL caching, |
|
248 |
-and parameter is quoted automatically. this is secure. |
|
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 |
|
249 | 589 |
|
250 |
-L<DBIx::Custom> hash parameter binding system improve |
|
251 |
-normal parameter binding to use hash parameter. |
|
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 |
+ ); |
|
252 | 619 |
|
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 |
+ |
|
253 | 628 |
my $result = $dbi->execute( |
254 |
- "select * from book where {= author} and {like title};" |
|
255 |
- param => {author => 'Ken', title => '%Perl%'} |
|
629 |
+ $sql, |
|
630 |
+ param => {issue_year => '2010'}, |
|
631 |
+ filter => {issue_year => 'tp_to_year'} |
|
256 | 632 |
); |
257 | 633 |
|
258 |
-This is same as the normal way, execpt that the parameter is hash. |
|
259 |
-{= author} and {like title} is called C<tag>. |
|
260 |
-tag is expand to placeholder string internally. |
|
634 |
+�����C<filter>��g���ǂ������ł��Bissue_date�̕ϊ��ɂ��Ă�C<apply_filter()> |
|
635 |
+�œo�^���Ă���̂ł����A�V�����쐬������ł���issue_year�ɂ��ẮA |
|
636 |
+���̕ϊ���o�^����Ă��܂���B�ł��̂ŁA�ʂɃt�B���^��ݒ肵�Ă��܂��B |
|
261 | 637 |
|
262 |
- select * from book where {= author} and {like title} |
|
263 |
- -> select * from book where author = ? and title like ?; |
|
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 |
|
264 | 641 |
|
265 |
-The following tags is available. |
|
642 |
+ $result->filter(issue_year => 'year_to_tp'); |
|
266 | 643 |
|
267 |
- [TAG] [REPLACED] |
|
268 |
- {? NAME} -> ? |
|
269 |
- {= NAME} -> NAME = ? |
|
270 |
- {<> NAME} -> NAME <> ? |
|
271 |
- |
|
272 |
- {< NAME} -> NAME < ? |
|
273 |
- {> NAME} -> NAME > ? |
|
274 |
- {>= NAME} -> NAME >= ? |
|
275 |
- {<= NAME} -> NAME <= ? |
|
276 |
- |
|
277 |
- {like NAME} -> NAME like ? |
|
278 |
- {in NAME COUNT} -> NAME in [?, ?, ..] |
|
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 ...} |
|
279 | 717 |
|
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 |
+ |
|
280 | 820 |
{insert_param NAME1 NAME2} -> (NAME1, NAME2) values (?, ?) |
821 |
+ |
|
822 |
+=head3 C<update_param> |
|
823 |
+ |
|
824 |
+C<update_param>�^�O�͈ȉ��̂悤�ɓW�J����܂��B |
|
825 |
+ |
|
281 | 826 |
{update_param NAME1 NAME2} -> set NAME1 = ?, NAME2 = ? |
282 | 827 |
|
283 |
-See also L<DBIx::Custom::QueryBuilder>. |
|
828 |
+=head2 �����̗�̈��� |
|
284 | 829 |
|
285 |
-C<{> and C<}> is reserved. If you use these charactors, |
|
286 |
-you must escape them using '\'. Note that '\' is |
|
287 |
-already perl escaped charactor, so you must write '\\'. |
|
830 |
+�����̗��܂ރ^�O������ꍇ�ɂ�ASQL���s���邱�Ƃ��ł��܂��B |
|
831 |
+���Ƃ��A��̓�t�Ŕ�r���Ȃ���Ȃ�Ȃ��ꍇ�� |
|
832 |
+�l���Č��܂��傤�B |
|
288 | 833 |
|
289 |
- 'select * from book \\{ something statement \\}' |
|
834 |
+ my $sql = "select * from table where {> date} and {< date};"; |
|
290 | 835 |
|
291 |
-=head2 5. Filtering |
|
836 |
+���̂悤�ȏꍇ�͑Ή�����p�����[�^�̒l��z��̃��t�@�����X�ɂ��܂��B |
|
292 | 837 |
|
293 |
-If you want to filter the value, you can do this. For example, |
|
294 |
-L<Time::Piece> object to database date format, or reverse. |
|
838 |
+ my $dbi->execute($sql, param => {date => ['2010-10-01', '2012-02-10']}); |
|
295 | 839 |
|
296 |
- $dbi->register_filter( |
|
297 |
- tp_to_date => sub { |
|
298 |
- return shift->strftime('%Y-%m-%d'); |
|
299 |
- }, |
|
300 |
- date_to_tp => sub { |
|
301 |
- return Time::Piece->strptime(shift, '%Y-%m-%d'); |
|
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]]; |
|
302 | 850 |
} |
303 | 851 |
); |
304 | 852 |
|
305 |
-In this example, L<Time::Piece> object is converted to 'yyyy-mm-dd' format |
|
306 |
-, and reverse. |
|
853 |
+�����ł̓f�t�H���g��C<=>�^�O���ǂ̂悤�Ɏ������Ă��邩����Ă��܂��B |
|
854 |
+�^�O��o�^������̈�̓^�O�̒��ɏ����ꂽ��ɂȂ�܂��B |
|
307 | 855 |
|
308 |
-You can apply this filter to use C<apply_filter()> method. |
|
856 |
+ {�^�O�� ��1 ��2} |
|
309 | 857 |
|
310 |
- $dbi->apply_filter('book', |
|
311 |
- puplication_date => {out => 'tp_to_date', in => 'date_to_tp'}, |
|
312 |
- someting_date => {out => 'tp_to_date', in => 'date_to_tp'} |
|
313 |
- ); |
|
858 |
+C<=>�^�O�̏ꍇ�� |
|
314 | 859 |
|
315 |
-In this case, C<book>'s C<publication_date> is automatically converted. |
|
316 |
-C<out> means Perl to Database, C<in> means Database to Perl. |
|
860 |
+ {= title} |
|
317 | 861 |
|
318 |
-These applied filters have effect C<insert>, C<update>, C<update_all>, |
|
319 |
-C<delete>, C<delete_all>, C<select> |
|
862 |
+�Ƃ����`���ł�����A�T�u���[�`���ɂ�title�Ƃ����ЂƂ̗��킽��Ă��܂��B |
|
320 | 863 |
|
321 |
- my $tp = Time::Piece::localtime; |
|
322 |
- $dbi->insert( |
|
323 |
- table => 'book', |
|
324 |
- param => {name => 'Perl', publication_date => $tp} |
|
325 |
- ); |
|
326 |
- |
|
327 |
- my $result = $dbi->select(table => 'book'); |
|
328 |
- my $tp = $result->{publication_date}; |
|
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']] |
|
329 | 889 |
|
890 |
+��Ԃ��K�v������Ƃ������Ƃł��B |
|
330 | 891 |
|
331 |
-Note that this has'nt C<execute> method by default. |
|
332 |
-If you want to have effect C<execute()> method, use C<table> |
|
333 |
-option. |
|
892 |
+�^�O�̎���̑��̃T���v����L<DBIx::Custom::Tag>�̃\�[�X�R�[�h |
|
893 |
+����ɂȂ�Ă݂Ă��������B |
|
334 | 894 |
|
335 |
- my $result = $dbi->execute( |
|
336 |
- "select * from book where {= id};", |
|
337 |
- param => {id => 5}, |
|
338 |
- table => ['book'] |
|
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}'] |
|
339 | 924 |
); |
340 | 925 |
|
926 |
+clause�̎w���@�͎��̂悤�ɂȂ�܂��B |
|
927 |
+ |
|
928 |
+ ['or' ���邢�� 'and', �^�O1, �^�O2, �^�O3] |
|
341 | 929 |
|
342 |
-You can also specify registered filters to C<filter> option of |
|
343 |
-C<insert()>, C<update()>, C<update_all()>, C<delete()>, C<delete_all()>, |
|
344 |
-C<select()> C<execute()>. This is overwirte applied filter. |
|
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 |
|
345 | 942 |
|
346 |
- $dbi->insert( |
|
347 |
- table => 'book', |
|
348 |
- param => {name => 'Perl', publication_date => $tp}, |
|
349 |
- filter => {publication_date => 'tp_to_date'} |
|
350 |
- ); |
|
943 |
+ my $param => {title => 'Perl'}; |
|
944 |
+ $where->param($param); |
|
351 | 945 |
|
352 |
-You can also specify C<DBIx::Custom::Result> object. |
|
353 |
-This is overwrite applied filter. |
|
946 |
+���̗�ł�title�������p�����[�^�Ɋ܂܂�Ă��܂��B |
|
354 | 947 |
|
355 |
- my $result = $dbi->select(table => 'book'); |
|
356 |
- $result->filter(publication_date => 'date_to_tp'); |
|
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 |
|
357 | 964 |
|
358 |
-B<Filter examples> |
|
965 |
+=head3 ����̗�܂ޏꍇ |
|
359 | 966 |
|
360 |
-=head2 6.Create table object |
|
967 |
+�^�O�̒��ɓ���̖��O���̂����݂����ꍇ�ł��I�� |
|
968 |
+where���쐬���邱�Ƃ��ł��܂��B |
|
361 | 969 |
|
362 |
-You can create table object which have methods. |
|
970 |
+���Ƃ��A�p�����[�^�Ƃ��ĊJ�n��t�ƏI����t��������Ƃ� |
|
971 |
+�l���Ă݂Ă��������B |
|
363 | 972 |
|
364 |
- $dbi->table('book'); |
|
973 |
+ my $param = {start_date => '2010-11-15', end_date => '2011-11-21'}; |
|
365 | 974 |
|
366 |
-This class have C<insert()>, C<update()>, C<update_all()>, |
|
367 |
-C<delete()>, C<delete_all()>, C<select()>. |
|
368 |
-These is same as L<DBIx::Custom>'s methods except that |
|
369 |
-you don't have to specify table. |
|
975 |
+�܂��J�n��t�ƏI����t�̕Е���A�ǂ������Ȃ��ꍇ���邩����܂���B |
|
370 | 976 |
|
371 |
- $dbi->table('book')->insert( |
|
372 |
- param => {author => 'Taro', name => 'Perl'} |
|
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}'] |
|
373 | 986 |
); |
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 |
|
374 | 1028 |
|
375 |
-You can define method for table. |
|
376 | 1029 |
|
377 |
- $dbi->table('book', |
|
378 |
- insert_multi => sub { |
|
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 { |
|
379 | 1074 |
my $self = shift; |
380 |
- my $table = $self->name; |
|
381 |
- my $dbi = $self->dbi; |
|
382 |
- |
|
383 |
- # Do something |
|
1075 |
+ my $table_name = $self->name; |
|
1076 |
+ # something |
|
384 | 1077 |
}, |
385 |
- cross_summary => sub { |
|
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 { |
|
386 | 1097 |
my $self = shift; |
387 |
- my $table = $self->name; |
|
388 |
- my $dbi = $self->dbi; |
|
389 | 1098 |
|
390 |
- # Do something |
|
1099 |
+ $self->base_insert(...); |
|
1100 |
+ |
|
1101 |
+ # something |
|
391 | 1102 |
} |
392 | 1103 |
); |
393 | 1104 |
|
394 |
-Each method receive L<DBIx::Custom::Table> object as first argument. |
|
395 |
-This class have C<name()> to get table name and C<dbi()> |
|
396 |
-to get L<DBIx::Custom> object. |
|
1105 |
+��Ƃ�Ƒ��݂��Ă���C<insert()>��ĂԂɂ�C<base_$method>�Ƃ��܂��BL<DBIx::Custom::Table> |
|
1106 |
+�̃I�[�o�[���C�h�̋@�\�͊ȈՓI�Ȃ�̂ł����A�ƂĂ�֗��ł��B |
|
397 | 1107 |
|
398 |
-Defined method is called from table class. |
|
1108 |
+=head2 �e�[�u���ŋ��L�̃��\�b�h�̓o�^ |
|
399 | 1109 |
|
400 |
- $dbi->table('book')->insert_multi(param => $param); |
|
1110 |
+���ׂẴe�[�u���Ń��\�b�h��L����ɂ�C<table>���\�b�h�Ńe�[�u����쐬����O�ɁA |
|
1111 |
+C<base_table>�Ƀ��\�b�h��o�^���Ă����܂��B |
|
401 | 1112 |
|
402 |
-=head2 7. Get high performance |
|
1113 |
+ $dbi->base_table->method( |
|
1114 |
+ count => sub { |
|
1115 |
+ my $self = shift; |
|
1116 |
+ return $self->select(column => ['count(*)']); |
|
1117 |
+ } |
|
1118 |
+ ); |
|
403 | 1119 |
|
404 |
-=head3 Use execute() method instead suger methods |
|
1120 |
+�܂��e�[�u�������L<DBIx::Custom>��L<DBI>�̂��ׂẴ��\�b�h��Ăяo�����Ƃ��ł��܂��B |
|
405 | 1121 |
|
406 |
-If you execute insert statement by C<insert()> method, |
|
407 |
-you sometimes can't get required performance. |
|
1122 |
+ # DBIx::Custom method |
|
1123 |
+ $table->execute($sql); |
|
1124 |
+ |
|
1125 |
+ # DBI method |
|
1126 |
+ $table->begin_work; |
|
1127 |
+ $table->commit; |
|
1128 |
+ |
|
1129 |
+=head2 ��ʓI�ȃ��f���̍\�� |
|
408 | 1130 |
|
409 |
-C<insert()> method is a little slow because SQL statement and statement handle |
|
410 |
-is created every time. |
|
1131 |
+��ʓI�ɂ́AL<DBIx::Custom>��p�����ăR���X�g���N�^�̒��ɁA���f����쐬 |
|
1132 |
+����̂��悢�ł��傤�B |
|
411 | 1133 |
|
412 |
-In that case, you can prepare a query by C<create_query()> method. |
|
1134 |
+ package MyDBI; |
|
1135 |
+ |
|
1136 |
+ use base 'DBIx::Custom'; |
|
413 | 1137 |
|
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 |
+ |
|
414 | 1177 |
my $query = $dbi->create_query( |
415 |
- "insert into book {insert_param title author};" |
|
1178 |
+ "insert into book {insert_param title author};"; |
|
416 | 1179 |
); |
417 | 1180 |
|
418 |
-Return value of C<create_query()> is L<DBIx::Custom::Query> object. |
|
419 |
-This keep the information of SQL and column names. |
|
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 |
|
420 | 1184 |
|
421 | 1185 |
{ |
422 | 1186 |
sql => 'insert into book (title, author) values (?, ?);', |
423 |
- columns => ['title', 'author'] |
|
1187 |
+ columns => ['title', 'author'], |
|
1188 |
+ sth => $sth |
|
424 | 1189 |
} |
425 | 1190 |
|
426 |
-Execute query repeatedly. |
|
1191 |
+�N�G���I�u�W�F�N�g��g��ČJ��Ԃ���s����ɂ�C<execute()>��g�p���܂��B |
|
427 | 1192 |
|
428 | 1193 |
my $params = [ |
429 | 1194 |
{title => 'Perl', author => 'Ken'}, |
430 | 1195 |
{title => 'Good days', author => 'Mike'} |
431 | 1196 |
]; |
432 | 1197 |
|
433 |
- foreach my $param (@$params) { |
|
434 |
- $dbi->execute($query, $param); |
|
1198 |
+ foreach my $param (@$paramss) { |
|
1199 |
+ $dbi->execute($query, table => 'book', param => $input); |
|
435 | 1200 |
} |
436 | 1201 |
|
437 |
-This is faster than C<insert()> method. |
|
1202 |
+C<execute>���\�b�h�̑���ɃN�G���I�u�W�F�g��n�����Ƃ��ł��܂��B |
|
1203 |
+C<insert()>���\�b�h�������ł��B |
|
438 | 1204 |
|
439 |
-=head2 8. More features |
|
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 |
|
440 | 1212 |
|
441 |
-=head3 Get DBI object |
|
1213 |
+=head2 9. ���̑��̋@�\ |
|
442 | 1214 |
|
443 |
-You can get L<DBI> object and call any method of L<DBI>. |
|
1215 |
+=head3 ���\�b�h�̓o�^ |
|
444 | 1216 |
|
445 |
- $dbi->dbh->begin_work; |
|
446 |
- $dbi->dbh->commit; |
|
447 |
- $dbi->dbh->rollback; |
|
1217 |
+���\�b�h��o�^����ɂ�C<method()>��g�p���܂��B |
|
448 | 1218 |
|
449 |
-=head3 Change Result class |
|
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 |
|
450 | 1237 |
|
451 |
-You can change Result class if you need. |
|
1238 |
+�K�v�Ȃ�Ό��ʃN���X��ύX���邱�Ƃ��ł��܂��B |
|
452 | 1239 |
|
453 |
- package Your::Result; |
|
1240 |
+ package MyResult; |
|
454 | 1241 |
use base 'DBIx::Custom::Result'; |
455 | 1242 |
|
456 | 1243 |
sub some_method { ... } |
... | ... |
@@ -459,39 +1246,50 @@ You can change Result class if you need. |
459 | 1246 |
|
460 | 1247 |
package main; |
461 | 1248 |
|
462 |
- use Your::Result; |
|
1249 |
+ use MyResult; |
|
463 | 1250 |
|
464 | 1251 |
my $dbi = DBIx::Custom->connect(...); |
465 |
- $dbi->result_class('Your::Result'); |
|
1252 |
+ $dbi->result_class('MyResult'); |
|
466 | 1253 |
|
467 |
-=head3 Register tag |
|
1254 |
+=head3 �L���b�V���O |
|
468 | 1255 |
|
469 |
- $dbi->register_tag( |
|
470 |
- name => sub { |
|
471 |
- ... |
|
472 |
- } |
|
473 |
- ); |
|
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 |
|
474 | 1258 |
|
475 |
-=head3 Resister method method |
|
1259 |
+ $dbi->cache(1); |
|
476 | 1260 |
|
477 |
-You can resiter method method. |
|
1261 |
+�L���b�V����@��C<cache_method>�Ƀ��\�b�h��w�肷�邱�Ƃ� |
|
1262 |
+�ύX���邱�Ƃ��ł��܂��B |
|
1263 |
+�f�[�^�̕ۑ��Ǝ擾�̂��߂̃��\�b�h���`���܂��B |
|
478 | 1264 |
|
479 |
- $dbi->method( |
|
480 |
- update_or_insert => sub { |
|
481 |
- my $self = shift; |
|
482 |
- # do something |
|
483 |
- }, |
|
484 |
- find_or_create => sub { |
|
1265 |
+�f�t�H���g�ł͎��̂悤�Ƀ�������ɃL���b�V����s����̂ɂȂ�Ă��܂��B |
|
1266 |
+ |
|
1267 |
+ $dbi->cache_method(sub { |
|
1268 |
+ sub { |
|
485 | 1269 |
my $self = shift; |
486 |
- # do something |
|
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 |
+ } |
|
487 | 1281 |
} |
488 |
- ); |
|
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 |
|
489 | 1287 |
|
490 |
-Register method methods. |
|
491 |
-These method can be called from L<DBIx::Custom> object directory. |
|
1288 |
+�����ō쐬����ꍇ�͑�O�����݂����ꍇ�̓L���b�V����ݒ肵�A |
|
1289 |
+���݂��Ȃ�����ꍇ�̓L���b�V����擾�������� |
|
1290 |
+�����������B |
|
492 | 1291 |
|
493 |
- $dbi->update_or_insert; |
|
494 |
- $dbi->find_or_create; |
|
1292 |
+=cut |
|
495 | 1293 |
|
496 | 1294 |
=head1 EXAMPLES |
497 | 1295 |
|
... | ... |
@@ -2,12 +2,12 @@ |
2 | 2 |
|
3 | 3 |
=head1 NAME |
4 | 4 |
|
5 |
-DBIx::Custom::Guide::Ja - DBIx::Customの日本語ガイド |
|
5 |
+DBIx::Custom::Guide::Ja - DBIx::Customのガイドブック |
|
6 | 6 |
|
7 | 7 |
=head1 ガイド |
8 | 8 |
|
9 |
-L<DBIx::Custom>はデータベースへのクエリの発行を簡単に行うための |
|
10 |
-クラスです。L<DBIx::Class>やL<DBIx::Simple>と同じように |
|
9 |
+L<DBIx::Custom>はSQLの実行を簡単に行うためのクラスです。 |
|
10 |
+L<DBIx::Class>やL<DBIx::Simple>と同じように |
|
11 | 11 |
L<DBI>のラッパクラスになっています。L<DBIx::Class>よりも簡単に、 |
12 | 12 |
L<DBIx::Simple>よりもはるかに柔軟なことを行うことができます。 |
13 | 13 |
|
... | ... |
@@ -23,7 +23,7 @@ L<DBIx::Custom>の主な目的は、SQLを尊重しつつ、L<DBI>だけでは |
23 | 23 |
多くの知識を持っているならば、L<DBIx::Custom>でそのまま |
24 | 24 |
活用することができます。 |
25 | 25 |
|
26 |
-L<DBIx::Custom>の仕組みを簡単に説明しておきましょう。 |
|
26 |
+L<DBIx::Custom>の仕組みを少しだけ説明しておきます。 |
|
27 | 27 |
L<DBIx::Custom>では、タグと呼ばれるものを |
28 | 28 |
SQLの中に埋め込むことができます。 |
29 | 29 |
|
... | ... |
@@ -35,14 +35,14 @@ SQLの中に埋め込むことができます。 |
35 | 35 |
select * from book where title = ? and author = ?; |
36 | 36 |
|
37 | 37 |
これらの展開にはどのような意味があるのでしょうかと質問 |
38 |
-されることかと思います。この簡単な仕組みの上に非常にたくさんの |
|
39 |
-有用で便利で使いやすい機能が構築されます。それは以下のようなものです。 |
|
38 |
+されるかもしれません。この簡単な仕組みの上に |
|
39 |
+便利な機能が実装されます。それは以下のようなものです。 |
|
40 | 40 |
|
41 | 41 |
=over 4 |
42 | 42 |
|
43 |
-=item 1. プレースホルダのパラメータをハッシュリファレンスで指定 |
|
43 |
+=item 1. プレースホルダにバインドする値をハッシュリファレンスで指定 |
|
44 | 44 |
|
45 |
-L<DBI>をそのまま使うのであればプレースホルダのパラメータは配列 |
|
45 |
+L<DBI>を使うのであればプレースホルダにバインドする値は配列 |
|
46 | 46 |
で指定する必要があります。 |
47 | 47 |
|
48 | 48 |
$sth->execute(@bind); |
... | ... |
@@ -53,8 +53,9 @@ L<DBIx::Custom>を利用するのであればハッシュリファレンスで |
53 | 53 |
my $param = {title => 'Perl', author => 'Ken'}; |
54 | 54 |
$dbi->execute($sql, $param); |
55 | 55 |
|
56 |
-=item 2. パラメータのフィルタリング |
|
56 |
+=item 2. 値のフィルタリング |
|
57 | 57 |
|
58 |
+L<DBIx::Custom>はフィルタリングの機能を提供します。 |
|
58 | 59 |
たとえば、日付の列は、Perlで扱うときにはC<Time::Piece>などの日付オブジェクト |
59 | 60 |
で扱い、データベースに格納するときはデータベースの日付型に変換したい |
60 | 61 |
と思うのではないでしょうか。またデータベースから取り出すときは |
... | ... |
@@ -82,13 +83,13 @@ L<DBIx::Custom>を利用するのであればハッシュリファレンスで |
82 | 83 |
|
83 | 84 |
outはPerlからデータベースに保存する方向、inはデータベースからPerlに取得する方向です。 |
84 | 85 |
|
85 |
-SQLを発行するときにテーブルの指定を行えば、自動的にこのフィルタが適用されます。 |
|
86 |
+多くのメソッドで自動的にこのフィルタが有効になります。 |
|
86 | 87 |
|
87 |
- $dbi->execute($sql, $param, table => 'book'); |
|
88 |
+ $dbi->insert(table => 'book', param => {issue_date => $tp}); |
|
88 | 89 |
|
89 | 90 |
=item 3. 選択的な検索条件 |
90 | 91 |
|
91 |
-生のDBIを利用しているとき一番たいへんなのは選択的な検索条件を作成したいときです。 |
|
92 |
+L<DBI>では選択的に検索条件を作成することは難しいです。 |
|
92 | 93 |
|
93 | 94 |
たとえば、検索条件にtitleとauthorが指定された場合は次のSQLを |
94 | 95 |
|
... | ... |
@@ -107,12 +108,18 @@ authorだけの場合は次のSQLを発行した場合を考えましょう。 |
107 | 108 |
|
108 | 109 |
L<DBIx::Custom>はさらに簡単で便利な方法を用意しています。 |
109 | 110 |
|
111 |
+ # Whereオブジェクト |
|
110 | 112 |
my $where = $dbi->where; |
111 |
- $where->param({title => 'Perl'}); |
|
113 |
+ |
|
114 |
+ # 検索条件 |
|
112 | 115 |
$where->clause( |
113 | 116 |
['and', '{= title}', {'= author'}] |
114 | 117 |
); |
118 |
+ |
|
119 |
+ # 必要な列を自動的に選択するための設定 |
|
120 |
+ $where->param({title => 'Perl'}); |
|
115 | 121 |
|
122 |
+ # SQLへのWhere句の埋め込み |
|
116 | 123 |
my $sql = "select * from book $where"; |
117 | 124 |
|
118 | 125 |
詳しい説明は後ほど行いますが、上記のように記述すれば、 |
... | ... |
@@ -1036,7 +1043,126 @@ C<execute()>との連携です。SQLを作成するときに埋め込むこと |
1036 | 1043 |
|
1037 | 1044 |
$dbi->execute($sql, param => $param); |
1038 | 1045 |
|
1039 |
-=head2 7. パフォーマンスの改善 |
|
1046 |
+=head2 7. テーブルモデル |
|
1047 |
+ |
|
1048 |
+=head3 テーブルオブジェクトの作成 C<table()> |
|
1049 |
+ |
|
1050 |
+L<DBIx::Custom>はロジックとしてテーブルを中心にすえた |
|
1051 |
+モデルの作成を支援します。 |
|
1052 |
+ |
|
1053 |
+アプリケーションのロジックを記述するときに、そのロジックを |
|
1054 |
+データベースのテーブルにすえることは、RDBMSを利用する |
|
1055 |
+モデルであれば、コードの重複も少なく |
|
1056 |
+わかりやすいものになります。 |
|
1057 |
+ |
|
1058 |
+テーブルオブジェクトを生成するにはC<table()>を使用します。 |
|
1059 |
+ |
|
1060 |
+ my $table = $dbi->table('book'); |
|
1061 |
+ |
|
1062 |
+実際にデータベースにテーブルは存在している必要はありません。 |
|
1063 |
+これは仮想的なテーブルオブジェクトです。これは |
|
1064 |
+L<DBIx::Customm::Table>オブジェクトになります。 |
|
1065 |
+ |
|
1066 |
+テーブルオブジェクトからはC<insert()>、C<update()>、C<update_all()>、 |
|
1067 |
+C<delete()>、C<delete_all()>、C<select()>などのメソッド呼び出すことができます。 |
|
1068 |
+L<DBIx::Custom>と異なるところは、C<table>を必ずしも指定する必要が |
|
1069 |
+ないということです。 |
|
1070 |
+ |
|
1071 |
+ $table->insert(param => $param); |
|
1072 |
+ |
|
1073 |
+C<table>の値は自動的にbookに設定されます。 |
|
1074 |
+ |
|
1075 |
+またテーブルオブジェクトには独自のメソッドを追加することができます。 |
|
1076 |
+ |
|
1077 |
+ $table->method( |
|
1078 |
+ register => sub { |
|
1079 |
+ my $self = shift; |
|
1080 |
+ my $table_name = $self->name; |
|
1081 |
+ # something |
|
1082 |
+ }, |
|
1083 |
+ list => sub { |
|
1084 |
+ my $self = shift; |
|
1085 |
+ my $table_name = $self->name; |
|
1086 |
+ # something |
|
1087 |
+ } |
|
1088 |
+ ); |
|
1089 |
+ |
|
1090 |
+メソッドに渡される第一引数はL<DBIx::Custom::Table>オブジェクトです。 |
|
1091 |
+C<name()>を使用して、テーブル名を取得することができます。 |
|
1092 |
+ |
|
1093 |
+このようにして登録したメソッドは直接呼び出すことができます。 |
|
1094 |
+ |
|
1095 |
+ $table->register(...); |
|
1096 |
+ $table->list(...); |
|
1097 |
+ |
|
1098 |
+またテーブル専用のメソッドをオーバーライドして作成することもできます。 |
|
1099 |
+ |
|
1100 |
+ $table->method( |
|
1101 |
+ insert => sub { |
|
1102 |
+ my $self = shift; |
|
1103 |
+ |
|
1104 |
+ $self->base_insert(...); |
|
1105 |
+ |
|
1106 |
+ # something |
|
1107 |
+ } |
|
1108 |
+ ); |
|
1109 |
+ |
|
1110 |
+もともと存在していたC<insert()>を呼ぶにはC<base_$method>とします。L<DBIx::Custom::Table> |
|
1111 |
+のオーバーライドの機能は簡易的なものですが、とても便利です。 |
|
1112 |
+ |
|
1113 |
+=head2 テーブルで共有のメソッドの登録 |
|
1114 |
+ |
|
1115 |
+すべてのテーブルでメソッドを共有するにはC<table>メソッドでテーブルを作成する前に、 |
|
1116 |
+C<base_table>にメソッドを登録しておきます。 |
|
1117 |
+ |
|
1118 |
+ $dbi->base_table->method( |
|
1119 |
+ count => sub { |
|
1120 |
+ my $self = shift; |
|
1121 |
+ return $self->select(column => ['count(*)']); |
|
1122 |
+ } |
|
1123 |
+ ); |
|
1124 |
+ |
|
1125 |
+またテーブルからはL<DBIx::Custom>とL<DBI>のすべてのメソッドを呼び出すことができます。 |
|
1126 |
+ |
|
1127 |
+ # DBIx::Custom method |
|
1128 |
+ $table->execute($sql); |
|
1129 |
+ |
|
1130 |
+ # DBI method |
|
1131 |
+ $table->begin_work; |
|
1132 |
+ $table->commit; |
|
1133 |
+ |
|
1134 |
+=head2 一般的なモデルの構成 |
|
1135 |
+ |
|
1136 |
+一般的には、L<DBIx::Custom>を継承してコンストラクタの中に、モデルを作成 |
|
1137 |
+するのがよいでしょう。 |
|
1138 |
+ |
|
1139 |
+ package MyDBI; |
|
1140 |
+ |
|
1141 |
+ use base 'DBIx::Custom'; |
|
1142 |
+ |
|
1143 |
+ sub connect { |
|
1144 |
+ my $self = shift->SUPER::connect(@_); |
|
1145 |
+ |
|
1146 |
+ $self->base_table->method( |
|
1147 |
+ ... => sub { ... } |
|
1148 |
+ ); |
|
1149 |
+ |
|
1150 |
+ $self->table('book')->method( |
|
1151 |
+ insert_multi => sub { ... }, |
|
1152 |
+ ... => sub { ... } |
|
1153 |
+ ); |
|
1154 |
+ |
|
1155 |
+ $self->table('company')->method( |
|
1156 |
+ ... => sub { ... }, |
|
1157 |
+ ); |
|
1158 |
+ } |
|
1159 |
+ |
|
1160 |
+このようにして定義しておけば、次のように利用することができます。 |
|
1161 |
+ |
|
1162 |
+ my $dbi = MyDBI->connect(...); |
|
1163 |
+ $dbi->table('book')->insert_multi(...); |
|
1164 |
+ |
|
1165 |
+=head2 8. パフォーマンスの改善 |
|
1040 | 1166 |
|
1041 | 1167 |
=head3 クエリの作成 |
1042 | 1168 |
|
... | ... |
@@ -1089,7 +1215,7 @@ C<insert()>メソッドよりも高速です。 |
1089 | 1215 |
C<table>を指定する必要のあることに注意してください。 |
1090 | 1216 |
本当に必要な場合だけ利用してください。 |
1091 | 1217 |
|
1092 |
-=head2 8. その他の機能 |
|
1218 |
+=head2 9. その他の機能 |
|
1093 | 1219 |
|
1094 | 1220 |
=head3 メソッドの登録 |
1095 | 1221 |
|