X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=DBSchema%2FTable.pm;h=b150d30de24dbe71ef567baa36c09fe223dc4eb5;hb=404241bce9880be2a101bda624e05bf6e944c21a;hp=0da05790a3d708bc2d2a5972503c4050a8099a0e;hpb=96622e37f51f347eb08b90c81add4f767a1ef367;p=DBIx-DBSchema.git diff --git a/DBSchema/Table.pm b/DBSchema/Table.pm index 0da0579..b150d30 100644 --- a/DBSchema/Table.pm +++ b/DBSchema/Table.pm @@ -435,9 +435,6 @@ sub unique_singles { Returns a list of SQL statments to create this table. -Optionally, the data source can be specified by passing an open DBI database -handle, or by passing the DBI data source name, username and password. - The data source can be specified by passing an open DBI database handle, or by passing the DBI data source name, username and password. @@ -475,7 +472,7 @@ sub sql_create_table { push @columns, "PRIMARY KEY (". $self->primary_key. ")" if $self->primary_key && ! grep /PRIMARY KEY/i, @columns; - push @columns, $self->foreign_keys_sql; +# push @columns, $self->foreign_keys_sql; my $indexnum = 1; @@ -494,6 +491,32 @@ sub sql_create_table { @r; } +=item sql_add_constraints [ DATABASE_HANDLE | DATA_SOURCE [ USERNAME PASSWORD [ ATTR ] ] ] + +Returns a list of SQL statments to add constraints (foreign keys) to this table. + +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- or PostgreSQL-specific syntax. Non-standard syntax for other engines +(if applicable) may also be supported in the future. + +=cut + +sub sql_add_constraints { + my $self = shift; + my @fks = $self->foreign_keys_sql or return (); + ( + 'ALTER TABLE '. $self->name. ' '. join(",\n ", map "ADD $_", @fks) + ); +} + =item sql_alter_table PROTOTYPE_TABLE, [ DATABASE_HANDLE | DATA_SOURCE [ USERNAME PASSWORD [ ATTR ] ] ] Returns a list of SQL statements to alter this table so that it is identical @@ -630,6 +653,56 @@ sub sql_alter_table { } ### + # return the statements + ### + + unshift @r, "ALTER TABLE $table ". join(', ', @at) if @at; + + push @r, @r_later; + + warn join('', map "$_\n", @r) + if $DEBUG && @r; + + @r; + +} + +=item sql_alter_constraints PROTOTYPE_TABLE, [ DATABASE_HANDLE | DATA_SOURCE [ USERNAME PASSWORD [ ATTR ] ] ] + +Returns a list of SQL statements to alter this table so that it is identical +to the provided table, also a DBIx::DBSchema::Table object. + +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 used to check the database version as well +as for more reliable quoting and type mapping. Note that the database +connection will be used passively, B to actually run the CREATE +statements. + +If passed a DBI data source (or handle) such as `DBI:mysql:database' or +`DBI:Pg:dbname=database', will use syntax specific to that database engine. +Currently supported databases are MySQL and PostgreSQL. + +If not passed a data source (or handle), or if there is no driver for the +specified database, will attempt to use generic SQL syntax. + +=cut + +#gosh, false laziness w/DBSchema::sql_update_schema + +sub sql_alter_constraints { + my($self, $opt, $new, $dbh) = ( shift, _parse_opt(\@_), shift, _dbh(@_) ); + + my $driver = _load_driver($dbh); + + my $table = $self->name; + + my @at = (); + + ### # foreign keys (add) ### @@ -642,18 +715,10 @@ sub sql_alter_table { # XXX foreign keys modify / drop - ### - # return the statements - ### - - unshift @r, "ALTER TABLE $table ". join(', ', @at) if @at; - - push @r, @r_later; - - warn join('', map "$_\n", @r) - if $DEBUG && @r; - - @r; + return () unless @at; + ( + 'ALTER TABLE '. $self->name. ' '. join(",\n ", @at) + ); }