Support for saving CLOB and BLOB types in Oracle.
[dbsrgits/DBIx-Class.git] / t / 73oracle.t
1 {
2   package    # hide from PAUSE
3     DBICTest::Schema::ArtistFQN;
4
5   use base 'DBIx::Class::Core';
6
7   __PACKAGE__->table(
8       defined $ENV{DBICTEST_ORA_USER}
9       ? $ENV{DBICTEST_ORA_USER} . '.artist'
10       : 'artist'
11   );
12   __PACKAGE__->add_columns(
13       'artistid' => {
14           data_type         => 'integer',
15           is_auto_increment => 1,
16       },
17       'name' => {
18           data_type   => 'varchar',
19           size        => 100,
20           is_nullable => 1,
21       },
22   );
23   __PACKAGE__->set_primary_key('artistid');
24
25   1;
26 }
27
28 use strict;
29 use warnings;  
30
31 use Test::Exception;
32 use Test::More;
33 use lib qw(t/lib);
34 use DBICTest;
35
36 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_ORA_${_}" } qw/DSN USER PASS/};
37
38 plan skip_all => 'Set $ENV{DBICTEST_ORA_DSN}, _USER and _PASS to run this test. ' .
39   'Warning: This test drops and creates tables called \'artist\', \'cd\', \'track\' and \'sequence_test\''.
40   ' as well as following sequences: \'pkid1_seq\', \'pkid2_seq\' and \'nonpkid_seq\''
41   unless ($dsn && $user && $pass);
42
43 plan tests => 32;
44
45 DBICTest::Schema->load_classes('ArtistFQN');
46 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
47
48 my $dbh = $schema->storage->dbh;
49
50 eval {
51   $dbh->do("DROP SEQUENCE artist_seq");
52   $dbh->do("DROP SEQUENCE pkid1_seq");
53   $dbh->do("DROP SEQUENCE pkid2_seq");
54   $dbh->do("DROP SEQUENCE nonpkid_seq");
55   $dbh->do("DROP TABLE artist");
56   $dbh->do("DROP TABLE sequence_test");
57   $dbh->do("DROP TABLE cd");
58   $dbh->do("DROP TABLE track");
59 };
60 $dbh->do("CREATE SEQUENCE artist_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
61 $dbh->do("CREATE SEQUENCE pkid1_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
62 $dbh->do("CREATE SEQUENCE pkid2_seq START WITH 10 MAXVALUE 999999 MINVALUE 0");
63 $dbh->do("CREATE SEQUENCE nonpkid_seq START WITH 20 MAXVALUE 999999 MINVALUE 0");
64 $dbh->do("CREATE TABLE artist (artistid NUMBER(12), name VARCHAR(255), rank NUMBER(38), charfield VARCHAR2(10))");
65 $dbh->do("CREATE TABLE sequence_test (pkid1 NUMBER(12), pkid2 NUMBER(12), nonpkid NUMBER(12), name VARCHAR(255))");
66 $dbh->do("CREATE TABLE cd (cdid NUMBER(12), artist NUMBER(12), title VARCHAR(255), year VARCHAR(4))");
67 $dbh->do("CREATE TABLE track (trackid NUMBER(12), cd NUMBER(12), position NUMBER(12), title VARCHAR(255), last_updated_on DATE)");
68
69 $dbh->do("ALTER TABLE artist ADD (CONSTRAINT artist_pk PRIMARY KEY (artistid))");
70 $dbh->do("ALTER TABLE sequence_test ADD (CONSTRAINT sequence_test_constraint PRIMARY KEY (pkid1, pkid2))");
71 $dbh->do(qq{
72   CREATE OR REPLACE TRIGGER artist_insert_trg
73   BEFORE INSERT ON artist
74   FOR EACH ROW
75   BEGIN
76     IF :new.artistid IS NULL THEN
77       SELECT artist_seq.nextval
78       INTO :new.artistid
79       FROM DUAL;
80     END IF;
81   END;
82 });
83
84 {
85     # Swiped from t/bindtype_columns.t to avoid creating my own Resultset.
86
87     local $SIG{__WARN__} = sub {};
88     eval { $dbh->do('DROP TABLE bindtype_test') };
89
90     $dbh->do(qq[
91         CREATE TABLE bindtype_test 
92         (
93             id              integer      NOT NULL   PRIMARY KEY,
94             bytea           integer      NULL,
95             blob            blob         NULL,
96             clob            clob         NULL
97         )
98     ],{ RaiseError => 1, PrintError => 1 });
99 }
100
101 # This is in Core now, but it's here just to test that it doesn't break
102 $schema->class('Artist')->load_components('PK::Auto');
103 # These are compat shims for PK::Auto...
104 $schema->class('CD')->load_components('PK::Auto::Oracle');
105 $schema->class('Track')->load_components('PK::Auto::Oracle');
106
107 # test primary key handling
108 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
109 is($new->artistid, 1, "Oracle Auto-PK worked");
110
111 # test again with fully-qualified table name
112 $new = $schema->resultset('ArtistFQN')->create( { name => 'bar' } );
113 is( $new->artistid, 2, "Oracle Auto-PK worked with fully-qualified tablename" );
114
115 # test join with row count ambiguity
116 my $cd = $schema->resultset('CD')->create({ cdid => 1, artist => 1, title => 'EP C', year => '2003' });
117 my $track = $schema->resultset('Track')->create({ trackid => 1, cd => 1, position => 1, title => 'Track1' });
118 my $tjoin = $schema->resultset('Track')->search({ 'me.title' => 'Track1'},
119         { join => 'cd',
120           rows => 2 }
121 );
122
123 is($tjoin->next->title, 'Track1', "ambiguous column ok");
124
125 # check count distinct with multiple columns
126 my $other_track = $schema->resultset('Track')->create({ trackid => 2, cd => 1, position => 1, title => 'Track2' });
127 my $tcount = $schema->resultset('Track')->search(
128     {},
129     {
130         select => [{count => {distinct => ['position', 'title']}}],
131         as => ['count']
132     }
133   );
134
135 is($tcount->next->get_column('count'), 2, "multiple column select distinct ok");
136
137 # test LIMIT support
138 for (1..6) {
139     $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
140 }
141 my $it = $schema->resultset('Artist')->search( {},
142     { rows => 3,
143       offset => 3,
144       order_by => 'artistid' }
145 );
146 is( $it->count, 3, "LIMIT count ok" );
147 is( $it->next->name, "Artist 2", "iterator->next ok" );
148 $it->next;
149 $it->next;
150 is( $it->next, undef, "next past end of resultset ok" );
151
152 {
153   my $rs = $schema->resultset('Track')->search( undef, { columns=>[qw/trackid position/], group_by=> [ qw/trackid position/ ] , rows => 2, offset=>1 });
154   my @results = $rs->all;
155   is( scalar @results, 1, "Group by with limit OK" );
156 }
157
158 # test auto increment using sequences WITHOUT triggers
159 for (1..5) {
160     my $st = $schema->resultset('SequenceTest')->create({ name => 'foo' });
161     is($st->pkid1, $_, "Oracle Auto-PK without trigger: First primary key");
162     is($st->pkid2, $_ + 9, "Oracle Auto-PK without trigger: Second primary key");
163     is($st->nonpkid, $_ + 19, "Oracle Auto-PK without trigger: Non-primary key");
164 }
165 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 });
166 is($st->pkid1, 55, "Oracle Auto-PK without trigger: First primary key set manually");
167
168 {
169         my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
170         $binstr{'large'} = $binstr{'small'} x 1024;
171
172         my $maxloblen = length $binstr{'large'};
173         note "Localizing LongReadLen to $maxloblen to avoid truncation of test data";
174         local $dbh->{'LongReadLen'} = $maxloblen;
175
176         my $rs = $schema->resultset('BindType');
177         my $id = 0;
178
179         foreach my $type (qw( blob clob )) {
180                 foreach my $size (qw( small large )) {
181                         $id++;
182
183                         lives_ok { $rs->create( { 'id' => $id, $type => $binstr{$type} } ) }
184                                 "inserted $size $type without dying";
185
186                         is( $rs->find($id)->$type, $binstr{$type}, "verified inserted $size $type" );
187                 }
188         }
189 }
190
191 # clean up our mess
192 END {
193     if($schema && ($dbh = $schema->storage->dbh)) {
194         $dbh->do("DROP SEQUENCE artist_seq");
195         $dbh->do("DROP SEQUENCE pkid1_seq");
196         $dbh->do("DROP SEQUENCE pkid2_seq");
197         $dbh->do("DROP SEQUENCE nonpkid_seq");
198         $dbh->do("DROP TABLE artist");
199         $dbh->do("DROP TABLE sequence_test");
200         $dbh->do("DROP TABLE cd");
201         $dbh->do("DROP TABLE track");
202         $dbh->do("DROP TABLE bindtype_test");
203     }
204 }
205