8 use DBIx::Class::Optional::Dependencies ();
11 use DBIC::DebugObj ();
12 use DBIC::SqlMakerTest;
14 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MSACCESS_ODBC_${_}" } qw/DSN USER PASS/};
15 my ($dsn2, $user2, $pass2) = @ENV{map { "DBICTEST_MSACCESS_ADO_${_}" } qw/DSN USER PASS/};
17 plan skip_all => 'Test needs ' .
18 (join ' or ', map { $_ ? $_ : () }
19 DBIx::Class::Optional::Dependencies->req_missing_for('test_rdbms_msaccess_odbc'),
20 DBIx::Class::Optional::Dependencies->req_missing_for('test_rdbms_msaccess_ado'))
22 $dsn && DBIx::Class::Optional::Dependencies->req_ok_for('test_rdbms_msaccess_odbc')
24 $dsn2 && DBIx::Class::Optional::Dependencies->req_ok_for('test_rdbms_msaccess_ado')
28 DBICTest::Schema->load_classes('ArtistGUID');
30 # Example DSNs (32bit only):
31 # dbi:ODBC:driver={Microsoft Access Driver (*.mdb, *.accdb)};dbq=C:\Users\rkitover\Documents\access_sample.accdb
32 # dbi:ADO:Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\rkitover\Documents\access_sample.accdb
33 # dbi:ADO:Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\rkitover\Documents\access_sample.accdb;Persist Security Info=False'
35 plan skip_all => <<'EOF' unless $dsn || $dsn2;
36 Set $ENV{DBICTEST_MSACCESS_ODBC_DSN} and/or $ENV{DBICTEST_MSACCESS_ADO_DSN} (and optionally _USER and _PASS) to run these tests.
37 Warning: this test drops and creates the tables 'artist', 'cd', 'bindtype_test' and 'artist_guid'.
41 [ $dsn, $user || '', $pass || '' ],
42 [ $dsn2, $user2 || '', $pass2 || '' ],
47 foreach my $info (@info) {
48 my ($dsn, $user, $pass) = @$info;
52 # Check that we can connect without any options.
53 $schema = DBICTest::Schema->connect($dsn, $user, $pass);
55 $schema->storage->ensure_connected;
56 } 'connection without any options';
58 my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
59 $binstr{'large'} = $binstr{'small'} x 1024;
61 my $maxloblen = length $binstr{'large'};
63 $schema = DBICTest::Schema->connect($dsn, $user, $pass, {
66 LongReadLen => $maxloblen,
69 my $guard = Scope::Guard->new(\&cleanup);
71 my $dbh = $schema->storage->dbh;
73 # turn off warnings for OLE exception from ADO about nonexistant table
74 eval { local $^W = 0; $dbh->do("DROP TABLE artist") };
78 artistid AUTOINCREMENT PRIMARY KEY,
79 name VARCHAR(255) NULL,
80 charfield CHAR(10) NULL,
85 my $ars = $schema->resultset('Artist');
86 is ( $ars->count, 0, 'No rows at first' );
88 # test primary key handling
89 my $new = $ars->create({ name => 'foo' });
90 ok($new->artistid, "Auto-PK worked");
92 my $first_artistid = $new->artistid;
94 # test explicit key spec
95 $new = $ars->create ({ name => 'bar', artistid => 66 });
96 is($new->artistid, 66, 'Explicit PK worked');
97 $new->discard_changes;
98 is($new->artistid, 66, 'Explicit PK assigned');
101 eval { local $^W = 0; $dbh->do("DROP TABLE cd") };
105 cdid AUTOINCREMENT PRIMARY KEY,
107 title VARCHAR(255) NULL,
109 genreid INTEGER NULL,
110 single_track INTEGER NULL
116 trackid AUTOINCREMENT PRIMARY KEY,
117 cd INTEGER REFERENCES cd(cdid),
120 last_updated_on DATETIME,
121 last_updated_at DATETIME
125 my $cd = $schema->resultset('CD')->create({
126 artist => $first_artistid,
127 title => 'Some Album',
131 my $joined_artist = $schema->resultset('Artist')->search({
132 artistid => $first_artistid,
135 '+select' => [ 'cds.title' ],
136 '+as' => [ 'cd_title' ],
139 is $joined_artist->get_column('cd_title'), 'Some Album',
140 'one-step join works';
143 my $track = $schema->resultset('Track')->create({
151 my $joined_track = try {
152 local $schema->storage->{debug} = 1;
153 local $schema->storage->{debugobj} = DBIC::DebugObj->new(\$sql, \@bind);
155 $schema->resultset('Artist')->search({
156 artistid => $first_artistid,
158 join => [{ cds => 'tracks' }],
159 '+select' => [ 'tracks.title' ],
160 '+as' => [ 'track_title' ],
164 diag "Could not execute two-step left join: $_";
167 s/^'//, s/'\z// for @bind;
172 'SELECT [me].[artistid], [me].[name], [me].[rank], [me].[charfield], [tracks].[title] FROM ( ( [artist] [me] LEFT JOIN cd [cds] ON [cds].[artist] = [me].[artistid] ) LEFT JOIN [track] [tracks] ON [tracks].[cd] = [cds].[cdid] ) WHERE ( [artistid] = ? )',
174 'correct SQL for two-step left join',
177 is try { $joined_track->get_column('track_title') }, 'my track',
178 'two-step left join works';
182 $joined_artist = try {
183 local $schema->storage->{debug} = 1;
184 local $schema->storage->{debugobj} = DBIC::DebugObj->new(\$sql, \@bind);
186 $schema->resultset('Track')->search({
187 trackid => $track->trackid,
189 join => [{ cd => 'artist' }],
190 '+select' => [ 'artist.name' ],
191 '+as' => [ 'artist_name' ],
195 diag "Could not execute two-step inner join: $_";
198 s/^'//, s/'\z// for @bind;
203 'SELECT [me].[trackid], [me].[cd], [me].[position], [me].[title], [me].[last_updated_on], [me].[last_updated_at], [artist].[name] FROM ( ( [track] [me] INNER JOIN cd [cd] ON [cd].[cdid] = [me].[cd] ) INNER JOIN [artist] [artist] ON [artist].[artistid] = [cd].[artist] ) WHERE ( [trackid] = ? )',
205 'correct SQL for two-step inner join',
208 is try { $joined_artist->get_column('artist_name') }, 'foo',
209 'two-step inner join works';
211 # test basic transactions
212 $schema->txn_do(sub {
213 $ars->create({ name => 'transaction_commit' });
215 ok($ars->search({ name => 'transaction_commit' })->first,
216 'transaction committed');
217 $ars->search({ name => 'transaction_commit' })->delete,
219 $schema->txn_do(sub {
220 $ars->create({ name => 'transaction_rollback' });
223 } qr/rolling back/, 'rollback executed';
224 is $ars->search({ name => 'transaction_rollback' })->first, undef,
225 'transaction rolled back';
227 # test two-phase commit and inner transaction rollback from nested transactions
228 $schema->txn_do(sub {
229 $ars->create({ name => 'in_outer_transaction' });
230 $schema->txn_do(sub {
231 $ars->create({ name => 'in_inner_transaction' });
233 ok($ars->search({ name => 'in_inner_transaction' })->first,
234 'commit from inner transaction visible in outer transaction');
236 $schema->txn_do(sub {
237 $ars->create({ name => 'in_inner_transaction_rolling_back' });
238 die 'rolling back inner transaction';
240 } qr/rolling back inner transaction/, 'inner transaction rollback executed';
242 ok($ars->search({ name => 'in_outer_transaction' })->first,
243 'commit from outer transaction');
244 ok($ars->search({ name => 'in_inner_transaction' })->first,
245 'commit from inner transaction');
246 is $ars->search({ name => 'in_inner_transaction_rolling_back' })->first,
248 'rollback from inner transaction';
249 $ars->search({ name => 'in_outer_transaction' })->delete;
250 $ars->search({ name => 'in_inner_transaction' })->delete;
256 push @pop, { name => "Artist_$_" };
258 $ars->populate (\@pop);
261 # test populate with explicit key
265 push @pop, { name => "Artist_expkey_$_", artistid => 100 + $_ };
267 $ars->populate (\@pop);
270 # count what we did so far
271 is ($ars->count, 6, 'Simple count works');
274 # not testing offset because access only supports TOP
275 my $lim = $ars->search( {},
279 order_by => 'artistid'
282 is( $lim->count, 2, 'ROWS+OFFSET count ok' );
283 is( $lim->all, 2, 'Number of ->all objects matches count' );
287 is( $lim->next->artistid, 1, "iterator->next ok" );
288 is( $lim->next->artistid, 66, "iterator->next ok" );
289 is( $lim->next, undef, "next past end of resultset ok" );
292 my $current_artistid = $ars->search({}, {
293 select => [ { max => 'artistid' } ], as => ['artistid']
297 lives_ok { $row = $ars->create({}) }
298 'empty insert works';
300 $row->discard_changes;
302 is $row->artistid, $current_artistid+1,
303 'empty insert generated correct PK';
305 # test that autoinc column still works after empty insert
306 $row = $ars->create({ name => 'after_empty_insert' });
308 is $row->artistid, $current_artistid+2,
309 'autoincrement column functional aftear empty insert';
311 # test blobs (stolen from 73oracle.t)
313 # turn off horrendous binary DBIC_TRACE output
315 local $schema->storage->{debug} = 0;
317 eval { local $^W = 0; $dbh->do('DROP TABLE bindtype_test') };
319 CREATE TABLE bindtype_test
321 id INT NOT NULL PRIMARY KEY,
327 ],{ RaiseError => 1, PrintError => 1 });
329 my $rs = $schema->resultset('BindType');
332 foreach my $type (qw( blob clob a_memo )) {
333 foreach my $size (qw( small large )) {
335 skip 'TEXT columns not cast to MEMO over ODBC', 2
336 if $type eq 'clob' && $size eq 'large' && $dsn =~ /:ODBC:/;
340 lives_ok { $rs->create( { 'id' => $id, $type => $binstr{$size} } ) }
341 "inserted $size $type without dying" or next;
343 my $from_db = eval { $rs->find($id)->$type } || '';
346 ok($from_db eq $binstr{$size}, "verified inserted $size $type" )
349 join '', map sprintf('%02X', ord), split //, shift
351 diag 'Got: ', "\n", substr($hexdump->($from_db),0,255), '...',
352 substr($hexdump->($from_db),-255);
353 diag 'Size: ', length($from_db);
354 diag 'Expected Size: ', length($binstr{$size});
355 diag 'Expected: ', "\n",
356 substr($hexdump->($binstr{$size}), 0, 255),
357 "...", substr($hexdump->($binstr{$size}),-255);
364 $rs->search({ id => 0 })->update({ blob => $binstr{small} });
365 } 'updated IMAGE to small binstr without dying';
368 $rs->search({ id => 0 })->update({ blob => $binstr{large} });
369 } 'updated IMAGE to large binstr without dying';
372 # test GUIDs (and the cursor GUID fixup stuff for ADO)
375 $schema->storage->new_guid(sub { Data::GUID->new->as_string });
377 local $schema->source('ArtistGUID')->column_info('artistid')->{data_type}
380 local $schema->source('ArtistGUID')->column_info('a_guid')->{data_type}
383 $schema->storage->dbh_do (sub {
384 my ($storage, $dbh) = @_;
385 eval { local $^W = 0; $dbh->do("DROP TABLE artist_guid") };
387 CREATE TABLE artist_guid (
388 artistid GUID NOT NULL,
391 charfield CHAR(10) NULL,
393 primary key(artistid)
399 $row = $schema->resultset('ArtistGUID')->create({ name => 'mtfnpy' })
400 } 'created a row with a GUID';
403 eval { $row->artistid },
404 'row has GUID PK col populated',
409 eval { $row->a_guid },
410 'row has a GUID col with auto_nextval populated',
414 my $row_from_db = $schema->resultset('ArtistGUID')
415 ->search({ name => 'mtfnpy' })->first;
417 is $row_from_db->artistid, $row->artistid,
418 'PK GUID round trip (via ->search->next)';
420 is $row_from_db->a_guid, $row->a_guid,
421 'NON-PK GUID round trip (via ->search->next)';
423 $row_from_db = $schema->resultset('ArtistGUID')
424 ->find($row->artistid);
426 is $row_from_db->artistid, $row->artistid,
427 'PK GUID round trip (via ->find)';
429 is $row_from_db->a_guid, $row->a_guid,
430 'NON-PK GUID round trip (via ->find)';
432 ($row_from_db) = $schema->resultset('ArtistGUID')
433 ->search({ name => 'mtfnpy' })->all;
435 is $row_from_db->artistid, $row->artistid,
436 'PK GUID round trip (via ->search->all)';
438 is $row_from_db->a_guid, $row->a_guid,
439 'NON-PK GUID round trip (via ->search->all)';
445 if (my $storage = eval { $schema->storage }) {
446 # cannot drop a table if it has been used, have to reconnect first
447 $schema->storage->disconnect;
448 local $^W = 0; # for ADO OLE exceptions
449 $schema->storage->dbh->do("DROP TABLE $_")
450 for qw/artist track cd bindtype_test artist_guid/;