]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiDB/backend/ADODB_postgres7.php
include [all] Include and file path should be devided with single space. File path...
[SourceForge/phpwiki.git] / lib / WikiDB / backend / ADODB_postgres7.php
1 <?php // -*-php-*-
2
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         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         return 0;       // if the wikiuser is not the table owner
43
44         foreach ($this->_table_names as $table) {
45             $this->_dbh->Execute("VACUUM ANALYZE $table");
46         }
47         return 1;
48     }
49
50     // just for blobs. the rest is escaped with qstr()
51     function _quote($s) {
52     if (USE_BYTEA)
53         return $this->_dbh->BlobEncode($s);
54         return base64_encode($s);
55     }
56
57     // just for blobs, which might be base64_encoded
58     function _unquote($s) {
59     if (USE_BYTEA) {
60         //if function_exists('pg_unescape_bytea')
61         //return pg_unescape_bytea($s);
62         // TODO: already unescaped by ADORecordSet_postgres64::_decode?
63         return $s;
64     }
65         return base64_decode($s);
66     }
67
68     function get_cached_html($pagename) {
69         $dbh = &$this->_dbh;
70         $page_tbl = $this->_table_names['page_tbl'];
71     $data = $dbh->GetOne(sprintf("SELECT cached_html FROM $page_tbl WHERE pagename=%s",
72                                      $dbh->qstr($pagename)));
73         if ($data) return $this->_unquote($data);
74         else return '';
75     }
76
77     function set_cached_html($pagename, $data) {
78         $dbh = &$this->_dbh;
79         $page_tbl = $this->_table_names['page_tbl'];
80     if (USE_BYTEA) {
81         $dbh->UpdateBlob($page_tbl,'cached_html',$data,"pagename=".$dbh->qstr($pagename));
82         /*
83         $dbh->Execute(sprintf("UPDATE $page_tbl"
84                   . " SET cached_html='%s'"
85                   . " WHERE pagename=%s",
86                   $this->_quote($data),
87                   $dbh->qstr($pagename)));
88         */
89     } else {
90         $dbh->Execute("UPDATE $page_tbl"
91               . " SET cached_html=?"
92               . " WHERE pagename=?",
93               array($this->_quote($data), $pagename));
94         }
95     }
96
97     /**
98      * Lock all tables we might use.
99      * postgresql has proper transactions so we dont need table locks.
100      */
101     function _lock_tables($tables, $write_lock = true) {
102         ;
103     }
104
105     /**
106      * Unlock all tables.
107      * postgresql has proper transactions so we dont need table locks.
108      */
109     function _unlock_tables($tables, $write_lock=false) {
110         ;
111     }
112
113     /**
114      * Serialize data
115      */
116     function _serialize($data) {
117         if (empty($data))
118             return '';
119         assert(is_array($data));
120         return $this->_quote(serialize($data));
121     }
122
123     /**
124      * Unserialize data
125      */
126     function _unserialize($data) {
127         if (empty($data))
128             return array();
129         // Base64 encoded data does not contain colons.
130         //  (only alphanumerics and '+' and '/'.)
131         if (substr($data,0,2) == 'a:')
132             return unserialize($data);
133         return unserialize($this->_unquote($data));
134     }
135
136 };
137
138 class WikiDB_backend_ADODB_postgres7_search
139 extends WikiDB_backend_ADODB_search
140 {
141     function _pagename_match_clause($node) {
142         $word = $node->sql();
143         if ($node->op == 'REGEX') { // posix regex extensions
144             return ($this->_case_exact
145                     ? "pagename ~* '$word'"
146                     : "pagename ~ '$word'");
147         } else {
148             return ($this->_case_exact
149                     ? "pagename LIKE '$word'"
150                     : "pagename ILIKE '$word'");
151         }
152     }
153
154     // TODO: use tsearch2
155     //function _fulltext_match_clause($node)
156 }
157
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: