1 package DBIx::Class::Schema::Loader::DBI::Pg;
5 use base 'DBIx::Class::Schema::Loader::DBI::Component::QuotedDefault';
8 our $VERSION = '0.07029';
12 DBIx::Class::Schema::Loader::DBI::Pg - DBIx::Class::Schema::Loader::DBI
13 PostgreSQL Implementation.
17 See L<DBIx::Class::Schema::Loader> and L<DBIx::Class::Schema::Loader::Base>.
24 $self->next::method(@_);
26 $self->{db_schema} ||= ['public'];
28 if (not defined $self->preserve_case) {
29 $self->preserve_case(0);
31 elsif ($self->preserve_case) {
32 $self->schema->storage->sql_maker->quote_char('"');
33 $self->schema->storage->sql_maker->name_sep('.');
40 return ($self->next::method(@_), 'pg_catalog');
44 my ($self, $table) = @_;
46 my $sth = $self->dbh->prepare_cached(<<"EOF");
47 SELECT rc.constraint_name, rc.unique_constraint_schema, uk_tc.table_name,
48 fk_kcu.column_name, uk_kcu.column_name, rc.delete_rule, rc.update_rule,
50 FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS fk_tc
51 JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc
52 ON rc.constraint_name = fk_tc.constraint_name
53 AND rc.constraint_schema = fk_tc.table_schema
54 JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE fk_kcu
55 ON fk_kcu.constraint_name = fk_tc.constraint_name
56 AND fk_kcu.table_name = fk_tc.table_name
57 AND fk_kcu.table_schema = fk_tc.table_schema
58 JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS uk_tc
59 ON uk_tc.constraint_name = rc.unique_constraint_name
60 AND uk_tc.table_schema = rc.unique_constraint_schema
61 JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE uk_kcu
62 ON uk_kcu.constraint_name = rc.unique_constraint_name
63 AND uk_kcu.ordinal_position = fk_kcu.ordinal_position
64 AND uk_kcu.table_name = uk_tc.table_name
65 AND uk_kcu.table_schema = rc.unique_constraint_schema
66 WHERE fk_tc.table_name = ?
67 AND fk_tc.table_schema = ?
68 ORDER BY fk_kcu.ordinal_position
71 $sth->execute($table->name, $table->schema);
75 while (my ($fk, $remote_schema, $remote_table, $col, $remote_col,
76 $delete_rule, $update_rule, $is_deferrable) = $sth->fetchrow_array) {
77 push @{ $rels{$fk}{local_columns} }, $self->_lc($col);
78 push @{ $rels{$fk}{remote_columns} }, $self->_lc($remote_col);
80 $rels{$fk}{remote_table} = DBIx::Class::Schema::Loader::Table->new(
82 name => $remote_table,
83 schema => $remote_schema,
84 ) unless exists $rels{$fk}{remote_table};
86 $rels{$fk}{attrs} ||= {
87 on_delete => uc $delete_rule,
88 on_update => uc $update_rule,
89 is_deferrable => uc $is_deferrable eq 'YES' ? 1 : 0,
93 return [ values %rels ];
97 sub _table_uniq_info {
98 my ($self, $table) = @_;
100 # Use the default support if available
101 return $self->next::method($table)
102 if $DBD::Pg::VERSION >= 1.50;
106 # Most of the SQL here is mostly based on
107 # Rose::DB::Object::Metadata::Auto::Pg, after some prodding from
108 # John Siracusa to use his superior SQL code :)
110 my $attr_sth = $self->{_cache}->{pg_attr_sth} ||= $self->dbh->prepare(
111 q{SELECT attname FROM pg_catalog.pg_attribute
112 WHERE attrelid = ? AND attnum = ?}
115 my $uniq_sth = $self->{_cache}->{pg_uniq_sth} ||= $self->dbh->prepare(
116 q{SELECT x.indrelid, i.relname, x.indkey
118 pg_catalog.pg_index x
119 JOIN pg_catalog.pg_class c ON c.oid = x.indrelid
120 JOIN pg_catalog.pg_class i ON i.oid = x.indexrelid
121 JOIN pg_catalog.pg_constraint con ON con.conname = i.relname
122 LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
124 x.indisunique = 't' AND
127 con.contype = 'u' AND
132 $uniq_sth->execute($table->schema, $table->name);
133 while(my $row = $uniq_sth->fetchrow_arrayref) {
134 my ($tableid, $indexname, $col_nums) = @$row;
135 $col_nums =~ s/^\s+//;
136 my @col_nums = split(/\s+/, $col_nums);
139 foreach (@col_nums) {
140 $attr_sth->execute($tableid, $_);
141 my $name_aref = $attr_sth->fetchrow_arrayref;
142 push(@col_names, $self->_lc($name_aref->[0])) if $name_aref;
146 warn "Failed to parse UNIQUE constraint $indexname on $table";
149 push(@uniqs, [ $indexname => \@col_names ]);
160 my $table_comment = $self->next::method(@_);
162 return $table_comment if $table_comment;
164 ($table_comment) = $self->dbh->selectrow_array(<<'EOF', {}, $table->name, $table->schema);
165 SELECT obj_description(oid)
167 WHERE relname=? AND relnamespace=(SELECT oid FROM pg_namespace WHERE nspname=?)
170 return $table_comment
174 sub _column_comment {
176 my ($table, $column_number, $column_name) = @_;
178 my $column_comment = $self->next::method(@_);
180 return $column_comment if $column_comment;
182 my ($table_oid) = $self->dbh->selectrow_array(<<'EOF', {}, $table->name, $table->schema);
185 WHERE relname=? AND relnamespace=(SELECT oid FROM pg_namespace WHERE nspname=?)
188 return $self->dbh->selectrow_array('SELECT col_description(?,?)', {}, $table_oid, $column_number);
191 # Make sure data_type's that don't need it don't have a 'size' column_info, and
192 # set the correct precision for datetime and varbit types.
193 sub _columns_info_for {
197 my $result = $self->next::method(@_);
199 while (my ($col, $info) = each %$result) {
200 my $data_type = $info->{data_type};
202 # these types are fixed size
203 # XXX should this be a negative match?
205 /^(?:bigint|int8|bigserial|serial8|bool(?:ean)?|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) {
206 delete $info->{size};
208 # for datetime types, check if it has a precision or not
209 elsif ($data_type =~ /^(?:interval|time|timestamp)\b/i) {
210 if (lc($data_type) eq 'timestamp without time zone') {
211 $info->{data_type} = 'timestamp';
213 elsif (lc($data_type) eq 'time without time zone') {
214 $info->{data_type} = 'time';
217 my ($precision) = $self->schema->storage->dbh
218 ->selectrow_array(<<EOF, {}, $table->name, $col);
219 SELECT datetime_precision
220 FROM information_schema.columns
221 WHERE table_name = ? and column_name = ?
224 if ($data_type =~ /^time\b/i) {
225 if ((not $precision) || $precision !~ /^\d/) {
226 delete $info->{size};
229 my ($integer_datetimes) = $self->dbh
230 ->selectrow_array('show integer_datetimes');
233 $integer_datetimes =~ /^on\z/i ? 6 : 10;
235 if ($precision == $max_precision) {
236 delete $info->{size};
239 $info->{size} = $precision;
243 elsif ((not $precision) || $precision !~ /^\d/ || $precision == 6) {
244 delete $info->{size};
247 $info->{size} = $precision;
250 elsif ($data_type =~ /^(?:bit(?: varying)?|varbit)\z/i) {
251 $info->{data_type} = 'varbit' if $data_type =~ /var/i;
253 my ($precision) = $self->dbh->selectrow_array(<<EOF, {}, $table->name, $col);
254 SELECT character_maximum_length
255 FROM information_schema.columns
256 WHERE table_name = ? and column_name = ?
259 $info->{size} = $precision if $precision;
261 $info->{size} = 1 if (not $precision) && lc($data_type) eq 'bit';
263 elsif ($data_type =~ /^(?:numeric|decimal)\z/i && (my $size = $info->{size})) {
266 my ($scale, $precision) = split /,/, $size;
268 $info->{size} = [ $precision, $scale ];
270 elsif (lc($data_type) eq 'character varying') {
271 $info->{data_type} = 'varchar';
273 if (not $info->{size}) {
274 $info->{data_type} = 'text';
275 $info->{original}{data_type} = 'varchar';
278 elsif (lc($data_type) eq 'character') {
279 $info->{data_type} = 'char';
282 my ($typetype) = $self->schema->storage->dbh
283 ->selectrow_array(<<EOF, {}, $data_type);
285 FROM pg_catalog.pg_type
288 if ($typetype && $typetype eq 'e') {
289 # The following will extract a list of allowed values for the
291 my $typevalues = $self->dbh
292 ->selectall_arrayref(<<EOF, {}, $info->{data_type});
294 FROM pg_catalog.pg_enum e
295 JOIN pg_catalog.pg_type t ON t.oid = e.enumtypid
299 $info->{extra}{list} = [ map { $_->[0] } @$typevalues ];
301 # Store its original name in extra for SQLT to pick up.
302 $info->{extra}{custom_type_name} = $info->{data_type};
304 $info->{data_type} = 'enum';
306 delete $info->{size};
310 # process SERIAL columns
311 if (ref($info->{default_value}) eq 'SCALAR'
312 && ${ $info->{default_value} } =~ /\bnextval\('([^:]+)'/i) {
313 $info->{is_auto_increment} = 1;
314 $info->{sequence} = $1;
315 delete $info->{default_value};
318 # alias now() to current_timestamp for deploying to other DBs
319 if ((eval { lc ${ $info->{default_value} } }||'') eq 'now()') {
320 # do not use a ref to a constant, that breaks Data::Dump output
321 ${$info->{default_value}} = 'current_timestamp';
324 $info->{original}{default_value} = \$now;
327 # detect 0/1 for booleans and rewrite
328 if ($data_type =~ /^bool/i && exists $info->{default_value}) {
329 if ($info->{default_value} eq '0') {
331 $info->{default_value} = \$false;
333 elsif ($info->{default_value} eq '1') {
335 $info->{default_value} = \$true;
345 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
346 L<DBIx::Class::Schema::Loader::DBI>
350 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
354 This library is free software; you can redistribute it and/or modify it under
355 the same terms as Perl itself.