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