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.07001';
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);
44 elsif ($self->preserve_case) {
45 $self->schema->storage->sql_maker->quote_char('"');
46 $self->schema->storage->sql_maker->name_sep('.');
50 sub _table_uniq_info {
51 my ($self, $table) = @_;
53 # Use the default support if available
54 return $self->next::method($table)
55 if $DBD::Pg::VERSION >= 1.50;
58 my $dbh = $self->schema->storage->dbh;
60 # Most of the SQL here is mostly based on
61 # Rose::DB::Object::Metadata::Auto::Pg, after some prodding from
62 # John Siracusa to use his superior SQL code :)
64 my $attr_sth = $self->{_cache}->{pg_attr_sth} ||= $dbh->prepare(
65 q{SELECT attname FROM pg_catalog.pg_attribute
66 WHERE attrelid = ? AND attnum = ?}
69 my $uniq_sth = $self->{_cache}->{pg_uniq_sth} ||= $dbh->prepare(
70 q{SELECT x.indrelid, i.relname, x.indkey
73 JOIN pg_catalog.pg_class c ON c.oid = x.indrelid
74 JOIN pg_catalog.pg_class i ON i.oid = x.indexrelid
75 JOIN pg_catalog.pg_constraint con ON con.conname = i.relname
76 LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
78 x.indisunique = 't' AND
86 $uniq_sth->execute($self->db_schema, $table);
87 while(my $row = $uniq_sth->fetchrow_arrayref) {
88 my ($tableid, $indexname, $col_nums) = @$row;
89 $col_nums =~ s/^\s+//;
90 my @col_nums = split(/\s+/, $col_nums);
94 $attr_sth->execute($tableid, $_);
95 my $name_aref = $attr_sth->fetchrow_arrayref;
96 push(@col_names, $name_aref->[0]) if $name_aref;
100 warn "Failed to parse UNIQUE constraint $indexname on $table";
103 push(@uniqs, [ $indexname => \@col_names ]);
111 my ( $self, $table ) = @_;
112 my ($table_comment) = $self->schema->storage->dbh->selectrow_array(
113 q{SELECT obj_description(oid)
115 WHERE relname=? AND relnamespace=(
116 SELECT oid FROM pg_namespace WHERE nspname=?)
117 }, undef, $table, $self->db_schema
119 return $table_comment
123 sub _column_comment {
124 my ( $self, $table, $column_number ) = @_;
125 my ($table_oid) = $self->schema->storage->dbh->selectrow_array(
128 WHERE relname=? AND relnamespace=(
129 SELECT oid FROM pg_namespace WHERE nspname=?)
130 }, undef, $table, $self->db_schema
132 return $self->schema->storage->dbh->selectrow_array('SELECT col_description(?,?)', undef, $table_oid,
136 # Make sure data_type's that don't need it don't have a 'size' column_info, and
137 # set the correct precision for datetime and varbit types.
138 sub _columns_info_for {
142 my $result = $self->next::method(@_);
144 while (my ($col, $info) = each %$result) {
145 my $data_type = $info->{data_type};
147 # these types are fixed size
148 # XXX should this be a negative match?
150 /^(?:bigint|int8|bigserial|serial8|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) {
151 delete $info->{size};
153 # for datetime types, check if it has a precision or not
154 elsif ($data_type =~ /^(?:interval|time|timestamp)\b/i) {
155 if (lc($data_type) eq 'timestamp without time zone') {
156 $info->{data_type} = 'timestamp';
158 elsif (lc($data_type) eq 'time without time zone') {
159 $info->{data_type} = 'time';
162 my ($precision) = $self->schema->storage->dbh
163 ->selectrow_array(<<EOF, {}, $table, $col);
164 SELECT datetime_precision
165 FROM information_schema.columns
166 WHERE table_name = ? and column_name = ?
169 if ($data_type =~ /^time\b/i) {
170 if ((not $precision) || $precision !~ /^\d/) {
171 delete $info->{size};
174 my ($integer_datetimes) = $self->schema->storage->dbh
175 ->selectrow_array('show integer_datetimes');
178 $integer_datetimes =~ /^on\z/i ? 6 : 10;
180 if ($precision == $max_precision) {
181 delete $info->{size};
184 $info->{size} = $precision;
188 elsif ((not $precision) || $precision !~ /^\d/ || $precision == 6) {
189 delete $info->{size};
192 $info->{size} = $precision;
195 elsif ($data_type =~ /^(?:bit(?: varying)?|varbit)\z/i) {
196 $info->{data_type} = 'varbit' if $data_type =~ /var/i;
198 my ($precision) = $self->schema->storage->dbh
199 ->selectrow_array(<<EOF, {}, $table, $col);
200 SELECT character_maximum_length
201 FROM information_schema.columns
202 WHERE table_name = ? and column_name = ?
205 $info->{size} = $precision if $precision;
207 $info->{size} = 1 if (not $precision) && lc($data_type) eq 'bit';
209 elsif ($data_type =~ /^(?:numeric|decimal)\z/i && (my $size = $info->{size})) {
212 my ($scale, $precision) = split /,/, $size;
214 $info->{size} = [ $precision, $scale ];
216 elsif (lc($data_type) eq 'character varying') {
217 $info->{data_type} = 'varchar';
219 if (not $info->{size}) {
220 $info->{data_type} = 'text';
221 $info->{original}{data_type} = 'varchar';
224 elsif (lc($data_type) eq 'character') {
225 $info->{data_type} = 'char';
228 # process SERIAL columns
229 if (ref($info->{default_value}) eq 'SCALAR' && ${ $info->{default_value} } =~ /\bnextval\(['"]([.\w]+)/i) {
230 $info->{is_auto_increment} = 1;
231 $info->{sequence} = $1;
232 delete $info->{default_value};
235 # alias now() to current_timestamp for deploying to other DBs
236 if ((eval { lc ${ $info->{default_value} } }||'') eq 'now()') {
237 # do not use a ref to a constant, that breaks Data::Dump output
238 ${$info->{default_value}} = 'current_timestamp';
241 $info->{original}{default_value} = \$now;
250 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
251 L<DBIx::Class::Schema::Loader::DBI>
255 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
259 This library is free software; you can redistribute it and/or modify it under
260 the same terms as Perl itself.