1 package DBIx::DBSchema::Column;
13 DBIx::DBSchema::Column - Column objects
17 use DBIx::DBSchema::Column;
19 #named params with a hashref (preferred)
20 $column = new DBIx::DBSchema::Column ( {
21 'name' => 'column_name',
30 $column = new DBIx::DBSchema::Column ( $name, $sql_type, $nullability, $length, $default, $local );
32 $name = $column->name;
33 $column->name( 'name' );
35 $sql_type = $column->type;
36 $column->sql_type( 'sql_type' );
38 $null = $column->null;
39 $column->null( 'NULL' );
40 $column->null( 'NOT NULL' );
43 $length = $column->length;
44 $column->length( '10' );
45 $column->length( '8,2' );
47 $default = $column->default;
48 $column->default( 'Roo' );
50 $sql_line = $column->line;
51 $sql_line = $column->line($datasrc);
55 DBIx::DBSchema::Column objects represent columns in tables (see
56 L<DBIx::DBSchema::Table>).
64 =item new [ name [ , type [ , null [ , length [ , default [ , local ] ] ] ] ] ]
66 Creates a new DBIx::DBSchema::Column object. Takes a hashref of named
67 parameters, or a list. B<name> is the name of the column. B<type> is the SQL
68 data type. B<null> is the nullability of the column (intrepreted using Perl's
69 rules for truth, with one exception: `NOT NULL' is false). B<length> is the
70 SQL length of the column. B<default> is the default value of the column.
71 B<local> is reserved for database-specific information.
77 my $class = ref($proto) || $proto;
83 $self = { map { $_ => shift } qw(name type null length default local) };
86 #croak "Illegal name: ". $self->{'name'}
87 # if grep $self->{'name'} eq $_, @reserved_words;
89 $self->{'null'} =~ s/^NOT NULL$//i;
90 $self->{'null'} = 'NULL' if $self->{'null'};
92 bless ($self, $class);
98 Returns or sets the column name.
104 if ( defined($value) ) {
105 #croak "Illegal name: $name" if grep $name eq $_, @reserved_words;
106 $self->{'name'} = $value;
114 Returns or sets the column type.
120 if ( defined($value) ) {
121 $self->{'type'} = $value;
129 Returns or sets the column null flag (the empty string is equivalent to
136 if ( defined($value) ) {
137 $value =~ s/^NOT NULL$//i;
138 $value = 'NULL' if $value;
139 $self->{'null'} = $value;
145 =item length [ LENGTH ]
147 Returns or sets the column length.
153 if ( defined($value) ) {
154 $self->{'length'} = $value;
160 =item default [ LOCAL ]
162 Returns or sets the default value.
168 if ( defined($value) ) {
169 $self->{'default'} = $value;
176 =item local [ LOCAL ]
178 Returns or sets the database-specific field.
184 if ( defined($value) ) {
185 $self->{'local'} = $value;
191 =item line [ DATABASE_HANDLE | DATA_SOURCE [ USERNAME PASSWORD [ ATTR ] ] ]
193 Returns an SQL column definition.
195 The data source can be specified by passing an open DBI database handle, or by
196 passing the DBI data source name, username and password.
198 Although the username and password are optional, it is best to call this method
199 with a database handle or data source including a valid username and password -
200 a DBI connection will be opened and the quoting and type mapping will be more
203 If passed a DBI data source (or handle) such as `DBI:mysql:database' or
204 `DBI:Pg:dbname=database', will use syntax specific to that database engine.
205 Currently supported databases are MySQL and PostgreSQL. Non-standard syntax
206 for other engines (if applicable) may also be supported in the future.
213 my $driver = DBIx::DBSchema::_load_driver($dbh);
215 %typemap = eval "\%DBIx::DBSchema::DBD::${driver}::typemap" if $driver;
216 my $type = defined( $typemap{uc($self->type)} )
217 ? $typemap{uc($self->type)}
220 my $null = $self->null;
222 my $default = $self->default;
223 if ( defined($default) && default ne ''
225 # false laziness: nicked from FS::Record::_quote
226 && ( $default !~ /^\-?\d+(\.\d+)?$/
227 || $type =~ /(char|binary|blob|text)$/i
230 $default = $dbh->quote($self->default);
232 $default = $self->default;
235 #this should be a callback into the driver
236 if ( $driver eq 'mysql' ) { #yucky mysql hack
238 } elsif ( $driver eq 'Pg' ) { #yucky Pg hack
239 $null ||= "NOT NULL";
245 $type. ( $self->length ? '('.$self->length.')' : '' ),
247 ( ( defined($default) && $default ne '' )
248 ? 'DEFAULT '. $default
251 ( ( $driver eq 'mysql' )
263 Ivan Kohler <ivan-dbix-dbschema@420.am>
267 Copyright (c) 2000 Ivan Kohler
268 Copyright (c) 2000 Mail Abuse Prevention System LLC
270 This program is free software; you can redistribute it and/or modify it under
271 the same terms as Perl itself.
275 line() has database-specific foo that probably ought to be abstracted into
276 the DBIx::DBSchema:DBD:: modules.
280 L<DBIx::DBSchema::Table>, L<DBIx::DBSchema>, L<DBIx::DBSchema::DBD>, L<DBI>