1 use DBIx::Class::Optional::Dependencies -skip_all_without => 'id_shortener';
8 use Data::Dumper::Concise;
10 use DBICTest ':DiffSQL';
11 use DBIx::Class::SQLMaker::Oracle;
14 # Offline test for connect_by
15 # ( without active database connection)
19 connect_by => { 'parentid' => { '-prior' => \'artistid' } },
20 stmt => '"parentid" = PRIOR artistid',
22 msg => 'Simple: "parentid" = PRIOR artistid',
25 connect_by => { 'parentid' => { '!=' => { '-prior' => { -ident => 'artistid' } } } },
26 stmt => '"parentid" != ( PRIOR "artistid" )',
28 msg => 'Simple: "parentid" != ( PRIOR "artistid" )',
30 # Examples from http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/queries003.htm
32 # CONNECT BY last_name != 'King' AND PRIOR employee_id = manager_id ...
35 last_name => { '!=' => 'King' },
36 manager_id => { '-prior' => { -ident => 'employee_id' } },
38 stmt => '( "last_name" != ? OR "manager_id" = PRIOR "employee_id" )',
40 msg => 'oracle.com example #1',
42 # CONNECT BY PRIOR employee_id = manager_id and
43 # PRIOR account_mgr_id = customer_id ...
46 manager_id => { '-prior' => { -ident => 'employee_id' } },
47 customer_id => { '>', { '-prior' => \'account_mgr_id' } },
49 stmt => '( "customer_id" > ( PRIOR account_mgr_id ) AND "manager_id" = PRIOR "employee_id" )',
51 msg => 'oracle.com example #2',
53 # CONNECT BY NOCYCLE PRIOR employee_id = manager_id AND LEVEL <= 4;
54 # TODO: NOCYCLE parameter doesn't work
57 my $sqla_oracle = DBIx::Class::SQLMaker::Oracle->new( quote_char => '"', name_sep => '.' );
58 isa_ok($sqla_oracle, 'DBIx::Class::SQLMaker::Oracle');
61 for my $case (@handle_tests) {
63 my $msg = sprintf("Offline: %s",
64 $case->{msg} || substr($case->{stmt},0,25),
68 ( $stmt, @bind ) = $sqla_oracle->_recurse_where( $case->{connect_by} );
69 is_same_sql_bind( $stmt, \@bind, $case->{stmt}, $case->{bind},$msg )
70 || diag "Search term:\n" . Dumper $case->{connect_by};
72 ,sprintf("lives is ok from '%s'",$msg));
76 $sqla_oracle->_shorten_identifier('short_id'),
78 '_shorten_identifier for short id without keywords ok'
82 $sqla_oracle->_shorten_identifier('short_id', [qw/ foo /]),
84 '_shorten_identifier for short id with one keyword ok'
88 $sqla_oracle->_shorten_identifier('short_id', [qw/ foo bar baz /]),
90 '_shorten_identifier for short id with keywords ok'
94 $sqla_oracle->_shorten_identifier('very_long_identifier_which_exceeds_the_30char_limit'),
95 'VryLngIdntfrWhchExc_72M8CIDTM7',
96 '_shorten_identifier without keywords ok',
100 $sqla_oracle->_shorten_identifier('very_long_identifier_which_exceeds_the_30char_limit',[qw/ foo /]),
101 'Foo_72M8CIDTM7KBAUPXG48B22P4E',
102 '_shorten_identifier with one keyword ok',
105 $sqla_oracle->_shorten_identifier('very_long_identifier_which_exceeds_the_30char_limit',[qw/ foo bar baz /]),
106 'FooBarBaz_72M8CIDTM7KBAUPXG48B',
107 '_shorten_identifier with keywords ok',
110 # test SQL generation for INSERT ... RETURNING
112 sub UREF { \do { my $x } };
114 $sqla_oracle->{bindtype} = 'columns';
116 for my $q ('', '"') {
117 local $sqla_oracle->{quote_char} = $q;
119 my ($sql, @bind) = $sqla_oracle->insert(
122 'name' => 'Testartist',
125 'returning' => 'artistid',
126 'returning_container' => [],
132 "INSERT INTO ${q}artist${q} (${q}name${q}) VALUES (?) RETURNING ${q}artistid${q} INTO ?",
133 [ [ name => 'Testartist' ], [ artistid => UREF ] ],
134 'sql_maker generates insert returning for one column'
137 ($sql, @bind) = $sqla_oracle->insert(
140 'name' => 'Testartist',
143 'returning' => \'artistid',
144 'returning_container' => [],
150 "INSERT INTO ${q}artist${q} (${q}name${q}) VALUES (?) RETURNING artistid INTO ?",
151 [ [ name => 'Testartist' ], [ artistid => UREF ] ],
152 'sql_maker generates insert returning for one column'
156 ($sql, @bind) = $sqla_oracle->insert(
157 'computed_column_test',
159 'a_timestamp' => '2010-05-26 18:22:00',
162 'returning' => [ 'id', 'a_computed_column', 'charfield' ],
163 'returning_container' => [],
169 "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 ?, ?, ?",
170 [ [ a_timestamp => '2010-05-26 18:22:00' ], [ id => UREF ], [ a_computed_column => UREF ], [ charfield => UREF ] ],
171 'sql_maker generates insert returning for multiple columns'