]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiDB/backend/ADODB_postgres7.php
note about vacuum permissions
[SourceForge/phpwiki.git] / lib / WikiDB / backend / ADODB_postgres7.php
1 <?php // -*-php-*-
2 rcs_id('$Id: ADODB_postgres7.php,v 1.7 2006-12-23 11:56:17 rurban Exp $');
3
4 require_once('lib/WikiDB/backend/ADODB.php');
5
6 if (!defined("USE_BYTEA"))     // see schemas/psql-initialize.sql
7     define("USE_BYTEA", true); // only BYTEA is binary safe
8     //define("USE_BYTEA", false);
9
10 /**
11  * WikiDB layer for ADODB-postgres (7 or 8), called by lib/WikiDB/ADODB.php.
12  * Changes 1.3.12: 
13  *  - use Foreign Keys and ON DELETE CASCADE.
14  *  - bytea blob type
15  * 
16  * @author: Reini Urban
17  */
18 class WikiDB_backend_ADODB_postgres7
19 extends WikiDB_backend_ADODB
20 {
21     /**
22      * Constructor.
23      */
24     function WikiDB_backend_ADODB_postgres7($dbparams) {
25         $this->WikiDB_backend_ADODB($dbparams);
26
27         $this->_serverinfo = $this->_dbh->ServerInfo();
28         if (!empty($this->_serverinfo['version'])) {
29             $arr = explode('.',$this->_serverinfo['version']);
30             $this->_serverinfo['version'] = (string)(($arr[0] * 100) + $arr[1]);
31             if (!empty($arr[2]))
32                 $this->_serverinfo['version'] .= ("." . (integer)$arr[2]);
33         }
34     }
35
36     /**
37      * Pack tables.
38      * NOTE: Only the table owner can do this. Either fix the schema or setup autovacuum.
39      */
40     function optimize() {
41         return 0;       // if the wikiuser is not the table owner
42
43         foreach ($this->_table_names as $table) {
44             $this->_dbh->Execute("VACUUM ANALYZE $table");
45         }
46         return 1;
47     }
48
49     // just for blobs. the rest is escaped with qstr()
50     function _quote($s) {
51         if (USE_BYTEA)
52             return $this->_dbh->BlobEncode($s);
53         return base64_encode($s);
54     }
55
56     // just for blobs, which might be base64_encoded
57     function _unquote($s) {
58         if (USE_BYTEA) {
59             //if function_exists('pg_unescape_bytea')
60             //return pg_unescape_bytea($s);
61             // TODO: already unescaped by ADORecordSet_postgres64::_decode?
62             return $s;
63         }
64         return base64_decode($s);
65     }
66
67     function get_cached_html($pagename) {
68         $dbh = &$this->_dbh;
69         $page_tbl = $this->_table_names['page_tbl'];
70         $data = $dbh->GetOne(sprintf("SELECT cached_html FROM $page_tbl WHERE pagename=%s",
71                                      $dbh->qstr($pagename)));
72         if ($data) return $this->_unquote($data);
73         else return '';
74     }
75
76     function set_cached_html($pagename, $data) {
77         $dbh = &$this->_dbh;
78         $page_tbl = $this->_table_names['page_tbl'];
79         if (USE_BYTEA) {
80             $dbh->UpdateBlob($page_tbl,'cached_html',$data,"pagename=".$dbh->qstr($pagename));
81             /*
82             $dbh->Execute(sprintf("UPDATE $page_tbl"
83                                   . " SET cached_html='%s'"
84                                   . " WHERE pagename=%s",
85                                   $this->_quote($data), 
86                                   $dbh->qstr($pagename)));
87             */
88         } else {
89             $dbh->Execute("UPDATE $page_tbl"
90                           . " SET cached_html=?"
91                           . " WHERE pagename=?",
92                           array($this->_quote($data), $pagename));
93         }
94     }
95
96     /**
97      * Lock all tables we might use.
98      * postgresql has proper transactions so we dont need table locks.
99      */
100     function _lock_tables($tables, $write_lock = true) {
101         ;
102     }
103
104     /**
105      * Unlock all tables.
106      * postgresql has proper transactions so we dont need table locks.
107      */
108     function _unlock_tables($tables, $write_lock=false) {
109         ;
110     }
111
112     /**
113      * Serialize data
114      */
115     function _serialize($data) {
116         if (empty($data))
117             return '';
118         assert(is_array($data));
119         return $this->_quote(serialize($data));
120     }
121
122     /**
123      * Unserialize data
124      */
125     function _unserialize($data) {
126         if (empty($data))
127             return array();
128         // Base64 encoded data does not contain colons.
129         //  (only alphanumerics and '+' and '/'.)
130         if (substr($data,0,2) == 'a:')
131             return unserialize($data);
132         return unserialize($this->_unquote($data));
133     }
134
135 };
136
137 class WikiDB_backend_ADODB_postgres7_search
138 extends WikiDB_backend_ADODB_search
139 {
140     function _pagename_match_clause($node) {
141         $word = $node->sql();
142         if ($node->op == 'REGEX') { // posix regex extensions
143             return ($this->_case_exact 
144                     ? "pagename ~* '$word'"
145                     : "pagename ~ '$word'");
146         } else {
147             return ($this->_case_exact 
148                     ? "pagename LIKE '$word'" 
149                     : "pagename ILIKE '$word'");
150         }
151     }
152
153     // TODO: use tsearch2
154     //function _fulltext_match_clause($node)
155 }
156
157 // (c-file-style: "gnu")
158 // Local Variables:
159 // mode: php
160 // tab-width: 8
161 // c-basic-offset: 4
162 // c-hanging-comment-ender-p: nil
163 // indent-tabs-mode: nil
164 // End:   
165 ?>