initial import
[DBIx-DBSchema.git] / DBSchema / Column.pm
1 package DBIx::DBSchema::Column;
2
3 use strict;
4 use vars qw(@ISA);
5 #use Carp;
6 #use Exporter;
7
8 #@ISA = qw(Exporter);
9 @ISA = qw();
10
11 =head1 NAME
12
13 DBIx::DBSchema::Column - Column objects
14
15 =head1 SYNOPSIS
16
17   use DBIx::DBSchema::Column;
18
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
24   $name = $column->name;
25   $column->name( 'name' );
26
27   $sql_type = $column->type;
28   $column->sql_type( 'sql_type' );
29
30   $null = $column->null;
31   $column->null( 'NULL' );
32   $column->null( 'NOT NULL' );
33   $column->null( '' );
34
35   $length = $column->length;
36   $column->length( '10' );
37   $column->length( '8,2' );
38
39   $sql_line = $column->line;
40   $sql_line = $column->line($datasrc);
41
42 =head1 DESCRIPTION
43
44 DBIx::DBSchema::Column objects represent columns in tables (see
45 L<DBIx::DBSchema::Table>).
46
47 =head1 METHODS
48
49 =over 4
50
51 =item new [ NAME [ , SQL_TYPE [ , NULL [ , LENGTH ] ] ] ]
52
53 Creates a new DBIx::DBSchema::Column object.  NAME is the name of the column.
54 SQL_TYPE is the SQL data type.  NULL is the nullability of the column (the
55 empty string is equivalent to `NOT NULL').  LENGTH is the SQL length of the
56 column.
57
58 =cut
59
60 sub new {
61   my($proto,$name,$type,$null,$length)=@_;
62
63   #croak "Illegal name: $name" if grep $name eq $_, @reserved_words;
64
65   $null =~ s/^NOT NULL$//i;
66   $null = 'NULL' if $null;
67
68   my $class = ref($proto) || $proto;
69   my $self = {
70     'name'   => $name,
71     'type'   => $type,
72     'null'   => $null,
73     'length' => $length,
74   };
75
76   bless ($self, $class);
77
78 }
79
80 =item name [ NAME ]
81
82 Returns or sets the column name.
83
84 =cut
85
86 sub name {
87   my($self,$value)=@_;
88   if ( defined($value) ) {
89   #croak "Illegal name: $name" if grep $name eq $_, @reserved_words;
90     $self->{'name'} = $value;
91   } else {
92     $self->{'name'};
93   }
94 }
95
96 =item type [ TYPE ]
97
98 Returns or sets the column type.
99
100 =cut
101
102 sub type {
103   my($self,$value)=@_;
104   if ( defined($value) ) {
105     $self->{'type'} = $value;
106   } else {
107     $self->{'type'};
108   }
109 }
110
111 =item null [ NULL ]
112
113 Returns or sets the column null flag (the empty string is equivalent to
114 `NOT NULL')
115
116 =cut
117
118 sub null {
119   my($self,$value)=@_;
120   if ( defined($value) ) {
121     $value =~ s/^NOT NULL$//i;
122     $value = 'NULL' if $value;
123     $self->{'null'} = $value;
124   } else {
125     $self->{'null'};
126   }
127 }
128
129 =item length [ LENGTH ]
130
131 Returns or sets the column length.
132
133 =cut
134
135 sub length {
136   my($self,$value)=@_;
137   if ( defined($value) ) {
138     $self->{'length'} = $value;
139   } else {
140     $self->{'length'};
141   }
142 }
143
144 =item line [ $datasrc ]
145
146 Returns an SQL column definition.
147
148 If passed a DBI data source such as `DBI:mysql:database' or
149 `DBI:Pg:dbname=database', will use syntax specific to that database engine.
150 Currently supported databases are MySQL and PostgreSQL.  Non-standard syntax
151 for other engines (if applicable) may also be supported in the future.
152
153 =cut
154
155 sub line {
156   my($self,$datasrc)=@_;
157   my($null)=$self->null;
158   if ( $datasrc =~ /^dbi:mysql:/i ) { #yucky mysql hack
159     $null ||= "NOT NULL"
160   }
161   if ( $datasrc =~ /^dbi:pg/i ) { #yucky Pg hack
162     $null ||= "NOT NULL";
163     $null =~ s/^NULL$//;
164   }
165   join(' ',
166     $self->name,
167     $self->type. ( $self->length ? '('.$self->length.')' : '' ),
168     $null,
169   );
170 }
171
172 =back
173
174 =head1 AUTHOR
175
176 Ivan Kohler <ivan-dbix-dbschema@420.am>
177
178 =head1 COPYRIGHT
179
180 Copyright (c) 2000 Ivan Kohler
181 Copyright (c) 2000 Mail Abuse Prevention System LLC
182 All rights reserved.
183 This program is free software; you can redistribute it and/or modify it under
184 the same terms as Perl itself.
185
186 =head1 BUGS
187
188 line() has database-specific foo that probably ought to be abstracted into
189 the DBIx::DBSchema:DBD:: modules.
190
191 =head1 SEE ALSO
192
193 L<DBIx::DBSchema::Table>, L<DBIx::DBSchema>, L<DBIx::DBSchema::DBD>, L<DBI>
194
195 =cut
196
197 1;
198