]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiDB/PDO.php
include [all] Include and file path should be devided with single space. File path...
[SourceForge/phpwiki.git] / lib / WikiDB / PDO.php
1 <?php // -*-php-*-
2
3
4 require_once 'lib/WikiDB.php';
5
6 /**
7  * WikiDB layer for PDO, the new php5 abstraction layer, with support for
8  * prepared statements and transactions.
9  *
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."
17  *
18  * @author: Reini Urban
19  */
20 class WikiDB_PDO extends WikiDB
21 {
22     function WikiDB_PDO ($dbparams) {
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         $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);
48         }
49         return $this->_cache->_id_cache[$pagename];
50     }
51
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)) {
57             return $in;
58         } elseif (is_bool($in)) {
59             return $in ? 1 : 0;
60         } elseif (is_null($in)) {
61             return 'NULL';
62         } else {
63             return $this->qstr($in);
64         }
65     }
66     // Don't add surrounding quotes '', same as in PearDB
67     // PDO-0.2.1 added now ::quote()
68     function qstr ($in) {
69         $in = str_replace(array('\\',"\0"),array('\\\\',"\\\0"), $in);
70         return str_replace("'", "\'", $in);
71     }
72
73     function isOpen () {
74         global $request;
75         if (!$request->_dbi) return false;
76         return is_object($this->_backend->_dbh);
77     }
78
79     // SQL result: for simple select or create/update queries
80     // returns the database specific resource type
81     function genericSqlQuery($sql, $args=false) {
82         try {
83             $sth = $this->_backend->_dbh->prepare($sql);
84             if ($args) {
85                 foreach ($args as $key => $val ) {
86                     $sth->bindParam($key, $val);
87                 }
88             }
89             if ($sth->execute())
90                 $result = $sth->fetch(PDO_FETCH_BOTH);
91             else
92                 return false;
93         }
94         catch (PDOException $e) {
95             trigger_error("SQL Error: ".$e->getMessage(), E_USER_WARNING);
96             return false;
97         }
98         return $result;
99     }
100
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);
106     }
107
108 };
109
110 // Local Variables:
111 // mode: php
112 // tab-width: 8
113 // c-basic-offset: 4
114 // c-hanging-comment-ender-p: nil
115 // indent-tabs-mode: nil
116 // End: