| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* This file contains the class XML_Query2XML_Driver_PDO.
|
|
|
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: PDO.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_PDO extends XML_Query2XML_Driver.
|
|
|
18 |
*/
|
|
|
19 |
require_once 'XML/Query2XML.php';
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* Driver for the database abstraction layer PDO.
|
|
|
23 |
*
|
|
|
24 |
* usage:
|
|
|
25 |
* <code>
|
|
|
26 |
* $driver = XML_Query2XML_Driver::factory(new PDO(...));
|
|
|
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_PDO extends XML_Query2XML_Driver
|
|
|
39 |
{
|
|
|
40 |
/**
|
|
|
41 |
* In instance of PDO
|
|
|
42 |
* @var PDO
|
|
|
43 |
*/
|
|
|
44 |
private $_db = null;
|
|
|
45 |
|
|
|
46 |
/**
|
|
|
47 |
* Constructor
|
|
|
48 |
*
|
|
|
49 |
* @param PDO $db An instance of PDO.
|
|
|
50 |
*
|
|
|
51 |
* @throws XML_Query2XML_DBException If PDO::ATTR_ERRMODE cannot be set to
|
|
|
52 |
* PDO::ERRMODE_EXCEPTION.
|
|
|
53 |
*/
|
|
|
54 |
public function __construct(PDO $db)
|
|
|
55 |
{
|
|
|
56 |
$success = $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
|
57 |
if (!$success) {
|
|
|
58 |
// no unit tests for this one
|
|
|
59 |
throw new XML_Query2XML_DBException(
|
|
|
60 |
'Could not set attribute PDO::ATTR_ERRMODE to PDO::ERRMODE_EXCEPTION'
|
|
|
61 |
);
|
|
|
62 |
}
|
|
|
63 |
$this->_db = $db;
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
/**
|
|
|
67 |
* Execute a SQL SELECT stement and fetch all records from the result set.
|
|
|
68 |
*
|
|
|
69 |
* @param mixed $sql The SQL query as a string or an array.
|
|
|
70 |
* @param string $configPath The config path; used for exception messages.
|
|
|
71 |
*
|
|
|
72 |
* @return array An array of records.
|
|
|
73 |
* @throws XML_Query2XML_DBException If a database related error occures.
|
|
|
74 |
* @see XML_Query2XML_Driver::getAllRecords()
|
|
|
75 |
*/
|
|
|
76 |
public function getAllRecords($sql, $configPath)
|
|
|
77 |
{
|
|
|
78 |
$result =& $this->_prepareAndExecute($sql, $configPath);
|
|
|
79 |
try {
|
|
|
80 |
$records = $result->fetchAll();
|
|
|
81 |
} catch (PDOException $e) {
|
|
|
82 |
/*
|
|
|
83 |
* unit tests: PDO/getXML/
|
|
|
84 |
* throwDBException_nullResultSet_complexQuery_multipleRecords.phpt
|
|
|
85 |
* throwDBException_nullResultSet_complexQuery_singleRecord.phpt
|
|
|
86 |
* throwDBException_nullResultSet_simpleQuery_multipleRecords.phpt
|
|
|
87 |
* throwDBException_nullResultSet_simpleQuery_singleRecord.phpt
|
|
|
88 |
*/
|
|
|
89 |
throw new XML_Query2XML_DBException(
|
|
|
90 |
$configPath . ': Could not fetch records for the following SQL '
|
|
|
91 |
. 'query: ' . $sql['query'] . '; '
|
|
|
92 |
. $e->getMessage()
|
|
|
93 |
);
|
|
|
94 |
}
|
|
|
95 |
return $records;
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
/**
|
|
|
99 |
* Private method that will use PDO::query() for simple and
|
|
|
100 |
* PDO::prepare() & PDOStatement::execute() for complex query specifications.
|
|
|
101 |
*
|
|
|
102 |
* @param mixed $sql A string or an array.
|
|
|
103 |
* @param string $configPath The config path used for exception messages.
|
|
|
104 |
*
|
|
|
105 |
* @return PDOStatement
|
|
|
106 |
* @throws XML_Query2XML_DBException If a database related error occures.
|
|
|
107 |
*/
|
|
|
108 |
private function _prepareAndExecute($sql, $configPath)
|
|
|
109 |
{
|
|
|
110 |
$query =& $sql['query'];
|
|
|
111 |
if (isset($this->_preparedQueries[$query])) {
|
|
|
112 |
$queryHandle = $this->_preparedQueries[$query];
|
|
|
113 |
} else {
|
|
|
114 |
// PREPARE
|
|
|
115 |
$queryHandle = $this->_db->prepare($query);
|
|
|
116 |
if ($queryHandle === false) {
|
|
|
117 |
/*
|
|
|
118 |
* No unit test for this exception as neither the mysql, pgsql
|
|
|
119 |
* or sqlite driver ever returns false from PDO::prepare().
|
|
|
120 |
*/
|
|
|
121 |
throw new XML_Query2XML_DBException(
|
|
|
122 |
$configPath . ': Could not prepare the following SQL '
|
|
|
123 |
. 'query - PDO::prepare() returned false: ' . $query
|
|
|
124 |
);
|
|
|
125 |
}
|
|
|
126 |
$this->_preparedQueries[$query] =& $queryHandle;
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
// EXECUTE
|
|
|
130 |
try {
|
|
|
131 |
if (isset($sql['data'])) {
|
|
|
132 |
$queryHandle->execute($sql['data']);
|
|
|
133 |
} else {
|
|
|
134 |
$queryHandle->execute();
|
|
|
135 |
}
|
|
|
136 |
} catch (PDOException $e) {
|
|
|
137 |
/*
|
|
|
138 |
* unit test: PDO/_prepareAndExecute
|
|
|
139 |
* throwDBException_complexQuery.phpt
|
|
|
140 |
*/
|
|
|
141 |
throw new XML_Query2XML_DBException(
|
|
|
142 |
$configPath . ': Could not execute the following SQL query: '
|
|
|
143 |
. $query . '; ' . $e->getMessage()
|
|
|
144 |
);
|
|
|
145 |
}
|
|
|
146 |
$success = $queryHandle->setFetchMode(PDO::FETCH_ASSOC);
|
|
|
147 |
if (!$success) {
|
|
|
148 |
// no unit tests for this one
|
|
|
149 |
throw new XML_Query2XML_DBException(
|
|
|
150 |
'Could not set fetch mode to PDO::FETCH_ASSOC'
|
|
|
151 |
);
|
|
|
152 |
}
|
|
|
153 |
return $queryHandle;
|
|
|
154 |
}
|
|
|
155 |
}
|
|
|
156 |
?>
|