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, $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 $sql_line = $column->line;
41 $sql_line = $column->line($datasrc);
45 DBIx::DBSchema::Column objects represent columns in tables (see
46 L<DBIx::DBSchema::Table>).
52 =item new [ NAME [ , SQL_TYPE [ , NULL [ , LENGTH [ , LOCAL ] ] ] ] ]
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.
62 my($proto,$name,$type,$null,$length,$local)=@_;
64 #croak "Illegal name: $name" if grep $name eq $_, @reserved_words;
66 $null =~ s/^NOT NULL$//i;
67 $null = 'NULL' if $null;
69 my $class = ref($proto) || $proto;
78 bless ($self, $class);
84 Returns or sets the column name.
90 if ( defined($value) ) {
91 #croak "Illegal name: $name" if grep $name eq $_, @reserved_words;
92 $self->{'name'} = $value;
100 Returns or sets the column type.
106 if ( defined($value) ) {
107 $self->{'type'} = $value;
115 Returns or sets the column null flag (the empty string is equivalent to
122 if ( defined($value) ) {
123 $value =~ s/^NOT NULL$//i;
124 $value = 'NULL' if $value;
125 $self->{'null'} = $value;
131 =item length [ LENGTH ]
133 Returns or sets the column length.
139 if ( defined($value) ) {
140 $self->{'length'} = $value;
146 =item local [ LOCAL ]
148 Returns or sets the database-specific field.
154 if ( defined($value) ) {
155 $self->{'local'} = $value;
161 =item line [ $datasrc ]
163 Returns an SQL column definition.
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.
173 my($self,$datasrc)=@_;
174 my($null)=$self->null;
175 if ( $datasrc =~ /^dbi:mysql:/i ) { #yucky mysql hack
178 if ( $datasrc =~ /^dbi:pg/i ) { #yucky Pg hack
179 $null ||= "NOT NULL";
184 $self->type. ( $self->length ? '('.$self->length.')' : '' ),
186 ( ( $datasrc =~ /^dbi:mysql:/i )
197 Ivan Kohler <ivan-dbix-dbschema@420.am>
201 Copyright (c) 2000 Ivan Kohler
202 Copyright (c) 2000 Mail Abuse Prevention System LLC
204 This program is free software; you can redistribute it and/or modify it under
205 the same terms as Perl itself.
209 line() has database-specific foo that probably ought to be abstracted into
210 the DBIx::DBSchema:DBD:: modules.
214 L<DBIx::DBSchema::Table>, L<DBIx::DBSchema>, L<DBIx::DBSchema::DBD>, L<DBI>