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 DBICTest::Schema->load_classes('ArtistGUID');
28 # Example DSNs (32bit only):
29 # dbi:ODBC:driver={Microsoft Access Driver (*.mdb, *.accdb)};dbq=C:\Users\rkitover\Documents\access_sample.accdb
30 # dbi:ADO:Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\rkitover\Documents\access_sample.accdb
31 # dbi:ADO:Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\rkitover\Documents\access_sample.accdb;Persist Security Info=False'
33 plan skip_all => <<'EOF' unless $dsn || $dsn2;
34 Set $ENV{DBICTEST_MSACCESS_ODBC_DSN} and/or $ENV{DBICTEST_MSACCESS_ADO_DSN} (and optionally _USER and _PASS) to run these tests.
35 Warning: this test drops and creates the tables 'artist', 'cd', 'bindtype_test' and 'artist_guid'.
39 [ $dsn, $user || '', $pass || '' ],
40 [ $dsn2, $user2 || '', $pass2 || '' ],
43 foreach my $info (@info) {
44 my ($dsn, $user, $pass) = @$info;
48 # Check that we can connect without any options.
49 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
51 $schema->storage->ensure_connected;
52 } 'connection without any options';
54 my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
55 $binstr{'large'} = $binstr{'small'} x 1024;
57 my $maxloblen = length $binstr{'large'};
59 $schema = DBICTest::Schema->connect($dsn, $user, $pass, {
62 LongReadLen => $maxloblen,
65 my $guard = Scope::Guard->new(sub { cleanup($schema) });
67 my $dbh = $schema->storage->dbh;
69 # turn off warnings for OLE exception from ADO about nonexistant table
70 eval { local $^W = 0; $dbh->do("DROP TABLE artist") };
74 artistid AUTOINCREMENT PRIMARY KEY,
75 name VARCHAR(255) NULL,
76 charfield CHAR(10) NULL,
81 my $ars = $schema->resultset('Artist');
82 is ( $ars->count, 0, 'No rows at first' );
84 # test primary key handling
85 my $new = $ars->create({ name => 'foo' });
86 ok($new->artistid, "Auto-PK worked");
88 my $first_artistid = $new->artistid;
90 # test explicit key spec
91 $new = $ars->create ({ name => 'bar', artistid => 66 });
92 is($new->artistid, 66, 'Explicit PK worked');
93 $new->discard_changes;
94 is($new->artistid, 66, 'Explicit PK assigned');
97 eval { local $^W = 0; $dbh->do("DROP TABLE cd") };
101 cdid AUTOINCREMENT PRIMARY KEY,
103 title VARCHAR(255) NULL,
105 genreid INTEGER NULL,
106 single_track INTEGER NULL
112 trackid AUTOINCREMENT PRIMARY KEY,
113 cd INTEGER REFERENCES cd(cdid),
116 last_updated_on DATETIME,
117 last_updated_at DATETIME
121 my $cd = $schema->resultset('CD')->create({
122 artist => $first_artistid,
123 title => 'Some Album',
127 my $joined_artist = $schema->resultset('Artist')->search({
128 artistid => $first_artistid,
131 '+select' => [ 'cds.title' ],
132 '+as' => [ 'cd_title' ],
135 is $joined_artist->get_column('cd_title'), 'Some Album',
136 'one-step join works';
139 my $track = $schema->resultset('Track')->create({
145 my $joined_track = try {
146 $schema->resultset('Artist')->search({
147 artistid => $first_artistid,
149 join => [{ cds => 'tracks' }],
150 '+select' => [ 'tracks.title' ],
151 '+as' => [ 'track_title' ],
155 diag "Could not execute two-step left join: $_";
158 is try { $joined_track->get_column('track_title') }, 'my track',
159 'two-step left join works';
161 $joined_artist = try {
162 $schema->resultset('Track')->search({
163 trackid => $track->trackid,
165 join => [{ cd => 'artist' }],
166 '+select' => [ 'artist.name' ],
167 '+as' => [ 'artist_name' ],
171 diag "Could not execute two-step inner join: $_";
174 is try { $joined_artist->get_column('artist_name') }, 'foo',
175 'two-step inner join works';
177 # test basic transactions
178 $schema->txn_do(sub {
179 $ars->create({ name => 'transaction_commit' });
181 ok($ars->search({ name => 'transaction_commit' })->first,
182 'transaction committed');
183 $ars->search({ name => 'transaction_commit' })->delete,
185 $schema->txn_do(sub {
186 $ars->create({ name => 'transaction_rollback' });
189 } qr/rolling back/, 'rollback executed';
190 is $ars->search({ name => 'transaction_rollback' })->first, undef,
191 'transaction rolled back';
193 # test two-phase commit and inner transaction rollback from nested transactions
194 $schema->txn_do(sub {
195 $ars->create({ name => 'in_outer_transaction' });
196 $schema->txn_do(sub {
197 $ars->create({ name => 'in_inner_transaction' });
199 ok($ars->search({ name => 'in_inner_transaction' })->first,
200 'commit from inner transaction visible in outer transaction');
202 $schema->txn_do(sub {
203 $ars->create({ name => 'in_inner_transaction_rolling_back' });
204 die 'rolling back inner transaction';
206 } qr/rolling back inner transaction/, 'inner transaction rollback executed';
208 ok($ars->search({ name => 'in_outer_transaction' })->first,
209 'commit from outer transaction');
210 ok($ars->search({ name => 'in_inner_transaction' })->first,
211 'commit from inner transaction');
212 is $ars->search({ name => 'in_inner_transaction_rolling_back' })->first,
214 'rollback from inner transaction';
215 $ars->search({ name => 'in_outer_transaction' })->delete;
216 $ars->search({ name => 'in_inner_transaction' })->delete;
222 push @pop, { name => "Artist_$_" };
224 $ars->populate (\@pop);
227 # test populate with explicit key
231 push @pop, { name => "Artist_expkey_$_", artistid => 100 + $_ };
233 $ars->populate (\@pop);
236 # count what we did so far
237 is ($ars->count, 6, 'Simple count works');
240 # not testing offset because access only supports TOP
241 my $lim = $ars->search( {},
245 order_by => 'artistid'
248 is( $lim->count, 2, 'ROWS+OFFSET count ok' );
249 is( $lim->all, 2, 'Number of ->all objects matches count' );
253 is( $lim->next->artistid, 1, "iterator->next ok" );
254 is( $lim->next->artistid, 66, "iterator->next ok" );
255 is( $lim->next, undef, "next past end of resultset ok" );
258 my $current_artistid = $ars->search({}, {
259 select => [ { max => 'artistid' } ], as => ['artistid']
263 lives_ok { $row = $ars->create({}) }
264 'empty insert works';
266 $row->discard_changes;
268 is $row->artistid, $current_artistid+1,
269 'empty insert generated correct PK';
271 # test that autoinc column still works after empty insert
272 $row = $ars->create({ name => 'after_empty_insert' });
274 is $row->artistid, $current_artistid+2,
275 'autoincrement column functional aftear empty insert';
277 # test blobs (stolen from 73oracle.t)
279 # turn off horrendous binary DBIC_TRACE output
281 local $schema->storage->{debug} = 0;
283 eval { local $^W = 0; $dbh->do('DROP TABLE bindtype_test') };
285 CREATE TABLE bindtype_test
287 id INT NOT NULL PRIMARY KEY,
293 ],{ RaiseError => 1, PrintError => 1 });
295 my $rs = $schema->resultset('BindType');
298 foreach my $type (qw( blob clob a_memo )) {
299 foreach my $size (qw( small large )) {
301 skip 'TEXT columns not cast to MEMO over ODBC', 2
302 if $type eq 'clob' && $size eq 'large' && $dsn =~ /:ODBC:/;
306 lives_ok { $rs->create( { 'id' => $id, $type => $binstr{$size} } ) }
307 "inserted $size $type without dying" or next;
309 my $from_db = eval { $rs->find($id)->$type } || '';
312 ok($from_db eq $binstr{$size}, "verified inserted $size $type" )
315 join '', map sprintf('%02X', ord), split //, shift
317 diag 'Got: ', "\n", substr($hexdump->($from_db),0,255), '...',
318 substr($hexdump->($from_db),-255);
319 diag 'Size: ', length($from_db);
320 diag 'Expected Size: ', length($binstr{$size});
321 diag 'Expected: ', "\n",
322 substr($hexdump->($binstr{$size}), 0, 255),
323 "...", substr($hexdump->($binstr{$size}),-255);
330 $rs->search({ id => 0 })->update({ blob => $binstr{small} });
331 } 'updated IMAGE to small binstr without dying';
334 $rs->search({ id => 0 })->update({ blob => $binstr{large} });
335 } 'updated IMAGE to large binstr without dying';
338 # test GUIDs (and the cursor GUID fixup stuff for ADO)
341 $schema->storage->new_guid(sub { Data::GUID->new->as_string });
343 local $schema->source('ArtistGUID')->column_info('artistid')->{data_type}
346 local $schema->source('ArtistGUID')->column_info('a_guid')->{data_type}
349 $schema->storage->dbh_do (sub {
350 my ($storage, $dbh) = @_;
351 eval { local $^W = 0; $dbh->do("DROP TABLE artist_guid") };
353 CREATE TABLE artist_guid (
354 artistid GUID NOT NULL,
357 charfield CHAR(10) NULL,
359 primary key(artistid)
365 $row = $schema->resultset('ArtistGUID')->create({ name => 'mtfnpy' })
366 } 'created a row with a GUID';
369 eval { $row->artistid },
370 'row has GUID PK col populated',
375 eval { $row->a_guid },
376 'row has a GUID col with auto_nextval populated',
380 my $row_from_db = $schema->resultset('ArtistGUID')
381 ->search({ name => 'mtfnpy' })->first;
383 is $row_from_db->artistid, $row->artistid,
384 'PK GUID round trip (via ->search->next)';
386 is $row_from_db->a_guid, $row->a_guid,
387 'NON-PK GUID round trip (via ->search->next)';
389 $row_from_db = $schema->resultset('ArtistGUID')
390 ->find($row->artistid);
392 is $row_from_db->artistid, $row->artistid,
393 'PK GUID round trip (via ->find)';
395 is $row_from_db->a_guid, $row->a_guid,
396 'NON-PK GUID round trip (via ->find)';
398 ($row_from_db) = $schema->resultset('ArtistGUID')
399 ->search({ name => 'mtfnpy' })->all;
401 is $row_from_db->artistid, $row->artistid,
402 'PK GUID round trip (via ->search->all)';
404 is $row_from_db->a_guid, $row->a_guid,
405 'NON-PK GUID round trip (via ->search->all)';
413 if (my $storage = eval { $schema->storage }) {
414 # cannot drop a table if it has been used, have to reconnect first
415 $schema->storage->disconnect;
416 local $^W = 0; # for ADO OLE exceptions
417 $schema->storage->dbh->do("DROP TABLE $_")
418 for qw/artist track cd bindtype_test artist_guid/;