Pg.pm 6.79 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
# -*- Mode: perl; indent-tabs-mode: nil -*-
#
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code is the Bugzilla Bug Tracking System.
#
# The Initial Developer of the Original Code is Netscape Communications
# Corporation. Portions created by Netscape are
# Copyright (C) 1998 Netscape Communications Corporation. All
# Rights Reserved.
#
# Contributor(s): Dave Miller <davem00@aol.com>
#                 Gayathri Swaminath <gayathrik00@aol.com>
#                 Jeroen Ruigrok van der Werven <asmodai@wxs.nl>
#                 Dave Lawrence <dkl@redhat.com>
#                 Tomas Kopal <Tomas.Kopal@altap.cz>
25
#                 Max Kanat-Alexander <mkanat@bugzilla.org>
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

=head1 NAME

Bugzilla::DB::Pg - Bugzilla database compatibility layer for PostgreSQL

=head1 DESCRIPTION

This module overrides methods of the Bugzilla::DB module with PostgreSQL
specific implementation. It is instantiated by the Bugzilla::DB module
and should never be used directly.

For interface details see L<Bugzilla::DB> and L<DBI>.

=cut

package Bugzilla::DB::Pg;

use strict;

use Bugzilla::Error;
46
use DBD::Pg;
47 48 49 50

# This module extends the DB interface via inheritance
use base qw(Bugzilla::DB);

51
use constant BLOB_TYPE => { pg_type => DBD::Pg::PG_BYTEA };
52
use constant REQUIRED_VERSION => '7.03.0000';
53
use constant PROGRAM_NAME => 'PostgreSQL';
54
use constant MODULE_NAME  => 'Pg';
55
use constant DBD_VERSION  => '1.31';
56

57 58 59
sub new {
    my ($class, $user, $pass, $host, $dbname, $port) = @_;

60 61 62 63 64
    # The default database name for PostgreSQL. We have
    # to connect to SOME database, even if we have
    # no $dbname parameter.
    $dbname ||= 'template1';

65
    # construct the DSN from the parameters we got
66 67 68
    my $dsn = "DBI:Pg:host=$host;dbname=$dbname";
    $dsn .= ";port=$port" if $port;
   
69 70 71 72
    my $self = $class->db_new($dsn, $user, $pass);

    # all class local variables stored in DBI derived class needs to have
    # a prefix 'private_'. See DBI documentation.
73
    $self->{private_bz_tables_locked} = "";
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

    bless ($self, $class);

    return $self;
}

# if last_insert_id is supported on PostgreSQL by lowest DBI/DBD version
# supported by Bugzilla, this implementation can be removed.
sub bz_last_key {
    my ($self, $table, $column) = @_;

    my $seq = $table . "_" . $column . "_seq";
    my ($last_insert_id) = $self->selectrow_array("SELECT CURRVAL('$seq')");

    return $last_insert_id;
}

sub sql_regexp {
92
    return "~*";
93 94 95
}

sub sql_not_regexp {
96
    return "!~*" 
97 98 99
}

sub sql_limit {
100
    my ($self, $limit, $offset) = @_;
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137

    if (defined($offset)) {
        return "LIMIT $limit OFFSET $offset";
    } else {
        return "LIMIT $limit";
    }
}

sub sql_to_days {
    my ($self, $date) = @_;

    return "TO_CHAR($date, 'J')::int";
}

sub sql_date_format {
    my ($self, $date, $format) = @_;
    
    $format = "%Y.%m.%d %H:%i:%s" if !$format;

    $format =~ s/\%Y/YYYY/g;
    $format =~ s/\%y/YY/g;
    $format =~ s/\%m/MM/g;
    $format =~ s/\%d/DD/g;
    $format =~ s/\%a/Dy/g;
    $format =~ s/\%H/HH24/g;
    $format =~ s/\%i/MI/g;
    $format =~ s/\%s/SS/g;

    return "TO_CHAR($date, " . $self->quote($format) . ")";
}

sub sql_interval {
    my ($self, $interval) = @_;
    
    return "INTERVAL '$interval'";
}

138 139 140 141 142 143
sub sql_string_concat {
    my ($self, @params) = @_;
    
    # Postgres 7.3 does not support concatenating of different types, so we
    # need to cast both parameters to text. Version 7.4 seems to handle this
    # properly, so when we stop support 7.3, this can be removed.
144
    return '(CAST(' . join(' AS text) || CAST(', @params) . ' AS text))';
145 146
}

147 148 149
sub bz_lock_tables {
    my ($self, @tables) = @_;
   
150
    my $list = join(', ', @tables);
151 152
    # Check first if there was no lock before
    if ($self->{private_bz_tables_locked}) {
153 154
        ThrowCodeError("already_locked", { current => $self->{private_bz_tables_locked},
                                           new => $list });
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
    } else {
        my %read_tables;
        my %write_tables;
        foreach my $table (@tables) {
            $table =~ /^([\d\w]+)([\s]+AS[\s]+[\d\w]+)?[\s]+(WRITE|READ)$/i;
            my $table_name = $1;
            if ($3 =~ /READ/i) {
                if (!exists $read_tables{$table_name}) {
                    $read_tables{$table_name} = undef;
                }
            }
            else {
                if (!exists $write_tables{$table_name}) {
                    $write_tables{$table_name} = undef;
                }
            }
        }
    
        # Begin Transaction
        $self->bz_start_transaction();
        
        Bugzilla->dbh->do('LOCK TABLE ' . join(', ', keys %read_tables) .
                          ' IN ROW SHARE MODE') if keys %read_tables;
        Bugzilla->dbh->do('LOCK TABLE ' . join(', ', keys %write_tables) .
                          ' IN ROW EXCLUSIVE MODE') if keys %write_tables;
180
        $self->{private_bz_tables_locked} = $list;
181 182 183 184 185 186 187 188 189 190 191 192
    }
}

sub bz_unlock_tables {
    my ($self, $abort) = @_;
    
    # Check first if there was previous matching lock
    if (!$self->{private_bz_tables_locked}) {
        # Abort is allowed even without previous lock for error handling
        return if $abort;
        ThrowCodeError("no_matching_lock");
    } else {
193
        $self->{private_bz_tables_locked} = "";
194 195 196 197 198 199 200 201 202
        # End transaction, tables will be unlocked automatically
        if ($abort) {
            $self->bz_rollback_transaction();
        } else {
            $self->bz_commit_transaction();
        }
    }
}

203 204 205 206 207 208 209 210 211 212 213 214 215 216
#####################################################################
# Custom Database Setup
#####################################################################

sub bz_setup_database {
    my $self = shift;
    $self->SUPER::bz_setup_database(@_);

    # PostgreSQL doesn't like having *any* index on the thetext
    # field, because it can't have index data longer than 2770
    # characters on that field.
    $self->bz_drop_index('longdescs', 'longdescs_thetext_idx');
}

217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
#####################################################################
# Custom Schema Information Functions
#####################################################################

# Pg includes the PostgreSQL system tables in table_list_real, so 
# we need to remove those.
sub bz_table_list_real {
    my $self = shift;

    my @full_table_list = $self->SUPER::bz_table_list_real(@_);
    # All PostgreSQL system tables start with "pg_" or "sql_"
    my @table_list = grep(!/(^pg_)|(^sql_)/, @full_table_list);
    return @table_list;
}

232
1;