object-oriented database connections
[lhc/web/wiklou.git] / includes / DatabaseFunctions.php
1 <?
2
3 # Backwards compatibility wrapper for Database.php
4
5 # NB: This file follows a connect on demand scheme. Do
6 # not access the $wgDatabase variable directly, use wfGetDB()
7
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
15
16 include_once( "Database.php" );
17
18 function wfQuery( $sql, $db, $fname = "" )
19 {
20 global $wgDatabase, $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname,
21 $wgDebugDumpSql, $wgBufferSQLResults, $wgIgnoreSQLErrors;
22
23 if ( !is_numeric( $db ) ) {
24 # Someone has tried to call this the old way
25 $wgOut->fatalError( wfMsgNoDB( "wrong_wfQuery_params", $db, $sql ) );
26 }
27
28 $db = wfGetDB();
29 return $db->query( $sql, $db, $fname );
30 }
31
32 # Connect on demand
33 function wfGetDB()
34 {
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 );
40 }
41 return $wgDatabase;
42 }
43
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.
47
48 function wfBufferSQLResults( $newstate )
49 {
50 $db = wfGetDB();
51 return $db->setBufferResults( $newstate );
52 }
53
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.
60
61 function wfIgnoreSQLErrors( $newstate )
62 {
63 $db = wfGetDB();
64 return $db->setIgnoreErrors( $newstate );
65 }
66
67 function wfFreeResult( $res )
68 {
69 $db = wfGetDB();
70 $db->freeResult( $res );
71 }
72
73 function wfFetchObject( $res )
74 {
75 $db = wfGetDB();
76 return $db->fetchObject( $res );
77 }
78
79 function wfNumRows( $res )
80 {
81 $db = wfGetDB();
82 return $db->numRows( $res );
83 }
84
85 function wfNumFields( $res )
86 {
87 $db = wfGetDB();
88 return $db->numFields( $res );
89 }
90
91 function wfFieldName( $res, $n )
92 {
93 $db = wfGetDB();
94 return $db->fieldName( $res, $n );
95 }
96
97 function wfInsertId()
98 {
99 $db = wfGetDB();
100 return $db->insertId();
101 }
102 function wfDataSeek( $res, $row )
103 {
104 $db = wfGetDB();
105 return $db->dataSeek( $res, $row );
106 }
107
108 function wfLastErrno()
109 {
110 $db = wfGetDB();
111 return $db->lastErrno();
112 }
113
114 function wfLastError()
115 {
116 $db = wfGetDB();
117 return $db->lastError();
118 }
119
120 function wfAffectedRows()
121 {
122 $db = wfGetDB();
123 return $db->affectedRows();
124 }
125
126 function wfLastDBquery()
127 {
128 $db = wfGetDB();
129 return $db->lastQuery();
130 }
131
132 function wfSetSQL( $table, $var, $value, $cond )
133 {
134 $db = wfGetDB();
135 return $db->set( $table, $var, $value, $cond );
136 }
137
138 function wfGetSQL( $table, $var, $cond )
139 {
140 $db = wfGetDB();
141 return $db->get( $table, $var, $cond );
142 }
143
144 function wfFieldExists( $table, $field )
145 {
146 $db = wfGetDB();
147 return $db->fieldExists( $table, $field );
148 }
149
150 function wfIndexExists( $table, $index )
151 {
152 $db = wfGetDB();
153 return $wgDatabase->indexExists( $table, $index );
154 }
155
156 ?>