1 package DBIx::DBSchema::Table;
4 use vars qw(@ISA %create_params);
7 use DBIx::DBSchema::Column;
8 use DBIx::DBSchema::ColGroup::Unique;
9 use DBIx::DBSchema::ColGroup::Index;
16 DBIx::DBSchema::Table - Table objects
20 use DBIx::DBSchema::Table;
22 $table = new DBIx::DBSchema::Table (
25 $dbix_dbschema_colgroup_unique_object,
26 $dbix_dbschema_colgroup_index_object,
27 @dbix_dbschema_column_objects,
30 $table->addcolumn ( $dbix_dbschema_column_object );
32 $table_name = $table->name;
33 $table->name("table_name");
35 $primary_key = $table->primary_key;
36 $table->primary_key("primary_key");
38 $dbix_dbschema_colgroup_unique_object = $table->unique;
39 $table->unique( $dbix_dbschema__colgroup_unique_object );
41 $dbix_dbschema_colgroup_index_object = $table->index;
42 $table->index( $dbix_dbschema_colgroup_index_object );
44 @column_names = $table->columns;
46 $dbix_dbschema_column_object = $table->column("column");
48 @sql_statements = $table->sql_create_table;
49 @sql_statements = $table->sql_create_table $datasrc;
53 DBIx::DBSchema::Table objects represent a single database table.
59 =item new [ TABLE_NAME [ , PRIMARY_KEY [ , UNIQUE [ , INDEX [ , COLUMN... ] ] ] ] ]
61 Creates a new DBIx::DBSchema::Table object. TABLE_NAME is the name of the
62 table. PRIMARY_KEY is the primary key (may be empty). UNIQUE is a
63 DBIx::DBSchema::ColGroup::Unique object (see
64 L<DBIx::DBSchema::ColGroup::Unique>). INDEX is a
65 DBIx::DBSchema::ColGroup::Index object (see
66 L<DBIx::DBSchema::ColGroup::Index>). The rest of the arguments should be
67 DBIx::DBSchema::Column objects (see L<DBIx::DBSchema::Column>).
72 my($proto,$name,$primary_key,$unique,$index,@columns)=@_;
74 my(%columns) = map { $_->name, $_ } @columns;
75 my(@column_order) = map { $_->name } @columns;
77 #check $primary_key, $unique and $index to make sure they are $columns ?
80 my $class = ref($proto) || $proto;
83 'primary_key' => $primary_key,
86 'columns' => \%columns,
87 'column_order' => \@column_order,
90 bless ($self, $class);
94 =item new_odbc DATABASE_HANDLE TABLE_NAME
96 Creates a new DBIx::DBSchema::Table object from the supplied DBI database
97 handle for the specified table. This uses the experimental DBI type_info
98 method to create a table with standard (ODBC) SQL column types that most
99 closely correspond to any non-portable column types. Use this to import a
100 schema that you wish to use with many different database engines. Although
101 primary key and (unique) index information will only be imported from databases
102 with DBIx::DBSchema::DBD drivers (currently MySQL and PostgreSQL), import of
103 column names and attributes *should* work for any database.
108 # undef => sub { '' },
110 'max length' => sub { $_[0]->{PRECISION}->[$_[1]]; },
112 sub { $_[0]->{PRECISION}->[$_[1]]. ','. $_[0]->{SCALE}->[$_[1]]; }
116 my( $proto, $dbh, $name) = @_;
117 my $driver = DBIx::DBSchema::_load_driver($dbh);
118 my $sth = _null_sth($dbh, $name);
122 scalar(eval "DBIx::DBSchema::DBD::$driver->primary_key(\$dbh, \$name)"),
123 DBIx::DBSchema::ColGroup::Unique->new(
125 ? [values %{eval "DBIx::DBSchema::DBD::$driver->unique(\$dbh, \$name)"}]
128 DBIx::DBSchema::ColGroup::Index->new(
130 ? [ values %{eval "DBIx::DBSchema::DBD::$driver->index(\$dbh, \$name)"} ]
134 my $type_info = scalar($dbh->type_info($sth->{TYPE}->[$sthpos]))
135 or die "DBI::type_info ". $dbh->{Driver}->{Name}. " driver ".
136 "returned no results for type ". $sth->{TYPE}->[$sthpos];
137 new DBIx::DBSchema::Column
139 $type_info->{'TYPE_NAME'},
140 #"SQL_". uc($type_info->{'TYPE_NAME'}),
141 $sth->{NULLABLE}->[$sthpos],
142 &{ $create_params{ $type_info->{CREATE_PARAMS} } }( $sth, $sthpos++ ), $driver && #default
144 eval "DBIx::DBSchema::DBD::$driver->column(\$dbh, \$name, \$_)"
151 =item new_native DATABASE_HANDLE TABLE_NAME
153 Creates a new DBIx::DBSchema::Table object from the supplied DBI database
154 handle for the specified table. This uses database-native methods to read the
155 schema, and will preserve any non-portable column types. The method is only
156 available if there is a DBIx::DBSchema::DBD for the corresponding database
157 engine (currently, MySQL and PostgreSQL).
162 my( $proto, $dbh, $name) = @_;
163 my $driver = DBIx::DBSchema::_load_driver($dbh);
166 scalar(eval "DBIx::DBSchema::DBD::$driver->primary_key(\$dbh, \$name)"),
167 DBIx::DBSchema::ColGroup::Unique->new(
168 [ values %{eval "DBIx::DBSchema::DBD::$driver->unique(\$dbh, \$name)"} ]
170 DBIx::DBSchema::ColGroup::Index->new(
171 [ values %{eval "DBIx::DBSchema::DBD::$driver->index(\$dbh, \$name)"} ]
174 DBIx::DBSchema::Column->new( @{$_} )
175 } eval "DBIx::DBSchema::DBD::$driver->columns(\$dbh, \$name)"
179 =item addcolumn COLUMN
181 Adds this DBIx::DBSchema::Column object.
186 my($self,$column)=@_;
187 ${$self->{'columns'}}{$column->name}=$column; #sanity check?
188 push @{$self->{'column_order'}}, $column->name;
191 =item name [ TABLE_NAME ]
193 Returns or sets the table name.
199 if ( defined($value) ) {
200 $self->{name} = $value;
206 =item primary_key [ PRIMARY_KEY ]
208 Returns or sets the primary key.
214 if ( defined($value) ) {
215 $self->{primary_key} = $value;
217 #$self->{primary_key};
218 #hmm. maybe should untaint the entire structure when it comes off disk
219 # cause if you don't trust that, ?
220 $self->{primary_key} =~ /^(\w*)$/
222 or die "Illegal primary key: ", $self->{primary_key};
227 =item unique [ UNIQUE ]
229 Returns or sets the DBIx::DBSchema::ColGroup::Unique object.
235 if ( defined($value) ) {
236 $self->{unique} = $value;
242 =item index [ INDEX ]
244 Returns or sets the DBIx::DBSchema::ColGroup::Index object.
250 if ( defined($value) ) {
251 $self->{'index'} = $value;
259 Returns a list consisting of the names of all columns.
265 #keys %{$self->{'columns'}};
267 @{ $self->{'column_order'} };
270 =item column COLUMN_NAME
272 Returns the column object (see L<DBIx::DBSchema::Column>) for the specified
278 my($self,$column)=@_;
279 $self->{'columns'}->{$column};
282 =item sql_create_table [ DATASRC ]
284 Returns a list of SQL statments to create this table.
286 If passed a DBI data source such as `DBI:mysql:database', will use
287 MySQL-specific syntax. PostgreSQL is also supported (requires no special
288 syntax). Non-standard syntax for other engines (if applicable) may also be
289 supported in the future.
293 sub sql_create_table {
294 my($self,$datasrc)=@_;
295 my(@columns)=map { $self->column($_)->line($datasrc) } $self->columns;
296 push @columns, "PRIMARY KEY (". $self->primary_key. ")"
297 if $self->primary_key;
298 if ( $datasrc =~ /^dbi:mysql:/i ) { #yucky mysql hack
299 push @columns, map "UNIQUE ($_)", $self->unique->sql_list;
300 push @columns, map "INDEX ($_)", $self->index->sql_list;
303 "CREATE TABLE ". $self->name. " (\n ". join(",\n ", @columns). "\n)\n",
305 my($index) = $self->name. "__". $_ . "_index";
306 $index =~ s/,\s*/_/g;
307 "CREATE UNIQUE INDEX $index ON ". $self->name. " ($_)\n"
308 } $self->unique->sql_list ),
310 my($index) = $self->name. "__". $_ . "_index";
311 $index =~ s/,\s*/_/g;
312 "CREATE INDEX $index ON ". $self->name. " ($_)\n"
313 } $self->index->sql_list ),
321 my($dbh, $table) = @_;
322 my $sth = $dbh->prepare("SELECT * FROM $table WHERE 1=0")
324 $sth->execute or die $sth->errstr;
332 Ivan Kohler <ivan-dbix-dbschema@420.am>
336 Copyright (c) 2000 Ivan Kohler
337 Copyright (c) 2000 Mail Abuse Prevention System LLC
339 This program is free software; you can redistribute it and/or modify it under
340 the same terms as Perl itself.
344 sql_create_table() has database-specific foo that probably ought to be
345 abstracted into the DBIx::DBSchema::DBD:: modules.
347 Some of the logic in new_odbc might be better abstracted into Column.pm etc.
351 L<DBIx::DBSchema>, L<DBIx::DBSchema::ColGroup::Unique>,
352 L<DBIx::DBSchema::ColGroup::Index>, L<DBIx::DBSchema::Column>, L<DBI>