]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiDB/SQL.php
Reformat code
[SourceForge/phpwiki.git] / lib / WikiDB / SQL.php
1 <?php
2
3 require_once 'lib/WikiDB.php';
4 //require_once('lib/WikiDB/backend/PearDB.php');
5 //require_once('DB.php'); // Always favor use our local pear copy
6
7 /**
8  *
9  */
10 class WikiDB_SQL extends WikiDB
11 {
12     function WikiDB_SQL($dbparams)
13     {
14         $backend = 'PearDB';
15         if (is_array($dbparams['dsn']))
16             $backend = $dbparams['dsn']['phptype'];
17         elseif (preg_match('/^(\w+):/', $dbparams['dsn'], $m))
18             $backend = $m[1];
19         if ($backend == 'postgres7') { // ADODB cross-compatiblity hack (for unit testing)
20             $backend = 'pgsql';
21             if (is_string($dbparams['dsn']))
22                 $dbparams['dsn'] = $backend . ':' . substr($dbparams['dsn'], 10);
23         }
24         include_once 'lib/WikiDB/backend/PearDB_' . $backend . '.php';
25         $backend_class = "WikiDB_backend_PearDB_" . $backend;
26         $backend = new $backend_class($dbparams);
27         if (DB::isError($backend->_dbh)) return;
28         $this->WikiDB($backend, $dbparams);
29     }
30
31     function view_dsn($dsn = false)
32     {
33         if (!$dsn)
34             $dsninfo = DB::parseDSN($GLOBALS['DBParams']['dsn']);
35         else
36             $dsninfo = DB::parseDSN($dsn);
37         return sprintf("%s://%s:<not displayed>@%s/%s",
38             $dsninfo['phptype'],
39             $dsninfo['username'],
40             $dsninfo['hostspec'],
41             $dsninfo['database']
42         );
43     }
44
45     /**
46      * Determine whether page exists (in non-default form).
47      * @see WikiDB::isWikiPage for the slow generic version
48      */
49     function isWikiPage($pagename)
50     {
51         $pagename = (string)$pagename;
52         if ($pagename === '') return false;
53         //if (empty($this->_iwpcache)) {  $this->_iwpcache = array();  }
54         if (empty($this->_cache->id_cache[$pagename])) {
55             $this->_cache->_id_cache[$pagename] = $this->_backend->is_wiki_page($pagename);
56         }
57         return $this->_cache->_id_cache[$pagename];
58     }
59
60     // adds surrounding quotes
61     function quote($s)
62     {
63         return $this->_backend->_dbh->quoteSmart($s);
64     }
65
66     // no surrounding quotes because we know it's a string
67     function qstr($s)
68     {
69         return $this->_backend->_dbh->escapeSimple($s);
70     }
71
72     function isOpen()
73     {
74         global $request;
75         if (!$request->_dbi) return false;
76         return is_resource($this->_backend->connection());
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     {
83         if ($args)
84             $result = $this->_backend->_dbh->query($sql, $args);
85         else
86             $result = $this->_backend->_dbh->query($sql);
87         if (DB::isError($result)) {
88             $msg = $result->getMessage();
89             trigger_error("SQL Error: " . DB::errorMessage($result), E_USER_WARNING);
90             return false;
91         } else {
92             return $result;
93         }
94     }
95
96     // SQL iter: for simple select or create/update queries
97     // returns the generic iterator object (count,next)
98     function genericSqlIter($sql, $field_list = NULL)
99     {
100         $result = $this->genericSqlQuery($sql);
101         return new WikiDB_backend_PearDB_generic_iter($this->_backend, $result);
102     }
103
104 }
105
106 ;
107
108 // Local Variables:
109 // mode: php
110 // tab-width: 8
111 // c-basic-offset: 4
112 // c-hanging-comment-ender-p: nil
113 // indent-tabs-mode: nil
114 // End: