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 );
24 #named params with a hashref (preferred)
25 $column = new DBIx::DBSchema::Column ( {
26 'name' => 'column_name',
35 $column = new DBIx::DBSchema::Column ( $name, $sql_type, $nullability, $length, $default, $local );
38 $name = $column->name;
39 $column->name( 'name' );
41 $sql_type = $column->type;
42 $column->sql_type( 'sql_type' );
44 $null = $column->null;
45 $column->null( 'NULL' );
46 $column->null( 'NOT NULL' );
49 $length = $column->length;
50 $column->length( '10' );
51 $column->length( '8,2' );
53 $default = $column->default;
54 $column->default( 'Roo' );
56 $sql_line = $column->line;
57 $sql_line = $column->line($datasrc);
61 DBIx::DBSchema::Column objects represent columns in tables (see
62 L<DBIx::DBSchema::Table>).
70 =item new [ name [ , type [ , null [ , length [ , default [ , local ] ] ] ] ] ]
72 Creates a new DBIx::DBSchema::Column object. Takes a hashref of named
73 parameters, or a list. B<name> is the name of the column. B<type> is the SQL
74 data type. B<null> is the nullability of the column (intrepreted using Perl's
75 rules for truth, with one exception: `NOT NULL' is false). B<length> is the
76 SQL length of the column. B<default> is the default value of the column.
77 B<local> is reserved for database-specific information.
83 my $class = ref($proto) || $proto;
89 $self = { map { $_ => shift } qw(name type null length default local) };
92 #croak "Illegal name: ". $self->{'name'}
93 # if grep $self->{'name'} eq $_, @reserved_words;
95 $self->{'null'} =~ s/^NOT NULL$//i;
96 $self->{'null'} = 'NULL' if $self->{'null'};
98 bless ($self, $class);
104 Returns or sets the column name.
110 if ( defined($value) ) {
111 #croak "Illegal name: $name" if grep $name eq $_, @reserved_words;
112 $self->{'name'} = $value;
120 Returns or sets the column type.
126 if ( defined($value) ) {
127 $self->{'type'} = $value;
135 Returns or sets the column null flag (the empty string is equivalent to
142 if ( defined($value) ) {
143 $value =~ s/^NOT NULL$//i;
144 $value = 'NULL' if $value;
145 $self->{'null'} = $value;
151 =item length [ LENGTH ]
153 Returns or sets the column length.
159 if ( defined($value) ) {
160 $self->{'length'} = $value;
166 =item default [ LOCAL ]
168 Returns or sets the default value.
174 if ( defined($value) ) {
175 $self->{'default'} = $value;
182 =item local [ LOCAL ]
184 Returns or sets the database-specific field.
190 if ( defined($value) ) {
191 $self->{'local'} = $value;
197 =item line [ DATABASE_HANDLE | DATA_SOURCE [ USERNAME PASSWORD [ ATTR ] ] ]
199 Returns an SQL column definition.
201 The data source can be specified by passing an open DBI database handle, or by
202 passing the DBI data source name, username and password.
204 Although the username and password are optional, it is best to call this method
205 with a database handle or data source including a valid username and password -
206 a DBI connection will be opened and the quoting and type mapping will be more
209 If passed a DBI data source (or handle) such as `DBI:mysql:database' or
210 `DBI:Pg:dbname=database', will use syntax specific to that database engine.
211 Currently supported databases are MySQL and PostgreSQL. Non-standard syntax
212 for other engines (if applicable) may also be supported in the future.
219 my $driver = DBIx::DBSchema::_load_driver($dbh);
221 %typemap = eval "\%DBIx::DBSchema::DBD::${driver}::typemap" if $driver;
222 my $type = defined( $typemap{uc($self->type)} )
223 ? $typemap{uc($self->type)}
226 my $null = $self->null;
228 my $default = $self->default;
229 if ( defined($default) && default ne ''
231 # false laziness: nicked from FS::Record::_quote
232 && ( $default !~ /^\-?\d+(\.\d+)?$/
233 || $type =~ /(char|binary|blob|text)$/i
236 $default = $dbh->quote($self->default);
238 $default = $self->default;
241 #this should be a callback into the driver
242 if ( $driver eq 'mysql' ) { #yucky mysql hack
244 } elsif ( $driver eq 'Pg' ) { #yucky Pg hack
245 $null ||= "NOT NULL";
251 $type. ( $self->length ? '('.$self->length.')' : '' ),
253 ( ( defined($default) && $default ne '' )
254 ? 'DEFAULT '. $default
257 ( ( $driver eq 'mysql' )
269 Ivan Kohler <ivan-dbix-dbschema@420.am>
273 Copyright (c) 2000 Ivan Kohler
274 Copyright (c) 2000 Mail Abuse Prevention System LLC
276 This program is free software; you can redistribute it and/or modify it under
277 the same terms as Perl itself.
281 line() has database-specific foo that probably ought to be abstracted into
282 the DBIx::DBSchema:DBD:: modules.
286 L<DBIx::DBSchema::Table>, L<DBIx::DBSchema>, L<DBIx::DBSchema::DBD>, L<DBI>