3 require_once 'lib/WikiDB.php';
6 * WikiDB layer for PDO, the new php5 abstraction layer, with support for
7 * prepared statements and transactions.
9 * "The PHP Data Objects (PDO) extension defines a lightweight,
10 * consistent interface for accessing databases in PHP. Each database
11 * driver that implements the PDO interface can expose
12 * database-specific features as regular extension functions. Note
13 * that you cannot perform any database functions using the PDO
14 * extension by itself; you must use a database-specific PDO driver to
15 * access a database server."
17 * @author: Reini Urban
19 class WikiDB_PDO extends WikiDB
21 function WikiDB_PDO($dbparams)
23 if (is_array($dbparams['dsn']))
24 $backend = $dbparams['dsn']['phptype'];
25 elseif (preg_match('/^(\w+):/', $dbparams['dsn'], $m))
27 // Do we have a override? Currently none: mysql, sqlite, oci, mssql
28 if (FindFile("lib/WikiDB/backend/PDO_$backend.php", true)) {
29 $backend = 'PDO_' . $backend;
33 include_once("lib/WikiDB/backend/$backend.php");
34 $backend_class = "WikiDB_backend_$backend";
35 $backend = new $backend_class($dbparams);
36 $this->WikiDB($backend, $dbparams);
40 * Determine whether page exists (in non-default form).
41 * @see WikiDB::isWikiPage
43 function isWikiPage($pagename)
45 $pagename = (string)$pagename;
46 if ($pagename === '') return false;
47 if (!array_key_exists($pagename, $this->_cache->_id_cache)) {
48 $this->_cache->_id_cache[$pagename] = $this->_backend->is_wiki_page($pagename);
50 return $this->_cache->_id_cache[$pagename];
53 // With PDO we should really use native quoting using prepared statements with ?
54 // Supported since PDO-0.3 (?)
55 // Add surrounding quotes '' if string
58 if (is_int($in) || is_double($in)) {
60 } elseif (is_bool($in)) {
62 } elseif (is_null($in)) {
65 return $this->qstr($in);
69 // Don't add surrounding quotes '', same as in PearDB
70 // PDO-0.2.1 added now ::quote()
73 $in = str_replace(array('\\', "\0"), array('\\\\', "\\\0"), $in);
74 return str_replace("'", "\'", $in);
80 if (!$request->_dbi) return false;
81 return is_object($this->_backend->_dbh);
84 // SQL result: for simple select or create/update queries
85 // returns the database specific resource type
86 function genericSqlQuery($sql, $args = false)
89 $sth = $this->_backend->_dbh->prepare($sql);
91 foreach ($args as $key => $val) {
92 $sth->bindParam($key, $val);
96 $result = $sth->fetch(PDO_FETCH_BOTH);
99 } catch (PDOException $e) {
100 trigger_error("SQL Error: " . $e->getMessage(), E_USER_WARNING);
106 // SQL iter: for simple select or create/update queries
107 // returns the generic iterator object (count, next)
108 function genericSqlIter($sql, $field_list = NULL)
110 $result = $this->genericSqlQuery($sql);
111 return new WikiDB_backend_PDO_generic_iter($this->_backend, $result, $field_list);
122 // c-hanging-comment-ender-p: nil
123 // indent-tabs-mode: nil