make the DB2/AS400 storage a subclass of DB2, do RNO detection, fix FetchFirst
[dbsrgits/DBIx-Class.git] / t / 745db2.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Exception;
6 use Try::Tiny;
7 use lib qw(t/lib);
8 use DBICTest;
9
10 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_DB2_${_}" } qw/DSN USER PASS/};
11
12 #warn "$dsn $user $pass";
13
14 plan skip_all => 'Set $ENV{DBICTEST_DB2_DSN}, _USER and _PASS to run this test'
15   unless ($dsn && $user);
16
17 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
18
19 my $dbh = $schema->storage->dbh;
20
21 # test RNO and name_sep detection
22 my $name_sep = $dbh->get_info(41);
23
24 is $schema->storage->sql_maker->name_sep, $name_sep,
25   'name_sep detection';
26
27 my $have_rno = try {
28   $dbh->selectrow_array(
29 "SELECT row_number() OVER (ORDER BY 1) FROM sysibm${name_sep}sysdummy1"
30   );
31   1;
32 };
33
34 is $schema->storage->sql_maker->limit_dialect,
35   ($have_rno ? 'RowNumberOver' : 'FetchFirst'),
36   'limit_dialect detection';
37
38 eval { $dbh->do("DROP TABLE artist") };
39
40 $dbh->do("CREATE TABLE artist (artistid INTEGER GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), name VARCHAR(255), charfield CHAR(10), rank INTEGER DEFAULT 13);");
41
42 my $ars = $schema->resultset('Artist');
43 is ( $ars->count, 0, 'No rows at first' );
44
45 # test primary key handling
46 my $new = $ars->create({ name => 'foo' });
47 ok($new->artistid, "Auto-PK worked");
48
49 # test explicit key spec
50 $new = $ars->create ({ name => 'bar', artistid => 66 });
51 is($new->artistid, 66, 'Explicit PK worked');
52 $new->discard_changes;
53 is($new->artistid, 66, 'Explicit PK assigned');
54
55 # test populate
56 lives_ok (sub {
57   my @pop;
58   for (1..2) {
59     push @pop, { name => "Artist_$_" };
60   }
61   $ars->populate (\@pop);
62 });
63
64 # test populate with explicit key
65 lives_ok (sub {
66   my @pop;
67   for (1..2) {
68     push @pop, { name => "Artist_expkey_$_", artistid => 100 + $_ };
69   }
70   $ars->populate (\@pop);
71 });
72
73 # count what we did so far
74 is ($ars->count, 6, 'Simple count works');
75
76 # test LIMIT support
77 my $lim = $ars->search( {},
78   {
79     rows => 3,
80     offset => 4,
81     order_by => 'artistid'
82   }
83 );
84 is( $lim->count, 2, 'ROWS+OFFSET count ok' );
85 is( $lim->all, 2, 'Number of ->all objects matches count' );
86
87 # Limit with select-lock
88 TODO: {
89   local $TODO = "Seems we can't SELECT ... FOR ... on subqueries";
90   lives_ok {
91     $schema->txn_do (sub {
92       isa_ok (
93         $schema->resultset('Artist')->find({artistid => 1}, {for => 'update', rows => 1}),
94         'DBICTest::Schema::Artist',
95       );
96     });
97   } 'Limited FOR UPDATE select works';
98 }
99
100 # test iterator
101 $lim->reset;
102 is( $lim->next->artistid, 101, "iterator->next ok" );
103 is( $lim->next->artistid, 102, "iterator->next ok" );
104 is( $lim->next, undef, "next past end of resultset ok" );
105
106 # test FetchFirst limit dialect syntax
107 {
108   local $schema->storage->sql_maker->{limit_dialect} = 'FetchFirst';
109
110   my $lim = $ars->search({}, {
111     rows => 3,
112     offset => 2,
113     order_by => 'artistid',
114   });
115
116   is $lim->count, 3, 'fetch first limit count ok';
117
118   is $lim->all, 3, 'fetch first number of ->all objects matches count';
119
120   is $lim->next->artistid, 3, 'iterator->next ok';
121   is $lim->next->artistid, 66, 'iterator->next ok';
122   is $lim->next->artistid, 101, 'iterator->next ok';
123   is $lim->next, undef, 'iterator->next past end of resultset ok';
124 }
125
126 my $test_type_info = {
127     'artistid' => {
128         'data_type' => 'INTEGER',
129         'is_nullable' => 0,
130         'size' => 10
131     },
132     'name' => {
133         'data_type' => 'VARCHAR',
134         'is_nullable' => 1,
135         'size' => 255
136     },
137     'charfield' => {
138         'data_type' => 'CHAR',
139         'is_nullable' => 1,
140         'size' => 10
141     },
142     'rank' => {
143         'data_type' => 'INTEGER',
144         'is_nullable' => 1,
145         'size' => 10
146     },
147 };
148
149
150 my $type_info = $schema->storage->columns_info_for('artist');
151 is_deeply($type_info, $test_type_info, 'columns_info_for - column data types');
152
153 done_testing;
154
155 # clean up our mess
156 END {
157     my $dbh = eval { $schema->storage->_dbh };
158     $dbh->do("DROP TABLE artist") if $dbh;
159 }