allow empty condition for wfGetSQL()
[lhc/web/wiklou.git] / includes / DatabaseFunctions.php
1 <?php
2 # $Id$
3
4 # Backwards compatibility wrapper for Database.php
5
6 # I imagine this file will eventually become a backwards
7 # compatibility wrapper around a load balancer object, and
8 # the load balancer will finally call Database, which will
9 # represent a single connection
10
11 # NB: This file follows a connect on demand scheme. Do
12 # not access the $wgDatabase variable directly unless
13 # you intend to set it. Use wfGetDB().
14
15 $wgIsMySQL=false;
16 $wgIsPg=false;
17
18 if ($wgDBtype=="mysql") {
19 require_once( "Database.php" );
20 $wgIsMySQL=true;
21 } elseif ($wgDBtype=="pgsql") {
22 require_once( "DatabasePostgreSQL.php" );
23 $wgIsPg=true;
24 }
25
26 # Query the database
27 # $db: DB_READ = -1 read from slave (or only server)
28 # DB_WRITE = -2 write to master (or only server)
29 # 0,1,2,... query a database with a specific index
30 # Replication is not actually implemented just yet
31 # Usually aborts on failure
32 # If errors are explicitly ignored, returns success
33 function wfQuery( $sql, $db, $fname = "" )
34 {
35 global $wgDatabase, $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname,
36 $wgDebugDumpSql, $wgBufferSQLResults, $wgIgnoreSQLErrors;
37
38 if ( !is_numeric( $db ) ) {
39 # Someone has tried to call this the old way
40 $wgOut->fatalError( wfMsgNoDB( "wrong_wfQuery_params", $db, $sql ) );
41 }
42
43 $db =& wfGetDB();
44 return $db->query( $sql, $fname );
45 }
46
47 # Connect on demand
48 function &wfGetDB()
49 {
50 global $wgDatabase, $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname,
51 $wgDebugDumpSql, $wgBufferSQLResults, $wgIgnoreSQLErrors;
52 if ( !$wgDatabase ) {
53 $wgDatabase = Database::newFromParams( $wgDBserver, $wgDBuser, $wgDBpassword,
54 $wgDBname, false, $wgDebugDumpSql, $wgBufferSQLResults, $wgIgnoreSQLErrors );
55 }
56 return $wgDatabase;
57 }
58
59 # Turns buffering of SQL result sets on (true) or off (false). Default is
60 # "on" and it should not be changed without good reasons.
61 # Returns the previous state.
62
63 function wfBufferSQLResults( $newstate )
64 {
65 $db =& wfGetDB();
66 return $db->setBufferResults( $newstate );
67 }
68
69 # Turns on (false) or off (true) the automatic generation and sending
70 # of a "we're sorry, but there has been a database error" page on
71 # database errors. Default is on (false). When turned off, the
72 # code should use wfLastErrno() and wfLastError() to handle the
73 # situation as appropriate.
74 # Returns the previous state.
75
76 function wfIgnoreSQLErrors( $newstate )
77 {
78 $db =& wfGetDB();
79 return $db->setIgnoreErrors( $newstate );
80 }
81
82 function wfFreeResult( $res )
83 {
84 $db =& wfGetDB();
85 $db->freeResult( $res );
86 }
87
88 function wfFetchObject( $res )
89 {
90 $db =& wfGetDB();
91 return $db->fetchObject( $res );
92 }
93
94 function wfFetchRow( $res )
95 {
96 $db =& wfGetDB();
97 return $db->fetchRow ( $res );
98 }
99
100 function wfNumRows( $res )
101 {
102 $db =& wfGetDB();
103 return $db->numRows( $res );
104 }
105
106 function wfNumFields( $res )
107 {
108 $db =& wfGetDB();
109 return $db->numFields( $res );
110 }
111
112 function wfFieldName( $res, $n )
113 {
114 $db =& wfGetDB();
115 return $db->fieldName( $res, $n );
116 }
117
118 function wfInsertId()
119 {
120 $db =& wfGetDB();
121 return $db->insertId();
122 }
123 function wfDataSeek( $res, $row )
124 {
125 $db =& wfGetDB();
126 return $db->dataSeek( $res, $row );
127 }
128
129 function wfLastErrno()
130 {
131 $db =& wfGetDB();
132 return $db->lastErrno();
133 }
134
135 function wfLastError()
136 {
137 $db =& wfGetDB();
138 return $db->lastError();
139 }
140
141 function wfAffectedRows()
142 {
143 $db =& wfGetDB();
144 return $db->affectedRows();
145 }
146
147 function wfLastDBquery()
148 {
149 $db =& wfGetDB();
150 return $db->lastQuery();
151 }
152
153 function wfSetSQL( $table, $var, $value, $cond )
154 {
155 $db =& wfGetDB();
156 return $db->set( $table, $var, $value, $cond );
157 }
158
159 function wfGetSQL( $table, $var, $cond="" )
160 {
161 $db =& wfGetDB();
162 return $db->get( $table, $var, $cond );
163 }
164
165 function wfFieldExists( $table, $field )
166 {
167 $db =& wfGetDB();
168 return $db->fieldExists( $table, $field );
169 }
170
171 function wfIndexExists( $table, $index )
172 {
173 $db =& wfGetDB();
174 return $db->indexExists( $table, $index );
175 }
176
177 function wfInsertArray( $table, $array )
178 {
179 $db =& wfGetDB();
180 return $db->insertArray( $table, $array );
181 }
182
183 function wfGetArray( $table, $vars, $conds, $fname = "wfGetArray" )
184 {
185 $db =& wfGetDB();
186 return $db->getArray( $table, $vars, $conds, $fname );
187 }
188
189 function wfUpdateArray( $table, $values, $conds, $fname = "wfUpdateArray" )
190 {
191 $db =& wfGetDB();
192 $db->updateArray( $table, $values, $conds, $fname );
193 }
194
195 ?>