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