1 package DBIx::DBSchema::Column;
13 DBIx::DBSchema::Column - Column objects
17 use DBIx::DBSchema::Column;
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 );
25 $name = $column->name;
26 $column->name( 'name' );
28 $sql_type = $column->type;
29 $column->sql_type( 'sql_type' );
31 $null = $column->null;
32 $column->null( 'NULL' );
33 $column->null( 'NOT NULL' );
36 $length = $column->length;
37 $column->length( '10' );
38 $column->length( '8,2' );
40 $default = $column->default;
41 $column->default( 'Roo' );
43 $sql_line = $column->line;
44 $sql_line = $column->line($datasrc);
48 DBIx::DBSchema::Column objects represent columns in tables (see
49 L<DBIx::DBSchema::Table>).
55 =item new [ NAME [ , SQL_TYPE [ , NULL [ , LENGTH [ , DEFAULT [ , LOCAL ] ] ] ] ] ]
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.
66 my($proto,$name,$type,$null,$length,$default,$local)=@_;
68 #croak "Illegal name: $name" if grep $name eq $_, @reserved_words;
70 $null =~ s/^NOT NULL$//i;
71 $null = 'NULL' if $null;
73 my $class = ref($proto) || $proto;
79 'default' => $default,
83 bless ($self, $class);
89 Returns or sets the column name.
95 if ( defined($value) ) {
96 #croak "Illegal name: $name" if grep $name eq $_, @reserved_words;
97 $self->{'name'} = $value;
105 Returns or sets the column type.
111 if ( defined($value) ) {
112 $self->{'type'} = $value;
120 Returns or sets the column null flag (the empty string is equivalent to
127 if ( defined($value) ) {
128 $value =~ s/^NOT NULL$//i;
129 $value = 'NULL' if $value;
130 $self->{'null'} = $value;
136 =item length [ LENGTH ]
138 Returns or sets the column length.
144 if ( defined($value) ) {
145 $self->{'length'} = $value;
151 =item default [ LOCAL ]
153 Returns or sets the default value.
159 if ( defined($value) ) {
160 $self->{'default'} = $value;
167 =item local [ LOCAL ]
169 Returns or sets the database-specific field.
175 if ( defined($value) ) {
176 $self->{'local'} = $value;
182 =item line [ $datasrc ]
184 Returns an SQL column definition.
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.
194 my($self,$datasrc)=@_;
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)}
202 my($null)=$self->null;
204 if ( $datasrc =~ /^dbi:mysql:/i ) { #yucky mysql hack
207 if ( $datasrc =~ /^dbi:pg/i ) { #yucky Pg hack
208 $null ||= "NOT NULL";
214 $type. ( $self->length ? '('.$self->length.')' : '' ),
216 ( ( defined($self->default) && $self->default ne '' )
217 ? 'DEFAULT '. $self->default
220 ( ( $datasrc =~ /^dbi:mysql:/i )
232 Ivan Kohler <ivan-dbix-dbschema@420.am>
236 Copyright (c) 2000 Ivan Kohler
237 Copyright (c) 2000 Mail Abuse Prevention System LLC
239 This program is free software; you can redistribute it and/or modify it under
240 the same terms as Perl itself.
244 line() has database-specific foo that probably ought to be abstracted into
245 the DBIx::DBSchema:DBD:: modules.
249 L<DBIx::DBSchema::Table>, L<DBIx::DBSchema>, L<DBIx::DBSchema::DBD>, L<DBI>