8 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MSSQL_ODBC_${_}" } qw/DSN USER PASS/};
10 plan skip_all => 'Set $ENV{DBICTEST_MSSQL_ODBC_DSN}, _USER and _PASS to run this test'
11 unless ($dsn && $user);
15 my $schema = DBICTest::Schema->connect($dsn, $user, $pass, {AutoCommit => 1});
18 no warnings 'redefine';
19 my $connect_count = 0;
20 my $orig_connect = \&DBI::connect;
21 local *DBI::connect = sub { $connect_count++; goto &$orig_connect };
23 $schema->storage->ensure_connected;
25 is( $connect_count, 1, 'only one connection made');
28 isa_ok( $schema->storage, 'DBIx::Class::Storage::DBI::ODBC::Microsoft_SQL_Server' );
30 $schema->storage->dbh_do (sub {
31 my ($storage, $dbh) = @_;
32 eval { $dbh->do("DROP TABLE artist") };
36 artistid INT IDENTITY NOT NULL,
38 rank INT NOT NULL DEFAULT '13',
39 charfield CHAR(10) NULL,
49 # fresh $schema so we start unconnected
50 $schema = DBICTest::Schema->connect($dsn, $user, $pass, {AutoCommit => 1});
52 # test primary key handling
53 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
54 ok($new->artistid > 0, "Auto-PK worked");
56 $seen_id{$new->artistid}++;
60 $new = $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
61 is ( $seen_id{$new->artistid}, undef, "id for Artist $_ is unique" );
62 $seen_id{$new->artistid}++;
65 my $it = $schema->resultset('Artist')->search( {}, {
67 order_by => 'artistid',
70 is( $it->count, 3, "LIMIT count ok" );
71 is( $it->next->name, "foo", "iterator->next ok" );
73 is( $it->next->name, "Artist 2", "iterator->next ok" );
74 is( $it->next, undef, "next past end of resultset ok" );
76 $schema->storage->dbh_do (sub {
77 my ($storage, $dbh) = @_;
78 eval { $dbh->do("DROP TABLE Owners") };
79 eval { $dbh->do("DROP TABLE Books") };
84 id INT IDENTITY (1, 1) NOT NULL,
92 id INT IDENTITY (1, 1) NOT NULL,
96 SET IDENTITY_INSERT Owners ON
101 $schema->populate ('Owners', [
108 $schema->populate ('BooksInLibrary', [
109 [qw/source owner title /],
110 [qw/Library 1 secrets1/],
111 [qw/Eatery 1 secrets2/],
112 [qw/Library 2 secrets3/],
116 # try a distinct + prefetch on tables with identically named columns
120 # try a ->has_many direction (due to a 'multi' accessor the select/group_by group is collapsed)
121 my $owners = $schema->resultset ('Owners')->search (
122 { 'books.id' => { '!=', undef }},
123 { prefetch => 'books', distinct => 1 }
125 my $owners2 = $schema->resultset ('Owners')->search ({ id => { -in => $owners->get_column ('me.id')->as_query }});
126 for ($owners, $owners2) {
127 is ($_->all, 2, 'Prefetched grouped search returns correct number of rows');
128 is ($_->count, 2, 'Prefetched grouped search returns correct count');
131 # try a ->belongs_to direction (no select collapse)
132 my $books = $schema->resultset ('BooksInLibrary')->search (
133 { 'owner.name' => 'wiggle' },
134 { prefetch => 'owner', distinct => 1 }
136 my $books2 = $schema->resultset ('BooksInLibrary')->search ({ id => { -in => $books->get_column ('me.id')->as_query }});
137 for ($books, $books2) {
138 is ($_->all, 1, 'Prefetched grouped search returns correct number of rows');
139 is ($_->count, 1, 'Prefetched grouped search returns correct count');
145 my $dbh = eval { $schema->storage->_dbh };
146 $dbh->do('DROP TABLE artist') if $dbh;