Subversion Repositories DevTools

Rev

Rev 1295 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1293 dpurdie 1
###############################################################################
2
# Codestriker: Copyright (c) 2001, 2002 David Sitsky.  All rights reserved.
3
# sits@users.sourceforge.net
4
#
5
# This program is free software; you can redistribute it and modify it under
6
# the terms of the GPL.
7
 
8
package Codestriker::DB::PostgreSQL;
9
 
10
use strict;
11
use DBI;
12
use Codestriker;
13
use Codestriker::DB::Database;
14
use Codestriker::DB::Column;
15
 
16
# Module for handling a PostgreSQL database.
17
 
18
@Codestriker::DB::PostgreSQL::ISA = ("Codestriker::DB::Database");
19
 
20
# Type mappings.
21
my $_TYPE = {
22
    $Codestriker::DB::Column::TYPE->{TEXT}	=> "text",
23
    $Codestriker::DB::Column::TYPE->{VARCHAR}	=> "varchar",
24
    $Codestriker::DB::Column::TYPE->{INT32}	=> "int",
25
    $Codestriker::DB::Column::TYPE->{INT16}	=> "smallint",
26
    $Codestriker::DB::Column::TYPE->{DATETIME}	=> "timestamp",
27
    $Codestriker::DB::Column::TYPE->{FLOAT}	=> "float"
28
};
29
 
30
# Create a new PostgreSQL database object.
31
sub new {
32
    my $type = shift;
33
    my $url = shift;
34
 
35
    # Database is parent class.
36
    my $self = Codestriker::DB::Database->new();
37
    $self->{sequence_created} = 0;
38
 
39
    # Determine which PostgreSQL driver to use.
40
    $url =~ /^DBI:(Pg\w*):/;
41
    $self->{driver} = $1;
42
 
43
    return bless $self, $type;
44
}
45
 
46
# Return the DBD module this is dependent on.
47
sub get_module_dependencies {
48
    my $self = shift;
49
    return { name => 'DBD::' . $self->{driver}, version => '0' };
50
}
51
 
52
sub getDBDModuleVer {
53
}
54
 
55
# Retrieve a database connection.
56
sub get_connection {
57
    my $self = shift;
58
 
59
    # PostgreSQL supports transactions, don't enable auto_commit.
60
    return $self->_get_connection(0, 1);
61
}
62
 
63
# Return the mapping for a specific type.
64
sub _map_type {
65
    my ($self, $type) = @_;
66
    return $_TYPE->{$type};
67
}
68
 
69
# Autoincrement type, which is based off a sequence.
70
sub _get_autoincrement_type {
71
    return "default nextval('sequence')";
72
}
73
 
74
# Create the table in the database for the specified table, and with the
75
# provided type mappings.
76
sub create_table {
77
    my ($self, $table) = @_;
78
 
79
    # Make sure the sequence is present in the database for autoincrement
80
    # fields.
81
    if ($self->{sequence_created} == 0) {
82
	my $dbh = $self->{dbh};
83
 
84
	eval {
85
	    $dbh->{PrintError} = 0;
86
 
87
	    $dbh->do("CREATE SEQUENCE sequence");
88
	    print "Created sequence\n";
89
	    $self->commit();
90
	};
91
	if ($@) {
92
	    eval { $self->rollback() };
93
	}
94
 
95
	$dbh->{PrintError} = 1;
96
	$self->{sequence_created} = 1;
97
    }
98
 
99
    # Now let the base class actually do the work in creating the table.
100
    $self->SUPER::create_table($table);
101
}
102
 
103
# Indicate if the LIKE operator can be applied on a "text" field.
104
# For PostgreSQL, this is true.
105
sub has_like_operator_for_text_field {
106
    my $self = shift;
107
    return 1;
108
}
109
 
110
# Function for generating an SQL subexpression for a case insensitive LIKE
111
# operation.
112
sub case_insensitive_like {
113
    my ($self, $field, $expression) = @_;
114
 
115
    $expression = $self->{dbh}->quote($expression);
116
 
117
    # Use the ILIKE operator.
118
    return "$field ILIKE $expression";
119
}
120
 
121
1;
122
 
123
 
124