pluf2

pluf2 Git Source Tree


Root/src/Pluf/DB/PostgreSQL.php

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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# ***** END LICENSE BLOCK ***** */
 
/**
 * PostgreSQL connection class
 */
class Pluf_DB_PostgreSQL
{
    /**
     * The connection resource.
     */
    public $con_id;
 
    /**
     * The prefix for the table names.
     */
    public $pfx = '';
 
    /**
     * Debug mode.
     */
    private $debug = false;
 
    /**
     * The last query, set with debug(). Used when an error is
     * returned.
     */
    public $lastquery = '';
 
    /**
     * Name of the engine.
     */
    public $engine = 'PostgreSQL';
 
    /**
     * Used by the model to convert the values from and to the
     * database.
     *
     * @see Pluf_DB_defaultTypecast
     */
    public $type_cast = array();
 
    /**
     * Current query cursor.
     */
    private $cur = null;
 
    /**
     * Current search path.
     */
    public $search_path = 'public';
 
    function __construct($user, $pwd, $server, $dbname, $pfx='', $debug=false)
    {
        Pluf::loadFunction('Pluf_DB_defaultTypecast');
        $this->type_cast = Pluf_DB_defaultTypecast();
        $this->type_cast['Pluf_DB_Field_Boolean'] =
            array('Pluf_DB_PostgreSQL_BooleanFromDb', 'Pluf_DB_BooleanToDb');
        $this->type_cast['Pluf_DB_Field_Compressed'] =
            array('Pluf_DB_PostgreSQL_CompressedFromDb', 'Pluf_DB_PostgreSQL_CompressedToDb');
 
        $this->debug('* POSTGRESQL CONNECT');
        $cstring = '';
        if ($server) {
            $cstring .= 'host='.$server.' ';
        }
        $cstring .= 'dbname='.$dbname.' user='.$user;
        if ($pwd) {
            $cstring .= ' password='.$pwd;
        }
        $this->debug = $debug;
        $this->pfx = $pfx;
        $this->cur = null;
        $this->con_id = @pg_connect($cstring);
        if (!$this->con_id) {
            throw new Exception($this->getError());
        }
    }
 
 
    /**
     * Get the version of the PostgreSQL server.
     *
     * Requires PostgreSQL 7.4 or later.
     *
     * @return string Version string
     */
    function getServerInfo()
    {
        $ver = pg_version($this->con_id);
        return $ver['server'];
    }
 
    /**
     * Log the queries. Keep track of the last query and if in debug mode
     * keep track of all the queries in
     * $GLOBALS['_PX_debug_data']['sql_queries']
     *
     * @param string Query to keep track
     * @return bool true
     */
    function debug($query)
    {
        $this->lastquery = $query;
        if (!$this->debug) return true;
        if (!isset($GLOBALS['_PX_debug_data']['sql_queries']))
            $GLOBALS['_PX_debug_data']['sql_queries'] = array();
        $GLOBALS['_PX_debug_data']['sql_queries'][] = $query;
        return true;
    }
 
    function close()
    {
        if ($this->con_id) {
            pg_close($this->con_id);
            return true;
        } else {
            return false;
        }
    }
 
    function select($query)
    {
        $this->debug($query);
        $this->cur = @pg_query($this->con_id, $query);
        if (!$this->cur) {
            throw new Exception($this->getError());
        }
        $res = array();
        while ($row = pg_fetch_assoc($this->cur)) {
            $res[] = $row;
        }
        @pg_free_result($this->cur);
        $this->cur = null;
        return $res;
    }
 
    function execute($query)
    {
        $this->debug($query);
        $this->cur = @pg_query($this->con_id, $query);
        if (!$this->cur) {
            throw new Exception($this->getError());
        }
        return true;
    }
 
    function getLastID()
    {
        $this->debug('* GET LAST ID');
        $res = $this->select('SELECT lastval() AS last_id');
        return (int) $res[0]['last_id'];
    }
 
    /**
     * Returns a string ready to be used in the exception.
     *
     * @return string Error string
     */
    function getError()
    {
        if ($this->cur) {
            return pg_result_error($this->cur).' - '.$this->lastquery;
        }
        if ($this->con_id) {
            return pg_last_error($this->con_id).' - '.$this->lastquery;
        } else {
            return pg_last_error().' - '.$this->lastquery;
        }
    }
 
    function esc($str)
    {
        return '\''.pg_escape_string($this->con_id, $str).'\'';
    }
 
    /**
     * Set the current search path.
     */
    function setSearchPath($search_path='public')
    {
        if (preg_match('/[^\w\s\,]/', $search_path)) {
            throw new Exception('The search path: "'.$search_path.'" is not valid.');           
        }
        $this->execute('SET search_path TO '.$search_path);
        $this->search_path = $search_path;
        return true;
    }
 
    /**
     * Quote the column name.
     *
     * @param string Name of the column
     * @return string Escaped name
     */
    function qn($col)
    {
        return '"'.$col.'"';
    }
 
    /**
     * Start a transaction.
     */
    function begin()
    {
        $this->execute('BEGIN');
    }
 
    /**
     * Commit a transaction.
     */
    function commit()
    {
        $this->execute('COMMIT');
    }
 
    /**
     * Rollback a transaction.
     */
    function rollback()
    {
        $this->execute('ROLLBACK');
    }
 
    function __toString()
    {
        return '<Pluf_DB_PostgreSQL('.$this->con_id.')>';
    }
}
 
function Pluf_DB_PostgreSQL_BooleanFromDb($val)
{
    if (!$val) {
        return false;
    }
    return (strtolower(substr($val, 0, 1)) == 't');
}
 
function Pluf_DB_PostgreSQL_CompressedToDb($val, $con)
{
    if (is_null($val)) {
        return 'NULL';
    }
    return "'".pg_escape_bytea(gzdeflate($val, 9))."'";
}
 
function Pluf_DB_PostgreSQL_CompressedFromDb($val)
{
    return ($val) ? gzinflate(pg_unescape_bytea($val)) : $val;
}

Archive Download this file

Branches

Number of commits:
Page rendered in 0.08492s using 11 queries.