1 package DBIx::DBSchema;
4 use vars qw(@ISA $VERSION);
8 use FreezeThaw qw(freeze thaw cmpStr);
9 use DBIx::DBSchema::Table;
10 use DBIx::DBSchema::Column;
11 use DBIx::DBSchema::ColGroup::Unique;
12 use DBIx::DBSchema::ColGroup::Index;
21 DBIx::DBSchema - Database-independent schema objects
27 $schema = new DBIx::DBSchema @dbix_dbschema_table_objects;
28 $schema = new_odbc DBIx::DBSchema $dbh;
29 $schema = new_odbc DBIx::DBSchema $dsn, $user, $pass;
30 $schema = new_native DBIx::DBSchema $dbh;
31 $schema = new_native DBIx::DBSchema $dsn, $user, $pass;
33 $schema->save("filename");
34 $schema = load DBIx::DBSchema "filename";
36 $schema->addtable($dbix_dbschema_table_object);
38 @table_names = $schema->tables;
40 $DBIx_DBSchema_table_object = $schema->table("table_name");
42 @sql = $schema->sql($dsn);
44 $perl_code = $schema->pretty_print;
45 %hash = eval $perl_code;
46 $schema = pretty_read DBIx::DBSchema \%hash;
50 DBIx::DBSchema objects are collections of DBIx::DBSchema::Table objects and
51 represent a database schema.
53 This module implements an OO-interface to database schemas. Using this module,
54 you can create a database schema with an OO Perl interface. You can read the
55 schema from an existing database. You can save the schema to disk and restore
56 it a different process. Most importantly, DBIx::DBSchema can write SQL
57 CREATE statements statements for different databases from a single source.
59 Currently supported databases are MySQL and PostgreSQL. DBIx::DBSchema will
60 attempt to use generic SQL syntax for other databases. Assistance adding
61 support for other databases is welcomed.
67 =item new TABLE_OBJECT, TABLE_OBJECT, ...
69 Creates a new DBIx::DBSchema object.
74 my($proto, @tables) = @_;
75 my %tables = map { $_->name, $_ } @tables; #check for duplicates?
77 my $class = ref($proto) || $proto;
82 bless ($self, $class);
86 =item new_odbc DATABASE_HANDLE || DATA_SOURCE USERNAME PASSWORD [ ATTR ]
88 Creates a new DBIx::DBSchema object from an existing data source, which can be
89 specified by passing an open DBI database handle, or by passing the DBI data
90 source name, username, and password. This uses the experimental DBI type_info
91 method to create a schema with standard (ODBC) SQL column types that most
92 closely correspond to any non-portable column types. Use this to import a
93 schema that you wish to use with many different database engines. Although
94 primary key and (unique) index information will only be read from databases
95 with DBIx::DBSchema::DBD drivers (currently MySQL and PostgreSQL), import of
96 column names and attributes *should* work for any database.
101 my($proto, $dbh) = (shift, shift);
102 $dbh = DBI->connect( $dbh, @_ ) or die $DBI::errstr unless ref($dbh);
104 map { new_odbc DBIx::DBSchema::Table $dbh, $_ } _tables_from_dbh($dbh)
108 =item new_native DATABASE_HANDLE || DATA_SOURCE USERNAME PASSWORD [ ATTR ]
110 Creates a new DBIx::DBSchema object from an existing data source, which can be
111 specified by passing an open DBI database handle, or by passing the DBI data
112 source name, username and password. This uses database-native methods to read
113 the schema, and will preserve any non-portable column types. The method is
114 only available if there is a DBIx::DBSchema::DBD for the corresponding database engine (currently, MySQL and PostgreSQL).
119 my($proto, $dbh) = (shift, shift);
120 $dbh = DBI->connect( $dbh, @_ ) or die $DBI::errstr unless ref($dbh);
122 map { new_native DBIx::DBSchema::Table ( $dbh, $_ ) } _tables_from_dbh($dbh)
128 Loads a DBIx::DBSchema object from a file.
133 my($proto,$file)=@_; #use $proto ?
134 open(FILE,"<$file") or die "Can't open $file: $!";
135 my($string)=join('',<FILE>); #can $string have newlines? pry not?
136 close FILE or die "Can't close $file: $!";
137 my($self)=thaw $string;
144 Saves a DBIx::DBSchema object to a file.
150 my($string)=freeze $self;
151 open(FILE,">$file") or die "Can't open $file: $!";
153 close FILE or die "Can't close file: $!";
154 my($check_self)=thaw $string;
155 die "Verify error: Can't freeze and thaw dbdef $self"
156 if (cmpStr($self,$check_self));
159 =item addtable TABLE_OBJECT
161 Adds the given DBIx::DBSchema::Table object to this DBIx::DBSchema.
167 $self->{'tables'}->{$table->name} = $table; #check for dupliates?
172 Returns a list of the names of all tables.
178 keys %{$self->{'tables'}};
181 =item table TABLENAME
183 Returns the specified DBIx::DBSchema::Table object.
189 $self->{'tables'}->{$table};
192 =item sql_string [ DATASRC ]
194 Returns a list of SQL `CREATE' statements for this schema.
196 If passed a DBI data source such as `DBI:mysql:database' or
197 `DBI:Pg:dbname=database', will use syntax specific to that database engine.
198 Currently supported databases are MySQL and PostgreSQL.
200 If not passed a data source, or if there is no driver for the specified
201 database, will attempt to use generic SQL syntax.
206 my($self, $datasrc) = @_;
207 map { $self->table($_)->sql_create_table($datasrc); } $self->tables;
212 Returns the data in this schema as Perl source, suitable for assigning to a
225 #cant because -w complains about , in qw()
226 # (also biiiig problems with empty lengths)
228 #$self->table($table)->column($_)->type. " ".
229 #( $self->table($table)->column($_)->null ? 'NULL' : 0 ). " ".
230 #$self->table($table)->column($_)->length. " ),\n"
232 "'". $self->table($table)->column($_)->type. "', ".
233 "'". $self->table($table)->column($_)->null. "', ".
234 "'". $self->table($table)->column($_)->length. "', ".
235 "'". $self->table($table)->column($_)->local. "',\n"
236 } $self->table($table)->columns
239 " 'primary_key' => '". $self->table($table)->primary_key. "',\n".
240 " 'unique' => [ ". join(', ',
241 map { "[ '". join("', '", @{$_}). "' ]" }
242 @{$self->table($table)->unique->lol_ref}
244 " 'index' => [ ". join(', ',
245 map { "[ '". join("', '", @{$_}). "' ]" }
246 @{$self->table($table)->index->lol_ref}
248 #" 'index' => [ ". " ],\n"
255 =item pretty_read HASHREF
257 Creates a schema as specified by a data structure such as that created by
258 B<pretty_print> method.
263 my($proto, $href) = @_;
264 my $schema = $proto->new( map {
266 while ( @{$href->{$_}{'columns'}} ) {
267 push @columns, DBIx::DBSchema::Column->new(
268 splice @{$href->{$_}{'columns'}}, 0, 5
271 DBIx::DBSchema::Table->new(
273 $href->{$_}{'primary_key'},
274 DBIx::DBSchema::ColGroup::Unique->new($href->{$_}{'unique'}),
275 DBIx::DBSchema::ColGroup::Index->new($href->{$_}{'index'}),
281 # private subroutines
285 my $driver = $dbh->{Driver}->{Name};
286 #require "DBIx/DBSchema/DBD/$driver.pm";
288 eval 'require "DBIx/DBSchema/DBD/$driver.pm"' and $driver;
291 sub _tables_from_dbh {
293 my $sth = $dbh->table_info or die $dbh->errstr;
294 #map { $_->{TABLE_NAME} } grep { $_->{TABLE_TYPE} eq 'TABLE' }
295 # @{ $sth->fetchall_arrayref({ TABLE_NAME=>1, TABLE_TYPE=>1}) };
296 map { $_->[0] } grep { $_->[1] =~ /^TABLE$/i }
297 @{ $sth->fetchall_arrayref([2,3]) };
304 Ivan Kohler <ivan-dbix-dbschema@420.am>
308 Copyright (c) 2000 Ivan Kohler
309 Copyright (c) 2000 Mail Abuse Prevention System LLC
311 This program is free software; you can redistribute it and/or modify it under
312 the same terms as Perl itself.
316 Each DBIx::DBSchema object should have a name which corresponds to its name
317 within the SQL database engine (DBI data source).
319 pretty_print is actually pretty ugly.
323 L<DBIx::DBSchema::Table>, L<DBIx::DBSchema::ColGroup>,
324 L<DBIx::DBSchema::ColGroup::Unique>, L<DBIx::DBSchema::ColGroup::Index>,
325 L<DBIx::DBSchema::Column>, L<DBIx::DBSchema::DBD>, L<DBIx::DBSchema::mysql>,
326 L<DBIx::DBSchema::Pg>, L<FS::Record>, L<DBI>