object-oriented database connections
[mediawiki.git] / includes / DatabaseFunctions.php
blobb3208e168b05565d5db0cd283f56e537099e7c1b
1 <?
3 # Backwards compatibility wrapper for Database.php
5 # NB: This file follows a connect on demand scheme. Do
6 # not access the $wgDatabase variable directly, use wfGetDB()
8 # Query the database
9 # $db: DB_READ = -1 read from slave (or only server)
10 # DB_WRITE = -2 write to master (or only server)
11 # 0,1,2,... query a database with a specific index
12 # Replication is not actually implemented just yet
13 # Usually aborts on failure
14 # If errors are explicitly ignored, returns success
16 include_once( "Database.php" );
18 function wfQuery( $sql, $db, $fname = "" )
20 global $wgDatabase, $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname,
21 $wgDebugDumpSql, $wgBufferSQLResults, $wgIgnoreSQLErrors;
23 if ( !is_numeric( $db ) ) {
24 # Someone has tried to call this the old way
25 $wgOut->fatalError( wfMsgNoDB( "wrong_wfQuery_params", $db, $sql ) );
28 $db = wfGetDB();
29 return $db->query( $sql, $db, $fname );
32 # Connect on demand
33 function wfGetDB()
35 global $wgDatabase, $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname,
36 $wgDebugDumpSql, $wgBufferSQLResults, $wgIgnoreSQLErrors;
37 if ( !$wgDatabase ) {
38 $wgDatabase = Database::newFromParams( $wgDBserver, $wgDBuser, $wgDBpassword,
39 $wgDBname, false, $wgDebugDumpSql, $wgBufferSQLResults, $wgIgnoreSQLErrors );
41 return $wgDatabase;
44 # Turns buffering of SQL result sets on (true) or off (false). Default is
45 # "on" and it should not be changed without good reasons.
46 # Returns the previous state.
48 function wfBufferSQLResults( $newstate )
50 $db = wfGetDB();
51 return $db->setBufferResults( $newstate );
54 # Turns on (false) or off (true) the automatic generation and sending
55 # of a "we're sorry, but there has been a database error" page on
56 # database errors. Default is on (false). When turned off, the
57 # code should use wfLastErrno() and wfLastError() to handle the
58 # situation as appropriate.
59 # Returns the previous state.
61 function wfIgnoreSQLErrors( $newstate )
63 $db = wfGetDB();
64 return $db->setIgnoreErrors( $newstate );
67 function wfFreeResult( $res )
69 $db = wfGetDB();
70 $db->freeResult( $res );
73 function wfFetchObject( $res )
75 $db = wfGetDB();
76 return $db->fetchObject( $res );
79 function wfNumRows( $res )
81 $db = wfGetDB();
82 return $db->numRows( $res );
85 function wfNumFields( $res )
87 $db = wfGetDB();
88 return $db->numFields( $res );
91 function wfFieldName( $res, $n )
93 $db = wfGetDB();
94 return $db->fieldName( $res, $n );
97 function wfInsertId()
99 $db = wfGetDB();
100 return $db->insertId();
102 function wfDataSeek( $res, $row )
104 $db = wfGetDB();
105 return $db->dataSeek( $res, $row );
108 function wfLastErrno()
110 $db = wfGetDB();
111 return $db->lastErrno();
114 function wfLastError()
116 $db = wfGetDB();
117 return $db->lastError();
120 function wfAffectedRows()
122 $db = wfGetDB();
123 return $db->affectedRows();
126 function wfLastDBquery()
128 $db = wfGetDB();
129 return $db->lastQuery();
132 function wfSetSQL( $table, $var, $value, $cond )
134 $db = wfGetDB();
135 return $db->set( $table, $var, $value, $cond );
138 function wfGetSQL( $table, $var, $cond )
140 $db = wfGetDB();
141 return $db->get( $table, $var, $cond );
144 function wfFieldExists( $table, $field )
146 $db = wfGetDB();
147 return $db->fieldExists( $table, $field );
150 function wfIndexExists( $table, $index )
152 $db = wfGetDB();
153 return $wgDatabase->indexExists( $table, $index );