1use strict;
2use warnings;
3
4use Test::More;
5use Test::Exception;
6use lib qw(t/lib);
7use DBICTest;
8
9my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MSSQL_ADO_${_}" } qw/DSN USER PASS/};
10
11plan skip_all => 'Set $ENV{DBICTEST_MSSQL_ADO_DSN}, _USER and _PASS to run this test'
12  unless ($dsn && $user);
13
14my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
15$schema->storage->ensure_connected;
16
17isa_ok( $schema->storage, 'DBIx::Class::Storage::DBI::ADO::Microsoft_SQL_Server' );
18
19$schema->storage->dbh_do (sub {
20    my ($storage, $dbh) = @_;
21    eval { $dbh->do("DROP TABLE artist") };
22    $dbh->do(<<'SQL');
23CREATE TABLE artist (
24   artistid INT IDENTITY NOT NULL,
25   name VARCHAR(100),
26   rank INT NOT NULL DEFAULT '13',
27   charfield CHAR(10) NULL,
28   primary key(artistid)
29)
30SQL
31});
32
33my $new = $schema->resultset('Artist')->create({ name => 'foo' });
34ok($new->artistid > 0, 'Auto-PK worked');
35
36# make sure select works
37my $found = $schema->resultset('Artist')->search({ name => 'foo' })->first;
38is $found->artistid, $new->artistid, 'search works';
39
40# test large column list in select
41$found = $schema->resultset('Artist')->search({ name => 'foo' }, {
42  select => ['artistid', 'name', map "'foo' foo_$_", 0..50],
43  as     => ['artistid', 'name', map       "foo_$_", 0..50],
44})->first;
45is $found->artistid, $new->artistid, 'select with big column list';
46is $found->get_column('foo_50'), 'foo', 'last item in big column list';
47
48# create a few more rows
49for (1..12) {
50  $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
51}
52
53# test multiple active cursors
54my $rs1 = $schema->resultset('Artist')->search({}, { order_by => 'artistid' });
55my $rs2 = $schema->resultset('Artist')->search({}, { order_by => 'name' });
56
57while ($rs1->next) {
58  ok eval { $rs2->next }, 'multiple active cursors';
59}
60
61# test bug where ADO blows up if the first bindparam is shorter than the second
62is $schema->resultset('Artist')->search({ artistid => 2 })->first->name,
63  'Artist 1',
64  'short bindparam';
65
66is $schema->resultset('Artist')->search({ artistid => 13 })->first->name,
67  'Artist 12',
68  'longer bindparam';
69
70done_testing;
71
72# clean up our mess
73END {
74  if (my $dbh = eval { $schema->storage->_dbh }) {
75    eval { $dbh->do("DROP TABLE $_") }
76      for qw/artist/;
77  }
78}
79# vim:sw=2 sts=2
80