1 package DBIx::Class::Schema::Loader::DBI::Pg;
6 DBIx::Class::Schema::Loader::DBI::Component::QuotedDefault
7 DBIx::Class::Schema::Loader::DBI
9 use Carp::Clan qw/^DBIx::Class/;
12 our $VERSION = '0.07000';
16 DBIx::Class::Schema::Loader::DBI::Pg - DBIx::Class::Schema::Loader::DBI
17 PostgreSQL Implementation.
22 use base qw/DBIx::Class::Schema::Loader/;
24 __PACKAGE__->loader_options( debug => 1 );
30 See L<DBIx::Class::Schema::Loader::Base>.
37 $self->next::method(@_);
39 $self->{db_schema} ||= 'public';
41 if (not defined $self->preserve_case) {
42 $self->preserve_case(0);
46 sub _table_uniq_info {
47 my ($self, $table) = @_;
49 # Use the default support if available
50 return $self->next::method($table)
51 if $DBD::Pg::VERSION >= 1.50;
54 my $dbh = $self->schema->storage->dbh;
56 # Most of the SQL here is mostly based on
57 # Rose::DB::Object::Metadata::Auto::Pg, after some prodding from
58 # John Siracusa to use his superior SQL code :)
60 my $attr_sth = $self->{_cache}->{pg_attr_sth} ||= $dbh->prepare(
61 q{SELECT attname FROM pg_catalog.pg_attribute
62 WHERE attrelid = ? AND attnum = ?}
65 my $uniq_sth = $self->{_cache}->{pg_uniq_sth} ||= $dbh->prepare(
66 q{SELECT x.indrelid, i.relname, x.indkey
69 JOIN pg_catalog.pg_class c ON c.oid = x.indrelid
70 JOIN pg_catalog.pg_class i ON i.oid = x.indexrelid
71 JOIN pg_catalog.pg_constraint con ON con.conname = i.relname
72 LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
74 x.indisunique = 't' AND
82 $uniq_sth->execute($self->db_schema, $table);
83 while(my $row = $uniq_sth->fetchrow_arrayref) {
84 my ($tableid, $indexname, $col_nums) = @$row;
85 $col_nums =~ s/^\s+//;
86 my @col_nums = split(/\s+/, $col_nums);
90 $attr_sth->execute($tableid, $_);
91 my $name_aref = $attr_sth->fetchrow_arrayref;
92 push(@col_names, $name_aref->[0]) if $name_aref;
96 warn "Failed to parse UNIQUE constraint $indexname on $table";
99 push(@uniqs, [ $indexname => \@col_names ]);
107 my ( $self, $table ) = @_;
108 my ($table_comment) = $self->schema->storage->dbh->selectrow_array(
109 q{SELECT obj_description(oid)
111 WHERE relname=? AND relnamespace=(
112 SELECT oid FROM pg_namespace WHERE nspname=?)
113 }, undef, $table, $self->db_schema
115 return $table_comment
119 sub _column_comment {
120 my ( $self, $table, $column_number ) = @_;
121 my ($table_oid) = $self->schema->storage->dbh->selectrow_array(
124 WHERE relname=? AND relnamespace=(
125 SELECT oid FROM pg_namespace WHERE nspname=?)
126 }, undef, $table, $self->db_schema
128 return $self->schema->storage->dbh->selectrow_array('SELECT col_description(?,?)', undef, $table_oid,
132 # Make sure data_type's that don't need it don't have a 'size' column_info, and
133 # set the correct precision for datetime and varbit types.
134 sub _columns_info_for {
138 my $result = $self->next::method(@_);
140 foreach my $col (keys %$result) {
141 my $data_type = $result->{$col}{data_type};
143 # these types are fixed size
145 /^(?: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) {
146 delete $result->{$col}{size};
148 # for datetime types, check if it has a precision or not
149 elsif ($data_type =~ /^(?:interval|time|timestamp)\b/i) {
150 my ($precision) = $self->schema->storage->dbh
151 ->selectrow_array(<<EOF, {}, $table, $col);
152 SELECT datetime_precision
153 FROM information_schema.columns
154 WHERE table_name = ? and column_name = ?
157 if ($data_type =~ /^time\b/i) {
158 if ((not $precision) || $precision !~ /^\d/) {
159 delete $result->{$col}{size};
162 my ($integer_datetimes) = $self->schema->storage->dbh
163 ->selectrow_array('show integer_datetimes');
166 $integer_datetimes =~ /^on\z/i ? 6 : 10;
168 if ($precision == $max_precision) {
169 delete $result->{$col}{size};
172 $result->{$col}{size} = $precision;
176 elsif ((not $precision) || $precision !~ /^\d/ || $precision == 6) {
177 delete $result->{$col}{size};
180 $result->{$col}{size} = $precision;
183 elsif ($data_type =~ /^(?:bit varying|varbit)\z/i) {
184 my ($precision) = $self->schema->storage->dbh
185 ->selectrow_array(<<EOF, {}, $table, $col);
186 SELECT character_maximum_length
187 FROM information_schema.columns
188 WHERE table_name = ? and column_name = ?
191 $result->{$col}{size} = $precision;
193 elsif ($data_type =~ /^(?:numeric|decimal)\z/i && (my $size = $result->{$col}{size})) {
196 my ($scale, $precision) = split /,/, $size;
198 $result->{$col}{size} = [ $precision, $scale ];
201 # process SERIAL columns
202 if (ref($result->{$col}{default_value}) eq 'SCALAR' && ${ $result->{$col}{default_value} } =~ /\bnextval\(['"](\w+)/i) {
203 $result->{$col}{is_auto_increment} = 1;
204 $result->{$col}{sequence} = $1;
205 delete $result->{$col}{default_value};
208 # alias now() to CURRENT_TIMESTAMP for deploying to other DBs
209 if (eval { lc ${ $result->{$col}{default_value} }||'' eq 'now()' }) {
210 # do not use a ref to a constant, that breaks Data::Dump output
211 ${$result->{$col}{default_value}} = 'CURRENT_TIMESTAMP';
220 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
221 L<DBIx::Class::Schema::Loader::DBI>
225 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
229 This library is free software; you can redistribute it and/or modify it under
230 the same terms as Perl itself.