]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiDB/PDO.php
Reformat code
[SourceForge/phpwiki.git] / lib / WikiDB / PDO.php
1 <?php
2
3 require_once 'lib/WikiDB.php';
4
5 /**
6  * WikiDB layer for PDO, the new php5 abstraction layer, with support for
7  * prepared statements and transactions.
8  *
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."
16  *
17  * @author: Reini Urban
18  */
19 class WikiDB_PDO extends WikiDB
20 {
21     function WikiDB_PDO($dbparams)
22     {
23         if (is_array($dbparams['dsn']))
24             $backend = $dbparams['dsn']['phptype'];
25         elseif (preg_match('/^(\w+):/', $dbparams['dsn'], $m))
26             $backend = $m[1];
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;
30         } else {
31             $backend = 'PDO';
32         }
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);
37     }
38
39     /**
40      * Determine whether page exists (in non-default form).
41      * @see WikiDB::isWikiPage
42      */
43     function isWikiPage($pagename)
44     {
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);
49         }
50         return $this->_cache->_id_cache[$pagename];
51     }
52
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
56     function quote($in)
57     {
58         if (is_int($in) || is_double($in)) {
59             return $in;
60         } elseif (is_bool($in)) {
61             return $in ? 1 : 0;
62         } elseif (is_null($in)) {
63             return 'NULL';
64         } else {
65             return $this->qstr($in);
66         }
67     }
68
69     // Don't add surrounding quotes '', same as in PearDB
70     // PDO-0.2.1 added now ::quote()
71     function qstr($in)
72     {
73         $in = str_replace(array('\\', "\0"), array('\\\\', "\\\0"), $in);
74         return str_replace("'", "\'", $in);
75     }
76
77     function isOpen()
78     {
79         global $request;
80         if (!$request->_dbi) return false;
81         return is_object($this->_backend->_dbh);
82     }
83
84     // SQL result: for simple select or create/update queries
85     // returns the database specific resource type
86     function genericSqlQuery($sql, $args = false)
87     {
88         try {
89             $sth = $this->_backend->_dbh->prepare($sql);
90             if ($args) {
91                 foreach ($args as $key => $val) {
92                     $sth->bindParam($key, $val);
93                 }
94             }
95             if ($sth->execute())
96                 $result = $sth->fetch(PDO_FETCH_BOTH);
97             else
98                 return false;
99         } catch (PDOException $e) {
100             trigger_error("SQL Error: " . $e->getMessage(), E_USER_WARNING);
101             return false;
102         }
103         return $result;
104     }
105
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)
109     {
110         $result = $this->genericSqlQuery($sql);
111         return new WikiDB_backend_PDO_generic_iter($this->_backend, $result, $field_list);
112     }
113
114 }
115
116 ;
117
118 // Local Variables:
119 // mode: php
120 // tab-width: 8
121 // c-basic-offset: 4
122 // c-hanging-comment-ender-p: nil
123 // indent-tabs-mode: nil
124 // End: