1 package DBIx::DBSchema::Column;
4 use vars qw(@ISA $VERSION);
15 DBIx::DBSchema::Column - Column objects
19 use DBIx::DBSchema::Column;
21 #named params with a hashref (preferred)
22 $column = new DBIx::DBSchema::Column ( {
23 'name' => 'column_name',
32 $column = new DBIx::DBSchema::Column ( $name, $sql_type, $nullability, $length, $default, $local );
34 $name = $column->name;
35 $column->name( 'name' );
37 $sql_type = $column->type;
38 $column->type( 'sql_type' );
40 $null = $column->null;
41 $column->null( 'NULL' );
42 $column->null( 'NOT NULL' );
45 $length = $column->length;
46 $column->length( '10' );
47 $column->length( '8,2' );
49 $default = $column->default;
50 $column->default( 'Roo' );
52 $sql_line = $column->line;
53 $sql_line = $column->line($datasrc);
57 DBIx::DBSchema::Column objects represent columns in tables (see
58 L<DBIx::DBSchema::Table>).
66 =item new [ name [ , type [ , null [ , length [ , default [ , local ] ] ] ] ] ]
68 Creates a new DBIx::DBSchema::Column object. Takes a hashref of named
69 parameters, or a list. B<name> is the name of the column. B<type> is the SQL
70 data type. B<null> is the nullability of the column (intrepreted using Perl's
71 rules for truth, with one exception: `NOT NULL' is false). B<length> is the
72 SQL length of the column. B<default> is the default value of the column.
73 B<local> is reserved for database-specific information.
79 my $class = ref($proto) || $proto;
85 $self = { map { $_ => shift } qw(name type null length default local) };
88 #croak "Illegal name: ". $self->{'name'}
89 # if grep $self->{'name'} eq $_, @reserved_words;
91 $self->{'null'} =~ s/^NOT NULL$//i;
92 $self->{'null'} = 'NULL' if $self->{'null'};
94 bless ($self, $class);
100 Returns or sets the column name.
106 if ( defined($value) ) {
107 #croak "Illegal name: $name" if grep $name eq $_, @reserved_words;
108 $self->{'name'} = $value;
116 Returns or sets the column type.
122 if ( defined($value) ) {
123 $self->{'type'} = $value;
131 Returns or sets the column null flag (the empty string is equivalent to
138 if ( defined($value) ) {
139 $value =~ s/^NOT NULL$//i;
140 $value = 'NULL' if $value;
141 $self->{'null'} = $value;
147 =item length [ LENGTH ]
149 Returns or sets the column length.
155 if ( defined($value) ) {
156 $self->{'length'} = $value;
162 =item default [ LOCAL ]
164 Returns or sets the default value.
170 if ( defined($value) ) {
171 $self->{'default'} = $value;
178 =item local [ LOCAL ]
180 Returns or sets the database-specific field.
186 if ( defined($value) ) {
187 $self->{'local'} = $value;
193 =item line [ DATABASE_HANDLE | DATA_SOURCE [ USERNAME PASSWORD [ ATTR ] ] ]
195 Returns an SQL column definition.
197 The data source can be specified by passing an open DBI database handle, or by
198 passing the DBI data source name, username and password.
200 Although the username and password are optional, it is best to call this method
201 with a database handle or data source including a valid username and password -
202 a DBI connection will be opened and the quoting and type mapping will be more
205 If passed a DBI data source (or handle) such as `DBI:mysql:database' or
206 `DBI:Pg:dbname=database', will use syntax specific to that database engine.
207 Currently supported databases are MySQL and PostgreSQL. Non-standard syntax
208 for other engines (if applicable) may also be supported in the future.
213 my($self,$dbh) = (shift, shift);
216 unless ( ref($dbh) || ! @_ ) {
217 $dbh = DBI->connect( $dbh, @_ ) or die $DBI::errstr;
218 my $gratuitous = $DBI::errstr; #surpress superfluous `used only once' error
222 my $driver = DBIx::DBSchema::_load_driver($dbh);
224 %typemap = eval "\%DBIx::DBSchema::DBD::${driver}::typemap" if $driver;
225 my $type = defined( $typemap{uc($self->type)} )
226 ? $typemap{uc($self->type)}
229 my $null = $self->null;
232 if ( defined($self->default) && $self->default ne ''
234 # false laziness: nicked from FS::Record::_quote
235 && ( $self->default !~ /^\-?\d+(\.\d+)?$/
236 || $type =~ /(char|binary|blob|text)$/i
239 $default = $dbh->quote($self->default);
241 $default = $self->default;
244 #this should be a callback into the driver
245 if ( $driver eq 'mysql' ) { #yucky mysql hack
246 $null ||= "NOT NULL";
247 $self->local('AUTO_INCREMENT') if uc($self->type) eq 'SERIAL';
248 } elsif ( $driver eq 'Pg' ) { #yucky Pg hack
249 $null ||= "NOT NULL";
255 $type. ( ( defined($self->length) && $self->length )
256 ? '('.$self->length.')'
260 ( ( defined($default) && $default ne '' )
261 ? 'DEFAULT '. $default
264 ( ( $driver eq 'mysql' && defined($self->local) )
269 $dbh->disconnect if $created_dbh;
278 Ivan Kohler <ivan-dbix-dbschema@420.am>
282 Copyright (c) 2000 Ivan Kohler
283 Copyright (c) 2000 Mail Abuse Prevention System LLC
285 This program is free software; you can redistribute it and/or modify it under
286 the same terms as Perl itself.
290 line() has database-specific foo that probably ought to be abstracted into
291 the DBIx::DBSchema:DBD:: modules.
295 L<DBIx::DBSchema::Table>, L<DBIx::DBSchema>, L<DBIx::DBSchema::DBD>, L<DBI>