bugfixes
[DBIx-DBSchema.git] / DBSchema / Column.pm
1 package DBIx::DBSchema::Column;
2
3 use strict;
4 use vars qw(@ISA);
5 #use Carp;
6 #use Exporter;
7
8 #@ISA = qw(Exporter);
9 @ISA = qw();
10
11 =head1 NAME
12
13 DBIx::DBSchema::Column - Column objects
14
15 =head1 SYNOPSIS
16
17   use DBIx::DBSchema::Column;
18
19   $column = new DBIx::DBSchema::Column ( $name, $sql_type, '' );
20   $column = new DBIx::DBSchema::Column ( $name, $sql_type, 'NULL' );
21   $column = new DBIx::DBSchema::Column ( $name, $sql_type, '', $length );
22   $column = new DBIx::DBSchema::Column ( $name, $sql_type, 'NULL', $length );
23   $column = new DBIx::DBSchema::Column ( $name, $sql_type, 'NULL', $length, $local );
24
25   $name = $column->name;
26   $column->name( 'name' );
27
28   $sql_type = $column->type;
29   $column->sql_type( 'sql_type' );
30
31   $null = $column->null;
32   $column->null( 'NULL' );
33   $column->null( 'NOT NULL' );
34   $column->null( '' );
35
36   $length = $column->length;
37   $column->length( '10' );
38   $column->length( '8,2' );
39
40   $sql_line = $column->line;
41   $sql_line = $column->line($datasrc);
42
43 =head1 DESCRIPTION
44
45 DBIx::DBSchema::Column objects represent columns in tables (see
46 L<DBIx::DBSchema::Table>).
47
48 =head1 METHODS
49
50 =over 4
51
52 =item new [ NAME [ , SQL_TYPE [ , NULL [ , LENGTH  [ , LOCAL ] ] ] ] ]
53
54 Creates a new DBIx::DBSchema::Column object.  NAME is the name of the column.
55 SQL_TYPE is the SQL data type.  NULL is the nullability of the column (the
56 empty string is equivalent to `NOT NULL').  LENGTH is the SQL length of the
57 column.  LOCAL is reserved for database-specific information.
58
59 =cut
60
61 sub new {
62   my($proto,$name,$type,$null,$length,$local)=@_;
63
64   #croak "Illegal name: $name" if grep $name eq $_, @reserved_words;
65
66   $null =~ s/^NOT NULL$//i;
67   $null = 'NULL' if $null;
68
69   my $class = ref($proto) || $proto;
70   my $self = {
71     'name'   => $name,
72     'type'   => $type,
73     'null'   => $null,
74     'length' => $length,
75     'local'  => $local,
76   };
77
78   bless ($self, $class);
79
80 }
81
82 =item name [ NAME ]
83
84 Returns or sets the column name.
85
86 =cut
87
88 sub name {
89   my($self,$value)=@_;
90   if ( defined($value) ) {
91   #croak "Illegal name: $name" if grep $name eq $_, @reserved_words;
92     $self->{'name'} = $value;
93   } else {
94     $self->{'name'};
95   }
96 }
97
98 =item type [ TYPE ]
99
100 Returns or sets the column type.
101
102 =cut
103
104 sub type {
105   my($self,$value)=@_;
106   if ( defined($value) ) {
107     $self->{'type'} = $value;
108   } else {
109     $self->{'type'};
110   }
111 }
112
113 =item null [ NULL ]
114
115 Returns or sets the column null flag (the empty string is equivalent to
116 `NOT NULL')
117
118 =cut
119
120 sub null {
121   my($self,$value)=@_;
122   if ( defined($value) ) {
123     $value =~ s/^NOT NULL$//i;
124     $value = 'NULL' if $value;
125     $self->{'null'} = $value;
126   } else {
127     $self->{'null'};
128   }
129 }
130
131 =item length [ LENGTH ]
132
133 Returns or sets the column length.
134
135 =cut
136
137 sub length {
138   my($self,$value)=@_;
139   if ( defined($value) ) {
140     $self->{'length'} = $value;
141   } else {
142     $self->{'length'};
143   }
144 }
145
146 =item local [ LOCAL ]
147
148 Returns or sets the database-specific field.
149
150 =cut
151
152 sub local {
153   my($self,$value)=@_;
154   if ( defined($value) ) {
155     $self->{'local'} = $value;
156   } else {
157     $self->{'local'};
158   }
159 }
160
161 =item line [ $datasrc ]
162
163 Returns an SQL column definition.
164
165 If passed a DBI data source such as `DBI:mysql:database' or
166 `DBI:Pg:dbname=database', will use syntax specific to that database engine.
167 Currently supported databases are MySQL and PostgreSQL.  Non-standard syntax
168 for other engines (if applicable) may also be supported in the future.
169
170 =cut
171
172 sub line {
173   my($self,$datasrc)=@_;
174   my($null)=$self->null;
175   if ( $datasrc =~ /^dbi:mysql:/i ) { #yucky mysql hack
176     $null ||= "NOT NULL"
177   }
178   if ( $datasrc =~ /^dbi:pg/i ) { #yucky Pg hack
179     $null ||= "NOT NULL";
180     $null =~ s/^NULL$//;
181   }
182   join(' ',
183     $self->name,
184     $self->type. ( $self->length ? '('.$self->length.')' : '' ),
185     $null,
186     ( ( $datasrc =~ /^dbi:mysql:/i )
187       ? $self->local
188       : ''
189     ),
190   );
191 }
192
193 =back
194
195 =head1 AUTHOR
196
197 Ivan Kohler <ivan-dbix-dbschema@420.am>
198
199 =head1 COPYRIGHT
200
201 Copyright (c) 2000 Ivan Kohler
202 Copyright (c) 2000 Mail Abuse Prevention System LLC
203 All rights reserved.
204 This program is free software; you can redistribute it and/or modify it under
205 the same terms as Perl itself.
206
207 =head1 BUGS
208
209 line() has database-specific foo that probably ought to be abstracted into
210 the DBIx::DBSchema:DBD:: modules.
211
212 =head1 SEE ALSO
213
214 L<DBIx::DBSchema::Table>, L<DBIx::DBSchema>, L<DBIx::DBSchema::DBD>, L<DBI>
215
216 =cut
217
218 1;
219