File: 81-examples_procedural.t

package info (click to toggle)
libsql-splitstatement-perl 1.00023-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 484 kB
  • sloc: perl: 3,384; sql: 1,478; makefile: 2
file content (51 lines) | stat: -rw-r--r-- 1,020 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
#!/usr/bin/env perl

use strict;
use warnings;

use SQL::SplitStatement;

use Test::More tests => 2;

my $sql_code = <<'SQL';
CREATE TABLE sqr_root_sum (num NUMBER, sq_root NUMBER(6,2),
                           sqr NUMBER, sum_sqrs NUMBER);
DECLARE
   s  PLS_INTEGER; -- inline sql comment
   s2 PLS_INTEGER;
   /* Multiline
   C-style
   comment */
BEGIN
  s2 := 10*i;
  FOR i in 1..100 LOOP
    s := (i * (i + 1) * (2*i +1)) / 6; -- sum of squares
    INSERT INTO sqr_root_sum VALUES (i, SQRT(i), i*i, s*s2);
  END LOOP;
END;
/
DROP TABLE sqr_root_sum
SQL

my $splitter;
my @statements;

$splitter = SQL::SplitStatement->new;
@statements = $splitter->split($sql_code);

cmp_ok (
    scalar(@statements), '==', 3,
    'Correct number of atomic statements'
);

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

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

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