1 package DBIx::Class::Schema::Loader::DBI::Pg;
5 use base 'DBIx::Class::Schema::Loader::DBI::Component::QuotedDefault';
8 our $VERSION = '0.07021';
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');
43 sub _table_uniq_info {
44 my ($self, $table) = @_;
46 # Use the default support if available
47 return $self->next::method($table)
48 if $DBD::Pg::VERSION >= 1.50;
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} ||= $self->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} ||= $self->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($table->schema, $table->name);
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, $self->_lc($name_aref->[0])) if $name_aref;
92 warn "Failed to parse UNIQUE constraint $indexname on $table";
95 push(@uniqs, [ $indexname => \@col_names ]);
106 my $table_comment = $self->next::method(@_);
108 return $table_comment if $table_comment;
110 ($table_comment) = $self->dbh->selectrow_array(<<'EOF', {}, $table->name, $table->schema);
111 SELECT obj_description(oid)
113 WHERE relname=? AND relnamespace=(SELECT oid FROM pg_namespace WHERE nspname=?)
116 return $table_comment
120 sub _column_comment {
122 my ($table, $column_number, $column_name) = @_;
124 my $column_comment = $self->next::method(@_);
126 return $column_comment if $column_comment;
128 my ($table_oid) = $self->dbh->selectrow_array(<<'EOF', {}, $table->name, $table->schema);
131 WHERE relname=? AND relnamespace=(SELECT oid FROM pg_namespace WHERE nspname=?)
134 return $self->dbh->selectrow_array('SELECT col_description(?,?)', {}, $table_oid, $column_number);
137 # Make sure data_type's that don't need it don't have a 'size' column_info, and
138 # set the correct precision for datetime and varbit types.
139 sub _columns_info_for {
143 my $result = $self->next::method(@_);
145 while (my ($col, $info) = each %$result) {
146 my $data_type = $info->{data_type};
148 # these types are fixed size
149 # XXX should this be a negative match?
151 /^(?: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) {
152 delete $info->{size};
154 # for datetime types, check if it has a precision or not
155 elsif ($data_type =~ /^(?:interval|time|timestamp)\b/i) {
156 if (lc($data_type) eq 'timestamp without time zone') {
157 $info->{data_type} = 'timestamp';
159 elsif (lc($data_type) eq 'time without time zone') {
160 $info->{data_type} = 'time';
163 my ($precision) = $self->schema->storage->dbh
164 ->selectrow_array(<<EOF, {}, $table->name, $col);
165 SELECT datetime_precision
166 FROM information_schema.columns
167 WHERE table_name = ? and column_name = ?
170 if ($data_type =~ /^time\b/i) {
171 if ((not $precision) || $precision !~ /^\d/) {
172 delete $info->{size};
175 my ($integer_datetimes) = $self->dbh
176 ->selectrow_array('show integer_datetimes');
179 $integer_datetimes =~ /^on\z/i ? 6 : 10;
181 if ($precision == $max_precision) {
182 delete $info->{size};
185 $info->{size} = $precision;
189 elsif ((not $precision) || $precision !~ /^\d/ || $precision == 6) {
190 delete $info->{size};
193 $info->{size} = $precision;
196 elsif ($data_type =~ /^(?:bit(?: varying)?|varbit)\z/i) {
197 $info->{data_type} = 'varbit' if $data_type =~ /var/i;
199 my ($precision) = $self->dbh->selectrow_array(<<EOF, {}, $table->name, $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 my ($typetype) = $self->schema->storage->dbh
229 ->selectrow_array(<<EOF, {}, $data_type);
231 FROM pg_catalog.pg_type
234 if ($typetype && $typetype eq 'e') {
235 # The following will extract a list of allowed values for the
237 my $typevalues = $self->dbh
238 ->selectall_arrayref(<<EOF, {}, $info->{data_type});
240 FROM pg_catalog.pg_enum e
241 JOIN pg_catalog.pg_type t ON t.oid = e.enumtypid
245 $info->{extra}{list} = [ map { $_->[0] } @$typevalues ];
247 # Store its original name in extra for SQLT to pick up.
248 $info->{extra}{custom_type_name} = $info->{data_type};
250 $info->{data_type} = 'enum';
252 delete $info->{size};
256 # process SERIAL columns
257 if (ref($info->{default_value}) eq 'SCALAR'
258 && ${ $info->{default_value} } =~ /\bnextval\('([^:]+)'/i) {
259 $info->{is_auto_increment} = 1;
260 $info->{sequence} = $1;
261 delete $info->{default_value};
264 # alias now() to current_timestamp for deploying to other DBs
265 if ((eval { lc ${ $info->{default_value} } }||'') eq 'now()') {
266 # do not use a ref to a constant, that breaks Data::Dump output
267 ${$info->{default_value}} = 'current_timestamp';
270 $info->{original}{default_value} = \$now;
273 # detect 0/1 for booleans and rewrite
274 if ($data_type =~ /^bool/i && exists $info->{default_value}) {
275 if ($info->{default_value} eq '0') {
277 $info->{default_value} = \$false;
279 elsif ($info->{default_value} eq '1') {
281 $info->{default_value} = \$true;
291 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
292 L<DBIx::Class::Schema::Loader::DBI>
296 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
300 This library is free software; you can redistribute it and/or modify it under
301 the same terms as Perl itself.