File: ADODB_postgres7.php

package info (click to toggle)
phpwiki 1.3.12p3-5etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 16,956 kB
  • ctags: 21,608
  • sloc: php: 82,335; xml: 3,840; sh: 1,522; sql: 1,198; perl: 625; makefile: 562; awk: 28
file content (163 lines) | stat: -rw-r--r-- 4,467 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php // -*-php-*-
rcs_id('$Id: ADODB_postgres7.php,v 1.3 2005/10/31 16:49:02 rurban Exp $');

require_once('lib/WikiDB/backend/ADODB.php');

if (!defined("USE_BYTEA"))     // see schemas/psql-initialize.sql
    define("USE_BYTEA", true); // only BYTEA is binary safe
    //define("USE_BYTEA", false);

/**
 * WikiDB layer for ADODB-postgres (7 or 8), called by lib/WikiDB/ADODB.php.
 * Pending changes from CVS HEAD:
 *  - use Foreign Keys and ON DELETE CASCADE.
 *  - bytea blob type
 *  - tsearch2
 * 
 * @author: Reini Urban
 */
class WikiDB_backend_ADODB_postgres7
extends WikiDB_backend_ADODB
{
    /**
     * Constructor.
     */
    function WikiDB_backend_ADODB_postgres7($dbparams) {
        $this->WikiDB_backend_ADODB($dbparams);

        $this->_serverinfo = $this->_dbh->ServerInfo();
        if (!empty($this->_serverinfo['version'])) {
            $arr = explode('.',$this->_serverinfo['version']);
            $this->_serverinfo['version'] = (string)(($arr[0] * 100) + $arr[1]);
	    if (!empty($arr[2]))
		$this->_serverinfo['version'] .= ("." . (integer)$arr[2]);
        }
    }

    /**
     * Pack tables.
     */
    function optimize() {
        foreach ($this->_table_names as $table) {
            $this->_dbh->Execute("VACUUM ANALYZE $table");
        }
        return 1;
    }

    // just for blobs. the rest is escaped with qstr()
    function _quote($s) {
	if (USE_BYTEA)
	    return $this->_dbh->BlobEncode($s);
        return base64_encode($s);
    }

    // just for blobs, which might be base64_encoded
    function _unquote($s) {
	if (USE_BYTEA) {
	    //if function_exists('pg_unescape_bytea')
	    //return pg_unescape_bytea($s);
	    // TODO: already unescaped by ADORecordSet_postgres64::_decode?
	    return $s;
	}
        return base64_decode($s);
    }

    function get_cached_html($pagename) {
        $dbh = &$this->_dbh;
        $page_tbl = $this->_table_names['page_tbl'];
	$data = $dbh->GetOne(sprintf("SELECT cached_html FROM $page_tbl WHERE pagename=%s",
                                     $dbh->qstr($pagename)));
        if ($data) return $this->_unquote($data);
        else return '';
    }

    function set_cached_html($pagename, $data) {
        $dbh = &$this->_dbh;
        $page_tbl = $this->_table_names['page_tbl'];
	if (USE_BYTEA) {
	    $dbh->UpdateBlob($page_tbl,'cached_html',$data,"pagename=".$dbh->qstr($pagename));
	    /*
	    $dbh->Execute(sprintf("UPDATE $page_tbl"
				  . " SET cached_html='%s'"
				  . " WHERE pagename=%s",
				  $this->_quote($data), 
				  $dbh->qstr($pagename)));
	    */
	} else {
	    $dbh->Execute("UPDATE $page_tbl"
			  . " SET cached_html=?"
			  . " WHERE pagename=?",
			  array($this->_quote($data), $pagename));
        }
    }

    /**
     * Lock all tables we might use.
     * postgresql has proper transactions so we dont need table locks.
     */
    function _lock_tables($tables, $write_lock = true) {
        ;
    }

    /**
     * Unlock all tables.
     * postgresql has proper transactions so we dont need table locks.
     */
    function _unlock_tables($tables, $write_lock=false) {
        ;
    }

    /**
     * Serialize data
     */
    function _serialize($data) {
        if (empty($data))
            return '';
        assert(is_array($data));
        return $this->_quote(serialize($data));
    }

    /**
     * Unserialize data
     */
    function _unserialize($data) {
        if (empty($data))
            return array();
        // Base64 encoded data does not contain colons.
        //  (only alphanumerics and '+' and '/'.)
        if (substr($data,0,2) == 'a:')
            return unserialize($data);
        return unserialize($this->_unquote($data));
    }

};

class WikiDB_backend_ADODB_postgres7_search
extends WikiDB_backend_ADODB_search
{
    function _pagename_match_clause($node) {
        $word = $node->sql();
        if ($node->op == 'REGEX') { // posix regex extensions
            return ($this->_case_exact 
                    ? "pagename ~* '$word'"
                    : "pagename ~ '$word'");
        } else {
            return ($this->_case_exact 
                    ? "pagename LIKE '$word'" 
                    : "pagename ILIKE '$word'");
        }
    }

    // TODO: use tsearch2
    //function _fulltext_match_clause($node)
}

// (c-file-style: "gnu")
// Local Variables:
// mode: php
// tab-width: 8
// c-basic-offset: 4
// c-hanging-comment-ender-p: nil
// indent-tabs-mode: nil
// End:   
?>