db.class.php
gehe zur Dokumentation dieser Datei00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00033 class DB
00034 {
00040 var $id;
00041
00047 var $conf;
00048
00054 var $available;
00055
00061 var $error;
00062
00068 var $client;
00069
00070
00078 function DB( $conf )
00079 {
00080 $this->available = false;
00081 $this->conf = $conf;
00082
00083 $this->connect();
00084
00085 return $this->available;
00086 }
00087
00088
00094 function connect()
00095 {
00096
00097 if ( !empty($this->conf['cmd']))
00098 {
00099 $ausgabe = array();
00100 $rc = false;
00101
00102 Logger::debug("Database command executing: ".$this->conf['cmd']);
00103 exec( $this->conf['cmd'],$ausgabe,$rc );
00104
00105 foreach( $ausgabe as $zeile )
00106 Logger::debug("Database command output: ".$zeile);
00107
00108 if ( $rc != 0 )
00109 {
00110 $this->error = 'Command failed: '.implode("",$ausgabe);
00111 $this->available = false;
00112 return false;
00113 }
00114 }
00115
00116 $type = $this->conf['type'];
00117 $classname = 'db_'.$type;
00118
00119 $this->client = & new $classname;
00120
00121 $ok = $this->client->connect( $this->conf );
00122
00123 if ( ! $ok )
00124 {
00125 $this->error = $this->client->error;
00126 $this->available = false;
00127 return false;
00128
00129
00130 }
00131
00132 $this->available = true;
00133 return true;
00134 }
00135
00136
00143 function query( $query )
00144 {
00145 Logger::trace('DB query: '.substr($query,0,45).'...');
00146
00147 $result = $this->client->query($query);
00148
00149 if ( $result === FALSE )
00150 {
00151 $this->error = $this->client->error;
00152
00153 if ( true )
00154 {
00155 Logger::warn('Database error: '.$this->error);
00156 die('Database Error:<pre style="color:red">'.$this->error.'</pre>');
00157 }
00158 }
00159
00160 return new DB_result( $this->client,$result );
00161 }
00162
00163
00169 function affectedRows()
00170 {
00171 return $this->client->affectedRows( $query );
00172 }
00173
00174
00182 function &getOne( $query )
00183 {
00184 $res = $this->query($query);
00185
00186 if ( $res->numRows() > 0 )
00187 {
00188 $row = $res->fetchRow( 0 );
00189 $res->free();
00190
00191 $keys = array_keys($row);
00192
00193 return $row[ $keys[0] ];
00194 }
00195 else
00196 {
00197 $res->free();
00198 $leer = '';
00199 return $leer;
00200 }
00201 }
00202
00203
00210 function &getRow( $query )
00211 {
00212 $res = $this->query($query);
00213
00214 if ( $res->numRows() > 0 )
00215 $row = $res->fetchRow( 0 );
00216 else
00217 $row = array();
00218
00219 $res->free();
00220
00221 return $row;
00222 }
00223
00224
00231 function &getCol( $query )
00232 {
00233 $res = $this->query( $query );
00234
00235 $ret = array();
00236
00237 $numRows = $res->numRows();
00238
00239 for( $i=0; $i<$numRows; $i++ )
00240 {
00241 $row = $res->fetchRow($i);
00242
00243 $keys = array_keys($row);
00244 $ret[] = $row[ $keys[0] ];
00245 }
00246
00247 $res->free();
00248
00249 return $ret;
00250 }
00251
00252
00260 function &getAssoc( $query, $force_array = false )
00261 {
00262 $res = $this->query($query);
00263
00264 $numCols = $res->numCols();
00265 $numRows = $res->numRows();
00266
00267 $results = array();
00268
00269 if ( $numCols > 2 || $force_array )
00270 {
00271 for( $i=0; $i<$numRows; $i++ )
00272 {
00273 $row = $res->fetchRow($i);
00274
00275 $keys = array_keys($row);
00276 $key1 = $keys[0];
00277
00278 unset( $row[$key1] );
00279 $results[ $row[$key1] ] = $row;
00280 }
00281 }
00282 else
00283 {
00284 for( $i=0; $i<$numRows; $i++ )
00285 {
00286 $row = $res->fetchRow($i);
00287
00288 $keys = array_keys($row);
00289 $key1 = $keys[0];
00290 $key2 = $keys[1];
00291
00292 $results[ $row[$key1] ] = $row[$key2];
00293 }
00294 }
00295
00296 $res->free();
00297
00298 return $results;
00299 }
00300
00301
00308 function &getAll( $query )
00309 {
00310 $res = $this->query( $query );
00311
00312 $results = array();
00313 $numRows = $res->numRows();
00314
00315 for( $i=0; $i<$numRows; $i++ )
00316 {
00317 $row = $res->fetchRow($i);
00318 $results[] = $row;
00319 }
00320
00321 $res->free();
00322
00323 return $results;
00324 }
00325 }
00326
00327
00328
00329
00338 class DB_result
00339 {
00340 var $client;
00341 var $result;
00342
00343
00344 function DB_result( $client, $result )
00345 {
00346 $this->client = $client;
00347 $this->result = $result;
00348 }
00349
00350
00351 function fetchRow( $rownum = 0 )
00352 {
00353 $arr = $this->client->fetchRow( $this->result, $rownum );
00354
00355 return $arr;
00356 }
00357
00358
00359 function numCols()
00360 {
00361 return $this->client->numCols($this->result);
00362 }
00363
00364
00365 function numRows()
00366 {
00367 return $this->client->numRows( $this->result );
00368 }
00369
00370
00371 function free()
00372 {
00373 $err = $this->client->freeResult($this->result);
00374 return true;
00375 }
00376 }
00377
00378
00379 ?>