]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiDB/SQL.php
rcs_id no longer makes sense with Subversion global version number
[SourceForge/phpwiki.git] / lib / WikiDB / SQL.php
1 <?php // rcs_id('$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     /**
45      * Determine whether page exists (in non-default form).
46      * @see WikiDB::isWikiPage for the slow generic version
47      */
48     function isWikiPage ($pagename) {
49         $pagename = (string) $pagename;
50         if ($pagename === '') return false;
51         //if (empty($this->_iwpcache)) {  $this->_iwpcache = array();  }
52         if (empty($this->_cache->id_cache[$pagename])) {
53             $this->_cache->_id_cache[$pagename] = $this->_backend->is_wiki_page($pagename);
54         }
55         return $this->_cache->_id_cache[$pagename];
56     }
57
58     // adds surrounding quotes 
59     function quote ($s) { return $this->_backend->_dbh->quoteSmart($s); }
60     // no surrounding quotes because we know it's a string
61     function qstr ($s) {  return $this->_backend->_dbh->escapeSimple($s); }
62
63     function isOpen () {
64         global $request;
65         if (!$request->_dbi) return false;
66         return is_resource($this->_backend->connection());
67     }
68
69     // SQL result: for simple select or create/update queries
70     // returns the database specific resource type
71     function genericSqlQuery($sql, $args=false) {
72         if ($args)
73             $result = $this->_backend->_dbh->query($sql, $args);
74         else
75             $result = $this->_backend->_dbh->query($sql);
76         if (DB::isError($result)) {
77             $msg = $result->getMessage();
78             trigger_error("SQL Error: ".DB::errorMessage($result), E_USER_WARNING);
79             return false;
80         } else {
81             return $result;
82         }
83     }
84
85     // SQL iter: for simple select or create/update queries
86     // returns the generic iterator object (count,next)
87     function genericSqlIter($sql, $field_list = NULL) {
88         $result = $this->genericSqlQuery($sql);
89         return new WikiDB_backend_PearDB_generic_iter($this->_backend, $result);
90     }
91
92 };
93
94   
95 // For emacs users
96 // Local Variables:
97 // mode: php
98 // tab-width: 8
99 // c-basic-offset: 4
100 // c-hanging-comment-ender-p: nil
101 // indent-tabs-mode: nil
102 // End:
103
104 ?>