#@ISA = qw(Exporter);
@ISA = ();
-$VERSION = "0.11";
+$VERSION = "0.12";
=head1 NAME
$perl_code = $schema->pretty_print;
%hash = eval $perl_code;
- $schema = pretty_read DBIx::DBSchema \%hash;
+ use DBI qw(:sql_types); $schema = pretty_read DBIx::DBSchema \%hash;
=head1 DESCRIPTION
"'". $self->table($table)->column($_)->type. "', ".
"'". $self->table($table)->column($_)->null. "', ".
"'". $self->table($table)->column($_)->length. "', ".
+ "'". $self->table($table)->column($_)->default. "', ".
"'". $self->table($table)->column($_)->local. "',\n"
} $self->table($table)->columns
).
my(@columns);
while ( @{$href->{$_}{'columns'}} ) {
push @columns, DBIx::DBSchema::Column->new(
- splice @{$href->{$_}{'columns'}}, 0, 5
+ splice @{$href->{$_}{'columns'}}, 0, 6
);
}
DBIx::DBSchema::Table->new(
sub _load_driver {
my($dbh) = @_;
- my $driver = $dbh->{Driver}->{Name};
+ 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";
+ }
+
#require "DBIx/DBSchema/DBD/$driver.pm";
#$driver;
eval 'require "DBIx/DBSchema/DBD/$driver.pm"' and $driver;
pretty_print is actually pretty ugly.
+Perhaps pretty_read should eval column types so that we can use DBI
+qw(:sql_types) here instead of externally.
+
=head1 SEE ALSO
L<DBIx::DBSchema::Table>, L<DBIx::DBSchema::ColGroup>,
$column = new DBIx::DBSchema::Column ( $name, $sql_type, 'NULL' );
$column = new DBIx::DBSchema::Column ( $name, $sql_type, '', $length );
$column = new DBIx::DBSchema::Column ( $name, $sql_type, 'NULL', $length );
- $column = new DBIx::DBSchema::Column ( $name, $sql_type, 'NULL', $length, $local );
+ $column = new DBIx::DBSchema::Column ( $name, $sql_type, 'NULL', $length, $default, $local );
$name = $column->name;
$column->name( 'name' );
$column->length( '10' );
$column->length( '8,2' );
+ $default = $column->default;
+ $column->default( 'Roo' );
+
$sql_line = $column->line;
$sql_line = $column->line($datasrc);
=over 4
-=item new [ NAME [ , SQL_TYPE [ , NULL [ , LENGTH [ , LOCAL ] ] ] ] ]
+=item new [ NAME [ , SQL_TYPE [ , NULL [ , LENGTH [ , DEFAULT [ , LOCAL ] ] ] ] ] ]
Creates a new DBIx::DBSchema::Column object. NAME is the name of the column.
SQL_TYPE is the SQL data type. NULL is the nullability of the column (the
empty string is equivalent to `NOT NULL'). LENGTH is the SQL length of the
-column. LOCAL is reserved for database-specific information.
+column. DEFAULT is the default value of the column. LOCAL is reserved for
+database-specific information.
=cut
sub new {
- my($proto,$name,$type,$null,$length,$local)=@_;
+ my($proto,$name,$type,$null,$length,$default,$local)=@_;
#croak "Illegal name: $name" if grep $name eq $_, @reserved_words;
my $class = ref($proto) || $proto;
my $self = {
- 'name' => $name,
- 'type' => $type,
- 'null' => $null,
- 'length' => $length,
- 'local' => $local,
+ 'name' => $name,
+ 'type' => $type,
+ 'null' => $null,
+ 'length' => $length,
+ 'default' => $default,
+ 'local' => $local,
};
bless ($self, $class);
}
}
+=item default [ LOCAL ]
+
+Returns or sets the default value.
+
+=cut
+
+sub default {
+ my($self,$value)=@_;
+ if ( defined($value) ) {
+ $self->{'default'} = $value;
+ } else {
+ $self->{'default'};
+ }
+}
+
+
=item local [ LOCAL ]
Returns or sets the database-specific field.
sub line {
my($self,$datasrc)=@_;
+
+ my $driver = DBIx::DBSchema::_load_driver($datasrc);
+ my %typemap = eval "\%DBIx::DBSchema::DBD::${driver}::typemap";
+ my $type = defined( $typemap{uc($self->type)} )
+ ? $typemap{uc($self->type)}
+ : $self->type;
+
my($null)=$self->null;
+
if ( $datasrc =~ /^dbi:mysql:/i ) { #yucky mysql hack
$null ||= "NOT NULL"
}
$null ||= "NOT NULL";
$null =~ s/^NULL$//;
}
+
join(' ',
$self->name,
- $self->type. ( $self->length ? '('.$self->length.')' : '' ),
+ $type. ( $self->length ? '('.$self->length.')' : '' ),
$null,
+ ( ( defined($self->default) && $self->default ne '' )
+ ? 'DEFAULT '. $self->default
+ : ''
+ ),
( ( $datasrc =~ /^dbi:mysql:/i )
? $self->local
: ''
),
);
+
}
=back
use strict;
use vars qw($VERSION);
-$VERSION = '0.01';
+$VERSION = '0.02';
=head1 NAME
-DBIx::DBSchema::DBD - DBIx::DBSchema Driver Writer's Guide
+DBIx::DBSchema::DBD - DBIx::DBSchema Driver Writer's Guide and Base Class
=head1 SYNOPSIS
perldoc DBIx::DBSchema::DBD
+ package DBIx::DBSchema::DBD::FooBase
+ use DBIx::DBSchmea::DBD;
+ @ISA = qw(DBIx::DBSchema::DBD);
+
=head1 DESCRIPTION
Drivers should be named DBIx::DBSchema::DBD::DatabaseName, where DatabaseName
=item columns CLASS DBI_DBH TABLE
Given an active DBI database handle, return a listref of listrefs (see
-L<perllol>), each containing five elements: column name, column type,
-nullability, column length, and a field reserved for driver-specific use.
+L<perllol>), each containing six elements: column name, column type,
+nullability, column length, column default, and a field reserved for
+driver-specific use.
+
+=item column CLASS DBI_DBH TABLE COLUMN
+
+Same as B<columns> above, except return the listref for a single column. You
+can inherit from DBIx::DBSchema::DBD to provide this function.
+
+=cut
+
+sub column {
+ my($proto, $dbh, $table, $column) = @_;
+ #@a = grep { $_->[0] eq $column } @{ $proto->columns( $dbh, $table ) };
+ #$a[0];
+ @{ [
+ grep { $_->[0] eq $column } @{ $proto->columns( $dbh, $table ) }
+ ] }[0]; #force list context on grep, return scalar of first element
+}
=item primary_key CLASS DBI_DBH TABLE
=head1 BUGS
+%typemap needs to be documented.
+
=head1 SEE ALSO
L<DBIx::DBSchema>, L<DBIx::DBSchema::DBD::mysql>, L<DBIx::DBSchema::DBD::Pg>,
package DBIx::DBSchema::DBD::Pg;
use strict;
-use vars qw($VERSION);
+use vars qw($VERSION @ISA %typemap);
+use DBIx::DBSchema::DBD;
-$VERSION = '0.01';
+$VERSION = '0.02';
+@ISA = qw(DBIx::DBSchema::DBD);
+
+%typemap = (
+ 'BLOB' => 'TEXT',
+);
=head1 NAME
? $_->{'atttypmod'} - 4
: ''
),
- ''
+ '', #default
+ '' #local
]
} @{ $sth->fetchall_arrayref({}) };
}
Yes.
+columns doesn't return column default information.
+
=head1 SEE ALSO
L<DBIx::DBSchema>, L<DBIx::DBSchema::DBD>, L<DBI>, L<DBI::DBD>
package DBIx::DBSchema::DBD::mysql;
use strict;
-use vars qw($VERSION);
+use vars qw($VERSION @ISA %typemap);
+use DBIx::DBSchema::DBD;
-$VERSION = '0.01';
+$VERSION = '0.02';
+@ISA = qw(DBIx::DBSchema::DBD);
+
+%typemap = (
+ 'TIMESTAMP' => 'DATETIME',
+);
=head1 NAME
$_->{'Type'} =~ /^(\w+)\(?([\d\,]+)?\)?( unsigned)?$/
or die "Illegal type: ". $_->{'Type'}. "\n";
my($type, $length) = ($1, $2);
- [ $_->{'Field'}, $type, $_->{'Null'}, $length, $_->{'Extra'} ]
+ [
+ $_->{'Field'},
+ $type,
+ $_->{'Null'},
+ $length,
+ $_->{'Default'},
+ $_->{'Extra'}
+ ]
} @{ $sth->fetchall_arrayref( {} ) };
}
new DBIx::DBSchema::Column
$_,
$type_info->{'TYPE_NAME'},
+ #"SQL_". uc($type_info->{'TYPE_NAME'}),
$sth->{NULLABLE}->[$sthpos],
- &{
- $create_params{ $type_info->{CREATE_PARAMS} }
- }( $sth, $sthpos++ )
+ &{ $create_params{ $type_info->{CREATE_PARAMS} } }( $sth, $sthpos++ ), $driver && #default
+ ${ [
+ eval "DBIx::DBSchema::DBD::$driver->column(\$dbh, \$name, \$_)"
+ ] }[4]
+ # DB-local
} @{$sth->{NAME}}
);
}
Changes
MANIFEST
+MANIFEST.SKIP
README
Makefile.PL
DBSchema.pm