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