Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 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 <kvesteri@cc.hut.fi>
28
 * @author      vadik56
29
 * @author      Miloslav Kmet <adrive-nospam@hip-hop.sk>
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_Oracle 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    => "oci8",
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_Oracle(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
        if (is_string($config))
91
        {
92
            $config = str_replace("oracle:","",$config);
93
            $parts = explode(";", $config);
94
            foreach($parts as $part) {
95
                $e = explode("=", $part);
96
                $key = array_shift($e);
97
                $this->config[$key] = implode('=', $e);
98
            }
99
 
100
            if ($username) {
101
                $this->config['username'] = $username;
102
            }
103
            if ($password) {
104
                $this->config['password'] = $password;
105
            }
106
        } else {
107
            if ( ! isset($config['password']) || ! isset($config['username'])) {
108
                throw new Doctrine_Adapter_Exception('config array must have at least a username and a password');
109
            }
110
 
111
            $this->config['username'] = $config['username'];
112
            $this->config['password'] = $config['password'];
113
            $this->config['dbname']   = $config['dbname'];
114
 
115
            if (isset($config['charset'])) {
116
                $this->config['charset']  = $config['charset'];
117
            }
118
 
119
            if (isset($config['persistent'])) {
120
                $this->config['persistent']  = $config['persistent'];
121
            }
122
        }
123
 
124
 
125
        if ($this->config['persistent'] == 'true'){
126
            $this->connection = @oci_pconnect($this->config['username'], $this->config['password'],
127
                $this->config['dbname'], $this->config['charset']);
128
        } else {
129
            $this->connection = @oci_new_connect($this->config['username'], $this->config['password'],
130
                $this->config['dbname'], $this->config['charset']);
131
        }
132
 
133
        if ($this->connection === false) {
134
            throw new Doctrine_Adapter_Exception(sprintf("Unable to Connect to :'%s' as '%s'", $this->config['dbname'], $this->config['username']));
135
        }
136
    }
137
 
138
    /**
139
     * Prepare a query statement
140
     *
141
     * @param string $query Query to prepare
142
     * @return Doctrine_Adapter_Statement_Oracle $stmt prepared statement
143
     */
144
    public function prepare($query)
145
    {
146
        $stmt = new Doctrine_Adapter_Statement_Oracle($this, $query, $this->executeMode);
147
 
148
        return $stmt;
149
    }
150
 
151
    /**
152
     * Execute query and return results as statement object
153
     *
154
     * @param string $query
155
     * @return Doctrine_Adapter_Statement_Oracle $stmt
156
     */
157
    public function query($query)
158
    {
159
        $stmt = new Doctrine_Adapter_Statement_Oracle($this, $query, $this->executeMode);
160
        $stmt->execute();
161
 
162
        return $stmt;
163
    }
164
 
165
    /**
166
     * Quote a value for the dbms
167
     *
168
     * @param string $input
169
     * @return string $quoted
170
     */
171
    public function quote($input)
172
    {
173
        return "'" . str_replace("'","''",$input) . "'";
174
    }
175
 
176
    /**
177
     * Execute a raw sql statement
178
     *
179
     * @param string $statement
180
     * @return void
181
     */
182
    public function exec($statement)
183
    {
184
        $stmt = new Doctrine_Adapter_Statement_Oracle($this, $statement, $this->executeMode);
185
        $stmt->execute();
186
        $count = $stmt->rowCount();
187
 
188
        return $count;
189
    }
190
 
191
    /**
192
     * Get the id of the last inserted record
193
     *
194
     * @return integer $id
195
     */
196
    public function lastInsertId()
197
    {
198
        throw new Doctrine_Adapter_Exception("unsupported");
199
    }
200
 
201
    /**
202
     * Begin a transaction
203
     *
204
     * @return boolean
205
     */
206
    public function beginTransaction()
207
    {
208
       $this->executeMode = OCI_DEFAULT;
209
       return true;
210
    }
211
 
212
    /**
213
     * Commit a transaction
214
     *
215
     * @return void
216
     */
217
    public function commit()
218
    {
219
        return @oci_commit($this->connection);
220
    }
221
 
222
    /**
223
     * Rollback a transaction
224
     *
225
     * @return boolean
226
     */
227
    public function rollBack()
228
    {
229
        return @oci_rollback($this->connection);
230
    }
231
 
232
    /**
233
     * Set connection attribute
234
     *
235
     * @param integer $attribute
236
     * @param mixed $value                  the value of given attribute
237
     * @return boolean                      Returns TRUE on success or FALSE on failure.
238
     */
239
    public function setAttribute($attribute, $value)
240
    {
241
        switch ($attribute) {
242
            case Doctrine_Core::ATTR_DRIVER_NAME:
243
                //TODO throw an error since driver name can not be changed
244
            case Doctrine_Core::ATTR_ERRMODE:
245
            break;
246
            case Doctrine_Core::ATTR_CASE:
247
                if ($value == Doctrine_Core::CASE_NATURAL) {
248
                    break;
249
                } else {
250
                    throw new Doctrine_Adapter_Exception("Unsupported Option for ATTR_CASE: $value");
251
                }
252
            default:
253
                throw new Doctrine_Adapter_Exception("Unsupported Attribute: $attribute");
254
                return false;
255
        }
256
        $this->attributes[$attribute] = $value;
257
        return true;
258
    }
259
 
260
    /**
261
     * Retrieve a statement attribute
262
     *
263
     * @param integer $attribute
264
     * @see Doctrine_Core::ATTR_* constants
265
     * @return mixed                        the attribute value
266
     */
267
    public function getAttribute($attribute)
268
    {
269
        return $this->attributes[$attribute];
270
    }
271
 
272
    /**
273
     * Returns established OCI connection handler
274
     *
275
     * @return resource OCI connection handler
276
     */
277
    public function getConnection()
278
    {
279
        return $this->connection;
280
    }
281
 
282
    /**
283
     * Returns current user name
284
     *
285
     * @return string current user name
286
     */
287
    public function getUserName()
288
    {
289
       return $this->config['username'];
290
    }
291
 
292
    public function errorCode()
293
    {
294
        if (is_resource($this->connection)) {
295
            $error = @oci_error($this->connection);
296
        } else {
297
            $error = @oci_error();
298
        }
299
        return $error['code'];
300
    }
301
 
302
    public function errorInfo()
303
    {
304
        if (is_resource($this->connection)) {
305
            $error = @oci_error($this->connection);
306
        } else {
307
            $error = @oci_error();
308
        }
309
        return $error['message'];
310
    }
311
 
312
    public function __destruct()
313
    {
314
        if (is_resource($this->connection)) {
315
            @oci_rollback($this->connection);
316
            @oci_close($this->connection);
317
        }
318
    }
319
}