]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiDB/backend/ADODB_postgres7.php
fix typo
[SourceForge/phpwiki.git] / lib / WikiDB / backend / ADODB_postgres7.php
1 <?php // -*-php-*-
2 rcs_id('$Id: ADODB_postgres7.php,v 1.4 2006-02-22 18:58:43 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);
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      */
39     function optimize() {
40         foreach ($this->_table_names as $table) {
41             $this->_dbh->Execute("VACUUM ANALYZE $table");
42         }
43         return 1;
44     }
45
46     // just for blobs. the rest is escaped with qstr()
47     function _quote($s) {
48         if (USE_BYTEA)
49             return $this->_dbh->BlobEncode($s);
50         if (function_exists('pg_escape_string'))
51             return pg_escape_string($s);
52         else
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         if (function_exists('pg_escape_string'))
65             return $s;
66         else
67             return base64_decode($s);
68     }
69
70     function get_cached_html($pagename) {
71         $dbh = &$this->_dbh;
72         $page_tbl = $this->_table_names['page_tbl'];
73         $data = $dbh->GetOne(sprintf("SELECT cached_html FROM $page_tbl WHERE pagename=%s",
74                                      $dbh->qstr($pagename)));
75         if ($data) return $this->_unquote($data);
76         else return '';
77     }
78
79     function set_cached_html($pagename, $data) {
80         $dbh = &$this->_dbh;
81         $page_tbl = $this->_table_names['page_tbl'];
82         if (USE_BYTEA) {
83             $dbh->UpdateBlob($page_tbl,'cached_html',$data,"pagename=".$dbh->qstr($pagename));
84             /*
85             $dbh->Execute(sprintf("UPDATE $page_tbl"
86                                   . " SET cached_html='%s'"
87                                   . " WHERE pagename=%s",
88                                   $this->_quote($data), 
89                                   $dbh->qstr($pagename)));
90             */
91         } else {
92             $dbh->Execute("UPDATE $page_tbl"
93                           . " SET cached_html=?"
94                           . " WHERE pagename=?",
95                           array($this->_quote($data), $pagename));
96     }
97
98     /**
99      * Lock all tables we might use.
100      */
101     function _lock_tables($tables, $write_lock = true) {
102         $this->_dbh->Execute("BEGIN");
103     }
104
105     /**
106      * Unlock all tables.
107      */
108     function _unlock_tables($tables, $write_lock=false) {
109         $this->_dbh->Execute("COMMIT");
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 ?>