+=item sql_add_column
+
+Returns a list of SQL statements to add this column.
+
+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
+PostgreSQL-specific syntax. Non-standard syntax for other engines (if
+applicable) may also be supported in the future.
+
+=cut
+
+sub sql_add_column {
+ my($self, $dbh) = (shift, shift);
+
+ die "$self: this column is not assigned to a table"
+ unless $self->table_name;
+
+ #false laziness w/Table::sql_create_driver
+ my $created_dbh = 0;
+ unless ( ref($dbh) || ! @_ ) {
+ $dbh = DBI->connect( $dbh, @_ ) or die $DBI::errstr;
+ my $gratuitous = $DBI::errstr; #surpress superfluous `used only once' error
+ $created_dbh = 1;
+ }
+
+ my $driver = $dbh ? _load_driver($dbh) : '';
+
+ #eofalse
+
+ my @after_add = ();
+
+ my $real_type = '';
+ if ( $driver eq 'Pg' && $self->type eq 'serial' ) {
+ $real_type = 'serial';
+ $self->type('int');
+
+ push @after_add, sub {
+ my($table, $column) = @_;
+
+ #needs more work for old Pg
+
+ my $nextval = "nextval('public.${table}_${column}_seq'::text)";
+
+ (
+ "ALTER TABLE $table ALTER COLUMN $column SET DEFAULT $nextval",
+ "CREATE SEQUENCE ${table}_${column}_seq",
+ "UPDATE $table SET $column = $nextval WHERE $column IS NULL",
+ #"ALTER TABLE $table ALTER $column SET NOT NULL",
+ );
+
+ };
+
+ }
+
+ my $real_null = undef;
+ if ( $driver eq 'Pg' && ! $self->null ) {
+ $real_null = $self->null;
+ $self->null('NULL');
+
+ push @after_add, sub {
+ my($table, $column) = @_;
+ "ALTER TABLE $table ALTER $column SET NOT NULL";
+ };
+
+ }
+
+ my @r = ();
+ my $table = $self->table_name;
+ my $column = $self->name;
+
+ push @r, "ALTER TABLE $table ADD COLUMN ". $self->line($dbh);
+
+ push @r, &{$_}($table, $column) foreach @after_add;
+
+ push @r, "ALTER TABLE $table ADD PRIMARY KEY ( ".
+ $self->table_obj->primary_key. " )"
+ if $self->name eq $self->table_obj->primary_key;
+
+ $self->type($real_type) if $real_type;
+ $self->null($real_null) if defined $real_null;
+
+ $dbh->disconnect if $created_dbh;
+
+ @r;
+
+}
+