t/sqlmaker/limit_dialects/toplimit.t - add missing me.id - fairly sure this was a bug
[dbsrgits/DBIx-Class.git] / t / sqlmaker / oracle.t
1 use strict;
2 use warnings;
3 use Test::More;
4
5 BEGIN {
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
11 use Test::Exception;
12 use Data::Dumper::Concise;
13 use lib qw(t/lib);
14 use DBICTest;
15 use DBIC::SqlMakerTest;
16 use DBIx::Class::SQLMaker::Oracle;
17
18 #
19 #  Offline test for connect_by
20 #  ( without active database connection)
21 #
22 my @handle_tests = (
23     {
24         connect_by  => { 'parentid' => { '-prior' => \'artistid' } },
25         stmt        => '"parentid" = ( PRIOR artistid )',
26         bind        => [],
27         msg         => 'Simple: "parentid" = PRIOR artistid',
28     },
29     {
30         connect_by  => { 'parentid' => { '!=' => { '-prior' => { -ident => 'artistid' } } } },
31         stmt        => '"parentid" != ( PRIOR "artistid" )',
32         bind        => [],
33         msg         => 'Simple: "parentid" != ( PRIOR "artistid" )',
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' },
41             manager_id => { '-prior' => { -ident => 'employee_id' } },
42         ],
43         stmt        => '( "last_name" != ? OR "manager_id" = ( PRIOR "employee_id" ) )',
44         bind        => ['King'],
45         msg         => 'oracle.com example #1',
46     },
47     # CONNECT BY PRIOR employee_id = manager_id and
48     #            PRIOR account_mgr_id = customer_id ...
49     {
50         connect_by  => {
51             manager_id => { '-prior' => { -ident => 'employee_id' } },
52             customer_id => { '>', { '-prior' => \'account_mgr_id' } },
53         },
54         stmt        => '( "customer_id" > ( PRIOR account_mgr_id ) AND "manager_id" = ( PRIOR "employee_id" ) )',
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
62 my $sqla_oracle = DBIx::Class::SQLMaker::Oracle->new( quote_char => '"', name_sep => '.' );
63 isa_ok($sqla_oracle, 'DBIx::Class::SQLMaker::Oracle');
64
65
66 for my $case (@handle_tests) {
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
80 is (
81   $sqla_oracle->_shorten_identifier('short_id'),
82   'short_id',
83   '_shorten_identifier for short id without keywords ok'
84 );
85
86 is (
87   $sqla_oracle->_shorten_identifier('short_id', [qw/ foo /]),
88   'short_id',
89   '_shorten_identifier for short id with one keyword ok'
90 );
91
92 is (
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
98 is (
99   $sqla_oracle->_shorten_identifier('very_long_identifier_which_exceeds_the_30char_limit'),
100   'VryLngIdntfrWhchExc_72M8CIDTM7',
101   '_shorten_identifier without keywords ok',
102 );
103
104 is (
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 );
109 is (
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 );
114
115 # test SQL generation for INSERT ... RETURNING
116
117 sub UREF { \do { my $x } };
118
119 $sqla_oracle->{bindtype} = 'columns';
120
121 for my $q ('', '"') {
122   local $sqla_oracle->{quote_char} = $q;
123   $sqla_oracle->clear_renderer;
124   $sqla_oracle->clear_converter;
125
126   my ($sql, @bind) = $sqla_oracle->insert(
127     'artist',
128     {
129       'name' => 'Testartist',
130     },
131     {
132       'returning' => 'artistid',
133       'returning_container' => [],
134     },
135   );
136
137   is_same_sql_bind(
138     $sql, \@bind,
139     "INSERT INTO ${q}artist${q} (${q}name${q}) VALUES (?) RETURNING ${q}artistid${q} INTO ?",
140     [ [ name => 'Testartist' ], [ artistid => UREF ] ],
141     'sql_maker generates insert returning for one column'
142   );
143
144   ($sql, @bind) = $sqla_oracle->insert(
145     'artist',
146     {
147       'name' => 'Testartist',
148     },
149     {
150       'returning' => \'artistid',
151       'returning_container' => [],
152     },
153   );
154
155   is_same_sql_bind(
156     $sql, \@bind,
157     "INSERT INTO ${q}artist${q} (${q}name${q}) VALUES (?) RETURNING artistid INTO ?",
158     [ [ name => 'Testartist' ], [ artistid => UREF ] ],
159     'sql_maker generates insert returning for one column'
160   );
161
162
163   ($sql, @bind) = $sqla_oracle->insert(
164     'computed_column_test',
165     {
166       'a_timestamp' => '2010-05-26 18:22:00',
167     },
168     {
169       'returning' => [ 'id', 'a_computed_column', 'charfield' ],
170       'returning_container' => [],
171     },
172   );
173
174   is_same_sql_bind(
175     $sql, \@bind,
176     "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 ?, ?, ?",
177     [ [ a_timestamp => '2010-05-26 18:22:00' ], [ id => UREF ], [ a_computed_column => UREF ], [ charfield => UREF ] ],
178     'sql_maker generates insert returning for multiple columns'
179   );
180 }
181
182 done_testing;