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.05003';
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(@_);
38 $self->{db_schema} ||= 'public';
42 sub _table_uniq_info {
43 my ($self, $table) = @_;
45 # Use the default support if available
46 return $self->next::method($table)
47 if $DBD::Pg::VERSION >= 1.50;
50 my $dbh = $self->schema->storage->dbh;
52 # Most of the SQL here is mostly based on
53 # Rose::DB::Object::Metadata::Auto::Pg, after some prodding from
54 # John Siracusa to use his superior SQL code :)
56 my $attr_sth = $self->{_cache}->{pg_attr_sth} ||= $dbh->prepare(
57 q{SELECT attname FROM pg_catalog.pg_attribute
58 WHERE attrelid = ? AND attnum = ?}
61 my $uniq_sth = $self->{_cache}->{pg_uniq_sth} ||= $dbh->prepare(
62 q{SELECT x.indrelid, i.relname, x.indkey
65 JOIN pg_catalog.pg_class c ON c.oid = x.indrelid
66 JOIN pg_catalog.pg_class i ON i.oid = x.indexrelid
67 JOIN pg_catalog.pg_constraint con ON con.conname = i.relname
68 LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
70 x.indisunique = 't' AND
78 $uniq_sth->execute($self->db_schema, $table);
79 while(my $row = $uniq_sth->fetchrow_arrayref) {
80 my ($tableid, $indexname, $col_nums) = @$row;
81 $col_nums =~ s/^\s+//;
82 my @col_nums = split(/\s+/, $col_nums);
86 $attr_sth->execute($tableid, $_);
87 my $name_aref = $attr_sth->fetchrow_arrayref;
88 push(@col_names, $name_aref->[0]) if $name_aref;
92 warn "Failed to parse UNIQUE constraint $indexname on $table";
95 push(@uniqs, [ $indexname => \@col_names ]);
103 my ( $self, $table ) = @_;
104 my ($table_comment) = $self->schema->storage->dbh->selectrow_array(
105 q{SELECT obj_description(oid)
107 WHERE relname=? AND relnamespace=(
108 SELECT oid FROM pg_namespace WHERE nspname=?)
109 }, undef, $table, $self->db_schema
111 return $table_comment
115 sub _column_comment {
116 my ( $self, $table, $column_number ) = @_;
117 my ($table_oid) = $self->schema->storage->dbh->selectrow_array(
120 WHERE relname=? AND relnamespace=(
121 SELECT oid FROM pg_namespace WHERE nspname=?)
122 }, undef, $table, $self->db_schema
124 return $self->schema->storage->dbh->selectrow_array('SELECT col_description(?,?)', undef, $table_oid,
128 # Make sure data_type's that don't need it don't have a 'size' column_info, and
129 # set the correct precision for datetime and varbit types.
130 sub _columns_info_for {
134 my $result = $self->next::method(@_);
136 foreach my $col (keys %$result) {
137 my $data_type = $result->{$col}{data_type};
139 # these types are fixed size
141 /^(?: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) {
142 delete $result->{$col}{size};
144 # for datetime types, check if it has a precision or not
145 elsif ($data_type =~ /^(?:interval|time|timestamp)\b/i) {
146 my ($precision) = $self->schema->storage->dbh
147 ->selectrow_array(<<EOF, {}, $table, $col);
148 SELECT datetime_precision
149 FROM information_schema.columns
150 WHERE table_name = ? and column_name = ?
153 if ($data_type =~ /^time\b/i) {
154 if ((not $precision) || $precision !~ /^\d/) {
155 delete $result->{$col}{size};
158 my ($integer_datetimes) = $self->schema->storage->dbh
159 ->selectrow_array('show integer_datetimes');
162 $integer_datetimes =~ /^on\z/i ? 6 : 10;
164 if ($precision == $max_precision) {
165 delete $result->{$col}{size};
168 $result->{$col}{size} = $precision;
172 elsif ((not $precision) || $precision !~ /^\d/ || $precision == 6) {
173 delete $result->{$col}{size};
176 $result->{$col}{size} = $precision;
179 elsif ($data_type =~ /^(?:bit varying|varbit)\z/i) {
180 my ($precision) = $self->schema->storage->dbh
181 ->selectrow_array(<<EOF, {}, $table, $col);
182 SELECT character_maximum_length
183 FROM information_schema.columns
184 WHERE table_name = ? and column_name = ?
187 $result->{$col}{size} = $precision;
189 elsif ($data_type =~ /^(?:numeric|decimal)\z/i) {
190 my $size = $result->{$col}{size};
193 my ($scale, $precision) = split /,/, $size;
195 $result->{$col}{size} = [ $precision, $scale ];
202 sub _extra_column_info {
203 my ($self, $info) = @_;
206 if ($info->{COLUMN_DEF} && $info->{COLUMN_DEF} =~ /\bnextval\(/i) {
207 $extra_info{is_auto_increment} = 1;
215 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
216 L<DBIx::Class::Schema::Loader::DBI>
220 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
224 This library is free software; you can redistribute it and/or modify it under
225 the same terms as Perl itself.