1 | <?php
|
---|
2 | /*
|
---|
3 | $Id: pgsql_database.php 1929 2008-02-10 06:13:30Z andrewsimpson $
|
---|
4 |
|
---|
5 | (c) 2002 - 2008 Andrew Simpson <andrew.simpson at paradise.net.nz>
|
---|
6 |
|
---|
7 | WebCollab
|
---|
8 | ---------------------------------------
|
---|
9 |
|
---|
10 | This program is free software; you can redistribute it and/or modify it under the
|
---|
11 | terms of the GNU General Public License as published by the Free Software Foundation;
|
---|
12 | either version 2 of the License, or (at your option) any later version.
|
---|
13 |
|
---|
14 | This program is distributed in the hope that it will be useful, but WITHOUT ANY
|
---|
15 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
---|
16 | PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
---|
17 |
|
---|
18 | You should have received a copy of the GNU General Public License along with this
|
---|
19 | program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave,
|
---|
20 | Cambridge, MA 02139, USA.
|
---|
21 |
|
---|
22 | Function:
|
---|
23 | ---------
|
---|
24 |
|
---|
25 | Creates a singular interface for database access.
|
---|
26 |
|
---|
27 | This file is for PostgreSQL with PHP versions greater than (and including) 4.2.0
|
---|
28 |
|
---|
29 | */
|
---|
30 |
|
---|
31 | require_once('path.php' );
|
---|
32 |
|
---|
33 | /* NOTE!
|
---|
34 | Standard Postgresql (Version 6.3 and later) setup does NOT support tcp/ip connections.
|
---|
35 | To support tcp/ip connections you need to:
|
---|
36 | - Edit PostgreSQL config file pg_hba.conf to allow tcp/ip from appropriate hosts
|
---|
37 | - Start postmaster with -i option to allow tcp/ip connections
|
---|
38 | */
|
---|
39 |
|
---|
40 | //set some base variables
|
---|
41 | $database_connection = '';
|
---|
42 | $delim = "'";
|
---|
43 | $epoch = 'extract(epoch FROM ';
|
---|
44 | $day_part = 'DATE_PART(\'day\', ';
|
---|
45 | $date_type = 'TIMESTAMP';
|
---|
46 | //
|
---|
47 | // Makes a connection to the database
|
---|
48 | //
|
---|
49 | function db_connection() {
|
---|
50 |
|
---|
51 | global $database_connection;
|
---|
52 |
|
---|
53 | //set host string correctly
|
---|
54 | $host = (DATABASE_HOST != 'localhost' ) ? 'host='.DATABASE_HOST : '';
|
---|
55 |
|
---|
56 | if( ! ($database_connection = @pg_connect($host.' user='.DATABASE_USER.' dbname='.DATABASE_NAME.' password='.DATABASE_PASSWORD ) ) )
|
---|
57 | error('No database connection', 'Sorry but there seems to be a problem in connecting to the database' );
|
---|
58 |
|
---|
59 | //make sure dates will be handled properly by internal date routines
|
---|
60 | pg_query($database_connection, 'SET DATESTYLE TO \'European, ISO\'');
|
---|
61 |
|
---|
62 | //set the timezone
|
---|
63 | if(! @pg_query($database_connection, 'SET TIME ZONE '.TZ ) ) {
|
---|
64 | error("Database error", "Not able to set timezone" );
|
---|
65 | }
|
---|
66 |
|
---|
67 | return;
|
---|
68 | }
|
---|
69 |
|
---|
70 | //
|
---|
71 | // Provides a safe way to do a query
|
---|
72 | //
|
---|
73 | function db_query($query, $die_on_error=1 ) {
|
---|
74 |
|
---|
75 | global $database_connection, $db_error_message;
|
---|
76 |
|
---|
77 | if(! $database_connection ) db_connection();
|
---|
78 |
|
---|
79 | //do it
|
---|
80 | if( ! ($result = @pg_query($database_connection, $query ) ) ) {
|
---|
81 | $db_error_message = pg_last_error($database_connection);
|
---|
82 |
|
---|
83 | if($die_on_error) {
|
---|
84 | error('Database query error', 'The following query :<br /><br /><b>'.$query.'</b><br /><br />Had the following error:<br /><b>'.pg_last_error($database_connection).'</b>' );
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | //all was okay return resultset
|
---|
89 | return $result;
|
---|
90 | }
|
---|
91 |
|
---|
92 | //
|
---|
93 | // escapes special characters in a string for use in a SQL statement
|
---|
94 | //
|
---|
95 | function db_escape_string($string ) {
|
---|
96 |
|
---|
97 | return pg_escape_string($string);
|
---|
98 | }
|
---|
99 |
|
---|
100 | //
|
---|
101 | // get a single result set
|
---|
102 | //
|
---|
103 | function db_result($q, $row=0, $field=0 ) {
|
---|
104 |
|
---|
105 | return pg_fetch_result($q, $row, $field );
|
---|
106 | }
|
---|
107 |
|
---|
108 | //
|
---|
109 | // fetch array result set
|
---|
110 | //
|
---|
111 | function db_fetch_array($q, $row=0 ) {
|
---|
112 |
|
---|
113 | return pg_fetch_array($q, $row, PGSQL_ASSOC );
|
---|
114 | }
|
---|
115 |
|
---|
116 | //
|
---|
117 | // fetch enumerated array result set
|
---|
118 | //
|
---|
119 | function db_fetch_num($q, $row=0 ) {
|
---|
120 |
|
---|
121 | return pg_fetch_row($q, $row );
|
---|
122 | }
|
---|
123 |
|
---|
124 | //
|
---|
125 | // last oid
|
---|
126 | //
|
---|
127 | function db_lastoid($seq_name ) {
|
---|
128 |
|
---|
129 | //must be done after an insert, and within a transaction
|
---|
130 | $result = db_query('SELECT CURRVAL(\''.$seq_name.'\') AS seq' );
|
---|
131 |
|
---|
132 | return pg_fetch_result( $result, 0, 0 );
|
---|
133 | }
|
---|
134 |
|
---|
135 | //
|
---|
136 | //free memory
|
---|
137 | //
|
---|
138 | function db_free_result($q ){
|
---|
139 |
|
---|
140 | global $database_connection;
|
---|
141 |
|
---|
142 | return pg_free_result($q );
|
---|
143 | }
|
---|
144 |
|
---|
145 | //
|
---|
146 | //begin transaction
|
---|
147 | //
|
---|
148 | function db_begin() {
|
---|
149 |
|
---|
150 | global $database_connection;
|
---|
151 |
|
---|
152 | return pg_query($database_connection, 'BEGIN WORK' );
|
---|
153 | }
|
---|
154 |
|
---|
155 | //
|
---|
156 | //rollback transaction
|
---|
157 | //
|
---|
158 | function db_rollback() {
|
---|
159 |
|
---|
160 | global $database_connection;
|
---|
161 |
|
---|
162 | return pg_query($database_connection, 'ROLLBACK WORK' );
|
---|
163 | }
|
---|
164 |
|
---|
165 | //
|
---|
166 | //commit transaction
|
---|
167 | //
|
---|
168 | function db_commit() {
|
---|
169 |
|
---|
170 | global $database_connection;
|
---|
171 |
|
---|
172 | return pg_query($database_connection, 'COMMIT WORK' );
|
---|
173 | }
|
---|
174 |
|
---|
175 | //
|
---|
176 | //sets the required session client encoding
|
---|
177 | //
|
---|
178 | function db_user_locale($encoding ) {
|
---|
179 |
|
---|
180 | global $database_connection;
|
---|
181 |
|
---|
182 | if(! $database_connection ) db_connection();
|
---|
183 |
|
---|
184 | switch(strtoupper($encoding) ) {
|
---|
185 |
|
---|
186 | case 'ISO-8859-1':
|
---|
187 | $pg_encoding = 'LATIN1';
|
---|
188 | break;
|
---|
189 |
|
---|
190 | case 'UTF-8':
|
---|
191 | $pg_encoding = 'UNICODE';
|
---|
192 | break;
|
---|
193 |
|
---|
194 | case 'ISO-8859-2':
|
---|
195 | $pg_encoding = 'LATIN2';
|
---|
196 | break;
|
---|
197 |
|
---|
198 | case 'ISO-8859-7':
|
---|
199 | $pg_encoding = 'ISO_8859_7';
|
---|
200 | break;
|
---|
201 |
|
---|
202 | case 'ISO-8859-9':
|
---|
203 | $pg_encoding = 'LATIN5';
|
---|
204 | break;
|
---|
205 |
|
---|
206 | case 'ISO-8859-15':
|
---|
207 | $pg_encoding = 'LATIN9';
|
---|
208 | break;
|
---|
209 |
|
---|
210 | case 'KOI8-R':
|
---|
211 | $pg_encoding = 'KOI8';
|
---|
212 | break;
|
---|
213 |
|
---|
214 | case 'WINDOWS-1251':
|
---|
215 | $pg_encoding = 'WIN';
|
---|
216 | break;
|
---|
217 |
|
---|
218 | default:
|
---|
219 | $pg_encoding = 'LATIN1';
|
---|
220 | break;
|
---|
221 | }
|
---|
222 |
|
---|
223 | //set client encoding to match character set in use
|
---|
224 | if(@pg_set_client_encoding($database_connection, $pg_encoding ) == -1 ){
|
---|
225 | error('Database client encoding', 'Setting client encoding to '.$pg_encoding.' character set had the following error:<br />'.pg_last_error($database_connection) );
|
---|
226 | }
|
---|
227 |
|
---|
228 | return true;
|
---|
229 | }
|
---|
230 |
|
---|
231 | ?>
|
---|