$dbix_dbschema_column_object = $table->column("column");
- @sql_statements = $table->sql_create_table;
+ #preferred
+ @sql_statements = $table->sql_create_table $dbh;
+ @sql_statements = $table->sql_create_table $datasrc, $username, $password;
+
+ #possible problems
@sql_statements = $table->sql_create_table $datasrc;
+ @sql_statements = $table->sql_create_table;
=head1 DESCRIPTION
$self->{'columns'}->{$column};
}
-=item sql_create_table [ DATASRC ]
+=item sql_create_table [ DATABASE_HANDLE | DATA_SOURCE [ USERNAME PASSWORD [ ATTR ] ] ]
Returns a list of SQL statments to create this table.
-If passed a DBI data source such as `DBI:mysql:database', will use
+The data source can be specified by passing an open DBI database handle, or by
+passing the DBI data source name, username and password.
+
+Although the username and password are optional, it is best to call this method
+with a database handle or data source including a valid username and password -
+a DBI connection will be opened and the quoting and type mapping will be more
+reliable.
+
+If passed a DBI data source (or handle) such as `DBI:mysql:database', will use
MySQL-specific syntax. PostgreSQL is also supported (requires no special
syntax). Non-standard syntax for other engines (if applicable) may also be
supported in the future.
=cut
sub sql_create_table {
- my($self,$datasrc)=@_;
- my(@columns)=map { $self->column($_)->line($datasrc) } $self->columns;
+ my($self, $dbh) = (shift, shift);
+ $dbh = DBI->connect( $dbh, @_ ) or die $DBI::errstr
+ unless ref($dbh) || ! @_;
+
+ #false laziness: nicked from DBSchema::_load_driver
+ my $driver;
+ if ( ref($dbh) ) {
+ $driver = $dbh->{Driver}->{Name};
+ } else {
+ $dbh =~ s/^dbi:(\w*?)(?:\((.*?)\))?://i #nicked from DBI->connect
+ or '' =~ /()/; # ensure $1 etc are empty if match fails
+ $driver = $1 or die "can't parse data source: $dbh";
+ }
+ #eofalse
+
+ my(@columns)=map { $self->column($_)->line($dbh) } $self->columns;
push @columns, "PRIMARY KEY (". $self->primary_key. ")"
if $self->primary_key;
- if ( $datasrc =~ /^dbi:mysql:/i ) { #yucky mysql hack
+ if ( $driver eq 'mysql' ) { #yucky mysql hack
push @columns, map "UNIQUE ($_)", $self->unique->sql_list;
push @columns, map "INDEX ($_)", $self->index->sql_list;
}