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