1 package DBIx::Class::Schema::Loader::DBI::Pg;
5 use base 'DBIx::Class::Schema::Loader::DBI';
6 use Carp::Clan qw/^DBIx::Class/;
9 our $VERSION = '0.04999_14';
13 DBIx::Class::Schema::Loader::DBI::Pg - DBIx::Class::Schema::Loader::DBI
14 PostgreSQL Implementation.
19 use base qw/DBIx::Class::Schema::Loader/;
21 __PACKAGE__->loader_options( debug => 1 );
27 See L<DBIx::Class::Schema::Loader::Base>.
34 $self->next::method(@_);
35 $self->{db_schema} ||= 'public';
39 sub _table_uniq_info {
40 my ($self, $table) = @_;
42 # Use the default support if available
43 return $self->next::method($table)
44 if $DBD::Pg::VERSION >= 1.50;
47 my $dbh = $self->schema->storage->dbh;
49 # Most of the SQL here is mostly based on
50 # Rose::DB::Object::Metadata::Auto::Pg, after some prodding from
51 # John Siracusa to use his superior SQL code :)
53 my $attr_sth = $self->{_cache}->{pg_attr_sth} ||= $dbh->prepare(
54 q{SELECT attname FROM pg_catalog.pg_attribute
55 WHERE attrelid = ? AND attnum = ?}
58 my $uniq_sth = $self->{_cache}->{pg_uniq_sth} ||= $dbh->prepare(
59 q{SELECT x.indrelid, i.relname, x.indkey
62 JOIN pg_catalog.pg_class c ON c.oid = x.indrelid
63 JOIN pg_catalog.pg_class i ON i.oid = x.indexrelid
64 JOIN pg_catalog.pg_constraint con ON con.conname = i.relname
65 LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
67 x.indisunique = 't' AND
75 $uniq_sth->execute($self->db_schema, $table);
76 while(my $row = $uniq_sth->fetchrow_arrayref) {
77 my ($tableid, $indexname, $col_nums) = @$row;
78 $col_nums =~ s/^\s+//;
79 my @col_nums = split(/\s+/, $col_nums);
83 $attr_sth->execute($tableid, $_);
84 my $name_aref = $attr_sth->fetchrow_arrayref;
85 push(@col_names, $name_aref->[0]) if $name_aref;
89 warn "Failed to parse UNIQUE constraint $indexname on $table";
92 push(@uniqs, [ $indexname => \@col_names ]);
100 my ( $self, $table ) = @_;
101 my ($table_comment) = $self->schema->storage->dbh->selectrow_array(
102 q{SELECT obj_description(oid)
104 WHERE relname=? AND relnamespace=(
105 SELECT oid FROM pg_namespace WHERE nspname=?)
106 }, undef, $table, $self->db_schema
108 return $table_comment
112 sub _column_comment {
113 my ( $self, $table, $column_number ) = @_;
114 my ($table_oid) = $self->schema->storage->dbh->selectrow_array(
117 WHERE relname=? AND relnamespace=(
118 SELECT oid FROM pg_namespace WHERE nspname=?)
119 }, undef, $table, $self->db_schema
121 return $self->schema->storage->dbh->selectrow_array('SELECT col_description(?,?)', undef, $table_oid,
125 # Make sure data_type's that don't need it don't have a 'size' column_info, and
126 # set the correct precision for datetime and varbit types.
127 sub _columns_info_for {
131 my $result = $self->next::method(@_);
133 foreach my $col (keys %$result) {
134 my $data_type = $result->{$col}{data_type};
136 # these types are fixed size
138 /^(?:bigint|int8|bigserial|serial8|bit|boolean|bool|box|bytea|cidr|circle|date|double precision|float8|inet|integer|int|int4|line|lseg|macaddr|money|path|point|polygon|real|float4|smallint|int2|serial|serial4|text)\z/i) {
139 delete $result->{$col}{size};
141 # for datetime types, check if it has a precision or not
142 elsif ($data_type =~ /^(?:interval|time|timestamp)\b/i) {
143 my ($precision) = $self->schema->storage->dbh
144 ->selectrow_array(<<EOF, {}, $table, $col);
145 SELECT datetime_precision
146 FROM information_schema.columns
147 WHERE table_name = ? and column_name = ?
150 if ((not $precision) || $precision !~ /^\d/
151 || ($data_type !~ /^time\b/i && $precision == 6)) { # only interval/timestamp default to precision == 6
152 delete $result->{$col}{size};
155 $result->{$col}{size} = $precision;
158 elsif ($data_type =~ /^(?:bit varying|varbit)\z/i) {
159 my ($precision) = $self->schema->storage->dbh
160 ->selectrow_array(<<EOF, {}, $table, $col);
161 SELECT character_maximum_length
162 FROM information_schema.columns
163 WHERE table_name = ? and column_name = ?
166 $result->{$col}{size} = $precision;
168 elsif ($data_type =~ /^(?:numeric|decimal)\z/i) {
169 my $size = $result->{$col}{size};
172 my ($scale, $precision) = split /,/, $size;
174 $result->{$col}{size} = [ $precision, $scale ];
181 sub _extra_column_info {
182 my ($self, $info) = @_;
185 if ($info->{COLUMN_DEF} && $info->{COLUMN_DEF} =~ /\bnextval\(/i) {
186 $extra_info{is_auto_increment} = 1;
194 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
195 L<DBIx::Class::Schema::Loader::DBI>
199 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
203 This library is free software; you can redistribute it and/or modify it under
204 the same terms as Perl itself.