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