~patrix-sbs/oraculum/git

« back to all changes in this revision

Viewing changes to models/doctrine/lib/Doctrine/Connection/Oracle.php

  • Committer: Patrick Kaminski
  • Date: 2009-09-02 02:33:07 UTC
  • Revision ID: git-v1:943803254fca67bfb4c0374422b1b836b14dc518
Tags: v0.1a
Sending Oraculum Framework v0.1 alpha

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/*
 
3
 *  $Id: Oracle.php 5116 2008-10-21 20:35:42Z adrive $
 
4
 *
 
5
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
6
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
7
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 
8
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 
9
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
10
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
11
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 
12
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 
13
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
14
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
15
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
16
 *
 
17
 * This software consists of voluntary contributions made by many individuals
 
18
 * and is licensed under the LGPL. For more information, see
 
19
 * <http://www.phpdoctrine.org>.
 
20
 */
 
21
 
 
22
/**
 
23
 * Doctrine_Connection_Oracle
 
24
 *
 
25
 * @package     Doctrine
 
26
 * @subpackage  Connection
 
27
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 
28
 * @link        www.phpdoctrine.org
 
29
 * @since       1.0
 
30
 * @version     $Revision: 5116 $
 
31
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 
32
 */
 
33
class Doctrine_Connection_Oracle extends Doctrine_Connection
 
34
{
 
35
    /**
 
36
     * @var string $driverName                  the name of this connection driver
 
37
     */
 
38
    protected $driverName = 'Oracle';
 
39
 
 
40
    public function __construct(Doctrine_Manager $manager, $adapter)
 
41
    {
 
42
        $this->supported = array(
 
43
                          'sequences'            => true,
 
44
                          'indexes'              => true,
 
45
                          'summary_functions'    => true,
 
46
                          'order_by_text'        => true,
 
47
                          'current_id'           => true,
 
48
                          'affected_rows'        => true,
 
49
                          'transactions'         => true,
 
50
                          'savepoints'           => true,
 
51
                          'limit_queries'        => true,
 
52
                          'LOBs'                 => true,
 
53
                          'replace'              => 'emulated',
 
54
                          'sub_selects'          => true,
 
55
                          'auto_increment'       => false, // implementation is broken
 
56
                          'primary_key'          => true,
 
57
                          'result_introspection' => true,
 
58
                          'prepared_statements'  => true,
 
59
                          'identifier_quoting'   => true,
 
60
                          'pattern_escaping'     => true,
 
61
                          );
 
62
        
 
63
        $this->properties['sql_file_delimiter']   = "\n/\n";
 
64
        $this->properties['varchar2_max_length']  = 4000;
 
65
        $this->properties['number_max_precision'] = 38;
 
66
        
 
67
        parent::__construct($manager, $adapter);
 
68
    }
 
69
 
 
70
    /**
 
71
     * Sets up the date/time format
 
72
     *
 
73
     */
 
74
    public function setDateFormat($format = 'YYYY-MM-DD HH24:MI:SS')
 
75
    {
 
76
        $this->exec('ALTER SESSION SET NLS_DATE_FORMAT = "' . $format . '"');
 
77
    }
 
78
 
 
79
    /**
 
80
     * Adds an driver-specific LIMIT clause to the query
 
81
     *
 
82
     * @param string $query         query to modify
 
83
     * @param integer $limit        limit the number of rows
 
84
     * @param integer $offset       start reading from given offset
 
85
     * @return string               the modified query
 
86
     */
 
87
    public function modifyLimitQuery($query, $limit = false, $offset = false, $isManip = false)
 
88
    {
 
89
        return $this->_createLimitSubquery($query, $limit, $offset);
 
90
    }
 
91
    
 
92
    private function _createLimitSubquery($query, $limit, $offset, $column = null)
 
93
    {
 
94
        $limit = (int) $limit;
 
95
        $offset = (int) $offset;
 
96
        if (preg_match('/^\s*SELECT/i', $query)) {
 
97
            if ( ! preg_match('/\sFROM\s/i', $query)) {
 
98
                $query .= " FROM dual";
 
99
            }
 
100
            if ($limit > 0) {
 
101
                $max = $offset + $limit;
 
102
                $column = $column === null ? '*' : $column;
 
103
                if ($offset > 0) {
 
104
                    $min = $offset + 1;
 
105
                    $query = 'SELECT b.'.$column.' FROM ('.
 
106
                                 'SELECT a.*, ROWNUM AS doctrine_rownum FROM ('
 
107
                                   . $query . ') a '.
 
108
                              ') b '.
 
109
                              'WHERE doctrine_rownum BETWEEN ' . $min .  ' AND ' . $max;
 
110
                } else {
 
111
                    $query = 'SELECT a.'.$column.' FROM (' . $query .') a WHERE ROWNUM <= ' . $max;
 
112
                }
 
113
            }
 
114
        }
 
115
        return $query;
 
116
    }
 
117
    
 
118
    /**
 
119
     * Creates the SQL for Oracle that can be used in the subquery for the limit-subquery
 
120
     * algorithm.
 
121
     */
 
122
    public function modifyLimitSubquery(Doctrine_Table $rootTable, $query, $limit = false,
 
123
            $offset = false, $isManip = false)
 
124
    {
 
125
        // NOTE: no composite key support
 
126
        $columnNames = $rootTable->getIdentifierColumnNames();
 
127
        if (count($columnNames) > 1) {
 
128
            throw new Doctrine_Connection_Exception("Composite keys in LIMIT queries are "
 
129
                    . "currently not supported.");
 
130
        }
 
131
        $column = $columnNames[0];
 
132
        return $this->_createLimitSubquery($query, $limit, $offset, $column);
 
133
    }
 
134
}
 
 
b'\\ No newline at end of file'