summaryrefslogtreecommitdiff
path: root/site_perl/dbdef_column.pm
blob: dc07305b8aae48e458239d6598f1878d000d1d2a (plain)
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package FS::dbdef_column;

use strict;
#use Carp;
use Exporter;
use vars qw(@ISA);

@ISA = qw(Exporter);

=head1 NAME

FS::dbdef_column - Column object

=head1 SYNOPSIS

  use FS::dbdef_column;

  $column_object = new FS::dbdef_column ( $name, $sql_type, '' );
  $column_object = new FS::dbdef_column ( $name, $sql_type, 'NULL' );
  $column_object = new FS::dbdef_column ( $name, $sql_type, '', $length );
  $column_object = new FS::dbdef_column ( $name, $sql_type, 'NULL', $length );

  $name = $column_object->name;
  $column_object->name ( 'name' );

  $name = $column_object->type;
  $column_object->name ( 'sql_type' );

  $name = $column_object->null;
  $column_object->name ( 'NOT NULL' );

  $name = $column_object->length;
  $column_object->name ( $length );

  $sql_line = $column->line;
  $sql_line = $column->line $datasrc;

=head1 DESCRIPTION

FS::dbdef::column objects represend columns in tables (see L<FS::dbdef_table>).

=head1 METHODS

=over 4

=item new

Creates a new FS::dbdef_column object.

=cut

sub new {
  my($proto,$name,$type,$null,$length)=@_;

  #croak "Illegal name: $name" if grep $name eq $_, @reserved_words;

  $null =~ s/^NOT NULL$//i;

  my $class = ref($proto) || $proto;
  my $self = {
    'name'   => $name,
    'type'   => $type,
    'null'   => $null,
    'length' => $length,
  };

  bless ($self, $class);

}

=item name

Returns or sets the column name.

=cut

sub name {
  my($self,$value)=@_;
  if ( defined($value) ) {
  #croak "Illegal name: $name" if grep $name eq $_, @reserved_words;
    $self->{'name'} = $value;
  } else {
    $self->{'name'};
  }
}

=item type

Returns or sets the column type.

=cut

sub type {
  my($self,$value)=@_;
  if ( defined($value) ) {
    $self->{'type'} = $value;
  } else {
    $self->{'type'};
  }
}

=item null

Returns or sets the column null flag.

=cut

sub null {
  my($self,$value)=@_;
  if ( defined($value) ) {
    $value =~ s/^NOT NULL$//i;
    $self->{'null'} = $value;
  } else {
    $self->{'null'};
  }
}

=item type

Returns or sets the column length.

=cut

sub length {
  my($self,$value)=@_;
  if ( defined($value) ) {
    $self->{'length'} = $value;
  } else {
    $self->{'length'};
  }
}

=item line [ $datasrc ]

Returns an SQL column definition.

If passed a DBI $datasrc specifying L<DBD::mysql> or L<DBD::Pg>, will use
engine-specific syntax.

=cut

sub line {
  my($self,$datasrc)=@_;
  my($null)=$self->null;
  if ( $datasrc =~ /mysql/ ) { #yucky mysql hack
    $null ||= "NOT NULL"
  }
  if ( $datasrc =~ /Pg/ ) { #yucky Pg hack
    $null ||= "NOT NULL";
    $null =~ s/^NULL$//;
  }
  join(' ',
    $self->name,
    $self->type. ( $self->length ? '('.$self->length.')' : '' ),
    $null,
  );
}

=back

=head1 BUGS

=head1 SEE ALSO

L<FS::dbdef_table>, L<FS::dbdef>, L<DBI>

=head1 VERSION

$Id: dbdef_column.pm,v 1.3 1998-10-13 13:04:17 ivan Exp $

=head1 HISTORY

class for dealing with column definitions

ivan@sisd.com 98-apr-17

now methods can be used to get or set data ivan@sisd.com 98-may-11

mySQL-specific hack for null (what should be default?) ivan@sisd.com 98-jun-2

$Log: dbdef_column.pm,v $
Revision 1.3  1998-10-13 13:04:17  ivan
fixed doc to indicate Pg specific syntax too

Revision 1.2  1998/10/12 23:40:28  ivan
added Pg-specific behaviour in sub line


=cut

1;