initial checkin of module files for proper perl installation
[freeside.git] / FS / FS / dbdef_column.pm
1 package FS::dbdef_column;
2
3 use strict;
4 #use Carp;
5 use Exporter;
6 use vars qw(@ISA);
7
8 @ISA = qw(Exporter);
9
10 =head1 NAME
11
12 FS::dbdef_column - Column object
13
14 =head1 SYNOPSIS
15
16   use FS::dbdef_column;
17
18   $column_object = new FS::dbdef_column ( $name, $sql_type, '' );
19   $column_object = new FS::dbdef_column ( $name, $sql_type, 'NULL' );
20   $column_object = new FS::dbdef_column ( $name, $sql_type, '', $length );
21   $column_object = new FS::dbdef_column ( $name, $sql_type, 'NULL', $length );
22
23   $name = $column_object->name;
24   $column_object->name ( 'name' );
25
26   $name = $column_object->type;
27   $column_object->name ( 'sql_type' );
28
29   $name = $column_object->null;
30   $column_object->name ( 'NOT NULL' );
31
32   $name = $column_object->length;
33   $column_object->name ( $length );
34
35   $sql_line = $column->line;
36   $sql_line = $column->line $datasrc;
37
38 =head1 DESCRIPTION
39
40 FS::dbdef::column objects represend columns in tables (see L<FS::dbdef_table>).
41
42 =head1 METHODS
43
44 =over 4
45
46 =item new
47
48 Creates a new FS::dbdef_column object.
49
50 =cut
51
52 sub new {
53   my($proto,$name,$type,$null,$length)=@_;
54
55   #croak "Illegal name: $name" if grep $name eq $_, @reserved_words;
56
57   $null =~ s/^NOT NULL$//i;
58
59   my $class = ref($proto) || $proto;
60   my $self = {
61     'name'   => $name,
62     'type'   => $type,
63     'null'   => $null,
64     'length' => $length,
65   };
66
67   bless ($self, $class);
68
69 }
70
71 =item name
72
73 Returns or sets the column name.
74
75 =cut
76
77 sub name {
78   my($self,$value)=@_;
79   if ( defined($value) ) {
80   #croak "Illegal name: $name" if grep $name eq $_, @reserved_words;
81     $self->{'name'} = $value;
82   } else {
83     $self->{'name'};
84   }
85 }
86
87 =item type
88
89 Returns or sets the column type.
90
91 =cut
92
93 sub type {
94   my($self,$value)=@_;
95   if ( defined($value) ) {
96     $self->{'type'} = $value;
97   } else {
98     $self->{'type'};
99   }
100 }
101
102 =item null
103
104 Returns or sets the column null flag.
105
106 =cut
107
108 sub null {
109   my($self,$value)=@_;
110   if ( defined($value) ) {
111     $value =~ s/^NOT NULL$//i;
112     $self->{'null'} = $value;
113   } else {
114     $self->{'null'};
115   }
116 }
117
118 =item type
119
120 Returns or sets the column length.
121
122 =cut
123
124 sub length {
125   my($self,$value)=@_;
126   if ( defined($value) ) {
127     $self->{'length'} = $value;
128   } else {
129     $self->{'length'};
130   }
131 }
132
133 =item line [ $datasrc ]
134
135 Returns an SQL column definition.
136
137 If passed a DBI $datasrc specifying L<DBD::mysql> or L<DBD::Pg>, will use
138 engine-specific syntax.
139
140 =cut
141
142 sub line {
143   my($self,$datasrc)=@_;
144   my($null)=$self->null;
145   if ( $datasrc =~ /mysql/ ) { #yucky mysql hack
146     $null ||= "NOT NULL"
147   }
148   if ( $datasrc =~ /Pg/ ) { #yucky Pg hack
149     $null ||= "NOT NULL";
150     $null =~ s/^NULL$//;
151   }
152   join(' ',
153     $self->name,
154     $self->type. ( $self->length ? '('.$self->length.')' : '' ),
155     $null,
156   );
157 }
158
159 =back
160
161 =head1 BUGS
162
163 =head1 SEE ALSO
164
165 L<FS::dbdef_table>, L<FS::dbdef>, L<DBI>
166
167 =head1 VERSION
168
169 $Id: dbdef_column.pm,v 1.1 1999-08-04 09:03:53 ivan Exp $
170
171 =cut
172
173 1;
174