8 use DBIx::Class::Optional::Dependencies ();
12 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MSACCESS_ODBC_${_}" } qw/DSN USER PASS/};
13 my ($dsn2, $user2, $pass2) = @ENV{map { "DBICTEST_MSACCESS_ADO_${_}" } qw/DSN USER PASS/};
15 plan skip_all => 'Test needs ' .
16 (join ' or ', map { $_ ? $_ : () }
17 DBIx::Class::Optional::Dependencies->req_missing_for('test_rdbms_msaccess_odbc'),
18 DBIx::Class::Optional::Dependencies->req_missing_for('test_rdbms_msaccess_ado'))
20 $dsn && DBIx::Class::Optional::Dependencies->req_ok_for('test_rdbms_msaccess_odbc')
22 $dsn2 && DBIx::Class::Optional::Dependencies->req_ok_for('test_rdbms_msaccess_ado')
26 require DBICTest::Schema;
27 DBICTest::Schema->load_classes('ArtistGUID');
29 # Example DSNs (32bit only):
30 # dbi:ODBC:driver={Microsoft Access Driver (*.mdb, *.accdb)};dbq=C:\Users\rkitover\Documents\access_sample.accdb
31 # dbi:ADO:Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\rkitover\Documents\access_sample.accdb
32 # dbi:ADO:Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\rkitover\Documents\access_sample.accdb;Persist Security Info=False'
34 plan skip_all => <<'EOF' unless $dsn || $dsn2;
35 Set $ENV{DBICTEST_MSACCESS_ODBC_DSN} and/or $ENV{DBICTEST_MSACCESS_ADO_DSN} (and optionally _USER and _PASS) to run these tests.
36 Warning: this test drops and creates the tables 'artist', 'cd', 'bindtype_test' and 'artist_guid'.
40 [ $dsn, $user || '', $pass || '' ],
41 [ $dsn2, $user2 || '', $pass2 || '' ],
44 foreach my $info (@info) {
45 my ($dsn, $user, $pass) = @$info;
49 # Check that we can connect without any options.
50 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
52 $schema->storage->ensure_connected;
53 } 'connection without any options';
55 my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
56 $binstr{'large'} = $binstr{'small'} x 1024;
58 my $maxloblen = length $binstr{'large'};
60 $schema = DBICTest::Schema->connect($dsn, $user, $pass, {
63 LongReadLen => $maxloblen,
66 my $guard = Scope::Guard->new(sub { cleanup($schema) });
68 my $dbh = $schema->storage->dbh;
70 # turn off warnings for OLE exception from ADO about nonexistant table
71 eval { local $^W = 0; $dbh->do("DROP TABLE artist") };
75 artistid AUTOINCREMENT PRIMARY KEY,
76 name VARCHAR(255) NULL,
77 charfield CHAR(10) NULL,
82 my $ars = $schema->resultset('Artist');
83 is ( $ars->count, 0, 'No rows at first' );
85 # test primary key handling
86 my $new = $ars->create({ name => 'foo' });
87 ok($new->artistid, "Auto-PK worked");
89 my $first_artistid = $new->artistid;
91 # test explicit key spec
92 $new = $ars->create ({ name => 'bar', artistid => 66 });
93 is($new->artistid, 66, 'Explicit PK worked');
94 $new->discard_changes;
95 is($new->artistid, 66, 'Explicit PK assigned');
98 eval { local $^W = 0; $dbh->do("DROP TABLE cd") };
102 cdid AUTOINCREMENT PRIMARY KEY,
104 title VARCHAR(255) NULL,
106 genreid INTEGER NULL,
107 single_track INTEGER NULL
113 trackid AUTOINCREMENT PRIMARY KEY,
114 cd INTEGER REFERENCES cd(cdid),
117 last_updated_on DATETIME,
118 last_updated_at DATETIME
122 my $cd = $schema->resultset('CD')->create({
123 artist => $first_artistid,
124 title => 'Some Album',
128 my $joined_artist = $schema->resultset('Artist')->search({
129 artistid => $first_artistid,
132 '+select' => [ 'cds.title' ],
133 '+as' => [ 'cd_title' ],
136 is $joined_artist->get_column('cd_title'), 'Some Album',
137 'one-step join works';
140 my $track = $schema->resultset('Track')->create({
146 my $joined_track = try {
147 $schema->resultset('Artist')->search({
148 artistid => $first_artistid,
150 join => [{ cds => 'tracks' }],
151 '+select' => [ 'tracks.title' ],
152 '+as' => [ 'track_title' ],
156 diag "Could not execute two-step left join: $_";
159 is try { $joined_track->get_column('track_title') }, 'my track',
160 'two-step left join works';
162 $joined_artist = try {
163 $schema->resultset('Track')->search({
164 trackid => $track->trackid,
166 join => [{ cd => 'artist' }],
167 '+select' => [ 'artist.name' ],
168 '+as' => [ 'artist_name' ],
172 diag "Could not execute two-step inner join: $_";
175 is try { $joined_artist->get_column('artist_name') }, 'foo',
176 'two-step inner join works';
178 # test basic transactions
179 $schema->txn_do(sub {
180 $ars->create({ name => 'transaction_commit' });
182 ok($ars->search({ name => 'transaction_commit' })->first,
183 'transaction committed');
184 $ars->search({ name => 'transaction_commit' })->delete,
186 $schema->txn_do(sub {
187 $ars->create({ name => 'transaction_rollback' });
190 } qr/rolling back/, 'rollback executed';
191 is $ars->search({ name => 'transaction_rollback' })->first, undef,
192 'transaction rolled back';
194 # test two-phase commit and inner transaction rollback from nested transactions
195 $schema->txn_do(sub {
196 $ars->create({ name => 'in_outer_transaction' });
197 $schema->txn_do(sub {
198 $ars->create({ name => 'in_inner_transaction' });
200 ok($ars->search({ name => 'in_inner_transaction' })->first,
201 'commit from inner transaction visible in outer transaction');
203 $schema->txn_do(sub {
204 $ars->create({ name => 'in_inner_transaction_rolling_back' });
205 die 'rolling back inner transaction';
207 } qr/rolling back inner transaction/, 'inner transaction rollback executed';
209 ok($ars->search({ name => 'in_outer_transaction' })->first,
210 'commit from outer transaction');
211 ok($ars->search({ name => 'in_inner_transaction' })->first,
212 'commit from inner transaction');
213 is $ars->search({ name => 'in_inner_transaction_rolling_back' })->first,
215 'rollback from inner transaction';
216 $ars->search({ name => 'in_outer_transaction' })->delete;
217 $ars->search({ name => 'in_inner_transaction' })->delete;
223 push @pop, { name => "Artist_$_" };
225 $ars->populate (\@pop);
228 # test populate with explicit key
232 push @pop, { name => "Artist_expkey_$_", artistid => 100 + $_ };
234 $ars->populate (\@pop);
237 # count what we did so far
238 is ($ars->count, 6, 'Simple count works');
241 # not testing offset because access only supports TOP
242 my $lim = $ars->search( {},
246 order_by => 'artistid'
249 is( $lim->count, 2, 'ROWS+OFFSET count ok' );
250 is( $lim->all, 2, 'Number of ->all objects matches count' );
254 is( $lim->next->artistid, 1, "iterator->next ok" );
255 is( $lim->next->artistid, 66, "iterator->next ok" );
256 is( $lim->next, undef, "next past end of resultset ok" );
259 my $current_artistid = $ars->search({}, {
260 select => [ { max => 'artistid' } ], as => ['artistid']
264 lives_ok { $row = $ars->create({}) }
265 'empty insert works';
267 $row->discard_changes;
269 is $row->artistid, $current_artistid+1,
270 'empty insert generated correct PK';
272 # test that autoinc column still works after empty insert
273 $row = $ars->create({ name => 'after_empty_insert' });
275 is $row->artistid, $current_artistid+2,
276 'autoincrement column functional aftear empty insert';
278 # test blobs (stolen from 73oracle.t)
280 # turn off horrendous binary DBIC_TRACE output
282 local $schema->storage->{debug} = 0;
284 eval { local $^W = 0; $dbh->do('DROP TABLE bindtype_test') };
286 CREATE TABLE bindtype_test
288 id INT NOT NULL PRIMARY KEY,
294 ],{ RaiseError => 1, PrintError => 1 });
296 my $rs = $schema->resultset('BindType');
299 foreach my $type (qw( blob clob a_memo )) {
300 foreach my $size (qw( small large )) {
302 skip 'TEXT columns not cast to MEMO over ODBC', 2
303 if $type eq 'clob' && $size eq 'large' && $dsn =~ /:ODBC:/;
307 lives_ok { $rs->create( { 'id' => $id, $type => $binstr{$size} } ) }
308 "inserted $size $type without dying" or next;
310 my $from_db = eval { $rs->find($id)->$type } || '';
313 ok($from_db eq $binstr{$size}, "verified inserted $size $type" )
316 join '', map sprintf('%02X', ord), split //, shift
318 diag 'Got: ', "\n", substr($hexdump->($from_db),0,255), '...',
319 substr($hexdump->($from_db),-255);
320 diag 'Size: ', length($from_db);
321 diag 'Expected Size: ', length($binstr{$size});
322 diag 'Expected: ', "\n",
323 substr($hexdump->($binstr{$size}), 0, 255),
324 "...", substr($hexdump->($binstr{$size}),-255);
331 $rs->search({ id => 0 })->update({ blob => $binstr{small} });
332 } 'updated IMAGE to small binstr without dying';
335 $rs->search({ id => 0 })->update({ blob => $binstr{large} });
336 } 'updated IMAGE to large binstr without dying';
339 # test GUIDs (and the cursor GUID fixup stuff for ADO)
342 $schema->storage->new_guid(sub { Data::GUID->new->as_string });
344 local $schema->source('ArtistGUID')->column_info('artistid')->{data_type}
347 local $schema->source('ArtistGUID')->column_info('a_guid')->{data_type}
350 $schema->storage->dbh_do (sub {
351 my ($storage, $dbh) = @_;
352 eval { local $^W = 0; $dbh->do("DROP TABLE artist_guid") };
354 CREATE TABLE artist_guid (
355 artistid GUID NOT NULL,
358 charfield CHAR(10) NULL,
360 primary key(artistid)
366 $row = $schema->resultset('ArtistGUID')->create({ name => 'mtfnpy' })
367 } 'created a row with a GUID';
370 eval { $row->artistid },
371 'row has GUID PK col populated',
376 eval { $row->a_guid },
377 'row has a GUID col with auto_nextval populated',
381 my $row_from_db = $schema->resultset('ArtistGUID')
382 ->search({ name => 'mtfnpy' })->first;
384 is $row_from_db->artistid, $row->artistid,
385 'PK GUID round trip (via ->search->next)';
387 is $row_from_db->a_guid, $row->a_guid,
388 'NON-PK GUID round trip (via ->search->next)';
390 $row_from_db = $schema->resultset('ArtistGUID')
391 ->find($row->artistid);
393 is $row_from_db->artistid, $row->artistid,
394 'PK GUID round trip (via ->find)';
396 is $row_from_db->a_guid, $row->a_guid,
397 'NON-PK GUID round trip (via ->find)';
399 ($row_from_db) = $schema->resultset('ArtistGUID')
400 ->search({ name => 'mtfnpy' })->all;
402 is $row_from_db->artistid, $row->artistid,
403 'PK GUID round trip (via ->search->all)';
405 is $row_from_db->a_guid, $row->a_guid,
406 'NON-PK GUID round trip (via ->search->all)';
414 if (my $storage = eval { $schema->storage }) {
415 # cannot drop a table if it has been used, have to reconnect first
416 $schema->storage->disconnect;
417 local $^W = 0; # for ADO OLE exceptions
418 $schema->storage->dbh->do("DROP TABLE $_")
419 for qw/artist track cd bindtype_test artist_guid/;