~ubuntu-branches/debian/squeeze/movabletype-opensource/squeeze

« back to all changes in this revision

Viewing changes to extlib/Data/ObjectDriver/Driver/DBD/Oracle.pm

  • Committer: Bazaar Package Importer
  • Author(s): Dominic Hargreaves
  • Date: 2009-06-19 23:03:15 UTC
  • mfrom: (1.2.1 upstream) (9.1.1 karmic)
  • Revision ID: james.westby@ubuntu.com-20090619230315-rm1vcgg5iymu2zh1
Tags: 4.2.6.1-1
* New upstream release
* Update Standards-Version (no changes)
* Don't specify full path to apache2ctl in postinst (thanks, Lintian)
* Remove unused Lintian overrides
* Don't install empty directory in extlib

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# $Id$
 
2
# Contributor(s): Xiaoou Wu <xiaoou.wu@oracle.com>
 
3
#
 
4
package Data::ObjectDriver::Driver::DBD::Oracle;
 
5
 
 
6
use strict;
 
7
 
 
8
use base qw( Data::ObjectDriver::Driver::DBD );
 
9
 
 
10
use Data::ObjectDriver::SQL::Oracle;
 
11
use Data::ObjectDriver::Errors;
 
12
use DBD::Oracle qw(:ora_types);
 
13
 
 
14
sub init_dbh {
 
15
    my $dbd   = shift;
 
16
    my ($dbh) = @_;
 
17
    $dbh->{LongReadLen}      = 1024000;
 
18
    $dbh->{FetchHashKeyName} = 'NAME_lc';
 
19
    return bless $dbh, 'Data::ObjectDriver::Driver::DBD::Oracle::db';
 
20
}
 
21
 
 
22
sub bind_param_attributes {
 
23
    my ($dbd, $data_type) = @_;
 
24
    if ($data_type && $data_type eq 'blob') {
 
25
        return { ora_type => ORA_BLOB };
 
26
    }
 
27
    return;
 
28
}
 
29
 
 
30
sub map_error_code {
 
31
    my $dbd = shift;
 
32
    my($code, $msg) = @_;
 
33
    if ($msg && $msg =~ /ORA-00001/i) {
 
34
        return Data::ObjectDriver::Errors->UNIQUE_CONSTRAINT;
 
35
    } else {
 
36
        return;
 
37
    }
 
38
}
 
39
 
 
40
## Oracle doesn't support auto-increment, it needs a SEQUENCE to emulate
 
41
## this feature. For usage, please see NOTES.
 
42
sub fetch_id {
 
43
    my $dbd = shift;
 
44
    my ($class, $dbh, $sth, $driver) = @_;
 
45
    my $seq = $dbd->sequence_name($class, $driver);
 
46
    my ($last_insert_id) = $dbh->selectrow_array("SELECT $seq.CURRVAL "
 
47
                                                  . " FROM DUAL");
 
48
    return $last_insert_id;
 
49
}
 
50
 
 
51
sub sequence_name {
 
52
    my $dbd = shift;
 
53
    my ($class, $driver) = @_;
 
54
    my $datasource = $class ->datasource;
 
55
    my $prefix     = $driver->prefix;
 
56
    $datasource    = join('', $prefix, $datasource) if $prefix;
 
57
    join '_', $datasource,
 
58
              $dbd->db_column_name(
 
59
                $class->datasource,
 
60
                $class->properties->{primary_key},
 
61
              ),
 
62
              'seq';
 
63
}
 
64
 
 
65
sub bulk_insert {
 
66
    my $dbd      = shift;
 
67
    my $dbh      = shift;
 
68
    my $table    = shift;
 
69
    my $cols     = shift;
 
70
    my $rows_ref = shift;
 
71
 
 
72
    my $sql = "INSERT INTO $table("
 
73
              . join(',', @$cols)
 
74
              . ") VALUES ("
 
75
              . join(',',  map {'?'} @$cols)
 
76
              .  ")";
 
77
    my $sth = $dbh->prepare($sql);
 
78
    foreach my $row (@{ $rows_ref || []}) {
 
79
        $sth->execute(@$row);
 
80
    }
 
81
    return 1;
 
82
}
 
83
 
 
84
##
 
85
sub sql_class { 'Data::ObjectDriver::SQL::Oracle' }
 
86
 
 
87
package Data::ObjectDriver::Driver::DBD::Oracle::db;
 
88
 
 
89
use strict;
 
90
 
 
91
## Inherit the DB class from DBI::db.
 
92
use base qw(DBI::db);
 
93
 
 
94
## Oracle doesn't allow a SELECT statement without FROM.
 
95
sub _adjust_stmt {
 
96
    my $stmt = shift;
 
97
    my $has_select = ($stmt =~ m/^\s*SELECT\b/io);
 
98
    my $has_from   = ($stmt =~ m/\bFROM\b/io);
 
99
    $stmt .= " FROM DUAL" if ($has_select and !$has_from);
 
100
    return $stmt;
 
101
}
 
102
 
 
103
sub selectrow_array {
 
104
    my $self = shift;
 
105
    my $stmt  = shift;
 
106
    $stmt = _adjust_stmt($stmt);
 
107
    unshift @_, $stmt;
 
108
    $self->SUPER::selectrow_array(@_);
 
109
}
 
110
 
 
111
1;
 
112
 
 
113
__END__
 
114
 
 
115
=head1 NAME
 
116
 
 
117
Data::ObjectDriver::Driver::DBD::Oracle - Oracle Driver for Data::ObjectDriver
 
118
 
 
119
=head1 DESCRIPTION
 
120
 
 
121
This module overrides methods of the Data::ObjectDriver::Driver::DBD module
 
122
with Oracle specific implementation.
 
123
 
 
124
=head1 NOTES
 
125
 
 
126
Oracle doesn't support auto-increment, so before you use this feature, you
 
127
should create a sequence and a trigger to work with it.
 
128
 
 
129
For example, you want field ID in table WINES be auto-increment, then create:
 
130
 
 
131
    -- Create sequence
 
132
    CREATE SEQUENCE WINES_ID_SEQ
 
133
    MINVALUE 1
 
134
    MAXVALUE 999999999999999999999999999
 
135
    START WITH 1
 
136
    INCREMENT BY 1
 
137
    NOCACHE;
 
138
 
 
139
    -- Create trigger
 
140
    CREATE OR REPLACE TRIGGER WINES_ID_TR
 
141
      BEFORE INSERT ON WINES
 
142
      FOR EACH ROW
 
143
    BEGIN
 
144
      SELECT WINES_ID_SEQ.NEXTVAL INTO :NEW.ID FROM DUAL;
 
145
    END;
 
146
 
 
147
=head1 LICENSE
 
148
 
 
149
This module is free software;
 
150
you may redistribute and/or modify it under the same
 
151
terms as Perl itself.
 
152
 
 
153
=head1 AUTHOR & COPYRIGHT
 
154
 
 
155
This module is
 
156
copyright (c) 2009 Xiaoou Wu E<lt>xiaoou.wu@oracle.comE<gt>.
 
157
All rights reserved.
 
158
 
 
159
=cut