make the DB2/AS400 storage a subclass of DB2, do RNO detection, fix FetchFirst
[dbsrgits/DBIx-Class.git] / t / 745db2.t
CommitLineData
70350518 1use strict;
835cdc8d 2use warnings;
70350518 3
4use Test::More;
88d20956 5use Test::Exception;
96eacdb7 6use Try::Tiny;
70350518 7use lib qw(t/lib);
8use DBICTest;
843f8ecd 9
10my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_DB2_${_}" } qw/DSN USER PASS/};
11
12#warn "$dsn $user $pass";
13
58d387fe 14plan skip_all => 'Set $ENV{DBICTEST_DB2_DSN}, _USER and _PASS to run this test'
843f8ecd 15 unless ($dsn && $user);
16
3ff5b740 17my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
843f8ecd 18
3ff5b740 19my $dbh = $schema->storage->dbh;
843f8ecd 20
96eacdb7 21# test RNO and name_sep detection
22my $name_sep = $dbh->get_info(41);
23
24is $schema->storage->sql_maker->name_sep, $name_sep,
25 'name_sep detection';
26
27my $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
34is $schema->storage->sql_maker->limit_dialect,
35 ($have_rno ? 'RowNumberOver' : 'FetchFirst'),
36 'limit_dialect detection';
37
88c00130 38eval { $dbh->do("DROP TABLE artist") };
843f8ecd 39
4e88dfc4 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);");
843f8ecd 41
25aa8311 42my $ars = $schema->resultset('Artist');
88d20956 43is ( $ars->count, 0, 'No rows at first' );
25aa8311 44
835cdc8d 45# test primary key handling
25aa8311 46my $new = $ars->create({ name => 'foo' });
843f8ecd 47ok($new->artistid, "Auto-PK worked");
48
835cdc8d 49# test explicit key spec
88d20956 50$new = $ars->create ({ name => 'bar', artistid => 66 });
51is($new->artistid, 66, 'Explicit PK worked');
52$new->discard_changes;
53is($new->artistid, 66, 'Explicit PK assigned');
54
835cdc8d 55# test populate
88d20956 56lives_ok (sub {
57 my @pop;
58 for (1..2) {
59 push @pop, { name => "Artist_$_" };
60 }
61 $ars->populate (\@pop);
62});
63
835cdc8d 64# test populate with explicit key
88d20956 65lives_ok (sub {
66 my @pop;
67 for (1..2) {
68 push @pop, { name => "Artist_expkey_$_", artistid => 100 + $_ };
69 }
70 $ars->populate (\@pop);
71});
835cdc8d 72
73# count what we did so far
88d20956 74is ($ars->count, 6, 'Simple count works');
75
835cdc8d 76# test LIMIT support
88d20956 77my $lim = $ars->search( {},
25aa8311 78 {
79 rows => 3,
88d20956 80 offset => 4,
25aa8311 81 order_by => 'artistid'
82 }
843f8ecd 83);
835cdc8d 84is( $lim->count, 2, 'ROWS+OFFSET count ok' );
88d20956 85is( $lim->all, 2, 'Number of ->all objects matches count' );
25aa8311 86
e5372da4 87# Limit with select-lock
88TODO: {
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
835cdc8d 100# test iterator
88d20956 101$lim->reset;
102is( $lim->next->artistid, 101, "iterator->next ok" );
103is( $lim->next->artistid, 102, "iterator->next ok" );
104is( $lim->next, undef, "next past end of resultset ok" );
25aa8311 105
96eacdb7 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}
843f8ecd 125
126my $test_type_info = {
127 'artistid' => {
128 'data_type' => 'INTEGER',
129 'is_nullable' => 0,
fc22fbac 130 'size' => 10
843f8ecd 131 },
132 'name' => {
133 'data_type' => 'VARCHAR',
134 'is_nullable' => 1,
135 'size' => 255
136 },
137 'charfield' => {
fc22fbac 138 'data_type' => 'CHAR',
843f8ecd 139 'is_nullable' => 1,
835cdc8d 140 'size' => 10
843f8ecd 141 },
70c29d32 142 'rank' => {
143 'data_type' => 'INTEGER',
144 'is_nullable' => 1,
835cdc8d 145 'size' => 10
70c29d32 146 },
843f8ecd 147};
148
149
3ff5b740 150my $type_info = $schema->storage->columns_info_for('artist');
843f8ecd 151is_deeply($type_info, $test_type_info, 'columns_info_for - column data types');
152
88d20956 153done_testing;
154
843f8ecd 155# clean up our mess
3ff5b740 156END {
3a4c1d89 157 my $dbh = eval { $schema->storage->_dbh };
3ff5b740 158 $dbh->do("DROP TABLE artist") if $dbh;
159}