package DBIx::DBSchema::Column;
use strict;
-use vars qw(@ISA $VERSION);
-#use Carp;
-#use Exporter;
+use vars qw($VERSION);
+use Carp;
use DBIx::DBSchema::_util qw(_load_driver _dbh);
-#@ISA = qw(Exporter);
-@ISA = qw();
-
-$VERSION = '0.08';
+$VERSION = '0.09';
=head1 NAME
if ( ref($_[0]) ) {
$self = shift;
} else {
+ #carp "Old-style $class creation without named parameters is deprecated!";
+ #croak "FATAL: old-style $class creation no longer supported;".
+ # " use named parameters";
+
$self = { map { $_ => shift } qw(name type null length default local) };
}
push @after_add, sub {
my($table, $column) = @_;
- #needs more work for old Pg
+ #needs more work for old Pg?
+
+ my $pg_server_version = $dbh->{'pg_server_version'};
+ unless ( $pg_server_version =~ /\d/ ) {
+ warn "WARNING: no pg_server_version! Assuming >= 7.3\n";
+ $pg_server_version = 70300;
+ }
my $nextval;
- if ( $dbh->{'pg_server_version'} > 70300 ) {
+ if ( $pg_server_version >= 70300 ) {
$nextval = "nextval('public.${table}_${column}_seq'::text)";
} else {
$nextval = "nextval('${table}_${column}_seq'::text)";
$real_null = $self->null;
$self->null('NULL');
- #if ( $dbh->{'pg_server_version'} > 70300 ) { #this seemed to work on 7.3
- if ( $dbh->{'pg_server_version'} > 70400 ) { #after all...
+ my $pg_server_version = $dbh->{'pg_server_version'};
+ unless ( $pg_server_version =~ /\d/ ) {
+ warn "WARNING: no pg_server_version! Assuming >= 7.3\n";
+ $pg_server_version = 70300;
+ }
+
+ if ( $pg_server_version >= 70300 ) { #this did work on 7.3
+ #if ( $pg_server_version > 70400 ) {
push @after_add, sub {
my($table, $column) = @_;
# change nullability from NOT NULL to NULL
if ( ! $self->null && $new->null ) {
- if ( $driver eq 'Pg' && $dbh->{'pg_server_version'} < 70300 ) {
- push @r, "UPDATE pg_attribute SET attnotnull = FALSE
- WHERE attname = '$name'
- AND attrelid = ( SELECT oid FROM pg_class
- WHERE relname = '$table'
- )";
- } else {
- push @r, "ALTER TABLE $table ALTER COLUMN $name DROP NOT NULL";
+ my $alter = "ALTER TABLE $table ALTER COLUMN $name DROP NOT NULL";
+
+ if ( $driver eq 'Pg' ) {
+
+ my $pg_server_version = $dbh->{'pg_server_version'};
+ unless ( $pg_server_version =~ /\d/ ) {
+ warn "WARNING: no pg_server_version! Assuming >= 7.3\n";
+ $pg_server_version = 70300;
+ }
+
+ if ( $pg_server_version < 70300 ) {
+ $alter = "UPDATE pg_attribute SET attnotnull = FALSE
+ WHERE attname = '$name'
+ AND attrelid = ( SELECT oid FROM pg_class
+ WHERE relname = '$table'
+ )";
+ }
+
}
+
+ push @r, $alter;
+
}
# change nullability from NULL to NOT NULL...
# the table first...
if ( $self->null && ! $new->null ) {
- if ( $driver eq 'Pg' && $dbh->{'pg_server_version'} < 70300 ) {
- push @r, "UPDATE pg_attribute SET attnotnull = TRUE
- WHERE attname = '$name'
- AND attrelid = ( SELECT oid FROM pg_class
- WHERE relname = '$table'
- )";
- } else {
- push @r, "ALTER TABLE $table ALTER COLUMN $name SET NOT NULL";
+ my $alter = "ALTER TABLE $table ALTER COLUMN $name SET NOT NULL";
+
+ if ( $driver eq 'Pg' ) {
+
+ my $pg_server_version = $dbh->{'pg_server_version'};
+ unless ( $pg_server_version =~ /\d/ ) {
+ warn "WARNING: no pg_server_version! Assuming >= 7.3\n";
+ $pg_server_version = 70300;
+ }
+
+ if ( $pg_server_version < 70300 ) {
+ push @r, "UPDATE pg_attribute SET attnotnull = TRUE
+ WHERE attname = '$name'
+ AND attrelid = ( SELECT oid FROM pg_class
+ WHERE relname = '$table'
+ )";
+ }
+
}
+
+ push @r, $alter;
+
}
# change other stuff...
=head1 BUGS
+The new() method should warn that
+"Old-style $class creation without named parameters is deprecated!"
+
Better documentation is needed for sql_add_column
line() and sql_add_column() hav database-specific foo that should be abstracted