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