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