Fix updating multiple CLOB/BLOB columns on Oracle
[dbsrgits/DBIx-Class.git] / t / 72pg_bytea.t
CommitLineData
6e399b4f 1use strict;
68de9438 2use warnings;
6e399b4f 3
4use Test::More;
199fbc45 5use DBIx::Class::Optional::Dependencies ();
9aec3ec6 6use Try::Tiny;
6e399b4f 7use lib qw(t/lib);
8use DBICTest;
9
199fbc45 10plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('rdbms_pg')
11 unless DBIx::Class::Optional::Dependencies->req_ok_for ('rdbms_pg');
12
9fdf90df 13my ($dsn, $dbuser, $dbpass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
6e399b4f 14
9fdf90df 15plan skip_all => 'Set $ENV{DBICTEST_PG_DSN}, _USER and _PASS to run this test'
16 unless ($dsn && $dbuser);
d7f20fdf 17
2c2bc4e5 18my $schema = DBICTest->connect_schema($dsn, $dbuser, $dbpass, { AutoCommit => 1 });
9fdf90df 19
9aec3ec6 20if ($schema->storage->_server_info->{normalized_dbms_version} >= 9.0) {
21 if (not try { DBD::Pg->VERSION('2.17.2') }) {
22 plan skip_all =>
23 'DBD::Pg < 2.17.2 does not work with Pg >= 9.0 BYTEA columns';
24 }
25}
26elsif (not try { DBD::Pg->VERSION('2.9.2') }) {
27 plan skip_all =>
28 'DBD::Pg < 2.9.2 does not work with BYTEA columns';
29}
30
eda28767 31my $dbh = $schema->storage->dbh;
9fdf90df 32
bb452615 33{
34 local $SIG{__WARN__} = sub {};
a65c1894 35 $dbh->do('DROP TABLE IF EXISTS bindtype_test');
6ec7d1bb 36
37 # the blob/clob are for reference only, will be useful when we switch to SQLT and can test Oracle along the way
bb452615 38 $dbh->do(qq[
f3a9ea3d 39 CREATE TABLE bindtype_test
bb452615 40 (
6ec7d1bb 41 id serial NOT NULL PRIMARY KEY,
42 bytea bytea NULL,
43 blob bytea NULL,
74b5397c 44 blob2 bytea NULL,
f3a9ea3d 45 clob text NULL,
74b5397c 46 clob2 text NULL,
f3a9ea3d 47 a_memo text NULL
bb452615 48 );
49 ],{ RaiseError => 1, PrintError => 1 });
50}
9fdf90df 51
0007aedf 52$schema->storage->debug(0); # these tests spew up way too much stuff, disable trace
53
d7f20fdf 54my $big_long_string = "\x00\x01\x02 abcd" x 125000;
6ffb5be5 55
56my $new;
57# test inserting a row
58{
59 $new = $schema->resultset('BindType')->create({ bytea => $big_long_string });
60
61 ok($new->id, "Created a bytea row");
10f6d2b7 62 ok($new->bytea eq $big_long_string, "Set the blob correctly.");
6ffb5be5 63}
64
b1e86b3e 65# test retrieval of the bytea column
66{
67 my $row = $schema->resultset('BindType')->find({ id => $new->id });
10f6d2b7 68 ok($row->get_column('bytea') eq $big_long_string, "Created the blob correctly.");
b1e86b3e 69}
9fdf90df 70
ba61fa2a 71{
6ffb5be5 72 my $rs = $schema->resultset('BindType')->search({ bytea => $big_long_string });
b1e86b3e 73
6ffb5be5 74 # search on the bytea column (select)
75 {
76 my $row = $rs->first;
77 is($row ? $row->id : undef, $new->id, "Found the row searching on the bytea column.");
78 }
b1e86b3e 79
6ffb5be5 80 # search on the bytea column (update)
81 {
82 my $new_big_long_string = $big_long_string . "2";
83 $schema->txn_do(sub {
84 $rs->update({ bytea => $new_big_long_string });
85 my $row = $schema->resultset('BindType')->find({ id => $new->id });
10f6d2b7 86 ok( ($row ? $row->get_column('bytea') : '') eq $new_big_long_string,
6ffb5be5 87 "Updated the row correctly (searching on the bytea column)."
88 );
89 $schema->txn_rollback;
90 });
91 }
b1e86b3e 92
6ffb5be5 93 # search on the bytea column (delete)
94 {
95 $schema->txn_do(sub {
96 $rs->delete;
97 my $row = $schema->resultset('BindType')->find({ id => $new->id });
98 is($row, undef, "Deleted the row correctly (searching on the bytea column).");
99 $schema->txn_rollback;
100 });
101 }
a780a0ff 102
103 # create with blob from $rs
104 $new = $rs->create({});
10f6d2b7 105 ok($new->bytea eq $big_long_string, 'Object has bytea value from $rs');
a780a0ff 106 $new->discard_changes;
10f6d2b7 107 ok($new->bytea eq $big_long_string, 'bytea value made it to db');
6ffb5be5 108}
9fdf90df 109
a9bac98f 110# test inserting a row via populate() (bindtype propagation through execute_for_fetch)
111# use a new $dbh to ensure no leakage due to prepare_cached
112{
113 my $cnt = 4;
114
115 $schema->storage->_dbh(undef);
116 my $rs = $schema->resultset('BindType');
117 $rs->delete;
118
119 $rs->populate([
120 [qw/id bytea/],
121 map { [
122 \[ '?', [ {} => $_ ] ],
123 "pop_${_}_" . $big_long_string,
124 ]} (1 .. $cnt)
125 ]);
126
127 is($rs->count, $cnt, 'All rows were correctly inserted');
128 for (1..$cnt) {
129 my $r = $rs->find({ bytea => "pop_${_}_" . $big_long_string });
130 is ($r->id, $_, "Row $_ found after find() on the blob");
131
132 }
133}
134
a780a0ff 135done_testing;
136
a9bac98f 137eval { $schema->storage->dbh_do(sub { $_[1]->do("DROP TABLE bindtype_test") } ) };
a780a0ff 138