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