Add extra fetch_first test for right-side-only order
[dbsrgits/DBIx-Class.git] / t / sqlmaker / oracle.t
CommitLineData
c2f2a718 1use strict;
2use warnings;
3use Test::More;
c7d50a7d 4
5BEGIN {
6 require DBIx::Class::Optional::Dependencies;
7 plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('id_shortener')
8 unless DBIx::Class::Optional::Dependencies->req_ok_for ('id_shortener');
9}
10
c2f2a718 11use Test::Exception;
a84fe712 12use Data::Dumper::Concise;
c2f2a718 13use lib qw(t/lib);
8d6b1478 14use DBICTest;
c2f2a718 15use DBIC::SqlMakerTest;
d5dedbd6 16use DBIx::Class::SQLMaker::Oracle;
c2f2a718 17
bf51641f 18#
8273e845 19# Offline test for connect_by
a6646e1b 20# ( without active database connection)
bf51641f 21#
c2f2a718 22my @handle_tests = (
23 {
24 connect_by => { 'parentid' => { '-prior' => \'artistid' } },
25 stmt => '"parentid" = PRIOR artistid',
26 bind => [],
27 msg => 'Simple: "parentid" = PRIOR artistid',
28 },
29 {
e6600283 30 connect_by => { 'parentid' => { '!=' => { '-prior' => { -ident => 'artistid' } } } },
31 stmt => '"parentid" != ( PRIOR "artistid" )',
c2f2a718 32 bind => [],
e6600283 33 msg => 'Simple: "parentid" != ( PRIOR "artistid" )',
c2f2a718 34 },
35 # Examples from http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/queries003.htm
36
37 # CONNECT BY last_name != 'King' AND PRIOR employee_id = manager_id ...
38 {
39 connect_by => [
40 last_name => { '!=' => 'King' },
e6600283 41 manager_id => { '-prior' => { -ident => 'employee_id' } },
c2f2a718 42 ],
e6600283 43 stmt => '( "last_name" != ? OR "manager_id" = PRIOR "employee_id" )',
c2f2a718 44 bind => ['King'],
45 msg => 'oracle.com example #1',
46 },
8273e845 47 # CONNECT BY PRIOR employee_id = manager_id and
c2f2a718 48 # PRIOR account_mgr_id = customer_id ...
49 {
50 connect_by => {
e6600283 51 manager_id => { '-prior' => { -ident => 'employee_id' } },
c2f2a718 52 customer_id => { '>', { '-prior' => \'account_mgr_id' } },
53 },
e6600283 54 stmt => '( "customer_id" > ( PRIOR account_mgr_id ) AND "manager_id" = PRIOR "employee_id" )',
c2f2a718 55 bind => [],
56 msg => 'oracle.com example #2',
57 },
58 # CONNECT BY NOCYCLE PRIOR employee_id = manager_id AND LEVEL <= 4;
59 # TODO: NOCYCLE parameter doesn't work
60);
61
d5dedbd6 62my $sqla_oracle = DBIx::Class::SQLMaker::Oracle->new( quote_char => '"', name_sep => '.' );
63isa_ok($sqla_oracle, 'DBIx::Class::SQLMaker::Oracle');
c2f2a718 64
65
c2f2a718 66for my $case (@handle_tests) {
c2f2a718 67 my ( $stmt, @bind );
68 my $msg = sprintf("Offline: %s",
69 $case->{msg} || substr($case->{stmt},0,25),
70 );
71 lives_ok(
72 sub {
73 ( $stmt, @bind ) = $sqla_oracle->_recurse_where( $case->{connect_by} );
74 is_same_sql_bind( $stmt, \@bind, $case->{stmt}, $case->{bind},$msg )
75 || diag "Search term:\n" . Dumper $case->{connect_by};
76 }
77 ,sprintf("lives is ok from '%s'",$msg));
78}
79
63ca94e1 80is (
81 $sqla_oracle->_shorten_identifier('short_id'),
82 'short_id',
83 '_shorten_identifier for short id without keywords ok'
84);
85
86is (
87 $sqla_oracle->_shorten_identifier('short_id', [qw/ foo /]),
88 'short_id',
89 '_shorten_identifier for short id with one keyword ok'
90);
91
92is (
93 $sqla_oracle->_shorten_identifier('short_id', [qw/ foo bar baz /]),
94 'short_id',
95 '_shorten_identifier for short id with keywords ok'
96);
97
98is (
99 $sqla_oracle->_shorten_identifier('very_long_identifier_which_exceeds_the_30char_limit'),
100 'VryLngIdntfrWhchExc_72M8CIDTM7',
101 '_shorten_identifier without keywords ok',
102);
103
104is (
105 $sqla_oracle->_shorten_identifier('very_long_identifier_which_exceeds_the_30char_limit',[qw/ foo /]),
106 'Foo_72M8CIDTM7KBAUPXG48B22P4E',
107 '_shorten_identifier with one keyword ok',
108);
109is (
110 $sqla_oracle->_shorten_identifier('very_long_identifier_which_exceeds_the_30char_limit',[qw/ foo bar baz /]),
111 'FooBarBaz_72M8CIDTM7KBAUPXG48B',
112 '_shorten_identifier with keywords ok',
113);
c2f2a718 114
bf51641f 115# test SQL generation for INSERT ... RETURNING
116
117sub UREF { \do { my $x } };
118
119$sqla_oracle->{bindtype} = 'columns';
120
121for my $q ('', '"') {
122 local $sqla_oracle->{quote_char} = $q;
123
124 my ($sql, @bind) = $sqla_oracle->insert(
125 'artist',
126 {
127 'name' => 'Testartist',
128 },
129 {
130 'returning' => 'artistid',
131 'returning_container' => [],
132 },
133 );
134
135 is_same_sql_bind(
136 $sql, \@bind,
137 "INSERT INTO ${q}artist${q} (${q}name${q}) VALUES (?) RETURNING ${q}artistid${q} INTO ?",
138 [ [ name => 'Testartist' ], [ artistid => UREF ] ],
139 'sql_maker generates insert returning for one column'
140 );
141
142 ($sql, @bind) = $sqla_oracle->insert(
143 'artist',
144 {
145 'name' => 'Testartist',
146 },
147 {
148 'returning' => \'artistid',
149 'returning_container' => [],
150 },
151 );
152
153 is_same_sql_bind(
154 $sql, \@bind,
155 "INSERT INTO ${q}artist${q} (${q}name${q}) VALUES (?) RETURNING artistid INTO ?",
156 [ [ name => 'Testartist' ], [ artistid => UREF ] ],
157 'sql_maker generates insert returning for one column'
158 );
159
160
161 ($sql, @bind) = $sqla_oracle->insert(
162 'computed_column_test',
163 {
164 'a_timestamp' => '2010-05-26 18:22:00',
165 },
166 {
167 'returning' => [ 'id', 'a_computed_column', 'charfield' ],
168 'returning_container' => [],
169 },
170 );
171
172 is_same_sql_bind(
173 $sql, \@bind,
174 "INSERT INTO ${q}computed_column_test${q} (${q}a_timestamp${q}) VALUES (?) RETURNING ${q}id${q}, ${q}a_computed_column${q}, ${q}charfield${q} INTO ?, ?, ?",
175 [ [ a_timestamp => '2010-05-26 18:22:00' ], [ id => UREF ], [ a_computed_column => UREF ], [ charfield => UREF ] ],
176 'sql_maker generates insert returning for multiple columns'
177 );
178}
179
63ca94e1 180done_testing;