68ac37a8db089606461a560f07ae9819dea2051c
Perl/DBIx-NamedParams.md
... | ... | @@ -13,8 +13,8 @@ |
13 | 13 | - 「:名前-型」でスカラー値を bind する。 |
14 | 14 | ```perl |
15 | 15 | my $sql_insert = qq{ |
16 | - INSERT INTO `Users` ( `ID`, `Name`, `Status`, `RegDate` ) |
|
17 | - VALUES ( NULL, :Name-VARCHAR, :State-INTEGER, :RegDate-DATETIME ); |
|
16 | + INSERT INTO `Users` ( `ID`, `Name`, `Status`, `RegDate` ) |
|
17 | + VALUES ( NULL, :Name-VARCHAR, :State-INTEGER, :RegDate-DATETIME ); |
|
18 | 18 | }; |
19 | 19 | $sth_insert = $dbh->prepare_ex( $sql_insert ) or die( "$DBI::errstr\n" ); |
20 | 20 | $sth_insert->bind_param_ex( { 'Name' => 'リオ', 'State' => 1, 'RegDate' => '2011-02-21 10:00' } ); |
... | ... | @@ -25,9 +25,9 @@ $sth_insert->execute() or die( "$DBI::errstr\n" ); |
25 | 25 | - 「:名前{個数}-型」で配列値を個数個分 bind する。 |
26 | 26 | ```perl |
27 | 27 | my $sql_select = qq{ |
28 | - SELECT `ID`, `Name`, `Status`, `RegDate` |
|
29 | - FROM `Users` |
|
30 | - WHERE `Status` in ( :State{4}-INTEGER ); |
|
28 | + SELECT `ID`, `Name`, `Status`, `RegDate` |
|
29 | + FROM `Users` |
|
30 | + WHERE `Status` in ( :State{4}-INTEGER ); |
|
31 | 31 | }; |
32 | 32 | $sth_select = $dbh->prepare_ex( $sql_select ) or die( "$DBI::errstr\n" ); |
33 | 33 | $sth_select->bind_param_ex( { 'State' => [ 1,2,4,8 ] } ); |
... | ... | @@ -39,12 +39,12 @@ $sth_select->execute() or die( "$DBI::errstr\n" ); |
39 | 39 | prepare_exの引数としてSQL文と共に割り当てるハッシュを指定する必要がある。 |
40 | 40 | ```perl |
41 | 41 | my $sql_select = qq{ |
42 | - SELECT `ID`, `Name`, `Status`, `RegDate` |
|
43 | - FROM `Users` |
|
44 | - WHERE `Status` in ( :State+-INTEGER ); |
|
42 | + SELECT `ID`, `Name`, `Status`, `RegDate` |
|
43 | + FROM `Users` |
|
44 | + WHERE `Status` in ( :State+-INTEGER ); |
|
45 | 45 | }; |
46 | 46 | $sth_select = $dbh->prepare_ex( $sql_select, { 'State' => [ 1,2,4,8 ] } ) |
47 | - or die( "$DBI::errstr\n" ); |
|
47 | + or die( "$DBI::errstr\n" ); |
|
48 | 48 | $sth_select->execute() or die( "$DBI::errstr\n" ); |
49 | 49 | ``` |
50 | 50 | |
... | ... | @@ -116,14 +116,14 @@ my $input = $path . 'InputData.yml'; |
116 | 116 | |
117 | 117 | my $DB_Info = YAML::Syck::LoadFile( $yaml ) or die( "$yaml: $!\n" ); |
118 | 118 | foreach( keys( %{$DB_Info} ) ){ |
119 | - $DB_Info->{'DSN'} =~ s/_${_}_/$DB_Info->{$_}/; |
|
119 | + $DB_Info->{'DSN'} =~ s/_${_}_/$DB_Info->{$_}/; |
|
120 | 120 | } |
121 | 121 | |
122 | 122 | my $dbh = DBI->connect( |
123 | - 'DBI:' . $DB_Info->{'DSN'}, |
|
124 | - $DB_Info->{'User'}, |
|
125 | - $DB_Info->{'Password'}, |
|
126 | - $DB_Info->{'Options'} |
|
123 | + 'DBI:' . $DB_Info->{'DSN'}, |
|
124 | + $DB_Info->{'User'}, |
|
125 | + $DB_Info->{'Password'}, |
|
126 | + $DB_Info->{'Options'} |
|
127 | 127 | ) or die( "$DBI::errstr\n" ); |
128 | 128 | |
129 | 129 | #DBIx::NamedParams::debug_log( 'testNamedParams.log' ); |
... | ... | @@ -132,11 +132,11 @@ my $dbh = DBI->connect( |
132 | 132 | #print Dump( \%DrvTypeToSQLType ); |
133 | 133 | |
134 | 134 | my $sql_insert = qq{ |
135 | - INSERT INTO |
|
136 | - `Users` |
|
137 | - ( `Name`, `Status`, `RegDate` ) |
|
138 | - VALUES |
|
139 | - ( :Name-VARCHAR, :State-INTEGER, :Now-DATETIME ); |
|
135 | + INSERT INTO |
|
136 | + `Users` |
|
137 | + ( `Name`, `Status`, `RegDate` ) |
|
138 | + VALUES |
|
139 | + ( :Name-VARCHAR, :State-INTEGER, :Now-DATETIME ); |
|
140 | 140 | }; |
141 | 141 | |
142 | 142 | my $sth_insert = $dbh->prepare_ex( $sql_insert ) or die( "$DBI::errstr\n" ); |
... | ... | @@ -146,31 +146,31 @@ my $users = YAML::Syck::LoadFile( $input ) or die( "$input: $!\n" ); |
146 | 146 | #exit; |
147 | 147 | |
148 | 148 | foreach( @{$users} ){ |
149 | - $_->{'Now'} = strftime( "%Y-%m-%d %H:%M:%S", localtime ); |
|
150 | - print Dump( $_ ); |
|
151 | - $sth_insert->bind_param_ex( $_ ); |
|
152 | - $sth_insert->execute() or die( "$DBI::errstr\n" ); |
|
153 | - sleep( 1 ); |
|
149 | + $_->{'Now'} = strftime( "%Y-%m-%d %H:%M:%S", localtime ); |
|
150 | + print Dump( $_ ); |
|
151 | + $sth_insert->bind_param_ex( $_ ); |
|
152 | + $sth_insert->execute() or die( "$DBI::errstr\n" ); |
|
153 | + sleep( 1 ); |
|
154 | 154 | } |
155 | 155 | $sth_insert->finish; |
156 | 156 | |
157 | 157 | my $sql_select = qq{ |
158 | - SELECT |
|
159 | - `ID`, `Name`, `Status`, `RegDate` |
|
160 | - FROM |
|
161 | - `Users` |
|
162 | - WHERE |
|
163 | - `Status` in ( :State+-INTEGER ); |
|
158 | + SELECT |
|
159 | + `ID`, `Name`, `Status`, `RegDate` |
|
160 | + FROM |
|
161 | + `Users` |
|
162 | + WHERE |
|
163 | + `Status` in ( :State+-INTEGER ); |
|
164 | 164 | }; |
165 | 165 | |
166 | 166 | my $sth_select = $dbh->prepare_ex( $sql_select, { 'State' => [ 1,2,5 ] } ) |
167 | - or die( "$DBI::errstr\n" ); |
|
167 | + or die( "$DBI::errstr\n" ); |
|
168 | 168 | $sth_select->execute() or die( "$DBI::errstr\n" ); |
169 | 169 | do { |
170 | - no warnings 'uninitialized'; |
|
171 | - while( my @a = $sth_select->fetchrow_array ){ |
|
172 | - printf( "%s\n", join( "\t", @a ) ); |
|
173 | - } |
|
170 | + no warnings 'uninitialized'; |
|
171 | + while( my @a = $sth_select->fetchrow_array ){ |
|
172 | + printf( "%s\n", join( "\t", @a ) ); |
|
173 | + } |
|
174 | 174 | }while( $sth_select->{odbc_more_results} ); |
175 | 175 | $sth_select->finish; |
176 | 176 | |
... | ... | @@ -251,14 +251,14 @@ my $input = $path . 'InputData.yml'; |
251 | 251 | |
252 | 252 | my $DB_Info = YAML::Syck::LoadFile( $yaml ) or die( "$yaml: $!\n" ); |
253 | 253 | foreach( keys( %{$DB_Info} ) ){ |
254 | - $DB_Info->{'DSN'} =~ s/_${_}_/$DB_Info->{$_}/; |
|
254 | + $DB_Info->{'DSN'} =~ s/_${_}_/$DB_Info->{$_}/; |
|
255 | 255 | } |
256 | 256 | |
257 | 257 | my $dbh = DBI->connect( |
258 | - 'DBI:' . $DB_Info->{'DSN'}, |
|
259 | - $DB_Info->{'User'}, |
|
260 | - $DB_Info->{'Password'}, |
|
261 | - $DB_Info->{'Options'} |
|
258 | + 'DBI:' . $DB_Info->{'DSN'}, |
|
259 | + $DB_Info->{'User'}, |
|
260 | + $DB_Info->{'Password'}, |
|
261 | + $DB_Info->{'Options'} |
|
262 | 262 | ) or die( "$DBI::errstr\n" ); |
263 | 263 | |
264 | 264 | #DBIx::NamedParams::debug_log( 'testNamedParams.log' ); |
... | ... | @@ -267,11 +267,11 @@ my $dbh = DBI->connect( |
267 | 267 | #print Dump( \%DrvTypeToSQLType ); |
268 | 268 | |
269 | 269 | my $sql_insert = qq{ |
270 | - INSERT INTO |
|
271 | - [Users] |
|
272 | - ( [Name], [Status], [RegDate] ) |
|
273 | - VALUES |
|
274 | - ( :Name-WVARCHAR, :State-INTEGER, :Now-WVARCHAR ); |
|
270 | + INSERT INTO |
|
271 | + [Users] |
|
272 | + ( [Name], [Status], [RegDate] ) |
|
273 | + VALUES |
|
274 | + ( :Name-WVARCHAR, :State-INTEGER, :Now-WVARCHAR ); |
|
275 | 275 | }; |
276 | 276 | |
277 | 277 | my $sth_insert = $dbh->prepare_ex( $sql_insert ) or die( "$DBI::errstr\n" ); |
... | ... | @@ -281,31 +281,31 @@ my $users = YAML::Syck::LoadFile( $input ) or die( "$input: $!\n" ); |
281 | 281 | #exit; |
282 | 282 | |
283 | 283 | foreach( @{$users} ){ |
284 | - $_->{'Now'} = strftime( "%Y-%m-%d %H:%M:%S", localtime ); |
|
285 | - print Dump( $_ ); |
|
286 | - $sth_insert->bind_param_ex( $_ ); |
|
287 | - $sth_insert->execute() or die( "$DBI::errstr\n" ); |
|
288 | - sleep( 1 ); |
|
284 | + $_->{'Now'} = strftime( "%Y-%m-%d %H:%M:%S", localtime ); |
|
285 | + print Dump( $_ ); |
|
286 | + $sth_insert->bind_param_ex( $_ ); |
|
287 | + $sth_insert->execute() or die( "$DBI::errstr\n" ); |
|
288 | + sleep( 1 ); |
|
289 | 289 | } |
290 | 290 | $sth_insert->finish; |
291 | 291 | |
292 | 292 | my $sql_select = qq{ |
293 | - SELECT |
|
294 | - [ID], [Name], [Status], [RegDate] |
|
295 | - FROM |
|
296 | - [Users] |
|
297 | - WHERE |
|
298 | - [Status] in ( :State+-INTEGER ); |
|
293 | + SELECT |
|
294 | + [ID], [Name], [Status], [RegDate] |
|
295 | + FROM |
|
296 | + [Users] |
|
297 | + WHERE |
|
298 | + [Status] in ( :State+-INTEGER ); |
|
299 | 299 | }; |
300 | 300 | |
301 | 301 | my $sth_select = $dbh->prepare_ex( $sql_select, { 'State' => [ 1,2,5 ] } ) |
302 | - or die( "$DBI::errstr\n" ); |
|
302 | + or die( "$DBI::errstr\n" ); |
|
303 | 303 | $sth_select->execute() or die( "$DBI::errstr\n" ); |
304 | 304 | do { |
305 | - no warnings 'uninitialized'; |
|
306 | - while( my @a = $sth_select->fetchrow_array ){ |
|
307 | - printf( "%s\n", join( "\t", @a ) ); |
|
308 | - } |
|
305 | + no warnings 'uninitialized'; |
|
306 | + while( my @a = $sth_select->fetchrow_array ){ |
|
307 | + printf( "%s\n", join( "\t", @a ) ); |
|
308 | + } |
|
309 | 309 | }while( $sth_select->{odbc_more_results} ); |
310 | 310 | $sth_select->finish; |
311 | 311 | |
... | ... | @@ -342,20 +342,20 @@ SET QUOTED_IDENTIFIER ON |
342 | 342 | GO |
343 | 343 | |
344 | 344 | CREATE TABLE [dbo].[Users]( |
345 | - [ID] [int] IDENTITY(1,1) NOT NULL, |
|
346 | - [Name] [nvarchar](40) NOT NULL, |
|
347 | - [Status] [int] NOT NULL, |
|
348 | - [RegDate] [datetime] NULL, |
|
349 | - CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED |
|
350 | - ( |
|
351 | - [ID] ASC |
|
352 | - ) WITH ( |
|
353 | - PAD_INDEX = OFF, |
|
354 | - STATISTICS_NORECOMPUTE = OFF, |
|
355 | - IGNORE_DUP_KEY = OFF, |
|
356 | - ALLOW_ROW_LOCKS = ON, |
|
357 | - ALLOW_PAGE_LOCKS = ON |
|
358 | - ) ON [PRIMARY] |
|
345 | + [ID] [int] IDENTITY(1,1) NOT NULL, |
|
346 | + [Name] [nvarchar](40) NOT NULL, |
|
347 | + [Status] [int] NOT NULL, |
|
348 | + [RegDate] [datetime] NULL, |
|
349 | + CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED |
|
350 | + ( |
|
351 | + [ID] ASC |
|
352 | + ) WITH ( |
|
353 | + PAD_INDEX = OFF, |
|
354 | + STATISTICS_NORECOMPUTE = OFF, |
|
355 | + IGNORE_DUP_KEY = OFF, |
|
356 | + ALLOW_ROW_LOCKS = ON, |
|
357 | + ALLOW_PAGE_LOCKS = ON |
|
358 | + ) ON [PRIMARY] |
|
359 | 359 | ) ON [PRIMARY] |
360 | 360 | GO |
361 | 361 | |
... | ... | @@ -620,3 +620,8 @@ xml: WLONGVARCHAR |
620 | 620 | - [CPAN:perl](http://search.cpan.org/dist/perl) POSIX strftime |
621 | 621 | |
622 | 622 | - [CPAN:FindBin-libs](http://search.cpan.org/dist/FindBin-libs) |
623 | + |
|
624 | +- [Perl Hackers Hub|gihyo.jp … 技術評論社](https://gihyo.jp/dev/serial/01/perl-hackers-hub) |
|
625 | + - [第50回 Minillaを使ったモダンなCPANモジュール開発(1)](http://gihyo.jp/dev/serial/01/perl-hackers-hub/005001) |
|
626 | + - [第50回 Minillaを使ったモダンなCPANモジュール開発(2)](https://gihyo.jp/dev/serial/01/perl-hackers-hub/005002) |
|
627 | + - [第50回 Minillaを使ったモダンなCPANモジュール開発(3)](http://gihyo.jp/dev/serial/01/perl-hackers-hub/005003) |