3 * Legacy database functions, for compatibility with pre-1.3 code
4 * NOTE: this file is no longer loaded by default.
10 * Usually aborts on failure
11 * If errors are explicitly ignored, returns success
12 * @param $sql String: SQL query
13 * @param $db Mixed: database handler
14 * @param $fname String: name of the php function calling
16 function wfQuery( $sql, $db, $fname = '' ) {
17 if ( !is_numeric( $db ) ) {
18 # Someone has tried to call this the old way
19 throw new FatalError( wfMsgNoDB( 'wrong_wfQuery_params', $db, $sql ) );
23 return $c->query( $sql, $fname );
31 * @param $sql String: SQL query
33 * @param $fname String: name of the php function calling
34 * @return Array: first row from the database
36 function wfSingleQuery( $sql, $dbi, $fname = '' ) {
37 $db = wfGetDB( $dbi );
38 $res = $db->query($sql, $fname );
39 $row = $db->fetchRow( $res );
41 $db->freeResult( $res );
46 * Turns on (false) or off (true) the automatic generation and sending
47 * of a "we're sorry, but there has been a database error" page on
48 * database errors. Default is on (false). When turned off, the
49 * code should use wfLastErrno() and wfLastError() to handle the
50 * situation as appropriate.
54 * @return Returns the previous state.
56 function wfIgnoreSQLErrors( $newstate, $dbi = DB_LAST
) {
57 $db = wfGetDB( $dbi );
58 if ( $db !== false ) {
59 return $db->ignoreErrors( $newstate );
66 * @param $res Database result handler
71 * Free a database result
72 * @return Bool: whether result is sucessful or not.
74 function wfFreeResult( $res, $dbi = DB_LAST
)
76 $db = wfGetDB( $dbi );
77 if ( $db !== false ) {
78 $db->freeResult( $res );
86 * Get an object from a database result
87 * @return object|false object we requested
89 function wfFetchObject( $res, $dbi = DB_LAST
) {
90 $db = wfGetDB( $dbi );
91 if ( $db !== false ) {
92 return $db->fetchObject( $res, $dbi = DB_LAST
);
99 * Get a row from a database result
100 * @return object|false row we requested
102 function wfFetchRow( $res, $dbi = DB_LAST
) {
103 $db = wfGetDB( $dbi );
104 if ( $db !== false ) {
105 return $db->fetchRow ( $res, $dbi = DB_LAST
);
112 * Get a number of rows from a database result
113 * @return integer|false number of rows
115 function wfNumRows( $res, $dbi = DB_LAST
) {
116 $db = wfGetDB( $dbi );
117 if ( $db !== false ) {
118 return $db->numRows( $res, $dbi = DB_LAST
);
125 * Get the number of fields from a database result
126 * @return integer|false number of fields
128 function wfNumFields( $res, $dbi = DB_LAST
) {
129 $db = wfGetDB( $dbi );
130 if ( $db !== false ) {
131 return $db->numFields( $res );
138 * Return name of a field in a result
139 * @param $res Mixed: Ressource link see Database::fieldName()
140 * @param $n Integer: id of the field
141 * @param $dbi Default DB_LAST
142 * @return string|false name of field
144 function wfFieldName( $res, $n, $dbi = DB_LAST
)
146 $db = wfGetDB( $dbi );
147 if ( $db !== false ) {
148 return $db->fieldName( $res, $n, $dbi = DB_LAST
);
156 * @todo document function
157 * @see Database::insertId()
159 function wfInsertId( $dbi = DB_LAST
) {
160 $db = wfGetDB( $dbi );
161 if ( $db !== false ) {
162 return $db->insertId();
169 * @todo document function
170 * @see Database::dataSeek()
172 function wfDataSeek( $res, $row, $dbi = DB_LAST
) {
173 $db = wfGetDB( $dbi );
174 if ( $db !== false ) {
175 return $db->dataSeek( $res, $row );
182 * Get the last error number
183 * @see Database::lastErrno()
185 function wfLastErrno( $dbi = DB_LAST
) {
186 $db = wfGetDB( $dbi );
187 if ( $db !== false ) {
188 return $db->lastErrno();
196 * @see Database::lastError()
198 function wfLastError( $dbi = DB_LAST
) {
199 $db = wfGetDB( $dbi );
200 if ( $db !== false ) {
201 return $db->lastError();
208 * Get the number of affected rows
209 * @see Database::affectedRows()
211 function wfAffectedRows( $dbi = DB_LAST
) {
212 $db = wfGetDB( $dbi );
213 if ( $db !== false ) {
214 return $db->affectedRows();
221 * Get the last query ran
222 * @see Database::lastQuery
224 function wfLastDBquery( $dbi = DB_LAST
) {
225 $db = wfGetDB( $dbi );
226 if ( $db !== false ) {
227 return $db->lastQuery();
234 * @see Database::Set()
235 * @todo document function
240 * @param $dbi Default DB_MASTER
242 function wfSetSQL( $table, $var, $value, $cond, $dbi = DB_MASTER
)
244 $db = wfGetDB( $dbi );
245 if ( $db !== false ) {
246 return $db->set( $table, $var, $value, $cond );
254 * Simple select wrapper, return one field
255 * @see Database::selectField()
258 * @param $cond Default ''
259 * @param $dbi Default DB_LAST
261 function wfGetSQL( $table, $var, $cond='', $dbi = DB_LAST
)
263 $db = wfGetDB( $dbi );
264 if ( $db !== false ) {
265 return $db->selectField( $table, $var, $cond );
272 * Does a given field exist on the specified table?
273 * @see Database::fieldExists()
276 * @param $dbi Default DB_LAST
277 * @return Result of Database::fieldExists() or false.
279 function wfFieldExists( $table, $field, $dbi = DB_LAST
) {
280 $db = wfGetDB( $dbi );
281 if ( $db !== false ) {
282 return $db->fieldExists( $table, $field );
289 * Does the requested index exist on the specified table?
290 * @see Database::indexExists()
291 * @param $table String
293 * @param $dbi Default DB_LAST
294 * @return Result of Database::indexExists() or false.
296 function wfIndexExists( $table, $index, $dbi = DB_LAST
) {
297 $db = wfGetDB( $dbi );
298 if ( $db !== false ) {
299 return $db->indexExists( $table, $index );
306 * @see Database::insert()
307 * @todo document function
308 * @param $table String
309 * @param $array Array
310 * @param $fname String, default 'wfInsertArray'.
311 * @param $dbi Default DB_MASTER
312 * @return result of Database::insert() or false.
314 function wfInsertArray( $table, $array, $fname = 'wfInsertArray', $dbi = DB_MASTER
) {
315 $db = wfGetDB( $dbi );
316 if ( $db !== false ) {
317 return $db->insert( $table, $array, $fname );
324 * @see Database::getArray()
325 * @todo document function
326 * @param $table String
329 * @param $fname String, default 'wfGetArray'.
330 * @param $dbi Default DB_LAST
331 * @return result of Database::getArray() or false.
333 function wfGetArray( $table, $vars, $conds, $fname = 'wfGetArray', $dbi = DB_LAST
) {
334 $db = wfGetDB( $dbi );
335 if ( $db !== false ) {
336 return $db->getArray( $table, $vars, $conds, $fname );
343 * @see Database::update()
344 * @param $table String
347 * @param $fname String, default 'wfUpdateArray'
348 * @param $dbi Default DB_MASTER
349 * @return Result of Database::update()) or false;
350 * @todo document function
352 function wfUpdateArray( $table, $values, $conds, $fname = 'wfUpdateArray', $dbi = DB_MASTER
) {
353 $db = wfGetDB( $dbi );
354 if ( $db !== false ) {
355 $db->update( $table, $values, $conds, $fname );
363 * Get fully usable table name
364 * @see Database::tableName()
366 function wfTableName( $name, $dbi = DB_LAST
) {
367 $db = wfGetDB( $dbi );
368 if ( $db !== false ) {
369 return $db->tableName( $name );
376 * @todo document function
377 * @see Database::strencode()
379 function wfStrencode( $s, $dbi = DB_LAST
) {
380 $db = wfGetDB( $dbi );
381 if ( $db !== false ) {
382 return $db->strencode( $s );
389 * @todo document function
390 * @see Database::nextSequenceValue()
392 function wfNextSequenceValue( $seqName, $dbi = DB_MASTER
) {
393 $db = wfGetDB( $dbi );
394 if ( $db !== false ) {
395 return $db->nextSequenceValue( $seqName );
402 * @todo document function
403 * @see Database::useIndexClause()
405 function wfUseIndexClause( $index, $dbi = DB_SLAVE
) {
406 $db = wfGetDB( $dbi );
407 if ( $db !== false ) {
408 return $db->useIndexClause( $index );