Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * This file contains the class XML_Query2XML_Driver_ADOdb.
4
 *
5
 * PHP version 5
6
 *
7
 * @category  XML
8
 * @package   XML_Query2XML
9
 * @author    Lukas Feiler <lukas.feiler@lukasfeiler.com>
10
 * @copyright 2006 Lukas Feiler
11
 * @license   http://www.gnu.org/copyleft/lesser.html  LGPL Version 2.1
12
 * @version   CVS: $Id: ADOdb.php 276639 2009-03-01 13:17:08Z lukasfeiler $
13
 * @link      http://pear.php.net/package/XML_Query2XML
14
 */
15
 
16
/**
17
 * XML_Query2XML_Driver_ADOdb extends XML_Query2XML_Driver.
18
 */
19
require_once 'XML/Query2XML.php';
20
 
21
/**
22
 * Driver for the database abstraction layer ADOdb.
23
 *
24
 * usage:
25
 * <code>
26
 * $driver = XML_Query2XML_Driver::factory(NewADOConnection(...));
27
 * </code>
28
 *
29
 * @category  XML
30
 * @package   XML_Query2XML
31
 * @author    Lukas Feiler <lukas.feiler@lukasfeiler.com>
32
 * @copyright 2006 Lukas Feiler
33
 * @license   http://www.gnu.org/copyleft/lesser.html  LGPL Version 2.1
34
 * @version   Release: 1.7.2
35
 * @link      http://pear.php.net/package/XML_Query2XML
36
 * @since     Release 1.5.0RC1
37
 */
38
class XML_Query2XML_Driver_ADOdb extends XML_Query2XML_Driver
39
{
40
    /**
41
     * In instance of a class that extends ADOConnection.
42
     * @var ADOConnection
43
     */
44
    private $_db = null;
45
 
46
    /**
47
     * Constructor
48
     *
49
     * @param ADOConnection $db An instance of ADOConnection.
50
     *
51
     * @throws XML_Query2XML_DBException If the ADOConnection instance passed as
52
     *                          argument was not connected to the database server.
53
     */
54
    public function __construct(ADOConnection $db)
55
    {
56
        if (!$db->IsConnected()) {
57
            throw new XML_Query2XML_DBException(
58
                'ADOConnection instance was not connected'
59
            );
60
        }
61
        $db->SetFetchMode(ADODB_FETCH_ASSOC);
62
        $this->_db = $db;
63
    }
64
 
65
    /**
66
     * Execute a SQL SELECT statement and fetch all records from the result set.
67
     *
68
     * @param mixed  $sql        The SQL query as a string or an array.
69
     * @param string $configPath The config path; used for exception messages.
70
     *
71
     * @return array An array of records.
72
     * @throws XML_Query2XML_DBException If a database related error occures.
73
     * @see XML_Query2XML_Driver::getAllRecords()
74
     */
75
    public function getAllRecords($sql, $configPath)
76
    {
77
        $result  =& $this->_prepareAndExecute($sql, $configPath);
78
        $records = array();
79
        while ($record = $result->fetchRow()) {
80
            if (class_exists('PEAR_Error') && $record instanceof PEAR_Error) {
81
                // no unit test for this exception as it cannot be produced easily
82
                throw new XML_Query2XML_DBException(
83
                    $configPath . ': Could not fetch rows for the following '
84
                    . 'SQL query: ' . $sql['query'] . '; '
85
                    . $record->toString()
86
                );
87
            }
88
            $records[] = $record;
89
        }
90
        if ($result instanceof ADORecordSet) {
91
            $result->free();
92
        }
93
        return $records;
94
    }
95
 
96
    /**
97
     * Private method that will ADOConnection::prepare() & ADOConnection::execute()
98
     * to retrieve records.
99
     *
100
     * @param mixed  $sql        An array with an element at the index 'query'.
101
     * @param string $configPath The config path used for exception messages.
102
     *
103
     * @return DB_result
104
     * @throws XML_Query2XML_DBException If a database related error occures.
105
     */
106
    private function _prepareAndExecute($sql, $configPath)
107
    {
108
        $query =& $sql['query'];
109
        if (isset($this->_preparedQueries[$query])) {
110
            $queryHandle = $this->_preparedQueries[$query];
111
        } else {
112
            // ADOdb centralizes all error-handling in execute()
113
            $queryHandle                    = $this->_db->prepare($query);
114
            $this->_preparedQueries[$query] =& $queryHandle;
115
        }
116
 
117
        /*
118
         * EXECUTE
119
         */
120
 
121
        try {
122
            if (isset($sql['data'])) {
123
                $result = $this->_db->execute($queryHandle, $sql['data']);
124
            } else {
125
                $result = $this->_db->execute($queryHandle);
126
            }
127
        } catch (Exception $e) {
128
            /*
129
             * unit test: ADOdbException/
130
             *  _prepareAndExecute/throwDBException_complexQuery.phpt
131
             */
132
            throw new XML_Query2XML_DBException(
133
                $configPath . ': Could not execute the following SQL '
134
                . 'query: ' . $query .  '; ' . $e->getMessage()
135
            );
136
        }
137
 
138
        if ($result === false && function_exists('ADODB_Pear_Error')) {
139
            $result = ADODB_Pear_Error();
140
        }
141
 
142
        if (class_exists('PEAR_Error') && $result instanceof PEAR_Error) {
143
            /*
144
             * unit test: ADOdbPEAR/
145
             *  _prepareAndExecute/throwDBException_complexQuery.phpt
146
             */
147
            throw new XML_Query2XML_DBException(
148
                $configPath . ': Could not execute the following SQL query: '
149
                . $query . '; ' . $result->toString()
150
            );
151
        } elseif ($result === false) {
152
            /*
153
             * unit test: ADOdbDefault/
154
             *  _prepareAndExecute/throwDBException_complexQuery.phpt
155
             */
156
            throw new XML_Query2XML_DBException(
157
                $configPath . ': Could not execute the following SQL query: '
158
                . $query . ' (false was returned)'
159
            );
160
        }
161
        return $result;
162
    }
163
}
164
?>