06af8392e5966e0d4fbfd1ed722f639bdc900674
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Schema / Field.pm
1 package SQL::Translator::Schema::Field;
2
3 # ----------------------------------------------------------------------
4 # $Id: Field.pm,v 1.2 2003-05-03 15:42:59 kycl4rk Exp $
5 # ----------------------------------------------------------------------
6 # Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>
7 #
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License as
10 # published by the Free Software Foundation; version 2.
11 #
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 # General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 # 02111-1307  USA
21 # -------------------------------------------------------------------
22
23 =pod
24
25 =head1 NAME
26
27 SQL::Translator::Schema::Field - SQL::Translator field object
28
29 =head1 SYNOPSIS
30
31   use SQL::Translator::Schema::Field;
32   my $field = SQL::Translator::Schema::Field->new(
33       name => 'foo',
34       sql  => 'select * from foo',
35   );
36
37 =head1 DESCRIPTION
38
39 C<SQL::Translator::Schema::Field> is the field object.
40
41 =head1 METHODS
42
43 =cut
44
45 use strict;
46 use Class::Base;
47
48 use base 'Class::Base';
49 use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT);
50
51 $VERSION = 1.00;
52
53 # ----------------------------------------------------------------------
54 sub init {
55
56 =pod
57
58 =head2 new
59
60 Object constructor.
61
62   my $schema = SQL::Translator::Schema::Field->new;
63
64 =cut
65
66     my ( $self, $config ) = @_;
67     $self->params( $config, qw[ name data_type size is_primary_key ] );
68     return $self;
69 }
70
71 # ----------------------------------------------------------------------
72 sub data_type {
73
74 =pod
75
76 =head2 data_type
77
78 Get or set the field's data_type.
79
80   my $data_type = $field->data_type('integer');
81
82 =cut
83
84     my $self = shift;
85     $self->{'data_type'} = shift if @_;
86     return $self->{'data_type'} || '';
87 }
88
89 # ----------------------------------------------------------------------
90 sub is_primary_key {
91
92 =pod
93
94 =head2 is_primary_key
95
96 Get or set the field's is_primary_key attribute.
97
98   my $is_pk = $field->is_primary_key(1);
99
100 =cut
101
102     my ( $self, $arg ) = @_;
103
104     if ( defined $arg ) {
105         $self->{'is_primary_key'} = $arg ? 1 : 0;
106     }
107
108     return $self->{'is_primary_key'} || 0;
109 }
110
111 # ----------------------------------------------------------------------
112 sub name {
113
114 =pod
115
116 =head2 name
117
118 Get or set the field's name.
119
120   my $name = $field->name('foo');
121
122 =cut
123
124     my $self = shift;
125     $self->{'name'} = shift if @_;
126     return $self->{'name'} || '';
127 }
128
129 # ----------------------------------------------------------------------
130 sub size {
131
132 =pod
133
134 =head2 size
135
136 Get or set the field's size.
137
138   my $size = $field->size('25');
139
140 =cut
141
142     my ( $self, $arg ) = @_;
143
144     if ( $arg && $arg =~ m/^\d+(?:\.\d+)?$/ ) {
145         $self->{'size'} = $arg;
146     }
147
148     return $self->{'size'} || 0;
149 }
150
151 # ----------------------------------------------------------------------
152 sub is_valid {
153
154 =pod
155
156 =head2 is_valid
157
158 Determine whether the field is valid or not.
159
160   my $ok = $field->is_valid;
161
162 =cut
163
164     my $self = shift;
165     return 1 if $self->name && $self->data_type;
166 }
167
168 1;
169
170 # ----------------------------------------------------------------------
171
172 =pod
173
174 =head1 AUTHOR
175
176 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>
177
178 =cut