File: 72-proc_plsql_nested_functions.t

package info (click to toggle)
libsql-splitstatement-perl 1.00020-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 456 kB
  • sloc: perl: 3,231; sql: 1,478; makefile: 2
file content (169 lines) | stat: -rw-r--r-- 3,978 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env perl

use strict;
use warnings;

use SQL::SplitStatement;

use Test::More tests => 5;

my $sql_code;
my $splitter;
my @statements;
my @endings;

$sql_code = <<'SQL';
CREATE OR REPLACE FUNCTION nested(some_date DATE) RETURN VARCHAR2 IS
 yrstr VARCHAR2(4);
 
-- beginning of nested function in declaration section 
FUNCTION turn_around (
  year_string VARCHAR2)
  RETURN VARCHAR2
IS
 
BEGIN
  yrstr := TO_CHAR(TO_NUMBER(year_string)*2);
  RETURN yrstr;
END;
-- end of nested function in declaration section
 
-- beginning of named function
BEGIN
  yrstr := TO_CHAR(some_date, 'YYYY');
  yrstr := turn_around(yrstr);
  RETURN yrstr; 
END nested;

CREATE PaCkaGe BODY emp_actions AS  -- body
   CURSOR desc_salary RETURN EmpRecTyp IS
      SELECT empno, sal FROM emp ORDER BY sal DESC;
   PROCEDURE hire_employee (
      ename  VARCHAR2,
      job    VARCHAR2,
      mgr    NUMBER,
      sal    NUMBER,
      comm   NUMBER,
      deptno NUMBER) IS
   BEGIN
      INSERT INTO emp VALUES (empno_seq.NEXTVAL, ename, job,
         mgr, SYSDATE, sal, comm, deptno);
   END hire_employee;

   PROCEDURE fire_employee (emp_id NUMBER) IS
   BEGIN
      DELETE FROM emp WHERE empno = emp_id;
   END fire_employee;
   
   FUNCTION nested(some_date DATE) RETURN VARCHAR2 IS
        yrstr VARCHAR2(4);
         
        -- beginning of nested function in declaration section 
        FUNCTION turn_around (
          year_string VARCHAR2)
          RETURN VARCHAR2
        IS
         
        BEGIN
          yrstr := TO_CHAR(TO_NUMBER(year_string)*2);
          RETURN yrstr;
        END;
        -- end of nested function in declaration section
         
        -- beginning of named function
        BEGIN
          yrstr := TO_CHAR(some_date, 'YYYY');
          yrstr := turn_around(yrstr);
          RETURN yrstr; 
   END nested;

END;


CREATE PACKAGE BODY emp_actions2 AS
     number_hired INT;  -- visible only in this package

     /* Fully define subprograms specified in package. */
     FUNCTION hire_employee (
        ename  VARCHAR2,
        job    VARCHAR2,
        mgr    REAL,
        sal    REAL,
        comm   REAL,
        deptno REAL) RETURN INT IS
        new_empno INT;
     BEGIN
        SELECT empno_seq.NEXTVAL INTO new_empno 
        FROM dual;
        INSERT INTO emp VALUES (new_empno, ename, job,
           mgr, SYSDATE, sal, comm, deptno);
        number_hired := number_hired + 1;
        RETURN new_empno;
     END hire_employee;

   FUNCTION nested(some_date DATE) RETURN VARCHAR2 IS
        yrstr VARCHAR2(4);
         
        -- beginning of nested function in declaration section 
        FUNCTION turn_around (
          year_string VARCHAR2)
          RETURN VARCHAR2
        IS
         
        BEGIN
          yrstr := TO_CHAR(TO_NUMBER(year_string)*2);
          RETURN yrstr;
        END;
        -- end of nested function in declaration section
         
        -- beginning of named function
        BEGIN
          yrstr := TO_CHAR(some_date, 'YYYY');
          yrstr := turn_around(yrstr);
          RETURN yrstr; 
   END nested;

BEGIN  -- initialization part starts here
     INSERT INTO emp_audit 
     VALUES (SYSDATE, USER, 'EMP_ACTIONS');
     number_hired := 0;
END emp_actions2;

SQL

$splitter = SQL::SplitStatement->new;

@statements = $splitter->split( $sql_code );

cmp_ok(
    @statements, '==', 3,
    'Statements split'
);

$splitter->keep_extra_spaces(1);
$splitter->keep_empty_statements(1);
$splitter->keep_terminators(1);
$splitter->keep_comments(1);

@statements = $splitter->split( $sql_code );

is(
    join( '', @statements ), $sql_code,
    'SQL code rebuilt'
);

@endings = qw|
    nested
    END
    emp_actions2
|;

$splitter->keep_extra_spaces(0);
$splitter->keep_empty_statements(0);
$splitter->keep_terminators(0);
$splitter->keep_comments(0);
@statements = $splitter->split( $sql_code );

like( $statements[$_], qr/\Q$endings[$_]\E$/, 'Statement ' . ($_+1) . ' check' )
    for 0..$#endings;