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