6ada5ed04de1ac5190b3f7713b08dc81321bd733
[lhc/web/wiklou.git] / includes / DatabasePostgreSQL.php
1 <?php
2 # $Id$
3 #
4 # DO NOT USE !!! Unless you want to help developping it.
5 #
6 # This file is an attempt to port the mysql database layer to postgreSQL. The
7 # only thing done so far is s/mysql/pg/ and dieing if function haven't been
8 # ported.
9 #
10 # As said brion 07/06/2004 :
11 # "table definitions need to be changed. fulltext index needs to work differently
12 # things that use the last insert id need to be changed. Probably other things
13 # need to be changed. various semantics may be different."
14 #
15 # Hashar
16
17 require_once( "Database.php" );
18
19 class DatabasePgsql extends Database {
20 var $mInsertId = NULL;
21
22 function DatabasePgsql($server = false, $user = false, $password = false, $dbName = false,
23 $failFunction = false, $flags = 0, $tablePrefix = 'get from global' )
24 {
25 Database::Database( $server, $user, $password, $dbName, $failFunction, $flags, $tablePrefix );
26 }
27
28 /* static */ function newFromParams( $server = false, $user = false, $password = false, $dbName = false,
29 $failFunction = false, $flags = 0, $tablePrefix = 'get from global' )
30 {
31 return new DatabasePgsql( $server, $user, $password, $dbName, $failFunction, $flags, $tablePrefix );
32 }
33
34 # Usually aborts on failure
35 # If the failFunction is set to a non-zero integer, returns success
36 function open( $server, $user, $password, $dbName )
37 {
38 # Test for PostgreSQL support, to avoid suppressed fatal error
39 if ( !function_exists( 'pg_connect' ) ) {
40 die( "PostgreSQL functions missing, have you compiled PHP with the --with-pgsql option?\n" );
41 }
42
43 $this->close();
44 $this->mServer = $server;
45 $this->mUser = $user;
46 $this->mPassword = $password;
47 $this->mDBname = $dbName;
48
49 $success = false;
50
51 if ( "" != $dbName ) {
52 # start a database connection
53 @$this->mConn = pg_connect("host=$server dbname=$dbName user=$user password=$password");
54 if ( $this->mConn == false ) {
55 wfDebug( "DB connection error\n" );
56 wfDebug( "Server: $server, Database: $dbName, User: $user, Password: " . substr( $password, 0, 3 ) . "...\n" );
57 wfDebug( $this->lastError()."\n" );
58 } else {
59 $this->mOpened = true;
60 }
61 }
62 return $this->mConn;
63 }
64
65 # Closes a database connection, if it is open
66 # Returns success, true if already closed
67 function close()
68 {
69 $this->mOpened = false;
70 if ( $this->mConn ) {
71 return pg_close( $this->mConn );
72 } else {
73 return true;
74 }
75 }
76
77 function doQuery( $sql ) {
78 return pg_query( $this->mConn , $sql);
79 }
80
81 function queryIgnore( $sql, $fname = "" ) {
82 return $this->query( $sql, $fname, true );
83 }
84
85 function freeResult( $res ) {
86 if ( !@pg_free_result( $res ) ) {
87 wfDebugDieBacktrace( "Unable to free PostgreSQL result\n" );
88 }
89 }
90 function fetchObject( $res ) {
91 @$row = pg_fetch_object( $res );
92 # FIXME: HACK HACK HACK HACK debug
93
94 # TODO:
95 # hashar : not sure if the following test really trigger if the object
96 # fetching failled.
97 if( pg_last_error($this->mConn) ) {
98 wfDebugDieBacktrace( "SQL error: " . htmlspecialchars( pg_last_error($this->mConn) ) );
99 }
100 return $row;
101 }
102
103 function fetchRow( $res ) {
104 @$row = pg_fetch_array( $res );
105 if( pg_last_error($this->mConn) ) {
106 wfDebugDieBacktrace( "SQL error: " . htmlspecialchars( pg_last_error($this->mConn) ) );
107 }
108 return $row;
109 }
110
111 function numRows( $res ) {
112 @$n = pg_num_rows( $res );
113 if( pg_last_error($this->mConn) ) {
114 wfDebugDieBacktrace( "SQL error: " . htmlspecialchars( pg_last_error($this->mConn) ) );
115 }
116 return $n;
117 }
118 function numFields( $res ) { return pg_num_fields( $res ); }
119 function fieldName( $res, $n ) { return pg_field_name( $res, $n ); }
120
121 # This must be called after nextSequenceVal
122 function insertId() {
123 return $this->mInsertId;
124 }
125
126 function dataSeek( $res, $row ) { return pg_result_seek( $res, $row ); }
127 function lastError() { return pg_last_error(); }
128 function lastErrno() { return 1; }
129
130 function affectedRows() {
131 return pg_affected_rows( $this->mLastResult );
132 }
133
134 # Returns information about an index
135 # If errors are explicitly ignored, returns NULL on failure
136 function indexInfo( $table, $index, $fname = "Database::indexExists" )
137 {
138 $sql = "SELECT indexname FROM pg_indexes WHERE tablename='$table'";
139 $res = $this->query( $sql, $fname );
140 if ( !$res ) {
141 return NULL;
142 }
143
144 while ( $row = $this->fetchObject( $res ) ) {
145 if ( $row->Key_name == $index ) {
146 return $row;
147 }
148 }
149 return false;
150 }
151
152 function fieldInfo( $table, $field )
153 {
154 wfDebugDieBacktrace( "Database::fieldInfo() error : mysql_fetch_field() not implemented for postgre" );
155 /*
156 $res = $this->query( "SELECT * FROM '$table' LIMIT 1" );
157 $n = pg_num_fields( $res );
158 for( $i = 0; $i < $n; $i++ ) {
159 // FIXME
160 wfDebugDieBacktrace( "Database::fieldInfo() error : mysql_fetch_field() not implemented for postgre" );
161 $meta = mysql_fetch_field( $res, $i );
162 if( $field == $meta->name ) {
163 return $meta;
164 }
165 }
166 return false;*/
167 }
168
169 function insertArray( $table, $a, $fname = "Database::insertArray", $options = array() ) {
170 # PostgreSQL doesn't support options
171 # We have a go at faking one of them
172 # TODO: DELAYED, LOW_PRIORITY
173
174 # IGNORE is performed using single-row inserts, ignoring errors in each
175 if ( in_array( 'IGNORE', $options ) ) {
176 # FIXME: need some way to distiguish between key collision and other types of error
177 $oldIgnore = $this->ignoreErrors( true );
178 if ( !is_array( reset( $a ) ) ) {
179 $a = array( $a );
180 }
181 foreach ( $a as $row ) {
182 parent::insertArray( $table, $row, $fname, array() );
183 }
184 $this->ignoreErrors( $oldIgnore );
185 $retVal = true;
186 } else {
187 $retVal = parent::insertArray( $table, $a, $fname, array() );
188 }
189 return $retVal;
190 }
191
192 function startTimer( $timeout )
193 {
194 global $IP;
195 wfDebugDieBacktrace( "Database::startTimer() error : mysql_thread_id() not implemented for postgre" );
196 /*$tid = mysql_thread_id( $this->mConn );
197 exec( "php $IP/killthread.php $timeout $tid &>/dev/null &" );*/
198 }
199
200 function tableName( $name ) {
201 # First run any transformations from the parent object
202 $name = parent::tableName( $name );
203
204 # Now quote PG reserved keywords
205 switch( $name ) {
206 case 'user':
207 return '"user"';
208 case 'old':
209 return '"old"';
210 default:
211 return $name;
212 }
213 }
214
215 function strencode( $s ) {
216 return pg_escape_string( $s );
217 }
218
219 # Return the next in a sequence, save the value for retrieval via insertId()
220 function nextSequenceValue( $seqName ) {
221 $value = $this->getField(""," nextval('" . $seqName . "')");
222 $this->mInsertId = $value;
223 return $value;
224 }
225
226 # USE INDEX clause
227 # PostgreSQL doesn't have them and returns ""
228 function useIndexClause( $index ) {
229 return '';
230 }
231
232 # REPLACE query wrapper
233 # PostgreSQL simulates this with a DELETE followed by INSERT
234 # $row is the row to insert, an associative array
235 # $uniqueIndexes is an array of indexes. Each element may be either a
236 # field name or an array of field names
237 #
238 # It may be more efficient to leave off unique indexes which are unlikely to collide.
239 # However if you do this, you run the risk of encountering errors which wouldn't have
240 # occurred in MySQL
241 function replace( $table, $uniqueIndexes, $rows, $fname = "Database::replace" ) {
242 $table = $this->tableName( $table );
243
244 # Single row case
245 if ( !is_array( reset( $rows ) ) ) {
246 $rows = array( $rows );
247 }
248
249 foreach( $rows as $row ) {
250 # Delete rows which collide
251 if ( $uniqueIndexes ) {
252 $sql = "DELETE FROM $table WHERE (";
253 $first = true;
254 foreach ( $uniqueIndexes as $index ) {
255 if ( $first ) {
256 $first = false;
257 } else {
258 $sql .= ") OR (";
259 }
260 if ( is_array( $index ) ) {
261 $first2 = true;
262 $sql .= "(";
263 foreach ( $index as $col ) {
264 if ( $first2 ) {
265 $first2 = false;
266 } else {
267 $sql .= " AND ";
268 }
269 $sql .= "$col=" . $this->addQuotes( $row[$col] );
270 }
271 } else {
272 $sql .= "$index=" . $this->addQuotes( $row[$index] );
273 }
274 }
275 $sql .= ")";
276 $this->query( $sql, $fname );
277 }
278
279 # Now insert the row
280 $sql = "INSERT INTO $table (" . $this->makeList( array_flip( $row ) ) .') VALUES (' .
281 $this->makeList( $row, LIST_COMMA ) . ')';
282 $this->query( $sql, $fname );
283 }
284 }
285
286 # DELETE where the condition is a join
287 function deleteJoin( $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = "Database::deleteJoin" ) {
288 if ( !$conds ) {
289 wfDebugDieBacktrace( 'Database::deleteJoin() called with empty $conds' );
290 }
291
292 $delTable = $this->tableName( $delTable );
293 $joinTable = $this->tableName( $joinTable );
294 $sql = "DELETE FROM $delTable WHERE $delVar IN (SELECT $joinVar FROM $joinTable ";
295 if ( $conds != '*' ) {
296 $sql .= "WHERE " . $this->makeList( $conds, LIST_AND );
297 }
298 $sql .= ")";
299
300 $this->query( $sql, $fname );
301 }
302
303 # Returns the size of a text field, or -1 for "unlimited"
304 function textFieldSize( $table, $field ) {
305 $table = $this->tableName( $table );
306 $res = $this->query( "SELECT $field FROM $table LIMIT 1", "Database::textFieldLength" );
307 $size = pg_field_size( $res, 0 );
308 $this->freeResult( $res );
309 return $size;
310 }
311
312 function lowPriorityOption() {
313 return '';
314 }
315
316 function limitResult($limit,$offset) {
317 return " LIMIT $limit ".(is_numeric($offset)?" OFFSET {$offset} ":"");
318 }
319
320 # FIXME: actually detecting deadlocks might be nice
321 function wasDeadlock() {
322 return false;
323 }
324 }
325
326 # Just an alias.
327 class DatabasePostgreSQL extends DatabasePgsql {
328 }
329
330 ?>