Fix updating multiple CLOB/BLOB columns on Oracle
[dbsrgits/DBIx-Class.git] / t / 73oracle_blob.t
1 use strict;
2 use warnings;
3
4 use Test::Exception;
5 use Test::More;
6 use Sub::Name;
7 use Try::Tiny;
8 use DBIx::Class::Optional::Dependencies ();
9
10 use lib qw(t/lib);
11
12 use DBICTest::Schema::BindType;
13 BEGIN {
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
26 use DBICTest;
27
28 my ($dsn,  $user,  $pass)  = @ENV{map { "DBICTEST_ORA_${_}" }  qw/DSN USER PASS/};
29
30 plan skip_all => 'Set $ENV{DBICTEST_ORA_DSN}, _USER and _PASS to run this test.'
31   unless ($dsn && $user && $pass);
32
33 plan 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
40 my $v = do {
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";
44 };
45
46 ##########
47 # the recyclebin (new for 10g) sometimes comes in the way
48 my $on_connect_sql = $v >= 10 ? ["ALTER SESSION SET recyclebin = OFF"] : [];
49
50 # iterate all tests on following options
51 my @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
57 my $dbh;
58
59 my $schema;
60 for 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
71 sub _run_blob_tests {
72 SKIP: {
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
95   my $id;
96   foreach my $size (qw( small large )) {
97     $id++;
98
99     local $schema->storage->{debug} = 0
100       if $size eq 'large';
101
102     my $str = $binstr{$size};
103     lives_ok {
104       $rs->create( { 'id' => $id, blob => "blob:$str", clob => "clob:$str", blb2 => "blb2:$str", clb2 => "clb2:$str" } )
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');
117     ok (try { $objs[0]->clb2 }||'' eq "clb2:$str", "clb2 inserted correctly");
118     ok (try { $objs[0]->blb2 }||'' eq "blb2:$str", "blb2 inserted correctly");
119
120     {
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" })
143         ->update({ blob => 'updated blob', clob => 'updated clob', clb2 => 'updated clb2', blb2 => 'updated blb2' });
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');
150     ok (try { $objs[0]->clb2 }||'' eq "updated clb2", "clb2 updated correctly");
151     ok (try { $objs[0]->blb2 }||'' eq "updated blb2", "blb2 updated correctly");
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   }
170 }
171
172   do_clean ($dbh);
173 }
174
175 done_testing;
176
177 sub do_creates {
178   my ($dbh, $q) = @_;
179
180   do_clean($dbh);
181
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)");
183 }
184
185 # clean up our mess
186 sub 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
198 END {
199   if ($dbh) {
200     local $SIG{__WARN__} = sub {};
201     do_clean($dbh);
202     undef $dbh;
203   }
204 }