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.07010';
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('.');
51 my ($self, $opts) = @_;
53 my $dbh = $self->schema->storage->dbh;
54 my @tables = $dbh->tables(undef, $self->db_schema, '%', '%');
56 my $schema_quoted = $tables[0] =~ /^"/;
59 s/^"[^"]+"\.// for @tables;
62 s/^[^.]+\.// for @tables;
65 s/^"([^"]+)"\z/$1/ for @tables;
67 return $self->_filter_tables(\@tables, $opts);
70 sub _table_uniq_info {
71 my ($self, $table) = @_;
73 # Use the default support if available
74 return $self->next::method($table)
75 if $DBD::Pg::VERSION >= 1.50;
78 my $dbh = $self->schema->storage->dbh;
80 # Most of the SQL here is mostly based on
81 # Rose::DB::Object::Metadata::Auto::Pg, after some prodding from
82 # John Siracusa to use his superior SQL code :)
84 my $attr_sth = $self->{_cache}->{pg_attr_sth} ||= $dbh->prepare(
85 q{SELECT attname FROM pg_catalog.pg_attribute
86 WHERE attrelid = ? AND attnum = ?}
89 my $uniq_sth = $self->{_cache}->{pg_uniq_sth} ||= $dbh->prepare(
90 q{SELECT x.indrelid, i.relname, x.indkey
93 JOIN pg_catalog.pg_class c ON c.oid = x.indrelid
94 JOIN pg_catalog.pg_class i ON i.oid = x.indexrelid
95 JOIN pg_catalog.pg_constraint con ON con.conname = i.relname
96 LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
98 x.indisunique = 't' AND
101 con.contype = 'u' AND
106 $uniq_sth->execute($self->db_schema, $table);
107 while(my $row = $uniq_sth->fetchrow_arrayref) {
108 my ($tableid, $indexname, $col_nums) = @$row;
109 $col_nums =~ s/^\s+//;
110 my @col_nums = split(/\s+/, $col_nums);
113 foreach (@col_nums) {
114 $attr_sth->execute($tableid, $_);
115 my $name_aref = $attr_sth->fetchrow_arrayref;
116 push(@col_names, $name_aref->[0]) if $name_aref;
120 warn "Failed to parse UNIQUE constraint $indexname on $table";
123 push(@uniqs, [ $indexname => \@col_names ]);
131 my ( $self, $table ) = @_;
132 my ($table_comment) = $self->schema->storage->dbh->selectrow_array(
133 q{SELECT obj_description(oid)
135 WHERE relname=? AND relnamespace=(
136 SELECT oid FROM pg_namespace WHERE nspname=?)
137 }, undef, $table, $self->db_schema
139 return $table_comment
143 sub _column_comment {
144 my ( $self, $table, $column_number ) = @_;
145 my ($table_oid) = $self->schema->storage->dbh->selectrow_array(
148 WHERE relname=? AND relnamespace=(
149 SELECT oid FROM pg_namespace WHERE nspname=?)
150 }, undef, $table, $self->db_schema
152 return $self->schema->storage->dbh->selectrow_array('SELECT col_description(?,?)', undef, $table_oid,
156 # Make sure data_type's that don't need it don't have a 'size' column_info, and
157 # set the correct precision for datetime and varbit types.
158 sub _columns_info_for {
162 my $result = $self->next::method(@_);
164 while (my ($col, $info) = each %$result) {
165 my $data_type = $info->{data_type};
167 # these types are fixed size
168 # XXX should this be a negative match?
170 /^(?: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) {
171 delete $info->{size};
173 # for datetime types, check if it has a precision or not
174 elsif ($data_type =~ /^(?:interval|time|timestamp)\b/i) {
175 if (lc($data_type) eq 'timestamp without time zone') {
176 $info->{data_type} = 'timestamp';
178 elsif (lc($data_type) eq 'time without time zone') {
179 $info->{data_type} = 'time';
182 my ($precision) = $self->schema->storage->dbh
183 ->selectrow_array(<<EOF, {}, $table, $col);
184 SELECT datetime_precision
185 FROM information_schema.columns
186 WHERE table_name = ? and column_name = ?
189 if ($data_type =~ /^time\b/i) {
190 if ((not $precision) || $precision !~ /^\d/) {
191 delete $info->{size};
194 my ($integer_datetimes) = $self->schema->storage->dbh
195 ->selectrow_array('show integer_datetimes');
198 $integer_datetimes =~ /^on\z/i ? 6 : 10;
200 if ($precision == $max_precision) {
201 delete $info->{size};
204 $info->{size} = $precision;
208 elsif ((not $precision) || $precision !~ /^\d/ || $precision == 6) {
209 delete $info->{size};
212 $info->{size} = $precision;
215 elsif ($data_type =~ /^(?:bit(?: varying)?|varbit)\z/i) {
216 $info->{data_type} = 'varbit' if $data_type =~ /var/i;
218 my ($precision) = $self->schema->storage->dbh
219 ->selectrow_array(<<EOF, {}, $table, $col);
220 SELECT character_maximum_length
221 FROM information_schema.columns
222 WHERE table_name = ? and column_name = ?
225 $info->{size} = $precision if $precision;
227 $info->{size} = 1 if (not $precision) && lc($data_type) eq 'bit';
229 elsif ($data_type =~ /^(?:numeric|decimal)\z/i && (my $size = $info->{size})) {
232 my ($scale, $precision) = split /,/, $size;
234 $info->{size} = [ $precision, $scale ];
236 elsif (lc($data_type) eq 'character varying') {
237 $info->{data_type} = 'varchar';
239 if (not $info->{size}) {
240 $info->{data_type} = 'text';
241 $info->{original}{data_type} = 'varchar';
244 elsif (lc($data_type) eq 'character') {
245 $info->{data_type} = 'char';
248 my ($typetype) = $self->schema->storage->dbh
249 ->selectrow_array(<<EOF, {}, $data_type);
251 FROM pg_catalog.pg_type
254 if ($typetype eq 'e') {
255 # The following will extract a list of allowed values for the
257 my $typevalues = $self->schema->storage->dbh
258 ->selectall_arrayref(<<EOF, {}, $info->{data_type});
260 FROM pg_catalog.pg_enum e
261 JOIN pg_catalog.pg_type t ON t.oid = e.enumtypid
265 $info->{extra}{list} = [ map { $_->[0] } @$typevalues ];
267 # Store its original name in extra for SQLT to pick up.
268 $info->{extra}{custom_type_name} = $info->{data_type};
270 $info->{data_type} = 'enum';
272 delete $info->{size};
276 # process SERIAL columns
277 if (ref($info->{default_value}) eq 'SCALAR'
278 && ${ $info->{default_value} } =~ /\bnextval\('([^:]+)'/i) {
279 $info->{is_auto_increment} = 1;
280 $info->{sequence} = $1;
281 delete $info->{default_value};
284 # alias now() to current_timestamp for deploying to other DBs
285 if ((eval { lc ${ $info->{default_value} } }||'') eq 'now()') {
286 # do not use a ref to a constant, that breaks Data::Dump output
287 ${$info->{default_value}} = 'current_timestamp';
290 $info->{original}{default_value} = \$now;
299 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
300 L<DBIx::Class::Schema::Loader::DBI>
304 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
308 This library is free software; you can redistribute it and/or modify it under
309 the same terms as Perl itself.