File: DB2.pm

package info (click to toggle)
libparse-dia-sql-perl 0.31-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 924 kB
  • sloc: perl: 3,865; makefile: 2
file content (144 lines) | stat: -rw-r--r-- 2,826 bytes parent folder | download | duplicates (2)
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
package Parse::Dia::SQL::Output::DB2;

# $Id: DB2.pm,v 1.5 2009/03/13 14:20:26 aff Exp $

=pod

=head1 NAME

Parse::Dia::SQL::Output::DB2 - Create SQL for DB2.

=head1 SYNOPSIS

    use Parse::Dia::SQL;
    my $dia = Parse::Dia::SQL->new(...);
    print $dia->get_sql();

=head1 DESCRIPTION

This class creates SQL for the IBM DB2 database.

=cut


use warnings;
use strict;

use Data::Dumper;
use File::Spec::Functions qw(catfile);

use lib q{lib};
use base q{Parse::Dia::SQL::Output}; # extends

require Parse::Dia::SQL::Logger;
require Parse::Dia::SQL::Const;

=head2 new

The constructor.  Arguments:

=cut

sub new {
  my ( $class, %param ) = @_;
  my $self = {};

  # Set defaults for db2
  $param{object_name_max_length} = $param{object_name_max_length} || 18;
  $param{index_options} = ['allow reverse scans'] unless 
    defined($param{index_options}) && scalar(@{$param{index_options}});
  $param{db} = q{db2}; 

  $self = $class->SUPER::new(%param);

  bless( $self, $class );
  return $self;
}

=head2

Create primary key clause, e.g.

constraint pk_<tablename> primary key (<column1>,..,<columnN>)

For DB2 the PK must be 18 characters or less

Returns undefined if list of primary key is empty (i.e. if there are no
primary keys on given table).

=cut


sub _create_pk_string {
  my ( $self, $tablename, @pks ) = @_;

  if ( !$tablename ) {
    $self->{log}
      ->error(q{Missing argument tablename - cannot create pk string!});
    return;
  }
  if ( scalar(@pks) == 0 ) {
    $self->{log}->debug(qq{table '$tablename' has no primary keys});
    return;
  }

  # old school name length reduction
  $tablename =
    $self->{utils}
    ->mangle_name( $tablename, $self->{object_name_max_length} - 4 );

  # new school name length reduction
  #  $tablename = $self->{utils}->make_name ($tablename);

  $self->{log}->debug( qq{tablename: '$tablename' pks: } . join( q{,}, @pks ) );

  return qq{constraint pk_$tablename primary key (} . join( q{,}, @pks ) . q{)};
}

=head2

For DB2 a constraint name must be 18 characters or less. 

Returns shortened tablename.

=cut

sub _create_constraint_name {
  my ( $self, $constraint_name ) = @_;

  if ( !defined($constraint_name) ||  $constraint_name eq q{} ) {
	$self->{log}->error( qq{constraint_name was undefined or empty!});
	return;
  }

  # new school
  return $self->{utils}->make_name (0, $constraint_name);

  # old school 
  #  return $self->{utils}->mangle_name( $constraint_name, $self->{object_name_max_length} - 4 );
}

=head2 _get_drop_schema_sql

DB2 do not support keyword 'if exists' in 'drop table' statement

=cut

sub _get_drop_schema_sql {
  my ($self, $tablename) = @_;

  return
      qq{drop table $tablename}
    . $self->{end_of_statement}
    . $self->{newline};
}

1;

__END__

=pod

Subclass for outputting SQL for the DB2 database

=cut