Skip to content

Commit ac86baf

Browse files
author
Drak
committed
Merge pull request zikula#7 from rgasch/portability
Portability files for Derby, Splice and JdbcBridge
2 parents 8febe10 + cbe2bf5 commit ac86baf

File tree

18 files changed

+1200
-0
lines changed

18 files changed

+1200
-0
lines changed
Lines changed: 289 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,289 @@
1+
<?php
2+
/*
3+
* $Id$
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+
* Custom Doctrine connection adapter for oracle
24+
*
25+
* @package Doctrine
26+
* @subpackage Adapter
27+
* @author Konsta Vesterinen <[email protected]>
28+
* @author vadik56
29+
* @author Miloslav Kmet <[email protected]>
30+
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
31+
* @link www.doctrine-project.org
32+
* @since 1.0
33+
* @version $Revision$
34+
*/
35+
36+
class Doctrine_Adapter_Jdbcbridge implements Doctrine_Adapter_Interface
37+
{
38+
/**
39+
* execution mode
40+
*/
41+
protected $executeMode = OCI_COMMIT_ON_SUCCESS;
42+
43+
/**
44+
* Resource representing connection to database
45+
*/
46+
protected $connection = false;
47+
48+
49+
protected $attributes = array(Doctrine_Core::ATTR_DRIVER_NAME => "jdbcbridge",
50+
Doctrine_Core::ATTR_ERRMODE => Doctrine_Core::ERRMODE_SILENT);
51+
52+
/**
53+
* User-provided configuration.
54+
*
55+
* Basic keys are:
56+
*
57+
* username => (string) Connect to the database as this username.
58+
* password => (string) Password associated with the username.
59+
* dbname => Either the name of the local Oracle instance, or the
60+
* name of the entry in tnsnames.ora to which you want to connect.
61+
*
62+
* @var array
63+
*/
64+
protected $config = array(
65+
'dbname' => null,
66+
'username' => null,
67+
'password' => null,
68+
'charset' => null,
69+
'persistent' => false
70+
);
71+
72+
/**
73+
* Doctrine Oracle adapter constructor
74+
*
75+
* <code>
76+
* $conn = new Doctrine_Adapter_Jdbcbridge(array('dbname'=>'db','username'=>'usr','password'=>'pass'));
77+
* </code>
78+
*
79+
* or
80+
*
81+
* <code>
82+
* Doctrine_Manager::connection(array('oracle:dbname=SID;charset=NLS_CHARACTERSET;persistent=true','usr', 'pass'),"doctrine_connection_name")
83+
* </code>
84+
*
85+
* @param string $name
86+
* @return void
87+
*/
88+
public function __construct($config = array(), $username = null, $password = null)
89+
{
90+
$this->config['dbname'] = $config['dbname'];
91+
92+
if (isset($config['charset'])) {
93+
$this->config['charset'] = $config['charset'];
94+
}
95+
96+
if (isset($config['persistent'])) {
97+
$this->config['persistent'] = $config['persistent'];
98+
}
99+
100+
$this->connection = new Zikula_JdbcBridge();
101+
$this->connection->connect ("jdbc:derby://$config[host]:$config[port]/$config[dbname]");;
102+
103+
if ($this->connection === false) {
104+
throw new Doctrine_Adapter_Exception(sprintf("Unable to Connect to :'%s' as '%s'", $this->config['dbname'], $this->config['username']));
105+
}
106+
; }
107+
108+
/**
109+
* Prepare a query statement
110+
*
111+
* @param string $query Query to prepare
112+
* @return Doctrine_Adapter_Statement_Oracle $stmt prepared statement
113+
*/
114+
public function prepare($query)
115+
{
116+
$stmt = new Doctrine_Adapter_Statement_Jdbcbridge($this, $query, $this->executeMode);
117+
118+
return $stmt;
119+
}
120+
121+
/**
122+
* Execute query and return results as statement object
123+
*
124+
* @param string $query
125+
* @return Doctrine_Adapter_Statement_Oracle $stmt
126+
*/
127+
public function query($query)
128+
{
129+
$stmt = new Doctrine_Adapter_Statement_Jdbcbridge($this, $query, $this->executeMode);
130+
$stmt->execute();
131+
132+
return $stmt;
133+
}
134+
135+
/**
136+
* Quote a value for the dbms
137+
*
138+
* @param string $input
139+
* @return string $quoted
140+
*/
141+
public function quote($input)
142+
{
143+
return "'" . str_replace("'","''",$input) . "'";
144+
}
145+
146+
/**
147+
* Execute a raw sql statement
148+
*
149+
* @param string $statement
150+
* @return void
151+
*/
152+
public function exec($statement)
153+
{
154+
$stmt = new Doctrine_Adapter_Statement_Jdbcbridge($this, $statement, $this->executeMode);
155+
$stmt->execute();
156+
$count = $stmt->rowCount();
157+
158+
return $count;
159+
}
160+
161+
/**
162+
* Get the id of the last inserted record
163+
*
164+
* @return integer $id
165+
*/
166+
public function lastInsertId()
167+
{
168+
throw new Doctrine_Adapter_Exception("unsupported");
169+
}
170+
171+
/**
172+
* Begin a transaction
173+
*
174+
* @return boolean
175+
*/
176+
public function beginTransaction()
177+
{
178+
$this->executeMode = OCI_DEFAULT;
179+
return true;
180+
}
181+
182+
/**
183+
* Commit a transaction
184+
*
185+
* @return void
186+
*/
187+
public function commit()
188+
{
189+
return @oci_commit($this->connection);
190+
}
191+
192+
/**
193+
* Rollback a transaction
194+
*
195+
* @return boolean
196+
*/
197+
public function rollBack()
198+
{
199+
return @oci_rollback($this->connection);
200+
}
201+
202+
/**
203+
* Set connection attribute
204+
*
205+
* @param integer $attribute
206+
* @param mixed $value the value of given attribute
207+
* @return boolean Returns TRUE on success or FALSE on failure.
208+
*/
209+
public function setAttribute($attribute, $value)
210+
{
211+
switch ($attribute) {
212+
case Doctrine_Core::ATTR_DRIVER_NAME:
213+
//TODO throw an error since driver name can not be changed
214+
case Doctrine_Core::ATTR_ERRMODE:
215+
break;
216+
case Doctrine_Core::ATTR_CASE:
217+
if ($value == Doctrine_Core::CASE_NATURAL) {
218+
break;
219+
} else {
220+
throw new Doctrine_Adapter_Exception("Unsupported Option for ATTR_CASE: $value");
221+
}
222+
default:
223+
throw new Doctrine_Adapter_Exception("Unsupported Attribute: $attribute");
224+
return false;
225+
}
226+
$this->attributes[$attribute] = $value;
227+
return true;
228+
}
229+
230+
/**
231+
* Retrieve a statement attribute
232+
*
233+
* @param integer $attribute
234+
* @see Doctrine_Core::ATTR_* constants
235+
* @return mixed the attribute value
236+
*/
237+
public function getAttribute($attribute)
238+
{
239+
return $this->attributes[$attribute];
240+
}
241+
242+
/**
243+
* Returns established OCI connection handler
244+
*
245+
* @return resource OCI connection handler
246+
*/
247+
public function getConnection()
248+
{
249+
return $this->connection;
250+
}
251+
252+
/**
253+
* Returns current user name
254+
*
255+
* @return string current user name
256+
*/
257+
public function getUserName()
258+
{
259+
return $this->config['username'];
260+
}
261+
262+
public function errorCode()
263+
{
264+
if (is_resource($this->connection)) {
265+
$error = @oci_error($this->connection);
266+
} else {
267+
$error = @oci_error();
268+
}
269+
return $error['code'];
270+
}
271+
272+
public function errorInfo()
273+
{
274+
if (is_resource($this->connection)) {
275+
$error = @oci_error($this->connection);
276+
} else {
277+
$error = @oci_error();
278+
}
279+
return $error['message'];
280+
}
281+
282+
public function __destruct()
283+
{
284+
if (is_resource($this->connection)) {
285+
@oci_rollback($this->connection);
286+
@oci_close($this->connection);
287+
}
288+
}
289+
}

0 commit comments

Comments
 (0)