1 package DBIx::DBSchema;
4 use vars qw(@ISA $VERSION);
8 use FreezeThaw qw(freeze thaw cmpStr);
9 use DBIx::DBSchema::Table;
18 DBIx::DBSchema - Database-independent schema objects
24 $schema = new DBIx::DBSchema @dbix_dbschema_table_objects;
25 $schema = new_odbc DBIx::DBSchema $dbh;
26 $schema = new_odbc DBIx::DBSchema $dsn, $user, $pass;
27 $schema = new_native DBIx::DBSchema $dbh;
28 $schema = new_native DBIx::DBSchema $dsn, $user, $pass;
30 $schema->save("filename");
31 $schema = load DBIx::DBSchema "filename";
33 $schema->addtable($dbix_dbschema_table_object);
35 @table_names = $schema->tables;
37 $DBIx_DBSchema_table_object = $schema->table("table_name");
39 $sql_string = $schema->sql($dsn);
41 $perl_code = $schema->pretty_print;
42 %hash = eval $perl_code;
43 $schema = pretty_read DBIx::DBSchema \%hash;
47 DBIx::DBSchema objects are collections of DBIx::DBSchema::Table objects and
48 represent a database schema.
54 =item new TABLE_OBJECT, TABLE_OBJECT, ...
56 Creates a new DBIx::DBSchema object.
61 my($proto, @tables) = @_;
62 my %tables = map { $_->name, $_ } @tables; #check for duplicates?
64 my $class = ref($proto) || $proto;
69 bless ($self, $class);
73 =item new_odbc DATABASE_HANDLE || DATA_SOURCE USERNAME PASSWORD [ ATTR ]
75 Creates a new DBIx::DBSchema object from an existing data source, which can be
76 specified by passing an open DBI database handle, or by passing the DBI data
77 source name, username, and password. This uses the experimental DBI type_info
78 method to create a schema with standard (ODBC) SQL column types that most
79 closely correspond to any non-portable column types. Use this to import a
80 schema that you wish to use with many different database engines. Although
81 primary key and (unique) index information will only be read from databases
82 with DBIx::DBSchema::DBD drivers (currently MySQL and PostgreSQL), import of
83 column names and attributes *should* work for any database.
88 my($proto, $dbh) = (shift, shift);
89 $dbh = DBI->connect( $dbh, @_ ) or die $DBI::errstr unless ref($dbh);
91 map { new_odbc DBIx::DBSchema::Table $dbh, $_ } _tables_from_dbh($dbh)
95 =item new_native DATABASE_HANDLE || DATA_SOURCE USERNAME PASSWORD [ ATTR ]
97 Creates a new DBIx::DBSchema object from an existing data source, which can be
98 specified by passing an open DBI database handle, or by passing the DBI data
99 source name, username and password. This uses database-native methods to read
100 the schema, and will preserve any non-portable column types. The method is
101 only available if there is a DBIx::DBSchema::DBD for the corresponding database engine (currently, MySQL and PostgreSQL).
106 my($proto, $dbh) = (shift, shift);
107 $dbh = DBI->connect( $dbh, @_ ) or die $DBI::errstr unless ref($dbh);
109 map { new_native DBIx::DBSchema::Table ( $dbh, $_ ) } _tables_from_dbh($dbh)
115 Loads a DBIx::DBSchema object from a file.
120 my($proto,$file)=@_; #use $proto ?
121 open(FILE,"<$file") or die "Can't open $file: $!";
122 my($string)=join('',<FILE>); #can $string have newlines? pry not?
123 close FILE or die "Can't close $file: $!";
124 my($self)=thaw $string;
131 Saves a DBIx::DBSchema object to a file.
137 my($string)=freeze $self;
138 open(FILE,">$file") or die "Can't open $file: $!";
140 close FILE or die "Can't close file: $!";
141 my($check_self)=thaw $string;
142 die "Verify error: Can't freeze and thaw dbdef $self"
143 if (cmpStr($self,$check_self));
146 =item addtable TABLE_OBJECT
148 Adds the given DBIx::DBSchema::Table object to this DBIx::DBSchema.
154 $self->{'tables'}->{$table->name} = $table; #check for dupliates?
159 Returns a list of the names of all tables.
165 keys %{$self->{'tables'}};
168 =item table TABLENAME
170 Returns the specified DBIx::DBSchema::Table object.
176 $self->{'tables'}->{$table};
179 =item sql_string [ DATASRC ]
181 Returns a list of SQL `CREATE' statements for this schema.
183 If passed a DBI data source such as `DBI:mysql:database' or
184 `DBI:Pg:dbname=database', will use syntax specific to that database engine.
185 Currently supported databases are MySQL and PostgreSQL.
187 If not passed a data source, or if there is no driver for the specified
188 database, will attempt to use generic SQL syntax.
193 my($self, $datasrc) = @_;
194 map { $self->table($_)->sql_create_table($datasrc); } $self->tables;
199 Returns the data in this schema as Perl source, suitable for assigning to a
212 #cant because -w complains about , in qw()
213 # (also biiiig problems with empty lengths)
215 #$self->table($table)->column($_)->type. " ".
216 #( $self->table($table)->column($_)->null ? 'NULL' : 0 ). " ".
217 #$self->table($table)->column($_)->length. " ),\n"
219 "'". $self->table($table)->column($_)->type. "', ".
220 "'". $self->table($table)->column($_)->null. "', ".
221 "'". $self->table($table)->column($_)->length. "'\n"
222 } $self->table($table)->columns
225 " 'primary_key' => '". $self->table($table)->primary_key. "',\n".
226 " 'unique' => [ ". join(', ',
227 map { "[ '". join("', '", @{$_}). "' ]" }
228 @{$self->table($table)->unique->lol_ref}
230 " 'index' => [ ". join(', ',
231 map { "[ '". join("', '", @{$_}). "' ]" }
232 @{$self->table($table)->index->lol_ref}
234 #" 'index' => [ ". " ],\n"
241 =item pretty_read HASHREF
243 Creates a schema as specified by a data structure such as that created by
244 B<pretty_print> method.
249 die "unimplemented (pull from fs-setup)";
253 # private subroutines
257 my $driver = $dbh->{Driver}->{Name};
258 #require "DBIx/DBSchema/DBD/$driver.pm";
260 eval 'require "DBIx/DBSchema/DBD/$driver.pm"' and $driver;
263 sub _tables_from_dbh {
265 my $sth = $dbh->table_info or die $dbh->errstr;
266 #map { $_->{TABLE_NAME} } grep { $_->{TABLE_TYPE} eq 'TABLE' }
267 # @{ $sth->fetchall_arrayref({ TABLE_NAME=>1, TABLE_TYPE=>1}) };
268 map { $_->[0] } grep { $_->[1] =~ /^TABLE$/i }
269 @{ $sth->fetchall_arrayref([2,3]) };
276 Ivan Kohler <ivan-dbix-dbschema@420.am>
280 Copyright (c) 2000 Ivan Kohler
281 Copyright (c) 2000 Mail Abuse Prevention System LLC
283 This program is free software; you can redistribute it and/or modify it under
284 the same terms as Perl itself.
288 Each DBIx::DBSchema object should have a name which corresponds to its name
289 within the SQL database engine (DBI data source).
291 pretty_print is actually pretty ugly.
295 L<DBIx::DBSchema::Table>, L<DBIx::DBSchema::ColGroup>,
296 L<DBIx::DBSchema::ColGroup::Unique>, L<DBIx::DBSchema::ColGroup::Index>,
297 L<DBIx::DBSchema::Column>, L<DBIx::DBSchema::DBD>, L<DBIx::DBSchema::mysql>,
298 L<DBIx::DBSchema::Pg>, L<FS::Record>, L<DBI>