| 1 |
lars |
1 |
<?php
|
|
|
2 |
/*
|
|
|
3 |
* $Id: Oracle.php 7664 2010-06-08 19:10:14Z jwage $
|
|
|
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.doctrine-project.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.doctrine-project.org
|
|
|
29 |
* @since 1.0
|
|
|
30 |
* @version $Revision: 7664 $
|
|
|
31 |
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
|
|
32 |
*/
|
|
|
33 |
class Doctrine_Connection_Oracle extends Doctrine_Connection_Common
|
|
|
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['number_max_precision'] = 38;
|
|
|
65 |
$this->properties['max_identifier_length'] = 30;
|
|
|
66 |
|
|
|
67 |
parent::__construct($manager, $adapter);
|
|
|
68 |
|
|
|
69 |
// moving properties to params to make them changeable by user
|
|
|
70 |
// VARCHAR2 allowed length is 4000 BYTE. For UTF8 strings is better to use 1000 CHAR
|
|
|
71 |
$this->setParam('varchar2_max_length', 4000);
|
|
|
72 |
// Oracle's default unit for char data types is BYTE. For UTF8 string it is better to use CHAR
|
|
|
73 |
$this->setParam('char_unit', null);
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
/**
|
|
|
77 |
* Sets up the date/time format
|
|
|
78 |
*
|
|
|
79 |
*/
|
|
|
80 |
public function setDateFormat($format = 'YYYY-MM-DD HH24:MI:SS')
|
|
|
81 |
{
|
|
|
82 |
$this->exec('ALTER SESSION SET NLS_DATE_FORMAT = "' . $format . '"');
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
/**
|
|
|
86 |
* Adds an driver-specific LIMIT clause to the query
|
|
|
87 |
*
|
|
|
88 |
* @param string $query query to modify
|
|
|
89 |
* @param integer $limit limit the number of rows
|
|
|
90 |
* @param integer $offset start reading from given offset
|
|
|
91 |
* @return string the modified query
|
|
|
92 |
*/
|
|
|
93 |
public function modifyLimitQuery($query, $limit = false, $offset = false, $isManip = false)
|
|
|
94 |
{
|
|
|
95 |
return $this->_createLimitSubquery($query, $limit, $offset);
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
private function _createLimitSubquery($query, $limit, $offset, $column = null)
|
|
|
99 |
{
|
|
|
100 |
$limit = (int) $limit;
|
|
|
101 |
$offset = (int) $offset;
|
|
|
102 |
if (preg_match('/^\s*SELECT/i', $query)) {
|
|
|
103 |
if ( ! preg_match('/\sFROM\s/i', $query)) {
|
|
|
104 |
$query .= " FROM dual";
|
|
|
105 |
}
|
|
|
106 |
if ($limit > 0) {
|
|
|
107 |
$max = $offset + $limit;
|
|
|
108 |
$column = $column === null ? '*' : $this->quoteIdentifier($column);
|
|
|
109 |
if ($offset > 0) {
|
|
|
110 |
$min = $offset + 1;
|
|
|
111 |
$query = 'SELECT '.$this->quoteIdentifier('b').'.'.$column.' FROM ( '.
|
|
|
112 |
'SELECT '.$this->quoteIdentifier('a').'.*, ROWNUM AS doctrine_rownum FROM ( '
|
|
|
113 |
. $query . ' ) ' . $this->quoteIdentifier('a') . ' '.
|
|
|
114 |
' ) ' . $this->quoteIdentifier('b') . ' '.
|
|
|
115 |
'WHERE doctrine_rownum BETWEEN ' . $min . ' AND ' . $max;
|
|
|
116 |
} else {
|
|
|
117 |
$query = 'SELECT a.'.$column.' FROM ( ' . $query .' ) a WHERE ROWNUM <= ' . $max;
|
|
|
118 |
}
|
|
|
119 |
}
|
|
|
120 |
}
|
|
|
121 |
return $query;
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
/**
|
|
|
125 |
* Creates the SQL for Oracle that can be used in the subquery for the limit-subquery
|
|
|
126 |
* algorithm.
|
|
|
127 |
*/
|
|
|
128 |
public function modifyLimitSubquery(Doctrine_Table $rootTable, $query, $limit = false,
|
|
|
129 |
$offset = false, $isManip = false)
|
|
|
130 |
{
|
|
|
131 |
// NOTE: no composite key support
|
|
|
132 |
$columnNames = $rootTable->getIdentifierColumnNames();
|
|
|
133 |
if (count($columnNames) > 1) {
|
|
|
134 |
throw new Doctrine_Connection_Exception("Composite keys in LIMIT queries are "
|
|
|
135 |
. "currently not supported.");
|
|
|
136 |
}
|
|
|
137 |
$column = $columnNames[0];
|
|
|
138 |
return $this->_createLimitSubquery($query, $limit, $offset, $column);
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
public function getTmpConnection($info)
|
|
|
142 |
{
|
|
|
143 |
return clone $this;
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
/**
|
|
|
147 |
* Override quote behaviour for boolean to fix issues with quoting of
|
|
|
148 |
* boolean values.
|
|
|
149 |
*/
|
|
|
150 |
public function quote($input, $type = null)
|
|
|
151 |
{
|
|
|
152 |
if ($type === 'boolean') {
|
|
|
153 |
if ($input === null) {
|
|
|
154 |
return null;
|
|
|
155 |
} else {
|
|
|
156 |
return $input ? 1 : 0;
|
|
|
157 |
}
|
|
|
158 |
} else {
|
|
|
159 |
return parent::quote($input, $type);
|
|
|
160 |
}
|
|
|
161 |
}
|
|
|
162 |
}
|