2 // $Id: PDO.php 7956 2011-03-03 17:08:31Z vargenau $
4 require_once('lib/WikiDB.php');
7 * WikiDB layer for PDO, the new php5 abstraction layer, with support for
8 * prepared statements and transactions.
10 * "The PHP Data Objects (PDO) extension defines a lightweight,
11 * consistent interface for accessing databases in PHP. Each database
12 * driver that implements the PDO interface can expose
13 * database-specific features as regular extension functions. Note
14 * that you cannot perform any database functions using the PDO
15 * extension by itself; you must use a database-specific PDO driver to
16 * access a database server."
18 * @author: Reini Urban
20 class WikiDB_PDO extends WikiDB
22 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) {
44 $pagename = (string) $pagename;
45 if ($pagename === '') return false;
46 if (!array_key_exists($pagename, $this->_cache->_id_cache)) {
47 $this->_cache->_id_cache[$pagename] = $this->_backend->is_wiki_page($pagename);
49 return $this->_cache->_id_cache[$pagename];
52 // With PDO we should really use native quoting using prepared statements with ?
53 // Supported since PDO-0.3 (?)
54 // Add surrounding quotes '' if string
55 function quote ($in) {
56 if (is_int($in) || is_double($in)) {
58 } elseif (is_bool($in)) {
60 } elseif (is_null($in)) {
63 return $this->qstr($in);
66 // Don't add surrounding quotes '', same as in PearDB
67 // PDO-0.2.1 added now ::quote()
69 $in = str_replace(array('\\',"\0"),array('\\\\',"\\\0"), $in);
70 return str_replace("'", "\'", $in);
75 if (!$request->_dbi) return false;
76 return is_object($this->_backend->_dbh);
79 // SQL result: for simple select or create/update queries
80 // returns the database specific resource type
81 function genericSqlQuery($sql, $args=false) {
83 $sth = $this->_backend->_dbh->prepare($sql);
85 foreach ($args as $key => $val ) {
86 $sth->bindParam($key, $val);
90 $result = $sth->fetch(PDO_FETCH_BOTH);
94 catch (PDOException $e) {
95 trigger_error("SQL Error: ".$e->getMessage(), E_USER_WARNING);
101 // SQL iter: for simple select or create/update queries
102 // returns the generic iterator object (count, next)
103 function genericSqlIter($sql, $field_list = NULL) {
104 $result = $this->genericSqlQuery($sql);
105 return new WikiDB_backend_PDO_generic_iter($this->_backend, $result, $field_list);
114 // c-hanging-comment-ender-p: nil
115 // indent-tabs-mode: nil