Fix updating multiple CLOB/BLOB columns on Oracle
[dbsrgits/DBIx-Class.git] / t / 73oracle_blob.t
CommitLineData
1143e5bd 1use strict;
2use warnings;
3
4use Test::Exception;
5use Test::More;
6use Sub::Name;
7use Try::Tiny;
8use DBIx::Class::Optional::Dependencies ();
9
10use lib qw(t/lib);
11use DBICTest;
1143e5bd 12
13my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_ORA_${_}" } qw/DSN USER PASS/};
14
15plan skip_all => 'Set $ENV{DBICTEST_ORA_DSN}, _USER and _PASS to run this test.'
16 unless ($dsn && $user && $pass);
17
18plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('test_rdbms_oracle')
19 unless DBIx::Class::Optional::Dependencies->req_ok_for ('test_rdbms_oracle');
20
21$ENV{NLS_SORT} = "BINARY";
22$ENV{NLS_COMP} = "BINARY";
23$ENV{NLS_LANG} = "AMERICAN";
24
25my $v = do {
2c2bc4e5 26 my $si = DBICTest->connect_schema($dsn, $user, $pass)->storage->_server_info;
af1f4f84 27 $si->{normalized_dbms_version}
28 or die "Unparseable Oracle server version: $si->{dbms_version}\n";
1143e5bd 29};
af1f4f84 30
1143e5bd 31##########
32# the recyclebin (new for 10g) sometimes comes in the way
33my $on_connect_sql = $v >= 10 ? ["ALTER SESSION SET recyclebin = OFF"] : [];
34
35# iterate all tests on following options
36my @tryopt = (
37 { on_connect_do => $on_connect_sql },
38 { quote_char => '"', on_connect_do => $on_connect_sql },
39);
40
41# keep a database handle open for cleanup
42my $dbh;
43
44my $schema;
45for my $opt (@tryopt) {
2c2bc4e5 46 my $schema = DBICTest->connect_schema($dsn, $user, $pass, $opt);
1143e5bd 47
48 $dbh = $schema->storage->dbh;
49 my $q = $schema->storage->sql_maker->quote_char || '';
50
51 do_creates($dbh, $q);
52
53 _run_blob_tests($schema, $opt);
54}
55
56sub _run_blob_tests {
57SKIP: {
1143e5bd 58 my ($schema, $opt) = @_;
59 my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
60 $binstr{'large'} = $binstr{'small'} x 1024;
61
74b5397c 62 my $maxloblen = (length $binstr{'large'}) + 6;
1143e5bd 63 note "Localizing LongReadLen to $maxloblen to avoid truncation of test data";
64 local $dbh->{'LongReadLen'} = $maxloblen;
65
66 my $rs = $schema->resultset('BindType');
67
68 if ($DBD::Oracle::VERSION eq '1.23') {
69 throws_ok { $rs->create({ id => 1, blob => $binstr{large} }) }
70 qr/broken/,
71 'throws on blob insert with DBD::Oracle == 1.23';
72 skip 'buggy BLOB support in DBD::Oracle 1.23', 1;
73 }
74
75 my $q = $schema->storage->sql_maker->quote_char || '';
76 local $TODO = 'Something is confusing column bindtype assignment when quotes are active'
77 . ': https://rt.cpan.org/Ticket/Display.html?id=64206'
78 if $q;
79
1143e5bd 80 my $id;
81 foreach my $size (qw( small large )) {
82 $id++;
83
49eeb48d 84 local $schema->storage->{debug} = 0
85 if $size eq 'large';
1143e5bd 86
87 my $str = $binstr{$size};
88 lives_ok {
74b5397c 89 $rs->create( { 'id' => $id, blob => "blob:$str", clob => "clob:$str", blob2 => "blob2:$str", clob2 => "clob2:$str" } )
1143e5bd 90 } "inserted $size without dying";
91
92 my %kids = %{$schema->storage->_dbh->{CachedKids}};
93 my @objs = $rs->search({ blob => "blob:$str", clob => "clob:$str" })->all;
94 is_deeply (
95 $schema->storage->_dbh->{CachedKids},
96 \%kids,
97 'multi-part LOB equality query was not cached',
98 ) if $size eq 'large';
99 is @objs, 1, 'One row found matching on both LOBs';
100 ok (try { $objs[0]->blob }||'' eq "blob:$str", 'blob inserted/retrieved correctly');
101 ok (try { $objs[0]->clob }||'' eq "clob:$str", 'clob inserted/retrieved correctly');
74b5397c 102 ok (try { $objs[0]->clob2 }||'' eq "clob2:$str", "clob2 inserted correctly");
103 ok (try { $objs[0]->blob2 }||'' eq "blob2:$str", "blob2 inserted correctly");
1143e5bd 104
4ca1fd6f 105 {
1143e5bd 106 local $TODO = '-like comparison on blobs not tested before ora 10 (fails on 8i)'
107 if $schema->storage->_server_info->{normalized_dbms_version} < 10;
108
109 lives_ok {
110 @objs = $rs->search({ clob => { -like => 'clob:%' } })->all;
111 ok (@objs, 'rows found matching CLOB with a LIKE query');
112 } 'Query with like on blob succeeds';
113 }
114
115 ok(my $subq = $rs->search(
116 { blob => "blob:$str", clob => "clob:$str" },
117 {
118 from => \ "(SELECT * FROM ${q}bindtype_test${q} WHERE ${q}id${q} != ?) ${q}me${q}",
119 bind => [ [ undef => 12345678 ] ],
120 }
121 )->get_column('id')->as_query);
122
123 @objs = $rs->search({ id => { -in => $subq } })->all;
124 is (@objs, 1, 'One row found matching on both LOBs as a subquery');
125
126 lives_ok {
127 $rs->search({ id => $id, blob => "blob:$str", clob => "clob:$str" })
74b5397c 128 ->update({ blob => 'updated blob', clob => 'updated clob', clob2 => 'updated clob2', blob2 => 'updated blob2' });
1143e5bd 129 } 'blob UPDATE with blobs in WHERE clause survived';
130
131 @objs = $rs->search({ blob => "updated blob", clob => 'updated clob' })->all;
132 is @objs, 1, 'found updated row';
133 ok (try { $objs[0]->blob }||'' eq "updated blob", 'blob updated/retrieved correctly');
134 ok (try { $objs[0]->clob }||'' eq "updated clob", 'clob updated/retrieved correctly');
74b5397c 135 ok (try { $objs[0]->clob2 }||'' eq "updated clob2", "clob2 updated correctly");
136 ok (try { $objs[0]->blob2 }||'' eq "updated blob2", "blob2 updated correctly");
1143e5bd 137
138 lives_ok {
139 $rs->search({ id => $id })
140 ->update({ blob => 're-updated blob', clob => 're-updated clob' });
141 } 'blob UPDATE without blobs in WHERE clause survived';
142
143 @objs = $rs->search({ blob => 're-updated blob', clob => 're-updated clob' })->all;
144 is @objs, 1, 'found updated row';
145 ok (try { $objs[0]->blob }||'' eq 're-updated blob', 'blob updated/retrieved correctly');
146 ok (try { $objs[0]->clob }||'' eq 're-updated clob', 'clob updated/retrieved correctly');
147
148 lives_ok {
149 $rs->search({ blob => "re-updated blob", clob => "re-updated clob" })
150 ->delete;
151 } 'blob DELETE with WHERE clause survived';
152 @objs = $rs->search({ blob => "re-updated blob", clob => 're-updated clob' })->all;
153 is @objs, 0, 'row deleted successfully';
154 }
4ca1fd6f 155}
1143e5bd 156
157 do_clean ($dbh);
158}
159
160done_testing;
161
162sub do_creates {
163 my ($dbh, $q) = @_;
164
165 do_clean($dbh);
166
167 $dbh->do("CREATE TABLE ${q}bindtype_test${q} (${q}id${q} integer NOT NULL PRIMARY KEY, ${q}bytea${q} integer NULL, ${q}blob${q} blob NULL, ${q}blob2${q} blob NULL, ${q}clob${q} clob NULL, ${q}clob2${q} clob NULL, ${q}a_memo${q} integer NULL)");
168}
169
170# clean up our mess
171sub do_clean {
172
173 my $dbh = shift || return;
174
175 for my $q ('', '"') {
176 my @clean = (
177 "DROP TABLE ${q}bindtype_test${q}",
178 );
179 eval { $dbh -> do ($_) } for @clean;
180 }
181}
182
183END {
6918c70e 184 if ($dbh) {
1143e5bd 185 local $SIG{__WARN__} = sub {};
6918c70e 186 do_clean($dbh);
187 undef $dbh;
1143e5bd 188 }
189}