added typemap foo and default values
[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, $default, $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   $default = $column->default;
41   $column->default( 'Roo' );
42
43   $sql_line = $column->line;
44   $sql_line = $column->line($datasrc);
45
46 =head1 DESCRIPTION
47
48 DBIx::DBSchema::Column objects represent columns in tables (see
49 L<DBIx::DBSchema::Table>).
50
51 =head1 METHODS
52
53 =over 4
54
55 =item new [ NAME [ , SQL_TYPE [ , NULL [ , LENGTH  [ , DEFAULT [ , LOCAL ] ] ] ] ] ]
56
57 Creates a new DBIx::DBSchema::Column object.  NAME is the name of the column.
58 SQL_TYPE is the SQL data type.  NULL is the nullability of the column (the
59 empty string is equivalent to `NOT NULL').  LENGTH is the SQL length of the
60 column.  DEFAULT is the default value of the column.  LOCAL is reserved for
61 database-specific information.
62
63 =cut
64
65 sub new {
66   my($proto,$name,$type,$null,$length,$default,$local)=@_;
67
68   #croak "Illegal name: $name" if grep $name eq $_, @reserved_words;
69
70   $null =~ s/^NOT NULL$//i;
71   $null = 'NULL' if $null;
72
73   my $class = ref($proto) || $proto;
74   my $self = {
75     'name'    => $name,
76     'type'    => $type,
77     'null'    => $null,
78     'length'  => $length,
79     'default' => $default,
80     'local'   => $local,
81   };
82
83   bless ($self, $class);
84
85 }
86
87 =item name [ NAME ]
88
89 Returns or sets the column name.
90
91 =cut
92
93 sub name {
94   my($self,$value)=@_;
95   if ( defined($value) ) {
96   #croak "Illegal name: $name" if grep $name eq $_, @reserved_words;
97     $self->{'name'} = $value;
98   } else {
99     $self->{'name'};
100   }
101 }
102
103 =item type [ TYPE ]
104
105 Returns or sets the column type.
106
107 =cut
108
109 sub type {
110   my($self,$value)=@_;
111   if ( defined($value) ) {
112     $self->{'type'} = $value;
113   } else {
114     $self->{'type'};
115   }
116 }
117
118 =item null [ NULL ]
119
120 Returns or sets the column null flag (the empty string is equivalent to
121 `NOT NULL')
122
123 =cut
124
125 sub null {
126   my($self,$value)=@_;
127   if ( defined($value) ) {
128     $value =~ s/^NOT NULL$//i;
129     $value = 'NULL' if $value;
130     $self->{'null'} = $value;
131   } else {
132     $self->{'null'};
133   }
134 }
135
136 =item length [ LENGTH ]
137
138 Returns or sets the column length.
139
140 =cut
141
142 sub length {
143   my($self,$value)=@_;
144   if ( defined($value) ) {
145     $self->{'length'} = $value;
146   } else {
147     $self->{'length'};
148   }
149 }
150
151 =item default [ LOCAL ]
152
153 Returns or sets the default value.
154
155 =cut
156
157 sub default {
158   my($self,$value)=@_;
159   if ( defined($value) ) {
160     $self->{'default'} = $value;
161   } else {
162     $self->{'default'};
163   }
164 }
165
166
167 =item local [ LOCAL ]
168
169 Returns or sets the database-specific field.
170
171 =cut
172
173 sub local {
174   my($self,$value)=@_;
175   if ( defined($value) ) {
176     $self->{'local'} = $value;
177   } else {
178     $self->{'local'};
179   }
180 }
181
182 =item line [ $datasrc ]
183
184 Returns an SQL column definition.
185
186 If passed a DBI data source such as `DBI:mysql:database' or
187 `DBI:Pg:dbname=database', will use syntax specific to that database engine.
188 Currently supported databases are MySQL and PostgreSQL.  Non-standard syntax
189 for other engines (if applicable) may also be supported in the future.
190
191 =cut
192
193 sub line {
194   my($self,$datasrc)=@_;
195   
196   my $driver = DBIx::DBSchema::_load_driver($datasrc);
197   my %typemap = eval "\%DBIx::DBSchema::DBD::${driver}::typemap";
198   my $type = defined( $typemap{uc($self->type)} )
199     ? $typemap{uc($self->type)}
200     : $self->type;
201
202   my($null)=$self->null;
203
204   if ( $datasrc =~ /^dbi:mysql:/i ) { #yucky mysql hack
205     $null ||= "NOT NULL"
206   }
207   if ( $datasrc =~ /^dbi:pg/i ) { #yucky Pg hack
208     $null ||= "NOT NULL";
209     $null =~ s/^NULL$//;
210   }
211
212   join(' ',
213     $self->name,
214     $type. ( $self->length ? '('.$self->length.')' : '' ),
215     $null,
216     ( ( defined($self->default) && $self->default ne '' )
217       ? 'DEFAULT '. $self->default
218       : ''
219     ),
220     ( ( $datasrc =~ /^dbi:mysql:/i )
221       ? $self->local
222       : ''
223     ),
224   );
225
226 }
227
228 =back
229
230 =head1 AUTHOR
231
232 Ivan Kohler <ivan-dbix-dbschema@420.am>
233
234 =head1 COPYRIGHT
235
236 Copyright (c) 2000 Ivan Kohler
237 Copyright (c) 2000 Mail Abuse Prevention System LLC
238 All rights reserved.
239 This program is free software; you can redistribute it and/or modify it under
240 the same terms as Perl itself.
241
242 =head1 BUGS
243
244 line() has database-specific foo that probably ought to be abstracted into
245 the DBIx::DBSchema:DBD:: modules.
246
247 =head1 SEE ALSO
248
249 L<DBIx::DBSchema::Table>, L<DBIx::DBSchema>, L<DBIx::DBSchema::DBD>, L<DBI>
250
251 =cut
252
253 1;
254