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